nodb

package module
v0.0.0-...-fc1ef06 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2016 License: MIT Imports: 22 Imported by: 0

README

NoDB

中文

Nodb is a fork of ledisdb and shrink version. It's get rid of all C or other language codes and only keep Go's. It aims to provide a nosql database library rather than a redis like server. So if you want a redis like server, ledisdb is the best choose.

Nodb is a pure Go and high performance NoSQL database library. It supports some data structure like kv, list, hash, zset, bitmap, set.

Nodb now use goleveldb as backend to store data.

Features

  • Rich data structure: KV, List, Hash, ZSet, Bitmap, Set.
  • Stores lots of data, over the memory limit.
  • Supports expiration and ttl.
  • Easy to embed in your own Go application.

Install

go get github.com/lunny/nodb

Package Example

Open And Select database
import(
  "github.com/lunny/nodb"
  "github.com/lunny/nodb/config"
)

cfg := new(config.Config)
cfg.DataDir = "./"
dbs, err := nodb.Open(cfg)
if err != nil {
  fmt.Printf("nodb: error opening db: %v", err)
}

db, _ := dbs.Select(0)
KV

KV is the most basic nodb type like any other key-value database.

err := db.Set(key, value)
value, err := db.Get(key)
List

List is simply lists of values, sorted by insertion order. You can push or pop value on the list head (left) or tail (right).

err := db.LPush(key, value1)
err := db.RPush(key, value2)
value1, err := db.LPop(key)
value2, err := db.RPop(key)
Hash

Hash is a map between fields and values.

n, err := db.HSet(key, field1, value1)
n, err := db.HSet(key, field2, value2)
value1, err := db.HGet(key, field1)
value2, err := db.HGet(key, field2)
ZSet

ZSet is a sorted collections of values. Every member of zset is associated with score, a int64 value which used to sort, from smallest to greatest score. Members are unique, but score may be same.

n, err := db.ZAdd(key, ScorePair{score1, member1}, ScorePair{score2, member2})
ay, err := db.ZRangeByScore(key, minScore, maxScore, 0, -1)

Thanks

Gmail: siddontang@gmail.com

Documentation

Overview

package nodb is a high performance embedded NoSQL.

nodb supports various data structure like kv, list, hash and zset like redis.

Other features include binlog replication, data with a limited time-to-live.

Usage

First create a nodb instance before use:

l := nodb.Open(cfg)

cfg is a Config instance which contains configuration for nodb use, like DataDir (root directory for nodb working to store data).

After you create a nodb instance, you can select a DB to store you data:

db, _ := l.Select(0)

DB must be selected by a index, nodb supports only 16 databases, so the index range is [0-15].

KV

KV is the most basic nodb type like any other key-value database.

err := db.Set(key, value)
value, err := db.Get(key)

List

List is simply lists of values, sorted by insertion order. You can push or pop value on the list head (left) or tail (right).

err := db.LPush(key, value1)
err := db.RPush(key, value2)
value1, err := db.LPop(key)
value2, err := db.RPop(key)

Hash

Hash is a map between fields and values.

n, err := db.HSet(key, field1, value1)
n, err := db.HSet(key, field2, value2)
value1, err := db.HGet(key, field1)
value2, err := db.HGet(key, field2)

ZSet

ZSet is a sorted collections of values. Every member of zset is associated with score, a int64 value which used to sort, from smallest to greatest score. Members are unique, but score may be same.

n, err := db.ZAdd(key, ScorePair{score1, member1}, ScorePair{score2, member2})
ay, err := db.ZRangeByScore(key, minScore, maxScore, 0, -1)

Binlog

nodb supports binlog, so you can sync binlog to another server for replication. If you want to open binlog support, set UseBinLog to true in config.

Index

Constants

