Documentation
¶
Overview ¶
Package db is a generated GoMock package.
Index ¶
- Constants
- Variables
- func CheckNamespacePrefixCollision(namespaces []string) error
- type BoltDB
- func (b *BoltDB) BucketExists(namespace string) bool
- func (b *BoltDB) Delete(namespace string, key []byte) (err error)
- func (b *BoltDB) Filter(namespace string, cond Condition, minKey, maxKey []byte) ([][]byte, [][]byte, error)
- func (b *BoltDB) ForEach(namespace string, fn func(k, v []byte) error) error
- func (b *BoltDB) Get(namespace string, key []byte) ([]byte, error)
- func (b *BoltDB) GetBucketByPrefix(namespace []byte) ([][]byte, error)
- func (b *BoltDB) GetKeyByPrefix(namespace, prefix []byte) ([][]byte, error)
- func (b *BoltDB) Insert(name []byte, key uint64, value []byte) error
- func (b *BoltDB) Purge(name []byte, key uint64) error
- func (b *BoltDB) Put(namespace string, key, value []byte) (err error)
- func (b *BoltDB) Range(namespace string, key []byte, count uint64) ([][]byte, error)
- func (b *BoltDB) Remove(name []byte, key uint64) error
- func (b *BoltDB) ScanRange(namespace string, min, max []byte, limit int) ([][]byte, [][]byte, error)
- func (b *BoltDB) SeekNext(name []byte, key uint64) ([]byte, error)
- func (b *BoltDB) SeekPrev(name []byte, key uint64) ([]byte, error)
- func (b *BoltDB) Start(_ context.Context) error
- func (b *BoltDB) Stop(_ context.Context) error
- func (b *BoltDB) WriteBatch(kvsb batch.KVStoreBatch) (err error)
- type BoltDBVersioned
- func (b *BoltDBVersioned) CommitBatch(version uint64, kvsb batch.KVStoreBatch) error
- func (b *BoltDBVersioned) Delete(version uint64, ns string, key []byte) error
- func (b *BoltDBVersioned) Filter(version uint64, ns string, cond Condition, minKey, maxKey []byte) ([][]byte, [][]byte, error)
- func (b *BoltDBVersioned) Get(version uint64, ns string, key []byte) ([]byte, error)
- func (b *BoltDBVersioned) Put(version uint64, ns string, key, value []byte) error
- func (b *BoltDBVersioned) Start(ctx context.Context) error
- func (b *BoltDBVersioned) Stop(ctx context.Context) error
- func (b *BoltDBVersioned) Version(ns string, key []byte) (uint64, error)
- type BoltDBVersionedOption
- type Condition
- type Config
- type CountingIndex
- type KVStore
- type KVStoreBasic
- type KVStoreFlusher
- type KVStoreFlusherOption
- type KVStoreForRangeIndex
- type KVStoreWithBuffer
- type KVStoreWithRange
- type KVStoreWithRangeScan
- type KvVersioned
- type KvWithVersion
- func (b *KvWithVersion) Delete(ns string, key []byte) error
- func (b *KvWithVersion) Filter(ns string, cond Condition, minKey, maxKey []byte) ([][]byte, [][]byte, error)
- func (b *KvWithVersion) Get(ns string, key []byte) ([]byte, error)
- func (b *KvWithVersion) Put(ns string, key, value []byte) error
- func (b *KvWithVersion) SetVersion(v uint64) KVStore
- func (b *KvWithVersion) Start(ctx context.Context) error
- func (b *KvWithVersion) Stop(ctx context.Context) error
- func (b *KvWithVersion) Version(ns string, key []byte) (uint64, error)
- func (b *KvWithVersion) WriteBatch(kvsb batch.KVStoreBatch) error
- type MockKVStore
- func (m *MockKVStore) Delete(arg0 string, arg1 []byte) error
- func (m *MockKVStore) EXPECT() *MockKVStoreMockRecorder
- func (m *MockKVStore) Filter(arg0 string, arg1 Condition, arg2, arg3 []byte) ([][]byte, [][]byte, error)
- func (m *MockKVStore) Get(arg0 string, arg1 []byte) ([]byte, error)
- func (m *MockKVStore) Put(arg0 string, arg1, arg2 []byte) error
- func (m *MockKVStore) Start(arg0 context.Context) error
- func (m *MockKVStore) Stop(arg0 context.Context) error
- func (m *MockKVStore) WriteBatch(arg0 batch.KVStoreBatch) error
- type MockKVStoreBasic
- func (m *MockKVStoreBasic) Delete(arg0 string, arg1 []byte) error
- func (m *MockKVStoreBasic) EXPECT() *MockKVStoreBasicMockRecorder
- func (m *MockKVStoreBasic) Get(arg0 string, arg1 []byte) ([]byte, error)
- func (m *MockKVStoreBasic) Put(arg0 string, arg1, arg2 []byte) error
- func (m *MockKVStoreBasic) Start(arg0 context.Context) error
- func (m *MockKVStoreBasic) Stop(arg0 context.Context) error
- type MockKVStoreBasicMockRecorder
- func (mr *MockKVStoreBasicMockRecorder) Delete(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreBasicMockRecorder) Get(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreBasicMockRecorder) Put(arg0, arg1, arg2 any) *gomock.Call
- func (mr *MockKVStoreBasicMockRecorder) Start(arg0 any) *gomock.Call
- func (mr *MockKVStoreBasicMockRecorder) Stop(arg0 any) *gomock.Call
- type MockKVStoreForRangeIndex
- func (m *MockKVStoreForRangeIndex) Delete(arg0 string, arg1 []byte) error
- func (m *MockKVStoreForRangeIndex) EXPECT() *MockKVStoreForRangeIndexMockRecorder
- func (m *MockKVStoreForRangeIndex) Filter(arg0 string, arg1 Condition, arg2, arg3 []byte) ([][]byte, [][]byte, error)
- func (m *MockKVStoreForRangeIndex) Get(arg0 string, arg1 []byte) ([]byte, error)
- func (m *MockKVStoreForRangeIndex) GetBucketByPrefix(arg0 []byte) ([][]byte, error)
- func (m *MockKVStoreForRangeIndex) GetKeyByPrefix(namespace, prefix []byte) ([][]byte, error)
- func (m *MockKVStoreForRangeIndex) Insert(arg0 []byte, arg1 uint64, arg2 []byte) error
- func (m *MockKVStoreForRangeIndex) Purge(arg0 []byte, arg1 uint64) error
- func (m *MockKVStoreForRangeIndex) Put(arg0 string, arg1, arg2 []byte) error
- func (m *MockKVStoreForRangeIndex) Remove(arg0 []byte, arg1 uint64) error
- func (m *MockKVStoreForRangeIndex) SeekNext(arg0 []byte, arg1 uint64) ([]byte, error)
- func (m *MockKVStoreForRangeIndex) SeekPrev(arg0 []byte, arg1 uint64) ([]byte, error)
- func (m *MockKVStoreForRangeIndex) Start(arg0 context.Context) error
- func (m *MockKVStoreForRangeIndex) Stop(arg0 context.Context) error
- func (m *MockKVStoreForRangeIndex) WriteBatch(arg0 batch.KVStoreBatch) error
- type MockKVStoreForRangeIndexMockRecorder
- func (mr *MockKVStoreForRangeIndexMockRecorder) Delete(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) Filter(arg0, arg1, arg2, arg3 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) Get(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) GetBucketByPrefix(arg0 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) GetKeyByPrefix(namespace, prefix any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) Insert(arg0, arg1, arg2 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) Purge(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) Put(arg0, arg1, arg2 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) Remove(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) SeekNext(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) SeekPrev(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) Start(arg0 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) Stop(arg0 any) *gomock.Call
- func (mr *MockKVStoreForRangeIndexMockRecorder) WriteBatch(arg0 any) *gomock.Call
- type MockKVStoreMockRecorder
- func (mr *MockKVStoreMockRecorder) Delete(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreMockRecorder) Filter(arg0, arg1, arg2, arg3 any) *gomock.Call
- func (mr *MockKVStoreMockRecorder) Get(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreMockRecorder) Put(arg0, arg1, arg2 any) *gomock.Call
- func (mr *MockKVStoreMockRecorder) Start(arg0 any) *gomock.Call
- func (mr *MockKVStoreMockRecorder) Stop(arg0 any) *gomock.Call
- func (mr *MockKVStoreMockRecorder) WriteBatch(arg0 any) *gomock.Call
- type MockKVStoreWithRange
- func (m *MockKVStoreWithRange) Delete(arg0 string, arg1 []byte) error
- func (m *MockKVStoreWithRange) EXPECT() *MockKVStoreWithRangeMockRecorder
- func (m *MockKVStoreWithRange) Filter(arg0 string, arg1 Condition, arg2, arg3 []byte) ([][]byte, [][]byte, error)
- func (m *MockKVStoreWithRange) Get(arg0 string, arg1 []byte) ([]byte, error)
- func (m *MockKVStoreWithRange) Put(arg0 string, arg1, arg2 []byte) error
- func (m *MockKVStoreWithRange) Range(arg0 string, arg1 []byte, arg2 uint64) ([][]byte, error)
- func (m *MockKVStoreWithRange) Start(arg0 context.Context) error
- func (m *MockKVStoreWithRange) Stop(arg0 context.Context) error
- func (m *MockKVStoreWithRange) WriteBatch(arg0 batch.KVStoreBatch) error
- type MockKVStoreWithRangeMockRecorder
- func (mr *MockKVStoreWithRangeMockRecorder) Delete(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreWithRangeMockRecorder) Filter(arg0, arg1, arg2, arg3 any) *gomock.Call
- func (mr *MockKVStoreWithRangeMockRecorder) Get(arg0, arg1 any) *gomock.Call
- func (mr *MockKVStoreWithRangeMockRecorder) Put(arg0, arg1, arg2 any) *gomock.Call
- func (mr *MockKVStoreWithRangeMockRecorder) Range(arg0, arg1, arg2 any) *gomock.Call
- func (mr *MockKVStoreWithRangeMockRecorder) Start(arg0 any) *gomock.Call
- func (mr *MockKVStoreWithRangeMockRecorder) Stop(arg0 any) *gomock.Call
- func (mr *MockKVStoreWithRangeMockRecorder) WriteBatch(arg0 any) *gomock.Call
- type Namespace
- type Option
- type PebbleDB
- func (b *PebbleDB) Delete(ns string, key []byte) (err error)
- func (b *PebbleDB) Filter(ns string, cond Condition, minKey []byte, maxKey []byte) (keys [][]byte, vals [][]byte, err error)
- func (b *PebbleDB) ForEach(ns string, fn func(k, v []byte) error) error
- func (b *PebbleDB) Get(ns string, key []byte) ([]byte, error)
- func (b *PebbleDB) Put(ns string, key, value []byte) (err error)
- func (b *PebbleDB) ScanRange(ns string, min, max []byte, limit int) ([][]byte, [][]byte, error)
- func (b *PebbleDB) Start(_ context.Context) error
- func (b *PebbleDB) Stop(_ context.Context) error
- func (b *PebbleDB) WriteBatch(kvsb batch.KVStoreBatch) error
- type RangeIndex
- type RangeScan
- type VersionedDB
Constants ¶
const ( DBBolt string = "boltdb" DBPebble = "pebbledb" DBAuto = "auto" )
Database types
Variables ¶
var ( // CountKey is the special key to store the size of index, it satisfies bytes.Compare(CountKey, MinUint64) = -1 CountKey = []byte{0, 0, 0, 0, 0, 0, 0} // ZeroIndex is 8-byte of 0 ZeroIndex = byteutil.Uint64ToBytesBigEndian(0) // ErrInvalid indicates an invalid input ErrInvalid = errors.New("invalid input") )
var ( // ErrBucketNotExist indicates certain bucket does not exist in db ErrBucketNotExist = errors.New("bucket not exist in DB") // ErrNotExist indicates certain item does not exist in Blockchain database ErrNotExist = errors.New("not exist in DB") // ErrIO indicates the generic error of DB I/O operation ErrIO = errors.New("DB I/O operation error") // ErrNotSupported indicates that api is not supported ErrNotSupported = errors.New("not supported") )
var ( // MaxKey is the special key such that bytes.Compare(MaxUint64, MaxKey) = -1 MaxKey = []byte{255, 255, 255, 255, 255, 255, 255, 255, 0} // NotExist is the empty byte slice to indicate a key does not exist (as a result of calling Purge()) NotExist = []byte{} )
var DefaultConfig = Config{ NumRetries: 3, MaxCacheSize: 64, BlockStoreBatchSize: 16, V2BlocksToSplitDB: 1000000, Compressor: "Snappy", CompressLegacy: false, SplitDBSizeMB: 0, SplitDBHeight: 900000, HistoryStateRetention: 2000, DBType: DBAuto, MemCacheSize: 1024 * 1024 * 128, }
DefaultConfig returns the default config
var ( // ErrDBNotStarted represents the error when a db has not started ErrDBNotStarted = errors.New("db has not started") )
var (
ErrDeleted = errors.New("deleted in DB")
)
var ( // ErrEmptyDBPath is the error when db path is empty ErrEmptyDBPath = errors.New("empty db path") )
Functions ¶
func CheckNamespacePrefixCollision ¶
CheckNamespacePrefixCollision verifies that no two distinct namespaces share the same 8-byte pebble key prefix.
PebbleDB stores every record as Hash160b(ns)[:8] || key, and decodeKey() strips those 8 bytes without checking which namespace produced them. Two namespaces whose prefixes collide would therefore interleave into a single logical bucket in pebble while remaining separate buckets in bolt -- i.e. the two engines would return different states for the same query, which is a chain fork.
It returns an error naming the colliding namespaces, or nil.
Types ¶
type BoltDB ¶
BoltDB is KVStore implementation based bolt DB
func (*BoltDB) BucketExists ¶
BucketExists returns true if bucket exists
func (*BoltDB) Filter ¶
func (b *BoltDB) Filter(namespace string, cond Condition, minKey, maxKey []byte) ([][]byte, [][]byte, error)
Filter returns <k, v> pair in a bucket that meet the condition
func (*BoltDB) ForEach ¶ added in v2.3.7
ForEach iterates over all key-value pairs in a bucket, calling fn for each. Unlike Filter, it does not accumulate results in memory — suitable for large buckets. The callback receives copies of key and value that are safe to retain. Return a non-nil error from fn to stop iteration early.
func (*BoltDB) GetBucketByPrefix ¶
GetBucketByPrefix retrieves all bucket those with const namespace prefix
func (*BoltDB) GetKeyByPrefix ¶
GetKeyByPrefix retrieves all keys those with const prefix
func (*BoltDB) ScanRange ¶
func (b *BoltDB) ScanRange(namespace string, min, max []byte, limit int) ([][]byte, [][]byte, error)
ScanRange returns up to limit <k, v> pairs in [min, max), ascending by bytes.Compare(k). See KVStoreWithRangeScan for the exact semantics, which must stay identical across engines.
func (*BoltDB) WriteBatch ¶
func (b *BoltDB) WriteBatch(kvsb batch.KVStoreBatch) (err error)
WriteBatch commits a batch
type BoltDBVersioned ¶
type BoltDBVersioned struct {
// contains filtered or unexported fields
}
BoltDBVersioned is KvVersioned implementation based on bolt DB
func NewBoltDBVersioned ¶
func NewBoltDBVersioned(cfg Config, opts ...BoltDBVersionedOption) *BoltDBVersioned
NewBoltDBVersioned instantiates an BoltDB which implements VersionedDB
func (*BoltDBVersioned) CommitBatch ¶ added in v2.2.0
func (b *BoltDBVersioned) CommitBatch(version uint64, kvsb batch.KVStoreBatch) error
CommitBatch write a batch to DB, where the batch can contain keys for both versioned and non-versioned namespace
func (*BoltDBVersioned) Delete ¶
func (b *BoltDBVersioned) Delete(version uint64, ns string, key []byte) error
Delete deletes a record, if key does not exist, it returns nil
func (*BoltDBVersioned) Filter ¶ added in v2.2.0
func (b *BoltDBVersioned) Filter(version uint64, ns string, cond Condition, minKey, maxKey []byte) ([][]byte, [][]byte, error)
Filter returns <k, v> pair in a bucket that meet the condition
func (*BoltDBVersioned) Put ¶
func (b *BoltDBVersioned) Put(version uint64, ns string, key, value []byte) error
Put writes a <key, value> record
func (*BoltDBVersioned) Start ¶
func (b *BoltDBVersioned) Start(ctx context.Context) error
Start starts the DB
type BoltDBVersionedOption ¶ added in v2.2.0
type BoltDBVersionedOption func(*BoltDBVersioned)
BoltDBVersionedOption sets option for BoltDBVersioned
func VnsOption ¶ added in v2.2.0
func VnsOption(ns ...Namespace) BoltDBVersionedOption
type Config ¶
type Config struct {
DbPath string `yaml:"dbPath"`
// NumRetries is the number of retries
NumRetries uint8 `yaml:"numRetries"`
// MaxCacheSize is the max number of blocks that will be put into an LRU cache. 0 means disabled
MaxCacheSize int `yaml:"maxCacheSize"`
// BlockStoreBatchSize is the number of blocks to be stored together as a unit (to get better compression)
BlockStoreBatchSize int `yaml:"blockStoreBatchSize"`
// V2BlocksToSplitDB is the accumulated number of blocks to split a new file after v1.1.2
V2BlocksToSplitDB uint64 `yaml:"v2BlocksToSplitDB"`
// Compressor is the compression used on block data, used by new DB file after v1.1.2
Compressor string `yaml:"compressor"`
// CompressLegacy enables gzip compression on block data, used by legacy DB file before v1.1.2
CompressLegacy bool `yaml:"compressLegacy"`
// SplitDBSizeMB is the config for DB's split file size
SplitDBSizeMB uint64 `yaml:"splitDBSizeMB"`
// SplitDBHeight is the config for DB's split start height
SplitDBHeight uint64 `yaml:"splitDBHeight"`
// HistoryStateRetention is the number of blocks account/contract state will be retained
HistoryStateRetention uint64 `yaml:"historyStateRetention"`
// ReadOnly is set db to be opened in read only mode
ReadOnly bool `yaml:"readOnly"`
// DBType is the type of database
DBType string `yaml:"dbType"`
// MemCacheSize is the size of the in-memory cache
MemCacheSize uint64 `yaml:"memCacheSize"`
}
Config is the config for database
func (Config) SplitDBSize ¶
SplitDBSize returns the configured SplitDBSizeMB
type CountingIndex ¶
type CountingIndex interface {
// Size returns the total number of keys so far
Size() uint64
// Add inserts a value into the index
Add([]byte, bool) error
// Get return value of key[slot]
Get(uint64) ([]byte, error)
// Range return value of keys [start, start+count)
Range(uint64, uint64) ([][]byte, error)
// Revert removes entries from end
Revert(uint64) error
// Close makes the index not usable
Close()
// Commit commits the batch
Commit() error
// UseBatch
UseBatch(batch.KVStoreBatch) error
// Finalize
Finalize() error
}
CountingIndex is a bucket of <k, v> where k consists of 8-byte whose value increments (0, 1, 2 ... n) upon each insertion the special key CountKey stores the total number of items in bucket so far
func GetCountingIndex ¶
func GetCountingIndex(kv KVStore, name []byte) (CountingIndex, error)
GetCountingIndex return an existing counting index
func NewCountingIndexNX ¶
func NewCountingIndexNX(kv KVStore, name []byte) (CountingIndex, error)
NewCountingIndexNX creates a new counting index if it does not exist, otherwise return existing index
type KVStore ¶
type KVStore interface {
KVStoreBasic
// WriteBatch commits a batch
WriteBatch(batch.KVStoreBatch) error
// Filter returns <k, v> pair in a bucket that meet the condition
Filter(string, Condition, []byte, []byte) ([][]byte, [][]byte, error)
}
KVStore is a KVStore with WriteBatch API
func CreateKVStore ¶
CreateKVStore creates db from config and db path
func CreateKVStoreWithCache ¶
CreateKVStoreWithCache creates db with cache from config and db path, cacheSize
func NewKvStoreWithCache ¶
NewKvStoreWithCache wraps kvstore with stateCaches
type KVStoreBasic ¶
type KVStoreBasic interface {
lifecycle.StartStopper
// Put insert or update a record identified by (namespace, key)
Put(string, []byte, []byte) error
// Get gets a record by (namespace, key)
Get(string, []byte) ([]byte, error)
// Delete deletes a record by (namespace, key)
Delete(string, []byte) error
}
KVStoreBasic is the interface of basic KV store.
type KVStoreFlusher ¶
type KVStoreFlusher interface {
SerializeQueue() []byte
Flush() error
KVStoreWithBuffer() KVStoreWithBuffer
BaseKVStore() KVStore
}
KVStoreFlusher is a wrapper of KVStoreWithBuffer, which has flush api
func NewKVStoreFlusher ¶
func NewKVStoreFlusher(store KVStore, buffer batch.CachedBatch, opts ...KVStoreFlusherOption) (KVStoreFlusher, error)
NewKVStoreFlusher returns kv store flusher
type KVStoreFlusherOption ¶
type KVStoreFlusherOption func(*flusher) error
KVStoreFlusherOption sets option for KVStoreFlusher
func FlushTranslateOption ¶
func FlushTranslateOption(wit batch.WriteInfoTranslate) KVStoreFlusherOption
FlushTranslateOption sets the translate for flush
func SerializeFilterOption ¶
func SerializeFilterOption(filter batch.WriteInfoFilter) KVStoreFlusherOption
SerializeFilterOption sets the filter for serialize write queue
func SerializeOption ¶
func SerializeOption(wis batch.WriteInfoSerialize) KVStoreFlusherOption
SerializeOption sets the serialize function for write queue
type KVStoreForRangeIndex ¶
type KVStoreForRangeIndex interface {
KVStore
// Insert inserts a value into the index
Insert([]byte, uint64, []byte) error
// SeekNext returns value by the key (if key not exist, use next key)
SeekNext([]byte, uint64) ([]byte, error)
// Remove removes an existing key
Remove([]byte, uint64) error
// Purge deletes an existing key and all keys before it
Purge([]byte, uint64) error
// GetBucketByPrefix retrieves all bucket those with const namespace prefix
GetBucketByPrefix([]byte) ([][]byte, error)
// GetKeyByPrefix retrieves all keys those with const prefix
GetKeyByPrefix(namespace, prefix []byte) ([][]byte, error)
// SeekPrev returns value by the key (if key not exist, use previous key)
SeekPrev([]byte, uint64) ([]byte, error)
}
KVStoreForRangeIndex is KVStore for range index
func NewMemKVStore ¶
func NewMemKVStore() KVStoreForRangeIndex
NewMemKVStore instantiates an in-memory KV store
type KVStoreWithBuffer ¶
type KVStoreWithBuffer interface {
KVStore
// contains filtered or unexported methods
}
KVStoreWithBuffer defines a KVStore with a buffer, which enables snapshot, revert, and transaction with multiple writes
type KVStoreWithRange ¶
type KVStoreWithRange interface {
KVStore
// Range gets a range of records by (namespace, key, count)
Range(string, []byte, uint64) ([][]byte, error)
}
KVStoreWithRange is KVStore with Range() API
type KVStoreWithRangeScan ¶
type KVStoreWithRangeScan interface {
KVStore
// ScanRange returns up to limit <k, v> pairs in [min, max), ascending by bytes.Compare(k)
ScanRange(ns string, min, max []byte, limit int) (keys [][]byte, values [][]byte, err error)
}
KVStoreWithRangeScan is a KVStore that supports ordered range iteration.
ScanRange is deliberately NOT the same as Filter(), and the differences are consensus-relevant. Every implementation MUST obey the following semantics exactly, because a divergence between storage engines (bolt vs pebble) is a chain fork:
- the interval is HALF-OPEN [min, max): a key equal to min is included, a key equal to max is excluded. (Filter()'s max is inclusive.) This lets a caller tile adjacent ranges without overlap or gaps.
- min == nil means "from the start of the namespace"; max == nil means "to the end of the namespace". A non-nil but empty max ([]byte{}) is a genuine upper bound and yields an empty result, since no key sorts before it.
- limit <= 0 means unlimited; otherwise at most limit pairs are returned, taken from the START of the ascending order.
- keys are returned in ascending bytes.Compare(k) order, and values[i] corresponds to keys[i].
- an empty result is (nil, nil, nil) -- NOT ErrNotExist. A namespace that does not exist is also an empty result, not an error. Callers scan ranges that are legitimately empty, so forcing them to distinguish "empty" from "missing" is a bug factory.
- if min >= max (with max != nil) the result is empty.
- returned slices are copies owned by the caller.
type KvVersioned ¶
type KvWithVersion ¶
type KvWithVersion struct {
// contains filtered or unexported fields
}
KvWithVersion wraps the versioned DB implementation with a certain version
func NewKVStoreWithVersion ¶ added in v2.2.0
func NewKVStoreWithVersion(cfg Config, opts ...Option) *KvWithVersion
NewKVStoreWithVersion implements a KVStore that can handle both versioned and non-versioned namespace
func (*KvWithVersion) Delete ¶
func (b *KvWithVersion) Delete(ns string, key []byte) error
Delete deletes a key
func (*KvWithVersion) Filter ¶ added in v2.2.0
func (b *KvWithVersion) Filter(ns string, cond Condition, minKey, maxKey []byte) ([][]byte, [][]byte, error)
Filter returns <k, v> pair in a bucket that meet the condition
func (*KvWithVersion) Get ¶
func (b *KvWithVersion) Get(ns string, key []byte) ([]byte, error)
Get retrieves a key's value
func (*KvWithVersion) Put ¶
func (b *KvWithVersion) Put(ns string, key, value []byte) error
Put writes a <key, value> record
func (*KvWithVersion) SetVersion ¶ added in v2.2.0
func (b *KvWithVersion) SetVersion(v uint64) KVStore
SetVersion sets the version, and returns a KVStore to call Put()/Get()
func (*KvWithVersion) Start ¶
func (b *KvWithVersion) Start(ctx context.Context) error
Start starts the DB
func (*KvWithVersion) Stop ¶
func (b *KvWithVersion) Stop(ctx context.Context) error
Stop stops the DB
func (*KvWithVersion) Version ¶ added in v2.2.0
func (b *KvWithVersion) Version(ns string, key []byte) (uint64, error)
Version returns the key's most recent version
func (*KvWithVersion) WriteBatch ¶ added in v2.2.0
func (b *KvWithVersion) WriteBatch(kvsb batch.KVStoreBatch) error
WriteBatch commits a batch
type MockKVStore ¶
type MockKVStore struct {
// contains filtered or unexported fields
}
MockKVStore is a mock of KVStore interface.
func NewMockKVStore ¶
func NewMockKVStore(ctrl *gomock.Controller) *MockKVStore
NewMockKVStore creates a new mock instance.
func (*MockKVStore) Delete ¶
func (m *MockKVStore) Delete(arg0 string, arg1 []byte) error
Delete mocks base method.
func (*MockKVStore) EXPECT ¶
func (m *MockKVStore) EXPECT() *MockKVStoreMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockKVStore) Filter ¶
func (m *MockKVStore) Filter(arg0 string, arg1 Condition, arg2, arg3 []byte) ([][]byte, [][]byte, error)
Filter mocks base method.
func (*MockKVStore) Get ¶
func (m *MockKVStore) Get(arg0 string, arg1 []byte) ([]byte, error)
Get mocks base method.
func (*MockKVStore) Put ¶
func (m *MockKVStore) Put(arg0 string, arg1, arg2 []byte) error
Put mocks base method.
func (*MockKVStore) Start ¶
func (m *MockKVStore) Start(arg0 context.Context) error
Start mocks base method.
func (*MockKVStore) Stop ¶
func (m *MockKVStore) Stop(arg0 context.Context) error
Stop mocks base method.
func (*MockKVStore) WriteBatch ¶
func (m *MockKVStore) WriteBatch(arg0 batch.KVStoreBatch) error
WriteBatch mocks base method.
type MockKVStoreBasic ¶
type MockKVStoreBasic struct {
// contains filtered or unexported fields
}
MockKVStoreBasic is a mock of KVStoreBasic interface.
func NewMockKVStoreBasic ¶
func NewMockKVStoreBasic(ctrl *gomock.Controller) *MockKVStoreBasic
NewMockKVStoreBasic creates a new mock instance.
func (*MockKVStoreBasic) Delete ¶
func (m *MockKVStoreBasic) Delete(arg0 string, arg1 []byte) error
Delete mocks base method.
func (*MockKVStoreBasic) EXPECT ¶
func (m *MockKVStoreBasic) EXPECT() *MockKVStoreBasicMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockKVStoreBasic) Get ¶
func (m *MockKVStoreBasic) Get(arg0 string, arg1 []byte) ([]byte, error)
Get mocks base method.
func (*MockKVStoreBasic) Put ¶
func (m *MockKVStoreBasic) Put(arg0 string, arg1, arg2 []byte) error
Put mocks base method.
type MockKVStoreBasicMockRecorder ¶
type MockKVStoreBasicMockRecorder struct {
// contains filtered or unexported fields
}
MockKVStoreBasicMockRecorder is the mock recorder for MockKVStoreBasic.
func (*MockKVStoreBasicMockRecorder) Delete ¶
func (mr *MockKVStoreBasicMockRecorder) Delete(arg0, arg1 any) *gomock.Call
Delete indicates an expected call of Delete.
func (*MockKVStoreBasicMockRecorder) Get ¶
func (mr *MockKVStoreBasicMockRecorder) Get(arg0, arg1 any) *gomock.Call
Get indicates an expected call of Get.
func (*MockKVStoreBasicMockRecorder) Put ¶
func (mr *MockKVStoreBasicMockRecorder) Put(arg0, arg1, arg2 any) *gomock.Call
Put indicates an expected call of Put.
type MockKVStoreForRangeIndex ¶
type MockKVStoreForRangeIndex struct {
// contains filtered or unexported fields
}
MockKVStoreForRangeIndex is a mock of KVStoreForRangeIndex interface.
func NewMockKVStoreForRangeIndex ¶
func NewMockKVStoreForRangeIndex(ctrl *gomock.Controller) *MockKVStoreForRangeIndex
NewMockKVStoreForRangeIndex creates a new mock instance.
func (*MockKVStoreForRangeIndex) Delete ¶
func (m *MockKVStoreForRangeIndex) Delete(arg0 string, arg1 []byte) error
Delete mocks base method.
func (*MockKVStoreForRangeIndex) EXPECT ¶
func (m *MockKVStoreForRangeIndex) EXPECT() *MockKVStoreForRangeIndexMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockKVStoreForRangeIndex) Filter ¶
func (m *MockKVStoreForRangeIndex) Filter(arg0 string, arg1 Condition, arg2, arg3 []byte) ([][]byte, [][]byte, error)
Filter mocks base method.
func (*MockKVStoreForRangeIndex) Get ¶
func (m *MockKVStoreForRangeIndex) Get(arg0 string, arg1 []byte) ([]byte, error)
Get mocks base method.
func (*MockKVStoreForRangeIndex) GetBucketByPrefix ¶
func (m *MockKVStoreForRangeIndex) GetBucketByPrefix(arg0 []byte) ([][]byte, error)
GetBucketByPrefix mocks base method.
func (*MockKVStoreForRangeIndex) GetKeyByPrefix ¶
func (m *MockKVStoreForRangeIndex) GetKeyByPrefix(namespace, prefix []byte) ([][]byte, error)
GetKeyByPrefix mocks base method.
func (*MockKVStoreForRangeIndex) Insert ¶
func (m *MockKVStoreForRangeIndex) Insert(arg0 []byte, arg1 uint64, arg2 []byte) error
Insert mocks base method.
func (*MockKVStoreForRangeIndex) Purge ¶
func (m *MockKVStoreForRangeIndex) Purge(arg0 []byte, arg1 uint64) error
Purge mocks base method.
func (*MockKVStoreForRangeIndex) Put ¶
func (m *MockKVStoreForRangeIndex) Put(arg0 string, arg1, arg2 []byte) error
Put mocks base method.
func (*MockKVStoreForRangeIndex) Remove ¶
func (m *MockKVStoreForRangeIndex) Remove(arg0 []byte, arg1 uint64) error
Remove mocks base method.
func (*MockKVStoreForRangeIndex) SeekNext ¶
func (m *MockKVStoreForRangeIndex) SeekNext(arg0 []byte, arg1 uint64) ([]byte, error)
SeekNext mocks base method.
func (*MockKVStoreForRangeIndex) SeekPrev ¶
func (m *MockKVStoreForRangeIndex) SeekPrev(arg0 []byte, arg1 uint64) ([]byte, error)
SeekPrev mocks base method.
func (*MockKVStoreForRangeIndex) Start ¶
func (m *MockKVStoreForRangeIndex) Start(arg0 context.Context) error
Start mocks base method.
func (*MockKVStoreForRangeIndex) Stop ¶
func (m *MockKVStoreForRangeIndex) Stop(arg0 context.Context) error
Stop mocks base method.
func (*MockKVStoreForRangeIndex) WriteBatch ¶
func (m *MockKVStoreForRangeIndex) WriteBatch(arg0 batch.KVStoreBatch) error
WriteBatch mocks base method.
type MockKVStoreForRangeIndexMockRecorder ¶
type MockKVStoreForRangeIndexMockRecorder struct {
// contains filtered or unexported fields
}
MockKVStoreForRangeIndexMockRecorder is the mock recorder for MockKVStoreForRangeIndex.
func (*MockKVStoreForRangeIndexMockRecorder) Delete ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) Delete(arg0, arg1 any) *gomock.Call
Delete indicates an expected call of Delete.
func (*MockKVStoreForRangeIndexMockRecorder) Filter ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) Filter(arg0, arg1, arg2, arg3 any) *gomock.Call
Filter indicates an expected call of Filter.
func (*MockKVStoreForRangeIndexMockRecorder) Get ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) Get(arg0, arg1 any) *gomock.Call
Get indicates an expected call of Get.
func (*MockKVStoreForRangeIndexMockRecorder) GetBucketByPrefix ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) GetBucketByPrefix(arg0 any) *gomock.Call
GetBucketByPrefix indicates an expected call of GetBucketByPrefix.
func (*MockKVStoreForRangeIndexMockRecorder) GetKeyByPrefix ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) GetKeyByPrefix(namespace, prefix any) *gomock.Call
GetKeyByPrefix indicates an expected call of GetKeyByPrefix.
func (*MockKVStoreForRangeIndexMockRecorder) Insert ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) Insert(arg0, arg1, arg2 any) *gomock.Call
Insert indicates an expected call of Insert.
func (*MockKVStoreForRangeIndexMockRecorder) Purge ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) Purge(arg0, arg1 any) *gomock.Call
Purge indicates an expected call of Purge.
func (*MockKVStoreForRangeIndexMockRecorder) Put ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) Put(arg0, arg1, arg2 any) *gomock.Call
Put indicates an expected call of Put.
func (*MockKVStoreForRangeIndexMockRecorder) Remove ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) Remove(arg0, arg1 any) *gomock.Call
Remove indicates an expected call of Remove.
func (*MockKVStoreForRangeIndexMockRecorder) SeekNext ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) SeekNext(arg0, arg1 any) *gomock.Call
SeekNext indicates an expected call of SeekNext.
func (*MockKVStoreForRangeIndexMockRecorder) SeekPrev ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) SeekPrev(arg0, arg1 any) *gomock.Call
SeekPrev indicates an expected call of SeekPrev.
func (*MockKVStoreForRangeIndexMockRecorder) Start ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) Start(arg0 any) *gomock.Call
Start indicates an expected call of Start.
func (*MockKVStoreForRangeIndexMockRecorder) Stop ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) Stop(arg0 any) *gomock.Call
Stop indicates an expected call of Stop.
func (*MockKVStoreForRangeIndexMockRecorder) WriteBatch ¶
func (mr *MockKVStoreForRangeIndexMockRecorder) WriteBatch(arg0 any) *gomock.Call
WriteBatch indicates an expected call of WriteBatch.
type MockKVStoreMockRecorder ¶
type MockKVStoreMockRecorder struct {
// contains filtered or unexported fields
}
MockKVStoreMockRecorder is the mock recorder for MockKVStore.
func (*MockKVStoreMockRecorder) Delete ¶
func (mr *MockKVStoreMockRecorder) Delete(arg0, arg1 any) *gomock.Call
Delete indicates an expected call of Delete.
func (*MockKVStoreMockRecorder) Filter ¶
func (mr *MockKVStoreMockRecorder) Filter(arg0, arg1, arg2, arg3 any) *gomock.Call
Filter indicates an expected call of Filter.
func (*MockKVStoreMockRecorder) Get ¶
func (mr *MockKVStoreMockRecorder) Get(arg0, arg1 any) *gomock.Call
Get indicates an expected call of Get.
func (*MockKVStoreMockRecorder) Put ¶
func (mr *MockKVStoreMockRecorder) Put(arg0, arg1, arg2 any) *gomock.Call
Put indicates an expected call of Put.
func (*MockKVStoreMockRecorder) Start ¶
func (mr *MockKVStoreMockRecorder) Start(arg0 any) *gomock.Call
Start indicates an expected call of Start.
func (*MockKVStoreMockRecorder) Stop ¶
func (mr *MockKVStoreMockRecorder) Stop(arg0 any) *gomock.Call
Stop indicates an expected call of Stop.
func (*MockKVStoreMockRecorder) WriteBatch ¶
func (mr *MockKVStoreMockRecorder) WriteBatch(arg0 any) *gomock.Call
WriteBatch indicates an expected call of WriteBatch.
type MockKVStoreWithRange ¶
type MockKVStoreWithRange struct {
// contains filtered or unexported fields
}
MockKVStoreWithRange is a mock of KVStoreWithRange interface.
func NewMockKVStoreWithRange ¶
func NewMockKVStoreWithRange(ctrl *gomock.Controller) *MockKVStoreWithRange
NewMockKVStoreWithRange creates a new mock instance.
func (*MockKVStoreWithRange) Delete ¶
func (m *MockKVStoreWithRange) Delete(arg0 string, arg1 []byte) error
Delete mocks base method.
func (*MockKVStoreWithRange) EXPECT ¶
func (m *MockKVStoreWithRange) EXPECT() *MockKVStoreWithRangeMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockKVStoreWithRange) Filter ¶
func (m *MockKVStoreWithRange) Filter(arg0 string, arg1 Condition, arg2, arg3 []byte) ([][]byte, [][]byte, error)
Filter mocks base method.
func (*MockKVStoreWithRange) Get ¶
func (m *MockKVStoreWithRange) Get(arg0 string, arg1 []byte) ([]byte, error)
Get mocks base method.
func (*MockKVStoreWithRange) Put ¶
func (m *MockKVStoreWithRange) Put(arg0 string, arg1, arg2 []byte) error
Put mocks base method.
func (*MockKVStoreWithRange) Start ¶
func (m *MockKVStoreWithRange) Start(arg0 context.Context) error
Start mocks base method.
func (*MockKVStoreWithRange) Stop ¶
func (m *MockKVStoreWithRange) Stop(arg0 context.Context) error
Stop mocks base method.
func (*MockKVStoreWithRange) WriteBatch ¶
func (m *MockKVStoreWithRange) WriteBatch(arg0 batch.KVStoreBatch) error
WriteBatch mocks base method.
type MockKVStoreWithRangeMockRecorder ¶
type MockKVStoreWithRangeMockRecorder struct {
// contains filtered or unexported fields
}
MockKVStoreWithRangeMockRecorder is the mock recorder for MockKVStoreWithRange.
func (*MockKVStoreWithRangeMockRecorder) Delete ¶
func (mr *MockKVStoreWithRangeMockRecorder) Delete(arg0, arg1 any) *gomock.Call
Delete indicates an expected call of Delete.
func (*MockKVStoreWithRangeMockRecorder) Filter ¶
func (mr *MockKVStoreWithRangeMockRecorder) Filter(arg0, arg1, arg2, arg3 any) *gomock.Call
Filter indicates an expected call of Filter.
func (*MockKVStoreWithRangeMockRecorder) Get ¶
func (mr *MockKVStoreWithRangeMockRecorder) Get(arg0, arg1 any) *gomock.Call
Get indicates an expected call of Get.
func (*MockKVStoreWithRangeMockRecorder) Put ¶
func (mr *MockKVStoreWithRangeMockRecorder) Put(arg0, arg1, arg2 any) *gomock.Call
Put indicates an expected call of Put.
func (*MockKVStoreWithRangeMockRecorder) Range ¶
func (mr *MockKVStoreWithRangeMockRecorder) Range(arg0, arg1, arg2 any) *gomock.Call
Range indicates an expected call of Range.
func (*MockKVStoreWithRangeMockRecorder) Start ¶
func (mr *MockKVStoreWithRangeMockRecorder) Start(arg0 any) *gomock.Call
Start indicates an expected call of Start.
func (*MockKVStoreWithRangeMockRecorder) Stop ¶
func (mr *MockKVStoreWithRangeMockRecorder) Stop(arg0 any) *gomock.Call
Stop indicates an expected call of Stop.
func (*MockKVStoreWithRangeMockRecorder) WriteBatch ¶
func (mr *MockKVStoreWithRangeMockRecorder) WriteBatch(arg0 any) *gomock.Call
WriteBatch indicates an expected call of WriteBatch.
type Namespace ¶ added in v2.2.0
type Namespace struct {
// contains filtered or unexported fields
}
Namespace specifies the name and key length of the versioned namespace
type Option ¶
type Option func(*KvWithVersion)
Option sets an option
func VersionedNamespaceOption ¶ added in v2.2.0
VersionedNamespaceOption pass in versioned namespaces
type PebbleDB ¶
PebbleDB is KVStore implementation based on pebble DB
func NewPebbleDB ¶
NewPebbleDB creates a new PebbleDB instance
func (*PebbleDB) Filter ¶
func (b *PebbleDB) Filter(ns string, cond Condition, minKey []byte, maxKey []byte) (keys [][]byte, vals [][]byte, err error)
Filter returns <k, v> pair in a bucket that meet the condition
func (*PebbleDB) ScanRange ¶
ScanRange returns up to limit <k, v> pairs in [min, max), ascending by bytes.Compare(k). See KVStoreWithRangeScan for the exact semantics, which must stay identical across engines.
This deliberately does NOT use SeekPrefixGE (which Filter() uses): prefix iteration is entangled with the Comparer.Split / bloom-filter configuration and its stopping rule is implicit. An explicitly bounded iterator is auditable and engine-independent.
func (*PebbleDB) WriteBatch ¶
func (b *PebbleDB) WriteBatch(kvsb batch.KVStoreBatch) error
WriteBatch commits a batch
type RangeIndex ¶
type RangeIndex interface {
// Insert inserts a value into the index
Insert(uint64, []byte) error
// Get returns value by the key
Get(uint64) ([]byte, error)
// Delete deletes an existing key
Delete(uint64) error
// Purge deletes an existing key and all keys before it
Purge(uint64) error
// Close makes the index not usable
Close()
}
RangeIndex is a bucket of sparse <k, v> pair, where k consists of 8-byte value and all keys that falls in 2 consecutive k have the same v for example, given 3 entries in the bucket:
k = 0x0000000000000004 ==> v1 k = 0x0000000000000123 ==> v2 k = 0x0000000000005678 ==> v3
we have: for all key 0x0 <= k < 0x4, value[k] = initial value for all key 0x4 <= k < 0x123, value[k] = v1 for all key 0x123 <= k < 0x5678, value[k] = v2 for all key k >= 0x5678, value[k] = v3
func NewRangeIndex ¶
func NewRangeIndex(kv KVStore, name, init []byte) (RangeIndex, error)
NewRangeIndex creates a new instance of rangeIndex
type RangeScan ¶
RangeScan is an ordered, bounded range-scan request. A nil *RangeScan means "no range scan requested", which every caller must treat as "keep doing exactly what you did before".
The fields carry the same semantics as the ScanRange arguments they end up in: Min == nil is the start of the namespace, Max == nil is the end, the interval is half-open [Min, Max), and Limit <= 0 is unlimited.
type VersionedDB ¶ added in v2.2.0
type VersionedDB interface {
lifecycle.StartStopper
// Put insert or update a record identified by (namespace, key)
Put(uint64, string, []byte, []byte) error
// Get gets a record by (namespace, key)
Get(uint64, string, []byte) ([]byte, error)
// Delete deletes a record by (namespace, key)
Delete(uint64, string, []byte) error
// CommitBatch writes a batch to the underlying DB
CommitBatch(uint64, batch.KVStoreBatch) error
// Filter returns <k, v> pair in a bucket that meet the condition
Filter(uint64, string, Condition, []byte, []byte) ([][]byte, [][]byte, error)
// Version returns the key's most recent version
Version(string, []byte) (uint64, error)
}