badgerHelper

package module
v0.0.0-...-8d1d1cb Latest Latest
Warning

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

Go to latest
Published: May 20, 2020 License: MIT Imports: 19 Imported by: 0

README

BadgerDB Helper

针对BadgerDB的数据结构辅助类集合,用于辅助存取BADGER的数据

安装
go get -u github.com/koangel/badgerHelper
支持badgerDB版本
go get github.com/dgraph-io/badger/v2
部分算法来自以及改进于
  • simplebolt
  • redix
目前支持数据结构
  • list
  • set
  • hashmap
  • kvStore
Codec支持
  • protobuf
  • msgpack
进度
  • 完成基本的测试代码
  • 可以基本自用
  • 底层算法支持自动大数量事务提交
已使用项目
  • 自用测试模型
  • 我司大型CDN系统以及DNS系统

BadgerDB Helper

Data Sturct For BadgerDB

Install
go get -u github.com/koangel/badgerHelper
Support For BadgerDB
go get github.com/dgraph-io/badger/v2
Source of inspiration
  • simplebolt
  • redix
Support Sturct
  • list
  • set
  • hashmap
  • kvStore
Support Codec
  • protobuf
  • msgpack

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotSetCodec      = errors.New("not set codec plugin")
	ErrInvaildProtoType = errors.New("Invaild Protobuf type")
)
View Source
var (
	ErrDatabaseUnopen = errors.New("database Unopen")
	// ErrKeyNotFound will be returned if the key was not found in a HashMap or KeyValue struct
	ErrKeyNotFound = errors.New("Key not found")

	// ErrDoesNotExist will be returned if an element was not found. Used in List, Set, HashMap and KeyValue.
	ErrDoesNotExist = errors.New("Does not exist")

	// ErrExistsInSet is only returned if an element is added to a Set, but it already exists
	ErrExistsInSet = errors.New("Element already exists in set")

	// ErrInvalidID is only returned if adding an element to a HashMap that contains a colon (:)
	ErrInvalidID = errors.New("Element ID can not contain \":\"")

	//
	ErrOnlyUseTx = errors.New("Tx Mode ,Only Use AddTx Or AddTxTTL")

	ErrKeyValueCountNotEqual = errors.New("Key And Value Count Not Equal")
)
View Source
var (
	ErrInvalidLengthMsgproto        = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowMsgproto          = fmt.Errorf("proto: integer overflow")
	ErrUnexpectedEndOfGroupMsgproto = fmt.Errorf("proto: unexpected end of group")
)

Functions

func NewDB

func NewDB(path string, syncWrite, Compression bool, maxMemory int64) (*badger.DB, error)

func SetCodec

func SetCodec(codec Codec) error

codec plugins

Types

type Codec

type Codec interface {
	InitCodec() error
	Marshal(value interface{}) ([]byte, error)
	Unmarshal(data []byte, value interface{}) error
	Name() string
}

codec for protobuf or msgpack or other type codec

type DBStruct

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

Used for each of the datatypes

type HashMap

type HashMap DBStruct

HASHMAP数据结构

func NewHashMap

func NewHashMap(db *badger.DB, id string) (*HashMap, error)

Create Hash Map Sturct

func (*HashMap) All

func (l *HashMap) All(elementid []byte, Reverse bool) ([][]byte, error)

返回全部的数据,可以排序,正序或倒序

func (*HashMap) AllElementId

func (h *HashMap) AllElementId(Reverse bool) ([][]byte, error)

Get Element All Values

func (*HashMap) AllKV

func (l *HashMap) AllKV(elementid []byte, Reverse bool) (map[string][]byte, error)

func (*HashMap) BeginWrite

func (l *HashMap) BeginWrite() error

func (*HashMap) Clear

func (h *HashMap) Clear() error

Clear HashMap All Data

func (*HashMap) Close

func (l *HashMap) Close() error

Close And Clear HashMap

func (*HashMap) Commit

func (l *HashMap) Commit() error

func (*HashMap) Decr

func (h *HashMap) Decr(elementid, key []byte) error

decrease value

func (*HashMap) DecrBy

func (h *HashMap) DecrBy(elementid, key []byte, v uint64) error

func (*HashMap) Del

func (h *HashMap) Del(elementid []byte) error

Delete ElementId

func (*HashMap) DelKey

func (h *HashMap) DelKey(elementid, key []byte) error

Delete Key

func (*HashMap) Exists

func (h *HashMap) Exists(elementid []byte) (bool, error)

Check Element Exist

func (*HashMap) Get

func (h *HashMap) Get(elementid, key []byte) ([]byte, error)

Get Element Data

func (*HashMap) GetWithCodec

func (h *HashMap) GetWithCodec(elementid, key []byte, value interface{}) error

func (*HashMap) Has

func (h *HashMap) Has(elementid, key []byte) (bool, error)

Check Element And Key Exist

func (*HashMap) HashKeys

