levelhub

package module
v0.0.0-...-1051b11 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2018 License: Apache-2.0 Imports: 8 Imported by: 0

README

README

levelhub is a wrapper of leveldb which supports multi-tenancy

Installation

go get -u github.com/xuender/levelhub

Usage

Default
package main

import "github.com/xuender/levelhub"

func main() {
	hub := levelhub.NewLevelHub("dbpath", nil)
	defer hub.Close()
	hub.Put(1, []byte("key"), []byte("val"), nil)
	hub.Put(2, []byte("key"), []byte("val"), nil)
}
Expire
hub := levelhub.NewLevelHub("dbpath", &levelhub.Options{
    Expire: time.Second * 3,
    Min:    1, // More than Min settings will trigger the expiration
})
defer hub.Close()
hub.Put(1, []byte("key"), []byte("val"), nil)
hub.Put(2, []byte("key"), []byte("val"), nil)
fmt.Println(hub.IsOpen(1), hub.IsOpen(2))
time.Sleep(time.Second * 10)
fmt.Println(hub.IsOpen(1), hub.IsOpen(2))

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LevelHub

type LevelHub struct {
	// contains filtered or unexported fields
}

LevelHub leveldb hub

func NewLevelHub

func NewLevelHub(path string, o *Options) *LevelHub

NewLevelHub create LevelHub

Example
hub := NewLevelHub("tmp", nil)
defer hub.Close()
hub.Put(1, []byte("key"), []byte("A"), nil)
hub.Put(2, []byte("key"), []byte("B"), nil)
a, _ := hub.Get(1, []byte("key"), nil)
fmt.Println(string(a))
b, _ := hub.Get(2, []byte("key"), nil)
fmt.Println(string(b))
Output:

A
B

func (*LevelHub) Clean

func (hub *LevelHub) Clean()

Clean expire db

func (*LevelHub) Close

func (hub *LevelHub) Close(nums ...int)

Close closes the nums DB.

func (*LevelHub) Delete

func (hub *LevelHub) Delete(num int, key []byte, wo *opt.WriteOptions) error

Delete deletes the value for the given key.

func (*LevelHub) Force

func (hub *LevelHub) Force()

Force more than Max db must be delete

func (*LevelHub) Get

func (hub *LevelHub) Get(num int, key []byte, ro *opt.ReadOptions) (value []byte, err error)

Get gets the value for the given key.

func (*LevelHub) Has

func (hub *LevelHub) Has(num int, key []byte, ro *opt.ReadOptions) (ret bool, err error)

Has returns true if the DB does contains the given key.

func (*LevelHub) IsOpen

func (hub *LevelHub) IsOpen(num int) bool

IsOpen returns true is DB Open.

func (*LevelHub) NewIterator

func (hub *LevelHub) NewIterator(num int, slice *util.Range, ro *opt.ReadOptions) iterator.Iterator

NewIterator returns an iterator for the latest snapshot of the underlying DB.

func (*LevelHub) Put

func (hub *LevelHub) Put(num int, key, value []byte, wo *opt.WriteOptions) error

Put sets the value for the given key.

type Options

type Options struct {
	DBOptions *opt.Options
	Min       int
	Max       int
	Expire    time.Duration
}

Options hub options

Example
hub := NewLevelHub("tmp", &Options{
	Expire: time.Second * 3,
	Min:    1, // More than Min settings will trigger the expiration
})
defer hub.Close()
hub.Put(1, []byte("key"), []byte("val"), nil)
hub.Put(2, []byte("key"), []byte("val"), nil)
fmt.Println(hub.IsOpen(1), hub.IsOpen(2))
time.Sleep(time.Second * 10)
fmt.Println(hub.IsOpen(1), hub.IsOpen(2))
Output:

true true
false false

func NewOptions

func NewOptions(o *opt.Options) *Options

NewOptions create Options

Jump to

Keyboard shortcuts

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