store

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: Apache-2.0 Imports: 12 Imported by: 33

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyNotFound = errors.New("key not found")

ErrKeyNotFound is returned if key is not found in KVStore.

Functions

This section is empty.

Types

type BadgerBatch

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

BadgerBatch encapsulates badger transaction

func (*BadgerBatch) Commit

func (bb *BadgerBatch) Commit() error

Commit commits a transaction

func (*BadgerBatch) Delete

func (bb *BadgerBatch) Delete(key []byte) error

Delete removes the key and associated value from store

func (*BadgerBatch) Discard

func (bb *BadgerBatch) Discard()

Discard cancels a transaction

func (*BadgerBatch) Set

func (bb *BadgerBatch) Set(key, value []byte) error

Set accumulates key-value entries in a transaction

type BadgerIterator

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

BadgerIterator encapsulates prefix iterator for badger kv store.

func (*BadgerIterator) Discard

func (i *BadgerIterator) Discard()

Discard has to be called to free iterator resources.

func (*BadgerIterator) Error

func (i *BadgerIterator) Error() error

Error returns last error that occurred during iteration.

func (*BadgerIterator) Key

func (i *BadgerIterator) Key() []byte

Key returns key pointed by iterator.

func (*BadgerIterator) Next

func (i *BadgerIterator) Next()

Next progresses iterator to the next key-value pair.

func (*BadgerIterator) Valid

func (i *BadgerIterator) Valid() bool

Valid returns true if iterator is inside its prefix, false otherwise.

func (*BadgerIterator) Value

func (i *BadgerIterator) Value() []byte

Value returns value pointer by iterator.

type BadgerKV

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

BadgerKV is a implementation of KVStore using Badger v3.

func (*BadgerKV) Delete

func (b *BadgerKV) Delete(key []byte) error

Delete removes key and corresponding value from store.

func (*BadgerKV) Get

func (b *BadgerKV) Get(key []byte) ([]byte, error)

Get returns value for given key, or error.

func (*BadgerKV) NewBatch

func (b *BadgerKV) NewBatch() Batch

NewBatch creates new batch. Note: badger batches should be short lived as they use extra resources.

func (*BadgerKV) PrefixIterator

func (b *BadgerKV) PrefixIterator(prefix []byte) Iterator

PrefixIterator returns instance of prefix Iterator for BadgerKV.

func (*BadgerKV) Set

func (b *BadgerKV) Set(key []byte, value []byte) error

Set saves key-value mapping in store.

type Batch

type Batch interface {
	Set(key, value []byte) error // Accumulates KV entries in a transaction.
	Delete(key []byte) error     // Deletes the given key.
	Commit() error               // Commits the transaction.
	Discard()                    // Discards the transaction.
}

Batch enables batching of transactions.

type DefaultStore

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

DefaultStore is a default store implmementation.

func (*DefaultStore) Base added in v1.1.0

func (s *DefaultStore) Base() uint64

Base returns height of the earliest block saved in the Store.

func (*DefaultStore) Height

func (s *DefaultStore) Height() uint64

Height returns height of the highest block saved in the Store.

func (*DefaultStore) LoadBlock

func (s *DefaultStore) LoadBlock(height uint64) (*types.Block, error)

LoadBlock returns block at given height, or error if it's not found in Store. TODO(tzdybal): what is more common access pattern? by height or by hash? currently, we're indexing height->hash, and store blocks by hash, but we might as well store by height and index hash->height

func (*DefaultStore) LoadBlockByHash

func (s *DefaultStore) LoadBlockByHash(hash [32]byte) (*types.Block, error)

LoadBlockByHash returns block with given block header hash, or error if it's not found in Store.

func (*DefaultStore) LoadBlockResponses

func (s *DefaultStore) LoadBlockResponses(height uint64) (*tmstate.ABCIResponses, error)

LoadBlockResponses returns block results at given height, or error if it's not found in Store.

