Help us shape the future of Go! Take the 2026 Go Developer Survey

db

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned by Get and Delete when no note has the given ID.

View Source
var ErrSchemaTooNew = errors.New("database schema is newer than this version of inode supports; upgrade inode")

ErrSchemaTooNew is returned when a database was migrated by a newer inode than the one opening it. Opening it anyway could corrupt data the older code does not understand.

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	// Save persists a note with its embedding. Returns the assigned ID.
	Save(ctx context.Context, note *model.Note) (string, error)

	// Get fetches a single note by its full ID. Returns ErrNotFound if absent.
	Get(ctx context.Context, id string) (*model.Note, error)

	// Delete removes a note by its full ID. Returns ErrNotFound if absent.
	Delete(ctx context.Context, id string) error

	// MatchIDs returns up to limit full IDs that start with prefix, compared
	// literally (no wildcards), in ID order. Callers resolve short IDs with it.
	MatchIDs(ctx context.Context, prefix string, limit int) ([]string, error)

	// SearchSimilar returns the top-K notes by cosine similarity to vec,
	// optionally filtered by Filters.
	SearchSimilar(ctx context.Context, vec []float32, topK int, filters Filters) ([]*model.Note, error)

	// List returns notes matching filters, with pagination.
	List(ctx context.Context, filters Filters, limit, offset int) ([]*model.Note, error)

	// UncheckedSensitive returns sensitive notes whose summary has not yet
	// been validated against their content (notes saved before v0.5.0).
	UncheckedSensitive(ctx context.Context) ([]*model.Note, error)

	// SetSummary replaces a note's summary by full ID and marks it validated.
	SetSummary(ctx context.Context, id, summary string) error

	// EmbeddingSpace returns the recorded embedding backend, model and
	// dimension. ok is false when none is recorded (databases created before
	// v0.6.0).
	EmbeddingSpace(ctx context.Context) (space EmbeddingSpace, ok bool, err error)

	// SetEmbeddingSpace records the embedding backend, model and dimension.
	SetEmbeddingSpace(ctx context.Context, s EmbeddingSpace) error

	// VectorDimension returns the vector size the schema was created with.
	VectorDimension(ctx context.Context) (int, error)

	// ResizeVectors changes the vector size of the schema. It fails if any
	// note has an embedding.
	ResizeVectors(ctx context.Context, dimension int) error

	// CountNotes returns the number of stored notes.
	CountNotes(ctx context.Context) (int, error)

	// Close releases the database connection.
	Close() error
}

Adapter defines the contract for all database backends. Implementations: sqlite.go (Phase 1), postgres.go (Phase 2).

type EmbeddingSpace added in v0.6.0

type EmbeddingSpace struct {
	Backend   string
	Model     string
	Dimension int
}

EmbeddingSpace identifies what produced a database's stored vectors. Vectors from different models cannot be compared, even when they have the same dimension. An empty Model means the model is unknown.

type Filters

type Filters struct {
	Category    string
	Tags        []string
	IsSensitive *bool // nil = no filter
}

Filters for list and search queries.

type PostgresAdapter

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

PostgresAdapter implements db.Adapter using PostgreSQL + pgvector.

