sqlcache

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: LGPL-3.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sqlcache implements tagging.EmbeddingCache on database/sql for the databases a Dialect describes. The public backends, sqlitecache and pgcache, are this package with their own Dialect and their own check of the database they get.

All indexes share one plain table, autotag_embeddings, keyed by index, model, role and the SHA-256 of the text. There is no vector extension and no per-index table: the cache is derived data that is only ever read by key, so an index never reaches SQL as an identifier, only as a bound value.

Index

Constants

This section is empty.

Variables

View Source
var Postgres = Dialect{Blob: "BYTEA", Int: "BIGINT", Numbered: true,
	SchemaLock: "SELECT pg_advisory_xact_lock(7022647215996888927)"}

Postgres is the Dialect of PostgreSQL. Two CREATE TABLE IF NOT EXISTS that run together both find no table and one then fails on Postgres's own catalog, so processes that start together take a transaction-level advisory lock first; its key is the eight bytes of "autotag_" as a number, for no reason but to be this table's own.

View Source
var SQLite = Dialect{Blob: "BLOB", Int: "INTEGER"}

SQLite is the Dialect of SQLite. It needs no SchemaLock: a transaction that writes already waits for the one writer the database allows.

Functions

This section is empty.

Types

type Cache

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

Cache implements tagging.EmbeddingCache on SQL. It is safe for concurrent use as far as its *sql.DB is.

func New

func New(ctx context.Context, db *sql.DB, d Dialect, now func() time.Time) (*Cache, error)

New returns a Cache that keeps its table in db, in the SQL of d, creating the table if it is missing. now is the clock the last-used times come from; tests inject theirs; a nil one is refused rather than left to panic on the first Put. It does not look at the database, which its caller knows how to check.

func (*Cache) DeleteIndex

func (c *Cache) DeleteIndex(ctx context.Context, index string) error

DeleteIndex implements tagging.EmbeddingCache.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, index string, keys []tagging.CacheKey) (map[tagging.CacheKey][]float32, error)

Get implements tagging.EmbeddingCache. Keys are grouped by model and role and matched 500 at a time; the entries found last used tagging.CacheRefreshAfter ago or longer, to the second, get their last-used time rewritten, as in every backend (see cachetest). When that write fails, on a read-only replica or a locked file, the vectors were read all the same: Get returns all of them together with an error wrapping tagging.ErrCacheWrite.

func (*Cache) Prune

func (c *Cache) Prune(ctx context.Context, unusedSince time.Time) (int64, error)

Prune implements tagging.EmbeddingCache.

func (*Cache) Put

func (c *Cache) Put(ctx context.Context, index string, vecs map[tagging.CacheKey][]float32) error

Put implements tagging.EmbeddingCache. It stores every vector in one transaction, so a failure leaves none of them, upserting them in the order of the primary key (see keyOrder), so concurrent Puts of the same keys do not deadlock.

func (*Cache) Stats added in v0.3.1

func (c *Cache) Stats(ctx context.Context) (tagging.CacheStats, error)

Stats implements tagging.EmbeddingCache. A vector is stored as its little-endian float32s, so the length of the column is its size.

func (*Cache) Touch

func (c *Cache) Touch(ctx context.Context, index string, keys []tagging.CacheKey) error

Touch implements tagging.EmbeddingCache.

type Dialect

type Dialect struct {
	Blob, Int string // vector/hash and role/time column types
	Numbered  bool   // $1, $2… placeholders instead of ?
	// SchemaLock, when not empty, is a statement that makes the transaction
	// creating the schema wait for any other one doing the same.
	SchemaLock string
}

Dialect holds what differs between the SQL databases a Cache runs on. The statements are written once, with ? placeholders, and are otherwise plain SQL that these databases share.

Jump to

Keyboard shortcuts

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