statemgmt

package
v0.0.0-...-667e438 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UpdatesNothing = map[string]*pb.UpdatedValue{}

maybe useful for some case

Functions

func AssertIteratorContains

func AssertIteratorContains(t *testing.T, itr RangeScanIterator, expected map[string][]byte)

AssertIteratorContains - tests wether the iterator (itr) contains expected results (provided in map)

func ConstructCompositeKey

func ConstructCompositeKey(chaincodeID string, key string) []byte

ConstructCompositeKey returns a []byte that uniquely represents a given chaincodeID and key. This assumes that chaincodeID does not contain a 0x00 byte, but the key may TODO:enforce this restriction on chaincodeID or use length prefixing here instead of delimiter

func Copy

func Copy(src []byte) []byte

Copy returns a copy of given bytes

func DecodeCompositeKey

func DecodeCompositeKey(compositeKey []byte) (string, string)

DecodeCompositeKey decodes the compositeKey constructed by ConstructCompositeKey method back to the original chaincodeID and key form

func GetRequiredParts

func GetRequiredParts(itr PartialRangeIterator, offset *protos.SyncOffset) (*protos.SyncStateChunk, error)

func PopulateStateForTest

func PopulateStateForTest(t testing.TB, target HashableState, db *db.OpenchainDB, datakeys int)

populate a moderate size of state collection for testing

func StartFullSyncTest

func StartFullSyncTest(t testing.TB, src, target HashAndDividableState, db *db.OpenchainDB)

Types

type DividableSyncState

type DividableSyncState interface {
	HashableState
	InitPartialSync([]byte)
	SyncTarget() []byte
	IsCompleted() bool
	//get all tasks current available, caller is encouraged to CACHE the result,
	//complete and apply all of them, then call RequiredParts again for more
	//tasks (it was all right to call it at anytime but the cost may be remarkable)
	RequiredParts() ([]*protos.SyncOffset, error)
	ApplyPartialSync(*protos.SyncStateChunk) error
}

type HashAndDividableState

type HashAndDividableState interface {
	DividableSyncState
	GetPartialRangeIterator(*db.DBSnapshot) (PartialRangeIterator, error)
}

type HashableState

type HashableState interface {

	// Initialize this gives a chance to initialize. For instance, state implementation can load some data from DB
	Initialize(configs map[string]interface{}) error

	// Get get the value from DB
	Get(chaincodeID string, key string) ([]byte, error)

	// Should replace the original Get
	GetSafe(sn *db.DBSnapshot, chaincodeID string, key string) ([]byte, error)

	// PrepareWorkingSet passes a stateDelta that captures the changes that needs to be applied to the state
	PrepareWorkingSet(stateDelta *StateDelta) error

	// ComputeCryptoHash state implementation to compute crypto-hash of state
	// assuming the stateDelta (passed in PrepareWorkingSet method) is to be applied
	//
	// When state impl. is under syncing status, it should always return the target
	// state hash it will be synced to when no error occurs. And can return undefined
	// value after any syncing error
	ComputeCryptoHash() ([]byte, error)

	// AddChangesForPersistence state implementation to add all the key-value pair that it needs
	// to persist for committing the  stateDelta (passed in PrepareWorkingSet method) to DB.
	// In addition to the information in the StateDelta, the implementation may also want to
	// persist intermediate results for faster crypto-hash computation
	AddChangesForPersistence(writeBatch *db.DBWriteBatch) error

	// ClearWorkingSet state implementation may clear any data structures that it may have constructed
	// for computing cryptoHash and persisting the changes for the stateDelta (passed in PrepareWorkingSet method)
	// NOTICE: the behavior will be UNDEFINED if data is not really persisted but it was call with
	// a true changesPersisted flag
	ClearWorkingSet(changesPersisted bool)

	// GetStateSnapshotIterator state implementation to provide an iterator that is supposed to give
	// All the key-value of global state. A particular implementation may need to remove additional information
	// that the implementation keeps for faster crypto-hash computation. For instance, filter a few of the
	// key-values or remove some data from particular key-values.
	// YA-fabric: this API will be deprecated and we recommend use partial iterator for syncing
	GetStateSnapshotIterator(snapshot *db.DBSnapshot) (StateSnapshotIterator, error)

	// GetRangeScanIterator - state implementation to provide an iterator that is supposed to give
	// All the key-values for a given chaincodeID such that a return key should be lexically greater than or
	// equal to startKey and less than or equal to endKey. If the value for startKey parameter is an empty string
	// startKey is assumed to be the smallest key available in the db for the chaincodeID. Similarly, an empty string
	// for endKey parameter assumes the endKey to be the greatest key available in the db for the chaincodeID
	GetRangeScanIterator(chaincodeID string, startKey string, endKey string) (RangeScanIterator, error)

	// PerfHintKeyChanged state implementation may be provided with some hints before (e.g., during tx execution)
	// the StateDelta is prepared and passed in PrepareWorkingSet method.
	// A state implementation may use this hint for prefetching relevant data so as if this could improve
	// the performance of ComputeCryptoHash method (when gets called at a later time)
	PerfHintKeyChanged(chaincodeID string, key string)
}

