Spectre.Server.Store behaviour (Spectre v0.4.0)

Copy Markdown View Source

AgentServer storage with compare-and-swap coordination and optional GAM storage.

A successful mutation acknowledges a durable, atomic acquisition of the entire value. Implementations must reject nonportable values and distinguish an absent key from an unavailable or corrupt store. A checkpoint adapter can acquire its inbox, state and command outbox as one value.

A provider can implement this one behaviour for coordination, canonical GAM history and its disposable persisted read model. Configure it with gam: [storage: :ledger, store: {MyStorage, reference}]. The same opaque reference is passed first to every callback. The host owns that reference and its lifecycle; selecting storage never starts another provider or database.

The unified history group consists of ledger_append/6, ledger_load/3, ledger_head/3, ledger_read_batch/4, ledger_lookup_batch/4 and ledger_export/3. ledger_load_from/4 is an optional optimization. Implement all six projection_* callbacks to persist the read model, or omit the whole projection group to keep it in memory. Incomplete groups fail before Managed bootstrap writes its authority profile.

These callbacks retain the contracts of Spectre.GAM.Ledger.Store and Spectre.GAM.Domain.Projection.Store: atomic idempotent appends, bounded indexed batch reads, generation-fenced projection changes and exact-cursor reads. An uncertain append acknowledgement is {:error, :ambiguous}. A projection is rebuilt from verified history and never supplies authority. Sharing a provider does not promise one transaction spanning the checkpoint, history and projection. Each resource keeps its own durability barrier.

Existing gam_domain/1 and gam_ledger/1 adapters remain supported. gam_domain/1 takes precedence; when absent or :not_configured, a unified callback group is selected before the history-only gam_ledger/1 fallback.

Summary

Callbacks

Supplies the Domain's canonical history and disposable persisted read model.

Optionally supplies the canonical GAM ledger used by Managed storage: :ledger.

Atomically appends one idempotent history batch after the expected revision.

Exports one coherent canonical history snapshot in the GAM ledger format.

Reads the committed history cursor without loading historical entries.

Loads a coherent canonical history snapshot; absence is distinct from failure.

Optionally loads the suffix after a verified complete-batch revision.

Looks up the committed identity and cursor of an idempotent history batch.

Reads one complete, indexed history batch beginning at the requested revision.

Reads one byte- and count-bounded page of a tuple-key namespace. Cursors are opaque and exclusive.

Atomically applies derived row changes and advances their exact predecessor cursor.

Fetches canonical row bytes at the exact projection generation and cursor.

Reads the current projection generation and cursor.

Opens a fresh disposable projection generation, fencing earlier generations.

Reads a count- and byte-bounded collection page at an exact projection cursor.

Reads an indexed projection selection with count and byte bounds.

Types

entry()

@type entry() :: %{revision: pos_integer(), value: term()}

store_ref()

@type store_ref() :: term()

Callbacks

compare_and_swap(term, term, pos_integer, term)

@callback compare_and_swap(term(), term(), pos_integer(), term()) ::
  {:ok, pos_integer()} | {:error, term()}

delete(term, term, pos_integer)

@callback delete(term(), term(), pos_integer()) :: :ok | {:error, term()}

fetch(term, term)

@callback fetch(term(), term()) :: {:ok, entry()} | :not_found | {:error, term()}

gam_domain(term)

(optional)
@callback gam_domain(term()) ::
  {:ok,
   %{
     ledger: Spectre.GAM.Ledger.Store.config(),
     projection: Spectre.GAM.Domain.Projection.Store.config()
   }}
  | :not_configured
  | {:error, term()}

Supplies the Domain's canonical history and disposable persisted read model.

Both resources describe the same Domain namespace. The projection is derived only from ledger-verified transitions, never a replacement source of authority. Older adapters may continue implementing only gam_ledger/1.

gam_ledger(term)

(optional)
@callback gam_ledger(term()) ::
  {:ok, Spectre.GAM.Ledger.Store.config()} | :not_configured | {:error, term()}

Optionally supplies the canonical GAM ledger used by Managed storage: :ledger.

This is a separate append-only contract, not the mutable coordination rows. Returning a backend does not grant authority or enable it for existing disk Domains. Adapters without this capability may return :not_configured.

ledger_append(store_ref, domain_ref, batch_id, list, non_neg_integer, keyword)

(optional)
@callback ledger_append(
  store_ref(),
  Spectre.GAM.Ledger.domain_ref(),
  Spectre.GAM.Ledger.batch_id(),
  [map()],
  non_neg_integer(),
  keyword()
) :: {:ok, non_neg_integer()} | {:error, term()}

