storage

package
v0.98.2-pre Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2022 License: MIT Imports: 16 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ExecBlock       byte = 1
	ExecTransaction byte = 2
)

Executable subtypes.

View Source
const (
	// MaxStorageKeyLen is the maximum length of a key for storage items.
	MaxStorageKeyLen = 64
	// MaxStorageValueLen is the maximum length of a value for storage items.
	// It is set to be the maximum value for uint16.
	MaxStorageValueLen = 65535
)

Variables

View Source
var Bucket = []byte("DB")

Bucket represents bucket used in boltdb to store all the data.

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

ErrKeyNotFound is an error returned by Store implementations when a certain key is not found.

Functions

func AppendPrefix

func AppendPrefix(k KeyPrefix, b []byte) []byte

AppendPrefix appends byteslice b to the given KeyPrefix. AppendKeyPrefix(SYSVersion, []byte{0x00, 0x01}).

func AppendPrefixInt

func AppendPrefixInt(k KeyPrefix, n int) []byte

AppendPrefixInt append int n to the given KeyPrefix.

AppendPrefixInt(SYSCurrentHeader, 10001)

Types

type Batch

type Batch interface {
	Delete(k []byte)
	Put(k, v []byte)
}

Batch represents an abstraction on top of batch operations. Each Store implementation is responsible of casting a Batch to its appropriate type. Batches can only be used in a single thread.

type BoltDBOptions

type BoltDBOptions struct {
	FilePath string `yaml:"FilePath"`
}

BoltDBOptions configuration for boltdb.

type BoltDBStore

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

BoltDBStore it is the storage implementation for storing and retrieving blockchain data.

func NewBoltDBStore

func NewBoltDBStore(cfg BoltDBOptions) (*BoltDBStore, error)

NewBoltDBStore returns a new ready to use BoltDB storage with created bucket.

func (*BoltDBStore) Batch

func (s *BoltDBStore) Batch() Batch

Batch implements the Batch interface and returns a boltdb compatible Batch.

func (*BoltDBStore) Close

func (s *BoltDBStore) Close() error

Close releases all db resources.

func (*BoltDBStore) Delete

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

Delete implements the Store interface.

func (*BoltDBStore) Get

func (s *BoltDBStore) Get(key []byte) (val []byte, err error)

Get implements the Store interface.

func (*BoltDBStore) Put

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

Put implements the Store interface.

func (*BoltDBStore) PutBatch

func (s *BoltDBStore) PutBatch(batch Batch) error

PutBatch implements the Store interface.

func (*BoltDBStore) PutChangeSet added in v0.97.2

func (s *BoltDBStore) PutChangeSet(puts map[string][]byte, dels map[string]bool) error

PutChangeSet implements the Store interface.

func (*BoltDBStore) Seek

func (s *BoltDBStore) Seek(rng SeekRange, f func(k, v []byte) bool)

Seek implements the Store interface.

type DBConfiguration

type DBConfiguration struct {
	Type           string         `yaml:"Type"`
	LevelDBOptions LevelDBOptions `yaml:"LevelDBOptions"`
	BoltDBOptions  BoltDBOptions  `yaml:"BoltDBOptions"`
}

DBConfiguration describes configuration for DB. Supported: 'levelDB', 'boltDB'.

type KeyPrefix

type KeyPrefix uint8

KeyPrefix is a constant byte added as a prefix for each key stored.

const (
	DataExecutable KeyPrefix = 0x01
	DataMPT        KeyPrefix = 0x03
	STAccount      KeyPrefix = 0x40
	STContractID   KeyPrefix = 0x51
	STStorage      KeyPrefix = 0x70
	// STTempStorage is used to store contract storage items during state sync process
	// in order not to mess up the previous state which has its own items stored by
	// STStorage prefix. Once state exchange process is completed, all items with
	// STStorage prefix will be replaced with STTempStorage-prefixed ones.
	STTempStorage                  KeyPrefix = 0x71
	STNEP11Transfers               KeyPrefix = 0x72
	STNEP17Transfers               KeyPrefix = 0x73
	STTokenTransferInfo            KeyPrefix = 0x74
	IXHeaderHashList               KeyPrefix = 0x80
	SYSCurrentBlock                KeyPrefix = 0xc0
	SYSCurrentHeader               KeyPrefix = 0xc1
	SYSStateSyncCurrentBlockHeight KeyPrefix = 0xc2
	SYSStateSyncPoint              KeyPrefix = 0xc3
	SYSStateJumpStage              KeyPrefix = 0xc4
	SYSCleanStorage                KeyPrefix = 0xc5
	SYSVersion                     KeyPrefix = 0xf0
)