func (h *HashMap) HashKeys(elementid []byte, prefix []byte) ([][]byte, error)

func (*HashMap) INT64

func (h *HashMap) INT64(elementid, key []byte) (int64, error)

func (*HashMap) Incr

func (h *HashMap) Incr(elementid, key []byte) error

increase value

func (*HashMap) IncrBy

func (h *HashMap) IncrBy(elementid, key []byte, v uint64) error

func (*HashMap) Keys

func (h *HashMap) Keys(owner []byte) ([][]byte, error)

Return All Keys(Only Keys)

func (*HashMap) Len

func (h *HashMap) Len(elementid []byte) int

func (*HashMap) RangeRev

func (h *HashMap) RangeRev(elementid []byte, startPos, limit int64) ([][]byte, error)

反向读取数据,数据是通过反向排序的

func (*HashMap) Set

func (h *HashMap) Set(elementid, key, value []byte) error

Set Element Key And Value

func (*HashMap) SetTTL

func (h *HashMap) SetTTL(elementid, key, value []byte, ttl time.Duration) error

Set Element Key And Value With TTL

func (*HashMap) SetTTLWithCodec

func (h *HashMap) SetTTLWithCodec(elementid, key []byte, value interface{}, ttl time.Duration) error

func (*HashMap) SetWithCodec

func (h *HashMap) SetWithCodec(elementid, key []byte, value interface{}) error

Set Element Key And Value With Codec

func (*HashMap) TTL

func (h *HashMap) TTL(elementid, key []byte) (int64, error)

func (*HashMap) UINT64

func (h *HashMap) UINT64(elementid, key []byte) (uint64, error)

type Hyperloglog

type Hyperloglog struct {
	DBStruct
	// contains filtered or unexported fields
}

type KeyValue

type KeyValue DBStruct

func NewKeyValue

func NewKeyValue(db *badger.DB, id string) (*KeyValue, error)

func (*KeyValue) BeginWrite

func (l *KeyValue) BeginWrite() error

func (*KeyValue) Clear

func (kv *KeyValue) Clear() error

清理全部数据

func (*KeyValue) Close

func (l *KeyValue) Close() error

func (*KeyValue) Commit

func (l *KeyValue) Commit() error

func (*KeyValue) Del

func (kv *KeyValue) Del(key []byte) error

删除KEY

func (*KeyValue) DelTx

func (kv *KeyValue) DelTx(tx *badger.Txn, key []byte) error

func (*KeyValue) Get

func (kv *KeyValue) Get(key []byte) ([]byte, error)

读取数据内容

func (*KeyValue) GetWithCodec

func (kv *KeyValue) GetWithCodec(key []byte, value interface{}) error

func (*KeyValue) Inc

func (kv *KeyValue) Inc(key []byte) ([]byte, error)

func (*KeyValue) Set

func (kv *KeyValue) Set(key, value []byte) error

设置数据

func (*KeyValue) SetBatch

func (kv *KeyValue) SetBatch(key, value [][]byte) error

func (*KeyValue) SetBatchWithCodec

func (kv *KeyValue) SetBatchWithCodec(data map[string]interface{}) error

func (*KeyValue) SetTX

func (kv *KeyValue) SetTX(tx *badger.Txn, key, value []byte) error

func (*KeyValue) SetTXWithCodec

func (kv *KeyValue) SetTXWithCodec(tx *badger.Txn, key []byte, value interface{}) error

func (*KeyValue) SetWithCodec

func (kv *KeyValue) SetWithCodec(key []byte, value interface{}) error

type List

type List DBStruct

List数据结构

func NewList

func NewList(db *badger.DB, id string) (*List, error)

创建LIST数据结构

func (*List) Add

func (l *List) Add(value []byte) error

添加一个LIST数据,这个数据有个前缀

func (*List) AddBatch

func (l *List) AddBatch(value [][]byte) error

func (*List) AddBatchTTL

func (l *List) AddBatchTTL(value [][]byte, nt time.Duration) error

func (*List) AddTTL

func (l *List) AddTTL(value []byte, nt time.Duration) error

func (*List) AddTTLWithCodec

func (l *List) AddTTLWithCodec(value interface{}, ttl time.Duration) error

func (*List) AddTx

func (l *List) AddTx(tx *badger.Txn, value []byte) error

func (*List) AddTxTTL

func (l *List) AddTxTTL(tx *badger.Txn, value []byte, nt time.Duration) error

func (*List) AddTxWB

func (l *List) AddTxWB(tx *badger.WriteBatch, value []byte) error

func (*List) AddTxWBTTL

func (l *List) AddTxWBTTL(tx *badger.WriteBatch, value []byte, nt time.Duration) error

func (*List) AddWithCodec

func (l *List) AddWithCodec(value interface{}) error

func (*List) All

func (l *List) All(Reverse bool) ([][]byte, error)

返回全部的数据,可以排序,正序或倒序

func (*List) BeginWrite