Atomically appends one idempotent history batch after the expected revision.

ledger_export(store_ref, domain_ref, keyword)

(optional)
@callback ledger_export(store_ref(), Spectre.GAM.Ledger.domain_ref(), keyword()) ::
  :not_found | {:ok, map()} | {:error, term()}

Exports one coherent canonical history snapshot in the GAM ledger format.

ledger_head(store_ref, domain_ref, keyword)

(optional)
@callback ledger_head(store_ref(), Spectre.GAM.Ledger.domain_ref(), keyword()) ::
  :not_found | {:ok, Spectre.GAM.Ledger.cursor()} | {:error, term()}

Reads the committed history cursor without loading historical entries.

ledger_load(store_ref, domain_ref, keyword)

(optional)
@callback ledger_load(store_ref(), Spectre.GAM.Ledger.domain_ref(), keyword()) ::
  :not_found | {:ok, Spectre.GAM.Ledger.snapshot()} | {:error, term()}

Loads a coherent canonical history snapshot; absence is distinct from failure.

ledger_load_from(store_ref, domain_ref, non_neg_integer, keyword)

(optional)
@callback ledger_load_from(
  store_ref(),
  Spectre.GAM.Ledger.domain_ref(),
  non_neg_integer(),
  keyword()
) ::
  :not_found | {:ok, Spectre.GAM.Ledger.snapshot()} | {:error, term()}

Optionally loads the suffix after a verified complete-batch revision.

ledger_lookup_batch(store_ref, domain_ref, batch_id, keyword)

(optional)
@callback ledger_lookup_batch(
  store_ref(),
  Spectre.GAM.Ledger.domain_ref(),
  Spectre.GAM.Ledger.batch_id(),
  keyword()
) :: :not_found | {:ok, Spectre.GAM.Ledger.batch_info()} | {:error, term()}

Looks up the committed identity and cursor of an idempotent history batch.

ledger_read_batch(store_ref, domain_ref, pos_integer, keyword)

(optional)
@callback ledger_read_batch(
  store_ref(),
  Spectre.GAM.Ledger.domain_ref(),
  pos_integer(),
  keyword()
) ::
  :not_found | {:ok, Spectre.GAM.Ledger.snapshot()} | {:error, term()}

Reads one complete, indexed history batch beginning at the requested revision.

list(term, tuple, keyword)

(optional)
@callback list(term(), tuple(), keyword()) ::
  {:ok, [{term(), entry()}], term() | nil} | {:error, term()}

Reads one byte- and count-bounded page of a tuple-key namespace. Cursors are opaque and exclusive.

projection_commit(store_ref, handle, cursor, cursor, t, keyword)

(optional)

Atomically applies derived row changes and advances their exact predecessor cursor.

projection_fetch(store_ref, handle, cursor, list, keyword)

(optional)

Fetches canonical row bytes at the exact projection generation and cursor.

projection_head(store_ref, t, keyword)

(optional)
@callback projection_head(store_ref(), String.t(), keyword()) ::
  {:ok,
   %{
     handle: Spectre.GAM.Domain.Projection.Store.handle(),
     cursor: Spectre.GAM.Ledger.cursor()
   }}
  | :not_found
  | {:error, term()}

Reads the current projection generation and cursor.

projection_open(store_ref, t, keyword)

(optional)
@callback projection_open(store_ref(), String.t(), keyword()) ::
  {:ok, Spectre.GAM.Domain.Projection.Store.handle()} | {:error, term()}

Opens a fresh disposable projection generation, fencing earlier generations.

projection_page(store_ref, handle, cursor, atom, keyword)

(optional)
@callback projection_page(
  store_ref(),
  Spectre.GAM.Domain.Projection.Store.handle(),
  Spectre.GAM.Ledger.cursor(),
  atom(),
  keyword()
) ::
  {:ok, [{Spectre.GAM.Domain.Projection.Store.key(), binary()}], term() | nil}
  | {:error, term()}

Reads a count- and byte-bounded collection page at an exact projection cursor.

projection_select(store_ref, handle, cursor, atom, keyword, keyword)

(optional)
@callback projection_select(
  store_ref(),
  Spectre.GAM.Domain.Projection.Store.handle(),
  Spectre.GAM.Ledger.cursor(),
  atom(),
  keyword(),
  keyword()
) ::
  {:ok, [{Spectre.GAM.Domain.Projection.Store.key(), binary()}], term() | nil}
  | {:error, term()}

Reads an indexed projection selection with count and byte bounds.

put_new(term, term, term)

@callback put_new(term(), term(), term()) :: {:ok, pos_integer()} | {:error, term()}