batch

package
v1.10.1-rc0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// 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")
	// ErrNotExist indicates certain item does not exist in Blockchain database
	ErrNotExist = errors.New("not exist in DB")
	// ErrOutOfBound indicates an out of bound error
	ErrOutOfBound = errors.New("out of bound")
	// ErrUnexpectedType indicates an invalid casting
	ErrUnexpectedType = errors.New("unexpected type")
)

Functions

This section is empty.

Types

type CachedBatch

type CachedBatch interface {
	KVStoreBatch
	Snapshot
	// Get gets a record by (namespace, key)
	Get(string, []byte) ([]byte, error)
}

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

func NewCachedBatch

func NewCachedBatch() CachedBatch

NewCachedBatch returns a new cached batch buffer

type KVStoreBatch

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)
	// Delete deletes a record by (namespace, key)
	Delete(string, []byte, string)
	// Size returns the size of batch
	Size() int
	// Entry returns the entry at the index
	Entry(int) (*WriteInfo, error)
	// SerializeQueue serialize the writes in queue
	SerializeQueue(WriteInfoSerialize, WriteInfoFilter) []byte
	// Clear clears entries staged in batch
	Clear()
	// Translate clones the batch
	Translate(WriteInfoTranslate) KVStoreBatch
	// CheckFillPercent
	CheckFillPercent(string) (float64, bool)
	// AddFillPercent
	AddFillPercent(string, float64)
}

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 WriteBatch() to persist to underlying DB KVStore.WriteBatch(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

func NewBatch() KVStoreBatch

NewBatch returns a batch

type KVStoreCache

type KVStoreCache interface {
	// Read retrieves a record
	Read(*kvCacheKey) ([]byte, error)
	// Write puts a record into cache
	Write(*kvCacheKey, []byte)
	// WriteIfNotExist puts a record into cache only if it doesn't exist, otherwise return ErrAlreadyExist
	WriteIfNotExist(*kvCacheKey, []byte) error
	// Evict deletes a record from cache
	Evict(*kvCacheKey)
	// Clear clear the cache
	Clear()
	// Append appends caches
	Append(...KVStoreCache) error
}

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

func NewKVCache

func NewKVCache() KVStoreCache

NewKVCache returns a KVCache

type Snapshot added in v1.8.0

type Snapshot interface {
	// Snapshot takes a snapshot of current cached batch
	Snapshot() int
	// RevertSnapshot sets the cached batch to the state at the given snapshot
	RevertSnapshot(int) error
	// ResetSnapshots() clears all snapshots
	ResetSnapshots()
}

Snapshot defines an interface which supports snapshot related functions

type WriteInfo

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

WriteInfo is the struct to store Put/Delete operation info

func NewWriteInfo

func NewWriteInfo(
	writeType WriteType,
	namespace string,
	key,
	value []byte,
	errorMessage string,
) *WriteInfo

NewWriteInfo creates a new write info

func (*WriteInfo) Error added in v1.8.4

func (wi *WriteInfo) Error() string

Error returns the error Message

func (*WriteInfo) Key

func (wi *WriteInfo) Key() []byte

Key returns a copy of key

func (*WriteInfo) Namespace

func (wi *WriteInfo) Namespace() string

Namespace returns the namespace of a write info

func (*WriteInfo) Serialize

func (wi *WriteInfo) Serialize() []byte

Serialize serializes the write info

func (*WriteInfo) SerializeWithoutWriteType

func (wi *WriteInfo) SerializeWithoutWriteType() []byte

SerializeWithoutWriteType serializes the write info without write type

func (*WriteInfo) Value

func (wi *WriteInfo) Value() []byte

Value returns a copy of value

func (*WriteInfo) WriteType

func (wi *WriteInfo) WriteType() WriteType

WriteType returns the type of a write info

type WriteInfoFilter

type WriteInfoFilter func(wi *WriteInfo) bool

WriteInfoFilter filters a write

type WriteInfoSerialize

type WriteInfoSerialize func(wi *WriteInfo) []byte

WriteInfoSerialize serializes a write to bytes

type WriteInfoTranslate

type WriteInfoTranslate func(wi *WriteInfo) *WriteInfo

WriteInfoTranslate translates a write info

type WriteType

type WriteType uint8

WriteType is the type of write

const (
	// Put indicate the type of write operation to be Put
	Put WriteType = iota
	// Delete indicate the type of write operation to be Delete
	Delete
)

Jump to

Keyboard shortcuts

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