kvstore

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2023 License: Apache-2.0, BSD-2-Clause Imports: 10 Imported by: 4

Documentation

Index

Constants

View Source
const (
	// StoreVersionNone is used to load an existing store without a version check (e.g. in the tools).
	StoreVersionNone byte = 0
)

Variables

View Source
var (
	ErrStoreVersionCheckNotSupported  = errors.New("store version check not supported")
	ErrStoreVersionUpdateFuncNotGiven = errors.New("store version update function not given")
)
View Source
var (
	// ErrKeyNotFound is returned when an op. doesn't find the given key.
	ErrKeyNotFound = errors.New("key not found")
	// ErrStoreClosed is returned when an op accesses the kvstore but it was already closed.
	ErrStoreClosed = errors.New("trying to access closed kvstore")

	EmptyPrefix = KeyPrefix{}
)

Functions

func Copy

func Copy(source KVStore, target KVStore) error

Copy copies the content from the source to the target KVStore.

func CopyBatched

func CopyBatched(source KVStore, target KVStore, batchSize ...int) error

CopyBatched copies the content from the source to the target KVStore in batches. If batchSize is not specified, everything is copied in a single batch.

Types

type BatchCollector

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

BatchCollector is used to collect objects that should be written.

func (*BatchCollector) Add

func (br *BatchCollector) Add(objectToPersist BatchWriteObject) (batchSizeReached bool)

Add adds an object to the batch. It returns true in case the batch size is reached.

func (*BatchCollector) Commit

func (br *BatchCollector) Commit() error

Commit applies the collected mutations.

type BatchWriteObject

type BatchWriteObject interface {
	// BatchWrite mashalls the object and adds it to the BatchedMutations.
	BatchWrite(batchedMuts BatchedMutations)
	// BatchWriteDone is called after the object was persisted.
	BatchWriteDone()
	// BatchWriteScheduled returns true if the object is already scheduled for a BatchWrite operation.
	BatchWriteScheduled() bool
	// ResetBatchWriteScheduled resets the flag that the object is scheduled for a BatchWrite operation.
	ResetBatchWriteScheduled()
}

BatchWriteObject is an object that can be persisted to the KVStore in batches using the BatchedWriter.

type BatchedMutations

type BatchedMutations interface {

	// Set sets the given key and value.
	Set(key Key, value Value) error

	// Delete deletes the entry for the given key.
	Delete(key Key) error

	// Cancel cancels the batched mutations.
	Cancel()

	// Commit commits/flushes the mutations.
	Commit() error
}

BatchedMutations represents batched mutations to the storage.

type BatchedWriter

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

BatchedWriter persists BatchWriteObjects in batches to a KVStore.

func NewBatchedWriter

func NewBatchedWriter(store KVStore, opts ...Option) *BatchedWriter

NewBatchedWriter creates a new BatchedWriter instance.

func (*BatchedWriter) Enqueue

func (bw *BatchedWriter) Enqueue(object BatchWriteObject)

Enqueue adds a BatchWriteObject to the write queue. It also starts the batch writer if not done yet.

func (*BatchedWriter) Flush

func (bw *BatchedWriter) Flush()

Flush sends a signal to flush all the queued elements.

func (*BatchedWriter) KVStore

func (bw *BatchedWriter) KVStore() KVStore

KVStore returns the underlying KVStore.

func (*BatchedWriter) StopBatchWriter

func (bw *BatchedWriter) StopBatchWriter()

StopBatchWriter stops the batch writer and waits until all enqueued objects are written.

type IterDirection

type IterDirection byte

IterDirection specifies the direction for iterations.

const (
	IterDirectionForward IterDirection = iota
	IterDirectionBackward
)

func GetIterDirection

func GetIterDirection(iterDirection ...IterDirection) IterDirection

GetIterDirection returns the direction to use for an iteration. If no direction is given, it defaults to IterDirectionForward.

type IteratorKeyConsumerFunc

type IteratorKeyConsumerFunc func(key Key) bool

IteratorKeyConsumerFunc is a consumer function for an iterating function which iterates only over keys. They key must not be prefixed with the realm. Returning false from this function indicates to abort the iteration.

type IteratorKeyValueConsumerFunc

type IteratorKeyValueConsumerFunc func(key Key, value Value) bool

IteratorKeyValueConsumerFunc is a consumer function for an iterating function which iterates over keys and values. They key must not be prefixed with the realm. Returning false from this function indicates to abort the iteration.

type KVStore

type KVStore interface {
	// WithRealm is a factory method for using the same underlying storage with a different realm.
	WithRealm(realm Realm) (KVStore, error)

	// WithExtendedRealm is a factory method for using the same underlying storage with an realm appended to existing one.
	WithExtendedRealm(realm Realm) (KVStore, error)

	// Realm returns the configured realm.
	Realm() Realm

	// Iterate iterates over all keys and values with the provided prefix. You can pass kvstore.EmptyPrefix to iterate over all keys and values.
	// Optionally the direction for the iteration can be passed (default: IterDirectionForward).
	Iterate(prefix KeyPrefix, kvConsumerFunc IteratorKeyValueConsumerFunc, direction ...IterDirection) error

	// IterateKeys iterates over all keys with the provided prefix. You can pass kvstore.EmptyPrefix to iterate over all keys.
	// Optionally the direction for the iteration can be passed (default: IterDirectionForward).
	IterateKeys(prefix KeyPrefix, consumerFunc IteratorKeyConsumerFunc, direction ...IterDirection) error

	// Clear clears the realm.
	Clear() error

	// Get gets the given key or nil if it doesn't exist or an error if an error occurred.
	Get(key Key) (value Value, err error)

	// Set sets the given key and value.
	Set(key Key, value Value) error

	// Has checks whether the given key exists.
	Has(key Key) (bool, error)

	// Delete deletes the entry for the given key.
	Delete(key Key) error

	// DeletePrefix deletes all the entries matching the given key prefix.
	DeletePrefix(prefix KeyPrefix) error

	// Flush persists all outstanding write operations to disc.
	Flush() error

	// Close closes the database file handles.
	Close() error

	// Batched returns a BatchedMutations interface to execute batched mutations.
	Batched() (BatchedMutations, error)
}

