store

package
v0.0.0-beta.31 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CosineSimilarity

func CosineSimilarity(a, b []float32) float32

func DecodeVector

func DecodeVector(b []byte) []float32

func EncodeVector

func EncodeVector(v []float32) []byte

Types

type Chunk

type Chunk struct {
	ID         string
	DocID      string
	ChunkIndex int
	Content    string
	TokenCount int
	Metadata   string
}

type ChunkWithEmbedding

type ChunkWithEmbedding struct {
	Chunk  Chunk
	Vector []float32
}

type Claim

type Claim struct {
	ID       string `json:"id"`
	EntityID string `json:"entity_id,omitempty"`
	Claim    string `json:"claim"`
	Status   string `json:"status,omitempty"`
	DocID    string `json:"doc_id,omitempty"`
}

type Community

type Community struct {
	ID       string    `json:"id"`
	Level    int       `json:"level"`
	ParentID string    `json:"parent_id,omitempty"`
	Title    string    `json:"title,omitempty"`
	Summary  string    `json:"summary,omitempty"`
	Rank     int       `json:"rank"`
	Vector   []float32 `json:"vector,omitempty"`
}

type Document

type Document struct {
	ID           string `json:"id"`
	Path         string `json:"path"`
	Title        string `json:"title"`
	DocType      string `json:"doc_type"`
	FileHash     string `json:"file_hash,omitempty"`
	Structured   string `json:"structured,omitempty"`
	Version      int    `json:"version,omitempty"`
	CanonicalID  string `json:"canonical_id,omitempty"` // empty on v1; ID of first version on v2+
	IsLatest     bool   `json:"is_latest,omitempty"`
	IndexedMtime int64  `json:"indexed_mtime,omitempty"` // Unix seconds mtime observed at index time
	CreatedAt    int64  `json:"created_at"`
	UpdatedAt    int64  `json:"updated_at"`
}

func (*Document) CanonicalOrID

func (d *Document) CanonicalOrID() string

CanonicalOrID returns CanonicalID if set, otherwise the document's own ID. This is the stable identifier across all versions of a file.

type Entity

type Entity struct {
	ID          string    `json:"id"`
	Name        string    `json:"name"`
	Type        string    `json:"type,omitempty"`
	Description string    `json:"description,omitempty"`
	Rank        int       `json:"rank"`
	CommunityID string    `json:"community_id,omitempty"`
	Vector      []float32 `json:"vector,omitempty"`
}

type NoteHit

type NoteHit struct {
	Key     string   `json:"key"`
	Title   string   `json:"title"`
	Snippet string   `json:"snippet"`
	Tags    []string `json:"tags,omitempty"`
	Rank    float64  `json:"rank"`
}

NoteHit is a single result from SearchNotes.

type Relationship

type Relationship struct {
	ID          string  `json:"id"`
	SourceID    string  `json:"source_id"`
	TargetID    string  `json:"target_id"`
	Predicate   string  `json:"predicate"`
	Description string  `json:"description,omitempty"`
	Weight      float64 `json:"weight"`
	DocID       string  `json:"doc_id"`
}

type Stats

type Stats struct {
	Documents     int `json:"documents"`
	Chunks        int `json:"chunks"`
	Embeddings    int `json:"embeddings"`
	Entities      int `json:"entities"`
	Relationships int `json:"relationships"`
	Claims        int `json:"claims"`
	Communities   int `json:"communities"`
}

type Store

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

Store wraps the single SQLite database.

func Open

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

Open opens (or creates) the SQLite database at path and runs migrations.

func OpenForProject

func OpenForProject(dataDir, slug string) (*Store, error)

OpenForProject opens (or creates) the SQLite DB for a single project at $dataDir/projects/<slug>/docscontext.db. The directory is created with 0o755 if missing. Validates slug against the canonical charset ([a-z0-9_-]+) so we never open a DB at a path the registry couldn't round-trip.

Callers that still need the flat legacy path can keep using Open(path); this function is additive.

func (*Store) AllChunkEmbeddings

func (s *Store) AllChunkEmbeddings(ctx context.Context, model string) ([]ChunkWithEmbedding, error)

func (*Store) AllCommunities

func (s *Store) AllCommunities(ctx context.Context) ([]*Community, error)

