storage

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package storage provides interfaces and implementations for key-value and set storage.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilKey is returned when a nil key is provided.
	ErrNilKey = errors.New("key cannot be nil")
	// ErrNilValue is returned when a nil value is provided.
	ErrNilValue = errors.New("value cannot be nil")
	// ErrEmptyKey is returned when an empty key is provided.
	ErrEmptyKey = errors.New("key cannot be empty")
)

Functions

func KVTestSuite

func KVTestSuite(t *testing.T, kv KV)

KVTestSuite is a generic test suite for KV implementations

func SetTestSuite

func SetTestSuite(t *testing.T, set Set)

SetTestSuite is a generic test suite for Set implementations

Types

type CachedKV added in v0.13.0

type CachedKV struct {
	// contains filtered or unexported fields
}

CachedKV wraps a KV implementation with a ristretto cache.

func (*CachedKV) Delete added in v0.13.0

func (c *CachedKV) Delete(key []byte) error

Delete implements KV.

func (*CachedKV) Get added in v0.13.0

func (c *CachedKV) Get(key []byte) ([]byte, error)

Get implements KV.

func (*CachedKV) Set added in v0.13.0

func (c *CachedKV) Set(key, value []byte, opts ...SetOptionsFunc) ([]byte, error)

Set implements KV.

type CachedKVConfig added in v0.13.0

type CachedKVConfig struct {
	// MaxCacheBytes is the maximum size of the cache in bytes.
	MaxCacheBytes int64
	// TTL is the time-to-live for cached entries.
	TTL time.Duration
	// SkipWriteIfCachedValueMatches skips writing to underlying storage if cached value equals new value.
	// This is disabled by default to maintain strong consistency.
	// Enable only if you control all writes and accept eventual consistency risks.
	SkipWriteIfCachedValueMatches bool
}

CachedKVConfig holds configuration for CachedKV.

func DefaultCachedKVConfig added in v0.13.0

func DefaultCachedKVConfig() *CachedKVConfig

DefaultCachedKVConfig returns default configuration.

type CachedKVConfigFunc added in v0.13.0

type CachedKVConfigFunc func(config *CachedKVConfig)

CachedKVConfigFunc is a function that modifies CachedKVConfig.

func WithCacheTTL added in v0.13.0

func WithCacheTTL(ttl time.Duration) CachedKVConfigFunc

WithCacheTTL sets the TTL for cached entries.

func WithMaxCacheBytes added in v0.13.0

func WithMaxCacheBytes(maxBytes int64) CachedKVConfigFunc

WithMaxCacheBytes sets the maximum cache size in bytes.

func WithSkipWriteIfCachedValueMatches added in v0.13.0

func WithSkipWriteIfCachedValueMatches(skip bool) CachedKVConfigFunc

WithSkipWriteIfCachedValueMatches enables skipping writes when cached value matches.

type InMemoryKV

type InMemoryKV struct {
	KV map[string][]byte
	// contains filtered or unexported fields
}

InMemoryKV is an in-memory implementation of the KV interface.

func (*InMemoryKV) Delete

func (k *InMemoryKV) Delete(key []byte) error

Delete removes a value from the in-memory storage by key.

func (*InMemoryKV) Get

func (k *InMemoryKV) Get(key []byte) ([]byte, error)

Get retrieves a value from the in-memory storage by key.

func (*InMemoryKV) Keys

func (k *InMemoryKV) Keys(prefix []byte, opts ...KeysOptionsFunc) ([][]byte, error)

Keys returns all keys in the in-memory storage that match the given prefix.

func (*InMemoryKV) Set

func (k *InMemoryKV) Set(key, value []byte, opts ...SetOptionsFunc) ([]byte, error)

Set stores a value in the in-memory storage with the given key.

type InMemorySet

type InMemorySet struct {
	HM map[string]map[string]struct{}
	// contains filtered or unexported fields
}

InMemorySet represents an in-memory set storage implementation.

func (*InMemorySet) Add

func (h *InMemorySet) Add(key, value []byte) error

Add adds a value to the in-memory set for the given key.

func (*InMemorySet) All

func (h *InMemorySet) All(key []byte) ([][]byte, error)

All returns all values in the in-memory set for the given key.

func (*InMemorySet) Delete

func (h *InMemorySet) Delete(key, value []byte) error

Delete removes a specific value from the in-memory set for the given key.

func (*InMemorySet) Drop added in v0.13.0

func (h *InMemorySet) Drop(key []byte) error

Drop removes a value from the in-memory set for the given key.

type KV

type KV interface {
	Get(key []byte) ([]byte, error)
	Set(key []byte, value []byte, opts ...SetOptionsFunc) ([]byte, error)
	Delete(key []byte) error
}

KV defines the interface for key-value storage.

func NewCachedKV added in v0.13.0

func NewCachedKV(underlying KV, opts ...CachedKVConfigFunc) (KV, error)

NewCachedKV creates a new cached KV implementation.

func NewInMemoryKV

func NewInMemoryKV() KV

NewInMemoryKV creates a new in-memory key-value store.

func NewMonitoringKV

func NewMonitoringKV(kv KV) KV

NewMonitoringKV wraps a KV with monitoring.

type KeysOptions

type KeysOptions struct {
	MaxKeys uint32
}

KeysOptions holds options for key retrieval.

func DefaultKeysOptions

func DefaultKeysOptions() *KeysOptions

DefaultKeysOptions returns the default KeysOptions.

type KeysOptionsFunc

type KeysOptionsFunc func(opts *KeysOptions)

KeysOptionsFunc is a function that modifies KeysOptions.

func WithMaxKeys

func WithMaxKeys(maxKeys uint32) KeysOptionsFunc

WithMaxKeys returns a KeysOptionsFunc that sets the maximum number of keys.

type Set

type Set interface {
	Add(key []byte, value []byte) error
	Delete(key []byte, value []byte) error
	All(key []byte) ([][]byte, error)
	Drop(key []byte) error
}

Set defines the interface for set storage.

func NewInMemorySet

func NewInMemorySet() Set

NewInMemorySet creates a new in-memory set.

func NewMonitoringSet

func NewMonitoringSet(set Set) Set

NewMonitoringSet wraps a Set with monitoring.

type SetOptions

type SetOptions struct {
	SkipIfKeyAlreadyExists bool
	ReturnPreviousValue    bool
}

SetOptions holds options for the Set operation.

func DefaultSetOptions

func DefaultSetOptions() *SetOptions

DefaultSetOptions returns the default SetOptions.

type SetOptionsFunc

type SetOptionsFunc func(opts *SetOptions)

SetOptionsFunc is a function that modifies SetOptions.

func WithReturnPreviousValue

func WithReturnPreviousValue(get bool) SetOptionsFunc

WithReturnPreviousValue returns a SetOptionsFunc that configures whether to return the previous value when setting a new one.

func WithSkipIfKeyAlreadyExists

func WithSkipIfKeyAlreadyExists(skipIfKeyAlreadyExists bool) SetOptionsFunc

WithSkipIfKeyAlreadyExists returns a SetOptionsFunc that configures whether to skip setting a value if the key already exists.

Jump to

Keyboard shortcuts

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