store

package
v1.9.13-beta2 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config[K comparable, V any] struct {
	Disk       *diskstore.DiskStore[V]
	WAL        wal.WAL
	EntityName string
	KeyToStr   func(K) string // convert key to string for WAL/disk
	StrToKey   func(string) K // convert string back to key
}

Config holds the configuration for creating a new Store.

type Store

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

Store is a generic in-memory cache backed by a DiskStore and WAL. All reads are served from memory. Writes go to WAL first, then memory. Dirty records are flushed to disk periodically by the background flusher.

func NewStore

func NewStore[K comparable, V any](cfg Config[K, V]) *Store[K, V]

NewStore creates a new Store. Call LoadAll() after creation to populate from disk, or start empty.

func (*Store[K, V]) All

func (s *Store[K, V]) All() []*V

All returns all records in the store. The returned slice contains pointers to the actual stored records.

func (*Store[K, V]) Clear

func (s *Store[K, V]) Clear()

Clear removes all records from memory without writing to WAL or disk. Used for testing and Drop operations.

func (*Store[K, V]) Count

func (s *Store[K, V]) Count(fn func(*V) bool) int

Count returns the number of records matching the predicate.

func (*Store[K, V]) CountUnlocked

func (s *Store[K, V]) CountUnlocked(fn func(*V) bool) int

CountUnlocked returns count without acquiring the lock.

func (*Store[K, V]) Delete

func (s *Store[K, V]) Delete(key K) error

Delete removes a record. The change is written to WAL first, then applied to memory.

func (*Store[K, V]) DeleteUnlocked

func (s *Store[K, V]) DeleteUnlocked(key K) error

DeleteUnlocked removes a record without acquiring the lock. The caller must hold the write lock.

func (*Store[K, V]) DeletedCount

func (s *Store[K, V]) DeletedCount() int

DeletedCount returns the number of tombstoned records pending flush.

func (*Store[K, V]) DirtyCount

func (s *Store[K, V]) DirtyCount() int

DirtyCount returns the number of dirty records pending flush.

func (*Store[K, V]) Filter

func (s *Store[K, V]) Filter(fn func(*V) bool) []*V

Filter returns all records matching the predicate.

func (*Store[K, V]) FilterUnlocked

func (s *Store[K, V]) FilterUnlocked(fn func(*V) bool) []*V

FilterUnlocked returns matching records without acquiring the lock. The caller must hold at least the read lock.

func (*Store[K, V]) FlushDirty

func (s *Store[K, V]) FlushDirty() error

FlushDirty writes all dirty records to disk and removes tombstoned records from disk. Clears the dirty and deleted sets afterward.

func (*Store[K, V]) ForEach

func (s *Store[K, V]) ForEach(fn func(K, *V))

ForEach calls fn for each (key, value) pair in the store.

func (*Store[K, V]) Get

func (s *Store[K, V]) Get(key K) (*V, bool)

Get retrieves a record by primary key. Returns nil, false if not found.

func (*Store[K, V]) GetUnlocked

func (s *Store[K, V]) GetUnlocked(key K) (*V, bool)

GetUnlocked retrieves a record without acquiring the lock. The caller must hold at least the read lock.

func (*Store[K, V]) Len

func (s *Store[K, V]) Len() int

Len returns the total number of records in the store.

func (*Store[K, V]) LoadAll

func (s *Store[K, V]) LoadAll() error

LoadAll loads all records from disk into memory. This is called during startup to populate the cache. Existing in-memory records are preserved (WAL replay may have already added some).

func (*Store[K, V]) Lock

func (s *Store[K, V]) Lock()

Lock acquires the write lock. Use with Unlock for atomic multi-step operations (e.g. SelectAndAssign). The caller must call Unlock.

func (*Store[K, V]) Put

func (s *Store[K, V]) Put(key K, value *V) error

Put adds or updates a record. The change is written to WAL first, then applied to memory and marked dirty.

func (*Store[K, V]) PutUnlocked

func (s *Store[K, V]) PutUnlocked(key K, value *V) error

PutUnlocked adds a record without acquiring the lock. The caller must hold the write lock. Used for atomic multi-step operations.

func (*Store[K, V]) RLock

func (s *Store[K, V]) RLock()

RLock acquires the read lock.

func (*Store[K, V]) RUnlock

func (s *Store[K, V]) RUnlock()

RUnlock releases the read lock.

func (*Store[K, V]) ReplayDelete

func (s *Store[K, V]) ReplayDelete(key K)

ReplayDelete removes a record directly from memory without writing to WAL. The caller must hold the write lock. Used during WAL replay.

func (*Store[K, V]) ReplayPut

func (s *Store[K, V]) ReplayPut(key K, value *V)

ReplayPut inserts a record directly into memory without writing to WAL. The caller must hold the write lock. Used during WAL replay to avoid double-logging and deadlocks.

func (*Store[K, V]) SetWAL

func (s *Store[K, V]) SetWAL(w wal.WAL)

SetWAL sets or replaces the WAL for this store. Useful for setting the WAL after replay to avoid double-logging.

func (*Store[K, V]) Unlock

func (s *Store[K, V]) Unlock()

Unlock releases the write lock.

Jump to

Keyboard shortcuts

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