Documentation ¶
Index ¶
- Constants
- Variables
- func PairKeyCodec[K1, K2 any](keyCodec1 codec.KeyCodec[K1], keyCodec2 codec.KeyCodec[K2]) codec.KeyCodec[Pair[K1, K2]]
- type GenericMultiIndex
- func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Has(ctx context.Context, referencing ReferencingKey, referenced ReferencedKey) (bool, error)
- func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Iterate(ctx context.Context, ranger Ranger[Pair[ReferencingKey, ReferencedKey]]) (KeySetIterator[Pair[ReferencingKey, ReferencedKey]], error)
- func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) IterateRaw(ctx context.Context, start, end []byte, order Order) (Iterator[Pair[ReferencingKey, ReferencedKey], NoValue], error)
- func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) KeyCodec() codec.KeyCodec[Pair[ReferencingKey, ReferencedKey]]
- func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Reference(ctx context.Context, pk PrimaryKey, value Value, oldValue *Value) error
- func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Unreference(ctx context.Context, pk PrimaryKey, value Value) error
- func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) ValueCodec() codec.ValueCodec[NoValue]
- func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Walk(ctx context.Context, ranger Ranger[Pair[ReferencingKey, ReferencedKey]], ...) error
- type GenericUniqueIndex
- func (i *GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Get(ctx context.Context, ref ReferencingKey) (ReferencedKey, error)
- func (i *GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Iterate(ctx context.Context, ranger Ranger[ReferencingKey]) (Iterator[ReferencingKey, ReferencedKey], error)
- func (i *GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) IterateRaw(ctx context.Context, start, end []byte, order Order) (Iterator[ReferencingKey, ReferencedKey], error)
- func (i *GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Reference(ctx context.Context, pk PrimaryKey, newValue Value, oldValue *Value) error
- func (i *GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Unreference(ctx context.Context, pk PrimaryKey, value Value) error
- func (i *GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Walk(ctx context.Context, ranger Ranger[ReferencingKey], ...) error
- type Index
- type IndexReference
- 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 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]) 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], walkFunc func(key K) bool) error
- type KeySetIterator
- type KeyValue
- type Map
- func (m Map[K, V]) Get(ctx context.Context, key K) (v V, err error)
- 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], walkFunc func(K, V) bool) 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 *RangeKey[Pair[K1, K2]], end *RangeKey[Pair[K1, K2]], order Order, ...)
- 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 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 *RangeKey[K], 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
- 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) ValidateGenesis(source appmodule.GenesisSource) error
- type SchemaBuilder
- type Sequence
Constants ¶
const DefaultSequenceStart uint64 = 1
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. 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 ErrInvalidIterator = errors.New("collections: invalid iterator")
ErrInvalidIterator is returned when an Iterate call resulted in an invalid iterator.
Functions ¶
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.
Types ¶
type GenericMultiIndex ¶
type GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value any] struct { // contains filtered or unexported fields }
GenericMultiIndex defines a generic Index type that given a primary key and the value associated with that primary key returns one or multiple IndexReference.
The referencing key can be anything, usually it is either a part of the primary key when we deal with multipart keys, or a field of Value.
The referenced key usually is the primary key, or it can be a part of the primary key in the context of multipart keys.
The Referencing and Referenced keys are joined and saved as a Pair in a KeySet where the key is Pair[ReferencingKey, ReferencedKey]. So if we wanted to get all the keys referenced by a generic (concrete) ReferencingKey we would just need to iterate over all the keys starting with bytes(ReferencingKey).
Unless you're trying to build your generic multi index, you should be using the indexes package.
func NewGenericMultiIndex ¶
func NewGenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value any]( schema *SchemaBuilder, prefix Prefix, name string, referencingKeyCodec codec.KeyCodec[ReferencingKey], referencedKeyCodec codec.KeyCodec[ReferencedKey], getRefsFunc func(pk PrimaryKey, value Value) ([]IndexReference[ReferencingKey, ReferencedKey], error), ) *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]
NewGenericMultiIndex instantiates a GenericMultiIndex, given schema, Prefix, humanised name, the key codec used to encode the referencing key to bytes, the key codec used to encode the referenced key to bytes and a function which given the primary key and a value of an object being saved or removed in IndexedMap returns all the possible IndexReference of that object.
The IndexReference is usually just one. But in certain cases can be multiple, for example when the Value has an array field, and we want to create a relationship between the object and all the elements of the array contained in the object.
func (*GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Has ¶
func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Has( ctx context.Context, referencing ReferencingKey, referenced ReferencedKey, ) (bool, error)
Has reports if there is a relationship in the index between the referencing and the referenced key.
func (*GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Iterate ¶
func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Iterate( ctx context.Context, ranger Ranger[Pair[ReferencingKey, ReferencedKey]], ) (KeySetIterator[Pair[ReferencingKey, ReferencedKey]], error)
Iterate allows to iterate over the index. It returns a KeySetIterator of Pair[ReferencingKey, ReferencedKey]. K1 of the Pair is the key (referencing) pointing to K2 (referenced).
func (*GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) IterateRaw ¶
func (*GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) KeyCodec ¶
func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) KeyCodec() codec.KeyCodec[Pair[ReferencingKey, ReferencedKey]]
func (*GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Reference ¶
func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Reference( ctx context.Context, pk PrimaryKey, value Value, oldValue *Value, ) error
Reference implements the Index interface.
func (*GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Unreference ¶
func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Unreference( ctx context.Context, pk PrimaryKey, value Value, ) error
Unreference implements the Index interface.
func (*GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) ValueCodec ¶
func (i *GenericMultiIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) ValueCodec() codec.ValueCodec[NoValue]
type GenericUniqueIndex ¶
type GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value any] struct { // contains filtered or unexported fields }
GenericUniqueIndex defines a generic index which enforces uniqueness constraints between ReferencingKey and ReferencedKey, meaning that one referencing key maps only one referenced key. The same referenced key can be mapped by multiple referencing keys.
The referencing key can be anything, usually it is either a part of the primary key when we deal with multipart keys, or a field of Value.
The referenced key usually is the primary key, or it can be a part of the primary key in the context of multipart keys.
The referencing and referenced keys are mapped together using a Map.
Unless you're trying to build your generic unique index, you should be using the indexes package.
func NewGenericUniqueIndex ¶
func NewGenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value any]( schema *SchemaBuilder, prefix Prefix, name string, referencingKeyCodec codec.KeyCodec[ReferencingKey], referencedKeyCodec codec.KeyCodec[ReferencedKey], getRefs func(pk PrimaryKey, value Value) ([]IndexReference[ReferencingKey, ReferencedKey], error), ) *GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]
NewGenericUniqueIndex instantiates a GenericUniqueIndex. Works in the same way as NewGenericMultiIndex.
func (*GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Get ¶
func (i *GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Get(ctx context.Context, ref ReferencingKey) (ReferencedKey, error)
func (*GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) IterateRaw ¶
func (*GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Reference ¶
func (i *GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Reference( ctx context.Context, pk PrimaryKey, newValue Value, oldValue *Value, ) error
Reference implements Index.
func (*GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Unreference ¶
func (i *GenericUniqueIndex[ReferencingKey, ReferencedKey, PrimaryKey, Value]) Unreference( ctx context.Context, pk PrimaryKey, value Value, ) error
Unreference implements Index.
type Index ¶
type Index[PrimaryKey, Value any] interface { // Reference creates a reference between the provided primary key and value. // If oldValue is not nil then the Index must update the references // of the primary key associated with the new value and remove the // old invalid references. Reference(ctx context.Context, pk PrimaryKey, newValue Value, oldValue *Value) error // Unreference removes the reference between the primary key and value. Unreference(ctx context.Context, pk PrimaryKey, value Value) error }
Index represents an index of the Value indexed using the type PrimaryKey.
type IndexReference ¶
type IndexReference[ReferencingKey, ReferencedKey any] struct { // Referring is the key that refers, points to the Referred key. Referring ReferencingKey // Referred is the key that is being pointed to by the Referring key. Referred ReferencedKey }
IndexReference defines a generic index reference.
func NewIndexReference ¶
func NewIndexReference[ReferencingKey, ReferencedKey any](referencing ReferencingKey, referenced ReferencedKey) IndexReference[ReferencingKey, ReferencedKey]
type IndexedMap ¶
type IndexedMap[PrimaryKey, Value any, Idx Indexes[PrimaryKey, Value]] 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 any, Idx Indexes[PrimaryKey, Value]]( 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 humanised 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 initialised indexes.
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 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
type Iterator ¶
type Iterator[K, V any] struct { // contains filtered or unexported fields }
Iterator defines a generic wrapper around an 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]) 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]) 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 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], ) 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]) 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/verzth/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.
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 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 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 *RangeKey[K], end *RangeKey[K], order Order, err error) }
Ranger defines a generic interface that provides a range of keys.
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) 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 humanised name for the sequence.
func (Sequence) Next ¶
Next returns the next sequence number, and sets the next expected sequence. Errors on encoding issues.
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. |