kv

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2019 License: Apache-2.0 Imports: 7 Imported by: 1

README

kv

A minimalist key-value store for simple use cases.

installation

go get github.com/sdeoras/kv

usage

This package defines a minimalist key-value interface and provides following implementations:

  • boltdb
  • in-memory database
  • Google cloud data-store

keys

Keys can be simple strings or be written in a filepath format, e.g. a/b/c/myKey. Keys are parsed and appropriate nested tree structure is constructed for whichever backend is being used.

Furthermore, a tree can be deleted simply by entering partial key (e.g. a/b), that is a common prefix to other keys in that tree.

Leading / is ignored, i.e., /a/b/c/myKey is the same as a/b/c/myKey.

using boltdb database as backend

To create an instance of kv using boltdb as backend you can use the NewBoltKv function as follows. A namespace is simply a partition inside the database file.

import "github.com/sdeoras/kv"

func main() {
	kvdb, closeKv, err := kv.NewBoltKv(dbFileName, nameSpace)
	// handle err
	defer closeKv()
	
	if err := kvdb.Set(key, val); err != nil {
		// handle err
	}
	
	if val, err := kvdb.Get(key); err != nil {
		// handle err
	}
}
using in-memory database backend

To create an instance of KV using in-memory backend you can use NewMemKv function as follows:

import "github.com/sdeoras/kv"

func main() {
	kvdb, err := kv.NewMemKv()
	// handle err
}
using google cloud data-store backend

To create an instance of KV using google cloud data-store as backend you can use NewDataStoreKv function as follows:

import "github.com/sdeoras/kv"

func main() {
	kvdb, closeKv, err := kv.NewDataStoreKv(context.Background(), projectID, nameSpace)
	// handle err
	defer closeKv()
}

nested keys

key can be represented in the filepath format. For instance /a/b/c/myKey1 and /a/b/c/myKey2 are part of the same bucket /a/b/c and both be deleted by deleting the key /a/b/c

enumerate keys

If following keys are present in the db:

  • /a/b/c/key1
  • /a/b/c/key2

then enumerating using /, or /a or /a/b or /a/b/c would result in the following list as output:

  • /a/b/c/key1
  • /a/b/c/key2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBoltKv

func NewBoltKv(dbFile, nameSpace string) (KV, CloseFunc, error)

NewBoltKv provides a new instance of KV with bolt db as backend.

func NewDataStoreKv added in v0.0.4

func NewDataStoreKv(ctx context.Context, projectID, nameSpace string) (KV, CloseFunc, error)

NewDataStoreKv provides a new instance of KV with Google cloud data-store as backend.

Types

type Buffer added in v0.0.4

type Buffer struct {
	Valid bool
	Value []byte
}

type CloseFunc

type CloseFunc func() error

CloseFunc is a closure that can be deferred called to close the database.

type KV

type KV interface {
	// Set sets a value against a key.
	// A key can be in the format of a path: a/b/c/key
	Set(key string, val []byte) error
	// Get gets a value from a key.
	Get(key string) ([]byte, error)
	// Delete delets a key deleting everything in the tree
	// if key points to a bucket name.
	Delete(key string) error
	// Enumerate lists keys
	Enumerate(key string) ([]string, error)
}

KV defines a minimalist interface to a key value store.

func NewMemKv added in v0.0.3

func NewMemKv() KV

NewMemKv provides a new instance of KV with mem db as backend.

Jump to

Keyboard shortcuts

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