HashableState - Interface that is be implemented by state management Different state management implementation can be effiecient for computing crypto-hash for state under different workload conditions.

type PartialRangeIterator

type PartialRangeIterator interface {
	StateSnapshotIterator
	Seek(*protos.SyncOffset) error
	//obtain the metadata for an iteration, that is, a calling after Seek,
	//GetMetaData MUST be called BEFORE any Next() in StateSnapshotIterator
	//is called
	GetMetaData() *protos.SyncMetadata
}

type RangeScanIterator

type RangeScanIterator interface {

	// Next moves to next key-value. Returns true if next key-value exists
	Next() bool

	// GetKeyValue returns next key-value
	GetKeyValue() (string, []byte)

	// Close releases resources occupied by the iterator
	Close()
}

RangeScanIterator - is to be implemented by the return value of GetRangeScanIterator method in the implementation of HashableState interface

type SnapshotState

type SnapshotState interface {
	GetStateSnapshotIterator(*db.DBSnapshot) (StateSnapshotIterator, error)
	GetPartialRangeIterator(*db.DBSnapshot) (PartialRangeIterator, error)
}

we should deprecated the snapshot API in hashablestate and put them here this interface should be global and not related to any instance of stateimpl

type StateDelta

type StateDelta struct {
	ChaincodeStateDeltas map[string]*pb.ChaincodeStateDelta
	// RollBackwards allows one to contol whether this delta will roll the state
	// forwards or backwards.
	RollBackwards bool
}

StateDelta holds the changes to existing state. This struct is used for holding the uncommitted changes during execution of a tx-batch Also, to be used for transferring the state to another peer in chunks

func ConstructRandomStateDelta

func ConstructRandomStateDelta(
	t testing.TB,
	chaincodeIDPrefix string,
	numChaincodes int,
	maxKeySuffix int,
	numKeysToInsert int,
	kvSize int) *StateDelta

ConstructRandomStateDelta creates a random state delta for testing

func GenUpdateStateDelta

func GenUpdateStateDelta(delta map[string]*pb.ChaincodeStateDelta) *StateDelta

func NewStateDelta

func NewStateDelta() *StateDelta

NewStateDelta constructs an empty StateDelta struct

func (*StateDelta) ApplyChanges

func (stateDelta *StateDelta) ApplyChanges(anotherStateDelta *StateDelta)

ApplyChanges merges another delta - if a key is present in both, the value of the existing key is overwritten

func (*StateDelta) ComputeCryptoHash

func (stateDelta *StateDelta) ComputeCryptoHash() []byte

ComputeCryptoHash computes crypto-hash for the data held returns nil if no data is present

func (*StateDelta) Delete

func (stateDelta *StateDelta) Delete(chaincodeID string, key string, previousValue []byte)

Delete deletes a key from the state

func (*StateDelta) Get

func (stateDelta *StateDelta) Get(chaincodeID string, key string) *pb.UpdatedValue

Get get the state from delta if exists

func (*StateDelta) GetUpdatedChaincodeIds

func (stateDelta *StateDelta) GetUpdatedChaincodeIds(sorted bool) []string

GetUpdatedChaincodeIds return the chaincodeIDs that are prepsent in the delta If sorted is true, the method return chaincodeIDs in lexicographical sorted order

func (*StateDelta) GetUpdates

func (stateDelta *StateDelta) GetUpdates(chaincodeID string) map[string]*pb.UpdatedValue

GetUpdates returns changes associated with given chaincodeId

func (*StateDelta) IsEmpty

func (stateDelta *StateDelta) IsEmpty() bool

IsEmpty checks whether StateDelta contains any data

func (*StateDelta) IsUpdatedValueSet

func (stateDelta *StateDelta) IsUpdatedValueSet(chaincodeID, key string) bool

IsUpdatedValueSet returns true if a update value is already set for the given chaincode ID and key.