func (*DefaultStore) LoadCommit

func (s *DefaultStore) LoadCommit(height uint64) (*types.Commit, error)

LoadCommit returns commit for a block at given height, or error if it's not found in Store.

func (*DefaultStore) LoadCommitByHash

func (s *DefaultStore) LoadCommitByHash(hash [32]byte) (*types.Commit, error)

LoadCommitByHash returns commit for a block with given block header hash, or error if it's not found in Store.

func (*DefaultStore) LoadState

func (s *DefaultStore) LoadState() (types.State, error)

LoadState returns last state saved with UpdateState.

func (*DefaultStore) LoadValidators

func (s *DefaultStore) LoadValidators(height uint64) (*tmtypes.ValidatorSet, error)

LoadValidators loads validator set at given block height from store.

func (*DefaultStore) NewBatch

func (s *DefaultStore) NewBatch() Batch

NewBatch creates a new db batch.

func (*DefaultStore) NextHeight added in v1.1.0

func (s *DefaultStore) NextHeight() uint64

NextHeight returns the next height that expected to be stored in store.

func (*DefaultStore) PruneBlocks added in v1.1.0

func (s *DefaultStore) PruneBlocks(heightInt int64) (uint64, error)

PruneBlocks removes block up to (but not including) a height. It returns number of blocks pruned.

func (*DefaultStore) SaveBlock

func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit, batch Batch) (Batch, error)

SaveBlock adds block to the store along with corresponding commit. Stored height is updated if block height is greater than stored value. In case a batch is provided, the block and commit are added to the batch and not saved.

func (*DefaultStore) SaveBlockResponses

func (s *DefaultStore) SaveBlockResponses(height uint64, responses *tmstate.ABCIResponses, batch Batch) (Batch, error)

SaveBlockResponses saves block responses (events, tx responses, validator set updates, etc) in Store.

func (*DefaultStore) SaveValidators

func (s *DefaultStore) SaveValidators(height uint64, validatorSet *tmtypes.ValidatorSet, batch Batch) (Batch, error)

SaveValidators stores validator set for given block height in store.

func (*DefaultStore) SetBase added in v1.1.0

func (s *DefaultStore) SetBase(height uint64) bool

SetBase sets the base height if it is higher than the existing base height returns OK if the value was updated successfully or did not need to be updated

func (*DefaultStore) SetHeight

func (s *DefaultStore) SetHeight(height uint64) bool

SetHeight sets the height saved in the Store if it is higher than the existing height returns OK if the value was updated successfully or did not need to be updated

func (*DefaultStore) UpdateState

func (s *DefaultStore) UpdateState(state types.State, batch Batch) (Batch, error)

UpdateState updates state saved in Store. Only one State is stored. If there is no State in Store, state will be saved.

type Iterator

type Iterator interface {
	Valid() bool
	Next()
	Key() []byte
	Value() []byte
	Error() error
	Discard()
}

Iterator enables traversal over a given prefix.

type KVStore

type KVStore interface {
	Get(key []byte) ([]byte, error)        // Get gets the value for a key.
	Set(key []byte, value []byte) error    // Set updates the value for a key.
	Delete(key []byte) error               // Delete deletes a key.
	NewBatch() Batch                       // NewBatch creates a new batch.
	PrefixIterator(prefix []byte) Iterator // PrefixIterator creates iterator to traverse given prefix.
}

KVStore encapsulates key-value store abstraction, in minimalistic interface.

KVStore MUST be thread safe.

func NewDefaultInMemoryKVStore

func NewDefaultInMemoryKVStore() KVStore

NewDefaultInMemoryKVStore builds KVStore that works in-memory (without accessing disk).

func NewDefaultKVStore

func NewDefaultKVStore(rootDir, dbPath, dbName string) KVStore

NewDefaultKVStore creates instance of default key-value store.

type PrefixKV

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

PrefixKV is a key-value store that prepends all keys with given prefix.

