sqlite

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MPL-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package sqlite provides an implementation of state.State in sqlite.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrAlreadyExists

func ErrAlreadyExists(r resource.Reference) error

ErrAlreadyExists generates error compatible with state.ErrConflict.

func ErrInvalidWatchBookmark

func ErrInvalidWatchBookmark(e error) error

ErrInvalidWatchBookmark generates error compatible with state.ErrInvalidWatchBookmark.

func ErrNotFound

func ErrNotFound(r resource.Pointer) error

ErrNotFound generates error compatible with state.ErrNotFound.

func ErrOwnerConflict

func ErrOwnerConflict(r resource.Pointer, owner string) error

ErrOwnerConflict generates error compatible with state.ErrConflict.

func ErrPendingFinalizers

func ErrPendingFinalizers(r resource.Pointer, fins []string) error

ErrPendingFinalizers generates error compatible with state.ErrConflict.

func ErrPhaseConflict

func ErrPhaseConflict(r resource.Reference, expectedPhase resource.Phase) error

ErrPhaseConflict generates error compatible with ErrConflict.

func ErrUnsupported

func ErrUnsupported(operation string) error

ErrUnsupported generates error compatible with state.ErrUnsupported.

func ErrVersionConflict

func ErrVersionConflict(r resource.Pointer, expected, found uint64) error

ErrVersionConflict generates error compatible with state.ErrConflict.

Types

type CompactionInfo

type CompactionInfo struct {
	EventsCompacted int64
	RemainingEvents int64
}

CompactionInfo holds information about a compaction operation.

type State

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

State implements state storage in sqlite database.

func NewState

func NewState(ctx context.Context, db *sqlitex.Pool, marshaler store.Marshaler, opts ...StateOption) (*State, error)

NewState creates new State with default options.

The following options should be enabled on the sqlite database: [TODO]: update this comment when we finalize the required options

  • busy_timeout pragma should be set to a reasonable value (e.g. 5000 ms)
  • journal_mode pragma should be set to WAL
  • txlock=immediate should be set in the DSN to avoid busy errors on concurrent writes.

func (*State) Close

func (st *State) Close()

Close shuts down the state and releases all resources.

func (*State) Compact

func (st *State) Compact(ctx context.Context) (*CompactionInfo, error)

Compact performs database compaction.

func (*State) Create

func (st *State) Create(ctx context.Context, res resource.Resource, opts ...state.CreateOption) error

Create a resource.

If a resource already exists, Create returns an error.

func (*State) DBSize added in v0.3.0

func (st *State) DBSize(ctx context.Context) (int64, error)

DBSize returns the size in bytes of tables used by this package.

It uses SQLite's dbstat virtual table to calculate the size of the resources and events tables within the main database file (logical table page usage), which does not include any separate WAL/SHM files.

func (*State) Destroy

func (st *State) Destroy(ctx context.Context, ptr resource.Pointer, opts ...state.DestroyOption) error

Destroy a resource.

If a resource doesn't exist, error is returned. If a resource has pending finalizers, error is returned.

func (*State) Get

func (st *State) Get(ctx context.Context, ptr resource.Pointer, opts ...state.GetOption) (resource.Resource, error)

Get a resource by type and ID.

If a resource is not found, error is returned.

func (*State) List

func (st *State) List(ctx context.Context, resourceKind resource.Kind, opts ...state.ListOption) (resource.List, error)

List resources by type.

func (*State) Update

func (st *State) Update(ctx context.Context, newResource resource.Resource, opts ...state.UpdateOption) error

Update a resource.

If a resource doesn't exist, error is returned. On update current version of resource `new` in the state should match the version on the backend, otherwise conflict error is returned.

func (*State) Watch

func (st *State) Watch(ctx context.Context, ptr resource.Pointer, ch chan<- state.Event, opts ...state.WatchOption) error

Watch state of a resource by type.

It's fine to watch for a resource which doesn't exist yet. Watch is canceled when context gets canceled. Watch sends initial resource state as the very first event on the channel, and then sends any updates to the resource as events.

func (*State) WatchKind

func (st *State) WatchKind(ctx context.Context, resourceKind resource.Kind, ch chan<- state.Event, opts ...state.WatchKindOption) error

WatchKind watches resources of specific kind (namespace and type).

func (*State) WatchKindAggregated

func (st *State) WatchKindAggregated(ctx context.Context, resourceKind resource.Kind, ch chan<- []state.Event, opts ...state.WatchKindOption) error

WatchKindAggregated watches resources of specific kind (namespace and type), updates are sent aggregated.

type StateOption

type StateOption func(*StateOptions)

StateOption configures sqlite state.

func WithCompactKeepEvents added in v0.1.1

func WithCompactKeepEvents(keepEvents int) StateOption

WithCompactKeepEvents sets the minimum number of events to keep during compaction.

func WithCompactMinAge

func WithCompactMinAge(minAge time.Duration) StateOption

WithCompactMinAge sets the minimum age of events to keep during compaction.

func WithCompactionInterval

func WithCompactionInterval(interval time.Duration) StateOption

WithCompactionInterval sets the interval between automatic database compactions.

func WithLogger

func WithLogger(logger *zap.Logger) StateOption

WithLogger sets the logger for the sqlite state.

func WithTablePrefix

func WithTablePrefix(prefix string) StateOption

WithTablePrefix sets the table prefix for all tables used by the sqlite state.

type StateOptions

type StateOptions struct {
	// Logger is the logger to use for logging.
	Logger *zap.Logger

	// TablePrefix is the prefix to use for all tables used by the sqlite state.
	//
	// Default is empty string.
	// Setting a table prefix allows multiple independent states to share the same database.
	TablePrefix string

	// CompactionInterval is the interval between automatic database compactions.
	//
	// Default is 30 minutes.
	CompactionInterval time.Duration

	// CompactKeepEvents is the number of events to keep during compaction.
	//
	// This ensures tat at least this many events are kept in the database to avoid
	// event ID wraparound and to allow watches to restart from recent bookmarks.
	//
	// Default is 1000.
	CompactKeepEvents int

	// CompactMinAge is the minimum age of events to keep during compaction.
	//
	// It might be important to keep recent events to allow restarting a watch
	// from a bookmark.
	//
	// Default is 1 hour.
	CompactMinAge time.Duration
}

StateOptions configures sqlite state.

func DefaultStateOptions

func DefaultStateOptions() StateOptions

DefaultStateOptions returns default sqlite state options.

Directories

Path Synopsis
internal
filter
Package filter translates List/Watch request filters into sqlite conditions.
Package filter translates List/Watch request filters into sqlite conditions.
sub
Package sub contains simple subscription management.
Package sub contains simple subscription management.

Jump to

Keyboard shortcuts

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