kvstore

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2022 License: Apache-2.0, BSD-2-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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)

	// 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 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