func NewPrefixKV

func NewPrefixKV(kv KVStore, prefix []byte) *PrefixKV

NewPrefixKV creates new PrefixKV on top of other KVStore.

func (*PrefixKV) Delete

func (p *PrefixKV) Delete(key []byte) error

Delete deletes key-value pair for given key.

func (*PrefixKV) Get

func (p *PrefixKV) Get(key []byte) ([]byte, error)

Get returns value for given key.

func (*PrefixKV) NewBatch

func (p *PrefixKV) NewBatch() Batch

NewBatch creates a new batch.

func (*PrefixKV) PrefixIterator

func (p *PrefixKV) PrefixIterator(prefix []byte) Iterator

PrefixIterator creates iterator to traverse given prefix.

func (*PrefixKV) Set

func (p *PrefixKV) Set(key []byte, value []byte) error

Set updates the value for given key.

type PrefixKVBatch

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

PrefixKVBatch enables batching of operations on PrefixKV.

func (*PrefixKVBatch) Commit

func (pb *PrefixKVBatch) Commit() error

Commit applies all operations in the batch atomically.

func (*PrefixKVBatch) Delete

func (pb *PrefixKVBatch) Delete(key []byte) error

Delete adds delete operation to batch.

func (*PrefixKVBatch) Discard

func (pb *PrefixKVBatch) Discard()

Discard discards all operations in the batch.

func (*PrefixKVBatch) Set

func (pb *PrefixKVBatch) Set(key, value []byte) error

Set adds key-value pair to batch.

type Store

type Store interface {
	// NewBatch creates a new db batch.
	NewBatch() Batch

	// Height returns height of the highest block in store.
	Height() uint64

	// NextHeight returns the next height that expected to be stored in store.
	NextHeight() uint64

	// SetHeight sets the height saved in the Store if it is higher than the existing height.
	SetHeight(height uint64) bool

	// Base returns height of the lowest block in store.
	Base() uint64

	// SetBase sets the height saved in the Store for the lowest block
	SetBase(height uint64) bool

	// SaveBlock saves block along with its seen commit (which will be included in the next block).
	SaveBlock(block *types.Block, commit *types.Commit, batch Batch) (Batch, error)

	// LoadBlock returns block at given height, or error if it's not found in Store.
	LoadBlock(height uint64) (*types.Block, error)
	// LoadBlockByHash returns block with given block header hash, or error if it's not found in Store.
	LoadBlockByHash(hash [32]byte) (*types.Block, error)

	// SaveBlockResponses saves block responses (events, tx responses, validator set updates, etc) in Store.
	SaveBlockResponses(height uint64, responses *tmstate.ABCIResponses, batch Batch) (Batch, error)

	// LoadBlockResponses returns block results at given height, or error if it's not found in Store.
	LoadBlockResponses(height uint64) (*tmstate.ABCIResponses, error)

	// LoadCommit returns commit for a block at given height, or error if it's not found in Store.
	LoadCommit(height uint64) (*types.Commit, error)
	// LoadCommitByHash returns commit for a block with given block header hash, or error if it's not found in Store.
	LoadCommitByHash(hash [32]byte) (*types.Commit, error)

	// UpdateState updates state saved in Store. Only one State is stored.
	// If there is no State in Store, state will be saved.
	UpdateState(state types.State, batch Batch) (Batch, error)

	// LoadState returns last state saved with UpdateState.
	LoadState() (types.State, error)

	SaveValidators(height uint64, validatorSet *tmtypes.ValidatorSet, batch Batch) (Batch, error)

	LoadValidators(height uint64) (*tmtypes.ValidatorSet, error)

	// Pruning functions
	PruneBlocks(height int64) (uint64, error)
}

Store is minimal interface for storing and retrieving blocks, commits and state.

func New

func New(kv KVStore) Store

New returns new, default store.

Jump to

Keyboard shortcuts

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