Documentation ¶
Index ¶
- Constants
- Variables
- func EncodeKeyWithPrefix[K any](prefix []byte, kc codec.KeyCodec[K], key K) ([]byte, error)
- func NamedPairKeyCodec[K1, K2 any](key1Name string, keyCodec1 codec.KeyCodec[K1], key2Name string, ...) codec.KeyCodec[Pair[K1, K2]]
- func NamedQuadKeyCodec[K1, K2, K3, K4 any](key1Name string, keyCodec1 codec.KeyCodec[K1], key2Name string, ...) codec.KeyCodec[Quad[K1, K2, K3, K4]]
- func NamedTripleKeyCodec[K1, K2, K3 any](key1Name string, keyCodec1 codec.KeyCodec[K1], key2Name string, ...) codec.KeyCodec[Triple[K1, K2, K3]]
- func NewJSONValueCodec[T any]() codec.ValueCodec[T]
- func PairKeyCodec[K1, K2 any](keyCodec1 codec.KeyCodec[K1], keyCodec2 codec.KeyCodec[K2]) codec.KeyCodec[Pair[K1, K2]]
- func QuadKeyCodec[K1, K2, K3, K4 any](keyCodec1 codec.KeyCodec[K1], keyCodec2 codec.KeyCodec[K2], ...) codec.KeyCodec[Quad[K1, K2, K3, K4]]
- func TripleKeyCodec[K1, K2, K3 any](keyCodec1 codec.KeyCodec[K1], keyCodec2 codec.KeyCodec[K2], ...) codec.KeyCodec[Triple[K1, K2, K3]]
- func WithKeySetSecondaryIndex() func(opt *keySetOptions)
- func WithKeySetUncheckedValue() func(opt *keySetOptions)
- type Collection
- type Index
- type IndexedMap
- func (m *IndexedMap[PrimaryKey, Value, Idx]) Get(ctx context.Context, pk PrimaryKey) (Value, error)
- func (m *IndexedMap[PrimaryKey, Value, Idx]) Has(ctx context.Context, pk PrimaryKey) (bool, error)
- func (m *IndexedMap[PrimaryKey, Value, Idx]) Iterate(ctx context.Context, ranger Ranger[PrimaryKey]) (Iterator[PrimaryKey, Value], error)
- func (m *IndexedMap[PrimaryKey, Value, Idx]) IterateRaw(ctx context.Context, start, end []byte, order Order) (Iterator[PrimaryKey, Value], error)
- func (m *IndexedMap[PrimaryKey, Value, Idx]) KeyCodec() codec.KeyCodec[PrimaryKey]
- func (m *IndexedMap[PrimaryKey, Value, Idx]) Remove(ctx context.Context, pk PrimaryKey) error
- func (m *IndexedMap[PrimaryKey, Value, Idx]) Set(ctx context.Context, pk PrimaryKey, value Value) error
- func (m *IndexedMap[PrimaryKey, Value, Idx]) ValueCodec() codec.ValueCodec[Value]
- func (m *IndexedMap[PrimaryKey, Value, Idx]) Walk(ctx context.Context, ranger Ranger[PrimaryKey], ...) error
- type Indexes
- type IndexingOptions
- type Item
- type Iterator
- func (i Iterator[K, V]) Close() error
- func (i Iterator[K, V]) Key() (K, error)
- func (i Iterator[K, V]) KeyValue() (kv KeyValue[K, V], err error)
- func (i Iterator[K, V]) KeyValues() ([]KeyValue[K, V], error)
- func (i Iterator[K, V]) Keys() ([]K, error)
- func (i Iterator[K, V]) Next()
- func (i Iterator[K, V]) Valid() bool
- func (i Iterator[K, V]) Value() (V, error)
- func (i Iterator[K, V]) Values() ([]V, error)
- type KeySet
- func (k KeySet[K]) Clear(ctx context.Context, ranger Ranger[K]) error
- func (k KeySet[K]) Has(ctx context.Context, key K) (bool, error)
- func (k KeySet[K]) Iterate(ctx context.Context, ranger Ranger[K]) (KeySetIterator[K], error)
- func (k KeySet[K]) IterateRaw(ctx context.Context, start, end []byte, order Order) (Iterator[K, NoValue], error)
- func (k KeySet[K]) KeyCodec() codec.KeyCodec[K]
- func (k KeySet[K]) Remove(ctx context.Context, key K) error
- func (k KeySet[K]) Set(ctx context.Context, key K) error
- func (k KeySet[K]) ValueCodec() codec.ValueCodec[NoValue]
- func (k KeySet[K]) Walk(ctx context.Context, ranger Ranger[K], ...) error
- type KeySetIterator
- type KeyValue
- type LookupMap
- func (m LookupMap[K, V]) Get(ctx context.Context, key K) (v V, err error)
- func (m LookupMap[K, V]) GetName() string
- func (m LookupMap[K, V]) GetPrefix() []byte
- func (m LookupMap[K, V]) Has(ctx context.Context, key K) (bool, error)
- func (m LookupMap[K, V]) KeyCodec() codec.KeyCodec[K]
- func (m LookupMap[K, V]) Remove(ctx context.Context, key K) error
- func (m LookupMap[K, V]) Set(ctx context.Context, key K, value V) error
- func (m LookupMap[K, V]) ValueCodec() codec.ValueCodec[V]
- type Map
- func (m Map[K, V]) Clear(ctx context.Context, ranger Ranger[K]) error
- func (m Map[K, V]) Get(ctx context.Context, key K) (v V, err error)
- func (m Map[K, V]) GetName() string
- func (m Map[K, V]) GetPrefix() []byte
- func (m Map[K, V]) Has(ctx context.Context, key K) (bool, error)
- func (m Map[K, V]) Iterate(ctx context.Context, ranger Ranger[K]) (Iterator[K, V], error)
- func (m Map[K, V]) IterateRaw(ctx context.Context, start, end []byte, order Order) (Iterator[K, V], error)
- func (m Map[K, V]) KeyCodec() codec.KeyCodec[K]
- func (m Map[K, V]) Remove(ctx context.Context, key K) error
- func (m Map[K, V]) Set(ctx context.Context, key K, value V) error
- func (m Map[K, V]) ValueCodec() codec.ValueCodec[V]
- func (m Map[K, V]) Walk(ctx context.Context, ranger Ranger[K], ...) error
- type NoValue
- type Order
- type Pair
- type PairRange
- func (p *PairRange[K1, K2]) Descending() *PairRange[K1, K2]
- func (p *PairRange[K1, K2]) EndExclusive(k2 K2) *PairRange[K1, K2]
- func (p *PairRange[K1, K2]) EndInclusive(k2 K2) *PairRange[K1, K2]
- func (p *PairRange[K1, K2]) RangeValues() (start, end *RangeKey[Pair[K1, K2]], order Order, err error)
- func (p *PairRange[K1, K2]) StartExclusive(k2 K2) *PairRange[K1, K2]
- func (p *PairRange[K1, K2]) StartInclusive(k2 K2) *PairRange[K1, K2]
- type Prefix
- type Quad
- func Join4[K1, K2, K3, K4 any](k1 K1, k2 K2, k3 K3, k4 K4) Quad[K1, K2, K3, K4]
- func QuadPrefix[K1, K2, K3, K4 any](k1 K1) Quad[K1, K2, K3, K4]
- func QuadSuperPrefix[K1, K2, K3, K4 any](k1 K1, k2 K2) Quad[K1, K2, K3, K4]
- func QuadSuperPrefix3[K1, K2, K3, K4 any](k1 K1, k2 K2, k3 K3) Quad[K1, K2, K3, K4]
- type Range
- func (r *Range[K]) Descending() *Range[K]
- func (r *Range[K]) EndExclusive(end K) *Range[K]
- func (r *Range[K]) EndInclusive(end K) *Range[K]
- func (r *Range[K]) Prefix(key K) *Range[K]
- func (r *Range[K]) RangeValues() (start, end *RangeKey[K], order Order, err error)
- func (r *Range[K]) StartExclusive(start K) *Range[K]
- func (r *Range[K]) StartInclusive(start K) *Range[K]
- type RangeKey
- type Ranger
- func NewPrefixUntilQuadRange[K1, K2, K3, K4 any](k1 K1) Ranger[Quad[K1, K2, K3, K4]]
- func NewPrefixUntilTripleRange[K1, K2, K3 any](k1 K1) Ranger[Triple[K1, K2, K3]]
- func NewPrefixUntilTripleRangeReversed[K1, K2, K3 any](k1 K1) Ranger[Triple[K1, K2, K3]]
- func NewPrefixedQuadRange[K1, K2, K3, K4 any](k1 K1) Ranger[Quad[K1, K2, K3, K4]]
- func NewPrefixedTripleRange[K1, K2, K3 any](k1 K1) Ranger[Triple[K1, K2, K3]]
- func NewPrefixedTripleRangeReversed[K1, K2, K3 any](k1 K1) Ranger[Triple[K1, K2, K3]]
- func NewSuperPrefixedQuadRange[K1, K2, K3, K4 any](k1 K1, k2 K2) Ranger[Quad[K1, K2, K3, K4]]
- func NewSuperPrefixedQuadRange3[K1, K2, K3, K4 any](k1 K1, k2 K2, k3 K3) Ranger[Quad[K1, K2, K3, K4]]
- func NewSuperPrefixedTripleRange[K1, K2, K3 any](k1 K1, k2 K2) Ranger[Triple[K1, K2, K3]]
- func NewSuperPrefixedTripleRangeReversed[K1, K2, K3 any](k1 K1, k2 K2) Ranger[Triple[K1, K2, K3]]
- type Schema
- func (s Schema) DefaultGenesis(target appmodule.GenesisTarget) error
- func (s Schema) ExportGenesis(ctx context.Context, target appmodule.GenesisTarget) error
- func (s Schema) InitGenesis(ctx context.Context, source appmodule.GenesisSource) error
- func (s Schema) IsAppModule()
- func (s Schema) IsOnePerModuleType()
- func (s Schema) ListCollections() []Collection
- func (s Schema) ModuleCodec(opts IndexingOptions) (schema.ModuleCodec, error)
- func (s Schema) ValidateGenesis(source appmodule.GenesisSource) error
- type SchemaBuilder
- type Sequence
- type Triple
- type Vec
- func (v Vec[T]) Get(ctx context.Context, index uint64) (elem T, err error)
- func (v Vec[T]) Iterate(ctx context.Context, rng Ranger[uint64]) (Iterator[uint64, T], error)
- func (v Vec[T]) Len(ctx context.Context) (uint64, error)
- func (v Vec[T]) Pop(ctx context.Context) (elem T, err error)
- func (v Vec[T]) Push(ctx context.Context, elem T) error
- func (v Vec[T]) Replace(ctx context.Context, index uint64, elem T) error
- func (v Vec[T]) Walk(ctx context.Context, rng Ranger[uint64], ...) error
Constants ¶
const ( VecElementsNameSuffix = "_elements" VecLengthNameSuffix = "_length" VecElementsPrefixSuffix = 0x0 VecLengthPrefixSuffix = 0x1 )
const DefaultSequenceStart uint64 = 0
DefaultSequenceStart defines the default starting number of a sequence.
const NameRegex = "[A-Za-z][A-Za-z0-9_]*"
NameRegex is the regular expression that all valid collection names must match.
Variables ¶
var ( // ErrNotFound is returned when the provided key is not present in the StorageProvider. ErrNotFound = errors.New("collections: not found") // ErrEncoding is returned when something fails during key or value encoding/decoding. ErrEncoding = codec.ErrEncoding // ErrConflict is returned when there are conflicts, for example in UniqueIndex. ErrConflict = errors.New("collections: conflict") )
var ( // Uint16Key can be used to encode uint16 keys. Encoding is big endian to retain ordering. Uint16Key = codec.NewUint16Key[uint16]() // Uint32Key can be used to encode uint32 keys. Encoding is big endian to retain ordering. Uint32Key = codec.NewUint32Key[uint32]() // Uint64Key can be used to encode uint64 keys. Encoding is big endian to retain ordering. Uint64Key = codec.NewUint64Key[uint64]() // Int32Key can be used to encode int32 keys. Encoding retains ordering by toggling the MSB. Int32Key = codec.NewInt32Key[int32]() // Int64Key can be used to encode int64 keys. Encoding retains ordering by toggling the MSB. Int64Key = codec.NewInt64Key[int64]() // StringKey can be used to encode string keys. The encoding just converts the string // to bytes. // Non-terminality in multipart keys is handled by appending the StringDelimiter, // this means that a string key when used as the non final part of a multipart key cannot // contain the StringDelimiter. // Lexicographical ordering is retained both in non and multipart keys. StringKey = codec.NewStringKeyCodec[string]() // BytesKey can be used to encode bytes keys. The encoding will just use // the provided bytes. // When used as the non-terminal part of a multipart key, we prefix the bytes key // with a single byte representing the length of the key. This means two things: // 1. When used in multipart keys the length can be at maximum 255 (max number that // can be represented with a single byte). // 2. When used in multipart keys the lexicographical ordering is lost due to the // length prefixing. // JSON encoding represents a bytes key as a hex encoded string. BytesKey = codec.NewBytesKey[[]byte]() // BoolKey can be used to encode booleans. It uses a single byte to represent the boolean. // 0x0 is used to represent false, and 0x1 is used to represent true. BoolKey = codec.NewBoolKey[bool]() )
var ( // BoolValue implements a ValueCodec for bool. BoolValue = codec.KeyToValueCodec(BoolKey) // Uint16Value implements a ValueCodec for uint16. Uint16Value = codec.KeyToValueCodec(Uint16Key) // Uint32Value implements a ValueCodec for uint32. Uint32Value = codec.KeyToValueCodec(Uint32Key) // Uint64Value implements a ValueCodec for uint64. Uint64Value = codec.KeyToValueCodec(Uint64Key) // Int32Value implements a ValueCodec for int32. Int32Value = codec.KeyToValueCodec(Int32Key) // Int64Value implements a ValueCodec for int64. Int64Value = codec.KeyToValueCodec(Int64Key) // StringValue implements a ValueCodec for string. StringValue = codec.KeyToValueCodec(StringKey) // BytesValue implements a ValueCodec for bytes. BytesValue = codec.KeyToValueCodec(BytesKey) )
var ( // ErrEmptyVec is returned when trying to pop an element from an empty Vec. ErrEmptyVec = errors.New("vec is empty") // ErrOutOfBounds is returned when trying to do an operation on an index that is out of bounds. ErrOutOfBounds = errors.New("vec index is out of bounds") )
var ErrInvalidIterator = errors.New("collections: invalid iterator")
ErrInvalidIterator is returned when an Iterate call resulted in an invalid iterator.
Functions ¶
func EncodeKeyWithPrefix ¶ added in v0.2.0
EncodeKeyWithPrefix returns how the collection would store the key in storage given prefix, key codec and the concrete key.
func NamedPairKeyCodec ¶ added in v1.0.0
func NamedPairKeyCodec[K1, K2 any](key1Name string, keyCodec1 codec.KeyCodec[K1], key2Name string, keyCodec2 codec.KeyCodec[K2]) codec.KeyCodec[Pair[K1, K2]]
NamedPairKeyCodec instantiates a new KeyCodec instance that can encode the Pair, given the KeyCodec of the first part of the key and the KeyCodec of the second part of the key. It also provides names for the keys which are used for indexing purposes.
func NamedQuadKeyCodec ¶ added in v1.0.0
func NamedQuadKeyCodec[K1, K2, K3, K4 any](key1Name string, keyCodec1 codec.KeyCodec[K1], key2Name string, keyCodec2 codec.KeyCodec[K2], key3Name string, keyCodec3 codec.KeyCodec[K3], key4Name string, keyCodec4 codec.KeyCodec[K4]) codec.KeyCodec[Quad[K1, K2, K3, K4]]
NamedQuadKeyCodec instantiates a new KeyCodec instance that can encode the Quad, given the KeyCodecs of the four parts of the key, in order. The provided names are used to identify the parts of the key in the schema for indexing.
func NamedTripleKeyCodec ¶ added in v1.0.0
func NewJSONValueCodec ¶ added in v1.0.0
func NewJSONValueCodec[T any]() codec.ValueCodec[T]
func PairKeyCodec ¶
func PairKeyCodec[K1, K2 any](keyCodec1 codec.KeyCodec[K1], keyCodec2 codec.KeyCodec[K2]) codec.KeyCodec[Pair[K1, K2]]
PairKeyCodec instantiates a new KeyCodec instance that can encode the Pair, given the KeyCodec of the first part of the key and the KeyCodec of the second part of the key.
func QuadKeyCodec ¶ added in v1.0.0
func QuadKeyCodec[K1, K2, K3, K4 any](keyCodec1 codec.KeyCodec[K1], keyCodec2 codec.KeyCodec[K2], keyCodec3 codec.KeyCodec[K3], keyCodec4 codec.KeyCodec[K4]) codec.KeyCodec[Quad[K1, K2, K3, K4]]
QuadKeyCodec instantiates a new KeyCodec instance that can encode the Quad, given the KeyCodecs of the four parts of the key, in order.
func TripleKeyCodec ¶ added in v0.4.0
func TripleKeyCodec[K1, K2, K3 any](keyCodec1 codec.KeyCodec[K1], keyCodec2 codec.KeyCodec[K2], keyCodec3 codec.KeyCodec[K3]) codec.KeyCodec[Triple[K1, K2, K3]]
TripleKeyCodec instantiates a new KeyCodec instance that can encode the Triple, given the KeyCodecs of the three parts of the key, in order.
func WithKeySetSecondaryIndex ¶ added in v1.0.0
func WithKeySetSecondaryIndex() func(opt *keySetOptions)
WithKeySetSecondaryIndex changes the behavior of the KeySet to be a secondary index.
func WithKeySetUncheckedValue ¶ added in v0.3.0
func WithKeySetUncheckedValue() func(opt *keySetOptions)
WithKeySetUncheckedValue changes the behavior of the KeySet when it encounters a value different from '[]byte{}', by default the KeySet errors when this happens. This option allows to ignore the value and continue with the operation, in turn the value will be cleared out and set to '[]byte{}'. You should never use this option if you're creating a new state object from scratch. This should be used only to behave nicely in case you have used values different from '[]byte{}' in your storage before migrating to collections.
Types ¶
type Collection ¶ added in v0.2.0
type Collection interface { // GetName is the unique name of the collection within a schema. It must // match format specified by NameRegex. GetName() string // GetPrefix is the unique prefix of the collection within a schema. GetPrefix() []byte // ValueCodec returns the codec used to encode/decode values of the collection. ValueCodec() codec.UntypedValueCodec // contains filtered or unexported methods }
Collection is the interface that all collections implement. It will eventually include methods for importing/exporting genesis data and schema reflection for clients. NOTE: Unstable.
type Index ¶
type Index[PrimaryKey, Value any] interface { // Reference creates a reference between the provided primary key and value. // It provides a lazyOldValue function that if called will attempt to fetch // the previous old value, returns ErrNotFound if no value existed. Reference(ctx context.Context, pk PrimaryKey, newValue Value, lazyOldValue func() (Value, error)) error // Unreference removes the reference between the primary key and value. // If error is ErrNotFound then it means that the value did not exist before. Unreference(ctx context.Context, pk PrimaryKey, lazyOldValue func() (Value, error)) error }
Index represents an index of the Value indexed using the type PrimaryKey.
type IndexedMap ¶
type IndexedMap[PrimaryKey, Value, Idx any] struct { Indexes Idx // contains filtered or unexported fields }
IndexedMap works like a Map but creates references between fields of Value and its PrimaryKey. These relationships are expressed and maintained using the Indexes type. Internally IndexedMap can be seen as a partitioned collection, one partition is a Map[PrimaryKey, Value], that maintains the object, the second are the Indexes.
func NewIndexedMap ¶
func NewIndexedMap[PrimaryKey, Value, Idx any]( schema *SchemaBuilder, prefix Prefix, name string, pkCodec codec.KeyCodec[PrimaryKey], valueCodec codec.ValueCodec[Value], indexes Idx, ) *IndexedMap[PrimaryKey, Value, Idx]
NewIndexedMap instantiates a new IndexedMap. Accepts a SchemaBuilder, a Prefix, a humanized name that defines the name of the collection, the primary key codec which is basically what IndexedMap uses to encode the primary key to bytes, the value codec which is what the IndexedMap uses to encode the value. Then it expects the initialized indexes. Reflection is used to infer the indexes, Indexes can optionally be implemented to be explicit. Panics on failure to create indexes. If you want an erroring API use NewIndexedMapSafe.
func NewIndexedMapSafe ¶ added in v1.0.0
func NewIndexedMapSafe[K, V, I any]( schema *SchemaBuilder, prefix Prefix, name string, pkCodec codec.KeyCodec[K], valueCodec codec.ValueCodec[V], indexes I, ) (im *IndexedMap[K, V, I], err error)
NewIndexedMapSafe behaves like NewIndexedMap but returns errors.
func (*IndexedMap[PrimaryKey, Value, Idx]) Get ¶
func (m *IndexedMap[PrimaryKey, Value, Idx]) Get(ctx context.Context, pk PrimaryKey) (Value, error)
Get gets the object given its primary key.
func (*IndexedMap[PrimaryKey, Value, Idx]) Has ¶
func (m *IndexedMap[PrimaryKey, Value, Idx]) Has(ctx context.Context, pk PrimaryKey) (bool, error)
Has reports if exists a value with the provided primary key.
func (*IndexedMap[PrimaryKey, Value, Idx]) Iterate ¶
func (m *IndexedMap[PrimaryKey, Value, Idx]) Iterate(ctx context.Context, ranger Ranger[PrimaryKey]) (Iterator[PrimaryKey, Value], error)
Iterate allows to iterate over the objects given a Ranger of the primary key.
func (*IndexedMap[PrimaryKey, Value, Idx]) IterateRaw ¶
func (m *IndexedMap[PrimaryKey, Value, Idx]) IterateRaw(ctx context.Context, start, end []byte, order Order) (Iterator[PrimaryKey, Value], error)
IterateRaw iterates the IndexedMap using raw bytes keys. Follows the same semantics as Map.IterateRaw
func (*IndexedMap[PrimaryKey, Value, Idx]) KeyCodec ¶
func (m *IndexedMap[PrimaryKey, Value, Idx]) KeyCodec() codec.KeyCodec[PrimaryKey]
func (*IndexedMap[PrimaryKey, Value, Idx]) Remove ¶
func (m *IndexedMap[PrimaryKey, Value, Idx]) Remove(ctx context.Context, pk PrimaryKey) error
Remove removes the value associated with the primary key from the map. Then it iterates over all the indexes and instructs them to remove all the references associated with the removed value.
func (*IndexedMap[PrimaryKey, Value, Idx]) Set ¶
func (m *IndexedMap[PrimaryKey, Value, Idx]) Set(ctx context.Context, pk PrimaryKey, value Value) error
Set maps the value using the primary key. It will also iterate every index and instruct them to add or update the indexes.
func (*IndexedMap[PrimaryKey, Value, Idx]) ValueCodec ¶
func (m *IndexedMap[PrimaryKey, Value, Idx]) ValueCodec() codec.ValueCodec[Value]
type Indexes ¶
type Indexes[PrimaryKey, Value any] interface { // IndexesList is implemented by the Indexes type // and returns all the grouped Index of Value. IndexesList() []Index[PrimaryKey, Value] }
Indexes represents a type which groups multiple Index of one Value saved with the provided PrimaryKey. Indexes is just meant to be a struct containing all the indexes to maintain relationship for.
type IndexingOptions ¶ added in v1.0.0
type IndexingOptions struct { // RetainDeletionsFor is the list of collections to retain deletions for. RetainDeletionsFor []string }
IndexingOptions are indexing options for the collections schema.
type Item ¶
Item is a type declaration based on Map with a non-existent key.
func NewItem ¶
func NewItem[V any]( schema *SchemaBuilder, prefix Prefix, name string, valueCodec codec.ValueCodec[V], ) Item[V]
NewItem instantiates a new Item instance, given the value encoder of the item V. Name and prefix must be unique within the schema and name must match the format specified by NameRegex, or else this method will panic.
func (Item[V]) Get ¶
Get gets the item, if it is not set it returns an ErrNotFound error. If value decoding fails then an ErrEncoding is returned.
func (Item[V]) Has ¶
Has reports whether the item exists in the store or not. Returns an error in case encoding fails.
type Iterator ¶
type Iterator[K, V any] struct { // contains filtered or unexported fields }
Iterator defines a generic wrapper around a storetypes.Iterator. This iterator provides automatic key and value encoding, it assumes all the keys and values contained within the storetypes.Iterator range are the same.
func (Iterator[K, V]) KeyValues ¶
KeyValues fully consumes the iterator and returns the list of key and values within the iterator range.
func (Iterator[K, V]) Keys ¶
Keys fully consumes the iterator and returns all the decoded keys contained within the range.
type KeySet ¶
KeySet builds on top of a Map and represents a collection retaining only a set of keys and no value. It can be used, for example, in an allow list.
func NewKeySet ¶
func NewKeySet[K any]( schema *SchemaBuilder, prefix Prefix, name string, keyCodec codec.KeyCodec[K], options ...func(opt *keySetOptions), ) KeySet[K]
NewKeySet returns a KeySet given a Schema, Prefix a human name for the collection and a KeyCodec for the key K.
func (KeySet[K]) Clear ¶ added in v0.3.0
Clear clears the KeySet using the provided Ranger. Refer to Map.Clear for behavioral documentation.
func (KeySet[K]) Has ¶
Has returns if the key is present in the KeySet. An error is returned only in case of encoding problems.
func (KeySet[K]) Iterate ¶
Iterate iterates over the keys given the provided Ranger. If ranger is nil, the KeySetIterator will include all the existing keys within the KeySet.
func (KeySet[K]) IterateRaw ¶
func (KeySet[K]) Remove ¶
Remove removes the key for the KeySet. An error is returned in case of encoding error, it won't report through the error if the key was removed or not.
func (KeySet[K]) ValueCodec ¶
func (k KeySet[K]) ValueCodec() codec.ValueCodec[NoValue]
type KeySetIterator ¶
KeySetIterator works like an Iterator, but it does not expose any API to deal with values.
func (KeySetIterator[K]) Close ¶
func (i KeySetIterator[K]) Close() error
func (KeySetIterator[K]) Key ¶
func (i KeySetIterator[K]) Key() (K, error)
func (KeySetIterator[K]) Keys ¶
func (i KeySetIterator[K]) Keys() ([]K, error)
func (KeySetIterator[K]) Next ¶
func (i KeySetIterator[K]) Next()
func (KeySetIterator[K]) Valid ¶
func (i KeySetIterator[K]) Valid() bool
type KeyValue ¶
type KeyValue[K, V any] struct { Key K Value V }
KeyValue represent a Key and Value pair of an iteration.
type LookupMap ¶ added in v1.0.0
LookupMap represents a map that is not iterable.
func NewLookupMap ¶ added in v1.0.0
func NewLookupMap[K, V any]( schemaBuilder *SchemaBuilder, prefix Prefix, name string, keyCodec codec.KeyCodec[K], valueCodec codec.ValueCodec[V], ) LookupMap[K, V]
NewLookupMap creates a new LookupMap.
func (LookupMap[K, V]) Get ¶ added in v1.0.0
Get returns the value associated with the provided key, errors with ErrNotFound if the key does not exist, or with ErrEncoding if the key or value decoding fails.
func (LookupMap[K, V]) Has ¶ added in v1.0.0
Has reports whether the key is present in storage or not. Errors with ErrEncoding if key encoding fails.
func (LookupMap[K, V]) Remove ¶ added in v1.0.0
Remove removes the key from the storage. Errors with ErrEncoding if key encoding fails. If the key does not exist then this is a no-op.
func (LookupMap[K, V]) Set ¶ added in v1.0.0
Set maps the provided value to the provided key in the store. Errors with ErrEncoding if key or value encoding fails.
func (LookupMap[K, V]) ValueCodec ¶ added in v1.0.0
func (m LookupMap[K, V]) ValueCodec() codec.ValueCodec[V]
ValueCodec returns the Map's ValueCodec.
type Map ¶
type Map[K, V any] struct { // contains filtered or unexported fields }
Map represents the basic collections object. It is used to map arbitrary keys to arbitrary objects.
func NewMap ¶
func NewMap[K, V any]( schemaBuilder *SchemaBuilder, prefix Prefix, name string, keyCodec codec.KeyCodec[K], valueCodec codec.ValueCodec[V], options ...func(opt *mapOptions), ) Map[K, V]
NewMap returns a Map given a StoreKey, a Prefix, human-readable name and the relative value and key encoders. Name and prefix must be unique within the schema and name must match the format specified by NameRegex, or else this method will panic.
func (Map[K, V]) Clear ¶ added in v0.3.0
Clear clears the collection contained within the provided key range. A nil ranger equals to clearing the whole collection. NOTE: this API needs to be used with care, considering that as of today cosmos-sdk stores the deletion records to be committed in a memory cache, clearing a lot of data might make the node go OOM.
func (Map[K, V]) Get ¶
Get returns the value associated with the provided key, errors with ErrNotFound if the key does not exist, or with ErrEncoding if the key or value decoding fails.
func (Map[K, V]) Has ¶
Has reports whether the key is present in storage or not. Errors with ErrEncoding if key encoding fails.
func (Map[K, V]) Iterate ¶
Iterate provides an Iterator over K and V. It accepts a Ranger interface. A nil ranger equals to iterate over all the keys in ascending order.
func (Map[K, V]) IterateRaw ¶
func (m Map[K, V]) IterateRaw(ctx context.Context, start, end []byte, order Order) (Iterator[K, V], error)
IterateRaw iterates over the collection. The iteration range is untyped, it uses raw bytes. The resulting Iterator is typed. A nil start iterates from the first key contained in the collection. A nil end iterates up to the last key contained in the collection. A nil start and a nil end iterates over every key contained in the collection. TODO(tip): simplify after https://github.com/cosmos/cosmos-sdk/pull/14310 is merged
func (Map[K, V]) Remove ¶
Remove removes the key from the storage. Errors with ErrEncoding if key encoding fails. If the key does not exist then this is a no-op.
func (Map[K, V]) Set ¶
Set maps the provided value to the provided key in the store. Errors with ErrEncoding if key or value encoding fails.
func (Map[K, V]) ValueCodec ¶
func (m Map[K, V]) ValueCodec() codec.ValueCodec[V]
ValueCodec returns the Map's ValueCodec.
func (Map[K, V]) Walk ¶
func (m Map[K, V]) Walk(ctx context.Context, ranger Ranger[K], walkFunc func(key K, value V) (stop bool, err error)) error
Walk iterates over the Map with the provided range, calls the provided walk function with the decoded key and value. If the callback function returns true then the walking is stopped. A nil ranger equals to walking over the entire key and value set.
type NoValue ¶
type NoValue struct{}
NoValue is a type that can be used to represent a non-existing value.
type Pair ¶
type Pair[K1, K2 any] struct { // contains filtered or unexported fields }
Pair defines a key composed of two keys.
func PairPrefix ¶
PairPrefix creates a new Pair instance composed only of the first part of the key.
type PairRange ¶
type PairRange[K1, K2 any] struct { // contains filtered or unexported fields }
PairRange is an API that facilitates working with Pair iteration. It implements the Ranger API. Unstable: API and methods are currently unstable.
func NewPrefixUntilPairRange ¶ added in v0.2.0
NewPrefixUntilPairRange defines a collection query which ranges until the provided Pair prefix. Unstable: this API might change in the future.
func NewPrefixedPairRange ¶
NewPrefixedPairRange creates a new PairRange which will prefix over all the keys starting with the provided prefix.
func (*PairRange[K1, K2]) Descending ¶
func (*PairRange[K1, K2]) EndExclusive ¶
func (*PairRange[K1, K2]) EndInclusive ¶
func (*PairRange[K1, K2]) RangeValues ¶
func (*PairRange[K1, K2]) StartExclusive ¶
func (*PairRange[K1, K2]) StartInclusive ¶
type Prefix ¶
type Prefix []byte
Prefix defines a segregation bytes namespace for specific collections objects.
func NewPrefix ¶
NewPrefix returns a Prefix given the provided namespace identifier. In the same module, no prefixes should share the same starting bytes meaning that having two namespaces whose bytes representation is: p1 := []byte("prefix") p2 := []byte("prefix1") yields to iterations of p1 overlapping over p2. If a numeric prefix is provided, it must be between 0 and 255 (uint8). If out of bounds this function will panic. Reason for which this function is constrained to `int` instead of `uint8` is for API ergonomics, golang's type inference will infer int properly but not uint8 meaning that developers would need to write NewPrefix(uint8(number)) for numeric prefixes.
type Quad ¶ added in v1.0.0
type Quad[K1, K2, K3, K4 any] struct { // contains filtered or unexported fields }
Quad defines a multipart key composed of four keys.
func Join4 ¶ added in v1.0.0
Join4 instantiates a new Quad instance composed of the four provided keys, in order.
func QuadPrefix ¶ added in v1.0.0
QuadPrefix creates a new Quad instance composed only of the first part of the key.
func QuadSuperPrefix ¶ added in v1.0.0
QuadSuperPrefix creates a new Quad instance composed only of the first two parts of the key.
func QuadSuperPrefix3 ¶ added in v1.0.0
QuadSuperPrefix3 creates a new Quad instance composed only of the first three parts of the key.
func (Quad[K1, K2, K3, K4]) K1 ¶ added in v1.0.0
func (t Quad[K1, K2, K3, K4]) K1() (x K1)
K1 returns the first part of the key. If nil, the zero value is returned.
func (Quad[K1, K2, K3, K4]) K2 ¶ added in v1.0.0
func (t Quad[K1, K2, K3, K4]) K2() (x K2)
K2 returns the second part of the key. If nil, the zero value is returned.
type Range ¶
type Range[K any] struct { // contains filtered or unexported fields }
Range is a Ranger implementer.
func (*Range[K]) Descending ¶
func (*Range[K]) EndExclusive ¶
EndExclusive makes the range contain only keys which are smaller to the provided end K.
func (*Range[K]) EndInclusive ¶
EndInclusive makes the range contain only keys which are smaller or equal to the provided end K.
func (*Range[K]) RangeValues ¶
func (*Range[K]) StartExclusive ¶
StartExclusive makes the range contain only keys which are bigger to the provided start K.
func (*Range[K]) StartInclusive ¶
StartInclusive makes the range contain only keys which are bigger or equal to the provided start K.
type RangeKey ¶
type RangeKey[K any] struct { // contains filtered or unexported fields }
RangeKey wraps a generic range key K, acts as an enum which defines different ways to encode the wrapped key to bytes when it's being used in an iteration.
func RangeKeyExact ¶
RangeKeyExact instantiates a RangeKey that applies no modifications to the key K. So its bytes representation will not be altered.
func RangeKeyNext ¶
RangeKeyNext instantiates a RangeKey that when encoded to bytes identifies the next key after the provided key K. Example: given a string key "ABCD" the next key is bytes("ABCD\0") It's useful when defining inclusivity or exclusivity of a key in store iteration. Specifically: to make an Iterator start exclude key K I would return a RangeKeyNext(key) in the Ranger start.
func RangeKeyPrefixEnd ¶
RangeKeyPrefixEnd instantiates a RangeKey that when encoded to bytes identifies the key that would end the prefix of the key K. Example: if the string key "ABCD" is provided, it would be encoded as bytes("ABCE").
type Ranger ¶
type Ranger[K any] interface { // RangeValues is defined by Ranger implementers. // The implementer can optionally return a start and an end. // If start is nil and end is not, the iteration will include all the keys // in the collection up until the provided end. // If start is defined and end is nil, the iteration will include all the keys // in the collection starting from the provided start. // If both are nil then the iteration will include all the possible keys in the // collection. // Order defines the order of the iteration, if order is OrderAscending then the // iteration will yield keys from the smallest to the biggest, if order // is OrderDescending then the iteration will yield keys from the biggest to the smallest. // Ordering is defined by the keys bytes representation, which is dependent on the KeyCodec used. RangeValues() (start, end *RangeKey[K], order Order, err error) }
Ranger defines a generic interface that provides a range of keys.
func NewPrefixUntilQuadRange ¶ added in v1.0.0
NewPrefixUntilQuadRange defines a collection query which ranges until the provided Quad prefix. Unstable: this API might change in the future.
func NewPrefixUntilTripleRange ¶ added in v0.4.0
NewPrefixUntilTripleRange defines a collection query which ranges until the provided Pair prefix. Unstable: this API might change in the future.
func NewPrefixUntilTripleRangeReversed ¶ added in v1.0.0
NewPrefixUntilTripleRangeReversed defines a collection query which ranges until the provided Pair prefix in reverse order. Unstable: this API might change in the future.
func NewPrefixedQuadRange ¶ added in v1.0.0
NewPrefixedQuadRange provides a Range for all keys prefixed with the given first part of the Quad key.
func NewPrefixedTripleRange ¶ added in v0.4.0
NewPrefixedTripleRange provides a Range for all keys prefixed with the given first part of the Triple key.
func NewPrefixedTripleRangeReversed ¶ added in v1.0.0
NewPrefixedTripleRangeReversed provides a Range for all keys prefixed with the given first part of the Triple key in reverse order.
func NewSuperPrefixedQuadRange ¶ added in v1.0.0
NewSuperPrefixedQuadRange provides a Range for all keys prefixed with the given first and second parts of the Quad key.
func NewSuperPrefixedQuadRange3 ¶ added in v1.0.0
func NewSuperPrefixedQuadRange3[K1, K2, K3, K4 any](k1 K1, k2 K2, k3 K3) Ranger[Quad[K1, K2, K3, K4]]
NewSuperPrefixedQuadRange3 provides a Range for all keys prefixed with the given first, second and third parts of the Quad key.
func NewSuperPrefixedTripleRange ¶ added in v0.4.0
NewSuperPrefixedTripleRange provides a Range for all keys prefixed with the given first and second parts of the Triple key.
func NewSuperPrefixedTripleRangeReversed ¶ added in v1.0.0
NewSuperPrefixedTripleRangeReversed provides a Range for all keys prefixed with the given first and second parts of the Triple key in reverse order.
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema specifies a group of collections stored within the storage specified by a single store key. All the collections within the schema must have a unique binary prefix and human-readable name. Schema will eventually include methods for importing/exporting genesis data and for schema reflection for clients.
func NewMemoryStoreSchema ¶
func NewMemoryStoreSchema(service store.MemoryStoreService) Schema
NewMemoryStoreSchema creates a new schema for the provided MemoryStoreService.
func NewSchema ¶
func NewSchema(service store.KVStoreService) Schema
NewSchema creates a new schema for the provided KVStoreService.
func NewSchemaFromAccessor ¶
NewSchemaFromAccessor(func(ctx context.Context) store.KVStore { return sdk.UnwrapSDKContext(ctx).KVStore(kvStoreKey) }
func (Schema) DefaultGenesis ¶
func (s Schema) DefaultGenesis(target appmodule.GenesisTarget) error
DefaultGenesis implements the appmodule.HasGenesis.DefaultGenesis method.
func (Schema) ExportGenesis ¶
ExportGenesis implements the appmodule.HasGenesis.ExportGenesis method.
func (Schema) InitGenesis ¶
InitGenesis implements the appmodule.HasGenesis.InitGenesis method.
func (Schema) IsAppModule ¶ added in v0.4.0
func (s Schema) IsAppModule()
IsAppModule implements the appmodule.AppModule interface.
func (Schema) IsOnePerModuleType ¶ added in v0.4.0
func (s Schema) IsOnePerModuleType()
IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (Schema) ListCollections ¶ added in v0.2.0
func (s Schema) ListCollections() []Collection
func (Schema) ModuleCodec ¶ added in v1.0.0
func (s Schema) ModuleCodec(opts IndexingOptions) (schema.ModuleCodec, error)
ModuleCodec returns the ModuleCodec for this schema for the provided options.
func (Schema) ValidateGenesis ¶
func (s Schema) ValidateGenesis(source appmodule.GenesisSource) error
ValidateGenesis implements the appmodule.HasGenesis.ValidateGenesis method.
type SchemaBuilder ¶
type SchemaBuilder struct {
// contains filtered or unexported fields
}
SchemaBuilder is used for building schemas. The Build method should always be called after all collections have been initialized. Initializing new collections with the builder after initialization will result in panics.
func NewSchemaBuilder ¶
func NewSchemaBuilder(service store.KVStoreService) *SchemaBuilder
NewSchemaBuilder creates a new schema builder from the provided store key. Callers should always call the SchemaBuilder.Build method when they are done adding collections to the schema.
func NewSchemaBuilderFromAccessor ¶
func NewSchemaBuilderFromAccessor(accessorFunc func(ctx context.Context) store.KVStore) *SchemaBuilder
NewSchemaBuilderFromAccessor creates a new schema builder from the provided store accessor function.
func (*SchemaBuilder) Build ¶
func (s *SchemaBuilder) Build() (Schema, error)
Build should be called after all collections that are part of the schema have been initialized in order to get a reference to the Schema. It is important to check the returned error for any initialization errors. The SchemaBuilder CANNOT be used after Build is called - doing so will result in panics.
type Sequence ¶
Sequence builds on top of an Item, and represents a monotonically increasing number.
func NewSequence ¶
func NewSequence(schema *SchemaBuilder, prefix Prefix, name string) Sequence
NewSequence instantiates a new sequence given a Schema, a Prefix and humanized name for the sequence.
func (Sequence) Next ¶
Next returns the current sequence number, and sets the next expected sequence. Errors on encoding issues.
type Triple ¶ added in v0.4.0
type Triple[K1, K2, K3 any] struct { // contains filtered or unexported fields }
Triple defines a multipart key composed of three keys.
func Join3 ¶ added in v0.4.0
Join3 instantiates a new Triple instance composed of the three provided keys, in order.
func TriplePrefix ¶ added in v0.4.0
TriplePrefix creates a new Triple instance composed only of the first part of the key.
func TripleSuperPrefix ¶ added in v0.4.0
TripleSuperPrefix creates a new Triple instance composed only of the first two parts of the key.
func (Triple[K1, K2, K3]) K1 ¶ added in v0.4.0
func (t Triple[K1, K2, K3]) K1() (x K1)
K1 returns the first part of the key. If nil, the zero value is returned.
type Vec ¶ added in v1.0.0
type Vec[T any] struct { // contains filtered or unexported fields }
Vec works like a slice sitting on top of a KVStore. It relies on two collections, one for the length which is an Item[uint64], the other for the elements which is a Map[uint64, T].
func NewVec ¶ added in v1.0.0
func NewVec[T any](sb *SchemaBuilder, prefix Prefix, name string, vc codec.ValueCodec[T]) Vec[T]
NewVec creates a new Vec instance. Since Vec relies on two collections, one for the length and the other for the elements, it will register two state objects on the schema builder. The first is the length which is an item, whose prefix is the provided prefix with a suffix which equals to VecLengthPrefixSuffix, the name is also suffixed with VecLengthNameSuffix. The second is the elements which is a map, whose prefix is the provided prefix with a suffix which equals to VecElementsPrefixSuffix, the name is also suffixed with VecElementsNameSuffix.
func (Vec[T]) Get ¶ added in v1.0.0
Get returns an element at a given index. Returns ErrOutOfBounds if the index is out of bounds.
func (Vec[T]) Iterate ¶ added in v1.0.0
Iterate iterates over the Vec. It returns an Iterator whose key is the index and the value is the element at that index.
func (Vec[T]) Pop ¶ added in v1.0.0
Pop removes an element from the end of the Vec and returns it. Fails if the Vec is empty.
func (Vec[T]) Replace ¶ added in v1.0.0
Replace replaces an element at a given index. Fails if the index is out of bounds.
func (Vec[T]) Walk ¶ added in v1.0.0
func (v Vec[T]) Walk(ctx context.Context, rng Ranger[uint64], walkFn func(index uint64, elem T) (stop bool, err error)) error
Walk walks over the Vec. It calls the walkFn for each element in the Vec, where the key is the index and the value is the element at that index.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package codec defines how collections transform keys and values into and from bytes.
|
Package codec defines how collections transform keys and values into and from bytes. |
Package indexes contains the most common indexes types to be used with a collections.IndexedMap.
|
Package indexes contains the most common indexes types to be used with a collections.IndexedMap. |
protocodec
module
|