# `Spectre.Server.Store`
[🔗](https://github.com/elchemista/spectre/blob/0.4.0/lib/spectre/server/store.ex#L1)

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.

# `entry`

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

# `store_ref`

```elixir
@type store_ref() :: term()
```

# `compare_and_swap`

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

# `delete`

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

# `fetch`

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

# `gam_domain`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@callback projection_commit(
  store_ref(),
  Spectre.GAM.Domain.Projection.Store.handle(),
  Spectre.GAM.Ledger.cursor(),
  Spectre.GAM.Ledger.cursor(),
  Enumerable.t(),
  keyword()
) :: :ok | {:error, term()}
```

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

# `projection_fetch`
*optional* 

```elixir
@callback projection_fetch(
  store_ref(),
  Spectre.GAM.Domain.Projection.Store.handle(),
  Spectre.GAM.Ledger.cursor(),
  [Spectre.GAM.Domain.Projection.Store.key()],
  keyword()
) ::
  {:ok, %{optional(Spectre.GAM.Domain.Projection.Store.key()) =&gt; binary()}}
  | {:error, term()}
```

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

# `projection_head`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@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`

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
