Documentation
¶
Overview ¶
Package sqlitecache is a tagging.EmbeddingCache backed by SQLite. It needs no extension: the embeddings of every index live in one plain table. It uses SQLite's upserts, so SQLite 3.24 or newer.
The cache uses the *sql.DB it is given, so the application imports a SQLite driver and opens the database. SQLite allows one writer at a time. When several goroutines write, open the database so that a writer waits for its turn rather than failing with "database is locked", for example with github.com/mattn/go-sqlite3:
db, err := sql.Open("sqlite3", "autotag.db?_txlock=immediate&_busy_timeout=5000")
_txlock=immediate makes every transaction take the write lock as it begins, and waiting for that lock is what the busy timeout covers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a tagging.EmbeddingCache on SQLite, in one table of db: Get, Put, Touch, DeleteIndex and Prune are the interface's. It is safe for concurrent use.
func New ¶
New returns a Cache using db, creating its table if needed, and checks that db is a working SQLite database.
Example ¶
This example mirrors the README's automatic tagging: a Tagger over the application's Catalog with its embeddings cached in SQLite, and the scheduled Prune. It has no output, so it is compiled and not run: emb and catalog are the application's.
package main
import (
"context"
"database/sql"
"log"
"time"
"github.com/go-bumbu/sense"
"github.com/go-bumbu/sense/tagging"
"github.com/go-bumbu/sense/tagging/sqlitecache"
_ "github.com/mattn/go-sqlite3"
)
func main() {
ctx := context.Background()
var (
emb interface {
sense.Embedder
sense.Identified
} // e.g. yzma.New's Embedder
catalog tagging.Catalog // the application's tags and hand-made links
)
db, err := sql.Open("sqlite3", "autotag.db?_busy_timeout=5000&_txlock=immediate")
if err != nil {
log.Fatal(err)
}
cache, err := sqlitecache.New(ctx, db)
if err != nil {
log.Fatal(err)
}
tagger, err := tagging.NewTagger(emb, catalog, cache, tagging.TaggerConfig{Model: emb.Model()})
if err != nil {
log.Fatal(err)
}
defer func() { _ = tagger.Close() }()
space := tagger.Scope(tagging.Space{Index: "tenant-1", Partition: "space-7"})
ids, err := space.Suggest(ctx, "rev-12", "Baking sourdough bread at home")
if err != nil {
log.Fatal(err)
}
log.Println(ids)
spaces := []tagging.Space{{Index: "tenant-1", Partition: "space-7"}} // every space still in use
removed, err := tagger.Prune(ctx, time.Now().AddDate(0, 0, -30), spaces...)
log.Println(removed, err)
}
Output: