memstore

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: 10 Imported by: 0

Documentation

Overview

Package memstore provides in-memory reference implementations of the persistence ports. It defines the reference semantics every other engine must match (via the internal/conformance suites) and is a perfectly serviceable backend for small corpora: brute-force cosine over float32 slices handles ~100k chunks in single-digit milliseconds.

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 a thread-safe, in-memory app.AnswerCache (the reference impl, and what the "memory" storage backend uses). It is process-local, so it only helps within a single run.

func NewAnswerCache

func NewAnswerCache() *AnswerCache

NewAnswerCache returns an empty cache.

func (*AnswerCache) Get

func (c *AnswerCache) Get(_ 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(_ context.Context, cutoff time.Time) error

Prune deletes every entry stored before cutoff.

func (*AnswerCache) Put

func (c *AnswerCache) Put(_ 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 a thread-safe, in-memory store of Collection aggregates, keyed by name. It owns its data: it stores and returns copies so callers cannot alias the stored aggregates.

func NewCollectionRepository

func NewCollectionRepository() *CollectionRepository

NewCollectionRepository returns an empty repository.

func (*CollectionRepository) Create

Create stores a copy of c, failing with ErrAlreadyExists if the name is taken.

func (*CollectionRepository) Delete

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

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

func (*CollectionRepository) Get

Get returns a copy of the named collection, or ErrNotFound.

func (*CollectionRepository) List

List returns copies of every collection, in unspecified order.

func (*CollectionRepository) RecordSource

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

RecordSource appends source to the collection's Sources, idempotently, or fails with ErrNotFound.

type DocumentRepository

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

DocumentRepository is a thread-safe, in-memory store of Documents and their Chunks. Chunk data lives in byChunkID (the source of truth for GetChunks); docChunks tracks each document's chunk IDs so Upsert can replace and Delete can cascade. Values are stored and returned by copy.

func NewDocumentRepository

func NewDocumentRepository() *DocumentRepository

NewDocumentRepository returns an empty repository.

func (*DocumentRepository) Delete

func (r *DocumentRepository) Delete(_ 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. Their vectors live in the VectorIndex and are removed by the use case.

func (*DocumentRepository) DeleteChunks

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

DeleteChunks removes the given chunks that belong to the collection, returning the IDs actually removed. The owning documents are left in place (their record stands even after losing chunks). IDs absent from the collection are skipped.

func (*DocumentRepository) DeleteCollection

func (r *DocumentRepository) DeleteCollection(_ 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(_ context.Context, collection, sourceURI string) (*domain.Document, error)

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

func (*DocumentRepository) GetChunks

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

GetChunks hydrates chunks by ID, preserving input order and skipping IDs with no stored chunk (the result may be shorter than the input).

func (*DocumentRepository) GetChunksByDocument

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

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

func (*DocumentRepository) GetChunksByIDs

func (r *DocumentRepository) GetChunksByIDs(_ 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(_ context.Context, ids []domain.DocumentID) ([]*domain.Document, error)

GetDocuments hydrates documents by ID, preserving input order and skipping IDs with no stored document (the result may be shorter than the input).

func (*DocumentRepository) ListDocuments

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

ListDocuments returns every document in the collection (order unspecified). An unknown or empty collection yields no documents and no error.

func (*DocumentRepository) Upsert

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

Upsert stores the document and replaces its chunks: any chunks the document previously had are dropped before the new ones are recorded.

type LexicalIndex added in v1.0.0

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

LexicalIndex is a thread-safe in-memory BM25 keyword index — the reference semantics for the lexical half of hybrid retrieval. Corpus statistics (idf, average document length) are recomputed per search; for the small corpora memstore targets this is cheap and keeps the store free of incremental bookkeeping.

func NewLexicalIndex added in v1.0.0

func NewLexicalIndex() *LexicalIndex

NewLexicalIndex returns an empty index.

func (*LexicalIndex) Delete added in v1.0.0

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

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

func (*LexicalIndex) Search added in v1.0.0

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

Search ranks the metadata-matching chunks that contain at least one query term by BM25, best first, returning up to k chunk IDs.

func (*LexicalIndex) Upsert added in v1.0.0

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

Upsert tokenizes and stores each document, replacing entries with the same ChunkID.

type VectorIndex

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

VectorIndex is a thread-safe, brute-force cosine similarity index.

func NewVectorIndex

func NewVectorIndex() *VectorIndex

NewVectorIndex returns an empty index.

func (*VectorIndex) Delete

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

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

func (*VectorIndex) Entries

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

Entries returns a copy of every stored vector (and its metadata) for the collection (order unspecified). An unknown collection yields no entries and no error.

func (*VectorIndex) Search

func (x *VectorIndex) Search(_ 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 entries whose metadata satisfies filter (the zero predicate matches all).

func (*VectorIndex) Upsert

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

Upsert stores copies of the vectors and their metadata, replacing entries with the same ChunkID.

Jump to

Keyboard shortcuts

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