kv

package module
v1.1.2 Latest Latest
Warning

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

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

README

📦 kv

GoDoc CircleCI Go Report Coverage Status

A flexible and extensible library for key-value storage.

  • Multiple encoding/decoding formats
  • Persistent database drivers
  • In-memory database drivers
  • Time-to-live caching
  • Safe for concurrent use
  • Production ready

Installation

go get github.com/renproject/kv

Requirements

Requires go1.6 or newer.

Usage

Codec

A Codec encodes interface{} values into bytes, decode bytes into the interface{} values. Generally, when a specific type is not supported, a Codec will panic. Out of the box, KV supports:

  • JSONCodec which encodes/decodes using the standard library JSON marshaler, and
  • GobCodec which encodes/decodes using the standard library [Gob]https://golang.org/pkg/encoding/gob marshaler (you must explicitly register types outside of KV).

An example of using the JSONCodec:

db := kv.NewLevelDB(".db", kv.JSONCodec)
DB

A DB is a key/value database. The key is a string and the value is an interface{} that can be encoded/decoded by the chosen Codec (different DBs can use different Codecs). A DB is safe for concurrent if, and only if, the underlying driver is safe for concurrent use (the LevelDB driver and BadgerDB driver are safe for concurrent use).

An example of initialising a DB:

// Initialising an in-memory database 
db := kv.NewMemDB(kv.JSONCodec)

// Initialising a LevelDB database
db = kv.NewLevelDB(".ldb", kv.JSONCodec)

// Initialising a BadgerDB database 
db = kv.NewBadgerDB(".bdb", kv.JSONCodec)

Although reading/writing is usually done through a Table, you can read/write using the DB directly (you must be careful that keys will not conflict with Table name hashes):

// Write
if err := db.Insert("key", "value"); err != nil {
    log.Fatalf("error inserting: %v", err)
}

// Read
var value string
if err := db.Get("key", &value); err != nil {
    log.Fatalf("error getting: %v", err)
}

// Delete
if err := db.Delete("key"); err != nil {
    log.Fatalf("error deleting: %v", err)
}

// Number of key/value pairs with the given prefix
size, err := db.Size("")
if err != nil {
    log.Fatalf("error sizing: %v", err)
}
log.Printf("%v key/value pairs found", size)

// Get an iterator over all key/value pairs with the given prefix
iter := db.Iterator("")
Table

A Table is an abstraction over a DB partitions key/value pairs into non-overlapping groups. This allows you to iterate over small groups of key/value pairs that are logically related. You must ensure that Table names are unique.

An example of basic use:

type Foo struct{
    A string
    B int
    C []byte
}

// Init
table := kv.NewTable(db, "myAwesomeTable")

// Write
foo := Foo{"foo", 420, []byte{1,2,3}}
if err := table.Insert("key", foo); err != nil {
    log.Fatalf("error inserting into table: %v", err)
}

// Read
bar := Foo{}
if err := table.Get("key", &bar); err != nil {
    log.Fatalf("error getting from table: %v", err)
}

The most useful feature of Tables is iteration:

// Get the number of key/value pairs in the table
size, err := table.Size()
if err != nil {
    log.Fatalf("error sizing table: %v", err)
}
log.Printf("%v key/value pairs found", size)

// Iterate over all key/value pairs in the table
for iter := table.Iterator(); iter.Next(); {
    key, err := iter.Key()
    if err != nil {
        continue
    }
    value := Foo{}
    if err = iter.Value(&value); err != nil {
        continue
    }
}

Benchmarks

Database Number of iterations run Time (ns/op) Memory (bytes/op)
LevelDB 2000 10784337 4397224
BadgerDB 100 200012411 200012411

Contributors

Built with ❤ by Ren.

Documentation

Overview

Package kv defines a standard interface for key-value storage and iteration. It supports persistent storage using LevelDB and BadgerDB. It also supports non-persistent storage using concurrent-safe in-memory maps.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound is returned when there is no value associated with a key.
	ErrKeyNotFound = db.ErrKeyNotFound

	// ErrEmptyKey is returned when a key is the empty string.
	ErrEmptyKey = db.ErrEmptyKey

	// ErrIndexOutOfRange is returned when the iterator index is less than zero,
	// or, greater than or equal to the size of the iterator.
	ErrIndexOutOfRange = db.ErrIndexOutOfRange
)
View Source
var (
	// BinaryCodec is a binary codec that marshals and unmarshals values using
	// the standard Golang Binary marshalers. For more information, see
	// https://golang.org/pkg/encoding.
	BinaryCodec = codec.BinaryCodec

	// JSONCodec is a json codec that marshals and unmarshals values using the
	// standard Golang JSON marshalers. For more information, see
	// https://golang.org/pkg/encoding/json.
	JSONCodec = codec.JSONCodec

	// GobCodec is a gob codec that encodes and decodes values using gob. For
	// more information, see https://golang.org/pkg/encoding/gob.
	GobCodec = codec.GobCodec
)

Codecs

View Source
var (
	// NewMemDB returns a key-value database that is implemented in-memory. This
	// implementation is fast, but does not store data on-disk. It is safe for
	// concurrent use.
	NewMemDB = memdb.New

	// NewBadgerDB returns a key-value database that is implemented using
	// BadgerDB. For more information, see https://github.com/dgraph-io/badger.
	NewBadgerDB = badgerdb.New

	// NewLevelDB returns a key-value database that is implemented using
	// levelDB. For more information, see https://github.com/syndtr/goleveldb.
	NewLevelDB = leveldb.New

	// NewTable returns a new table basing on the given DB and codec.
	NewTable = db.NewTable
)
View Source
var (
	// NewLRUTable wraps a given Table and creates a Table which has lru cache.
	NewLRUTable = lru.NewLruTable

	// NewTTLCache wraps a given DB and creates a time-to-live DB. It will
	// automatically prune the data in the db until the context expires.
	NewTTLCache = ttl.New
)

Functions

This section is empty.

Types

type Codec added in v1.0.0

type Codec = db.Codec

A Codec defines an encoding/decoding between values and bytes.

type DB added in v1.0.0

type DB = db.DB

A DB is a low-level interface for storing and iterating over key/value pairs.

type Iterator

type Iterator = db.Iterator

An Iterator is used to lazily iterate over key/value pairs.

type Table added in v1.0.0

type Table = db.Table

A Table is an abstraction over the DB that partitions key/value pairs. The Table name must be unique compared to other Table names.

Directories

Path Synopsis
lru
ttl

Jump to

Keyboard shortcuts

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