Documentation ¶
Overview ¶
Package db 数据库操作底层接口定义以及实现包括:leveldb、 memdb、mvcc、badgerdb、pegasus、ssdb
Index ¶
- Constants
- Variables
- func GetKey(key []byte, version int64) ([]byte, error)
- func GetKeyPerfix(key []byte) []byte
- func MustWrite(batch Batch)
- func ToNum(bs []byte) int
- type BaseDB
- type Batch
- type DB
- type GoBadgerDB
- func (db *GoBadgerDB) Close()
- func (db *GoBadgerDB) DB() *badger.DB
- func (db *GoBadgerDB) Delete(key []byte) error
- func (db *GoBadgerDB) DeleteSync(key []byte) error
- func (db *GoBadgerDB) Get(key []byte) ([]byte, error)
- func (db *GoBadgerDB) Iterator(start, end []byte, reverse bool) Iterator
- func (db *GoBadgerDB) NewBatch(sync bool) Batch
- func (db *GoBadgerDB) Print()
- func (db *GoBadgerDB) Set(key []byte, value []byte) error
- func (db *GoBadgerDB) SetSync(key []byte, value []byte) error
- func (db *GoBadgerDB) Stats() map[string]string
- type GoBadgerDBBatch
- type GoLevelDB
- func (db *GoLevelDB) BeginTx() (TxKV, error)
- func (db *GoLevelDB) Close()
- func (db *GoLevelDB) CompactRange(start, limit []byte) error
- func (db *GoLevelDB) DB() *leveldb.DB
- func (db *GoLevelDB) Delete(key []byte) error
- func (db *GoLevelDB) DeleteSync(key []byte) error
- func (db *GoLevelDB) Get(key []byte) ([]byte, error)
- func (db *GoLevelDB) Iterator(start []byte, end []byte, reverse bool) Iterator
- func (db *GoLevelDB) NewBatch(sync bool) Batch
- func (db *GoLevelDB) Print()
- func (db *GoLevelDB) Set(key []byte, value []byte) error
- func (db *GoLevelDB) SetSync(key []byte, value []byte) error
- func (db *GoLevelDB) Stats() map[string]string
- type GoMemDB
- func (db *GoMemDB) Close()
- func (db *GoMemDB) DB() *memdb.DB
- func (db *GoMemDB) Delete(key []byte) error
- func (db *GoMemDB) DeleteSync(key []byte) error
- func (db *GoMemDB) Get(key []byte) ([]byte, error)
- func (db *GoMemDB) Iterator(start []byte, end []byte, reverse bool) Iterator
- func (db *GoMemDB) NewBatch(sync bool) Batch
- func (db *GoMemDB) Print()
- func (db *GoMemDB) Set(key []byte, value []byte) error
- func (db *GoMemDB) SetSync(key []byte, value []byte) error
- func (db *GoMemDB) Stats() map[string]string
- type GoSSDB
- func (db *GoSSDB) Close()
- func (db *GoSSDB) Delete(key []byte) error
- func (db *GoSSDB) DeleteSync(key []byte) error
- func (db *GoSSDB) Get(key []byte) ([]byte, error)
- func (db *GoSSDB) Iterator(itbeg []byte, itend []byte, reverse bool) Iterator
- func (db *GoSSDB) NewBatch(sync bool) Batch
- func (db *GoSSDB) Print()
- func (db *GoSSDB) Set(key []byte, value []byte) error
- func (db *GoSSDB) SetSync(key []byte, value []byte) error
- func (db *GoSSDB) Stats() map[string]string
- type Iterator
- type IteratorDB
- type IteratorSeeker
- type KV
- type KVDB
- type KVDBList
- type ListHelper
- func (db *ListHelper) IteratorCallback(start []byte, end []byte, count int32, direction int32, ...)
- func (db *ListHelper) IteratorScan(prefix []byte, key []byte, count int32, direction int32) [][]byte
- func (db *ListHelper) IteratorScanFromFirst(prefix []byte, count int32, direction int32) (values [][]byte)
- func (db *ListHelper) IteratorScanFromLast(prefix []byte, count int32, direction int32) (values [][]byte)
- func (db *ListHelper) List(prefix, key []byte, count, direction int32) [][]byte
- func (db *ListHelper) PrefixCount(prefix []byte) (count int64)
- func (db *ListHelper) PrefixScan(prefix []byte) [][]byte
- type Lister
- type LocalDB
- func (l *LocalDB) Begin()
- func (l *LocalDB) Commit() error
- func (l *LocalDB) Get(key []byte) ([]byte, error)
- func (l *LocalDB) List(prefix, key []byte, count, direction int32) ([][]byte, error)
- func (l *LocalDB) PrefixCount(prefix []byte) (count int64)
- func (l *LocalDB) Rollback()
- func (l *LocalDB) Set(key []byte, value []byte) error
- type MVCC
- type MVCCHelper
- type MVCCIter
- type MVCCKV
- type PegasusBatch
- type PegasusDB
- func (db *PegasusDB) Close()
- func (db *PegasusDB) Delete(key []byte) error
- func (db *PegasusDB) DeleteSync(key []byte) error
- func (db *PegasusDB) Get(key []byte) ([]byte, error)
- func (db *PegasusDB) Iterator(begin []byte, end []byte, reverse bool) Iterator
- func (db *PegasusDB) NewBatch(sync bool) Batch
- func (db *PegasusDB) Print()
- func (db *PegasusDB) Set(key []byte, value []byte) error
- func (db *PegasusDB) SetSync(key []byte, value []byte) error
- func (db *PegasusDB) Stats() map[string]string
- type PegasusIt
- func (dbit *PegasusIt) Close()
- func (dbit *PegasusIt) Error() error
- func (it *PegasusIt) IsReverse() bool
- func (dbit *PegasusIt) Key() []byte
- func (dbit *PegasusIt) Next() bool
- func (it *PegasusIt) Prefix() []byte
- func (dbit *PegasusIt) Rewind() bool
- func (dbit *PegasusIt) Seek(key []byte) bool
- func (dbit *PegasusIt) Valid() bool
- func (dbit *PegasusIt) Value() []byte
- func (dbit *PegasusIt) ValueCopy() []byte
- type RoundInt
- type SDBClient
- func (c *SDBClient) Close() error
- func (c *SDBClient) Del(key string) error
- func (c *SDBClient) Do(args ...interface{}) ([]string, error)
- func (c *SDBClient) Get(key string) (*Value, error)
- func (c *SDBClient) Keys(keyStart, keyEnd string, limit int64) ([]string, error)
- func (c *SDBClient) MultiDel(key ...string) (err error)
- func (c *SDBClient) MultiGet(key ...string) (vals []*Value, err error)
- func (c *SDBClient) MultiSet(kvs map[string][]byte) (err error)
- func (c *SDBClient) Rkeys(keyStart, keyEnd string, limit int64) ([]string, error)
- func (c *SDBClient) Set(key string, val []byte) (err error)
- type SDBPool
- type SimpleMVCC
- func (m *SimpleMVCC) AddMVCC(kvs []*types.KeyValue, hash []byte, prevHash []byte, version int64) ([]*types.KeyValue, error)
- func (m *SimpleMVCC) DelMVCC(hash []byte, version int64, strict bool) ([]*types.KeyValue, error)
- func (m *SimpleMVCC) DelVersionKV(hash []byte, version int64) ([]*types.KeyValue, error)
- func (m *SimpleMVCC) GetDelKV(key []byte, version int64) (*types.KeyValue, error)
- func (m *SimpleMVCC) GetDelKVList(version int64) ([]*types.KeyValue, error)
- func (m *SimpleMVCC) GetMaxVersion() (int64, error)
- func (m *SimpleMVCC) GetSaveKV(key []byte, value []byte, version int64) (*types.KeyValue, error)
- func (m *SimpleMVCC) GetV(key []byte, version int64) ([]byte, error)
- func (m *SimpleMVCC) GetVersion(hash []byte) (int64, error)
- func (m *SimpleMVCC) GetVersionHash(version int64) ([]byte, error)
- func (m *SimpleMVCC) SetV(key []byte, value []byte, version int64) error
- func (m *SimpleMVCC) SetVersionKV(hash []byte, version int64) ([]*types.KeyValue, error)
- type SsdbBench
- type SsdbNode
- type TxKV
- type Value
Constants ¶
const ( ENDN = '\n' ENDR = '\r' OK = "ok" NotFound = "not_found" ReadTimeOut = 3 WriteTimeOut = 3 ReadBufSize = 8 * 1024 IteratorPageSize = 10240 PooledSize = 3 )
const
const ( // direction 位模式指定direction 参数 // 000, <- 位从低位开始数 // 0位:direction ListDESC ListASC // 1位:next key, 目前是一个特殊用途, 因为其他的都是返回value, 这个模式同时返回key, value。 不和其他位组合 // 2位: 为1返回时,返回 key+value,默认只返回value. 返回的是 types.Encode(types.KeyValue) // 3位: 为1返回时,返回 key, 默认只返回value. 和 2位不可以同时设置为1 ListDESC = int32(0) // 0 ListASC = int32(1) // 1 ListSeek = int32(2) // 10 ListWithKey = int32(4) // 01xx ListKeyOnly = int32(8) // 10xx )
const
Variables ¶
var (
ErrIterReleased = errors.New("ErrIterReleased")
)
合并错误列表
var ErrNotFoundInDb = types.ErrNotFound
ErrNotFoundInDb error
var (
//FormatString 格式化字符串
FormatString = "%v\nthe trace error is\n%s"
)
var HashKeyLen = 24
HashKeyLen hash长度
Functions ¶
Types ¶
type BaseDB ¶
type BaseDB struct {
// contains filtered or unexported fields
}
BaseDB 交易缓存
func (*BaseDB) CompactRange ¶
CompactRange call panic when CompactRange not rewrite
type Batch ¶
type Batch interface { Set(key, value []byte) Delete(key []byte) Write() error ValueSize() int // size of data in the batch ValueLen() int // amount of data in the batch Reset() // Reset resets the batch for reuse }
Batch batch
type DB ¶
type DB interface { KV IteratorDB SetSync([]byte, []byte) error Delete([]byte) error DeleteSync([]byte) error Close() NewBatch(sync bool) Batch BeginTx() (TxKV, error) CompactRange(start, limit []byte) error // For debugging Print() Stats() map[string]string SetCacheSize(size int) GetCache() *lru.ARCCache }
DB db
type GoBadgerDB ¶
type GoBadgerDB struct { BaseDB // contains filtered or unexported fields }
GoBadgerDB db
func NewGoBadgerDB ¶
func NewGoBadgerDB(name string, dir string, cache int) (*GoBadgerDB, error)
NewGoBadgerDB new
func (*GoBadgerDB) Iterator ¶
func (db *GoBadgerDB) Iterator(start, end []byte, reverse bool) Iterator
Iterator 迭代器
type GoBadgerDBBatch ¶
type GoBadgerDBBatch struct {
// contains filtered or unexported fields
}
GoBadgerDBBatch batch
func (*GoBadgerDBBatch) ValueSize ¶
func (mBatch *GoBadgerDBBatch) ValueSize() int
ValueSize batch大小
type GoLevelDB ¶
type GoLevelDB struct { BaseDB // contains filtered or unexported fields }
GoLevelDB db
func NewGoLevelDB ¶
NewGoLevelDB new
func (*GoLevelDB) CompactRange ¶
type GoMemDB ¶
type GoMemDB struct { BaseDB // contains filtered or unexported fields }
GoMemDB db
func NewGoMemDB ¶
NewGoMemDB new
type GoSSDB ¶
type GoSSDB struct { BaseDB // contains filtered or unexported fields }
GoSSDB db
type Iterator ¶
type Iterator interface { IteratorSeeker Valid() bool Key() []byte Value() []byte ValueCopy() []byte Error() error Prefix() []byte IsReverse() bool Close() }
Iterator 迭代器
func NewMergedIterator ¶
NewMergedIterator returns an iterator that merges its input. Walking the resultant iterator will return all key/value pairs of all input iterators in strictly increasing key order, as defined by cmp. The input's key ranges may overlap, but there are assumed to be no duplicate keys: if iters[i] contains a key k then iters[j] will not contain that key k. None of the iters may be nil.
If strict is true the any 'corruption errors' (i.e errors.IsCorrupted(err) == true) won't be ignored and will halt 'merged iterator', otherwise the iterator will continue to the next 'input iterator'.
type IteratorDB ¶
IteratorDB 迭代
func NewMergedIteratorDB ¶
func NewMergedIteratorDB(iters []IteratorDB) IteratorDB
NewMergedIteratorDB 合并两个迭代数据库
type IteratorSeeker ¶
type IteratorSeeker interface { Rewind() bool // 返回false, 表示系统中没有指定的key,Iterator会指向key附近 Seek(key []byte) bool Next() bool }
IteratorSeeker ...
type KV ¶
type KV interface { Get(key []byte) ([]byte, error) Set(key []byte, value []byte) (err error) Begin() Commit() error Rollback() }
KV kv
type KVDBList ¶
type KVDBList struct { DB // contains filtered or unexported fields }
KVDBList list
type ListHelper ¶
type ListHelper struct {
// contains filtered or unexported fields
}
ListHelper ...
func (*ListHelper) IteratorCallback ¶
func (db *ListHelper) IteratorCallback(start []byte, end []byte, count int32, direction int32, fn func(key, value []byte) bool)
IteratorCallback 迭代回滚
func (*ListHelper) IteratorScan ¶
func (db *ListHelper) IteratorScan(prefix []byte, key []byte, count int32, direction int32) [][]byte
IteratorScan 迭代
func (*ListHelper) IteratorScanFromFirst ¶
func (db *ListHelper) IteratorScanFromFirst(prefix []byte, count int32, direction int32) (values [][]byte)
IteratorScanFromFirst 从头迭代
func (*ListHelper) IteratorScanFromLast ¶
func (db *ListHelper) IteratorScanFromLast(prefix []byte, count int32, direction int32) (values [][]byte)
IteratorScanFromLast 从尾迭代
func (*ListHelper) List ¶
func (db *ListHelper) List(prefix, key []byte, count, direction int32) [][]byte
List 列表
func (*ListHelper) PrefixCount ¶
func (db *ListHelper) PrefixCount(prefix []byte) (count int64)
PrefixCount 前缀数量
func (*ListHelper) PrefixScan ¶
func (db *ListHelper) PrefixScan(prefix []byte) [][]byte
PrefixScan 前缀
type Lister ¶
type Lister interface { List(prefix, key []byte, count, direction int32) ([][]byte, error) PrefixCount(prefix []byte) int64 }
Lister 列表接口
type LocalDB ¶
type LocalDB struct {
// contains filtered or unexported fields
}
LocalDB local db for store key value in local
func (*LocalDB) PrefixCount ¶
PrefixCount 从数据库中查询指定前缀的key的数量
type MVCC ¶
type MVCC interface { MVCCKV SetVersion(hash []byte, version int64) error DelVersion(hash []byte) error GetVersion(hash []byte) (int64, error) GetV(key []byte, version int64) ([]byte, error) SetV(key []byte, value []byte, version int64) error DelV(key []byte, version int64) error AddMVCC(kvs []*types.KeyValue, hash []byte, prevHash []byte, version int64) ([]*types.KeyValue, error) DelMVCC(hash []byte, version int64, strict bool) ([]*types.KeyValue, error) GetMaxVersion() (int64, error) GetVersionHash(version int64) ([]byte, error) //回收: 某个版本之前的所有数据 //1. 保证有一个最新版本 //2. 这个操作回遍历所有的key所以比较慢 Trash(version int64) error }
MVCC mvcc interface
type MVCCHelper ¶
type MVCCHelper struct { *SimpleMVCC // contains filtered or unexported fields }
MVCCHelper impl MVCC interface
func (*MVCCHelper) DelV ¶
func (m *MVCCHelper) DelV(key []byte, version int64) error
DelV del key with version
func (*MVCCHelper) DelVersion ¶
func (m *MVCCHelper) DelVersion(hash []byte) error
DelVersion del stateHash version map
func (*MVCCHelper) SetVersion ¶
func (m *MVCCHelper) SetVersion(hash []byte, version int64) error
SetVersion set stateHash -> version map
type MVCCIter ¶
type MVCCIter struct {
*MVCCHelper
}
MVCCIter mvcc迭代器版本 支持db 原生迭代器接口 为了支持快速迭代,我这里采用了复制数据的做法
func (*MVCCIter) AddMVCC ¶
func (m *MVCCIter) AddMVCC(kvs []*types.KeyValue, hash []byte, prevHash []byte, version int64) ([]*types.KeyValue, error)
AddMVCC add
type MVCCKV ¶
type MVCCKV interface { GetSaveKV(key []byte, value []byte, version int64) (*types.KeyValue, error) GetDelKV(key []byte, version int64) (*types.KeyValue, error) SetVersionKV(hash []byte, version int64) ([]*types.KeyValue, error) DelVersionKV([]byte, int64) ([]*types.KeyValue, error) }
MVCCKV only return kv when change database
type PegasusBatch ¶
type PegasusBatch struct {
// contains filtered or unexported fields
}
PegasusBatch batch
func (*PegasusBatch) Write ¶
func (db *PegasusBatch) Write() error
注意本方法的实现逻辑,因为ssdb没有提供删除和更新同时进行的批量操作; 所以这里先执行更新操作(删除的KEY在这里会将VALUE设置为空); 然后再执行删除操作; 这样即使中间执行出错,也不会导致删除结果未写入的情况(值已经被置空);
type PegasusDB ¶
type PegasusDB struct { BaseDB // contains filtered or unexported fields }
PegasusDB db
func NewPegasusDB ¶
NewPegasusDB new
type SDBClient ¶
type SDBClient struct {
// contains filtered or unexported fields
}
SDBClient ...
func (*SDBClient) Keys ¶
Keys 列出处于区间 (key_start, key_end] 的 key 列表.("", ""] 表示整个区间.
keyStart int 返回的起始 key(不包含), 空字符串表示 -inf. keyEnd int 返回的结束 key(包含), 空字符串表示 +inf. limit int 最多返回这么多个元素. 返回 返回包含 key 的数组. 返回 err,可能的错误,操作成功返回 nil
func (*SDBClient) MultiDel ¶
MultiDel 批量删除一批 key 和其对应的值内容.
key,要删除的 key,可以为多个 返回 err,可能的错误,操作成功返回 nil
func (*SDBClient) MultiGet ¶
MultiGet 批量删除一批 key 和其对应的值内容.
key,要删除的 key,可以为多个 返回 err,可能的错误,操作成功返回 nil
type SimpleMVCC ¶
type SimpleMVCC struct {
// contains filtered or unexported fields
}
SimpleMVCC kvdb
func (*SimpleMVCC) AddMVCC ¶
func (m *SimpleMVCC) AddMVCC(kvs []*types.KeyValue, hash []byte, prevHash []byte, version int64) ([]*types.KeyValue, error)
AddMVCC add keys in a version 添加MVCC的规则: 必须提供现有hash 和 prev 的hash, 而且这两个版本号相差1
func (*SimpleMVCC) DelMVCC ¶
DelMVCC 删除某个版本 我们目前规定删除的方法: 1 -> 1-2-3-4-5,6 version 必须连续的增长 2 -> del 也必须从尾部开始删除
func (*SimpleMVCC) DelVersionKV ¶
DelVersionKV only export del version key value
func (*SimpleMVCC) GetDelKVList ¶
func (m *SimpleMVCC) GetDelKVList(version int64) ([]*types.KeyValue, error)
GetDelKVList 获取列表
func (*SimpleMVCC) GetMaxVersion ¶
func (m *SimpleMVCC) GetMaxVersion() (int64, error)
GetMaxVersion 获取最高版本
func (*SimpleMVCC) GetV ¶
func (m *SimpleMVCC) GetV(key []byte, version int64) ([]byte, error)
GetV get key with version
func (*SimpleMVCC) GetVersion ¶
func (m *SimpleMVCC) GetVersion(hash []byte) (int64, error)
GetVersion get stateHash and version map
func (*SimpleMVCC) GetVersionHash ¶
func (m *SimpleMVCC) GetVersionHash(version int64) ([]byte, error)
GetVersionHash 获取版本hash
func (*SimpleMVCC) SetV ¶
func (m *SimpleMVCC) SetV(key []byte, value []byte, version int64) error
SetV set key and value with version
func (*SimpleMVCC) SetVersionKV ¶
SetVersionKV only export SetVersionKV key and value