SuperPi.Durable.Doc (super_pi v0.1.0)

Copy Markdown View Source

Scoped JSON document manager supporting latest and rewindable (fork: "as_of") policies.

Rewindable documents store base snapshots and RFC 6902 (Jsonpatch) operational deltas stamped with the commit seq, enabling exact historical reconstruction on conversation fork via snapshot_as_of/3. Active snapshots are cached in :ets (:super_pi_doc_cache).

Summary

Functions

Removes cache entries (another node wrote these documents).

Creates or updates a scoped document at seq and caches the new immutable snapshot in ETS.

Writes a fast in-memory ETS overlay for deferred commits (100ms streaming updates).

Reads the latest snapshot of a document, checking ETS first before reconstructing from Ecto.

Reconstructs a rewindable conversation document (pi.agent) as of at_seq, walking conversation fork ancestry when at_seq predates the fork point (spec.md §3.3).

Reads the latest value of a document from the database, never from the cache, and (on PostgreSQL) locks its row until the transaction ends. Use it for a read-modify-write inside SuperPi.Durable.Storage.commit/1: a cached copy may be older than what another node wrote.

Functions

builtin_spec(kind)

cache_invalidate(keys)

Removes cache entries (another node wrote these documents).

ensure_ets!()

put_doc(scope_kind, scope_id, kind, new_value, seq, opts \\ [])

Creates or updates a scoped document at seq and caches the new immutable snapshot in ETS.

put_ets_overlay(scope_kind, scope_id, kind, value, seq, key \\ "")

Writes a fast in-memory ETS overlay for deferred commits (100ms streaming updates).

snapshot(scope_kind, scope_id, kind, key \\ "")

Reads the latest snapshot of a document, checking ETS first before reconstructing from Ecto.

snapshot_as_of(conversation_id, kind, at_seq, key \\ "")

Reconstructs a rewindable conversation document (pi.agent) as of at_seq, walking conversation fork ancestry when at_seq predates the fork point (spec.md §3.3).

snapshot_for_update(scope_kind, scope_id, kind, key \\ "")

Reads the latest value of a document from the database, never from the cache, and (on PostgreSQL) locks its row until the transaction ends. Use it for a read-modify-write inside SuperPi.Durable.Storage.commit/1: a cached copy may be older than what another node wrote.