Documentation ¶
Overview ¶
Package db 数据库操作底层接口定义以及实现包括:leveldb、 memdb、mvcc、badgerdb、pegasus、ssdb
Index ¶
- Constants
- Variables
- func CopyBytes(b []byte) (copiedBytes []byte)
- func GetKey(key []byte, version int64) ([]byte, error)
- func GetKeyPerfix(key []byte) []byte
- func ToNum(bs []byte) int
- type Batch
- type DB
- type GoBadgerDB
- func (db *GoBadgerDB) BatchGet(keys [][]byte) (value [][]byte, err error)
- 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) BatchGet(keys [][]byte) (value [][]byte, err error)
- func (db *GoLevelDB) Close()
- 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) BatchGet(keys [][]byte) (value [][]byte, err error)
- func (db *GoMemDB) Close()
- func (db *GoMemDB) DB() map[string][]byte
- 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) BatchGet(keys [][]byte) (values [][]byte, err error)
- 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) (values [][]byte)
- func (db *ListHelper) IteratorScanFromFirst(prefix []byte, count int32) (values [][]byte)
- func (db *ListHelper) IteratorScanFromLast(prefix []byte, count int32) (values [][]byte)
- func (db *ListHelper) List(prefix, key []byte, count, direction int32) (values [][]byte)
- func (db *ListHelper) PrefixCount(prefix []byte) (count int64)
- func (db *ListHelper) PrefixScan(prefix []byte) (values [][]byte)
- type Lister
- type MVCC
- type MVCCHelper
- type MVCCIter
- type MVCCKV
- type PegasusBatch
- type PegasusDB
- func (db *PegasusDB) BatchGet(keys [][]byte) (values [][]byte, err error)
- 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 (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 TransactionDB
- 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 ( ListDESC = int32(0) ListASC = int32(1) ListSeek = int32(2) )
const
Variables ¶
var ErrNotFoundInDb = types.ErrNotFound
ErrNotFoundInDb error
var (
//FormatString 格式化字符串
FormatString = "%v\nthe trace error is\n%s"
)
var HashKeyLen = 24
HashKeyLen hash长度
Functions ¶
Types ¶
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 // For debugging Print() Stats() map[string]string SetCacheSize(size int) GetCache() *lru.ARCCache }
DB db
type GoBadgerDB ¶
type GoBadgerDB struct { TransactionDB // contains filtered or unexported fields }
GoBadgerDB db
func NewGoBadgerDB ¶
func NewGoBadgerDB(name string, dir string, cache int) (*GoBadgerDB, error)
NewGoBadgerDB new
func (*GoBadgerDB) BatchGet ¶
func (db *GoBadgerDB) BatchGet(keys [][]byte) (value [][]byte, err error)
BatchGet 批量获取
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 { TransactionDB // contains filtered or unexported fields }
GoLevelDB db
func NewGoLevelDB ¶
NewGoLevelDB new
type GoMemDB ¶
type GoMemDB struct { TransactionDB // contains filtered or unexported fields }
GoMemDB db
func NewGoMemDB ¶
NewGoMemDB new
type GoSSDB ¶
type GoSSDB struct { TransactionDB // 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 Close() }
Iterator 迭代器
type IteratorDB ¶
IteratorDB 迭代
type IteratorSeeker ¶
IteratorSeeker ...
type KV ¶
type KV interface { Get(key []byte) ([]byte, error) BatchGet(keys [][]byte) (values [][]byte, err error) Set(key []byte, value []byte) (err error) Begin() Rollback() Commit() }
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) (values [][]byte)
IteratorScan 迭代
func (*ListHelper) IteratorScanFromFirst ¶
func (db *ListHelper) IteratorScanFromFirst(prefix []byte, count int32) (values [][]byte)
IteratorScanFromFirst 从头迭代
func (*ListHelper) IteratorScanFromLast ¶
func (db *ListHelper) IteratorScanFromLast(prefix []byte, count int32) (values [][]byte)
IteratorScanFromLast 从尾迭代
func (*ListHelper) List ¶
func (db *ListHelper) List(prefix, key []byte, count, direction int32) (values [][]byte)
List 列表
func (*ListHelper) PrefixCount ¶
func (db *ListHelper) PrefixCount(prefix []byte) (count int64)
PrefixCount 前缀数量
func (*ListHelper) PrefixScan ¶
func (db *ListHelper) PrefixScan(prefix []byte) (values [][]byte)
PrefixScan 前缀
type Lister ¶
type Lister interface { List(prefix, key []byte, count, direction int32) ([][]byte, error) PrefixCount(prefix []byte) int64 }
Lister 列表接口
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 { TransactionDB // 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
type TransactionDB ¶
type TransactionDB struct {
// contains filtered or unexported fields
}
TransactionDB 交易缓存
func (*TransactionDB) SetCacheSize ¶
func (db *TransactionDB) SetCacheSize(size int)
SetCacheSize 设置缓存大小