Ferricstore.Store.TypeRegistry (ferricstore v0.3.6)

Copy Markdown View Source

Manages type metadata for Redis keys that hold data structures.

When a key is first used as a hash, list, set, or sorted set, a type metadata entry is written to the same Bitcask shard:

T:keyname -> "hash" | "list" | "set" | "zset"

Subsequent commands check this metadata and return a WRONGTYPE error if the command's expected type does not match. String keys do NOT get a type entry -- only data structure keys do. This matches Redis behavior where string operations on a data structure key return WRONGTYPE.

Type Enforcement

The check_or_set/3 function either:

  1. Sets the type if the key has no type yet (first use)
  2. Returns :ok if the key already has the expected type
  3. Returns {:error, wrongtype_message} if the type mismatches

Summary

Functions

Checks that redis_key has the expected type, or sets it if the key has no type metadata yet.

Checks that a key either does not exist or has the expected type, WITHOUT setting the type if it doesn't exist. Used for read-only operations that should not create keys.

Removes the type metadata for a Redis key.

Returns the type of a Redis key, or nil if no type metadata exists.

Functions

check_or_set(redis_key, type, store)

@spec check_or_set(binary(), Ferricstore.Store.CompoundKey.data_type(), map()) ::
  :ok | {:error, binary()}

Checks that redis_key has the expected type, or sets it if the key has no type metadata yet.

Parameters

  • redis_key - the Redis key to check
  • type - the expected data type (:hash, :list, :set, :zset)
  • store - the store (Instance, LocalTxStore, or closure map)

Returns

  • :ok if the type matches or was newly set
  • {:error, wrongtype_message} if the type mismatches

check_type(redis_key, type, store)

@spec check_type(binary(), Ferricstore.Store.CompoundKey.data_type(), map()) ::
  :ok | {:error, binary()}

Checks that a key either does not exist or has the expected type, WITHOUT setting the type if it doesn't exist. Used for read-only operations that should not create keys.

Returns

  • :ok if the key doesn't exist or has the expected type
  • {:error, wrongtype_message} if the type mismatches

delete_type(redis_key, store)

@spec delete_type(binary(), map()) :: :ok

Removes the type metadata for a Redis key.

Called when DEL removes a data structure key.

Parameters

  • redis_key - the Redis key whose type to remove
  • store - the store (Instance, LocalTxStore, or closure map)

get_type(redis_key, store)

@spec get_type(binary(), map()) :: binary()

Returns the type of a Redis key, or nil if no type metadata exists.

Used by the TYPE command.

Parameters

  • redis_key - the Redis key to look up
  • store - the store (Instance, LocalTxStore, or closure map)

Returns

  • "hash", "list", "set", "zset", "string", or "none"