kv

package module
v0.0.0-...-954e36a Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2024 License: MIT Imports: 8 Imported by: 12

README

KV interface

Go Reference coverage

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyNotFound = errors.New("key not found")

Functions

This section is empty.

Types

type Binary

Binary is an interface that represents a binary marshaler and unmarshaler.

type Bytes

type Bytes interface {
	~[]byte | ~string
}

Bytes is an interface that represents a byte slice or a string.

type Codec

type Codec[V any] interface {
	// Marshal encodes a Go value to a slice of bytes.
	Marshal(v V) ([]byte, error)
	// Unmarshal decodes a slice of bytes into a Go value.
	Unmarshal(data []byte, v *V) error
}

type CodecBinary

type CodecBinary[V encoding.BinaryMarshaler, VP binaryPointer[V]] struct{}

CodecBinary encodes/decodes Go values to/from binary.

func (CodecBinary[V, VP]) Marshal

func (c CodecBinary[V, VP]) Marshal(v V) ([]byte, error)

Marshal encodes a Go value to JSON.

func (CodecBinary[V, VP]) Unmarshal

func (c CodecBinary[V, VP]) Unmarshal(data []byte, v VP) error

Unmarshal decodes a JSON value into a Go value.

type CodecBytes

type CodecBytes[V Bytes] struct{}

func (CodecBytes[V]) Marshal

func (CodecBytes[V]) Marshal(v V) ([]byte, error)

Marshal implements kv.Codec.

func (CodecBytes[V]) Unmarshal

func (CodecBytes[V]) Unmarshal(data []byte, v *V) error

Unmarshal implements kv.Codec.

type CodecGob

type CodecGob[V any] struct{}

CodecGob encodes/decodes Go values to/from gob. You can use encoding.Gob instead of creating an instance of this struct.

func (CodecGob[V]) Marshal

func (c CodecGob[V]) Marshal(v V) ([]byte, error)

Marshal encodes a Go value to gob.

func (CodecGob[V]) Unmarshal

func (c CodecGob[V]) Unmarshal(data []byte, v *V) error

Unmarshal decodes a gob value into a Go value.

type CodecJSON

type CodecJSON[V any] struct{}

CodecJSON encodes/decodes Go values to/from JSON. You can use encoding.JSON instead of creating an instance of this struct.

func (CodecJSON[V]) Marshal

func (c CodecJSON[V]) Marshal(v V) ([]byte, error)

Marshal encodes a Go value to JSON.

func (CodecJSON[V]) Unmarshal

func (c CodecJSON[V]) Unmarshal(data []byte, v *V) error

Unmarshal decodes a JSON value into a Go value.

type Edit

type Edit[V any] func(ctx context.Context, v V) (V, error)

type Iter

type Iter[K, V any] func(k K, v V) error

Iter is a function type that represents an iterator function for key-value pairs.

type Locks

type Locks[K any] interface {
	Lock(ctx context.Context, key K) error
	Unlock(ctx context.Context, key K) error

	// closing lock storage and releasing all locks
	Close(ctx context.Context) error
}

type Order

type Order[K any] struct {
	Min     K
	Max     K
	Reverse bool
}

type Store

type Store[K any, V any] interface {
	// Set stores the given value for the given key.
	// The implementation automatically marshalls the value.
	// The marshalling format depends on the implementation. It can be JSON, gob, etc.
	Set(ctx context.Context, k K, v V) error

	// Get retrieves the value for the given key.
	// The implementation automatically unmarshalls the value.
	// The unmarshalling source depends on the implementation. It can be JSON, gob, etc.
	// The automatic unmarshalling requires a pointer to an object of the correct type
	// being passed as a parameter.
	// In the case of a struct, the Get method will populate the fields of the object
	// that the passed pointer points to with the values of the retrieved object's values.
	// If no value is found, it returns (false, nil).
	Get(ctx context.Context, k K) (V, error)

	// Delete deletes the stored value for the given key.
	// Deleting a non-existing key-value MUST NOT lead to an error.
	Delete(ctx context.Context, k K) error

	// Edit retrieves the value for the given key, calls the provided edit function with the value,
	Edit(ctx context.Context, k K, edit Edit[V]) error

	// Close must be called when the work with the key-value store is done.
	//
	// As the same interface is used for managing transactions, calling Close() will commit the transaction in this case.
	// Most other implementations are meant to be long-lived, so only call Close() at the very end.
	//
	// Some implementations might not need the store to be closed,
	// but as long as you work with the kv.Store interface, you never know which implementation
	// is passed to your method, so you should always call it.
	Close(ctx context.Context) error

	// Range iterates over all key-value pairs in the store and calls the provided iterator function for each pair.
	// The iterator function should return non-nil error to stop the iteration, is this case. This error will be returned by Range, its canonical to return [io.EOF]
	Range(ctx context.Context, iter Iter[K, V]) error

	// RangeWithPrefix iterates over all key-value pairs in the store that have the given prefix
	// and calls the provided iterator function for each pair.
	// The iterator function should return non-nil error to stop the iteration.
	RangeWithPrefix(ctx context.Context, prefix K, iter Iter[K, V]) error
}

Store is an interface that represents a key-value store. It provides methods for storing, retrieving, and deleting key-value pairs. The implementation of the store can use different marshalling formats, such as JSON or gob. Generic type of the value must be non-pointer type, until implementation says otherwise.

func PrefixBinary

func PrefixBinary[K encoding.BinaryMarshaler, V any, KP binaryUnmarshalerDereference[K]](s Store[K, V], prefix K) (Store[K, V], error)

func PrefixBytes

func PrefixBytes[K Bytes, V any](s Store[K, V], prefix K) Store[K, V]

type StoreOrdered

type StoreOrdered[K, V any] interface {
	RangeOrdered(ctx context.Context, order Order[K], iter Iter[K, V]) error
}

type TransactionalStore

type TransactionalStore[K, V any] interface {
	Transaction(update bool) (Store[K, V], error)
}

Directories

Path Synopsis
kvbadger module
kvbbolt module
kvbitcask module
kvmemory module
kvolric module
testsuite module

Jump to

Keyboard shortcuts

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