Spectre.GAM.Ledger.Store behaviour (Spectre v0.4.0)

Copy Markdown View Source

Adapter contract and guarded invocation facade for Domain ledgers.

Store adapters must make each batch atomically visible, enforce expected_revision, and treat an uncertain append acknowledgement as {:error, :ambiguous}. A successful append may only be returned after the adapter's advertised durability boundary has acknowledged the batch.

Spectre.GAM.Ledger.Store.Support contains portable shape and option helpers for adapter authors. It deliberately does not abstract transactions, locks, encoding, recovery or durability: those guarantees stay visible in each concrete store.

Summary

Functions

Whether an adapter implements native batch paging and a lightweight head read.

Reads the advertised head; it does not authenticate a projection checkpoint.

Reads a coherent suffix after a revision, with the captured ledger head.

Normalizes a Store module or {module, options} pair.

Reads one indexed batch; unsupported adapters return an explicit error.

Types

batch_id()

@type batch_id() :: Spectre.GAM.Ledger.batch_id()

config()

@type config() :: module() | {module(), keyword()}

domain_ref()

@type domain_ref() :: Spectre.GAM.Ledger.domain_ref()

Callbacks

append(domain_ref, batch_id, list, non_neg_integer, keyword)

@callback append(domain_ref(), batch_id(), [map()], non_neg_integer(), keyword()) ::
  {:ok, non_neg_integer()}
  | {:error, :conflict}
  | {:error, :ambiguous}
  | {:error, term()}

export(domain_ref, keyword)

@callback export(
  domain_ref(),
  keyword()
) :: :not_found | {:ok, map()} | {:error, term()}

head(domain_ref, keyword)

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

Captures the committed head without loading historical entries.

load(domain_ref, keyword)

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

load_from(domain_ref, non_neg_integer, keyword)

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

lookup_batch(domain_ref, batch_id, keyword)

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

read_batch(domain_ref, pos_integer, keyword)

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

Reads exactly one complete batch beginning at first_revision.

This is the paging unit: a batch is already bounded by Entry's admission limits and must never be split across semantic folds. Implementations must use indexed reads, not load the entire ledger and slice it. The returned snapshot describes the batch end, not the current Domain head. Its chain must still be verified against the caller's trusted predecessor.

Functions

batch_reads?(store)

@spec batch_reads?(config()) :: boolean()

Whether an adapter implements native batch paging and a lightweight head read.

head(store, domain_ref, opts \\ [])

@spec head(config(), domain_ref(), keyword()) ::
  :not_found | {:ok, Spectre.GAM.Ledger.cursor()} | {:error, term()}

Reads the advertised head; it does not authenticate a projection checkpoint.

load_from(store, domain_ref, revision, opts)

@spec load_from(config(), domain_ref(), non_neg_integer(), keyword()) ::
  :not_found | {:ok, Spectre.GAM.Ledger.snapshot()} | {:error, term()}

Reads a coherent suffix after a revision, with the captured ledger head.

Native adapters can use their revision index. Adapters without this optional callback fall back to a verified full read. The caller must verify the suffix against its trusted predecessor; use Spectre.GAM.Ledger.load_from/4 for that.

normalize(module)

@spec normalize(config()) :: {:ok, {module(), keyword()}} | {:error, term()}

Normalizes a Store module or {module, options} pair.

read_batch(store, domain_ref, first_revision, opts \\ [])

@spec read_batch(config(), domain_ref(), pos_integer(), keyword()) ::
  :not_found | {:ok, Spectre.GAM.Ledger.snapshot()} | {:error, term()}

Reads one indexed batch; unsupported adapters return an explicit error.