db

package
v1.68.1 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2023 License: BSD-3-Clause Imports: 25 Imported by: 331

README

数据库配置选项说明

目前支持 leveldbbadger(测试阶段) 两种可选数据库做为KV数据存储,默认为leveldb
参考性能测试:https://github.com/suyanlong/dbcompare 总结出:

  • 优点:
    • badger在机械硬盘环境下,读写速度和leveldb相差不大,但在SSD固态硬盘环境下,读写速度都快于leveldb
    • badger的KV分离存储,所以相同数据情况下LSM会更小,可以全部加载到RAM,提高读写速度
  • 缺点:
    • badger的value是存储在valuelog中的,默认超过1KB的value才会压缩存储,会导致valuelog有很多冗余信息(可通过DB.RunValueLogGC()进行回收),占用磁盘空间会比leveldb多

leveldb

选用 leveldb 做为KV数据存储
修改chain33.toml文件中,[blockchain]、[store]、[wallet] 标签中driver的值为leveldb

{
    "driver": "leveldb"
}

badger

选用 badger 做为KV数据存储
修改chain33.toml文件中,[blockchain]、[store]、[wallet] 标签中driver的值为gobadgerdb

{
    "driver": "gobadgerdb"
}
  • 已知bugs:
    • 在windows环境下,系统重启后会因LOCK文件已存在,不能启动,需要手动删除 通过修改 vendor/github.com/dgraph-io/badger/dir_windows.go 72行暂时解决
    • 在0xff的情况下,边界测试有问题,详见db_test.go

实现自定义数据库接口说明

type DB interface {
	Get([]byte) []byte)		// 读
	Set([]byte, []byte)		// 写
	SetSync([]byte, []byte)		// 同步写
	Delete([]byte)			// 同步删除
	DeleteSync([]byte)		// 同步删除
	Close()				// 关闭数据库
	NewBatch(sync bool) Batch 	// 批量操作
	//迭代prefix 范围的所有key value, 支持正反顺序迭代
	Iterator(prefix []byte, key []byte, count int32, reserver bool) Iterator
	// For debugging
	Print()				// 调试打印
	Stats() map[string]string	// 数据库状态
}

type Batch interface {
	Set(key, value []byte)	// 写
	Delete(key []byte)	// 删除
	Write()			// 事务提交
}

Documentation

Overview

Package db 数据库操作底层接口定义以及实现包括:leveldb、 memdb、mvcc、badgerdb、pegasus、ssdb

Index

Constants

View Source
const (
	ENDN = '\n'
	ENDR = '\r'

	OK       = "ok"
	NotFound = "not_found"

	ReadTimeOut  = 3
	WriteTimeOut = 3

	ReadBufSize = 8 * 1024

	IteratorPageSize = 10240

	PooledSize = 3
)

const

View Source
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

View Source
var (
	ErrIterReleased = errors.New("ErrIterReleased")
)

合并错误列表

View Source
var ErrNotFoundInDb = types.ErrNotFound

ErrNotFoundInDb error

View Source
var (
	//FormatString 格式化字符串
	FormatString = "%v\nthe trace error is\n%s"
)
View Source
var HashKeyLen = 24

HashKeyLen hash长度

Functions

func GetKey

func GetKey(key []byte, version int64) ([]byte, error)

GetKey 获取键

func GetKeyPerfix

func GetKeyPerfix(key []byte) []byte

GetKeyPerfix 获取key前缀

func MustWrite

func MustWrite(batch Batch)

MustWrite must write correct

func ToNum

func ToNum(bs []byte) int

ToNum []byte -> int

Types

type BaseDB

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

BaseDB 交易缓存

func (*BaseDB) Begin

func (db *BaseDB) Begin()

Begin call panic when Begin not rewrite

func (*BaseDB) BeginTx

func (db *BaseDB) BeginTx() (TxKV, error)

BeginTx call panic when BeginTx not rewrite

func (*BaseDB) Commit

func (db *BaseDB) Commit() error

Commit call panic when Commit not rewrite

func (*BaseDB) CompactRange

func (db *BaseDB) CompactRange(start, limit []byte) error

CompactRange call panic when CompactRange not rewrite

func (*BaseDB) GetCache

func (db *BaseDB) GetCache() *lru.ARCCache

GetCache 获取缓存

func (*BaseDB) Rollback

func (db *BaseDB) Rollback()

Rollback call panic when Rollback not rewrite

func (*BaseDB) SetCacheSize

func (db *BaseDB) SetCacheSize(size int)

SetCacheSize 设置缓存大小

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
	UpdateWriteSync(sync bool) // update write sync
}

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

func NewDB

func NewDB(name string, backend string, dir string, cache int32) DB

NewDB new

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) Close

func (db *GoBadgerDB) Close()

Close 关闭

func (*GoBadgerDB) DB

func (db *GoBadgerDB) DB() *badger.DB

DB db

func (*GoBadgerDB) Delete

func (db *GoBadgerDB) Delete(key []byte) error

Delete 删除

func (*GoBadgerDB) DeleteSync

func (db *GoBadgerDB) DeleteSync(key []byte) error

DeleteSync 删除同步

func (*GoBadgerDB) Get

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

Get get

func (*GoBadgerDB) Iterator

func (db *GoBadgerDB) Iterator(start, end []byte, reverse bool) Iterator

Iterator 迭代器

func (*GoBadgerDB) NewBatch

func (db *GoBadgerDB) NewBatch(sync bool) Batch

NewBatch new

func (*GoBadgerDB) Print

func (db *GoBadgerDB) Print()

Print 打印

func (*GoBadgerDB) Set

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

Set set

func (*GoBadgerDB) SetSync

func (db *GoBadgerDB) SetSync(key []byte, value []byte) error

SetSync 同步

func (*GoBadgerDB) Stats

func (db *GoBadgerDB) Stats() map[string]string

Stats ...

type GoBadgerDBBatch

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

GoBadgerDBBatch batch

func (*GoBadgerDBBatch) Delete

func (mBatch *GoBadgerDBBatch) Delete(key []byte)

Delete 设置

func (*GoBadgerDBBatch) Reset

func (mBatch *GoBadgerDBBatch) Reset()

Reset 重置

func (*GoBadgerDBBatch) Set

func (mBatch *GoBadgerDBBatch) Set(key, value []byte)

Set set

func (*GoBadgerDBBatch) UpdateWriteSync added in v1.65.0

func (mBatch *GoBadgerDBBatch) UpdateWriteSync(sync bool)

UpdateWriteSync ...

func (*GoBadgerDBBatch) ValueLen

func (mBatch *GoBadgerDBBatch) ValueLen() int

ValueLen batch数量

func (*GoBadgerDBBatch) ValueSize

func (mBatch *GoBadgerDBBatch) ValueSize() int

ValueSize batch大小

func (*GoBadgerDBBatch) Write

func (mBatch *GoBadgerDBBatch) Write() error

Write 写入

type GoLevelDB

type GoLevelDB struct {
	BaseDB
	// contains filtered or unexported fields
}

GoLevelDB db

func NewGoLevelDB

func NewGoLevelDB(name string, dir string, cache int) (*GoLevelDB, error)

NewGoLevelDB new

func (*GoLevelDB) BeginTx

func (db *GoLevelDB) BeginTx() (TxKV, error)

BeginTx call panic when BeginTx not rewrite

func (*GoLevelDB) Close

func (db *GoLevelDB) Close()

Close 关闭

func (*GoLevelDB) CompactRange

func (db *GoLevelDB) CompactRange(start, limit []byte) error

CompactRange ...

func (*GoLevelDB) DB

func (db *GoLevelDB) DB() *leveldb.DB

DB db

func (*GoLevelDB) Delete

func (db *GoLevelDB) Delete(key []byte) error

Delete 删除

func (*GoLevelDB) DeleteSync

func (db *GoLevelDB) DeleteSync(key []byte) error

DeleteSync 删除同步

func (*GoLevelDB) Get

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

Get get

func (*GoLevelDB) Iterator

func (db *GoLevelDB) Iterator(start []byte, end []byte, reverse bool) Iterator

Iterator 迭代器

func (*GoLevelDB) NewBatch

func (db *GoLevelDB) NewBatch(sync bool) Batch

NewBatch new

func (*GoLevelDB) Print

func (db *GoLevelDB) Print()

Print 打印

func (*GoLevelDB) Set

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

Set set

func (*GoLevelDB) SetSync

func (db *GoLevelDB) SetSync(key []byte, value []byte) error

SetSync 同步

func (*GoLevelDB) Stats

func (db *GoLevelDB) Stats() map[string]string

Stats ...

type GoMemDB

type GoMemDB struct {
	BaseDB
	// contains filtered or unexported fields
}

GoMemDB db

func NewGoMemDB

func NewGoMemDB(name string, dir string, cache int) (*GoMemDB, error)

NewGoMemDB new

func (*GoMemDB) Close

func (db *GoMemDB) Close()

Close 关闭

func (*GoMemDB) DB

func (db *GoMemDB) DB() *memdb.DB

DB db

func (*GoMemDB) Delete

func (db *GoMemDB) Delete(key []byte) error

Delete 删除

func (*GoMemDB) DeleteSync

func (db *GoMemDB) DeleteSync(key []byte) error

DeleteSync 删除同步

func (*GoMemDB) Get

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

Get get

func (*GoMemDB) Iterator

func (db *GoMemDB) Iterator(start []byte, end []byte, reverse bool) Iterator

Iterator 迭代器

func (*GoMemDB) NewBatch

func (db *GoMemDB) NewBatch(sync bool) Batch

NewBatch new

func (*GoMemDB) Print

func (db *GoMemDB) Print()

Print 打印

func (*GoMemDB) Set

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

Set set

func (*GoMemDB) SetSync

func (db *GoMemDB) SetSync(key []byte, value []byte) error

SetSync 设置同步

func (*GoMemDB) Stats

func (db *GoMemDB) Stats() map[string]string

Stats ...

type GoSSDB

type GoSSDB struct {
	BaseDB
	// contains filtered or unexported fields
}

GoSSDB db

func NewGoSSDB

func NewGoSSDB(name string, dir string, cache int) (*GoSSDB, error)

NewGoSSDB new

func (*GoSSDB) Close

func (db *GoSSDB) Close()

Close 关闭

func (*GoSSDB) Delete

func (db *GoSSDB) Delete(key []byte) error

Delete 删除

func (*GoSSDB) DeleteSync

func (db *GoSSDB) DeleteSync(key []byte) error

DeleteSync 删除同步

func (*GoSSDB) Get

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

Get get

func (*GoSSDB) Iterator

func (db *GoSSDB) Iterator(itbeg []byte, itend []byte, reverse bool) Iterator

Iterator 迭代器

func (*GoSSDB) NewBatch

func (db *GoSSDB) NewBatch(sync bool) Batch

NewBatch new

func (*GoSSDB) Print

func (db *GoSSDB) Print()

Print 打印

func (*GoSSDB) Set

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

Set 设置

func (*GoSSDB) SetSync

func (db *GoSSDB) SetSync(key []byte, value []byte) error

SetSync 同步

func (*GoSSDB) Stats

func (db *GoSSDB) Stats() map[string]string

Stats ...

type Iterator

type Iterator interface {
	IteratorSeeker
	Valid() bool
	Key() []byte
	Value() []byte
	ValueCopy() []byte
	Error() error
	Prefix() []byte
	IsReverse() bool
	Close()
}

Iterator 迭代器

func NewMergedIterator

func NewMergedIterator(iters []Iterator) Iterator

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

type IteratorDB interface {
	Iterator(start []byte, end []byte, reserver bool) Iterator
}

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 KVDB

type KVDB interface {
	KV
	Lister
}

KVDB kvdb

func NewKVDB

func NewKVDB(db DB) KVDB

NewKVDB new

func NewLocalDB

func NewLocalDB(maindb DB, readOnly bool) KVDB

NewLocalDB new local db

type KVDBList

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

KVDBList list

func (*KVDBList) List

func (l *KVDBList) List(prefix, key []byte, count, direction int32) ([][]byte, error)

List 列表

func (*KVDBList) PrefixCount

func (l *KVDBList) PrefixCount(prefix []byte) int64

PrefixCount 前缀长度

type ListHelper

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

ListHelper ...

func NewListHelper

func NewListHelper(db IteratorDB) *ListHelper

NewListHelper new

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) Begin

func (l *LocalDB) Begin()

Begin 开启内存事务处理

func (*LocalDB) Commit

func (l *LocalDB) Commit() error

Commit canche tx

func (*LocalDB) Get

func (l *LocalDB) Get(key []byte) ([]byte, error)

Get get value from local db

func (*LocalDB) List

func (l *LocalDB) List(prefix, key []byte, count, direction int32) ([][]byte, error)

List 从数据库中查询数据列表,set 中的cache 更新不会影响这个list

func (*LocalDB) PrefixCount

func (l *LocalDB) PrefixCount(prefix []byte) (count int64)

PrefixCount 从数据库中查询指定前缀的key的数量

func (*LocalDB) Rollback

func (l *LocalDB) Rollback()

Rollback reset tx

func (*LocalDB) Set

func (l *LocalDB) Set(key []byte, value []byte) error

Set set key value to local db

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 NewMVCC

func NewMVCC(db DB) *MVCCHelper

NewMVCC create MVCC object use db DB

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) PrintAll

func (m *MVCCHelper) PrintAll()

PrintAll 打印全部

func (*MVCCHelper) SetVersion

