memdb

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package memdb provides a generic, concurrency-safe, copy-on-write in-memory store following the point-in-time snapshot pattern. Readers obtain lock-free immutable views, while writers mutate atomically through transactions or direct mutation methods.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyPrefix is returned by GetByPrefix when prefix is empty.
	ErrEmptyPrefix = errors.New("memdb: prefix cannot be empty")
	// ErrNotFound is returned by GetByPrefix when no item matches prefix.
	ErrNotFound = errors.New("memdb: not found")
	// ErrAmbiguous is returned by GetByPrefix when multiple items match prefix.
	ErrAmbiguous = errors.New("memdb: multiple items match prefix")
)

Functions

This section is empty.

Types

type DB

type DB[T any] struct {
	// contains filtered or unexported fields
}

DB is a generic, concurrency-safe, copy-on-write in-memory store.

func New

func New[T any](keyFn func(T) string) *DB[T]

New creates a new DB where keyFn extracts the unique string key from T. Items in All() are kept sorted by key ascending.

func (*DB[T]) Delete

func (db *DB[T]) Delete(ids ...string)

Delete removes items by ID atomically.

func (*DB[T]) Replace

func (db *DB[T]) Replace(items ...T)

Replace atomically replaces all items in the DB with items.

func (*DB[T]) Save

func (db *DB[T]) Save(items ...T)

Save inserts or updates one or more items atomically.

func (*DB[T]) Snapshot

func (db *DB[T]) Snapshot() *View[T]

Snapshot returns a consistent, point-in-time read-only View. Reads are lock-free via atomic.Pointer.

func (*DB[T]) WithTxn

func (db *DB[T]) WithTxn(fn func(tx *Txn[T]) error) error

WithTxn executes fn inside a write transaction and commits atomically if fn returns nil.

type Txn

type Txn[T any] struct {
	// contains filtered or unexported fields
}

Txn represents an isolated write transaction.

func (*Txn[T]) Delete

func (tx *Txn[T]) Delete(ids ...string)

Delete removes items by ID within the transaction.

func (*Txn[T]) Save

func (tx *Txn[T]) Save(items ...T)

Save inserts or updates items within the transaction.

type View

type View[T any] struct {
	// contains filtered or unexported fields
}

View represents a consistent point-in-time snapshot.

func (*View[T]) All

func (v *View[T]) All() []T

All returns a slice of all items sorted by key ascending.

func (*View[T]) Get

func (v *View[T]) Get(id string) (T, bool)

Get returns the item with the exact key, or false if not found.

func (*View[T]) GetByPrefix

func (v *View[T]) GetByPrefix(prefix string) (T, error)

GetByPrefix resolves an item by prefix. Returns ErrEmptyPrefix if prefix is empty. If an exact match exists, it is returned immediately. If multiple items match prefix, ErrAmbiguous is returned. If no item matches prefix, ErrNotFound is returned.

func (*View[T]) Len

func (v *View[T]) Len() int

Len returns the number of items in the view.

Jump to

Keyboard shortcuts

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