kv

package module
v0.0.0-...-8cf5696 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 8 Imported by: 0

README

KV interface

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// JSON is a JSONcodec that encodes/decodes Go values to/from JSON.
	JSON = JSONcodec{}
	// Gob is a GobCodec that encodes/decodes Go values to/from gob.
	Gob = GobCodec{}
)

Convenience variables

Functions

This section is empty.

Types

type BadgerStore

type BadgerStore interface {
	BadgerDB() *badger.DB
}

type Binary

type Bytes

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

type Codec

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

type GobCodec

type GobCodec struct{}

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

func (GobCodec) Marshal

func (c GobCodec) Marshal(v any) ([]byte, error)

Marshal encodes a Go value to gob.

func (GobCodec) Unmarshal

func (c GobCodec) Unmarshal(data []byte, v any) error

Unmarshal decodes a gob value into a Go value.

type Iter

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

type JSONcodec

type JSONcodec struct{}

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

func (JSONcodec) Marshal

func (c JSONcodec) Marshal(v any) ([]byte, error)

Marshal encodes a Go value to JSON.

func (JSONcodec) Unmarshal

func (c JSONcodec) Unmarshal(data []byte, v any) error

Unmarshal decodes a JSON value into a Go value.

type Option

type Option func(options) options

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.
	// The key must not be "" and the value must not be nil.
	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 parameter.
	// In 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).
	// The key must not be "" and the pointer must not be nil.
	Get(ctx context.Context, k K) (v V, found bool, err error)

	// Delete deletes the stored value for the given key.
	// Deleting a non-existing key-value pair does NOT lead to an error.
	// The key must not be "".
	Delete(ctx context.Context, k K) error

	// Close must be called when the work with the key-value store is done.
	// Most (if not all) implementations are meant to be used long-lived,
	// so only call Close() at the very end.
	// Depending on the store implementation it might do one or more of the following:
	// Make sure all pending updates make their way to disk,
	// finish open transactions,
	// close the file handle to an embedded DB,
	// close the connection to the DB server,
	// release any open resources,
	// etc.
	// Some implementation 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(ctx context.Context, iter Iter[K, V]) error
	RangeWithPrefix(ctx context.Context, k K, iter Iter[K, V]) error
}

func NewBadgerKV

func NewBadgerKV[K Bytes, V any](dir string, opts ...Option) (Store[K, V], error)

func NewBadgerKVBytes

func NewBadgerKVBytes[K, V Bytes](dir string, opts ...Option) (Store[K, V], error)

func NewBadgerKVMarhsler

func NewBadgerKVMarhsler[K encoding.BinaryMarshaler, V any, KP binaryPointer[K]](dir string, opts ...Option) (Store[K, V], error)

func NewMemoryKV

func NewMemoryKV[K Bytes, V any]() Store[K, V]

func PrefixBinary

func PrefixBinary[K encoding.BinaryMarshaler, V any, KP binaryPointer[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]

Jump to

Keyboard shortcuts

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