Documentation
¶
Overview ¶
Package sqlite provides a pure-Go (CGo-free) SQLite implementation of persistence.VersionedPersistence using modernc.org/sqlite. It stores per-room incremental V1 updates, named snapshots, and a crash-safety checkpoint in one database file (WAL mode). Reads clamp to the checkpoint; PruneAfter is two-phase and the next AppendUpdate finishes an interrupted prune, mirroring persistence.FilePersistence.
Index ¶
- type Store
- func (s *Store) AppendUpdate(ctx context.Context, room string, update []byte) (persistence.Version, error)
- func (s *Store) CaptureSnapshot(ctx context.Context, room, name string, state []byte) (persistence.Version, error)
- func (s *Store) Close() error
- func (s *Store) Compact(ctx context.Context, room string, keep int) (int, error)
- func (s *Store) Delete(ctx context.Context, room string) error
- func (s *Store) DeleteSnapshot(ctx context.Context, room string, id int64) error
- func (s *Store) GetSnapshotState(ctx context.Context, room string, id int64) ([]byte, error)
- func (s *Store) GetUpdate(ctx context.Context, room string, v persistence.Version) ([]byte, persistence.VersionMeta, bool, error)
- func (s *Store) ListRooms(ctx context.Context) ([]string, error)
- func (s *Store) ListSnapshots(ctx context.Context, room string) ([]persistence.SnapshotInfo, error)
- func (s *Store) ListVersions(ctx context.Context, room string) ([]persistence.VersionMeta, error)
- func (s *Store) Load(ctx context.Context, room string) (persistence.LoadResult, error)
- func (s *Store) MaterializeAt(ctx context.Context, room string, v persistence.Version) ([]byte, error)
- func (s *Store) PruneAfter(ctx context.Context, room string, target persistence.Version, ...) error
- func (s *Store) Reopen() (persistence.VersionedPersistence, error)
- func (s *Store) RestoreSnapshot(ctx context.Context, room, name string) ([]byte, persistence.Version, bool, error)
- func (s *Store) SaveSnapshot(ctx context.Context, room, label string, state []byte) (int64, error)
- func (s *Store) SetCrashAfterCheckpoint(fn func() bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a VersionedPersistence backed by a single SQLite database. The zero value is not usable; call Open.
func Open ¶
Open opens (creating if needed) a SQLite-backed Store at path. An empty path or ":memory:" opens an ephemeral in-memory database pinned to one connection.
func (*Store) AppendUpdate ¶
func (s *Store) AppendUpdate(ctx context.Context, room string, update []byte) (persistence.Version, error)
AppendUpdate validates and stores a V1 update as the room's next version, returning the assigned version. Invalid updates are rejected before any write.
func (*Store) CaptureSnapshot ¶
func (s *Store) CaptureSnapshot(ctx context.Context, room, name string, state []byte) (persistence.Version, error)
CaptureSnapshot stores a named V1 snapshot for room, associated with the current clamped head version (0 for an empty room). An existing snapshot with the same (room, name) is overwritten.
func (*Store) Compact ¶
Compact bounds a room's history to the newest keep updates, returning the number of rows removed. It never drops materialized state: the trimmed prefix is FOLDED into the oldest retained row (merged into one V1 blob), then the now-redundant older rows are deleted. Retained version numbers are unchanged, so reads still reconstruct the same head. keep <= 0 is a no-op (returns 0).
func (*Store) Delete ¶
Delete removes all data for room: its updates, named snapshots, and any crash-safety checkpoint, atomically in one transaction.
func (*Store) DeleteSnapshot ¶ added in v1.41.0
DeleteSnapshot removes one snapshot. Deleting an unknown snapshot is a no-op.
func (*Store) GetSnapshotState ¶ added in v1.41.0
GetSnapshotState returns the state blob of one snapshot. persistence.ErrSnapshotNotFound when (room, id) is unknown.
func (*Store) GetUpdate ¶
func (s *Store) GetUpdate(ctx context.Context, room string, v persistence.Version) ([]byte, persistence.VersionMeta, bool, error)
GetUpdate returns the single (non-cumulative) update stored at version v and its metadata, with ok=true when present. Versions above the clamped head are invisible (ok=false, nil error), as are unknown rooms/versions.
func (*Store) ListRooms ¶ added in v1.41.0
ListRooms returns every room holding at least one update or snapshot.
func (*Store) ListSnapshots ¶ added in v1.41.0
func (s *Store) ListSnapshots(ctx context.Context, room string) ([]persistence.SnapshotInfo, error)
ListSnapshots returns snapshot metadata for room, newest-first. The state blob is not read: only its length, so listing stays cheap for large snapshots.
func (*Store) ListVersions ¶
func (s *Store) ListVersions(ctx context.Context, room string) ([]persistence.VersionMeta, error)
ListVersions returns metadata for every stored update in room with version <= the clamped head, newest first. An empty (or unknown) room yields nil.
func (*Store) Load ¶
func (s *Store) Load(ctx context.Context, room string) (persistence.LoadResult, error)
Load returns the merged state of all updates up to the room's clamped head (the checkpoint target if one exists, else the latest version). An empty room yields a zero LoadResult.
Reads (Load, ListVersions, GetUpdate, MaterializeAt) are intentionally lock-free: they never take s.mu. Consistency against a concurrent mid-prune comes from the clampSQL correlated subquery, which resolves the checkpoint ceiling and the row filter within a single statement. Do not add s.mu here — it would needlessly serialize reads against writers.
Reads that issue TWO statements (Load, MaterializeAt run clampedHead then mergedUpTo) wrap both in a single read-only transaction: WAL snapshot isolation makes them see one consistent committed state, so a concurrent PruneAfter cannot interleave between the two statements and either expose future versions or return a mismatched {Version, Update}. This is still lock-free (no s.mu); the read tx only pins a snapshot, it does not block writers.
func (*Store) MaterializeAt ¶
func (s *Store) MaterializeAt(ctx context.Context, room string, v persistence.Version) ([]byte, error)
MaterializeAt rebuilds the full V1 head state as of version v by merging all updates with version <= min(v, clamped head). It returns a nil slice for v == 0, and ErrRoomNotFound if the room has no visible updates and v > 0.
func (*Store) PruneAfter ¶
func (s *Store) PruneAfter(ctx context.Context, room string, target persistence.Version, rolledBack []byte) error
PruneAfter removes every update with version > target, crash-safely. It is two-phase: Phase 1 durably records a checkpoint (the prune ceiling plus the rolled-back head) so reads clamp to target the instant it commits; Phase 2 deletes the future updates and clears the checkpoint. A crash between the two phases leaves stale future rows on disk, but the checkpoint keeps them invisible, and the next AppendUpdate finishes the interrupted prune. Mirrors persistence.FilePersistence.PruneAfter.
func (*Store) Reopen ¶
func (s *Store) Reopen() (persistence.VersionedPersistence, error)
Reopen satisfies persistence.Reopener: a fresh handle over the same backing store. In-memory stores (no durable file) return the same handle.
func (*Store) RestoreSnapshot ¶
func (s *Store) RestoreSnapshot(ctx context.Context, room, name string) ([]byte, persistence.Version, bool, error)
RestoreSnapshot returns the V1 blob stored under (room, name), the version it was captured at, and ok=true when present. ok=false (nil error) when absent.
func (*Store) SaveSnapshot ¶ added in v1.41.0
SaveSnapshot stores state as a new labelled snapshot of room and returns its ID. AUTOINCREMENT guarantees the id is monotonic and never reused.
func (*Store) SetCrashAfterCheckpoint ¶
SetCrashAfterCheckpoint satisfies persistence.CrashInjector (test-only).