db

package
v0.5.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

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 ID.
	Get(ctx context.Context, id string) (*model.Note, error)

	// Delete removes a note by ID.
	Delete(ctx context.Context, id 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

	// 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 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) Delete

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

Delete removes a note by ID prefix.

func (*PostgresAdapter) Get

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

Get fetches a single note by ID prefix.

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

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

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

Delete removes a note and its embedding by ID prefix.

func (*SQLiteAdapter) Get

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

Get fetches a single note by ID prefix.

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

Jump to

Keyboard shortcuts

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