store

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package store implements the persistence layer for Coggo v0.1.

The backend is SQLite (via ncruces/go-sqlite3) augmented with the sqlite-vec extension for vector similarity search. A single SQLite database backs the whole binary; per-peer isolation is enforced at the application layer by tagging every row with PeerDID.

See .ai/docs/decisions.md for the rationale on choosing SQLite over the originally planned SurrealDB embedded backend.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

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

Store is the SQLite-backed implementation of types.Store.

func New

func New(dbPath string, dim int) (*Store, error)

New constructs a Store backed by a SQLite database at the given path. The dim argument fixes the dimensionality of vectors stored in the entity_embeddings vec0 virtual table; it must match the embedder used to generate vectors that will be inserted later.

The returned Store is not yet initialised — call Init before use.

func (*Store) AppendEvent

func (s *Store) AppendEvent(ctx context.Context, ev *types.Event) error

AppendEvent inserts an event into the append-only log. If ev.ID is empty a fresh ULID is allocated; if ev.Timestamp is zero, the current UTC time is used.

func (*Store) ArchiveEntity

func (s *Store) ArchiveEntity(ctx context.Context, peerDID, id string, at time.Time) error

ArchiveEntity marks an entity as archived without deleting it.

func (*Store) Close

func (s *Store) Close() error

Close closes the underlying database handle.

func (*Store) DB

func (s *Store) DB() *sql.DB

DB returns the underlying *sql.DB. Intended for test code that needs to poke at the schema directly (e.g. asserting append-only triggers fire). Production code MUST go through the Store interface.

func (*Store) DissolveRelation

func (s *Store) DissolveRelation(ctx context.Context, peerDID, id string) error

DissolveRelation removes a relation row. v0.1 deletes outright; the canonical record lives in the events log.

func (*Store) GetEntity

func (s *Store) GetEntity(ctx context.Context, peerDID, id string) (*types.Entity, error)

GetEntity returns the entity with the given (peer, id), or (nil, nil) if not found.

func (*Store) Init

func (s *Store) Init(ctx context.Context) error

Init creates the schema if it does not exist. Idempotent.

func (*Store) ListEvents

func (s *Store) ListEvents(ctx context.Context, peerDID string, since, until time.Time) ([]*types.Event, error)

ListEvents returns events in [since, until], ascending by timestamp then ID. Zero values for since/until mean unbounded on that side.

func (*Store) QueryEntities

func (s *Store) QueryEntities(ctx context.Context, peerDID string, q types.EntityQuery) ([]*types.Entity, error)

QueryEntities lists entities for a peer, optionally filtered by type and JSON field equality. Archived rows are excluded unless q.IncludeArchived is true. Default Limit is 50.

func (*Store) QueryRelations

func (s *Store) QueryRelations(ctx context.Context, peerDID, from, to, relType string, limit int) ([]*types.Relation, error)

QueryRelations returns relations matching the given non-empty filters.

func (*Store) SemanticSearch

func (s *Store) SemanticSearch(ctx context.Context, peerDID string, vector []float32, typeFilter string, limit int) ([]types.SemanticHit, error)

SemanticSearch returns the top-`limit` entities by cosine similarity to the given vector, restricted to the given peer (and optionally entity type). Returns an empty slice if vector is empty.

func (*Store) SubstringSearch

func (s *Store) SubstringSearch(ctx context.Context, peerDID, query, typeFilter string, limit int) ([]types.SemanticHit, error)

SubstringSearch returns entities whose JSON data contains the given query substring. Score is always 1.0.

func (*Store) TimeTravelEntities

func (s *Store) TimeTravelEntities(ctx context.Context, peerDID string, q types.EntityQuery, asOf time.Time) ([]*types.Entity, error)

TimeTravelEntities reconstructs entity state by folding the event log up to (and including) asOf, then applying the EntityQuery filters in Go. Intended for occasional historical queries — the spec marks this "use sparingly".

func (*Store) UpsertEmbedding

func (s *Store) UpsertEmbedding(ctx context.Context, peerDID, entityID string, vector []float32, model string) error

UpsertEmbedding writes or replaces the embedding for an entity.

func (*Store) UpsertEntity

func (s *Store) UpsertEntity(ctx context.Context, e *types.Entity) error

UpsertEntity inserts or replaces an entity row.

func (*Store) UpsertRelation

func (s *Store) UpsertRelation(ctx context.Context, r *types.Relation) error

UpsertRelation inserts or replaces a relation row.

Jump to

Keyboard shortcuts

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