func (m *MVCCHelper) SetVersion(hash []byte, version int64) error

SetVersion set stateHash -> version map

func (*MVCCHelper) Trash

func (m *MVCCHelper) Trash(version int64) error

Trash del some old kv

type MVCCIter

type MVCCIter struct {
	*MVCCHelper
}

MVCCIter mvcc迭代器版本 支持db 原生迭代器接口 为了支持快速迭代,我这里采用了复制数据的做法

func NewMVCCIter

func NewMVCCIter(db DB) *MVCCIter

NewMVCCIter new

func (*MVCCIter) AddMVCC

func (m *MVCCIter) AddMVCC(kvs []*types.KeyValue, hash []byte, prevHash []byte, version int64) ([]*types.KeyValue, error)

AddMVCC add

func (*MVCCIter) DelMVCC

func (m *MVCCIter) DelMVCC(hash []byte, version int64, strict bool) ([]*types.KeyValue, error)

DelMVCC del

func (*MVCCIter) Iterator

func (m *MVCCIter) Iterator(start, end []byte, reserver bool) Iterator

Iterator 迭代

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) Delete

func (db *PegasusBatch) Delete(key []byte)

Delete 删除

func (*PegasusBatch) Reset

func (db *PegasusBatch) Reset()

Reset 重置

func (*PegasusBatch) Set

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

Set set

func (*PegasusBatch) UpdateWriteSync added in v1.65.0

func (db *PegasusBatch) UpdateWriteSync(sync bool)

UpdateWriteSync ...

func (*PegasusBatch) ValueLen

func (db *PegasusBatch) ValueLen() int

ValueLen batch数量

func (*PegasusBatch) ValueSize

func (db *PegasusBatch) ValueSize() int

ValueSize value批长度

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

func NewPegasusDB(name string, dir string, cache int) (*PegasusDB, error)

NewPegasusDB new

func (*PegasusDB) Close

func (db *PegasusDB) Close()

Close 同步

func (*PegasusDB) Delete

func (db *PegasusDB) Delete(key []byte) error

Delete 删除

func (*PegasusDB) DeleteSync

func (db *PegasusDB) DeleteSync(key []byte) error

DeleteSync 删除同步

func (*PegasusDB) Get

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

Get get

func (*PegasusDB) Iterator

func (db *PegasusDB) Iterator(begin []byte, end []byte, reverse bool) Iterator

Iterator 迭代器

func (*PegasusDB) NewBatch

func (db *PegasusDB) NewBatch(sync bool) Batch

NewBatch new

func (*PegasusDB) Print

func (db *PegasusDB) Print()

Print 打印

func (*PegasusDB) Set

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

Set set

func (*PegasusDB) SetSync

func (db *PegasusDB) SetSync(key []byte, value []byte) error

SetSync 设置同步

func (*PegasusDB) Stats

func (db *PegasusDB) Stats() map[string]string

Stats ...

type PegasusIt

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

PegasusIt ...

func (*PegasusIt) Close

func (dbit *PegasusIt) Close()

Close 关闭

func (*PegasusIt) Error

func (dbit *PegasusIt) Error() error

func (*PegasusIt) IsReverse

func (it *PegasusIt) IsReverse() bool

func (*PegasusIt) Key

func (dbit *PegasusIt) Key() []byte

Key key

func (*PegasusIt) Next

func (dbit *PegasusIt) Next() bool

Next next

func (*PegasusIt) Prefix

func (it *PegasusIt) Prefix() []byte

Prefix 前缀

func (*PegasusIt) Rewind

func (dbit *PegasusIt) Rewind() bool

Rewind 从头开始

func (*PegasusIt) Seek

func (dbit *PegasusIt) Seek(key []byte) bool

Seek 查找

func (*PegasusIt) Valid

func (dbit *PegasusIt) Valid() bool

Valid 合法性

func (*PegasusIt) Value

func (dbit *PegasusIt) Value() []byte

Value value

func (*PegasusIt) ValueCopy

func (dbit *PegasusIt) ValueCopy() []byte

ValueCopy 复制

type RoundInt

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

RoundInt ...

type SDBClient

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

SDBClient ...

func Connect

func Connect(ip string, port int) (*SDBClient, error)

Connect 连接

func (*SDBClient) Close

func (c *SDBClient) Close() error

Close The Client Connection

func (*SDBClient) Del

func (c *SDBClient) Del(key string) error

Del 删除指定 key

key 要删除的 key
返回 err,执行的错误,操作成功返回 nil

func (*SDBClient) Do

func (c *SDBClient) Do(args ...interface{}) ([]string, error)

Do do

func (*SDBClient) Get

