persistence

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package persistence adds durable storage to ConcurrentOrderedMap. It uses a Write-Ahead Log (WAL) for crash-safe individual writes and periodic binary snapshots for fast recovery.

Design goals:

  • Zero external dependencies (stdlib only: os, encoding/gob, compress/zlib)
  • Non-blocking reads; writers hold the lock only during encode+append
  • Recovery is deterministic: snapshot base + WAL replay in sequence order
  • Optional fsync-on-write for mission-critical durability

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FlatVectorIndex

type FlatVectorIndex[K comparable] struct {
	// contains filtered or unexported fields
}

FlatVectorIndex is an in-memory brute-force cosine-similarity index.

Suitable for up to ~500 000 vectors. For larger corpora consider a hierarchical index (HNSW) as a drop-in replacement behind the same interface.

All operations are safe for concurrent use.

func NewFlatVectorIndex

func NewFlatVectorIndex[K comparable](dims int) *FlatVectorIndex[K]

NewFlatVectorIndex creates an empty index that expects vectors of `dims` dimensions.

func (*FlatVectorIndex[K]) Delete

func (idx *FlatVectorIndex[K]) Delete(key K)

Delete removes a key from the index.

func (*FlatVectorIndex[K]) Len

func (idx *FlatVectorIndex[K]) Len() int

Len returns the number of vectors in the index.

func (*FlatVectorIndex[K]) Search

func (idx *FlatVectorIndex[K]) Search(
	queryVec []float64,
	topK int,
	filter func(K) bool,
) ([]VectorResult[K], error)

Search returns the topK nearest neighbours of queryVec by cosine similarity. filter is an optional key-predicate; pass nil to search all entries.

func (*FlatVectorIndex[K]) Upsert

func (idx *FlatVectorIndex[K]) Upsert(key K, vec []float64) error

Upsert adds or replaces a vector for key. The vector is normalised internally so raw embeddings (e.g. BGE-M3 outputs) can be passed directly.

type OpType

type OpType uint8

OpType identifies the kind of mutation recorded in the WAL.

const (
	OpSet    OpType = 1
	OpDelete OpType = 2
)

type Options

type Options struct {
	// SnapshotInterval is how often a compaction snapshot is written.
	// Set to 0 to disable automatic snapshots (you can call Snapshot() manually).
	SnapshotInterval time.Duration

	// MaxWALEntries triggers a snapshot + WAL truncation once the WAL grows
	// beyond this many entries. 0 = disabled.
	MaxWALEntries int

	// SyncOnWrite calls fdatasync after every WAL append.
	// Safer but slower. Recommended for critical data.
	SyncOnWrite bool

	// VectorDims is the embedding dimensionality. Set >0 to enable the vector
	// index. Vectors are stored alongside values using SetWithVector.
	VectorDims int
}

Options configures the persistent map.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns sensible defaults.

type PersistentConcurrentOrderedMap

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

PersistentConcurrentOrderedMap wraps ConcurrentOrderedMap with WAL-based durability and an optional in-memory vector index for semantic search.

Files created under `dir`:

  • data.wal — append-only write-ahead log
  • data.snap — latest binary snapshot
  • data.snap.tmp — transient during snapshot write (atomic rename)

func New

func New[K comparable, V any](dir string, opts Options) (*PersistentConcurrentOrderedMap[K, V], error)

New opens or creates a PersistentConcurrentOrderedMap in `dir`. If existing data is found it is loaded before the function returns.

func (*PersistentConcurrentOrderedMap[K, V]) Close

func (p *PersistentConcurrentOrderedMap[K, V]) Close() error

Close flushes, takes a final snapshot, and releases all resources.

func (*PersistentConcurrentOrderedMap[K, V]) Delete

func (p *PersistentConcurrentOrderedMap[K, V]) Delete(key K) error

Delete removes key/value and appends a WAL delete entry.

func (*PersistentConcurrentOrderedMap[K, V]) Exists

func (p *PersistentConcurrentOrderedMap[K, V]) Exists(key K) bool

Exists reports whether key is present.

func (*PersistentConcurrentOrderedMap[K, V]) Get

func (p *PersistentConcurrentOrderedMap[K, V]) Get(key K) (V, bool)

Get retrieves a value by key.

func (*PersistentConcurrentOrderedMap[K, V]) Inner

Inner exposes the underlying ConcurrentOrderedMap for read operations and similarity searches (Levenshtein, vector, etc.).

func (*PersistentConcurrentOrderedMap[K, V]) Len

func (p *PersistentConcurrentOrderedMap[K, V]) Len() int

Len returns the number of stored entries.

func (*PersistentConcurrentOrderedMap[K, V]) Set

func (p *PersistentConcurrentOrderedMap[K, V]) Set(key K, value V) error

Set stores key/value and appends a WAL entry.

func (*PersistentConcurrentOrderedMap[K, V]) SetWithVector

func (p *PersistentConcurrentOrderedMap[K, V]) SetWithVector(key K, value V, vec []float64) error

SetWithVector stores key/value and its embedding vector. The vector is indexed for semantic search (requires VectorDims > 0).

func (*PersistentConcurrentOrderedMap[K, V]) Snapshot

func (p *PersistentConcurrentOrderedMap[K, V]) Snapshot() error

Snapshot writes a compacted snapshot and truncates the WAL.

func (*PersistentConcurrentOrderedMap[K, V]) Sync

func (p *PersistentConcurrentOrderedMap[K, V]) Sync() error

Sync flushes WAL buffers to OS.

func (*PersistentConcurrentOrderedMap[K, V]) VectorSearch

func (p *PersistentConcurrentOrderedMap[K, V]) VectorSearch(
	queryVec []float64,
	topK int,
	filter func(K) bool,
) ([]VectorResult[K], error)

VectorSearch performs a cosine-similarity search over all stored vectors. Requires VectorDims > 0 in Options.

type VectorResult

type VectorResult[K comparable] struct {
	Key   K
	Score float64
}

VectorResult is a single hit from a vector search.

type WALWriter

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

WALWriter appends entries to a WAL file. It is safe for concurrent use.

func (*WALWriter[K, V]) Append

func (w *WALWriter[K, V]) Append(op OpType, key K, value V) error

Append writes a single entry to the WAL.

func (*WALWriter[K, V]) Close

func (w *WALWriter[K, V]) Close() error

Close flushes and closes the underlying file.

func (*WALWriter[K, V]) Flush

func (w *WALWriter[K, V]) Flush() error

Flush ensures the OS buffer is flushed to disk.

Jump to

Keyboard shortcuts

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