sqlite

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package sqlite is anfra's query cache store (querycache.Store), on SQLite: one file per repo, under anfra's home. modernc.org/sqlite is pure Go, as anfra builds without cgo. Its queries are queries.sql, generated into gen/ by sqlc (sqlc.yaml).

Import it as sqlitestore, so a call site says it is the store it opens, not the query cache:

import sqlitestore "github.com/holistics/anfra/internal/querycache/store/sqlite"

store, err := sqlitestore.Open(ctx, path, sqlitestore.Options{})

Index

Constants

This section is empty.

Variables

View Source
var CurrentVersion = len(migrations)

CurrentVersion is the schema version this anfra creates and migrates stores to: its last migration's.

View Source
var ErrNotStore = errors.New("not a query cache store")

ErrNotStore is a file that isn't a query cache store: not a database, or one with tables but no store version.

View Source
var ErrOtherVersion = errors.New("the query cache's store is of a newer, incompatible version")

ErrOtherVersion is a store this anfra can't use: a newer anfra changed it in a way this one doesn't know. A *VersionError says which versions.

Functions

func ReadContents

func ReadContents(ctx context.Context, path string, now time.Time) (querycache.Contents, error)

ReadContents reads what the store at path holds at now, without creating, changing or migrating it, as Peek reads its versions: nothing when it isn't there. A store this anfra can't use is a *VersionError, since its tables may not be the ones read here.

Types

type Options

type Options struct {
	// OnCreate, if set, runs when Open creates the store, inside the
	// transaction that creates it. Another process's Open waits for it, so
	// nothing has used the store yet: what OnCreate finds is no entry's.
	OnCreate func()
}

Options are how Open opens a store.

type Store

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

Store is a querycache.Store on one SQLite file.

func Open

func Open(ctx context.Context, path string, o Options) (*Store, error)

Open opens the store at path, creating it when there is none, and migrates it to this anfra's schema version (CurrentVersion). It refuses a newer store this anfra can't use (ErrOtherVersion, a *VersionError) and a file that isn't a store (ErrNotStore), and never changes either. A failed migration leaves the store as it was, to be tried again at the next Open.

func (*Store) Abandon

func (s *Store) Abandon(ctx context.Context, id querycache.EntryID, now time.Time) error

func (*Store) Begin

func (s *Store) Begin(ctx context.Context, e querycache.Entry, tags []querycache.Tag) (querycache.EntryID, error)

func (*Store) Close

func (s *Store) Close() error

Close closes the store.

func (*Store) Commit

func (s *Store) Commit(ctx context.Context, e querycache.Entry, now time.Time) error

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, ids []querycache.EntryID) error

func (*Store) Expire

func (s *Store) Expire(ctx context.Context, tags []querycache.Tag, now time.Time) (querycache.Expired, error)

func (*Store) Get

func (*Store) Lookup

func (s *Store) Lookup(ctx context.Context, key querycache.Key, now time.Time) (querycache.Entry, bool, error)

func (*Store) Sweepable

func (s *Store) Sweepable(ctx context.Context, cutoff time.Time, limit int) ([]querycache.Entry, error)

type VersionError

type VersionError struct{ Versions }

VersionError is a store this anfra can't use (ErrOtherVersion), with its versions.

func (*VersionError) Error

func (e *VersionError) Error() string

func (*VersionError) Is

func (e *VersionError) Is(target error) bool

type Versions

type Versions struct {
	// Installed is the store's schema version, which is the number of the last
	// migration applied to it. SQLite keeps it in the file's header, as
	// user_version.
	Installed int

	// RequiredByInstalled is the lowest CurrentVersion an anfra must have to
	// use the store's installed schema. The store keeps it in query_cache_meta,
	// as min_store_version.
	//
	// Only a breaking migration changes it, when that migration runs on the
	// store. For example:
	//
	//	Migrations run on the store      Installed  RequiredByInstalled  Anfras that can use it
	//	1 and 2, neither breaking        2          1                    at version 1 or later
	//	then 3, which adds a column      3          1                    at version 1 or later
	//	then 4, which drops a column     4          4                    at version 4 or later
	//
	// The store keeps this number because an older anfra can't work it out.
	// It has never seen the migrations after its own, so it can't tell whether
	// they break it. The anfra that ran them can, and records the answer.
	RequiredByInstalled int
}

Versions are a store's schema versions. A store not made yet is at 0.

func Peek

func Peek(ctx context.Context, path string) (Versions, error)

Peek reads the versions of the store at path without changing it, nor creating it: what a status reports. os.ErrNotExist when there is none.

func (Versions) Compatible

func (v Versions) Compatible() bool

Compatible reports whether this anfra can use the store: as it is, or once migrated.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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