KeyPrefix constants.

func (KeyPrefix) Bytes

func (k KeyPrefix) Bytes() []byte

Bytes returns the bytes representation of KeyPrefix.

type KeyValue

type KeyValue struct {
	Key   []byte
	Value []byte
}

KeyValue represents key-value pair.

type KeyValueExists added in v0.98.0

type KeyValueExists struct {
	KeyValue

	Exists bool
}

KeyValueExists represents key-value pair with indicator whether the item exists in the persistent storage.

type LevelDBOptions

type LevelDBOptions struct {
	DataDirectoryPath string `yaml:"DataDirectoryPath"`
}

LevelDBOptions configuration for LevelDB.

type LevelDBStore

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

LevelDBStore is the official storage implementation for storing and retrieving blockchain data.

func NewLevelDBStore

func NewLevelDBStore(cfg LevelDBOptions) (*LevelDBStore, error)

NewLevelDBStore returns a new LevelDBStore object that will initialize the database found at the given path.

func (*LevelDBStore) Batch

func (s *LevelDBStore) Batch() Batch

Batch implements the Batch interface and returns a leveldb compatible Batch.

func (*LevelDBStore) Close

func (s *LevelDBStore) Close() error

Close implements the Store interface.

func (*LevelDBStore) Delete

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

Delete implements the Store interface.

func (*LevelDBStore) Get

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

Get implements the Store interface.

func (*LevelDBStore) Put

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

Put implements the Store interface.

func (*LevelDBStore) PutBatch

func (s *LevelDBStore) PutBatch(batch Batch) error

PutBatch implements the Store interface.

func (*LevelDBStore) PutChangeSet added in v0.97.2

func (s *LevelDBStore) PutChangeSet(puts map[string][]byte, dels map[string]bool) error

PutChangeSet implements the Store interface.

func (*LevelDBStore) Seek

func (s *LevelDBStore) Seek(rng SeekRange, f func(k, v []byte) bool)

Seek implements the Store interface.

type MemBatch

type MemBatch struct {
	Put     []KeyValueExists
	Deleted []KeyValueExists
}

MemBatch represents a changeset to be persisted.

type MemCachedStore

type MemCachedStore struct {
	MemoryStore
	// contains filtered or unexported fields
}

MemCachedStore is a wrapper around persistent store that caches all changes being made for them to be later flushed in one batch.

func NewMemCachedStore

func NewMemCachedStore(lower Store) *MemCachedStore

NewMemCachedStore creates a new MemCachedStore object.

func (*MemCachedStore) Close

func (s *MemCachedStore) Close() error

Close implements Store interface, clears up memory and closes the lower layer Store.

func (*MemCachedStore) Get

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

Get implements the Store interface.

func (*MemCachedStore) GetBatch

func (s *MemCachedStore) GetBatch() *MemBatch

GetBatch returns currently accumulated changeset.

func (*MemCachedStore) Persist

func (s *MemCachedStore) Persist() (int, error)

Persist flushes all the MemoryStore contents into the (supposedly) persistent store ps. MemCachedStore remains accessible for the most part of this action (any new changes will be cached in memory).

func (*MemCachedStore) PersistSync added in v0.98.0

func (s *MemCachedStore) PersistSync() (int, error)

PersistSync flushes all the MemoryStore contents into the (supposedly) persistent store ps. It's different from Persist in that it blocks MemCachedStore completely while flushing things from memory to persistent store.

func (*MemCachedStore) Seek

func (s *MemCachedStore) Seek(rng SeekRange, f func(k, v []byte) bool)

Seek implements the Store interface.

func (*MemCachedStore) SeekAsync added in v0.98.0

func (s *MemCachedStore) SeekAsync(ctx context.Context, rng SeekRange, cutPrefix bool) chan KeyValue