View Source
const (
	NoneType    byte = 0
	KVType      byte = 1
	HashType    byte = 2
	HSizeType   byte = 3
	ListType    byte = 4
	LMetaType   byte = 5
	ZSetType    byte = 6
	ZSizeType   byte = 7
	ZScoreType  byte = 8
	BitType     byte = 9
	BitMetaType byte = 10
	SetType     byte = 11
	SSizeType   byte = 12

	ExpTimeType byte = 101
	ExpMetaType byte = 102
)
View Source
const (
	//we don't support too many databases
	MaxDBNumber uint8 = 16

	//max key size
	MaxKeySize int = 1024

	//max hash field size
	MaxHashFieldSize int = 1024

	//max zset member size
	MaxZSetMemberSize int = 1024

	//max set member size
	MaxSetMemberSize int = 1024

	//max value size
	MaxValueSize int = 10 * 1024 * 1024
)
View Source
const (
	BinLogTypeDeletion uint8 = 0x0
	BinLogTypePut      uint8 = 0x1
	BinLogTypeCommand  uint8 = 0x2
)
View Source
const (
	DBAutoCommit    uint8 = 0x0
	DBInTransaction uint8 = 0x1
	DBInMulti       uint8 = 0x2
)
View Source
const (
	OPand uint8 = iota + 1
	OPor
	OPxor
	OPnot
)
View Source
const (
	UnionType byte = 51
	DiffType  byte = 52
	InterType byte = 53
)
View Source
const (
	MinScore     int64 = -1<<63 + 1
	MaxScore     int64 = 1<<63 - 1
	InvalidScore int64 = -1 << 63

	AggregateSum byte = 0
	AggregateMin byte = 1
	AggregateMax byte = 2
)

Variables

View Source
var (
	ErrNestMulti = errors.New("nest multi not supported")
	ErrMultiDone = errors.New("multi has been closed")
)
View Source
var (
	ErrNestTx = errors.New("nest transaction not supported")
	ErrTxDone = errors.New("Transaction has already been committed or rolled back")
)
View Source
var (
	ErrScoreMiss = errors.New("zset score miss")
)
View Source
var (
	ErrSkipEvent = errors.New("skip to next event")
)
View Source
var (
	TypeName = map[byte]string{
		KVType:      "kv",
		HashType:    "hash",
		HSizeType:   "hsize",
		ListType:    "list",
		LMetaType:   "lmeta",
		ZSetType:    "zset",
		ZSizeType:   "zsize",
		ZScoreType:  "zscore",
		BitType:     "bit",
		BitMetaType: "bitmeta",
		SetType:     "set",
		SSizeType:   "ssize",
		ExpTimeType: "exptime",
		ExpMetaType: "expmeta",
	}
)
View Source
var (
	Version = "0.1"
)

Functions

func FormatBinLogEvent

func FormatBinLogEvent(event []byte) (string, error)

func Int64

func Int64(v []byte, err error) (int64, error)

func MaxInt32

func MaxInt32(a int32, b int32) int32

func MaxUInt32

func MaxUInt32(a uint32, b uint32) uint32

func MinUInt32

func MinUInt32(a uint32, b uint32) uint32

func PutInt64

func PutInt64(v int64) []byte

func ReadEventFromReader

func ReadEventFromReader(rb io.Reader, f func(head *BinLogHead, event []byte) error) error

func Slice

func Slice(s string) (b []byte)

no copy to change string to slice use your own risk

func StrInt32

func StrInt32(v []byte, err error) (int32, error)

func StrInt64

func StrInt64(v []byte, err error) (int64, error)

func StrInt8

func StrInt8(v []byte, err error) (int8, error)

func StrPutInt64

func StrPutInt64(v int64) []byte

func String

func String(b []byte) (s string)

no copy to change slice to string use your own risk

Types

type BinLog

type BinLog struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewBinLog

func NewBinLog(cfg *config.Config) (*BinLog, error)

func (*BinLog) Close

func (l *BinLog) Close()

func (*BinLog) FormatLogFileName

func (l *BinLog) FormatLogFileName(index int64) string

func (*BinLog) FormatLogFilePath

func (l *BinLog) FormatLogFilePath(index int64) string

func (*BinLog) Log

func (l *BinLog) Log(args ...[]byte) error

func (*BinLog) LogFileIndex

func (l *BinLog) LogFileIndex() int64

func (*BinLog) LogFileName

func (l *BinLog) LogFileName() string

func (*BinLog) LogFilePos

func (l *BinLog) LogFilePos() int64

func (*BinLog) LogNames

func (l *BinLog) LogNames() []string

func (*BinLog) LogPath

func (l *BinLog) LogPath() string

func (*BinLog) Purge

func (l *BinLog) Purge(n int) error

func (*BinLog) PurgeAll

func (l *BinLog) PurgeAll() error

func (*BinLog) Wait

func (l *BinLog) Wait() <-chan struct{}

type BinLogAnchor

type BinLogAnchor struct {
	LogFileIndex int64
	LogPos       int64
}

func (*BinLogAnchor) ReadFrom

func (m *BinLogAnchor) ReadFrom(r io.Reader) error

func (*BinLogAnchor) WriteTo

func (m *BinLogAnchor) WriteTo(w io.Writer) error

type BinLogHead

type BinLogHead struct {
	CreateTime uint32
	BatchId    uint32
	PayloadLen uint32
}

func (*BinLogHead) InSameBatch

func (h *BinLogHead) InSameBatch(ho *BinLogHead) bool

func (*BinLogHead) Len

func (h *BinLogHead) Len() int

func (*BinLogHead) Read

func (h *BinLogHead) Read(r io.Reader) error

func (*BinLogHead) Write

func (h *BinLogHead) Write(w io.Writer) error

type BitPair

type BitPair struct {
	Pos int32
	Val uint8
}

type DB

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

func (*DB) BCount

func (db *DB) BCount(key []byte, start int32, end int32) (cnt int32, err error)

func (*DB) BDelete

func (db *DB) BDelete(key []byte) (drop int64, err error)

func (*DB) BExpire

func (db *DB) BExpire(key []byte, duration int64) (int64, error)

func (*DB) BExpireAt

func (db *DB) BExpireAt(key []byte, when int64) (int64, error)

func (*DB) BGet

func (db *DB) BGet(key []byte) (data []byte, err error)

func (*DB) BGetBit

func (db *DB) BGetBit(key []byte, offset int32) (uint8, error)

func (*DB) BMSetBit

func (db *DB) BMSetBit(key []byte, args ...BitPair) (place int64, err error)

func (*DB) BOperation

func (db *DB) BOperation(op uint8, dstkey []byte, srckeys ...[]byte) (blen int32, err error)

func (*DB) BPersist

func (db *DB) BPersist(key []byte) (int64, error)

func (*DB) BScan

func (db *DB) BScan(key []byte, count int, inclusive bool, match string) ([][]byte, error)

func (*DB) BSetBit

func (db *DB) BSetBit(key []byte, offset int32, val uint8) (ori uint8, err error)

func (*DB) BTTL

func (db *DB) BTTL(key []byte) (int64, error)

func (*DB) BTail

func (db *DB) BTail(key []byte) (int32, error)

func (*DB) Begin

func (db *DB) Begin() (*Tx, error)

Begin a transaction, it will block all other write operations before calling Commit or Rollback. You must be very careful to prevent long-time transaction.

func (*DB) Commit

func (db *DB) Commit() error

func (*DB) Decr

func (db *DB) Decr(key []byte) (int64, error)

func (*DB) DecrBy

func (db *DB) DecrBy(key []byte, decrement int64) (int64, error)

func (*DB) Del

func (db *DB) Del(keys ...[]byte) (int64, error)

func (*DB) Exists

func (db *DB) Exists(key []byte) (int64, error)

func (*DB) Expire

func (db *DB) Expire(key []byte, duration int64) (int64, error)

func (*DB) ExpireAt

func (db *DB) ExpireAt(key []byte, when int64) (int64, error)

func (*DB) FlushAll

func (db *DB) FlushAll() (drop int64, err error)

func (*DB) Get

func (db *DB) Get(key []byte) ([]byte, error)

func (*DB) GetSet

func (db *DB) GetSet(key []byte, value []byte) ([]byte, error)

func (*DB) HClear

func (db *DB) HClear(key []byte) (int64, error)

