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
- Variables
- func FormatBinLogEvent(event []byte) (string, error)
- func Int64(v []byte, err error) (int64, error)
- func MaxInt32(a int32, b int32) int32
- func MaxUInt32(a uint32, b uint32) uint32
- func MinUInt32(a uint32, b uint32) uint32
- func PutInt64(v int64) []byte
- func ReadEventFromReader(rb io.Reader, f func(head *BinLogHead, event []byte) error) error
- func Slice(s string) (b []byte)
- func StrInt32(v []byte, err error) (int32, error)
- func StrInt64(v []byte, err error) (int64, error)
- func StrInt8(v []byte, err error) (int8, error)
- func StrPutInt64(v int64) []byte
- func String(b []byte) (s string)
- type BinLog
- func (l *BinLog) Close()
- func (l *BinLog) FormatLogFileName(index int64) string
- func (l *BinLog) FormatLogFilePath(index int64) string
- func (l *BinLog) Log(args ...[]byte) error
- func (l *BinLog) LogFileIndex() int64
- func (l *BinLog) LogFileName() string
- func (l *BinLog) LogFilePos() int64
- func (l *BinLog) LogNames() []string
- func (l *BinLog) LogPath() string
- func (l *BinLog) Purge(n int) error
- func (l *BinLog) PurgeAll() error
- func (l *BinLog) Wait() <-chan struct{}
- type BinLogAnchor
- type BinLogHead
- type BitPair
- type DB
- func (db *DB) BCount(key []byte, start int32, end int32) (cnt int32, err error)
- func (db *DB) BDelete(key []byte) (drop int64, err error)
- func (db *DB) BExpire(key []byte, duration int64) (int64, error)
- func (db *DB) BExpireAt(key []byte, when int64) (int64, error)
- func (db *DB) BGet(key []byte) (data []byte, err error)
- func (db *DB) BGetBit(key []byte, offset int32) (uint8, error)
- func (db *DB) BMSetBit(key []byte, args ...BitPair) (place int64, err error)
- func (db *DB) BOperation(op uint8, dstkey []byte, srckeys ...[]byte) (blen int32, err error)
- func (db *DB) BPersist(key []byte) (int64, error)
- func (db *DB) BScan(key []byte, count int, inclusive bool, match string) ([][]byte, error)
- func (db *DB) BSetBit(key []byte, offset int32, val uint8) (ori uint8, err error)
- func (db *DB) BTTL(key []byte) (int64, error)
- func (db *DB) BTail(key []byte) (int32, error)
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) Commit() error
- func (db *DB) Decr(key []byte) (int64, error)
- func (db *DB) DecrBy(key []byte, decrement int64) (int64, error)
- func (db *DB) Del(keys ...[]byte) (int64, error)
- func (db *DB) Exists(key []byte) (int64, error)
- func (db *DB) Expire(key []byte, duration int64) (int64, error)
- func (db *DB) ExpireAt(key []byte, when int64) (int64, error)
- func (db *DB) FlushAll() (drop int64, err error)
- func (db *DB) Get(key []byte) ([]byte, error)
- func (db *DB) GetSet(key []byte, value []byte) ([]byte, error)
- func (db *DB) HClear(key []byte) (int64, error)
- func (db *DB) HDel(key []byte, args ...[]byte) (int64, error)
- func (db *DB) HExpire(key []byte, duration int64) (int64, error)
- func (db *DB) HExpireAt(key []byte, when int64) (int64, error)
- func (db *DB) HGet(key []byte, field []byte) ([]byte, error)
- func (db *DB) HGetAll(key []byte) ([]FVPair, error)
- func (db *DB) HIncrBy(key []byte, field []byte, delta int64) (int64, error)
- func (db *DB) HKeys(key []byte) ([][]byte, error)
- func (db *DB) HLen(key []byte) (int64, error)
- func (db *DB) HMclear(keys ...[]byte) (int64, error)
- func (db *DB) HMget(key []byte, args ...[]byte) ([][]byte, error)
- func (db *DB) HMset(key []byte, args ...FVPair) error
- func (db *DB) HPersist(key []byte) (int64, error)
- func (db *DB) HScan(key []byte, count int, inclusive bool, match string) ([][]byte, error)
- func (db *DB) HSet(key []byte, field []byte, value []byte) (int64, error)
- func (db *DB) HTTL(key []byte) (int64, error)
- func (db *DB) HValues(key []byte) ([][]byte, error)
- func (db *DB) Incr(key []byte) (int64, error)
- func (db *DB) IncrBy(key []byte, increment int64) (int64, error)
- func (db *DB) Index() int
- func (db *DB) IsAutoCommit() bool
- func (db *DB) IsInMulti() bool
- func (db *DB) IsTransaction() bool
- func (db *DB) Key(it *store.Iterator) ([]byte, error)
- func (db *DB) LClear(key []byte) (int64, error)
- func (db *DB) LExpire(key []byte, duration int64) (int64, error)
- func (db *DB) LExpireAt(key []byte, when int64) (int64, error)
- func (db *DB) LIndex(key []byte, index int32) ([]byte, error)
- func (db *DB) LLen(key []byte) (int64, error)
- func (db *DB) LMclear(keys ...[]byte) (int64, error)
- func (db *DB) LPersist(key []byte) (int64, error)
- func (db *DB) LPop(key []byte) ([]byte, error)
- func (db *DB) LPush(key []byte, arg1 []byte, args ...[]byte) (int64, error)
- func (db *DB) LRange(key []byte, start int32, stop int32) ([][]byte, error)
- func (db *DB) LScan(key []byte, count int, inclusive bool, match string) ([][]byte, error)
- func (db *DB) LTTL(key []byte) (int64, error)
- func (db *DB) Lock()
- func (db *DB) MGet(keys ...[]byte) ([][]byte, error)
- func (db *DB) MSet(args ...KVPair) error
- func (db *DB) MaxKey() ([]byte, error)
- func (db *DB) Multi() (*Multi, error)
- func (db *DB) Persist(key []byte) (int64, error)
- func (db *DB) RPop(key []byte) ([]byte, error)
- func (db *DB) RPush(key []byte, arg1 []byte, args ...[]byte) (int64, error)
- func (db *DB) Remove(key []byte) bool
- func (db *DB) SAdd(key []byte, args ...[]byte) (int64, error)
- func (db *DB) SCard(key []byte) (int64, error)
- func (db *DB) SClear(key []byte) (int64, error)
- func (db *DB) SDiff(keys ...[]byte) ([][]byte, error)
- func (db *DB) SDiffStore(dstKey []byte, keys ...[]byte) (int64, error)
- func (db *DB) SExpire(key []byte, duration int64) (int64, error)
- func (db *DB) SExpireAt(key []byte, when int64) (int64, error)
- func (db *DB) SInter(keys ...[]byte) ([][]byte, error)
- func (db *DB) SInterStore(dstKey []byte, keys ...[]byte) (int64, error)
- func (db *DB) SIsMember(key []byte, member []byte) (int64, error)
- func (db *DB) SMclear(keys ...[]byte) (int64, error)
- func (db *DB) SMembers(key []byte) ([][]byte, error)
- func (db *DB) SPersist(key []byte) (int64, error)
- func (db *DB) SRem(key []byte, args ...[]byte) (int64, error)
- func (db *DB) SScan(key []byte, count int, inclusive bool, match string) ([][]byte, error)
- func (db *DB) STTL(key []byte) (int64, error)
- func (db *DB) SUnion(keys ...[]byte) ([][]byte, error)
- func (db *DB) SUnionStore(dstKey []byte, keys ...[]byte) (int64, error)
- func (db *DB) Scan(key []byte, count int, inclusive bool, match string) ([][]byte, error)
- func (db *DB) Seek(key []byte) (*store.Iterator, error)
- func (db *DB) Set(key []byte, value []byte) error
- func (db *DB) SetNX(key []byte, value []byte) (int64, error)
- func (db *DB) TTL(key []byte) (int64, error)
- func (db *DB) Unlock()
- func (db *DB) ZAdd(key []byte, args ...ScorePair) (int64, error)
- func (db *DB) ZCard(key []byte) (int64, error)
- func (db *DB) ZClear(key []byte) (int64, error)
- func (db *DB) ZCount(key []byte, min int64, max int64) (int64, error)
- func (db *DB) ZExpire(key []byte, duration int64) (int64, error)
- func (db *DB) ZExpireAt(key []byte, when int64) (int64, error)
- func (db *DB) ZIncrBy(key []byte, delta int64, member []byte) (int64, error)
- func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error)
- func (db *DB) ZMclear(keys ...[]byte) (int64, error)
- func (db *DB) ZPersist(key []byte) (int64, error)
- func (db *DB) ZRange(key []byte, start int, stop int) ([]ScorePair, error)
- func (db *DB) ZRangeByScore(key []byte, min int64, max int64, offset int, count int) ([]ScorePair, error)
- func (db *DB) ZRangeByScoreGeneric(key []byte, min int64, max int64, offset int, count int, reverse bool) ([]ScorePair, error)
- func (db *DB) ZRangeGeneric(key []byte, start int, stop int, reverse bool) ([]ScorePair, error)
- func (db *DB) ZRank(key []byte, member []byte) (int64, error)
- func (db *DB) ZRem(key []byte, members ...[]byte) (int64, error)
- func (db *DB) ZRemRangeByRank(key []byte, start int, stop int) (int64, error)
- func (db *DB) ZRemRangeByScore(key []byte, min int64, max int64) (int64, error)
- func (db *DB) ZRevRange(key []byte, start int, stop int) ([]ScorePair, error)
- func (db *DB) ZRevRangeByScore(key []byte, min int64, max int64, offset int, count int) ([]ScorePair, error)
- func (db *DB) ZRevRank(key []byte, member []byte) (int64, error)
- func (db *DB) ZScan(key []byte, count int, inclusive bool, match string) ([][]byte, error)
- func (db *DB) ZScore(key []byte, member []byte) (int64, error)
- func (db *DB) ZTTL(key []byte) (int64, error)
- func (db *DB) ZUnionStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error)
- type FVPair
- type KVPair
- type Multi
- type Nodb
- func (l *Nodb) Close()
- func (l *Nodb) DataDB() *store.DB
- func (l *Nodb) Dump(w io.Writer) error
- func (l *Nodb) DumpFile(path string) error
- func (l *Nodb) FlushAll() error
- func (l *Nodb) LoadDump(r io.Reader) (*BinLogAnchor, error)
- func (l *Nodb) LoadDumpFile(path string) (*BinLogAnchor, error)
- func (l *Nodb) ReadEventsTo(info *BinLogAnchor, w io.Writer) (n int, err error)
- func (l *Nodb) ReadEventsToTimeout(info *BinLogAnchor, w io.Writer, timeout int) (n int, err error)
- func (l *Nodb) ReplicateFromBinLog(filePath string) error
- func (l *Nodb) ReplicateFromData(data []byte) error
- func (l *Nodb) ReplicateFromReader(rb io.Reader) error
- func (l *Nodb) Select(index int) (*DB, error)
- type ScorePair
- type Tx
Constants ¶
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 )
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 )
const ( BinLogTypeDeletion uint8 = 0x0 BinLogTypePut uint8 = 0x1 BinLogTypeCommand uint8 = 0x2 )
const ( DBAutoCommit uint8 = 0x0 DBInTransaction uint8 = 0x1 DBInMulti uint8 = 0x2 )
const ( OPand uint8 = iota + 1 OPor OPxor OPnot )
const ( UnionType byte = 51 DiffType byte = 52 InterType byte = 53 )
Variables ¶
var ( ErrNestMulti = errors.New("nest multi not supported") ErrMultiDone = errors.New("multi has been closed") )
var ( ErrNestTx = errors.New("nest transaction not supported") ErrTxDone = errors.New("Transaction has already been committed or rolled back") )
var (
ErrScoreMiss = errors.New("zset score miss")
)
var (
ErrSkipEvent = errors.New("skip to next event")
)
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", } )
var (
Version = "0.1"
)
Functions ¶
func FormatBinLogEvent ¶
func ReadEventFromReader ¶
func StrPutInt64 ¶
Types ¶
type BinLog ¶
func (*BinLog) FormatLogFileName ¶
func (*BinLog) FormatLogFilePath ¶
func (*BinLog) LogFileIndex ¶
func (*BinLog) LogFileName ¶
func (*BinLog) LogFilePos ¶
type BinLogAnchor ¶
type BinLogHead ¶
func (*BinLogHead) InSameBatch ¶
func (h *BinLogHead) InSameBatch(ho *BinLogHead) bool
func (*BinLogHead) Len ¶
func (h *BinLogHead) Len() int
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
func (*DB) BOperation ¶
func (*DB) Begin ¶
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) IsAutoCommit ¶
func (*DB) IsTransaction ¶
func (*DB) Multi ¶
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) ZInterStore ¶
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) ZRemRangeByRank ¶
func (*DB) ZRemRangeByScore ¶
min and max must be inclusive
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
type Nodb ¶
type Nodb struct {
// contains filtered or unexported fields
}
func (*Nodb) LoadDumpFile ¶
func (l *Nodb) LoadDumpFile(path string) (*BinLogAnchor, error)
func (*Nodb) ReadEventsTo ¶
func (*Nodb) ReadEventsToTimeout ¶
try to read events, if no events read, try to wait the new event singal until timeout seconds