func (*StateDelta) Marshal

func (stateDelta *StateDelta) Marshal() []byte

Marshal serializes the StateDelta

func (*StateDelta) SafeGetUpdates

func (stateDelta *StateDelta) SafeGetUpdates(chaincodeID string) *pb.ChaincodeStateDelta

func (*StateDelta) Set

func (stateDelta *StateDelta) Set(chaincodeID string, key string, value, previousValue []byte)

Set sets state value for a key

func (*StateDelta) Unmarshal

func (stateDelta *StateDelta) Unmarshal(bytes []byte) error

// Unmarshal deserializes StateDelta

type StateDeltaIterator

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

StateDeltaIterator - An iterator implementation over state-delta

func NewStateDeltaRangeScanIterator

func NewStateDeltaRangeScanIterator(delta *StateDelta, chaincodeID string, startKey string, endKey string) *StateDeltaIterator

NewStateDeltaRangeScanIterator - return an iterator for performing a range scan over a state-delta object

func NewStateDeltaRangeScanIteratorFromRaw

func NewStateDeltaRangeScanIteratorFromRaw(delta *StateDelta, implItr RangeScanIterator, chaincodeID string, startKey string, endKey string) *StateDeltaIterator

func (*StateDeltaIterator) Close

func (itr *StateDeltaIterator) Close()

Close - see interface 'RangeScanIterator' for details

func (*StateDeltaIterator) GetKeyRawValue

func (itr *StateDeltaIterator) GetKeyRawValue() (string, *pb.UpdatedValue)

func (*StateDeltaIterator) GetKeyValue

func (itr *StateDeltaIterator) GetKeyValue() (string, []byte)

GetKeyValue - see interface 'RangeScanIterator' for details

func (*StateDeltaIterator) Next

func (itr *StateDeltaIterator) Next() bool

Next - see interface 'RangeScanIterator' for details

func (*StateDeltaIterator) NextWithDeleted

func (itr *StateDeltaIterator) NextWithDeleted() bool

type StateSnapshotIterator

type StateSnapshotIterator interface {

	// Next moves to next key-value. Returns true if next key-value exists
	Next() bool

	// GetRawKeyValue returns next key-value
	GetRawKeyValue() ([]byte, []byte)

	// Close releases resources occupied by the iterator
	Close()
}

StateSnapshotIterator An interface that is to be implemented by the return value of GetStateSnapshotIterator method in the implementation of HashableState interface

type SyncInProgress

type SyncInProgress interface {
	error
	IsSyncInProgress()
	SyncTarget() []byte
}

An stateimpl supporting DividableSyncState interface may report this error in the calling of Initialize, indicating the initialize is success but the state is under a syncing progress so it was not possible to use it for query or updating

type SyncSimulator

type SyncSimulator struct {
	*db.OpenchainDB

	SyncingOffset *protos.SyncOffset
	SyncingData   *protos.SyncStateChunk
	SyncingError  error
	// contains filtered or unexported fields
}

func NewSyncSimulator

func NewSyncSimulator(db *db.OpenchainDB) *SyncSimulator

func (*SyncSimulator) AttachSource

func (s *SyncSimulator) AttachSource(t PartialRangeIterator)

func (*SyncSimulator) AttachTarget

func (s *SyncSimulator) AttachTarget(t HashAndDividableState)

func (*SyncSimulator) PeekTasks

func (s *SyncSimulator) PeekTasks() []*protos.SyncOffset

func (*SyncSimulator) PullOut

func (s *SyncSimulator) PullOut(onTask ...func()) error

func (*SyncSimulator) Release

func (s *SyncSimulator) Release()

func (*SyncSimulator) Result

func (s *SyncSimulator) Result() error

func (*SyncSimulator) TestSyncEachStep

func (s *SyncSimulator) TestSyncEachStep(onFinish ...func()) (e error)

func (*SyncSimulator) TestSyncEachStep_Applyphase

func (s *SyncSimulator) TestSyncEachStep_Applyphase() *SyncSimulator

func (*SyncSimulator) TestSyncEachStep_Finalphase

func (s *SyncSimulator) TestSyncEachStep_Finalphase()

func (*SyncSimulator) TestSyncEachStep_Pollphase

func (s *SyncSimulator) TestSyncEachStep_Pollphase() *SyncSimulator

func (*SyncSimulator) TestSyncEachStep_Taskphase

func (s *SyncSimulator) TestSyncEachStep_Taskphase() *SyncSimulator

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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