docdb

package
v0.0.0-...-f07582e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package docdb maintains indexed SQLite views of document streams.

The unit of storage is a JSON document. A doc table keeps each document whole in its raw column and exposes it relationally. A few real columns, such as the primary key and the update clock, are extracted by the write statement itself, and every other column is GENERATED from raw, so a column can never disagree with the document it derives from. Writes are guarded upserts that keep the newer document, which makes them idempotent and order-independent: replaying a write, or applying old and new in either order, converges on the same row.

A derived table is the other kind: a SELECT over the database's other tables, materialized once when the database is built.

Changes travel between databases as segments, gzip JSONL files of bare documents that replay through the same guarded upserts. Cache serves a local copy of a published database, applying new segments as they land and downloading the database afresh when it is replaced wholesale.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyDocs

func ApplyDocs(db *sqlite3.Conn, td TableDef, docs []json.RawMessage) (err error)

ApplyDocs upserts documents into td's existing doc table.

func ApplySegment

func ApplySegment(db *sqlite3.Conn, src billy.Basic, defs []TableDef, name string) error

ApplySegment applies one of src's segments to db with the same guarded upserts the table writes use, so replaying a segment, applying one twice, or applying out of order can never regress a row.

func Doc

func Doc(path string) string

Doc extracts a field of the document being written.

func DocTime

func DocTime(path string) string

DocTime is Doc normalized to sqlitex.TimeFormat, zero time to NULL.

func EnsureDocTables

func EnsureDocTables(db *sqlite3.Conn, defs []TableDef) error

EnsureDocTables creates the doc tables among defs (and their indexes) that db does not already have, making an empty or partial database writable in place. Derived tables are skipped: they materialize from queries, not writes. NOTE: Existing tables are not reconciled with defs. Columns added to the registry appear only on the next full rebuild, since SQLite cannot ALTER in a STORED generated column.

func ListSegments

func ListSegments(src billy.Filesystem, prefix, start string) ([]string, error)

ListSegments returns the segment names src holds under prefix at or after start (a segment name, empty for all of them) in write order. A prefix that does not exist yet lists empty.

func OneofKey

func OneofKey(path string) string

OneofKey names the single field present in a oneof object of the document being written (empty when none): the oneof's JSON tags double as labels.

func Raw

func Raw(path string) string

Raw extracts a stored-document field.

func RawSeconds

func RawSeconds(path string) string

RawSeconds converts a Go duration field (integer nanoseconds) to seconds.

func RawTime

func RawTime(path string) string

RawTime is Raw normalized to sqlitex.TimeFormat, zero time to NULL.

func SegmentName

func SegmentName(prefix string, t time.Time) string

SegmentName returns the object name for a segment written at t under a destination prefix. Owners version the prefix with their schema so a reader only ever consumes segments matching its base's era.

func SegmentTime

func SegmentTime(name string) (time.Time, error)

SegmentTime parses a segment object name back to its write time.

func StoreDocs

func StoreDocs(db *sqlite3.Conn, td TableDef, docs []json.RawMessage) error

StoreDocs creates td's doc table, upserts the given documents, and builds its declared indexes.

func StoreQuery

func StoreQuery(db *sqlite3.Conn, td TableDef) (int, error)

StoreQuery materializes a derived table from its defining query and builds its declared indexes, returning the row count. Materializing at build keeps reads cheap and snapshot-consistent. It refreshes only by rebuild.

func WriteSegment

func WriteSegment(dest billy.Basic, prefix string, t time.Time, defs []TableDef, tables map[string][]json.RawMessage) (_ string, err error)

WriteSegment writes documents (doc table name to document list) into dest as one gzip JSONL segment named for t under prefix. When every list is empty nothing is written and the returned name is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache serves an updated local copy of an upstream database. Contents are refreshed at the specified interval, applying new delta segments incrementally and rehydrating wholesale when a newer full base becomes available. Query serializes access to the current connection.

func OpenCache

func OpenCache(ctx context.Context, up Upstream, interval time.Duration) (*Cache, error)

OpenCache hydrates a cache of up's base object and refreshes it in the background every interval until Close. A base from any era but up.Schema is refused.

func (*Cache) Close

func (c *Cache) Close() error

Close stops refreshing, flushes in-flight refresh, and removes local data.

func (*Cache) Freshness

func (c *Cache) Freshness() time.Time

Freshness returns the time through which the served data is complete. This is the base watermark and is advanced by applied delta segments.

func (*Cache) Query

func (c *Cache) Query(f func(*sqlite3.Conn) error) error

Query runs f with the current database, serialized against refreshes.

type Col

type Col struct {
	Name string
	Type string
	Expr string
}

Col is a real column of a doc table, extracted from the bound document by the write statement itself. Unlike generated columns, real columns can be primary keys and carry the upsert guard.

type GenCol

type GenCol struct {
	Name   string
	Type   string
	Expr   string
	Stored bool
}

GenCol is a column generated from the stored raw document. Stored columns cost disk and compute at write when they are updated. Virtual ones cost nothing on write and compute on read, so columns in hot filters or orderings should be stored (or indexed, which also stores the computed values).

type TableDef

type TableDef struct {
	Name    string
	Cols    []Col
	PK      []string
	GenCols []GenCol
	Query   string
	Indexes [][]string
}

TableDef describes one table. A table is either a doc table (Cols set: real columns extracted from each written document, an implicit raw column holding the document, and optionally GenCols computed from raw) or a derived table (Query set: a SELECT over the database's other tables, materialized by StoreQuery). Indexes lists the column sets to index after load. A real column named updated becomes the upsert guard clock, and without one replays are last-write-wins. NOTE: Names and expressions are trusted SQL fragments. They should be exclusively sourced from compile-time registries.

type Upstream

type Upstream struct {
	FS        billy.Filesystem
	Object    string
	Deltas    string
	Defs      []TableDef
	Schema    int
	Watermark func(*sqlite3.Conn) (time.Time, error)
}

Upstream names the published database a Cache follows: the filesystem holding it, its gzip base object, the prefix its delta segments land under, the doc tables those segments replay into, and the schema era this binary reads. Watermark reads the time through which the base is complete, which is where segment replay resumes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL