utilsleveldb

package
v0.0.0-...-ff2eb03 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 10 Imported by: 5

Documentation

Index

Constants

View Source
const (
	MaxKeySize           = 65535              //max key size
	MaxValueSize     int = 1024 * 1024 * 1024 //max value size
	DBRemoveNum          = 100                //分批次删除map集合中的数据,每批次默认数量
	DBRemoveInterval     = time.Second        //分批次删除map集合中的数据,每批次默认间隔时间

	ERROR_CODE_key_size   = 1101 //数据库key长度错误
	ERROR_CODE_value_size = 1102 //数据库value长度错误
)

Variables

View Source
var (
	DataType_Index_bs            []byte //
	DataType_Data_Map_start_bs   []byte //
	DataType_Data_Map_bs         []byte //
	DataType_Data_Map_end_bs     []byte //
	DataType_Data_List_start_bs  []byte //
	DataType_Data_List_bs        []byte //
	DataType_Data_List_end_bs    []byte //
	DataType_Map_In_Map_start_bs []byte //
	DataType_Map_In_Map_bs       []byte //
	DataType_Map_In_Map_end_bs   []byte //
)

Functions

func Init

func Init()

func LeveldbBuildKey

func LeveldbBuildKey(key []byte) ([]byte, utils.ERROR)

leveldb中,所有的key都带一个key长度前缀,用2字节保存。 2字节容量是65535,因此key长度不能大于65535。 避免不同长度的key前缀查询的时候混合。

func LeveldbParseKey

func LeveldbParseKey(key []byte) (uint16, []byte, utils.ERROR)

leveldb中,所有的key都带一个key长度前缀,用2字节保存。 2字节容量是65535,因此key长度不能大于65535。 避免不同长度的key前缀查询的时候混合。 @return uint16 长度 @return []byte 解析出来的key

func RegisterDbKey

func RegisterDbKey(id []byte) bool

注册一个数据库ID,并判断是否有重复 @return bool 是否成功

Types

type DBItem

type DBItem struct {
	Index []byte
	Key   LeveldbKey
	Value []byte
}

集合中的项目

type IndexLock

type IndexLock struct {
	Index *big.Int
	// contains filtered or unexported fields
}

func NewIndexLock

func NewIndexLock(index *big.Int) *IndexLock

type KVPair

type KVPair struct {
	IsAddOrDel bool   //操作类型,true=保存或修改;false=删除;
	Key        []byte //
	Value      []byte //
}

一次数据库操作

func NewKVPair

func NewKVPair(isAddOrDel bool, key, value []byte) (*KVPair, utils.ERROR)

创建一次数据库操作 @isAddOrDel bool true=保存或修改;false=删除;

type LevelDB

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

func CreateLevelDB

func CreateLevelDB(path string) (*LevelDB, error)

func (*LevelDB) Close

func (this *LevelDB) Close()

关闭leveldb连接

func (*LevelDB) Commit

func (this *LevelDB) Commit() error

提交事务

func (*LevelDB) Discard

func (this *LevelDB) Discard()

回滚事务

func (*LevelDB) Find

func (this *LevelDB) Find(key LeveldbKey) (*DBItem, error)

查找

func (*LevelDB) FindListAll

func (this *LevelDB) FindListAll(dbkey LeveldbKey) ([]DBItem, error)

查询List集合中的所有项目

func (*LevelDB) FindListByIndex

func (this *LevelDB) FindListByIndex(dbkey LeveldbKey, index []byte) (*DBItem, utils.ERROR)

修改List集合中的记录

func (*LevelDB) FindListRange

func (this *LevelDB) FindListRange(dbkey LeveldbKey, startIndex []byte, limit uint64, order bool) ([]DBItem, utils.ERROR)

查询List集合中一个范围的记录 不包含startIndex @order bool 查询顺序。true=从前向后查询;false=从后向前查询;

func (*LevelDB) FindListTotal

func (this *LevelDB) FindListTotal(dbkey LeveldbKey) (uint64, []byte, []byte, utils.ERROR)

查询List集合中记录总条数 @return uint64 记录总条数 @return []byte 开始index @return []byte 结束index @return error 记录总条数

func (*LevelDB) FindMap

func (this *LevelDB) FindMap(dbkey, key LeveldbKey) ([]byte, error)

查询Map

func (*LevelDB) FindMapAllToList

func (this *LevelDB) FindMapAllToList(dbkey LeveldbKey) ([]DBItem, error)

查询Map中所有的key和value值

func (*LevelDB) FindMapByKeys

func (this *LevelDB) FindMapByKeys(dbkey LeveldbKey, keys ...LeveldbKey) ([]DBItem, error)

查询Map中所有的key和value值

func (*LevelDB) FindMapInListAll

func (this *LevelDB) FindMapInListAll(dbkey LeveldbKey) ([]DBItem, utils.ERROR)

查询MapInList集合中一个范围的记录 不包含startIndex

func (*LevelDB) FindMapInListAllByKey

func (this *LevelDB) FindMapInListAllByKey(dbkey, key LeveldbKey) ([]DBItem, error)

查询MapInList集合中所有key

func (*LevelDB) FindMapInListByIndex

func (this *LevelDB) FindMapInListByIndex(dbkey, key LeveldbKey, index []byte) (*DBItem, utils.ERROR)

查找MapInList集合中的一个key的value值

func (*LevelDB) FindMapInListRangeByKeyIn

func (this *LevelDB) FindMapInListRangeByKeyIn(dbkey, key LeveldbKey, startIndex []byte, limit uint64, order bool) ([]DBItem, utils.ERROR)

查询MapInList集合中一个范围的记录 不包含startIndex @order bool 查询顺序。true=从前向后查询;false=从后向前查询;

func (*LevelDB) FindMapInListRangeByKeyOut

func (this *LevelDB) FindMapInListRangeByKeyOut(dbkey LeveldbKey, startIndex []byte, limit uint64, order bool) ([]DBItem, utils.ERROR)

查询MapInList集合中一个范围的记录 不包含startIndex @order bool 查询顺序。true=从前向后查询;false=从后向前查询;

func (*LevelDB) FindMapInListTotal

func (this *LevelDB) FindMapInListTotal(dbkey, key LeveldbKey) (uint64, []byte, []byte, utils.ERROR)

查询MapInList集合中一个key下的列表记录总数 @return uint64 记录总条数 @return []byte 开始index @return []byte 结束index @return error 记录总条数

func (*LevelDB) FindMapInMapByKeyIn

func (this *LevelDB) FindMapInMapByKeyIn(dbkey, keyOut, keyIn LeveldbKey) (*[]byte, error)

查询 Map中有Map 集合中内层key对应的值 @dbkey LeveldbKey 数据库ID @keyOut LeveldbKey 外层map索引 @keyIn LeveldbKey 内层map索引 @return []byte 内层map索引对应的值

func (*LevelDB) FindMapInMapByKeyOut

func (this *LevelDB) FindMapInMapByKeyOut(dbkey, keyOut LeveldbKey) ([]DBItem, utils.ERROR)

查询 Map中有Map 集合中内层key对应的值 @dbkey LeveldbKey 数据库ID @keyOut LeveldbKey 外层map索引 @return []DBItem 内层map索引对应的值

func (*LevelDB) FindMore

func (this *LevelDB) FindMore(keys ...LeveldbKey) ([]DBItem, error)

查找多条记录

func (*LevelDB) GetDB

func (this *LevelDB) GetDB() *leveldb.DB

获取数据库连接

func (*LevelDB) GetPath

func (this *LevelDB) GetPath() string

获取数据库所在磁盘路径

func (*LevelDB) Has

func (this *LevelDB) Has(key LeveldbKey) (bool, error)

检查key是否存在 @return bool true:存在;false:不存在;

func (*LevelDB) InitDB

func (this *LevelDB) InitDB() (err error)

链接leveldb

func (*LevelDB) OpenTransaction

func (this *LevelDB) OpenTransaction() error

开启事务

func (*LevelDB) PrintAll

func (this *LevelDB) PrintAll() ([][]byte, error)

打印所有key

func (*LevelDB) Remove

func (this *LevelDB) Remove(key LeveldbKey) error

删除

func (*LevelDB) RemoveListAll

func (this *LevelDB) RemoveListAll(dbkey LeveldbKey, batch *leveldb.Batch) error

删除List集合中的所有记录 @dbkey []byte 数据库ID

func (*LevelDB) RemoveListByIndex

func (this *LevelDB) RemoveListByIndex(dbkey LeveldbKey, index []byte, batch *leveldb.Batch) utils.ERROR

删除List集合中1条数据 @isTransaction bool 是否是事务操作

func (*LevelDB) RemoveListInterval

func (this *LevelDB) RemoveListInterval(dbkey LeveldbKey, num uint64, interval time.Duration) error

间隔删除List集合中的所有记录 当删除大量数据时,会花很长时间,长期占用数据库,让其他业务无法使用数据库。 可以分批次删除,并且设置每批次间隔时间 @dbkey []byte 数据库ID @num uint64 一次删除条数 @interval time.Duration 删除间隔时间

func (*LevelDB) RemoveListMore

func (this *LevelDB) RemoveListMore(isTransaction bool, dbkey LeveldbKey, index ...[]byte) utils.ERROR

删除List集合中多条数据 @isTransaction bool 是否是事务操作

func (*LevelDB) RemoveMapByDbKey

func (this *LevelDB) RemoveMapByDbKey(dbkey LeveldbKey, num uint64, interval time.Duration) error

删除Map 当删除大量数据时,会花很长时间,长期占用数据库,让其他业务无法使用数据库。 可以分批次删除,并且设置每批次间隔时间 @num uint64 一次删除条数 @interval time.Duration 删除间隔时间

func (*LevelDB) RemoveMapByKey

func (this *LevelDB) RemoveMapByKey(dbkey, key LeveldbKey, batch *leveldb.Batch) error

删除Map中的一个key

func (*LevelDB) RemoveMapByKey_Transaction

func (this *LevelDB) RemoveMapByKey_Transaction(dbkey, key LeveldbKey) error

删除Map中的一个key 事务处理

func (*LevelDB) RemoveMapInListAll

func (this *LevelDB) RemoveMapInListAll(dbkey, key LeveldbKey, batch *leveldb.Batch) error

删除MapInList集合中的所有记录

func (*LevelDB) RemoveMapInListByIndex

func (this *LevelDB) RemoveMapInListByIndex(dbkey, key LeveldbKey, index []byte, batch *leveldb.Batch) utils.ERROR

删除MapInList集合中的一个key @isTransaction bool 是否是事务操作

func (*LevelDB) RemoveMapInListByKeyInterval

func (this *LevelDB) RemoveMapInListByKeyInterval(dbkey, key LeveldbKey, num uint64, interval time.Duration) error

间隔删除MapInList集合中的一个key 当删除大量数据时,会花很长时间,长期占用数据库,让其他业务无法使用数据库。 可以分批次删除,并且设置每批次间隔时间 @num uint64 一次删除条数 @interval time.Duration 删除间隔时间

func (*LevelDB) RemoveMapInListMore

func (this *LevelDB) RemoveMapInListMore(isTransaction bool, dbkey, key LeveldbKey, index ...[]byte) utils.ERROR

删除MapInList集合中的一个key @isTransaction bool 是否是事务操作

func (*LevelDB) RemoveMapInMapByKeyIn

func (this *LevelDB) RemoveMapInMapByKeyIn(dbkey, keyOut, keyIn LeveldbKey, batch *leveldb.Batch) error

查询 Map中有Map 集合中内层key对应的值 @dbkey LeveldbKey 数据库ID @keyOut LeveldbKey 外层map索引 @keyIn LeveldbKey 内层map索引 @return []byte 内层map索引对应的值

func (*LevelDB) RemoveMapInMapByKeyIn_Transaction

func (this *LevelDB) RemoveMapInMapByKeyIn_Transaction(dbkey, keyOut, keyIn LeveldbKey) error

查询 Map中有Map 集合中内层key对应的值 事务处理 @dbkey LeveldbKey 数据库ID @keyOut LeveldbKey 外层map索引 @keyIn LeveldbKey 内层map索引 @return []byte 内层map索引对应的值

func (*LevelDB) RemoveMapInMapByKeyOut

func (this *LevelDB) RemoveMapInMapByKeyOut(dbkey, keyOut LeveldbKey, batch *leveldb.Batch) error

删除 Map中有Map 集合中外层key对应的值 @dbkey LeveldbKey 数据库ID @keyOut LeveldbKey 外层map索引

func (*LevelDB) RemoveMapInMapByKeyOutInterval

func (this *LevelDB) RemoveMapInMapByKeyOutInterval(dbkey, keyOut LeveldbKey, num uint64, interval time.Duration) error

间隔删除 Map中有Map 集合中外层key对应的值 当删除大量数据时,会花很长时间,长期占用数据库,让其他业务无法使用数据库。 可以分批次删除,并且设置每批次间隔时间 @dbkey LeveldbKey 数据库ID @keyOut LeveldbKey 外层map索引 @num uint64 一次删除条数 @interval time.Duration 删除间隔时间

func (*LevelDB) RemoveMore

func (this *LevelDB) RemoveMore(kvps ...KVPair) error

删除多个操作

func (*LevelDB) RemoveMore_Transaction

func (this *LevelDB) RemoveMore_Transaction(kvps ...KVPair) error

删除多个操作 事务处理

func (*LevelDB) Remove_Transaction

func (this *LevelDB) Remove_Transaction(key LeveldbKey) error

删除 事务处理

func (*LevelDB) Save

func (this *LevelDB) Save(key LeveldbKey, bs *[]byte) utils.ERROR

保存

func (*LevelDB) SaveBatch

func (this *LevelDB) SaveBatch(batch *leveldb.Batch) error

保存

func (*LevelDB) SaveList

func (this *LevelDB) SaveList(dbkey LeveldbKey, value []byte, batch *leveldb.Batch) ([]byte, utils.ERROR)

保存1条记录到List集合中 @return []byte 返回本记录索引。big.Int类型的byte字节序

func (*LevelDB) SaveListMore

func (this *LevelDB) SaveListMore(dbkey LeveldbKey, value [][]byte, batch *leveldb.Batch) ([][]byte, utils.ERROR)

保存多条记录到List集合中 @isTransaction bool 是否是事务操作 @return []byte 返回本记录索引。big.Int类型的byte字节序

func (*LevelDB) SaveMap

func (this *LevelDB) SaveMap(dbkey, key LeveldbKey, value []byte, batch *leveldb.Batch) utils.ERROR

保存到Map

func (*LevelDB) SaveMapInList

func (this *LevelDB) SaveMapInList(dbkey, key LeveldbKey, value []byte, batch *leveldb.Batch) ([]byte, utils.ERROR)

保存到MapInList集合中 @isTransaction bool 是否是事务操作 @return []byte index索引字节 @return error 错误

func (*LevelDB) SaveMapInListMore

func (this *LevelDB) SaveMapInListMore(dbkey, key LeveldbKey, value [][]byte, batch *leveldb.Batch) ([][]byte, utils.ERROR)

保存多条记录到MapInList集合中 @isTransaction bool 是否是事务操作 @return []byte 索引字节 @return error 错误

func (*LevelDB) SaveMapInMap

func (this *LevelDB) SaveMapInMap(dbkey, keyOut, keyIn LeveldbKey, value []byte, batch *leveldb.Batch) utils.ERROR

保存到 Map中有Map 集合中 @dbkey LeveldbKey 数据库ID @keyOut LeveldbKey 外层map索引 @keyIn LeveldbKey 内层map索引 @value []byte 内层map索引对应的值

func (*LevelDB) SaveMapInMap_Transaction

func (this *LevelDB) SaveMapInMap_Transaction(dbkey, keyOut, keyIn LeveldbKey, value []byte) utils.ERROR

保存到 Map中有Map 集合中 事务处理 @dbkey LeveldbKey 数据库ID @keyOut LeveldbKey 外层map索引 @keyIn LeveldbKey 内层map索引 @value []byte 内层map索引对应的值

func (*LevelDB) SaveMap_Transaction

func (this *LevelDB) SaveMap_Transaction(dbkey, key LeveldbKey, value []byte) utils.ERROR

保存到Map 事务处理

func (*LevelDB) SaveMore

func (this *LevelDB) SaveMore(kvps ...KVPair) error

保存多条数据

func (*LevelDB) SaveMore_TransacTion

func (this *LevelDB) SaveMore_TransacTion(kvps ...KVPair) error

保存多条数据 事务处理

func (*LevelDB) SaveOrUpdateListByIndex

func (this *LevelDB) SaveOrUpdateListByIndex(dbkey LeveldbKey, index, value []byte, batch *leveldb.Batch) utils.ERROR

修改List集合中的记录 @isTransaction bool 是否是事务操作

func (*LevelDB) SaveOrUpdateMapInListByIndex

func (this *LevelDB) SaveOrUpdateMapInListByIndex(dbkey, key LeveldbKey, index, value []byte, batch *leveldb.Batch) utils.ERROR

修改MapInList集合中的一个key的value值 @isTransaction bool 是否是事务操作

func (*LevelDB) Save_Transaction

func (this *LevelDB) Save_Transaction(key LeveldbKey, bs *[]byte) utils.ERROR

保存 事务处理

type LeveldbInfo

type LeveldbInfo struct {
	Version    int   //版本
	CreateTime int64 //创建时间
}

数据库中带的基本信息

type LeveldbKey

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

func JoinDbKey

func JoinDbKey(dbkeys ...LeveldbKey) *LeveldbKey

把多个dbkey合并为1个

func LeveldbParseKeyMore

func LeveldbParseKeyMore(key []byte) ([]LeveldbKey, utils.ERROR)

多个长度+[]byte拼接,通过长度解析出来多个数据。不能有不符合规范的数据,有则报错

func NewLeveldbKey

func NewLeveldbKey(key []byte) (*LeveldbKey, utils.ERROR)

创建一个数据库key

func NewLeveldbKeyJoin

func NewLeveldbKeyJoin(keys ...[]byte) (*LeveldbKey, utils.ERROR)

联合key

func (*LeveldbKey) BaseKey

func (this *LeveldbKey) BaseKey() ([]byte, utils.ERROR)

获取去掉长度前缀的原始key

func (*LeveldbKey) Byte

func (this *LeveldbKey) Byte() []byte

获取去掉长度前缀的原始key

Jump to

Keyboard shortcuts

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