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

inmemory

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package inmemory implements an in-process vector store for testing and prototyping. Each store owns independent collections; nothing is persisted unless explicitly exported.

Collections support structs, pointers to structs, and map[string]any records. Keys must be non-nil comparable values without NaN. Assignable keys are normalized to the schema's declared key type for storage and lookup, without changing records. Interface key properties retain concrete key types. Records are retained and returned without cloning: pointer and map records, and reference-valued fields in struct records, share their underlying data with the caller. Plain structs are copied by value. Collection dictionaries support concurrent operations. Shared record data is not synchronized: callers must prevent concurrent mutation, including upserts that generate keys on the same record. Custom JSON marshalers also receive the stored records directly.

Native vectors are []float32, embedding.VectorOf[float32], embedding.Of[float32], and *embedding.Of[float32]. Searches scan all matching records, regardless of index hints. Supported metrics are cosine similarity (the default), cosine distance, dot product similarity, and Euclidean distance. Hybrid search is not supported. Result order among equal scores is unspecified.

Filters and sorting order primitive values and UUIDs. After dereferencing pointers and interfaces, a type T with a Compare(T) int method uses that method in preference to primitive ordering. Comparison methods must not mutate their operands. UUIDs use lexicographic byte ordering. Equality uses Equal(T) bool methods when available, otherwise reflect.DeepEqual. Nil values are compared without calling methods.

Native vectors are always returned, regardless of IncludeVectors. Generated vectors are stored separately from their input fields; IncludeVectors=true is unsupported for collections with generated fields. Generated UUIDs are written into records. Use pointer or map records to observe those keys on the input.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Import

func Import[K comparable, R any](ctx context.Context, s *Store, reader io.Reader, definition *vectordata.Definition) (vectordata.Collection[K, R], error)

Import reads a JSON snapshot, creates its named collection if necessary, and merges records, replacing matching keys. A nil definition uses the record's vectordata tags; otherwise the supplied definition configures the collection. Generated vectors are restored without invoking a generator. Invalid snapshots do not modify the store. The reader is not closed.

Dynamic properties are decoded using their schema types. Other interface values follow encoding/json's usual decoding rules.

Types

type Collection

type Collection[K comparable, R any] struct {
	// contains filtered or unexported fields
}

Collection is a configured handle to an in-memory collection. Construct one with NewCollection. Its schema is fixed at construction. A shared Store also creates collections through its embedded vectordata.Store.

func NewCollection

func NewCollection[K comparable, R any](name string, definition *vectordata.Definition) (*Collection[K, R], error)

NewCollection creates a handle backed by its own independent store. A nil definition uses the record's vectordata tags. The collection does not exist until Collection.EnsureExists is called.

func (*Collection[K, R]) Delete

func (c *Collection[K, R]) Delete(ctx context.Context, key K) error

Delete deletes a record if it exists. A missing collection is an error.

func (*Collection[K, R]) EnsureDeleted

func (c *Collection[K, R]) EnsureDeleted(ctx context.Context) error

EnsureDeleted deletes the collection if it exists.

func (*Collection[K, R]) EnsureExists

func (c *Collection[K, R]) EnsureExists(ctx context.Context) error

EnsureExists creates the collection if it does not already exist.

func (*Collection[K, R]) Exists

func (c *Collection[K, R]) Exists(ctx context.Context) (bool, error)

Exists reports whether the collection exists.

func (*Collection[K, R]) Get

func (c *Collection[K, R]) Get(ctx context.Context, key K, options *vectordata.RecordRetrievalOptions) (R, bool, error)

Get returns the stored record, or found=false for a missing key. A missing collection is an error. Native vectors are always included. IncludeVectors=true is unsupported when any field requires embedding generation.

func (*Collection[K, R]) GetFiltered

func (c *Collection[K, R]) GetFiltered(ctx context.Context, filter vectordata.Filter, top int, options *vectordata.FilteredRecordRetrievalOptions) iter.Seq2[R, error]

GetFiltered returns each stored matching record after multi-column ordering, Skip, and top. Supported ordered values are booleans, strings, numbers, UUIDs, types with Compare(T) int methods, and nullable pointers to these types. Nulls sort before non-null values in ascending order. Without OrderBy, order is unspecified and filtering stops after the requested page. Strings use Go's lexicographic ordering, without culture-specific collation. Property names refer to Go fields or dynamic schema properties, not storage names. Results retain native vectors regardless of IncludeVectors.

func (*Collection[K, R]) Name

func (c *Collection[K, R]) Name() string

Name returns the collection name.

func (*Collection[K, R]) Search

func (c *Collection[K, R]) Search(ctx context.Context, searchValue any, top int, options *vectordata.SearchOptions) iter.Seq2[vectordata.SearchResult[R], error]

Search performs an exact vector search after filtering, then applies the inclusive score threshold, ordering, Skip, and top. Execution is lazy and uses a snapshot of stored record references. Similarities sort descending; distances ascending. Scores have float32 precision. Null record vectors and undefined (NaN) scores are skipped. Results retain native vectors regardless of IncludeVectors.

func (*Collection[K, R]) Upsert

func (c *Collection[K, R]) Upsert(ctx context.Context, records ...R) error

Upsert retains records without cloning them, generating embeddings once per generated property for the entire batch. Collection dictionary updates are committed atomically after preparation succeeds. Duplicate keys use the last record. Changes to input records are not rolled back on failure.

Empty batches are no-ops. Nonempty batches require an existing collection. After embedding generation, auto-generated UUIDs replace uuid.Nil in records. Use pointer or map records to observe generated keys on the input; plain struct values are copied by value.

type Config

type Config struct {
	// EmbeddingGenerator supplies the default generator for collection handles.
	// Definition and property configuration may override it.
	EmbeddingGenerator *embedding.BoundGenerator
}

Config configures an in-memory store.

type Store

type Store struct {
	*vectordata.Store
	// contains filtered or unexported fields
}

Store holds independently named collections with different key and record types. Create it with New. Its embedded vectordata.Store provides collection factories and lifecycle operations; pass that field to provider-independent APIs. A Store must not be copied after use. Its methods support concurrent use.

func New

func New(config Config) *Store

New creates a store. The generator is borrowed, not closed by the store.

func (*Store) Export

func (s *Store) Export(ctx context.Context, name string, writer io.Writer) error

Export writes a JSON snapshot of the named collection, including native and generated vectors. The snapshot contains a name and an array of records, not a schema or generator configuration. Records must support encoding/json. It does not close the writer. Record order in the snapshot is unspecified.

Jump to

Keyboard shortcuts

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