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 ¶
- Variables
- type Cache
- func (c *Cache) DeleteIndex(ctx context.Context, index string) error
- func (c *Cache) Get(ctx context.Context, index string, keys []tagging.CacheKey) (map[tagging.CacheKey][]float32, error)
- func (c *Cache) Prune(ctx context.Context, unusedSince time.Time) (int64, error)
- func (c *Cache) Put(ctx context.Context, index string, vecs map[tagging.CacheKey][]float32) error
- func (c *Cache) Touch(ctx context.Context, index string, keys []tagging.CacheKey) error
- type Dialect
Constants ¶
This section is empty.
Variables ¶
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.
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 ¶
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 ¶
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.
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.