func (*DB) HDel

func (db *DB) HDel(key []byte, args ...[]byte) (int64, error)

func (*DB) HExpire

func (db *DB) HExpire(key []byte, duration int64) (int64, error)

func (*DB) HExpireAt

func (db *DB) HExpireAt(key []byte, when int64) (int64, error)

func (*DB) HGet

func (db *DB) HGet(key []byte, field []byte) ([]byte, error)

func (*DB) HGetAll

func (db *DB) HGetAll(key []byte) ([]FVPair, error)

func (*DB) HIncrBy

func (db *DB) HIncrBy(key []byte, field []byte, delta int64) (int64, error)

func (*DB) HKeys

func (db *DB) HKeys(key []byte) ([][]byte, error)

func (*DB) HLen

func (db *DB) HLen(key []byte) (int64, error)

func (*DB) HMclear

func (db *DB) HMclear(keys ...[]byte) (int64, error)

func (*DB) HMget

func (db *DB) HMget(key []byte, args ...[]byte) ([][]byte, error)

func (*DB) HMset

func (db *DB) HMset(key []byte, args ...FVPair) error

func (*DB) HPersist

func (db *DB) HPersist(key []byte) (int64, error)

func (*DB) HScan

func (db *DB) HScan(key []byte, count int, inclusive bool, match string) ([][]byte, error)

func (*DB) HSet

func (db *DB) HSet(key []byte, field []byte, value []byte) (int64, error)

func (*DB) HTTL

func (db *DB) HTTL(key []byte) (int64, error)

func (*DB) HValues

func (db *DB) HValues(key []byte) ([][]byte, error)

func (*DB) Incr

func (db *DB) Incr(key []byte) (int64, error)

func (*DB) IncrBy

func (db *DB) IncrBy(key []byte, increment int64) (int64, error)

func (*DB) Index

func (db *DB) Index() int

func (*DB) IsAutoCommit

func (db *DB) IsAutoCommit() bool

func (*DB) IsInMulti

func (db *DB) IsInMulti() bool

func (*DB) IsTransaction

func (db *DB) IsTransaction() bool

func (*DB) Key

func (db *DB) Key(it *store.Iterator) ([]byte, error)

func (*DB) LClear

func (db *DB) LClear(key []byte) (int64, error)

func (*DB) LExpire

func (db *DB) LExpire(key []byte, duration int64) (int64, error)

func (*DB) LExpireAt

func (db *DB) LExpireAt(key []byte, when int64) (int64, error)

func (*DB) LIndex

func (db *DB) LIndex(key []byte, index int32) ([]byte, error)

func (*DB) LLen

func (db *DB) LLen(key []byte) (int64, error)

func (*DB) LMclear

func (db *DB) LMclear(keys ...[]byte) (int64, error)

func (*DB) LPersist

func (db *DB) LPersist(key []byte) (int64, error)

func (*DB) LPop

func (db *DB) LPop(key []byte) ([]byte, error)

func (*DB) LPush

func (db *DB) LPush(key []byte, arg1 []byte, args ...[]byte) (int64, error)

func (*DB) LRange

func (db *DB) LRange(key []byte, start int32, stop int32) ([][]byte, error)

func (*DB) LScan

func (db *DB) LScan(key []byte, count int, inclusive bool, match string) ([][]byte, error)

func (*DB) LTTL

func (db *DB) LTTL(key []byte) (int64, error)

func (*DB) Lock

func (db *DB) Lock()

func (*DB) MGet

func (db *DB) MGet(keys ...[]byte) ([][]byte, error)

func (*DB) MSet

func (db *DB) MSet(args ...KVPair) error

func (*DB) MaxKey

func (db *DB) MaxKey() ([]byte, error)

func (*DB) Multi

func (db *DB) Multi() (*Multi, error)

begin a mutli to execute commands, it will block any other write operations before you close the multi, unlike transaction, mutli can not rollback

func (*DB) Persist

func (db *DB) Persist(key []byte) (int64, error)

func (*DB) RPop

func (db *DB) RPop(key []byte) ([]byte, error)

func (*DB) RPush

