gkv

package module
v0.0.0-...-535183f Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2018 License: MIT Imports: 5 Imported by: 6

README

gkv

gkv is an embeddable, persistent, simple key/value(KV) database adapter for Go.

Build Status GoDoc

This package depends on third-party databases.

Features

  • Out of the box, easy to use.
  • Basic, common, pure Go.
  • Support multiple databases.

Databases

Installing

go get -u github.com/WindomZ/gkv/...

Usage

import (
	"github.com/WindomZ/gkv"
	_ "github.com/WindomZ/gkv/bolt"
)
...

// init db
db := Open("../data/bolt.db")
db.Register([]byte("tablename"))
...

// put the value for a key
db.Put([]byte("key1"), []byte("value1"))
db.Put([]byte("key2"), []byte("value2"))
...

// get the value for a key
demo.Get([]byte("key1"))
demo.Get([]byte("key2"))

If you want to switch between different databases, just change import _ "github.com/WindomZ/gkv/bolt".

For example:

  • bolt - import _ "github.com/WindomZ/gkv/bolt"
  • badger - import _ "github.com/WindomZ/gkv/badger"
  • leveldb - import _ "github.com/WindomZ/gkv/leveldb"
  • buntdb - import _ "github.com/WindomZ/gkv/buntdb"
  • sqlite3 - import _ "github.com/WindomZ/gkv/sqlite3"

Easy to switch, choose the most suitable database.

Contributing

Welcome to pull requests, report bugs, suggest ideas and discuss on issues page.

If you like it then you can put a ⭐ on it.

Documentation

Index

Constants

View Source
const DefaultTableName = "gkv"

DefaultTableName the default name of table.

Variables

View Source
var ErrTableName = errors.New("illegal table name")

ErrTableName illegal table name error

Functions

func Btos

func Btos(b []byte) string

Btos returns the string representation of b bytes.

func Close

func Close() error

Close releases all database resources.

func Count

func Count() int

Count returns the total number of all the keys.

func Delete

func Delete(key []byte) error

Delete deletes the given key from the database resources.

func Get

func Get(key []byte) []byte

Get retrieves the value for a key.

func Iterator

func Iterator(f func([]byte, []byte) bool) error

Iterator creates an iterator for iterating over all the keys.

func Open

func Open(table []byte, paths ...string) error

Open creates a new KV driver by table name and storage file path. table is the name of storage. paths are storage file paths.

func ProjectDir

func ProjectDir() string

ProjectDir returns the project directory.

func Put

func Put(key, value []byte) error

Put sets the value for a key.

func Register

func Register(i Instance)

Register makes a KV adapter available by the adapter name. Only the last one can take effect.

func Stob

func Stob(s string) []byte

Stob returns the bytes representation of s string.

Types

type Instance

type Instance func(paths ...string) KV

Instance is a function create a new KV Instance

type KV

type KV interface {
	// DB returns the native DB of the adapter.
	DB() interface{}
	// Close releases all database resources.
	Close() error
	// Register creates a new storage if it doesn't already exist.
	Register([]byte) error
	// Put sets the value for a key.
	Put([]byte, []byte) error
	// Get retrieves the value for a key.
	Get([]byte) []byte
	// Delete deletes the given key from the database resources.
	Delete([]byte) error
	// Count returns the total number of all the keys.
	Count() int
	// Iterator creates an iterator for iterating over all the keys.
	Iterator(func([]byte, []byte) bool) error
}

KV short for key-value, interface contains all behaviors for key-value adapter.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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