Giter VIP home page Giter VIP logo

kv's Introduction

๐Ÿ“ฆ kv

GoDoc Go Report Coverage Status License: MIT

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.11 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.

kv's People

Contributors

divyakoshy avatar jazg avatar loongy avatar vinceau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kv's Issues

Migrate to GitHub Actions from CircleCI

We want to migrate all of our CI/CD into GitHub Actions so that we can minimize the number of different services that we are using, but still maximize the integration of different features. Moving to GitHub Actions means that we can drop CircleCI (which we have been having issues with), but still allows for a deep integration with GitHub.

Tracking

  • Translate the CircleCI config file into one that is compatible with GitHub Actions.
  • Check that Actions is passing all checks.
  • Require status checks from Actions to be passing before allowing PRs to merge.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.