func (*Store) AllDocuments

func (s *Store) AllDocuments(ctx context.Context) ([]*Document, error)

AllDocuments returns every row in the documents table regardless of is_latest or doc_type. Used by `docsiq index --prune` to scan for documents whose backing file has disappeared.

func (*Store) AllEntities

func (s *Store) AllEntities(ctx context.Context) ([]*Entity, error)

func (*Store) AllRelationships

func (s *Store) AllRelationships(ctx context.Context) ([]*Relationship, error)

func (*Store) BatchInsertChunks

func (s *Store) BatchInsertChunks(ctx context.Context, chunks []*Chunk) error

BatchInsertChunks inserts multiple chunks in one transaction.

func (*Store) BatchInsertClaims

func (s *Store) BatchInsertClaims(ctx context.Context, claims []*Claim) error

BatchInsertClaims inserts multiple claims in one transaction.

func (*Store) BatchInsertCommunityMembers

func (s *Store) BatchInsertCommunityMembers(ctx context.Context, communityID string, entityIDs []string) error

BatchInsertCommunityMembers inserts community memberships in one transaction.

func (*Store) BatchInsertRelationships

func (s *Store) BatchInsertRelationships(ctx context.Context, rels []*Relationship) error

BatchInsertRelationships inserts multiple relationships in one transaction.

func (*Store) BatchUpdateEntityCommunities

func (s *Store) BatchUpdateEntityCommunities(ctx context.Context, assignments map[string]string) error

BatchUpdateEntityCommunities updates community_id for multiple entities in one transaction.

func (*Store) BatchUpdateEntityRanks

func (s *Store) BatchUpdateEntityRanks(ctx context.Context, ranks map[string]int) error

BatchUpdateEntityRanks updates rank for multiple entities in one transaction.

func (*Store) BatchUpsertEmbeddings

func (s *Store) BatchUpsertEmbeddings(ctx context.Context, model string, chunkIDs []string, vectors [][]float32) error

BatchUpsertEmbeddings upserts multiple embeddings in one transaction.

func (*Store) BatchUpsertEntities

func (s *Store) BatchUpsertEntities(ctx context.Context, entities []*Entity) error

BatchUpsertEntities upserts multiple entities in one transaction.

func (*Store) ClaimsForEntity

func (s *Store) ClaimsForEntity(ctx context.Context, entityID string) ([]*Claim, error)

ClaimsForEntity returns all claims attached to the given entity ID. Returns an empty slice (not nil) on no-match so JSON encoders emit `[]` rather than `null`.

func (*Store) ClearCommunities

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

func (*Store) Close

func (s *Store) Close() error

func (*Store) CommunityMembers

func (s *Store) CommunityMembers(ctx context.Context, communityID string) ([]*Entity, error)

func (*Store) CountNotes

func (s *Store) CountNotes(ctx context.Context) (int, error)

CountNotes returns the row count of notes_fts.

func (*Store) DB

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

func (*Store) DeleteDocument

func (s *Store) DeleteDocument(ctx context.Context, id string) (int64, error)

DeleteDocument hard-deletes a document row. ON DELETE CASCADE on chunks/embeddings/relationships takes care of dependent rows. Returns the number of rows removed so callers can distinguish "did nothing" from "did something."

func (*Store) DeleteNote

func (s *Store) DeleteNote(ctx context.Context, key string) error

DeleteNote removes a key from notes_fts. Not an error if the key is absent.

func (*Store) FindRelationships

func (s *Store) FindRelationships(ctx context.Context, fromID, toID, predicate string) ([]*Relationship, error)

func (*Store) GetChunk

func (s *Store) GetChunk(ctx context.Context, id string) (*Chunk, error)

func (*Store) GetCommunity

func (s *Store) GetCommunity(ctx context.Context, id string) (*Community, error)

func (*Store) GetDocument

func (s *Store) GetDocument(ctx context.Context, id string) (*Document, error)

func (*Store) GetDocumentByHash

func (s *Store) GetDocumentByHash(ctx context.Context, hash string) (*Document, error)

func (*Store) GetDocumentByPath

func (s *Store) GetDocumentByPath(ctx context.Context, path string) (*Document, error)

func (*Store) GetDocumentVersions