func (c *SDBClient) Get(key string) (*Value, error)

Get 获取指定 key 的值内容

key 键值
返回 一个 Value,可以方便的向其它类型转换
返回 一个可能的错误,操作成功返回 nil

func (*SDBClient) Keys

func (c *SDBClient) Keys(keyStart, keyEnd string, limit int64) ([]string, error)

Keys 列出处于区间 (key_start, key_end] 的 key 列表.("", ""] 表示整个区间.

keyStart int 返回的起始 key(不包含), 空字符串表示 -inf.
keyEnd int 返回的结束 key(包含), 空字符串表示 +inf.
limit int 最多返回这么多个元素.
返回 返回包含 key 的数组.
返回 err,可能的错误,操作成功返回 nil

func (*SDBClient) MultiDel

func (c *SDBClient) MultiDel(key ...string) (err error)

MultiDel 批量删除一批 key 和其对应的值内容.

key,要删除的 key,可以为多个
返回 err,可能的错误,操作成功返回 nil

func (*SDBClient) MultiGet

func (c *SDBClient) MultiGet(key ...string) (vals []*Value, err error)

MultiGet 批量删除一批 key 和其对应的值内容.

key,要删除的 key,可以为多个
返回 err,可能的错误,操作成功返回 nil

func (*SDBClient) MultiSet

func (c *SDBClient) MultiSet(kvs map[string][]byte) (err error)

MultiSet 批量设置一批 key-value.

包含 key-value 的字典
返回 err,可能的错误,操作成功返回 nil

func (*SDBClient) Rkeys

func (c *SDBClient) Rkeys(keyStart, keyEnd string, limit int64) ([]string, error)

Rkeys 列出处于区间 (key_start, key_end] 的 key 列表.("", ""] 表示整个区间.反向选择

keyStart int 返回的起始 key(不包含), 空字符串表示 -inf.
keyEnd int 返回的结束 key(包含), 空字符串表示 +inf.
limit int 最多返回这么多个元素.
返回 返回包含 key 的数组.
返回 err,可能的错误,操作成功返回 nil

func (*SDBClient) Set

func (c *SDBClient) Set(key string, val []byte) (err error)

Set 设置指定 key 的值内容

key 键值
val 存贮的 value 值,val只支持基本的类型,如果要支持复杂的类型,需要开启连接池的 Encoding 选项
ttl 可选,设置的过期时间,单位为秒
返回 err,可能的错误,操作成功返回 nil

type SDBPool

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

SDBPool SDB池

func NewSDBPool

func NewSDBPool(nodes []*SsdbNode) (pool *SDBPool, err error)

NewSDBPool new

type SimpleMVCC

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

SimpleMVCC kvdb

func NewSimpleMVCC

func NewSimpleMVCC(db KVDB) *SimpleMVCC

NewSimpleMVCC new

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

func (m *SimpleMVCC) DelMVCC(hash []byte, version int64, strict bool) ([]*types.KeyValue, error)

DelMVCC 删除某个版本 我们目前规定删除的方法: 1 -> 1-2-3-4-5,6 version 必须连续的增长 2 -> del 也必须从尾部开始删除

func (*SimpleMVCC) DelVersionKV

func (m *SimpleMVCC) DelVersionKV(hash []byte, version int64) ([]*types.KeyValue, error)

DelVersionKV only export del version key value

func (*SimpleMVCC) GetDelKV

func (m *SimpleMVCC) GetDelKV(key []byte, version int64) (*types.KeyValue, error)

GetDelKV only export del key and value with version

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) GetSaveKV

func (m *SimpleMVCC) GetSaveKV(key []byte, value []byte, version int64) (*types.KeyValue, error)

GetSaveKV only export set key and value with version

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

func (m *SimpleMVCC) SetVersionKV(hash []byte, version int64) ([]*types.KeyValue, error)

SetVersionKV only export SetVersionKV key and value

type SsdbBench

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

SsdbBench ...

func (*SsdbBench) String

func (bench *SsdbBench) String() string

type SsdbNode

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

SsdbNode 节点

type TxKV

type TxKV interface {
	KV
	IteratorDB
}

TxKV transaction Key Value

type Value

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

Value 扩展值,原始类型为 string

func (*Value) Bytes

func (v *Value) Bytes() []byte

Bytes 返回 []byte 类型的值

func (*Value) String

func (v *Value) String() string

返回 string 的值

Directories

Path Synopsis
Package table 实现一个基于kv的关系型数据库的表格功能
Package table 实现一个基于kv的关系型数据库的表格功能

Jump to

Keyboard shortcuts

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