MST.Store behaviour
(mst v0.1.0)
View Source
Behaviour for MST node block stores.
A store holds a mapping from DASL.CID to decoded MST.Node structs. All
implementations must satisfy these callbacks. The store state is an opaque
term managed by the implementation module.
The primary built-in implementation is MST.Store.Memory, a simple
map-backed store suitable for in-memory use and tests.
Usage
An MST.Tree holds its store as a {module, state} pair. You interact with
the store through the tree API; direct store access is only needed when
building trees from external data (e.g., importing a CAR file).
store = MST.Store.Memory.new()
tree = MST.Tree.new(store)
Summary
Callbacks
Returns all CIDs present in the store.
Retrieves a node by CID. Returns {:ok, node} or {:error, :not_found}.
Returns true if the store contains a node for the given CID.
Stores a node under its CID. Returns updated state.
Functions
Returns all CIDs present in the store.
Retrieves a node from the store.
Returns true if the store contains a node for the given CID.
Stores a node in the store, returning the updated {module, state} pair.
Types
Callbacks
@callback cids(state :: any()) :: [DASL.CID.t()]
Returns all CIDs present in the store.
@callback get(state :: any(), DASL.CID.t()) :: {:ok, MST.Node.t()} | {:error, :not_found}
Retrieves a node by CID. Returns {:ok, node} or {:error, :not_found}.
@callback has?(state :: any(), DASL.CID.t()) :: boolean()
Returns true if the store contains a node for the given CID.
@callback put(state :: any(), DASL.CID.t(), MST.Node.t()) :: any()
Stores a node under its CID. Returns updated state.
Functions
@spec cids(t()) :: [DASL.CID.t()]
Returns all CIDs present in the store.
@spec get(t(), DASL.CID.t()) :: {:ok, MST.Node.t()} | {:error, :not_found}
Retrieves a node from the store.
Examples
iex> store = MST.Store.Memory.new()
iex> cid = DASL.CID.compute("test", :drisl)
iex> MST.Store.get(store, cid)
{:error, :not_found}
@spec has?(t(), DASL.CID.t()) :: boolean()
Returns true if the store contains a node for the given CID.
@spec put(t(), DASL.CID.t(), MST.Node.t()) :: t()
Stores a node in the store, returning the updated {module, state} pair.
Examples
iex> store = MST.Store.Memory.new()
iex> node = MST.Node.empty()
iex> {:ok, cid} = MST.Node.cid(node)
iex> store2 = MST.Store.put(store, cid, node)
iex> {:ok, _} = MST.Store.get(store2, cid)
iex> :ok
:ok