kvdb

package module
v0.0.0-...-9babf30 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2021 License: MIT Imports: 3 Imported by: 0

README

KVDB

A tree like structure key value database powered by SQLite, LevelDB, MongoDB etc

Ci codecov Go Report Card Go Reference MPLv2 License

Getting Started

Simple local usage
// Create A KVDB instance using RDB or LevelDB backend
db, err := rdb.NewDB(rdb.DriverSqlite3, "sqlite.db")
if err != nil {
    panic(err)
}
// Use this instance for `Get/Set/Delete/...` operations
err = db.Set("k", "v")
if err != nil {
    panic(err)
}
rst, err := db.Get("k")
if err != nil {
    panic(err)
}
fmt.Println("value is:", rst.Value) // should be v
Service usage

Some DB like SQLite and LevelDB does not provide a server for remote connect, which means unavailable for a common data source for distributed services. KVDB provide a service layer so you can easily use it in other process or a remote program.

  • Server side
db, err := rdb.NewDB(rdb.DriverSqlite3, "sqlite.db")
if err != nil {
    panic(err)
}
// this line will block your goroutine and start serving KVDB by provided
// network and address
err = server.StartServer(db, "tcp", ":9090")
if err != nil {
    panic(err)
}
  • Client side
db, err := service.DialKVDBService("tcp", ":9090")
if err != nil {
    panic(err)
}
// just use db like local KVDB instance, for example
err = db.Set("k", "v")
if err != nil {
    panic(err)
}
rst, err := db.Get("k")
if err != nil {
    panic(err)
}
fmt.Println("value is:", rst.Value) // should be v
Tree like structure

KVDB treat key also as path of key tree, for example, for a key tree like this:

        a  
        |  
     /     \  
   b1       b2  
   |        |  
 /   \    /   \  
c1   c2  c3   c4  

Should use follow keys:

a  
a.b1  
a.b2  
a.b1.c1  
a.b1.c2  
a.b2.c3  
a.b2.c4  

Then when using Get/GetMulti, you can also retrieve children (not grand children) key-values

rst, err := db.Get("a", kvdb.GetChildren("", 2))
if err != nil {
    panic(err)
}
fmt.Println("length of children of result:", len(rst.Children)) // should be 2
for k, v := range rst.Children {
    fmt.Printf("Child key: %s, value: %s", k, v)
    // should ouput a.b1 and a.b2 with it's value
}
TTL

KVDB support time to live for key, you can set expire time when using Set/SetMulti

err = db.Set("k", "v",  kvdb.SetExpire(time.Now()))
if err != nil {
    panic(err)
}
time.Sleep(time.Second)
rst, err := db.Get("k")
if err != nil {
    panic(err)
}
fmt.Println("result is:", rst) // should be nil

KVDB would not delete expired key-value data until you called Cleanup. Or you can enable auto cleanup, which will cleanup periodically.

rdb.NewDB(rdb.DriverSqlite3, "sqlite.db", kvdb.AutoClean())

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorKeyValuePairs = errors.New("invalid key value pairs")
)

Functions

This section is empty.

Types

type DBOption

type DBOption func(d *Option)

func AutoClean

func AutoClean() DBOption

AutoClean specify to enable auto clean process of DB.

func Debug

func Debug() DBOption

Debug specify to enable debug mode of DB.

func DefaultLimit

func DefaultLimit(l int) DBOption

DefaultLimit specify default limit of children pagination for DB.

func KeyPathSep

func KeyPathSep(s string) DBOption

KeyPathSep specify separater of key path for DB, default is ".".

type DeleteOption

type DeleteOption func(d *Deleter)

func DeleteChildren

func DeleteChildren() DeleteOption

DeleteChildren specify to delete children for `Delete()` or `DeleteMulti()`.

type Deleter

type Deleter struct {
	Children bool `json:"children"`
}

type GetOption

type GetOption func(g *Getter)

func GetChildren

func GetChildren(start string, limit int) GetOption

GetChildren specify to get children and set children pagination for `Get()` or `GetMulti()`. Start is the start key of children, could be full key or bare key, if using for `GetMulti()` to get children of every key, should use bare key for start. Limit is the maximum count of children of every key.

type Getter

type Getter struct {
	Children bool   `json:"children"`
	Start    string `json:"start"`
	Limit    int    `json:"limit"`
}

type KVDB

type KVDB interface {
	// Get get node of key, which include value and optional children key value
	// pairs with pagination.
	Get(key string, opts ...GetOption) (*Node, error)

	// GetMulti get node map of keys, which include value and optional children
	// key value pairs with pagination of every key.
	GetMulti(keys []string, opts ...GetOption) (map[string]Node, error)

	// Set set value for key with options, which you can specify expire time of
	// key.
	Set(key, value string, opts ...SetOption) error

	// SetMulti set key value pairs with options, which you can specify expire
	// time of keys.
	// For example, SetMulti([]string{"a", 1, "b", 2}) means set value 1 for key
	// a and set value 2 for key b.
	SetMulti(kvPairs []string, opts ...SetOption) error

	// Delete delete key with options, which you can specify also delete
	// children of this key.
	// Delete would not effect on any other keys, for example, if you delete the
	// key without any option, you can still use it's child keys or parent key.
	Delete(key string, opts ...DeleteOption) error

	// DeleteMulti delete keys with options, which you can specify also delete
	// children of these keys.
	// DeleteMulti would not effect on any other keys, for example, if you
	// delete the key without any option, you can still use it's child keys or
	// parent key.
	DeleteMulti(keys []string, opts ...DeleteOption) error

	// Exist check if key is exist.
	Exist(key string) (bool, error)

	// Cleanup delete all expired keys from DB.
	Cleanup() error

	// Close close DB. should only execute once and cannot use after close.
	Close() error
}

type Node

type Node struct {
	Value    string            `json:"value"`
	Children map[string]string `json:"children,omitempty"`
}

type Option

type Option struct {
	AutoClean    bool
	KeyPathSep   string
	DefaultLimit int
	Debug        bool
}

func InitOption

func InitOption() *Option

func (*Option) BareKey

func (db *Option) BareKey(key string) string

func (*Option) FullKey

func (db *Option) FullKey(bareKey, parentKey string) string

func (*Option) IsBareKey

func (db *Option) IsBareKey(key string) bool

func (*Option) ParentBareKey

func (db *Option) ParentBareKey(key string) string

func (*Option) ParentKey

func (db *Option) ParentKey(key string) string

type SetOption

type SetOption func(s *Setter)

func SetExpire

func SetExpire(at time.Time) SetOption

SetExpire set expire time of key(s) for `Set()` or `SetMulti()`. Expired key value data is not deleted immediately after expire, the actual delete timing depends on the logic of auto clean or manually call `Clean()`.

type Setter

type Setter struct {
	ExpireAt time.Time `json:"expireAt"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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