Documentation
¶
Index ¶
- Variables
- type Keyer
- type Store
- func (s *Store[K, V]) Begin() *Transaction[K, V]
- func (s *Store[K, V]) Close() error
- func (s *Store[K, V]) Delete(key K) error
- func (s *Store[K, V]) Get(key K) (V, bool)
- func (s *Store[K, V]) GetOptimistic(key K) (VersionedValue[K, V], bool)
- func (s *Store[K, V]) List(filter func(V) bool) []V
- func (s *Store[K, V]) ListOptimistic(filter func(V) bool) []VersionedValue[K, V]
- func (s *Store[K, V]) Set(v V) error
- func (s *Store[K, V]) SetOptimistic(v VersionedValue[K, V]) error
- type StoreOption
- type Transaction
- type VersionedValue
Constants ¶
This section is empty.
Variables ¶
var ( // ErrStoreIsClosed is returned when attempting to perform operations on a closed store ErrStoreIsClosed = errors.New("store is closed") // ErrVersionMismatch is returned when an optimistic update fails due to version conflict ErrVersionMismatch = errors.New("version mismatch") // ErrTransactionConflict is returned when transaction conflicts with other changes ErrTransactionConflict = errors.New("transaction conflict") )
Functions ¶
This section is empty.
Types ¶
type Keyer ¶
type Keyer[K comparable] interface { // Key returns the key associated with the value Key() K }
Keyer defines an interface for types that can provide their own key
type Store ¶
type Store[K comparable, V Keyer[K]] struct { // contains filtered or unexported fields }
Store is a persistent key-value storage with versioning and compaction support
func NewStore ¶
func NewStore[K comparable, V Keyer[K]](fileName string, opts ...StoreOption) (*Store[K, V], error)
NewStore creates a new Store instance with the given filename and options fileName: path to the data file (will be created if it doesn't exist) opts: optional configuration parameters Returns a new Store instance or an error if initialization fails
func (*Store[K, V]) Begin ¶ added in v1.0.1
func (s *Store[K, V]) Begin() *Transaction[K, V]
Begin starts a new transaction
func (*Store[K, V]) Close ¶
Close shuts down the store gracefully, waiting for all operations to complete Returns an error if closing the underlying file fails
func (*Store[K, V]) Delete ¶
Delete marks a record as deleted key: the key of the record to delete Returns an error if the store is closed
func (*Store[K, V]) Get ¶
Get retrieves a value by its key key: the key to look up Returns the value and true if found, or zero value and false if not found
func (*Store[K, V]) GetOptimistic ¶
func (s *Store[K, V]) GetOptimistic(key K) (VersionedValue[K, V], bool)
GetOptimistic retrieves a value with its version number key: the key to look up Returns the VersionedValue and true if found, or zero value and false if not found
func (*Store[K, V]) List ¶
List returns all values that satisfy the given filter filter: optional function to filter results (nil returns all values) Returns a slice of matching values
func (*Store[K, V]) ListOptimistic ¶
func (s *Store[K, V]) ListOptimistic(filter func(V) bool) []VersionedValue[K, V]
ListOptimistic returns all values with their versions that satisfy the given filter filter: optional function to filter results (nil returns all values) Returns a slice of matching VersionedValue instances
func (*Store[K, V]) Set ¶
Set stores a new value or updates an existing one v: the value to store (must implement Keyer interface) Returns an error if the store is closed
func (*Store[K, V]) SetOptimistic ¶
func (s *Store[K, V]) SetOptimistic(v VersionedValue[K, V]) error
SetOptimistic updates a value only if the provided version matches the current version v: the VersionedValue to update Returns ErrVersionMismatch if versions don't match, or ErrStoreIsClosed if store is closed
type StoreOption ¶
type StoreOption func(*storeOpts)
StoreOption defines a functional option type for configuring the Store
func WithCompactionInterval ¶
func WithCompactionInterval(interval time.Duration) StoreOption
WithCompactionInterval sets the interval at which automatic compaction will occur
func WithLogger ¶
func WithLogger(logger *slog.Logger) StoreOption
WithLogger sets a custom logger for the Store
func WithWriteQueueSize ¶
func WithWriteQueueSize(size int) StoreOption
WithWriteQueueSize sets the size of the asynchronous write queue
type Transaction ¶ added in v1.0.1
type Transaction[K comparable, V Keyer[K]] struct { // contains filtered or unexported fields }
Transaction represents an atomic transaction
func (*Transaction[K, V]) Commit ¶ added in v1.0.1
func (t *Transaction[K, V]) Commit() error
Commit applies all transaction operations atomically
func (*Transaction[K, V]) Delete ¶ added in v1.0.1
func (t *Transaction[K, V]) Delete(key K) error
Delete adds a delete operation to the transaction
func (*Transaction[K, V]) Rollback ¶ added in v1.0.1
func (t *Transaction[K, V]) Rollback()
Rollback cancels the transaction
func (*Transaction[K, V]) Set ¶ added in v1.0.1
func (t *Transaction[K, V]) Set(v V) error
Set adds a set operation to the transaction
type VersionedValue ¶
type VersionedValue[K comparable, V Keyer[K]] struct { Value V // the stored value Version int // current version of the value }
VersionedValue represents a value with its version number