collections

package
v0.15.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultSequenceStart uint64 = 1

DefaultSequenceStart is the initial starting number of the Sequence.

Variables

View Source
var ErrNotFound = errors.New("collections: not found")

Functions

This section is empty.

Types

type IndexedMap

type IndexedMap[PK keys.Key, V any, PV interface {
	*V
	Object
}, I IndexersProvider[PK, V]] struct {
	Indexes I // struct that groups together Indexer instances, implements IndexersProvider
	// contains filtered or unexported fields
}

IndexedMap defines a map which is indexed using the IndexersProvider PK defines the primary key of the object V.

func NewIndexedMap

func NewIndexedMap[PK keys.Key, V any, PV interface {
	*V
	Object
}, I IndexersProvider[PK, V]](cdc codec.BinaryCodec, storeKey sdk.StoreKey, namespace uint8, indexers I) IndexedMap[PK, V, PV, I]

NewIndexedMap instantiates a new IndexedMap instance.

func (IndexedMap[PK, V, PV, I]) Delete

func (i IndexedMap[PK, V, PV, I]) Delete(ctx sdk.Context, key PK) error

Delete fetches the object from the Map removes it from the Map then instructs every Indexer to remove the relationships between the object and the associated primary keys.

func (IndexedMap[PK, V, PV, I]) Get

func (i IndexedMap[PK, V, PV, I]) Get(ctx sdk.Context, key PK) (V, error)

Get returns the object V given its primary key PK.

func (IndexedMap[PK, V, PV, I]) GetOr

func (i IndexedMap[PK, V, PV, I]) GetOr(ctx sdk.Context, key PK, def V) V

GetOr returns the object V given its primary key PK, or if the operation fails returns the provided default.

func (IndexedMap[PK, V, PV, I]) Insert

func (i IndexedMap[PK, V, PV, I]) Insert(ctx sdk.Context, key PK, v V)

Insert inserts the object v into the Map using the primary key, then iterates over every registered Indexer and instructs them to create the relationship between the primary key PK and the object v.

func (IndexedMap[PK, V, PV, I]) Iterate

func (i IndexedMap[PK, V, PV, I]) Iterate(ctx sdk.Context, rng keys.Range[PK]) MapIterator[PK, V, PV]

Iterate iterates over the underlying store containing the concrete objects. The range provided filters over the primary keys.

type Indexer

type Indexer[PK keys.Key, V any] interface {
	// Insert is called when the IndexedMap is inserting
	// an object into its state, so the Indexer here
	// creates the relationship between primary key
	// and the fields of the object V.
	Insert(ctx sdk.Context, primaryKey PK, v V)
	// Delete is called when the IndexedMap is removing
	// the object V and hence the relationship between
	// V and its primary keys need to be removed too.
	Delete(ctx sdk.Context, primaryKey PK, v V)
}

Indexer defines an object which given an object V and a primary key PK, creates a relationship between one or multiple fields of the object V with the primary key PK.

type IndexerIterator

type IndexerIterator[IK, PK keys.Key] KeySetIterator[keys.Pair[IK, PK]]

IndexerIterator wraps a KeySetIterator to provide more useful functionalities around index key iteration.

func (IndexerIterator[IK, PK]) Close

func (i IndexerIterator[IK, PK]) Close()

func (IndexerIterator[IK, PK]) FullKey

func (i IndexerIterator[IK, PK]) FullKey() keys.Pair[IK, PK]

FullKey returns the iterator current key composed of both indexing key and primary key.

func (IndexerIterator[IK, PK]) FullKeys

func (i IndexerIterator[IK, PK]) FullKeys() []keys.Pair[IK, PK]

FullKeys fully consumes the iterator and returns the set of joined indexing key and primary key found.

func (IndexerIterator[IK, PK]) Next

func (i IndexerIterator[IK, PK]) Next()

func (IndexerIterator[IK, PK]) PrimaryKey

func (i IndexerIterator[IK, PK]) PrimaryKey() PK

PrimaryKey returns the iterator current primary key

func (IndexerIterator[IK, PK]) PrimaryKeys

func (i IndexerIterator[IK, PK]) PrimaryKeys() []PK

PrimaryKeys fully consumes the iterator and returns the set of primary keys found.

func (IndexerIterator[IK, PK]) Valid

func (i IndexerIterator[IK, PK]) Valid() bool

type IndexersProvider

