sqlite

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package sqlite is a persistent backend: one SQLite database file holding all collections, documents, chunks, and vectors. A single Store exposes the three persistence ports through Collections/Documents/Vectors accessors — Go forbids one type from having several same-named methods (Upsert, Delete), so the ports are separate types sharing the *sql.DB. Vector search is brute-force cosine in Go over stored BLOBs. The driver is pure-Go (modernc.org/sqlite): no cgo, so the static-binary and cross-compile goals hold.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnswerCache

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

AnswerCache is the SQLite-backed app.AnswerCache: synthesized answers keyed by an opaque content hash, surviving across processes (the point of caching for a CLI). The answer is stored as JSON; timestamps are RFC3339Nano text so the stored_at index orders correctly.

func (*AnswerCache) Get

func (c *AnswerCache) Get(ctx context.Context, key string, notBefore time.Time) (app.Answer, bool, error)

Get returns the cached answer for key when present and stored at or after notBefore; otherwise ok is false.

func (*AnswerCache) Prune

func (c *AnswerCache) Prune(ctx context.Context, cutoff time.Time) error

Prune deletes every entry stored before cutoff.

func (*AnswerCache) Put

func (c *AnswerCache) Put(ctx context.Context, key string, answer app.Answer, storedAt time.Time) error

Put stores answer under key (replacing any prior entry), stamped storedAt.

type CollectionRepository

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

CollectionRepository is the SQLite-backed CollectionRepository.

func (*CollectionRepository) Create

Create inserts the collection, failing with ErrAlreadyExists if the name is taken. The INSERT ... ON CONFLICT DO NOTHING + RowsAffected check is atomic.

func (*CollectionRepository) Delete

func (r *CollectionRepository) Delete(ctx context.Context, name string) error

Delete removes the collection record and its recorded sources, or fails with ErrNotFound. The cross-aggregate cascade (documents and vectors) is the use case's job.

func (*CollectionRepository) Get

Get returns the named collection (with its recorded sources), or ErrNotFound.

func (*CollectionRepository) List

List returns every collection (with its recorded sources), in unspecified order.

func (*CollectionRepository) RecordSource

func (r *CollectionRepository) RecordSource(ctx context.Context, name, source string) error

RecordSource adds source to the collection's recorded sources (idempotent via the composite primary key), or fails with ErrNotFound if no such collection.

type DocumentRepository

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

DocumentRepository is the SQLite-backed DocumentRepository.

func (*DocumentRepository) Delete

func (r *DocumentRepository) Delete(ctx context.Context, collection string, id domain.DocumentID) ([]domain.ChunkID, error)

Delete removes the document and its chunks, returning the removed chunk IDs, or fails with ErrNotFound.

func (*DocumentRepository) DeleteChunks

func (r *DocumentRepository) DeleteChunks(ctx context.Context, collection string, ids []domain.ChunkID) ([]domain.ChunkID, error)

DeleteChunks removes the given chunks that belong to the collection (their documents are left in place), returning the IDs actually removed. IDs absent from the collection are skipped. Resolving which IDs belong to the collection before deleting keeps the returned set exact — the vector cascade needs it.

func (*DocumentRepository) DeleteCollection

func (r *DocumentRepository) DeleteCollection(ctx context.Context, collection string) ([]domain.ChunkID, error)

DeleteCollection removes every document and its chunks in the collection, returning all removed chunk IDs. An empty collection is a no-op.

func (*DocumentRepository) GetBySource

func (r *DocumentRepository) GetBySource(ctx context.Context, collection, sourceURI string) (*domain.Document, error)

GetBySource returns the document for (collection, sourceURI), or ErrNotFound.

func (*DocumentRepository) GetChunks

func (r *DocumentRepository) GetChunks(ctx context.Context, ids []domain.ChunkID) ([]domain.Chunk, error)

GetChunks hydrates chunks by ID, preserving input order and skipping IDs with no stored chunk.

func (*DocumentRepository) GetChunksByDocument

func (r *DocumentRepository) GetChunksByDocument(ctx context.Context, collection string, id domain.DocumentID) ([]domain.Chunk, error)

GetChunksByDocument returns all chunks of one document in seq order. An unknown document (or collection) yields no chunks and no error.

func (*DocumentRepository) GetChunksByIDs

func (r *DocumentRepository) GetChunksByIDs(ctx context.Context, collection string, ids []string) ([]domain.Chunk, error)

GetChunksByIDs returns the chunks with the given IDs that belong to the collection, in input order. IDs absent from the collection (unknown, or owned by another collection) are omitted. An unknown collection yields no chunks.

func (*DocumentRepository) GetDocuments

func (r *DocumentRepository) GetDocuments(ctx context.Context, ids []domain.DocumentID) ([]*domain.Document, error)

GetDocuments hydrates documents by ID, preserving input order and skipping IDs with no stored document.

func (*DocumentRepository) ListDocuments

func (r *DocumentRepository) ListDocuments(ctx context.Context, collection string) ([]*domain.Document, error)

ListDocuments returns every document in the collection, ordered by source URI. An unknown or empty collection yields no documents and no error.

func (*DocumentRepository) Upsert

func (r *DocumentRepository) Upsert(ctx context.Context, doc *domain.Document, chunks []domain.Chunk) error

Upsert stores the document and replaces its chunks, atomically.

type LexicalIndex added in v1.0.0

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

LexicalIndex is the SQLite FTS5-backed LexicalIndex: chunk text is indexed in an FTS5 virtual table and ranked by its built-in BM25. The metadata predicate is evaluated in Go after the MATCH (the same single-source-of-truth choice the vector index makes), so it cannot drift from the memstore reference.

func (*LexicalIndex) Delete added in v1.0.0

func (x *LexicalIndex) Delete(ctx context.Context, collection string, ids []domain.ChunkID) error

Delete removes the given chunk IDs from the collection; absent IDs are a no-op.

func (*LexicalIndex) Search added in v1.0.0

func (x *LexicalIndex) Search(ctx context.Context, collection, query string, k int, filter domain.Predicate) ([]domain.ChunkID, error)

Search returns up to k chunk IDs whose content matches any query term, ranked by FTS5 BM25 (best first), filtered to those whose metadata satisfies filter.

func (*LexicalIndex) Upsert added in v1.0.0

func (x *LexicalIndex) Upsert(ctx context.Context, collection string, docs []app.LexicalDoc) error

Upsert replaces each chunk's lexical content. FTS5 has no UPSERT, so each row is deleted then inserted within one transaction.

type Store

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

Store is a SQLite-backed persistence engine.

func Open

func Open(path string) (*Store, error)

Open opens (creating if needed) the database at path and applies the schema. Pass ":memory:" for an ephemeral store. The pool is capped to one connection: SQLite is single-writer, and an in-memory database lives on its connection.

func (*Store) Cache

func (s *Store) Cache() *AnswerCache

Cache returns the AnswerCache view of the store.

func (*Store) Close

func (s *Store) Close() error

Close releases the database handle.

func (*Store) Collections

func (s *Store) Collections() *CollectionRepository

Collections returns the CollectionRepository view of the store.

func (*Store) Documents

func (s *Store) Documents() *DocumentRepository

Documents returns the DocumentRepository view of the store.

func (*Store) Lexical added in v1.0.0

func (s *Store) Lexical() *LexicalIndex

Lexical returns the LexicalIndex (FTS5) view of the store.

func (*Store) Vectors

func (s *Store) Vectors() *VectorIndex

Vectors returns the VectorIndex view of the store.

type VectorIndex

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

VectorIndex is the SQLite-backed VectorIndex: vectors are stored as BLOBs and searched by brute-force cosine in Go.

func (*VectorIndex) Delete

func (x *VectorIndex) Delete(ctx context.Context, collection string, ids []domain.ChunkID) error

Delete removes the given chunk IDs from the collection; absent IDs are a no-op.

func (*VectorIndex) Entries

func (x *VectorIndex) Entries(ctx context.Context, collection string) ([]app.VectorEntry, error)

Entries returns every stored (chunk_id, vector, metadata) for the collection. An unknown collection yields no entries and no error.

func (*VectorIndex) Search

func (x *VectorIndex) Search(ctx context.Context, collection string, query []float32, k int, filter domain.Predicate) ([]domain.VectorMatch, error)

Search returns up to k matches by cosine similarity, best first, considering only rows whose metadata satisfies filter. The predicate is evaluated in Go (in the brute-force scan the index already runs), not translated to SQL, so the domain evaluator is the single source of truth for filter semantics and memstore and sqlite cannot diverge. The zero predicate skips the metadata decode entirely.

func (*VectorIndex) Upsert

func (x *VectorIndex) Upsert(ctx context.Context, collection string, entries []app.VectorEntry) error

Upsert stores the vectors, replacing entries with the same ChunkID.

Jump to

Keyboard shortcuts

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