Documentation
¶
Overview ¶
Package tinystore keeps one application's data in one directory: a file per engine, and one lifecycle for all of them.
data/
├── LOCK held while a store is open
├── metrics.db metrics.Open(ctx, store, …)
├── records.db records.Open(ctx, store, …)
├── kv.db kv.Open(ctx, store, …)
├── jobs.db jobs.Open(ctx, store, …)
├── blobs/ blobs.Open(ctx, store, …): blobs.db and the files
└── sql/
└── app.db sqldb.Open(ctx, store, "app", migrations)
Open the store, then each engine the program uses against it. Close stops background work and closes every engine, the last opened first. The store imports no engine, so a program links only the engines it opens.
tinystore.go Open, Close: the store and its lifecycle errors.go the sentinels every engine wraps engine.go Claim, Attach, Logger, Now: how an engine joins the store every.go background work, and how its failures are logged memory.go Reserve: one memory budget for every engine's work self.go opt-in bounded engine reports, stored as ordinary metrics snapshot.go Snapshot: every engine's file copied while it keeps working
Index ¶
- Variables
- func SnapshotPath(dir, name string) string
- type Engine
- type LimitError
- type Measure
- type MemoryUsage
- type Options
- type Reporter
- type Reservation
- type SelfWriter
- type Snapshot
- type SnapshotFile
- type Snapshotter
- type Store
- func (s *Store) Attach(engine Engine) error
- func (s *Store) Claim(name string) (filePath string, release func(), err error)
- func (s *Store) Close(ctx context.Context) error
- func (s *Store) Dir() string
- func (s *Store) Every(name string, interval time.Duration, work func(context.Context) error) (soon func())
- func (s *Store) EveryEngine(engine, name string, interval time.Duration, work func(context.Context) error) (soon func())
- func (s *Store) FlushSelfMetrics(ctx context.Context) error
- func (s *Store) Logger(engine string) *slog.Logger
- func (s *Store) Memory() MemoryUsage
- func (s *Store) Now() time.Time
- func (s *Store) Reserve(ctx context.Context, bytes int64) (*Reservation, error)
- func (s *Store) ReserveNow(bytes int64) (*Reservation, error)
- func (s *Store) Snapshot(ctx context.Context) (Snapshot, error)
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalid = errors.New("invalid request") ErrLimit = errors.New("resource limit") ErrClosed = errors.New("store is closed") ErrInUse = errors.New("already in use") ErrConflict = errors.New("state changed") ErrCorrupt = errors.New("corrupt data") ErrTooOld = errors.New("too old") ErrTooNew = errors.New("too new") ErrSuspended = errors.New("suspended") )
Every engine wraps these, so errors.Is means the same in all of them.
Functions ¶
func SnapshotPath ¶
SnapshotPath is where an engine writes its copy of name inside dir.
Types ¶
type LimitError ¶
LimitError is a limit a call reached, named: the bound, what the call would have taken of it, what it held included, and the bound's size, so that a caller can say which limit to raise or how much less to ask for:
resource limit: decoded samples: 120000 past 100000
errors.Is finds ErrLimit through it, and Kind, an engine's own limit, when it has one.
func (*LimitError) Error ¶
func (e *LimitError) Error() string
func (*LimitError) Unwrap ¶
func (e *LimitError) Unwrap() error
type Measure ¶
Measure is an integer counter or gauge reported by an engine. Names have bounded, fixed cardinality: do not put a bucket, queue, key or path in them.
type MemoryUsage ¶
type MemoryUsage struct {
Used, Peak, Capacity int64
}
MemoryUsage is the store's budget: Capacity is Options.Memory, zero when every engine keeps only its own per-call limits.
type Options ¶
type Options struct {
// Logger receives TinyStore's own logs; nil discards them.
Logger *slog.Logger
// Manual runs no background work: the application calls each engine's
// maintenance itself. Tests usually want this.
Manual bool
// Clock replaces time.Now for the store and every engine opened against it.
Clock func() time.Time
// Memory bounds the bytes that all engines' in-flight work holds at once;
// zero leaves each engine to its own per-call limits.
Memory int64
// SelfMetrics periodically writes available engine reports into an opened
// metrics engine. Manual stores call FlushSelfMetrics themselves.
SelfMetrics bool
}
type Reservation ¶
type Reservation struct {
// contains filtered or unexported fields
}
Reservation is bytes of the store's memory that one piece of work holds. The zero Reservation holds nothing, for work that needs no memory.
func (*Reservation) Release ¶
func (r *Reservation) Release()
Release gives back what the reservation holds; a second call gives nothing.
func (*Reservation) Shrink ¶
func (r *Reservation) Shrink(n int64)
Shrink gives back all but n of the bytes, for work that reserved its worst case and has learnt what it holds.
type SelfWriter ¶
SelfWriter writes one report without counting that write as user ingest. The root uses this interface without importing the metrics engine.
type Snapshot ¶
type Snapshot struct {
Dir string
Files []SnapshotFile
}
Snapshot is a copy of every engine's file, in Dir inside the store's directory, so that it takes the space of the disk the data is on.
type SnapshotFile ¶
type SnapshotFile struct {
// Name is the file's path inside the store, such as "metrics.db",
// "sql/app.db" or "blobs/objects/1a3/1a3f07".
Name string `json:"name"`
Engine string `json:"engine"`
// Schema is how many migrations the engine's database has.
Schema int `json:"schema"`
// Stored asks a backup to keep the bytes as they are rather than deflate
// them: an application's files are mostly media, which does not compress.
Stored bool `json:"-"`
}
SnapshotFile is one file of an engine's copy.
type Snapshotter ¶
type Snapshotter interface {
Snapshot(ctx context.Context, dir string) ([]SnapshotFile, error)
}
Snapshotter is an engine that can copy its files while it keeps working.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
func Open ¶
Open creates dir if needed and holds it until Close; a second store on the same directory, in this process or another, is refused with ErrInUse. ctx bounds the opening only: background work lives until Close.
func (*Store) Claim ¶
Claim reserves a file or directory inside the store for one engine and returns its path, creating the directory it lies in, or the directory itself for a name that ends in /. release gives the name back to an engine that failed to open; an attached engine keeps it until the store closes.
metrics.db records.db blobs/ engines sql/<name>.db databases the application names
func (*Store) Close ¶
Close stops background work, closes every engine, the last opened first, and releases the directory. When ctx ends first Close returns its error, and the cleanup still finishes; a later Close waits for it.
func (*Store) Dir ¶
Dir is the directory the store holds. A tool that backs the store up looks there for the files of engines no one has opened.
func (*Store) Every ¶
func (s *Store) Every(name string, interval time.Duration, work func(context.Context) error) (soon func())
Every runs work at each interval until the store closes, and soon asks for its next run now rather than at the next interval: asks that come while it runs or waits to run make one run more. A Manual store runs nothing, and its soon does nothing; a failure is logged, and the next run still happens.
func (*Store) EveryEngine ¶
func (s *Store) EveryEngine(engine, name string, interval time.Duration, work func(context.Context) error) ( soon func(), )
EveryEngine runs engine work with the same engine attribute as Store.Logger.
func (*Store) FlushSelfMetrics ¶
FlushSelfMetrics captures available reports and the store's memory budget once, then commits their samples together. It is a no-op when disabled or when no metrics engine is open. It reports reserved bytes, not process RSS.
func (*Store) Memory ¶
func (s *Store) Memory() MemoryUsage
func (*Store) Reserve ¶
Reserve waits, in arrival order, until bytes fit the store's memory. A reservation larger than the whole budget is refused at once with ErrLimit, and a store without Options.Memory grants every reservation.
func (*Store) ReserveNow ¶
func (s *Store) ReserveNow(bytes int64) (*Reservation, error)
ReserveNow takes bytes only if they fit at once, ahead of any reservation waiting, and is ErrLimit otherwise. Work holding what a waiting reservation may need, such as a file's writer, reserves this way: waiting, it could wait for itself.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package backup writes every engine of a tinystore.Store into one zip, and restores it into an empty directory before the store opens.
|
Package backup writes every engine of a tinystore.Store into one zip, and restores it into an empty directory before the store opens. |
|
Package blobs keeps an application's files in blobs/ inside a tinystore.Store: objects under keys that are paths, their rows in blobs.db, their bytes inline up to 16 KiB and in a file of their own above it.
|
Package blobs keeps an application's files in blobs/ inside a tinystore.Store: objects under keys that are paths, their rows in blobs.db, their bytes inline up to 16 KiB and in a file of their own above it. |
|
cmd
|
|
|
tinystore
module
|
|
|
examples
|
|
|
notes
command
Command notes is what a program using TinyStore looks like: one directory, its own SQL, its own metrics and logs, its drafts, its jobs, its notes' attachments, a backup, and one Close.
|
Command notes is what a program using TinyStore looks like: one directory, its own SQL, its own metrics and logs, its drafts, its jobs, its notes' attachments, a backup, and one Close. |
|
internal
|
|
|
admission
Package admission lets work into an engine while it is open, a bounded number of each kind at a time, and lets Close wait for the work already in.
|
Package admission lets work into an engine while it is open, a bounded number of each kind at a time, and lets Close wait for the work already in. |
|
dbstat
Package dbstat divides a SQLite file's pages among the tables and indexes that own them, as SQLite's dbstat table does, for measurements: SQLite as the driver builds it leaves dbstat out.
|
Package dbstat divides a SQLite file's pages among the tables and indexes that own them, as SQLite's dbstat table does, for measurements: SQLite as the driver builds it leaves dbstat out. |
|
dirlock
Package dirlock holds a store's directory for one process, through a lock on its LOCK file.
|
Package dirlock holds a store's directory for one process, through a lock on its LOCK file. |
|
release
command
Command release makes a TinyStore release: the three tags of its Go modules, and into dist/ the tinystore binary for every platform, an archive of each with SHA256SUMS, the npm packages and the Python wheels that carry it, and the release's notes.
|
Command release makes a TinyStore release: the three tags of its Go modules, and into dist/ the tinystore binary for every platform, an archive of each with SHA256SUMS, the npm packages and the Python wheels that carry it, and the release's notes. |
|
sizeprobe
command
Command sizeprobe links the public API so task size can report what importing it costs.
|
Command sizeprobe links the public API so task size can report what importing it costs. |
|
sizeprobe/baseline
command
Command baseline is the same program without the import, so task size can subtract it.
|
Command baseline is the same program without the import, so task size can subtract it. |
|
sizeprobe/report
command
Command report prints what task size measured: the probe's target, its size and what the import added over the baseline.
|
Command report prints what task size measured: the probe's target, its size and what the import added over the baseline. |
|
sqlite
Package sqlite owns file and transaction mechanics, not engine data.
|
Package sqlite owns file and transaction mechanics, not engine data. |
|
term
Package term says whether a file is a terminal a person reads, and whether it shows colours, for the console lines a logger writes beside the store.
|
Package term says whether a file is a terminal a person reads, and whether it shows colours, for the console lines a logger writes beside the store. |
|
Package jobs keeps work that runs at its time in jobs.db inside a tinystore.Store: typed queues whose jobs wait for their time, are leased to a worker, retried, repeated by cron text, and run at least once.
|
Package jobs keeps work that runs at its time in jobs.db inside a tinystore.Store: typed queues whose jobs wait for their time, are leased to a worker, retried, repeated by cron text, and run at least once. |
|
Package kv keeps an application's current state in kv.db inside a tinystore.Store: buckets of one value type and counters, keyed by text in branches, with expiry by the store's clock and versions that never repeat.
|
Package kv keeps an application's current state in kv.db inside a tinystore.Store: buckets of one value type and counters, keyed by text in branches, with expiry by the store's clock and versions that never repeat. |
|
Package metrics stores exact numeric time series in one SQLite file.
|
Package metrics stores exact numeric time series in one SQLite file. |
|
Package records keeps an application's logs and events in records.db inside a tinystore.Store.
|
Package records keeps an application's logs and events in records.db inside a tinystore.Store. |
|
server
module
|
|
|
Package sqldb gives an application its own SQLite databases inside a tinystore.Store.
|
Package sqldb gives an application its own SQLite databases inside a tinystore.Store. |
|
internal/catalog
Package catalog reads what a SQLite file says its tables are, through its pragmas and the text sqlite_schema keeps, and says how two files differ: in structure, which sqldb's Open checks, and in the text of expressions, which a test reports.
|
Package catalog reads what a SQLite file says its tables are, through its pragmas and the text sqlite_schema keeps, and says how two files differ: in structure, which sqldb's Open checks, and in the text of expressions, which a test reports. |
|
sqldbtest
Package sqldbtest checks, in a program's own tests, that a database's migrations make what its schema declares, and writes the next migration when go tool tinystore asks it to.
|
Package sqldbtest checks, in a program's own tests, that a database's migrations make what its schema declares, and writes the next migration when go tool tinystore asks it to. |