sqlite

package
v0.0.0-...-ad078e6 Latest Latest
Warning

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

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

Documentation

Overview

Package sqlite is the persistent result store (SPEC §9.4, §13): the run and window summaries and the run errors of the monitoring sessions, in an SQLite database that survives a restart of gotwampd.

The Store keeps a store.Memory in front of the database: Publish feeds it and the watchers as before, and queues the results to keep for one writer goroutine, so that a slow disk never holds up a run. Recent, Packets, Sessions and Watch answer from memory, which Open fills with the latest results of each session; Range reads the database, so that it reaches back as far as the retention. Per-packet records stay in memory only.

The database uses the WAL journal. Its schema carries a version, upgraded on Open; results older than the retention are deleted every hour.

Index

Constants

View Source
const (
	// DefaultRetention is how long results are kept.
	//
	//declscope:ignore overexported // the default of store.sqlite.retention, for internal/config, which impl-stats wires next
	DefaultRetention = 30 * 24 * time.Hour
)

Defaults of Config.

Variables

View Source
var (
	// ErrCorrupt means the file is not a gotwamp database or failed its
	// integrity check; Config.ResetCorrupt moves it aside instead.
	ErrCorrupt = errors.New("result database is corrupt")
	// ErrNewerSchema means the database was written by a newer gotwampd.
	ErrNewerSchema = errors.New("result database has a newer schema")
)

Errors of Open.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Path is the database file (store.sqlite.path). Required.
	Path string
	// Retention is how long results are kept (store.sqlite.retention);
	// zero means 30 days, negative keeps them for ever.
	Retention time.Duration
	// MaxSize is the size of the database and its WAL above which every
	// sweep logs a warning; zero never warns.
	MaxSize int64
	// History and MaxPackets size the memory in front of the database, as
	// for store.NewMemory: Recent returns at most History results per
	// session.
	History, MaxPackets int
	// Buffer is how many results may wait for the writer; more are dropped
	// and counted. Zero means 4096.
	Buffer int
	// Definitions keeps the session definitions; see DefinitionCodec.
	Definitions DefinitionCodec
	// ResetCorrupt moves a corrupt database aside (to Path plus
	// ".corrupt-" and the time) and starts an empty one, rather than fail.
	ResetCorrupt bool
	// Logger receives the warnings; nil discards them.
	Logger *slog.Logger
	// Now returns the current time, for the retention; nil means time.Now.
	Now func() time.Time
	// Sweep is how often expired results are deleted; zero means an hour.
	Sweep time.Duration
}

Config configures a Store. Zero values take the defaults.

type DefinitionCodec

type DefinitionCodec struct {
	Encode func(def any) ([]byte, error)
	Decode func(b []byte) (any, error)
}

DefinitionCodec turns store.SessionInfo.Definition, which the store does not know, into octets and back. gotwampd passes one for its session definitions; without it the definitions are not kept and come back nil.

type Stats

type Stats struct {
	// Written counts results committed to the database.
	Written uint64
	// Dropped counts results not written because the writer was behind by
	// Config.Buffer, or the store was closed.
	Dropped uint64
	// Failed counts results the database refused.
	Failed uint64
}

Stats are the counters of a Store.

type Store

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

Store is the SQLite result store. It implements store.Store; its methods are safe for concurrent use. Close it to flush and release the database.

func Open

func Open(cfg Config) (*Store, error)

Open opens the database at cfg.Path, creating it, upgrading its schema and deleting what the retention expired, and loads the latest results of each session into memory. A corrupt file is an error wrapping ErrCorrupt, or with cfg.ResetCorrupt is moved aside for an empty database.

func (*Store) Close

func (s *Store) Close() error

Close writes what is queued and closes the database. Results published afterwards are dropped. It may be called more than once.

func (*Store) Delete

func (s *Store) Delete(session string)

Delete implements store.Store: the session's results go from memory at once and from the database in order with the writes queued before.

func (*Store) Packets

func (s *Store) Packets(session string) []store.PacketSample

Packets implements store.Store from memory.

func (*Store) Publish

func (s *Store) Publish(e store.Event)

Publish implements store.ResultSink: the memory and the watchers get e at once, and the summaries and run errors of named sessions are queued for the database. When the writer is Config.Buffer results behind, e is not written and is counted in Stats.Dropped. The Summary and Error of e must not be modified afterwards.

func (*Store) Range

func (s *Store) Range(session string, from, to time.Time) []store.Event

Range implements store.Store from the database, after the writer has caught up: it reaches back as far as the retention, beyond History.

func (*Store) Recent

func (s *Store) Recent(session string, n int) []store.Event

Recent implements store.Store from memory: at most Config.History results.

func (*Store) Sessions

func (s *Store) Sessions() []string

Sessions implements store.Store from memory, which Open fills with every session the database has results of.

func (*Store) Stats

func (s *Store) Stats() Stats

Stats returns the counters.

func (*Store) Watch

func (s *Store) Watch(opts store.WatchOptions) *store.Watcher

Watch implements store.Store.

Jump to

Keyboard shortcuts

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