storage

package
v0.18.3 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 14 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 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 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