Documentation ¶
Overview ¶
Package simplebolt provides a simple way to use the Bolt database. The API design is similar to xyproto/simpleredis, and the database backends are interchangeable, by using the xyproto/pinterface package.
Index ¶
- Constants
- Variables
- func BytesToInt64(buf []byte) int64
- func Int64ToBytes(i int64) []byte
- func NewTTL(db *Database)
- func SetCallback(fn ExpFunc)
- func SetCodec(codec Codec) error
- type BoltCreator
- type Codec
- type Database
- func (db *Database) Close()
- func (db *Database) DB() *bbolt.DB
- func (db *Database) Path() string
- func (db *Database) Ping() error
- func (db *Database) RemoveTTLBucket(b []byte)
- func (db *Database) RemoveTTLBucketTx(tx *bbolt.Tx, b []byte)
- func (db *Database) RemoveTTLKey(b, key []byte)
- func (db *Database) RemoveTTLKeyData(bname []byte, key [][]byte)
- func (db *Database) RemoveTTLKeyDataTx(tx *bbolt.Tx, bname []byte, key [][]byte) error
- func (db *Database) RemoveTTLKeyTx(tx *bbolt.Tx, b, key []byte)
- func (db *Database) TTLBucket(b []byte, exp time.Duration) error
- func (db *Database) TTLBucketTx(tx *bbolt.Tx, b []byte, exp time.Duration) error
- func (db *Database) TTLKey(b, key []byte, exp time.Duration) error
- func (db *Database) TTLKeyTx(tx *bbolt.Tx, b, key []byte, exp time.Duration) error
- type ExpFunc
- type HashMap
- func (h *HashMap) All() ([][]byte, error)
- func (l *HashMap) BeginWrite() error
- func (h *HashMap) Clear() error
- func (l *HashMap) Commit() error
- func (h *HashMap) Decr(elementid, key []byte) error
- func (h *HashMap) DecrBy(elementid, key []byte, v uint64) error
- func (h *HashMap) Del(elementid []byte) error
- func (h *HashMap) DelKey(elementid, key []byte) error
- func (h *HashMap) Exists(elementid []byte) (bool, error)
- func (h *HashMap) Get(elementid, key []byte) ([]byte, error)
- func (h *HashMap) GetWithCodec(elementid, key []byte, value interface{}) error
- func (h *HashMap) Has(elementid, key []byte) (bool, error)
- func (h *HashMap) HashKeys(elementid []byte, prefix []byte) ([][]byte, error)
- func (h *HashMap) INT64(elementid, key []byte) (int64, error)
- func (h *HashMap) Incr(elementid, key []byte) error
- func (h *HashMap) IncrBy(elementid, key []byte, v uint64) error
- func (h *HashMap) Keys(owner []byte) ([][]byte, error)
- func (h *HashMap) Remove() error
- func (l *HashMap) Rollback() error
- func (h *HashMap) Set(elementid, key, value []byte) error
- func (h *HashMap) SetTTL(elementid, key, value []byte, ttl time.Duration) error
- func (h *HashMap) SetTTLWithCodec(elementid, key []byte, value interface{}, ttl time.Duration) error
- func (h *HashMap) SetWithCodec(elementid, key []byte, value interface{}) error
- func (h *HashMap) UINT64(elementid, key []byte) (uint64, error)
- func (l *HashMap) UpdateTTL()
- type KeyValue
- func (l *KeyValue) BeginWrite() error
- func (kv *KeyValue) Clear() error
- func (l *KeyValue) Commit() error
- func (kv *KeyValue) Del(key []byte) error
- func (kv *KeyValue) DelTx(tx *bbolt.Tx, key []byte) error
- func (kv *KeyValue) Get(key []byte) ([]byte, error)
- func (kv *KeyValue) Inc(key []byte) ([]byte, error)
- func (kv *KeyValue) InsertBatch(key, value []byte) error
- func (kv *KeyValue) Remove() error
- func (l *KeyValue) Rollback() error
- func (kv *KeyValue) Set(key, value []byte) error
- func (kv *KeyValue) SetBatch(key, value [][]byte) error
- func (kv *KeyValue) SetTX(tx *bbolt.Tx, key, value []byte) error
- func (l *KeyValue) UpdateTTL()
- type List
- func NewList(db *Database, id string) (*List, error)
- func NewListNoCreate(db *Database, id string) (*List, error)
- func NewListTTL(db *Database, id string, ntime time.Duration) (*List, error)
- func NewListTx(db *Database, tx *bbolt.Tx, id string) (*List, error)
- func NewListTxTTL(db *Database, tx *bbolt.Tx, id string, ntime time.Duration) (*List, error)
- func (l *List) Add(value []byte) error
- func (l *List) AddBatch(value [][]byte) error
- func (l *List) AddBatchTTL(value [][]byte, nt time.Duration) error
- func (l *List) AddTTL(value []byte, nt time.Duration) error
- func (l *List) AddTx(tx *bbolt.Tx, value []byte) error
- func (l *List) AddTxTTL(tx *bbolt.Tx, value []byte, nt time.Duration) error
- func (l *List) All() ([][]byte, error)
- func (l *List) BeginWrite() error
- func (l *List) Clear() error
- func (l *List) Commit() error
- func (l *List) KeepFrontData(keep int) error
- func (l *List) Last() ([]byte, error)
- func (l *List) LastN(n int) ([][]byte, error)
- func (l *List) Len() int
- func (l *List) RangeRev(startPos, limit int64) ([][]byte, error)
- func (l *List) Remove() error
- func (l *List) Rollback() error
- func (l *List) UpdateTTL()
- type MsgpackCodec
- type ProtoCodec
- type Set
- func (s *Set) Add(value []byte) error
- func (s *Set) AddBatchTx(value [][]byte, ttl time.Duration) error
- func (s *Set) AddTTL(value []byte, ttl time.Duration) error
- func (s *Set) All() ([][]byte, error)
- func (l *Set) BeginWrite() error
- func (s *Set) Clear() error
- func (l *Set) Commit() error
- func (s *Set) Del(value []byte) error
- func (s *Set) Has(value []byte) (bool, error)
- func (s *Set) Remove() error
- func (l *Set) Rollback() error
- func (l *Set) UpdateTTL()
- type StoredData
- type TestMsg
- func (*TestMsg) Descriptor() ([]byte, []int)
- func (m *TestMsg) GetAbc() string
- func (m *TestMsg) GetIntv32() int32
- func (m *TestMsg) GetIntv64() int64
- func (m *TestMsg) Marshal() (dAtA []byte, err error)
- func (m *TestMsg) MarshalTo(dAtA []byte) (int, error)
- func (m *TestMsg) MarshalToSizedBuffer(dAtA []byte) (int, error)
- func (*TestMsg) ProtoMessage()
- func (m *TestMsg) Reset()
- func (m *TestMsg) Size() (n int)
- func (m *TestMsg) String() string
- func (m *TestMsg) Unmarshal(dAtA []byte) error
- func (m *TestMsg) XXX_DiscardUnknown()
- func (m *TestMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *TestMsg) XXX_Merge(src proto.Message)
- func (m *TestMsg) XXX_Size() int
- func (m *TestMsg) XXX_Unmarshal(b []byte) error
- type TimeSeries
- func NewTimeSeries(db *Database, id string) (*TimeSeries, error)
- func NewTimeSeriesNoCreate(db *Database, id string) (*TimeSeries, error)
- func NewTimeSeriesTTL(db *Database, id string, ntime time.Duration) (*TimeSeries, error)
- func NewTimeSeriesTx(db *Database, tx *bbolt.Tx, id string) (*TimeSeries, error)
- func NewTimeSeriesTxTTL(db *Database, tx *bbolt.Tx, id string, ntime time.Duration) (*TimeSeries, error)
- func (l *TimeSeries) Add(value []byte) error
- func (l *TimeSeries) AddBatch(value [][]byte) error
- func (l *TimeSeries) AddBatchTTL(value [][]byte, nt time.Duration) error
- func (l *TimeSeries) AddTTL(value []byte, nt time.Duration) error
- func (l *TimeSeries) AddTx(tx *bbolt.Tx, value []byte) error
- func (l *TimeSeries) AddTxTTL(tx *bbolt.Tx, value []byte, nt time.Duration) error
- func (l *TimeSeries) AddWithTime(value []byte, timeVal int64) error
- func (l *TimeSeries) AddWithTimeTTL(value []byte, nt time.Duration, timeVal int64) error
- func (l *TimeSeries) All() ([][]byte, error)
- func (l *TimeSeries) BeginWrite() error
- func (l *TimeSeries) Clear() error
- func (l *TimeSeries) Commit() error
- func (l *TimeSeries) KeepFrontData(keep int) error
- func (l *TimeSeries) Last() ([]byte, error)
- func (l *TimeSeries) LastN(n int) ([][]byte, error)
- func (l *TimeSeries) Len() int
- func (l *TimeSeries) RangeRev(startPos, limit int64) ([][]byte, error)
- func (l *TimeSeries) Remove() error
- func (l *TimeSeries) Rollback() error
- func (l *TimeSeries) UpdateTTL()
Constants ¶
const ( // Version number. Stable API within major version numbers. // 不在兼容以前版本 Version = 6.0 )
Variables ¶
var ( ErrNotSetCodec = errors.New("not set codec plugin") ErrInvaildProtoType = errors.New("Invaild Protobuf type") )
var ( ErrInvalidLengthMsgproto = fmt.Errorf("proto: negative length found during unmarshaling") ErrIntOverflowMsgproto = fmt.Errorf("proto: integer overflow") ErrUnexpectedEndOfGroupMsgproto = fmt.Errorf("proto: unexpected end of group") )
var ( // ErrBucketNotFound may be returned if a no Bolt bucket was found ErrBucketNotFound = errors.New("Bucket not found") // 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") )
Functions ¶
func BytesToInt64 ¶
func Int64ToBytes ¶
func SetCallback ¶
func SetCallback(fn ExpFunc)
Types ¶
type BoltCreator ¶
type BoltCreator struct {
// contains filtered or unexported fields
}
BoltCreator is used for implementing pinterface.ICreator. It contains a database and provides functions for creating data structures within that database.
func NewCreator ¶
func NewCreator(db *Database) *BoltCreator
NewCreator can create a new BoltCreator struct
func (*BoltCreator) NewHashMap ¶
func (b *BoltCreator) NewHashMap(id string) (*HashMap, error)
NewHashMap can create a new HashMap with the given ID. The HashMap elements have a name and then a key+value. For example a username for the name, then "password" as the key and a password hash as the value.
func (*BoltCreator) NewKeyValue ¶
func (b *BoltCreator) NewKeyValue(id string) (*KeyValue, error)
NewKeyValue can create a new KeyValue with the given ID. The KeyValue elements have a key with a corresponding value.
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 Database ¶
Database represents Bolt database
func New ¶
New creates a new Bolt database struct, using the given file or creating a new file, as needed
func (*Database) RemoveTTLBucket ¶
func (*Database) RemoveTTLKey ¶
func (*Database) RemoveTTLKeyData ¶
func (*Database) RemoveTTLKeyDataTx ¶
func (*Database) TTLBucketTx ¶
type HashMap ¶
type HashMap boltBucket
HashMap is a Bolt bucket, with methods for acting like a hash map (with an ID and then key=>value)
func NewHashMap ¶
NewHashMap loads or creates a new HashMap struct, with the given ID
func NewHashMapTTL ¶
func (*HashMap) BeginWrite ¶
func (*HashMap) DelKey ¶
DelKey will remove a key for an entry in a hashmap (for instance the email field for a user)
func (*HashMap) Get ¶
Get a value from a hashmap given the element id (for instance a user id) and the key (for instance "password")
func (*HashMap) GetWithCodec ¶
func (*HashMap) Set ¶
Set a value in a hashmap given the element id (for instance a user id) and the key (for instance "password")
func (*HashMap) SetTTLWithCodec ¶
func (*HashMap) SetWithCodec ¶
type KeyValue ¶
type KeyValue boltBucket
KeyValue is a Bolt bucket, with methods for acting like a key=>value store
func NewKeyValue ¶
NewKeyValue loads or creates a new KeyValue struct, with the given ID
func NewKeyValueTTL ¶
func (*KeyValue) BeginWrite ¶
func (*KeyValue) Inc ¶
Inc will increase the value of a key, returns the new value Returns an empty string if there were errors, or "0" if the key does not already exist.
func (*KeyValue) InsertBatch ¶
type List ¶
type List boltBucket
List is a Bolt bucket, with methods for acting like a list
func NewListNoCreate ¶
only list no create bucket
func NewListTxTTL ¶
func (*List) BeginWrite ¶
func (*List) KeepFrontData ¶
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 boltBucket
Set is a Bolt bucket, with methods for acting like a set, only allowing unique keys
func (*Set) BeginWrite ¶
type StoredData ¶
type StoredData interface { // Value returns the current value of the // element at which the item refers to. Value() []byte // Update resets the value of the element at // which the item refers to with newData. // // Returns "Empty data" error if newData is nil. // // It may also return an error in case of bbolt Update or protocol buffer // serialization/deserialization fail. In both cases, the data isn't updated. Update(newData []byte) error // Remove deletes from Bolt the element at which the item data refers to. // // It may return an error in case of bbolt Update or protocol buffer // serialization/deserialization fail. In both cases, the data isn't removed. Remove() error }
StoredData is the set of methods that provides access to the element's underlying data in every data structure.
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) MarshalToSizedBuffer ¶
func (*TestMsg) ProtoMessage ¶
func (*TestMsg) ProtoMessage()
func (*TestMsg) XXX_DiscardUnknown ¶
func (m *TestMsg) XXX_DiscardUnknown()
func (*TestMsg) XXX_Marshal ¶
func (*TestMsg) XXX_Unmarshal ¶
type TimeSeries ¶
type TimeSeries boltBucket
TimeSeries is a Bolt bucket, with methods for acting like a TimeSeries 新增时间序列数据结构
func NewTimeSeries ¶
func NewTimeSeries(db *Database, id string) (*TimeSeries, error)
NewTimeSeries loads or creates a new List struct, with the given ID
func NewTimeSeriesNoCreate ¶
func NewTimeSeriesNoCreate(db *Database, id string) (*TimeSeries, error)
only list no create bucket
func NewTimeSeriesTTL ¶
func NewTimeSeriesTx ¶
func NewTimeSeriesTxTTL ¶
func (*TimeSeries) AddBatch ¶
func (l *TimeSeries) AddBatch(value [][]byte) error
func (*TimeSeries) AddBatchTTL ¶
func (l *TimeSeries) AddBatchTTL(value [][]byte, nt time.Duration) error
func (*TimeSeries) AddWithTime ¶
func (l *TimeSeries) AddWithTime(value []byte, timeVal int64) error
func (*TimeSeries) AddWithTimeTTL ¶
func (*TimeSeries) All ¶
func (l *TimeSeries) All() ([][]byte, error)
All returns all elements in the list
func (*TimeSeries) BeginWrite ¶
func (l *TimeSeries) BeginWrite() error
func (*TimeSeries) Clear ¶
func (l *TimeSeries) Clear() error
Clear will remove all elements from this list
func (*TimeSeries) Commit ¶
func (l *TimeSeries) Commit() error
func (*TimeSeries) KeepFrontData ¶
func (l *TimeSeries) KeepFrontData(keep int) error
func (*TimeSeries) Last ¶
func (l *TimeSeries) Last() ([]byte, error)
Last will return the last element of a list
func (*TimeSeries) LastN ¶
func (l *TimeSeries) LastN(n int) ([][]byte, error)
LastN will return the last N elements of a list
func (*TimeSeries) Len ¶
func (l *TimeSeries) Len() int
func (*TimeSeries) RangeRev ¶
func (l *TimeSeries) RangeRev(startPos, limit int64) ([][]byte, error)
反向读取数据,数据是通过反向排序的
func (*TimeSeries) Rollback ¶
func (l *TimeSeries) Rollback() error