simplebolt

package module
v1.5.2-0...-7e8729b Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 15 Imported by: 0

README

Simple Bolt Build Status GoDoc Go Report Card

Simple way to use the Bolt database. Similar design to simpleredis.

Online API Documentation

godoc.org

修改内容

  • 与原版不兼容
  • 优化存放不使用STRING,而使用BYTES
  • 增加TTL
  • 分离所有的数据结构到不同的文件,更好维护
  • LIST结构增加更多的操作函数
  • 懒得写英文文档
  • 基本自用,需要的话请自行修改
  • Add TimeSeries
go get -u github.com/koangel/simplebolt@v6.0.0

Features and limitations

  • Supports simple use of lists, hashmaps, sets and key/values.
  • Deals mainly with strings.
  • Requires Go 1.9 or later.
  • Note that HashMap is implemented only for API-compatibility with simpleredis, and does not have the same performance profile as the HashMap implementation in simpleredis, simplemaria (MariaDB/MySQL) or simplehstore (PostgreSQL w/ HSTORE).
  • Add TTL Time
  • Add TimeSeries

Example usage

package main

import (
	"log"

	"github.com/koangel/simplebolt"
)

func main() {
	// New bolt database struct
	db, err := simplebolt.New("bolt.db")
	if err != nil {
		log.Fatalf("Could not create database! %s", err)
	}
	defer db.Close()

	// Create a list named "greetings"
	list, err := simplebolt.NewList(db, "greetings")
	if err != nil {
		log.Fatalf("Could not create a list! %s", err)
	}

	// Add "hello" to the list
	if err := list.Add([]byte("hello")); err != nil {
		log.Fatalf("Could not add an item to the list! %s", err)
	}

	// Get the last item of the list
	if item, err := list.Last(); err != nil {
		log.Fatalf("Could not fetch the last item from the list! %s", err)
	} else {
		log.Println("The value of the stored item is:", item)
	}

	// Remove the list
	if err := list.Remove(); err != nil {
		log.Fatalf("Could not remove the list! %s", err)
	}
}

Contributors

  • Luis Villegas, for the linked list functionality.

Version, license and author

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

View Source
const (
	// Version number. Stable API within major version numbers.
	// 不在兼容以前版本
	Version = 6.0
)

Variables

View Source
var (
	ErrNotSetCodec      = errors.New("not set codec plugin")
	ErrInvaildProtoType = errors.New("Invaild Protobuf type")
)
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")
)
View Source
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 BytesToInt64(buf []byte) int64

func Int64ToBytes

func Int64ToBytes(i int64) []byte

func NewTTL

func NewTTL(db *Database)

func SetCallback

func SetCallback(fn ExpFunc)

func SetCodec

func SetCodec(codec Codec) error

codec plugins

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.

func (*BoltCreator) NewList

func (b *BoltCreator) NewList(id string) (*List, error)

NewList can create a new List with the given ID

func (*BoltCreator) NewSet

func (b *BoltCreator) NewSet(id string) (*Set, error)

NewSet can create a new Set with the given ID

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

type Database bbolt.DB

Database represents Bolt database

func New

func New(filename string) (*Database, error)

New creates a new Bolt database struct, using the given file or creating a new file, as needed

func (*Database) Close

func (db *Database) Close()

Close the database

func (*Database) DB

func (db *Database) DB() *bbolt.DB

func (*Database) Path

func (db *Database) Path() string

Path returns the full path to the database file

func (*Database) Ping

func (db *Database) Ping() error

Ping the database (only for fulfilling the pinterface.IHost interface)

func (*Database) RemoveTTLBucket

func (db *Database) RemoveTTLBucket(b []byte)

func (*Database) RemoveTTLBucketTx

func (db *Database) RemoveTTLBucketTx(tx *bbolt.Tx, b []byte)

func (*Database) RemoveTTLKey

func (db *Database) RemoveTTLKey(b, key []byte)

func (*Database) RemoveTTLKeyData

func (db *Database) RemoveTTLKeyData(bname []byte, key [][]byte)

func (*Database) RemoveTTLKeyDataTx

func (db *Database) RemoveTTLKeyDataTx(tx *bbolt.Tx, bname []byte, key [][]byte) error

func (*Database) RemoveTTLKeyTx

func (db *Database) RemoveTTLKeyTx(tx *bbolt.Tx, b, key []byte)

func (*Database) TTLBucket

func (db *Database) TTLBucket(b []byte, exp time.Duration) error

func (*Database) TTLBucketTx

func (db *Database) TTLBucketTx(tx *bbolt.Tx, b []byte, exp time.Duration) error

func (*Database) TTLKey

func (db *Database) TTLKey(b, key []byte, exp time.Duration) error

func (*Database) TTLKeyTx

func (db *Database) TTLKeyTx(tx *bbolt.Tx, b, key []byte, exp time.Duration) error

type ExpFunc

type ExpFunc func(bName, key, value []byte)

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

func NewHashMap(db *Database, id string) (*HashMap, error)

NewHashMap loads or creates a new HashMap struct, with the given ID

func NewHashMapOnly

func NewHashMapOnly(db *Database, id string) (*HashMap, error)

func NewHashMapTTL

func NewHashMapTTL(db *Database, id string, ntime time.Duration) (*HashMap, error)

func (*HashMap) All

func (h *HashMap) All() ([][]byte, error)

All returns all ID's, for all hash elements

func (*HashMap) BeginWrite

func (l *HashMap) BeginWrite() error

func (*HashMap) Clear

func (h *HashMap) Clear() error

Clear will remove all elements from this hash map

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

Del will remove an element (for instance a user)

