Slither.Store.Server (Slither v0.1.0)

Copy Markdown View Source

GenServer owning ETS tables for a Store implementation.

Tables are :protected — any BEAM process can read directly via ETS, but only this GenServer can write. This gives lock-free concurrent reads with serialized write consistency.

Summary

Functions

Bulk insert an enumerable of records into a table. Serialized.

Returns a specification to start this module under a supervisor.

Delete a key from a table. Serialized.

Fold over all entries in a table. Lock-free.

Read a value from a store's table. Lock-free, no GenServer call.

Read a value, raising if not found.

Match objects in a table. Lock-free.

Insert a key-value pair into a table. Serialized.

Reload all tables by re-running the store's load/1 callback. Creates new tables, loads data, then atomically swaps pointers.

Select from a table using a match spec. Lock-free.

Get the number of entries in a table. Lock-free.

Atomically read-modify-write a value. Serialized.

Functions

bulk_insert(server, table, records)

@spec bulk_insert(GenServer.server(), atom(), Enumerable.t()) :: :ok

Bulk insert an enumerable of records into a table. Serialized.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

delete(server, table, key)

@spec delete(GenServer.server(), atom(), term()) :: :ok

Delete a key from a table. Serialized.

fold(store_mod, table, acc, fun)

@spec fold(module(), atom(), acc, (tuple(), acc -> acc)) :: acc when acc: term()

Fold over all entries in a table. Lock-free.

get(store_mod, table, key)

@spec get(module(), atom(), term()) :: term() | nil

Read a value from a store's table. Lock-free, no GenServer call.

get!(store_mod, table, key)

@spec get!(module(), atom(), term()) :: term()

Read a value, raising if not found.

match(store_mod, table, pattern)

@spec match(module(), atom(), term()) :: [tuple()]

Match objects in a table. Lock-free.

put(server, table, key, value)

@spec put(GenServer.server(), atom(), term(), term()) :: :ok

Insert a key-value pair into a table. Serialized.

reload(server)

@spec reload(GenServer.server()) :: :ok | {:error, term()}

Reload all tables by re-running the store's load/1 callback. Creates new tables, loads data, then atomically swaps pointers.

select(store_mod, table, match_spec)

@spec select(module(), atom(), :ets.match_spec()) :: [term()]

Select from a table using a match spec. Lock-free.

size(store_mod, table)

@spec size(module(), atom()) :: non_neg_integer()

Get the number of entries in a table. Lock-free.

start_link(opts)

update(server, table, key, fun)

@spec update(GenServer.server(), atom(), term(), (term() -> term())) ::
  {:ok, term()} | {:error, :not_found}

Atomically read-modify-write a value. Serialized.