ExGAM.Ledger.Store.Disk (ExGAM v0.1.0)

Copy Markdown View Source

File-backed, serialized Store for Domain ledgers.

Each Domain is stored in a file named by the SHA-256 digest of its reference. Every atomic logical batch occupies one length-delimited frame containing a canonical payload and a raw SHA-256 checksum:

magic | version | payload length | checksum | canonical batch payload

A successful append is returned only after the complete frame and any new directory entry cross their filesystem durability barriers. Recovery accepts complete verified frames only and repeats that barrier before trusting a recovered commit. The default tail_policy: :reject reports an incomplete tail; tail_policy: :truncate explicitly removes only an incomplete final frame and records that recovery in the returned runtime snapshot. A checksum, canonical decoding, identity, or chain failure is corruption and is never truncated automatically.

The process retains only Domain cursors and private compressed ETS indexes of batch identities/file offsets. Entry bodies remain on disk. Native batch reads fetch one frame; load/export explicitly materialize a complete result. compressed: true compresses new frame bodies losslessly when worthwhile. Both stored and expanded sizes are bounded by :max_frame_bytes; old plain frames remain readable regardless of the current write setting.

One GenServer serializes every mutation for its directory. Directory ownership is registered globally, so a second Store in the same connected BEAM cluster is rejected. A filesystem shared by disconnected Erlang clusters still needs deployment-level fencing.

:fault_injection (or :fault) accepts :before_commit and :after_commit. Both return {:error, :ambiguous}; the latter writes and syncs the batch first.

Summary

Functions

Returns a specification to start this module under a supervisor.

Starts a serialized disk Store.

Types

domain_state()

@type domain_state() :: %{
  revision: non_neg_integer(),
  head_digest: ExGAM.Ledger.Entry.digest(),
  index: :ets.tid(),
  file_bytes: non_neg_integer(),
  recovery: map() | nil
}

state()

@type state() :: %{
  directory: String.t(),
  ownership_name: term(),
  tail_policy: :reject | :truncate,
  max_frame_bytes: pos_integer(),
  compressed: boolean(),
  domains: %{optional(String.t()) => domain_state()}
}

Functions

child_spec(init_arg)

@spec child_spec(keyword()) :: Supervisor.child_spec()

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts a serialized disk Store.

:path (or :directory) is required. :tail_policy defaults to :reject. :max_frame_bytes defaults to 64 MiB and is bounded to 1 GiB.