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
- Variables
- func Check(ctx context.Context, path string) error
- func Get[T proto.Message](ctx context.Context, r Reader, id string) (T, error)
- func Latest[T proto.Message](ctx context.Context, r Reader, where Where, f func(T) bool) error
- func List[T proto.Message](ctx context.Context, r Reader, where Where) ([]T, error)
- func ListAfter[T proto.Message](ctx context.Context, r Reader, where Where, after After) ([]T, error)
- func NewID() string
- func SnapshotFile(ctx context.Context, src, dst string) error
- type After
- type Command
- type Reader
- type Retention
- type Sample
- type Store
- func (s *Store) AddSamples(ctx context.Context, samples []Sample) error
- func (s *Store) Close() error
- func (s *Store) OnCommit(f func([]proto.Message))
- func (s *Store) PruneSamples(ctx context.Context, now time.Time, r Retention) error
- func (s *Store) Samples(ctx context.Context, series string, from time.Time) ([]Sample, error)
- func (s *Store) Snapshot(ctx context.Context, dst string) error
- func (s *Store) Tx(ctx context.Context, fn func(*Tx) error) error
- func (s *Store) Watch(ctx context.Context, dir string)
- type Tx
- type Where
Constants ¶
const File = "djinn.db"
File is the database of the machine, in the data directory.
Variables ¶
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 Latest ¶
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 ¶
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.
Types ¶
type After ¶ added in v0.1.1
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 ¶
Command is an entry of the journal.
func Commands ¶
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 ¶
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
AddSamples writes samples, in one transaction.
func (*Store) OnCommit ¶
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
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
Samples reads the samples of series measured after from, the oldest first.
func (*Store) Snapshot ¶
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.
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
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 ¶
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 ¶
Journal writes the command received: who sent it, the full name of its method, and the request as is.