SeekAsync returns non-buffered channel with matching KeyValue pairs. Key and value slices may not be copied and may be modified. SeekAsync can guarantee that key-value items are sorted by key in ascending way.

type MemoryBatch

type MemoryBatch struct {
	MemoryStore
}

MemoryBatch is an in-memory batch compatible with MemoryStore.

func (*MemoryBatch) Delete

func (b *MemoryBatch) Delete(k []byte)

Delete implements Batch interface.

func (*MemoryBatch) Put

func (b *MemoryBatch) Put(k, v []byte)

Put implements the Batch interface.

type MemoryStore

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

MemoryStore is an in-memory implementation of a Store, mainly used for testing. Do not use MemoryStore in production.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a new MemoryStore object.

func (*MemoryStore) Batch

func (s *MemoryStore) Batch() Batch

Batch implements the Batch interface and returns a compatible Batch.

func (*MemoryStore) Close

func (s *MemoryStore) Close() error

Close implements Store interface and clears up memory. Never returns an error.

func (*MemoryStore) Delete

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

Delete implements Store interface. Never returns an error.

func (*MemoryStore) Get

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

Get implements the Store interface.

func (*MemoryStore) Put

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

Put implements the Store interface. Never returns an error.

func (*MemoryStore) PutBatch

func (s *MemoryStore) PutBatch(batch Batch) error

PutBatch implements the Store interface. Never returns an error.

func (*MemoryStore) PutChangeSet added in v0.97.2

func (s *MemoryStore) PutChangeSet(puts map[string][]byte, dels map[string]bool) error

PutChangeSet implements the Store interface. Never returns an error.

func (*MemoryStore) Seek

func (s *MemoryStore) Seek(rng SeekRange, f func(k, v []byte) bool)

Seek implements the Store interface.

func (*MemoryStore) SeekAll added in v0.92.0

func (s *MemoryStore) SeekAll(key []byte, f func(k, v []byte))

SeekAll is like seek but also iterates over deleted items.

type Operation added in v0.98.1

type Operation struct {
	// State can be Added, Changed or Deleted.
	State string `json:"state"`
	Key   []byte `json:"key"`
	Value []byte `json:"value,omitempty"`
}

Operation represents a single KV operation (add/del/change) performed in the DB.

func BatchToOperations added in v0.98.1

func BatchToOperations(batch *MemBatch) []Operation

BatchToOperations converts a batch of changes into array of Operations.

type SeekRange added in v0.98.1

type SeekRange struct {
	// Prefix denotes the Seek's lookup key.
	// Empty Prefix means seeking through all keys in the DB starting from
	// the Start if specified.
	Prefix []byte
	// Start denotes value appended to the Prefix to start Seek from.
	// Seeking starting from some key includes this key to the result;
	// if no matching key was found then next suitable key is picked up.
	// Start may be empty. Empty Start means seeking through all keys in
	// the DB with matching Prefix.
	// Empty Prefix and empty Start can be combined, which means seeking
	// through all keys in the DB.
	Start []byte
	// Backwards denotes whether Seek direction should be reversed, i.e.
	// whether seeking should be performed in a descending way.
	// Backwards can be safely combined with Prefix and Start.
	Backwards bool
}

SeekRange represents options for Store.Seek operation.

type Store

type Store interface {
	Batch() Batch
	Delete(k []byte) error
	Get([]byte) ([]byte, error)
	Put(k, v []byte) error
	PutBatch(Batch) error
	// PutChangeSet allows to push prepared changeset to the Store.
	PutChangeSet(puts map[string][]byte, dels map[string]bool) error
	// Seek can guarantee that provided key (k) and value (v) are the only valid until the next call to f.
	// Seek continues iteration until false is returned from f.
	// Key and value slices should not be modified.
	// Seek can guarantee that key-value items are sorted by key in ascending way.
	Seek(rng SeekRange, f func(k, v []byte) bool)
	Close() error
}

Store is anything that can persist and retrieve the blockchain. information.

func NewStore

func NewStore(cfg DBConfiguration) (Store, error)

NewStore creates storage with preselected in configuration database type.

Jump to

Keyboard shortcuts

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