type IndexersProvider[PK keys.Key, V any] interface {
	// IndexerList provides the list of Indexer contained
	// in the struct.
	IndexerList() []Indexer[PK, V]
}

IndexersProvider is implemented by structs containing a series of Indexer instances.

type Item

type Item[V any, PV interface {
	*V
	Object
}] struct {
	// contains filtered or unexported fields
}

Item represents a state object which will always have one instance of itself saved in the namespace. Examples are:

  • config
  • parameters
  • a sequence

func NewItem

func NewItem[V any, PV interface {
	*V
	Object
}](cdc codec.BinaryCodec, sk sdk.StoreKey, prefix uint8) Item[V, PV]

NewItem instantiates a new Item instance.

func (Item[V, PV]) Get

func (i Item[V, PV]) Get(ctx sdk.Context) (V, error)

Get gets the item V or returns an error.

func (Item[V, PV]) GetOr

func (i Item[V, PV]) GetOr(ctx sdk.Context, def V) V

GetOr either returns the provided default if it's not present in state, or the value found in state.

func (Item[V, PV]) Set

func (i Item[V, PV]) Set(ctx sdk.Context, v V)

Set sets the item value to v.

type KeySet

type KeySet[K keys.Key] Map[K, nilObject, *nilObject]

KeySet wraps the default Map, but is used only for keys.Key presence and ranging functionalities.

func NewKeySet

func NewKeySet[K keys.Key](cdc codec.BinaryCodec, sk sdk.StoreKey, prefix uint8) KeySet[K]

NewKeySet instantiates a new KeySet.

func (KeySet[K]) Delete

func (s KeySet[K]) Delete(ctx sdk.Context, k K)

Delete deletes the key from the set. Does not check if the key exists or not.

func (KeySet[K]) Has

func (s KeySet[K]) Has(ctx sdk.Context, k K) bool

Has reports whether the key K is present or not in the set.

func (KeySet[K]) Insert

func (s KeySet[K]) Insert(ctx sdk.Context, k K)

Insert inserts the key K in the set.

func (KeySet[K]) Iterate

func (s KeySet[K]) Iterate(ctx sdk.Context, r keys.Range[K]) KeySetIterator[K]

Iterate returns a KeySetIterator over the provided keys.Range of keys.

type KeySetIterator

type KeySetIterator[K keys.Key] MapIterator[K, nilObject, *nilObject]

KeySetIterator wraps the default MapIterator, but is used only for keys.Key ranging.

func (KeySetIterator[K]) Close

func (s KeySetIterator[K]) Close()

Close closes the KeySetIterator. No other operation is valid.

func (KeySetIterator[K]) Key

func (s KeySetIterator[K]) Key() K

Key returns the current iterator key.

func (KeySetIterator[K]) Keys

func (s KeySetIterator[K]) Keys() []K

Keys consumes the iterator fully and returns all the available keys. The KeySetIterator is closed after this operation.

func (KeySetIterator[K]) Next

func (s KeySetIterator[K]) Next()

Next moves the iterator onto the next key.

func (KeySetIterator[K]) Valid

func (s KeySetIterator[K]) Valid() bool

Valid checks if the iterator is still valid.

type KeyValue

type KeyValue[K keys.Key, V any, PV interface {
	*V
	Object
}] struct {
	Key   K
	Value V
}

type Map

type Map[K keys.Key, V any, PV interface {
	*V
	Object
}] struct {
	// contains filtered or unexported fields
}

Map defines a collection which simply does mappings between primary keys and objects.

func NewMap

func NewMap[K keys.Key, V any, PV interface {
	*V
	Object
}](cdc codec.BinaryCodec, sk sdk.StoreKey, prefix uint8) Map[K, V, PV]

func (Map[K, V, PV]) Delete

func (m Map[K, V, PV]) Delete(ctx sdk.Context, key K) error

func (Map[K, V, PV]) Get

func (m Map[K, V, PV]) Get(ctx sdk.Context, key K) (V, error)

func (Map[K, V, PV]) GetOr

func (m Map[K, V, PV]) GetOr(ctx sdk.Context, key K, def V) V

func (Map[K, V, PV]) Insert

func (m Map[K, V, PV]) Insert(ctx sdk.Context, key K, object V)

func (Map[K, V, PV]) Iterate

func (m Map[K, V, PV]) Iterate(ctx sdk.Context, r keys.Range[K]) MapIterator[K, V, PV]

