kvstore

package
v0.0.0-...-84c3d0b Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package kvstore provides a simple key-value store safe for concurrent reads and writes and and interface for interacting with the store. The KVStore uses []byte for both keys and values, aliased as the Key and Value types. This allows for the KVStore to be used with any type that can be serialised or converted into a byte slice.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound is returned when a key is not found
	ErrKeyNotFound = errors.New("key not found")
	// ErrEmptyStoreKey is returned when attempting to use an empty key
	ErrEmptyStoreKey = errors.New("empty store key")
	// ErrInvalidIterDirections is returned when an invalid number of
	// iteration directions are provided to the Iterate functions
	ErrInvalidIterDirections = errors.New("invalid number of iteration directions")
	// ErrUnknownIterDirection is returned when an unknown iteration
	// direction is provided to the Iterate functions
	ErrUnknownIterDirection = errors.New("unknown iteration direction")
)

Functions

func NewKVStore

func NewKVStore() kvwal.KVStore

NewKVStore creates a new instance of the KVStore interface.

Types

type ConcurrentMap

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

ConcurrentMap is a struct that implements the KVStore interface. Using a RWMutex around the map we can ensure the map is concurrency safe.

func (*ConcurrentMap) ClearAll

func (c *ConcurrentMap) ClearAll()

ClearAll deletes all key-value pairs from the store.

func (*ConcurrentMap) Clone

func (c *ConcurrentMap) Clone() kvwal.KVStore

Clone returns a copy of the store.

func (*ConcurrentMap) Delete

func (c *ConcurrentMap) Delete(key kvwal.Key) error

Delete deletes the given key from the store.

func (*ConcurrentMap) DeletePrefix

func (c *ConcurrentMap) DeletePrefix(prefix kvwal.KeyPrefix)

DeletePrefix deletes all keys with the given prefix from the store. To delete all keys from the store, pass in a nil prefix.

func (*ConcurrentMap) Equal

func (c *ConcurrentMap) Equal(other kvwal.KVStore) (bool, error)

Equal returns true if the provided store is equal to the current store, by using an iterator to compare the key-value pairs in both stores.

func (*ConcurrentMap) Get

func (c *ConcurrentMap) Get(key kvwal.Key) (kvwal.Value, error)

Get returns the value associated with the given key.

func (*ConcurrentMap) GetAll

func (c *ConcurrentMap) GetAll() ([]kvwal.Key, []kvwal.Value)

GetAll returns all keys and values in the store.

func (*ConcurrentMap) GetPrefix

func (c *ConcurrentMap) GetPrefix(prefix kvwal.KeyPrefix) []kvwal.Value

GetPrefix returns all values in the store who's key has the given prefix.

func (*ConcurrentMap) Has

func (c *ConcurrentMap) Has(key kvwal.Key) (bool, error)

Has checks whether the given key exists in the store or not.

func (*ConcurrentMap) Iterate

func (c *ConcurrentMap) Iterate(
	prefix kvwal.KeyPrefix,
	consumer kvwal.IteratorConsumerFn,
	direction ...kvwal.IterDirection,
) (err error)

Iterate iterates over all key-value pairs in the store with the provided prefix, in the specified direction (or forwards if not specified), and invokes the provided consumer function on each key-value pair. If the consumer function returns false, then the iteration is stopped. The consumer function is invoked with a copy of the key and value and does not mutate the store.

func (*ConcurrentMap) IterateKeys

func (c *ConcurrentMap) IterateKeys(
	prefix kvwal.KeyPrefix,
	consumer kvwal.IteratorKeysConsumerFn,
	direction ...kvwal.IterDirection,
) (err error)

IterateKeys iterates over all keys in the store with the provided prefix, in the specified direction (or forwards if not specified), and invokes the provided consumer function on each key. If the consumer function returns false, then the iteration is stopped. The consumer function is invoked with a copy of the key, and does not mutate the store.

func (*ConcurrentMap) Len

func (c *ConcurrentMap) Len() int

Len returns the number of key-value pairs in the store.

func (*ConcurrentMap) Set

func (c *ConcurrentMap) Set(key kvwal.Key, value kvwal.Value) error

Set sets/updates the value associated with the given key in the store.

Jump to

Keyboard shortcuts

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