Grove.Storage behaviour (Grove v0.1.1)

View Source

Behaviour for CRDT storage backends.

Provides an abstraction layer for persisting CRDTs to various storage backends (in-memory, disk, database, etc.).

Implementations

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

crdt_state()

@type crdt_state() :: term()

error()

@type error() :: {:error, term()}

group()

@type group() :: term()

opts()

@type opts() :: keyword()

storage_ref()

@type storage_ref() :: term()

Callbacks

close(storage_ref)

(optional)
@callback close(storage_ref()) :: :ok | error()

Closes/cleans up the storage backend.

delete(storage_ref, group)

@callback delete(storage_ref(), group()) :: :ok | error()

Deletes the stored state for a replication group.

init(opts)

@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")

list_groups(storage_ref)

(optional)
@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.

load(storage_ref, group)

@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.

save(storage_ref, group, crdt_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.