ExGAM.Ledger.Store behaviour (ExGAM v0.1.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.

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

Callbacks

Atomically appends a batch at its expected predecessor revision and returns the committed revision.

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

Captures the committed head without loading historical entries.

Loads a coherent history snapshot for the Domain, or returns :not_found.

Optionally reads entries strictly after a revision and returns the corresponding snapshot head.

Looks up a previously committed idempotency batch by its Domain and batch ID.

Reads exactly one complete batch beginning at first_revision.

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() :: ExGAM.Ledger.batch_id()

config()

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

domain_ref()

@type domain_ref() :: ExGAM.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()}

Atomically appends a batch at its expected predecessor revision and returns the committed revision.

export(domain_ref, keyword)

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

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

head(domain_ref, keyword)

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

Captures the committed head without loading historical entries.

load(domain_ref, keyword)

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

Loads a coherent history snapshot for the Domain, or returns :not_found.

load_from(domain_ref, non_neg_integer, keyword)

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

Optionally reads entries strictly after a revision and returns the corresponding snapshot head.

lookup_batch(domain_ref, batch_id, keyword)

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

Looks up a previously committed idempotency batch by its Domain and batch ID.

read_batch(domain_ref, pos_integer, keyword)

(optional)
@callback read_batch(domain_ref(), pos_integer(), keyword()) ::
  :not_found | {:ok, ExGAM.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, ExGAM.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, ExGAM.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 ExGAM.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, ExGAM.Ledger.snapshot()} | {:error, term()}

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