func (db *DB) RPush(key []byte, arg1 []byte, args ...[]byte) (int64, error)

func (*DB) Remove

func (db *DB) Remove(key []byte) bool

func (*DB) SAdd

func (db *DB) SAdd(key []byte, args ...[]byte) (int64, error)

func (*DB) SCard

func (db *DB) SCard(key []byte) (int64, error)

func (*DB) SClear

func (db *DB) SClear(key []byte) (int64, error)

func (*DB) SDiff

func (db *DB) SDiff(keys ...[]byte) ([][]byte, error)

func (*DB) SDiffStore

func (db *DB) SDiffStore(dstKey []byte, keys ...[]byte) (int64, error)

func (*DB) SExpire

func (db *DB) SExpire(key []byte, duration int64) (int64, error)

func (*DB) SExpireAt

func (db *DB) SExpireAt(key []byte, when int64) (int64, error)

func (*DB) SInter

func (db *DB) SInter(keys ...[]byte) ([][]byte, error)

func (*DB) SInterStore

func (db *DB) SInterStore(dstKey []byte, keys ...[]byte) (int64, error)

func (*DB) SIsMember

func (db *DB) SIsMember(key []byte, member []byte) (int64, error)

func (*DB) SMclear

func (db *DB) SMclear(keys ...[]byte) (int64, error)

func (*DB) SMembers

func (db *DB) SMembers(key []byte) ([][]byte, error)

func (*DB) SPersist

func (db *DB) SPersist(key []byte) (int64, error)

func (*DB) SRem

func (db *DB) SRem(key []byte, args ...[]byte) (int64, error)

func (*DB) SScan

func (db *DB) SScan(key []byte, count int, inclusive bool, match string) ([][]byte, error)

func (*DB) STTL

func (db *DB) STTL(key []byte) (int64, error)

func (*DB) SUnion

func (db *DB) SUnion(keys ...[]byte) ([][]byte, error)

func (*DB) SUnionStore

func (db *DB) SUnionStore(dstKey []byte, keys ...[]byte) (int64, error)

func (*DB) Scan

func (db *DB) Scan(key []byte, count int, inclusive bool, match string) ([][]byte, error)

if inclusive is true, scan range [key, inf) else (key, inf)

func (*DB) Seek

func (db *DB) Seek(key []byte) (*store.Iterator, error)

Seek search the prefix key

func (*DB) Set

func (db *DB) Set(key []byte, value []byte) error

func (*DB) SetNX

func (db *DB) SetNX(key []byte, value []byte) (int64, error)

func (*DB) TTL

func (db *DB) TTL(key []byte) (int64, error)

func (*DB) Unlock

func (db *DB) Unlock()

func (*DB) ZAdd

func (db *DB) ZAdd(key []byte, args ...ScorePair) (int64, error)

func (*DB) ZCard

func (db *DB) ZCard(key []byte) (int64, error)

func (*DB) ZClear

func (db *DB) ZClear(key []byte) (int64, error)

func (*DB) ZCount

func (db *DB) ZCount(key []byte, min int64, max int64) (int64, error)

func (*DB) ZExpire

func (db *DB) ZExpire(key []byte, duration int64) (int64, error)

func (*DB) ZExpireAt

func (db *DB) ZExpireAt(key []byte, when int64) (int64, error)

func (*DB) ZIncrBy

func (db *DB) ZIncrBy(key []byte, delta int64, member []byte) (int64, error)

func (*DB) ZInterStore

func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error)

func (*DB) ZMclear

func (db *DB) ZMclear(keys ...[]byte) (int64, error)

func (*DB) ZPersist

func (db *DB) ZPersist(key []byte) (int64, error)

func (*DB) ZRange

func (db *DB) ZRange(key []byte, start int, stop int) ([]ScorePair, error)

func (*DB) ZRangeByScore

func (db *DB) ZRangeByScore(key []byte, min int64, max int64,
	offset int, count int) ([]ScorePair, error)

min and max must be inclusive if no limit, set offset = 0 and count = -1

func (*DB) ZRangeByScoreGeneric