Compared to the SQLite path, this backend stores the embedding inline on the notes table (pgvector's vector(N) column type) rather than in a separate virtual table. L2 distance is computed by the `<->` operator, matching sqlite-vec's metric so the relevance threshold is portable.

id is stored as TEXT (not UUID) so prefix-match semantics on Get/Delete stay identical to the SQLite implementation.

func NewPostgresAdapter

func NewPostgresAdapter(ctx context.Context, dsn string, dimension int) (*PostgresAdapter, error)

NewPostgresAdapter connects to Postgres, registers pgvector types, and runs migrations. The DSN follows libpq format, e.g.:

postgres://user:pass@host:5432/db?sslmode=disable

The pgvector extension is created if missing — the connecting role therefore needs CREATE privilege on the database (superuser in the standard pgvector/pgvector Docker image).

func (*PostgresAdapter) Close

func (a *PostgresAdapter) Close() error

Close releases the pool.

func (*PostgresAdapter) CountNotes added in v0.6.0

func (a *PostgresAdapter) CountNotes(ctx context.Context) (int, error)

func (*PostgresAdapter) Delete

func (a *PostgresAdapter) Delete(ctx context.Context, id string) error

Delete removes a note by full ID.

func (*PostgresAdapter) EmbeddingSpace added in v0.6.0

func (a *PostgresAdapter) EmbeddingSpace(ctx context.Context) (EmbeddingSpace, bool, error)

func (*PostgresAdapter) Get

func (a *PostgresAdapter) Get(ctx context.Context, id string) (*model.Note, error)

Get fetches a single note by full ID.

func (*PostgresAdapter) List

func (a *PostgresAdapter) List(ctx context.Context, filters Filters, limit, offset int) ([]*model.Note, error)

List returns notes matching filters with pagination.

func (*PostgresAdapter) MatchIDs added in v0.5.1

func (a *PostgresAdapter) MatchIDs(ctx context.Context, prefix string, limit int) ([]string, error)

MatchIDs returns up to limit IDs starting with prefix. left() compares the prefix literally, so % and _ in the input are not wildcards.

func (*PostgresAdapter) ResizeVectors added in v0.6.0

func (a *PostgresAdapter) ResizeVectors(ctx context.Context, dimension int) error

func (*PostgresAdapter) Save

func (a *PostgresAdapter) Save(ctx context.Context, note *model.Note) (string, error)

Save persists a note and its embedding. Returns the assigned UUID.

func (*PostgresAdapter) SearchSimilar

func (a *PostgresAdapter) SearchSimilar(ctx context.Context, vec []float32, topK int, filters Filters) ([]*model.Note, error)

SearchSimilar returns top-K notes by L2 distance to vec, ascending. Each returned note has Distance populated from the pgvector `<->` operator.

Category filter is pushed down; tag filter is applied in-memory after fetching to match the SQLite path's semantics (OR across tags, not AND).

func (*PostgresAdapter) SetEmbeddingSpace added in v0.6.0

func (a *PostgresAdapter) SetEmbeddingSpace(ctx context.Context, s EmbeddingSpace) error

func (*PostgresAdapter) SetSummary

func (a *PostgresAdapter) SetSummary(ctx context.Context, id, summary string) error

SetSummary replaces a note's summary and marks it validated.

func (*PostgresAdapter) UncheckedSensitive

func (a *PostgresAdapter) UncheckedSensitive(ctx context.Context) ([]*model.Note, error)

UncheckedSensitive returns sensitive notes whose summary has not been validated against their content.

func (*PostgresAdapter) VectorDimension added in v0.6.0

func (a *PostgresAdapter) VectorDimension(ctx context.Context) (int, error)

VectorDimension reads the type modifier of notes.embedding, which for pgvector's vector(n) is n itself.

type SQLiteAdapter

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

SQLiteAdapter implements DBAdapter using SQLite + sqlite-vec.

func NewSQLiteAdapter

func NewSQLiteAdapter(path string, dimension int) (*SQLiteAdapter, error)

NewSQLiteAdapter opens (or creates) the SQLite database and runs migrations. dimension is the embedding vector size (e.g. 768 for nomic-embed-text, 1024 for voyage-3).

func (*SQLiteAdapter) Close

func (a *SQLiteAdapter) Close() error

Close releases the database connection.

func (*SQLiteAdapter) CountNotes added in v0.6.0

func (a *SQLiteAdapter) CountNotes(ctx context.Context) (int, error)

func (*SQLiteAdapter) Delete

func (a *SQLiteAdapter) Delete(ctx context.Context, id string) error

Delete removes a note and its embedding by full ID.

func (*SQLiteAdapter) EmbeddingSpace added in v0.6.0

func (a *SQLiteAdapter) EmbeddingSpace(ctx context.Context) (EmbeddingSpace, bool, error)

func (*SQLiteAdapter) Get

func (a *SQLiteAdapter) Get(ctx context.Context, id string) (*model.Note, error)

Get fetches a single note by full ID.

func (*SQLiteAdapter) List

func (a *SQLiteAdapter) List(ctx context.Context, filters Filters, limit, offset int) ([]*model.Note, error)

List returns notes matching filters with pagination.

func (*SQLiteAdapter) MatchIDs added in v0.5.1

func (a *SQLiteAdapter) MatchIDs(ctx context.Context, prefix string, limit int) ([]string, error)

MatchIDs returns up to limit IDs starting with prefix. substr compares the prefix literally, so % and _ in the input are not wildcards.

func (*SQLiteAdapter) ResizeVectors added in v0.6.0

func (a *SQLiteAdapter) ResizeVectors(ctx context.Context, dimension int) error

ResizeVectors recreates the vec0 table, which cannot be altered in place.

func (*SQLiteAdapter) Save

func (a *SQLiteAdapter) Save(ctx context.Context, note *model.Note) (string, error)

Save persists a note and its embedding. Returns the assigned UUID.

func (*SQLiteAdapter) SearchSimilar

func (a *SQLiteAdapter) SearchSimilar(ctx context.Context, vec []float32, topK int, filters Filters) ([]*model.Note, error)

SearchSimilar returns top-K notes by L2 distance to vec, in ascending order. Each returned note has its Distance field populated.

func (*SQLiteAdapter) SetEmbeddingSpace added in v0.6.0

func (a *SQLiteAdapter) SetEmbeddingSpace(ctx context.Context, s EmbeddingSpace) error

func (*SQLiteAdapter) SetSummary

func (a *SQLiteAdapter) SetSummary(ctx context.Context, id, summary string) error

SetSummary replaces a note's summary and marks it validated.

func (*SQLiteAdapter) UncheckedSensitive

func (a *SQLiteAdapter) UncheckedSensitive(ctx context.Context) ([]*model.Note, error)

scanNote scans a single row into a Note. UncheckedSensitive returns sensitive notes whose summary has not been validated against their content.

func (*SQLiteAdapter) VectorDimension added in v0.6.0

func (a *SQLiteAdapter) VectorDimension(ctx context.Context) (int, error)

VectorDimension reads the size from the vec0 table definition, since sqlite-vec exposes no other way to ask for it.

Jump to

Keyboard shortcuts

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