KVStore persists, deletes and retrieves data.

type Key

type Key = []byte

type KeyPrefix

type KeyPrefix = []byte

type Option

type Option func(opts *Options)

Option is a function setting a BatchedWriter option.

func WithBatchSize

func WithBatchSize(batchSize int) Option

WithBatchSize defines the maximum amount of elements in the batch.

func WithBatchTimeout

func WithBatchTimeout(batchTimeout time.Duration) Option

WithBatchTimeout defines the timeout for collecting elements for the batch.

func WithQueueSize

func WithQueueSize(queueSize int) Option

WithQueueSize defines the size of the batch queue.

type Options

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

Options define options for the BatchedWriter.

type Realm

type Realm = []byte

type Sequence

type Sequence struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Sequence represents a simple integer sequence backed by a KVStore. A Sequence can be used to get a list of monotonically increasing integers.

func NewSequence

func NewSequence(store KVStore, key []byte, interval uint64) (*Sequence, error)

NewSequence initiates a new sequence object backed by the provided store. The interval value defines how many Next() requests can be served from memory without an access to the store. Multiple sequences can be created by providing different keys.

func (*Sequence) Next

func (seq *Sequence) Next() (uint64, error)

Next returns the next integer in the sequence.

func (*Sequence) Release

func (seq *Sequence) Release() error

Release the leased sequence to avoid wasted integers.

type StoreHealthTracker

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

func NewStoreHealthTracker

func NewStoreHealthTracker(store KVStore, storePrefixHealth []byte, storeVersion byte, storeVersionUpdateFunc StoreVersionUpdateFunc) (*StoreHealthTracker, error)

func (*StoreHealthTracker) CheckCorrectStoreVersion

func (s *StoreHealthTracker) CheckCorrectStoreVersion() (bool, error)

func (*StoreHealthTracker) Close

func (s *StoreHealthTracker) Close() error

func (*StoreHealthTracker) Flush

func (s *StoreHealthTracker) Flush() error

func (*StoreHealthTracker) IsCorrupted

func (s *StoreHealthTracker) IsCorrupted() (bool, error)

func (*StoreHealthTracker) IsTainted

func (s *StoreHealthTracker) IsTainted() (bool, error)

func (*StoreHealthTracker) MarkCorrupted

func (s *StoreHealthTracker) MarkCorrupted() error

func (*StoreHealthTracker) MarkHealthy

func (s *StoreHealthTracker) MarkHealthy() error

func (*StoreHealthTracker) MarkTainted

func (s *StoreHealthTracker) MarkTainted() error

func (*StoreHealthTracker) StoreVersion

func (s *StoreHealthTracker) StoreVersion() (byte, error)

StoreVersion returns the store version.

func (*StoreHealthTracker) UpdateStoreVersion

func (s *StoreHealthTracker) UpdateStoreVersion() (bool, error)

UpdateStoreVersion tries to migrate the existing data to the new store version. Returns true if the store needs to be updated / was updated.

type StoreVersionUpdateFunc

type StoreVersionUpdateFunc func(oldVersion byte, newVersion byte) error

type TypedStore

type TypedStore[K, V any, KPtr serializer.MarshalablePtr[K], VPtr serializer.MarshalablePtr[V]] struct {
	// contains filtered or unexported fields
}

TypedStore is a generically typed wrapper around a KVStore that abstracts serialization away.

func NewTypedStore

func NewTypedStore[K, V any, KPtr serializer.MarshalablePtr[K], VPtr serializer.MarshalablePtr[V]](kv KVStore) *TypedStore[K, V, KPtr, VPtr]

NewTypedStore is the constructor for TypedStore.

func (*TypedStore[K, V, KPtr, VPtr]) Delete

func (t *TypedStore[K, V, KPtr, VPtr]) Delete(key K) (err error)

Delete deletes the given key from the store.

func (*TypedStore[K, V, KPtr, VPtr]) Get

func (t *TypedStore[K, V, KPtr, VPtr]) Get(key K) (value V, err error)

Get gets the given key or an error if an error occurred.

func (*TypedStore[K, V, KPtr, VPtr]) Iterate

func (t *TypedStore[K, V, KPtr, VPtr]) Iterate(prefix KeyPrefix, callback func(key K, value V) (advance bool), direction ...IterDirection) (err error)

func (*TypedStore[K, V, KPtr, VPtr]) Set

func (t *TypedStore[K, V, KPtr, VPtr]) Set(key K, value V) (err error)

Set sets the given key and value.

type Value

type Value = []byte

Directories

Path Synopsis
Package mapdb provides a map implementation of a key value store.
Package mapdb provides a map implementation of a key value store.

Jump to

Keyboard shortcuts

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