kv

package module
v0.0.0-...-c9de069 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2020 License: MIT Imports: 11 Imported by: 0

README

KV

Minimal persistent key-value store + LRU cache.

forked from: https://github.com/zserge/kv

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteItem

type ByteItem struct {
	Value []byte
}

Simple raw item encoding - copies value bytes as is

func (*ByteItem) ReadFrom

func (b *ByteItem) ReadFrom(r io.Reader) (int64, error)

func (*ByteItem) WriteTo

func (b *ByteItem) WriteTo(w io.Writer) (int64, error)

type GobItem

type GobItem struct {
	Value interface{}
}

Gob encoding - serializes arbitrary value to/from gob format

func (*GobItem) ReadFrom

func (e *GobItem) ReadFrom(r io.Reader) (int64, error)

func (*GobItem) WriteTo

func (e *GobItem) WriteTo(w io.Writer) (int64, error)

type Item

type Item interface {
	io.ReaderFrom
	io.WriterTo
}

Item is something that can be put into a Store. Items should be able to read their values from io.Reader and write them into io.Writer. Several helper implemenations are provided - for raw bytes, for JSON and for gob format.

type JSONItem

type JSONItem struct {
	Value interface{}
}

JSON encoding - serializes arbitrary value to/from JSON

func (*JSONItem) ReadFrom

func (e *JSONItem) ReadFrom(r io.Reader) (int64, error)

func (*JSONItem) WriteTo

func (e *JSONItem) WriteTo(w io.Writer) (int64, error)

type Store

type Store interface {
	Get(key string, item Item) Item
	Set(key string, item Item) <-chan error
	List(prefix string) []string
	Flush() <-chan error
}

Store is the interface that wraps basic get/set functions for a simple key-value storage.

Store can be implemented as on-disk persistent storage, or as in-memory cache.

Get fulfils the item with the value associated with the key and returns the item. Caller must provide the item instance beforehand, because the way how item serializes/deserializes itself depends on its type. Get returns no errors explicitly, but nil is returned if key is missing or any other I/O error happended. Get is synchronous, your goroutine is blocked until item is fully read from the store.

Set writes item contents to the store at the given key. It's an asynchronous operation, but caller can read from the returned channel to wait for write completion and to get notified if I/O error occurred. If item is nil the key is removed from the store

List returns list of keys that exists and in the store and start with the given prefix. If prefix is an empty string - all keys are returned. List function is syncrhonous.

Flush waits for all writing goroutines to finish and syncs all store data to the disk. Flush can be called asynchronously, or the caller can wait for the actual flush to happen by reading from the returned channel.

func NewLRU

func NewLRU(size int, backend Store) Store

Returns LRU cache which is backed up to some other store.

func NewStore

func NewStore(path string) Store

Creates a new store from the given path. Keys are file names relative to the path, values are file contents.

Jump to

Keyboard shortcuts

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