type MapIterator

type MapIterator[K keys.Key, V any, PV interface {
	*V
	Object
}] struct {
	// contains filtered or unexported fields
}

func (MapIterator[K, V, PV]) Close

func (i MapIterator[K, V, PV]) Close()

func (MapIterator[K, V, PV]) Key

func (i MapIterator[K, V, PV]) Key() K

func (MapIterator[K, V, PV]) KeyValues

func (i MapIterator[K, V, PV]) KeyValues() []KeyValue[K, V, PV]

todo doc

func (MapIterator[K, V, PV]) Keys

func (i MapIterator[K, V, PV]) Keys() []K

TODO doc

func (MapIterator[K, V, PV]) Next

func (i MapIterator[K, V, PV]) Next()

func (MapIterator[K, V, PV]) Valid

func (i MapIterator[K, V, PV]) Valid() bool

func (MapIterator[K, V, PV]) Value

func (i MapIterator[K, V, PV]) Value() V

func (MapIterator[K, V, PV]) Values

func (i MapIterator[K, V, PV]) Values() []V

TODO doc

type MultiIndex

type MultiIndex[IK, PK keys.Key, V any] struct {
	// contains filtered or unexported fields
}

MultiIndex defines an Indexer with no uniqueness constraints. Meaning that given two objects V1 and V2 both can be indexed with the same secondary key. Example: Person1 { ID: 0, City: Milan } Person2 { ID: 1, City: Milan } Both can be indexed with the secondary key "Milan". The key generated are, respectively: keys.Pair[Milan, 0] keys.Pair[Milan, 1] So if we want to get all the objects whose City is Milan we prefix over keys.Pair[Milan, nil], and we get the respective primary keys: 0,1.

func NewMultiIndex

func NewMultiIndex[IK, PK keys.Key, V any](cdc codec.BinaryCodec, sk sdk.StoreKey, namespace uint8, getIndexingKeyFunc func(v V) IK) MultiIndex[IK, PK, V]

NewMultiIndex instantiates a new MultiIndex instance. namespace is the unique storage namespace for the index. getIndexingKeyFunc is a function which given the object returns the key we use to index the object.

func (MultiIndex[IK, PK, V]) Delete

func (i MultiIndex[IK, PK, V]) Delete(ctx sdk.Context, pk PK, v V)

Delete implements the Indexer interface.

func (MultiIndex[IK, PK, V]) ExactMatch

func (i MultiIndex[IK, PK, V]) ExactMatch(ctx sdk.Context, ik IK) IndexerIterator[IK, PK]

ExactMatch returns an iterator of all the primary keys of objects which contain the provided indexing key ik.

func (MultiIndex[IK, PK, V]) Insert

func (i MultiIndex[IK, PK, V]) Insert(ctx sdk.Context, pk PK, v V)

Insert implements the Indexer interface.

func (MultiIndex[IK, PK, V]) Iterate

func (i MultiIndex[IK, PK, V]) Iterate(ctx sdk.Context, rng keys.Range[keys.Pair[IK, PK]]) IndexerIterator[IK, PK]

Iterate iterates over the provided range.

func (MultiIndex[IK, PK, V]) ReverseExactMatch

func (i MultiIndex[IK, PK, V]) ReverseExactMatch(ctx sdk.Context, ik IK) IndexerIterator[IK, PK]

ReverseExactMatch works in the same way as ExactMatch, but the iteration happens in reverse.

type Object

type Object interface {
	// Marshal marshals the object into bytes.
	Marshal() (b []byte, err error)
	// Unmarshal populates the object from bytes.
	Unmarshal(b []byte) error
}

Object defines an object which can marshal and unmarshal itself to and from bytes.

type Sequence

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

Sequence defines a collection item which contains an always increasing number. Useful for those flows which require ever raising unique ids.

func NewSequence

func NewSequence(cdc codec.BinaryCodec, sk sdk.StoreKey, prefix uint8) Sequence

NewSequence instantiates a new sequence object.

func (Sequence) Next

func (s Sequence) Next(ctx sdk.Context) uint64

Next returns the next available sequence number and also increases the sequence number count.

func (Sequence) Peek

func (s Sequence) Peek(ctx sdk.Context) uint64

Peek gets the next available sequence number without increasing it.

func (Sequence) Set

func (s Sequence) Set(ctx sdk.Context, u uint64)

Set hard resets the sequence to the provided number.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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