store

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package store keeps the entities of Djinn in one SQLite database, by protobuf reflection: one table per message, its id as primary key, the fields it is searched by as columns, and the whole message as a binary payload. A field that is not searched by lives only in the payload, so adding one needs no migration.

Every change goes through Tx, which writes the command that caused it to the journal in the same transaction.

Index

Constants

View Source
const File = "djinn.db"

File is the database of the machine, in the data directory.

Variables

View Source
var (
	// ErrNotFound is returned by Get when no entity has the id.
	ErrNotFound = errors.New("not found")
	// ErrDuplicate is returned by Put when another entity has the same values in a unique group of fields.
	ErrDuplicate = errors.New("duplicate")
)

Functions

func Check

func Check(ctx context.Context, path string) error

Check opens the database file at path and runs SQLite's integrity check.

func Get

func Get[T proto.Message](ctx context.Context, r Reader, id string) (T, error)

Get returns the entity of type T with this id, or an error wrapping ErrNotFound.

func Latest

func Latest[T proto.Message](ctx context.Context, r Reader, where Where, f func(T) bool) error

Latest calls f on the entities of type T that match where, the last written first, until f returns false: an entity put again keeps its place. It reads no further than f asks, where List reads everything.

func List

func List[T proto.Message](ctx context.Context, r Reader, where Where) ([]T, error)

List returns the entities of type T that match where, oldest first: ids are UUIDv7, ordered by time.

func ListAfter added in v0.1.1

func ListAfter[T proto.Message](ctx context.Context, r Reader, where Where, after After) ([]T, error)

ListAfter is List narrowed by after, in the order of its field: through an index on where's fields and after's, it reads only the entities past From, where List reads them all.

func NewID

func NewID() string

NewID returns a new UUIDv7, the identifier of every entity.

func SnapshotFile

func SnapshotFile(ctx context.Context, src, dst string) error

SnapshotFile writes a consistent copy of the database file src to dst, which must not exist. It neither migrates nor writes src, and is safe while a djinn uses it: in WAL mode, a reader never blocks a writer.

Types

type After added in v0.1.1

type After struct {
	Field    string
	From     int64
	Contains [][]byte
}

After narrows a ListAfter to the entities whose indexed integer field Field is past From, and whose payload holds each of Contains: a filter on the encoded bytes that skips decoding the others, which the caller checks again once decoded, as an entity may hold the bytes elsewhere.

type Command

type Command struct {
	Seq     int64
	ID      string
	Actor   string
	At      time.Time
	Method  string
	Request []byte
}

Command is an entry of the journal.

func Commands

func Commands(ctx context.Context, r Reader, keep func(Command) bool) ([]Command, error)

Commands returns the entries of the journal, oldest first, for which keep returns true. keep sees each entry once, in order; it may be nil to keep them all.

func CommandsOf

func CommandsOf(ctx context.Context, r Reader, prefix string, keep func(Command) bool) ([]Command, error)

CommandsOf is Commands among the entries whose method starts with prefix, ASCII ("/" for the methods of the API), read through the index of the methods: the many entries of other methods, as the workers' events, are not read.

type Reader

type Reader interface {
	// contains filtered or unexported methods
}

Reader is the store or a transaction: what Get and List read from.

type Retention added in v0.1.1

type Retention struct {
	// Readings younger than Fine stay as read; older ones are rolled up by the minute, keeping each minute's highest
	// values, so a peak stays readable.
	Fine time.Duration
	// Samples older than Keep go.
	Keep time.Duration
	// The most samples kept: beyond it, the oldest go first.
	Max int
}

Retention is how long the samples are kept, and how many at most.

type Sample added in v0.1.1

type Sample struct {
	// The series: "machine", or "task:" and the task's id.
	Series string
	At     time.Time
	// Seconds the sample stands for: 5 for a reading, 60 for a minute rolled up (its highest values).
	Seconds     int32
	CPU         float64
	Memory      uint64
	DjinnCPU    float64
	DjinnMemory uint64
	// Djinn's own process alone, without what it started.
	SelfCPU    float64
	SelfMemory uint64
}

