Grove.Storage behaviour (Grove v0.1.1)
View SourceBehaviour for CRDT storage backends.
Provides an abstraction layer for persisting CRDTs to various storage backends (in-memory, disk, database, etc.).
Implementations
Grove.Storage.ETS- Fast in-memory storage (lost on restart)Grove.Storage.DETS- Disk-based persistence (survives restart)
Usage
# Initialize a storage backend
{:ok, ref} = Grove.Storage.ETS.init(table: :my_crdts)
# Save a CRDT state
:ok = Grove.Storage.ETS.save(ref, {:counter, "views"}, crdt_state)
# Load a CRDT state
{:ok, crdt_state} = Grove.Storage.ETS.load(ref, {:counter, "views"})Integration with Replication.Server
Storage can be configured when starting a replication server:
Grove.Replication.Server.start_link(
crdt_module: Grove.Counter.GCounter,
actor: :node_a,
group: {:counter, "views"},
storage: Grove.Storage.ETS,
storage_opts: [table: :grove_storage],
snapshot_interval: 30_000
)
Summary
Callbacks
Closes/cleans up the storage backend.
Deletes the stored state for a replication group.
Initializes the storage backend.
Lists all stored replication groups.
Loads the CRDT state for a replication group.
Saves the full CRDT state for a replication group.
Types
Callbacks
@callback close(storage_ref()) :: :ok | error()
Closes/cleans up the storage backend.
@callback delete(storage_ref(), group()) :: :ok | error()
Deletes the stored state for a replication group.
@callback init(opts()) :: {:ok, storage_ref()} | error()
Initializes the storage backend.
Returns a storage reference that will be passed to all subsequent calls. Options are backend-specific.
Examples
{:ok, ref} = Grove.Storage.ETS.init(table: :my_crdts)
{:ok, ref} = Grove.Storage.DETS.init(file: "/tmp/crdts.dets")
@callback list_groups(storage_ref()) :: {:ok, [group()]} | error()
Lists all stored replication groups.
Useful for recovery scenarios where you need to restart all previously active CRDTs.
@callback load(storage_ref(), group()) :: {:ok, crdt_state() | nil} | error()
Loads the CRDT state for a replication group.
Returns {:ok, nil} if the group has no saved state.
@callback save(storage_ref(), group(), crdt_state()) :: :ok | error()
Saves the full CRDT state for a replication group.
This creates a snapshot of the complete CRDT state.