func (db *DB) ZRangeByScoreGeneric(key []byte, min int64, max int64,
	offset int, count int, reverse bool) ([]ScorePair, error)

min and max must be inclusive if no limit, set offset = 0 and count = -1

func (*DB) ZRangeGeneric

func (db *DB) ZRangeGeneric(key []byte, start int, stop int, reverse bool) ([]ScorePair, error)

func (*DB) ZRank

func (db *DB) ZRank(key []byte, member []byte) (int64, error)

func (*DB) ZRem

func (db *DB) ZRem(key []byte, members ...[]byte) (int64, error)

func (*DB) ZRemRangeByRank

func (db *DB) ZRemRangeByRank(key []byte, start int, stop int) (int64, error)

func (*DB) ZRemRangeByScore

func (db *DB) ZRemRangeByScore(key []byte, min int64, max int64) (int64, error)

min and max must be inclusive

func (*DB) ZRevRange

func (db *DB) ZRevRange(key []byte, start int, stop int) ([]ScorePair, error)

func (*DB) ZRevRangeByScore

func (db *DB) ZRevRangeByScore(key []byte, min int64, max int64, offset int, count int) ([]ScorePair, error)

min and max must be inclusive if no limit, set offset = 0 and count = -1

func (*DB) ZRevRank

func (db *DB) ZRevRank(key []byte, member []byte) (int64, error)

func (*DB) ZScan

func (db *DB) ZScan(key []byte, count int, inclusive bool, match string) ([][]byte, error)

func (*DB) ZScore

func (db *DB) ZScore(key []byte, member []byte) (int64, error)

func (*DB) ZTTL

func (db *DB) ZTTL(key []byte) (int64, error)

func (*DB) ZUnionStore

func (db *DB) ZUnionStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error)

type FVPair

type FVPair struct {
	Field []byte
	Value []byte
}

type KVPair

type KVPair struct {
	Key   []byte
	Value []byte
}

type Multi

type Multi struct {
	*DB
}

func (*Multi) Close

func (m *Multi) Close() error

func (*Multi) Select

func (m *Multi) Select(index int) error

type Nodb

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

func Open

func Open(cfg *config.Config) (*Nodb, error)

func (*Nodb) Close

func (l *Nodb) Close()

func (*Nodb) DataDB

func (l *Nodb) DataDB() *store.DB

very dangerous to use

func (*Nodb) Dump

func (l *Nodb) Dump(w io.Writer) error

func (*Nodb) DumpFile

func (l *Nodb) DumpFile(path string) error

func (*Nodb) FlushAll

func (l *Nodb) FlushAll() error

func (*Nodb) LoadDump

func (l *Nodb) LoadDump(r io.Reader) (*BinLogAnchor, error)

func (*Nodb) LoadDumpFile

func (l *Nodb) LoadDumpFile(path string) (*BinLogAnchor, error)

func (*Nodb) ReadEventsTo

func (l *Nodb) ReadEventsTo(info *BinLogAnchor, w io.Writer) (n int, err error)

func (*Nodb) ReadEventsToTimeout

func (l *Nodb) ReadEventsToTimeout(info *BinLogAnchor, w io.Writer, timeout int) (n int, err error)

try to read events, if no events read, try to wait the new event singal until timeout seconds

func (*Nodb) ReplicateFromBinLog

func (l *Nodb) ReplicateFromBinLog(filePath string) error

func (*Nodb) ReplicateFromData

func (l *Nodb) ReplicateFromData(data []byte) error

func (*Nodb) ReplicateFromReader

func (l *Nodb) ReplicateFromReader(rb io.Reader) error

func (*Nodb) Select

func (l *Nodb) Select(index int) (*DB, error)

type ScorePair

type ScorePair struct {
	Score  int64
	Member []byte
}

type Tx

type Tx struct {
	*DB
	// contains filtered or unexported fields
}

func (*Tx) Commit

func (tx *Tx) Commit() error

func (*Tx) Rollback

func (tx *Tx) Rollback() error

func (*Tx) Select

func (tx *Tx) Select(index int) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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