db

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2019 License: Apache-2.0 Imports: 10 Imported by: 16

Documentation

Index

Constants

View Source
const (
	// Put indicate the type of write operation to be Put
	Put int32 = iota
	// Delete indicate the type of write operation to be Delete
	Delete int32 = 1
)

Variables

View Source
var (
	// ZeroIndex is 8-byte of 0
	ZeroIndex = byteutil.Uint64ToBytesBigEndian(0)
	// ErrInvalid indicates an invalid input
	ErrInvalid = errors.New("invalid input")
)
View Source
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")
	// ErrAlreadyDeleted indicates the key has been deleted
	ErrAlreadyDeleted = errors.New("already deleted from DB")
	// ErrAlreadyExist indicates certain item already exists in Blockchain database
	ErrAlreadyExist = errors.New("already exist in DB")
	// ErrIO indicates the generic error of DB I/O operation
	ErrIO = errors.New("DB I/O operation error")
)

Functions

This section is empty.

Types

type CachedBatch added in v0.4.0

type CachedBatch interface {
	KVStoreBatch
	// Get gets a record by (namespace, key)
	Get(string, []byte) ([]byte, error)
	// Snapshot takes a snapshot of current cached batch
	Snapshot() int
	// Revert sets the cached batch to the state at the given snapshot
	Revert(int) error
	// Digest of the cached batch
	Digest() hash.Hash256
	// contains filtered or unexported methods
}

CachedBatch derives from Batch interface A local cache is added to provide fast retrieval of pending Put/Delete entries

func NewCachedBatch added in v0.4.0

func NewCachedBatch() CachedBatch

NewCachedBatch returns a new cached batch buffer

type CountingIndex added in v0.9.0

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
}

CountingIndex is a bucket of <k, v> where k consists of 8-byte whose value increments (0, 1, 2 ... n) upon each insertion position 0 (k = 0x0000000000000000) stores the total number of items in bucket so far

func NewCountingIndex added in v0.9.0

func NewCountingIndex(db *bolt.DB, retry uint8, name []byte, size uint64) (CountingIndex, error)

NewCountingIndex creates a new instance of countingIndex

func NewInMemCountingIndex added in v0.9.0

func NewInMemCountingIndex(mem *memKVStore, name []byte, size uint64) (CountingIndex, error)

NewInMemCountingIndex creates a new instance of memCountingIndex

type KVStore

type KVStore 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
	// Commit commits a batch
	Commit(KVStoreBatch) error
	// CountingIndex returns the index, and nil if not exist
	CountingIndex([]byte) (CountingIndex, error)
	// CreateCountingIndexNX creates a new index if it does not exist, otherwise return existing index
	CreateCountingIndexNX([]byte) (CountingIndex, error)
}

KVStore is the interface of KV store.

func NewBoltDB

func NewBoltDB(cfg config.DB) KVStore

NewBoltDB instantiates an BoltDB with implements KVStore

func NewMemKVStore

func NewMemKVStore() KVStore

NewMemKVStore instantiates an in-memory KV store

type KVStoreBatch added in v0.3.0

type KVStoreBatch interface {
	// Lock locks the batch
	Lock()
	// Unlock unlocks the batch
	Unlock()
	// ClearAndUnlock clears the write queue and unlocks the batch
	ClearAndUnlock()
	// Put insert or update a record identified by (namespace, key)
	Put(string, []byte, []byte, string, ...interface{})
	// Delete deletes a record by (namespace, key)
	Delete(string, []byte, string, ...interface{})
	// Size returns the size of batch
	Size() int
	// Entry returns the entry at the index
	Entry(int) (*writeInfo, error)
	// Clear clears entries staged in batch
	Clear()
	// CloneBatch clones the batch
	CloneBatch() KVStoreBatch
	// contains filtered or unexported methods
}

KVStoreBatch defines a batch buffer interface that stages Put/Delete entries in sequential order To use it, first start a new batch b := NewBatch() and keep batching Put/Delete operation into it b.Put(bucket, k, v) b.Delete(bucket, k, v) once it's done, call KVStore interface's Commit() to persist to underlying DB KVStore.Commit(b) if commit succeeds, the batch is cleared otherwise the batch is kept intact (so batch user can figure out what’s wrong and attempt re-commit later)

func NewBatch added in v0.4.0

func NewBatch() KVStoreBatch

NewBatch returns a batch

type KVStoreCache added in v0.4.4

type KVStoreCache interface {
	// Read retrieves a record
	Read(hash160 hash.Hash160) ([]byte, error)
	// Write puts a record into cache
	Write(hash.Hash160, []byte)
	// WriteIfNotExist puts a record into cache only if it doesn't exist, otherwise return ErrAlreadyExist
	WriteIfNotExist(hash.Hash160, []byte) error
	// Evict deletes a record from cache
	Evict(hash.Hash160)
	// Clear clear the cache
	Clear()
	// Clone clones the cache
	Clone() KVStoreCache
}

KVStoreCache is a local cache of batched <k, v> for fast query

func NewKVCache added in v0.4.4

func NewKVCache() KVStoreCache

NewKVCache returns a KVCache

type KVStoreForTrie added in v0.4.4

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

KVStoreForTrie defines a kvstore with fixed bucket and cache layer for trie. It may be used in other cases as well

func NewKVStoreForTrie added in v0.4.4

func NewKVStoreForTrie(bucket string, dao KVStore, options ...Option) (*KVStoreForTrie, error)

NewKVStoreForTrie creates a new KVStoreForTrie

func (*KVStoreForTrie) Delete added in v0.4.4

func (s *KVStoreForTrie) Delete(key []byte) error

Delete deletes key

func (*KVStoreForTrie) Flush added in v0.4.4

func (s *KVStoreForTrie) Flush() error

Flush flushs the data in cache layer to db

func (*KVStoreForTrie) Get added in v0.4.4

func (s *KVStoreForTrie) Get(key []byte) ([]byte, error)

Get gets value of key

func (*KVStoreForTrie) Put added in v0.4.4

func (s *KVStoreForTrie) Put(key []byte, value []byte) error

Put puts value for key

func (*KVStoreForTrie) Start added in v0.4.4

func (s *KVStoreForTrie) Start(ctx context.Context) error

Start starts the kv store

func (*KVStoreForTrie) Stop added in v0.4.4

func (s *KVStoreForTrie) Stop(ctx context.Context) error

Stop stops the kv store

type Option added in v0.4.4

type Option func(*KVStoreForTrie) error

Option defines an interface to initialize the kv store

func CachedBatchOption added in v0.4.4

func CachedBatchOption(cb CachedBatch) Option

CachedBatchOption defines a way to set the cache layer for db

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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