func (s *Store) GetDocumentVersions(ctx context.Context, canonicalID string) ([]*Document, error)

GetDocumentVersions returns all versions of a document by canonical ID, oldest first.

func (*Store) GetEntitiesByNames

func (s *Store) GetEntitiesByNames(ctx context.Context, names []string) (map[string]*Entity, error)

GetEntitiesByNames fetches entities for a set of names in one query. Returns map name→Entity.

func (*Store) GetEntity

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

func (*Store) GetEntityByName

func (s *Store) GetEntityByName(ctx context.Context, name string) (*Entity, error)

func (*Store) GetStats

func (s *Store) GetStats(ctx context.Context) (*Stats, error)

func (*Store) GraphFingerprint

func (s *Store) GraphFingerprint(ctx context.Context) (entities, relationships, communities int, err error)

GraphFingerprint returns counts of entities, relationships, and communities to detect whether the graph has changed since last finalization.

func (*Store) IndexNote

func (s *Store) IndexNote(ctx context.Context, n *notes.Note) error

IndexNote upserts a note into notes_fts. DELETE-by-key + INSERT is used instead of `INSERT ... ON CONFLICT` because the contentless FTS5 table has no declared primary key.

Tags are lowercased on index (decision: lowercase normalization so searches are case-insensitive). The title is derived from the last path segment of the key.

func (*Store) InsertChunk

func (s *Store) InsertChunk(ctx context.Context, c *Chunk) error

func (*Store) InsertClaim

func (s *Store) InsertClaim(ctx context.Context, c *Claim) error

func (*Store) InsertCommunityMember

func (s *Store) InsertCommunityMember(ctx context.Context, communityID, entityID string) error

func (*Store) InsertRelationship

func (s *Store) InsertRelationship(ctx context.Context, r *Relationship) error

func (*Store) ListChunksByDoc

func (s *Store) ListChunksByDoc(ctx context.Context, docID string) ([]*Chunk, error)

func (*Store) ListClaims

func (s *Store) ListClaims(ctx context.Context, status string, limit int) ([]*Claim, error)

ListClaims returns claims, optionally filtered by status. A non-positive limit is clamped to 100; an upper bound of 1000 is enforced to keep a single request from pulling the whole table.

func (*Store) ListCommunities

func (s *Store) ListCommunities(ctx context.Context, level int) ([]*Community, error)

func (*Store) ListDocuments

func (s *Store) ListDocuments(ctx context.Context, docType string, limit, offset int) ([]*Document, error)

func (*Store) ListEntities

func (s *Store) ListEntities(ctx context.Context, typ string, limit, offset int) ([]*Entity, error)

func (*Store) RelationshipsForEntity

func (s *Store) RelationshipsForEntity(ctx context.Context, entityID string, depth int) ([]*Relationship, error)

func (*Store) SearchNotes

func (s *Store) SearchNotes(ctx context.Context, query string, limit int) ([]NoteHit, error)

SearchNotes runs a FTS5 MATCH query ranked by bm25() and returns up to `limit` hits with a highlighted snippet. `limit` <= 0 defaults to 20.

Empty / whitespace-only queries return an empty slice (no hits) rather than an error — callers can keep their code simple.

Special chars in `query` are escaped by wrapping each whitespace- delimited token in double quotes (FTS5 string literal syntax). This lets callers pass arbitrary user text including `:`, `*`, `"`, etc. without triggering a "malformed MATCH expression" error.

func (*Store) SupersedeDocument

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

SupersedeDocument marks a document as no longer the latest version.

func (*Store) UpdateDocumentStructured

func (s *Store) UpdateDocumentStructured(ctx context.Context, id, structured string) error

func (*Store) UpdateEntityCommunity

func (s *Store) UpdateEntityCommunity(ctx context.Context, entityID, communityID string) error

func (*Store) UpdateEntityRank

func (s *Store) UpdateEntityRank(ctx context.Context, entityID string, rank int) error

func (*Store) UpsertCommunity

func (s *Store) UpsertCommunity(ctx context.Context, c *Community) error

func (*Store) UpsertDocument

func (s *Store) UpsertDocument(ctx context.Context, doc *Document) error

func (*Store) UpsertEmbedding

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

func (*Store) UpsertEntity

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

Jump to

Keyboard shortcuts

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