sqlitecache

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: 7 Imported by: 1 module, 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 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

func New(ctx context.Context, db *sql.DB) (*Cache, error)

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)
}

Jump to

Keyboard shortcuts

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