Sample is a measure of a series at a time: the machine's CPU and memory with Djinn's own share, or a worker's. A sample is an observation, not a change of the plan: it is kept apart from the journal and from the entities, and no commit hook hears of it.

type Store

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

Store is the database. It is safe for concurrent use: transactions run one at a time.

func Open

func Open(ctx context.Context, path string, entities ...proto.Message) (*Store, error)

Open opens the database file at path, creating it and its directory if needed, in WAL mode. An empty path opens a database in memory, for tests. The tables of entities are created, and the columns they lack added; nothing is ever dropped.

Each entity is a message with a string id field. Its singular string fields named *_id are indexed, and each (djinn.v1.unique) option becomes a unique index, ignoring case on text.

func (*Store) AddSamples added in v0.1.1

func (s *Store) AddSamples(ctx context.Context, samples []Sample) error

AddSamples writes samples, in one transaction.

func (*Store) Close

func (s *Store) Close() error

Close closes the database.

func (*Store) OnCommit

func (s *Store) OnCommit(f func([]proto.Message))

OnCommit calls f after each committed transaction that changed entities, with what it put and deleted, in order; a deleted entity may hold its id only. f runs in the writer's goroutine, after the commit: it must be quick, and must neither keep nor change the messages.

func (*Store) PruneSamples added in v0.1.1

func (s *Store) PruneSamples(ctx context.Context, now time.Time, r Retention) error

PruneSamples rolls up and removes samples as r says, now being the time of the last reading.

func (*Store) Samples added in v0.1.1

func (s *Store) Samples(ctx context.Context, series string, from time.Time) ([]Sample, error)

Samples reads the samples of series measured after from, the oldest first.

func (*Store) Snapshot

func (s *Store) Snapshot(ctx context.Context, dst string) error

Snapshot writes a consistent copy of the database to dst, which must not exist: VACUUM INTO, in one read transaction. Writes wait for it, as they wait for any transaction.

func (*Store) Tx

func (s *Store) Tx(ctx context.Context, fn func(*Tx) error) error

Tx runs fn in a transaction, committed when fn returns nil and rolled back otherwise. A change must follow the command that caused it: call Journal before Put.

func (*Store) Watch added in v0.1.2

func (s *Store) Watch(ctx context.Context, dir string)

Watch logs, until ctx ends, a transaction that holds the database's one connection past logx.HeldLong: every other call waits for it. Past logx.HeldFrozen it dumps the goroutines to dir (logx.Held.Watch).

type Tx

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

Tx is a transaction: it reads what it wrote, and writes the journal and the entities together.

func (*Tx) Change added in v0.1.1

func (tx *Tx) Change(ms ...proto.Message) error

Change notes entities that changed outside of Put or Delete (such as an entity moved to another wish), to be reported to OnCommit hooks. Like Put, it follows the command that caused it.

func (*Tx) Delete

func (tx *Tx) Delete(m proto.Message) error

Delete removes an entity, found by the type and the id of m. Like Put, it follows the command that caused it.

func (*Tx) Journal

func (tx *Tx) Journal(actor, method string, req proto.Message) error

Journal writes the command received: who sent it, the full name of its method, and the request as is.

func (*Tx) JournalID added in v0.1.1

func (tx *Tx) JournalID() string

JournalID returns the identifier of the last command journaled in this transaction, or empty before Journal. A newly created entity may use it as its identifier, tying creation to its command without extra stored fields.

func (*Tx) Put

func (tx *Tx) Put(m proto.Message) error

Put inserts or replaces an entity. It returns an error wrapping ErrDuplicate when another entity has the same values in a unique group.

type Where

type Where map[string]any

Where filters List: each key is an indexed field, compared for equality, ignoring case on text.

Jump to

Keyboard shortcuts

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