func (*HashMap) DelKey

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

DelKey will remove a key for an entry in a hashmap (for instance the email field for a user)

func (*HashMap) Exists

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

Exists will check if a given elementid exists as a hash map at all

func (*HashMap) Get

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

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 (h *HashMap) GetWithCodec(elementid, key []byte, value interface{}) error

func (*HashMap) Has

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

Has will check if a given elementid + key is in the hash map

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)

Keys returns all names of all keys of a given owner.

func (*HashMap) Remove

func (h *HashMap) Remove() error

Remove this hashmap

func (*HashMap) Rollback

func (l *HashMap) Rollback() error

func (*HashMap) Set

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

Set a value in a hashmap given the element id (for instance a user id) and the key (for instance "password")

func (*HashMap) SetTTL

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

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

func (*HashMap) UINT64

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

func (*HashMap) UpdateTTL

func (l *HashMap) UpdateTTL()

type KeyValue

type KeyValue boltBucket

KeyValue is a Bolt bucket, with methods for acting like a key=>value store

func NewKeyValue

func NewKeyValue(db *Database, id string) (*KeyValue, error)

NewKeyValue loads or creates a new KeyValue struct, with the given ID

func NewKeyValueOnly

func NewKeyValueOnly(db *Database, id string) (*KeyValue, error)

func NewKeyValueTTL

func NewKeyValueTTL(db *Database, id string, ntime time.Duration) (*KeyValue, error)

func (*KeyValue) BeginWrite

func (l *KeyValue) BeginWrite() error

func (*KeyValue) Clear

func (kv *KeyValue) Clear() error

Clear will remove all elements from this key/value

func (*KeyValue) Commit

func (l *KeyValue) Commit() error

func (*KeyValue) Del

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

Del will remove a key

func (*KeyValue) DelTx

func (kv *KeyValue) DelTx(tx *bbolt.Tx, key []byte) error

func (*KeyValue) Get

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

Get a value given a key Returns an error if the key was not found

func (*KeyValue) Inc

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

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

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

func (*KeyValue) Remove

func (kv *KeyValue) Remove() error

Remove this key/value

func (*KeyValue) Rollback

func (l *KeyValue) Rollback() error

func (*KeyValue) Set

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

Set a key and value

func (*KeyValue) SetBatch

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

func (*KeyValue) SetTX

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

func (*KeyValue) UpdateTTL

func (l *KeyValue) UpdateTTL()

type List

type List boltBucket

List is a Bolt bucket, with methods for acting like a list

func NewList

func NewList(db *Database, id string) (*List, error)

NewList loads or creates a new List struct, with the given ID

func NewListNoCreate

func NewListNoCreate(db *Database, id string) (*List, error)

only list no create bucket

func NewListTTL

func NewListTTL(db *Database, id string, ntime time.Duration) (*List, error)

func NewListTx

func NewListTx(db *Database, tx *bbolt.Tx, id string) (*List, error)

func NewListTxTTL

func NewListTxTTL(db *Database, tx *bbolt.Tx, id string, ntime time.Duration) (*List, error)

func (*List) Add

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

Add an element to the 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) AddTx

func (l *List) AddTx(tx *bbolt.Tx, value []byte) error

func (*List) AddTxTTL

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

func (*List) All

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

All returns all elements in the list

func (*List) BeginWrite

func (l *List) BeginWrite() error

func (*List) Clear

func (l *List) Clear() error

Clear will remove all elements from this list

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)

LastN will return the last N elements of a list

func (*List) Len

func (l *List) Len() int

func (*List) RangeRev

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

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

func (*List) Remove

func (l *List) Remove() error

Remove this list

func (*List) Rollback

func (l *List) Rollback() error

func (*List) UpdateTTL

func (l *List) UpdateTTL()

--- List functions ---

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 NewSet

func NewSet(db *Database, id string) (*Set, error)

NewSet loads or creates a new Set struct, with the given ID

func NewSetOnly

func NewSetOnly(db *Database, id string) (*Set, error)

func NewSetTTL

func NewSetTTL(db *Database, id string, ntime time.Duration) (*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() ([][]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) 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) Remove

func (s *Set) Remove() error

Remove this set

func (*Set) Rollback

func (l *Set) Rollback() error

func (*Set) UpdateTTL

func (l *Set) UpdateTTL()

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

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 NewTimeSeriesTTL(db *Database, id string, ntime time.Duration) (*TimeSeries, error)

func NewTimeSeriesTx

func NewTimeSeriesTx(db *Database, tx *bbolt.Tx, id string) (*TimeSeries, error)

func NewTimeSeriesTxTTL

func NewTimeSeriesTxTTL(db *Database, tx *bbolt.Tx, id string, ntime time.Duration) (*TimeSeries, error)

func (*TimeSeries) Add

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

Add an element to the list

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

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

func (*TimeSeries) AddTx

func (l *TimeSeries) AddTx(tx *bbolt.Tx, value []byte) error

func (*TimeSeries) AddTxTTL

func (l *TimeSeries) AddTxTTL(tx *bbolt.Tx, value []byte, nt time.Duration) error

func (*TimeSeries) AddWithTime

func (l *TimeSeries) AddWithTime(value []byte, timeVal int64) error

func (*TimeSeries) AddWithTimeTTL

func (l *TimeSeries) AddWithTimeTTL(value []byte, nt time.Duration, timeVal int64) error

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

func (l *TimeSeries) Remove() error

Remove this list

func (*TimeSeries) Rollback

func (l *TimeSeries) Rollback() error

func (*TimeSeries) UpdateTTL

func (l *TimeSeries) UpdateTTL()

--- List functions ---

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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