func (l *List) BeginWrite() error

func (*List) Clear

func (l *List) Clear() error

清理列表所有数据

func (*List) Close

func (l *List) Close() error

func (*List) Commit

func (l *List) Commit() error

func (*List) KeepFrontData

func (l *List) KeepFrontData(keep int) error

func (*List) Last

func (l *List) Last() ([]byte, error)

Last will return the last element of a list

func (*List) LastN

func (l *List) LastN(n int) ([][]byte, error)

获得结尾的数量

func (*List) Len

func (l *List) Len() int

func (*List) RangeRev

func (l *List) RangeRev(startPos, limit int64) ([][]byte, error)

反向读取数据,数据是通过反向排序的

func (*List) RemoveLast

func (l *List) RemoveLast() error

删除最后一个数据

type MsgpackCodec

type MsgpackCodec struct{}

msgpack codec

func (*MsgpackCodec) InitCodec

func (c *MsgpackCodec) InitCodec() error

func (*MsgpackCodec) Marshal

func (c *MsgpackCodec) Marshal(value interface{}) ([]byte, error)

func (*MsgpackCodec) Name

func (c *MsgpackCodec) Name() string

func (*MsgpackCodec) Unmarshal

func (c *MsgpackCodec) Unmarshal(data []byte, value interface{}) error

type ProtoCodec

type ProtoCodec struct{}

protobuf codec

func (*ProtoCodec) InitCodec

func (c *ProtoCodec) InitCodec() error

func (*ProtoCodec) Marshal

func (c *ProtoCodec) Marshal(value interface{}) ([]byte, error)

func (*ProtoCodec) Name

func (c *ProtoCodec) Name() string

func (*ProtoCodec) Unmarshal

func (c *ProtoCodec) Unmarshal(data []byte, value interface{}) error

type Set

type Set DBStruct

SET数据结构

func NewSet

func NewSet(db *badger.DB, id string) (*Set, error)

func (*Set) Add

func (s *Set) Add(value []byte) error

Add an element to the set

func (*Set) AddBatchTx

func (s *Set) AddBatchTx(value [][]byte, ttl time.Duration) error

批量设置

func (*Set) AddTTL

func (s *Set) AddTTL(value []byte, ttl time.Duration) error

func (*Set) All

func (s *Set) All(Reverse bool) ([][]byte, error)

All returns all elements in the set

func (*Set) BeginWrite

func (l *Set) BeginWrite() error

func (*Set) Clear

func (s *Set) Clear() error

Clear will remove all elements from this set

func (*Set) Close

func (l *Set) Close() error

func (*Set) Commit

func (l *Set) Commit() error

func (*Set) Del

func (s *Set) Del(value []byte) error

Del will remove an element from the set

func (*Set) Has

func (s *Set) Has(value []byte) (bool, error)

Has will check if a given value is in the set

func (*Set) RemoveLast

func (s *Set) RemoveLast() error

Remove this set

type TestMsg

type TestMsg struct {
	Abc                  string   `protobuf:"bytes,1,opt,name=abc,proto3" json:"abc,omitempty"`
	Intv32               int32    `protobuf:"zigzag32,2,opt,name=intv32,proto3" json:"intv32,omitempty"`
	Intv64               int64    `protobuf:"zigzag64,3,opt,name=intv64,proto3" json:"intv64,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*TestMsg) Descriptor

func (*TestMsg) Descriptor() ([]byte, []int)

func (*TestMsg) GetAbc

func (m *TestMsg) GetAbc() string

func (*TestMsg) GetIntv32

func (m *TestMsg) GetIntv32() int32

func (*TestMsg) GetIntv64

func (m *TestMsg) GetIntv64() int64

func (*TestMsg) Marshal

func (m *TestMsg) Marshal() (dAtA []byte, err error)

func (*TestMsg) MarshalTo

func (m *TestMsg) MarshalTo(dAtA []byte) (int, error)

func (*TestMsg) MarshalToSizedBuffer

func (m *TestMsg) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*TestMsg) ProtoMessage

func (*TestMsg) ProtoMessage()

func (*TestMsg) Reset

func (m *TestMsg) Reset()

func (*TestMsg) Size

func (m *TestMsg) Size() (n int)

func (*TestMsg) String

func (m *TestMsg) String() string

func (*TestMsg) Unmarshal

func (m *TestMsg) Unmarshal(dAtA []byte) error

func (*TestMsg) XXX_DiscardUnknown

func (m *TestMsg) XXX_DiscardUnknown()

func (*TestMsg) XXX_Marshal

func (m *TestMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*TestMsg) XXX_Merge

func (m *TestMsg) XXX_Merge(src proto.Message)

func (*TestMsg) XXX_Size

func (m *TestMsg) XXX_Size() int

func (*TestMsg) XXX_Unmarshal

func (m *TestMsg) XXX_Unmarshal(b []byte) error

Jump to

Keyboard shortcuts

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