|
| 1 | +package collections |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "math" |
| 8 | + |
| 9 | + "cosmossdk.io/collections/codec" |
| 10 | + "cosmossdk.io/schema" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + // ErrNotFound is returned when the provided key is not present in the StorageProvider. |
| 15 | + ErrNotFound = errors.New("collections: not found") |
| 16 | + // ErrEncoding is returned when something fails during key or value encoding/decoding. |
| 17 | + ErrEncoding = codec.ErrEncoding |
| 18 | + // ErrConflict is returned when there are conflicts, for example in UniqueIndex. |
| 19 | + ErrConflict = errors.New("collections: conflict") |
| 20 | +) |
| 21 | + |
| 22 | +// KEYS |
| 23 | + |
| 24 | +var ( |
| 25 | + // Uint16Key can be used to encode uint16 keys. Encoding is big endian to retain ordering. |
| 26 | + Uint16Key = codec.NewUint16Key[uint16]() |
| 27 | + // Uint32Key can be used to encode uint32 keys. Encoding is big endian to retain ordering. |
| 28 | + Uint32Key = codec.NewUint32Key[uint32]() |
| 29 | + // Uint64Key can be used to encode uint64 keys. Encoding is big endian to retain ordering. |
| 30 | + Uint64Key = codec.NewUint64Key[uint64]() |
| 31 | + // Int32Key can be used to encode int32 keys. Encoding retains ordering by toggling the MSB. |
| 32 | + Int32Key = codec.NewInt32Key[int32]() |
| 33 | + // Int64Key can be used to encode int64 keys. Encoding retains ordering by toggling the MSB. |
| 34 | + Int64Key = codec.NewInt64Key[int64]() |
| 35 | + // StringKey can be used to encode string keys. The encoding just converts the string |
| 36 | + // to bytes. |
| 37 | + // Non-terminality in multipart keys is handled by appending the StringDelimiter, |
| 38 | + // this means that a string key when used as the non final part of a multipart key cannot |
| 39 | + // contain the StringDelimiter. |
| 40 | + // Lexicographical ordering is retained both in non and multipart keys. |
| 41 | + StringKey = codec.NewStringKeyCodec[string]() |
| 42 | + // BytesKey can be used to encode bytes keys. The encoding will just use |
| 43 | + // the provided bytes. |
| 44 | + // When used as the non-terminal part of a multipart key, we prefix the bytes key |
| 45 | + // with a single byte representing the length of the key. This means two things: |
| 46 | + // 1. When used in multipart keys the length can be at maximum 255 (max number that |
| 47 | + // can be represented with a single byte). |
| 48 | + // 2. When used in multipart keys the lexicographical ordering is lost due to the |
| 49 | + // length prefixing. |
| 50 | + // JSON encoding represents a bytes key as a hex encoded string. |
| 51 | + BytesKey = codec.NewBytesKey[[]byte]() |
| 52 | + // BoolKey can be used to encode booleans. It uses a single byte to represent the boolean. |
| 53 | + // 0x0 is used to represent false, and 0x1 is used to represent true. |
| 54 | + BoolKey = codec.NewBoolKey[bool]() |
| 55 | +) |
| 56 | + |
| 57 | +// VALUES |
| 58 | + |
| 59 | +var ( |
| 60 | + // BoolValue implements a ValueCodec for bool. |
| 61 | + BoolValue = codec.KeyToValueCodec(BoolKey) |
| 62 | + // Uint16Value implements a ValueCodec for uint16. |
| 63 | + Uint16Value = codec.KeyToValueCodec(Uint16Key) |
| 64 | + // Uint32Value implements a ValueCodec for uint32. |
| 65 | + Uint32Value = codec.KeyToValueCodec(Uint32Key) |
| 66 | + // Uint64Value implements a ValueCodec for uint64. |
| 67 | + Uint64Value = codec.KeyToValueCodec(Uint64Key) |
| 68 | + // Int32Value implements a ValueCodec for int32. |
| 69 | + Int32Value = codec.KeyToValueCodec(Int32Key) |
| 70 | + // Int64Value implements a ValueCodec for int64. |
| 71 | + Int64Value = codec.KeyToValueCodec(Int64Key) |
| 72 | + // StringValue implements a ValueCodec for string. |
| 73 | + StringValue = codec.KeyToValueCodec(StringKey) |
| 74 | + // BytesValue implements a ValueCodec for bytes. |
| 75 | + BytesValue = codec.KeyToValueCodec(BytesKey) |
| 76 | +) |
| 77 | + |
| 78 | +// Collection is the interface that all collections implement. It will eventually |
| 79 | +// include methods for importing/exporting genesis data and schema |
| 80 | +// reflection for clients. |
| 81 | +// NOTE: Unstable. |
| 82 | +type Collection interface { |
| 83 | + // GetName is the unique name of the collection within a schema. It must |
| 84 | + // match format specified by NameRegex. |
| 85 | + GetName() string |
| 86 | + |
| 87 | + // GetPrefix is the unique prefix of the collection within a schema. |
| 88 | + GetPrefix() []byte |
| 89 | + |
| 90 | + // ValueCodec returns the codec used to encode/decode values of the collection. |
| 91 | + ValueCodec() codec.UntypedValueCodec |
| 92 | + |
| 93 | + genesisHandler |
| 94 | + |
| 95 | + // collectionSchemaCodec returns the schema codec for this collection. |
| 96 | + schemaCodec() (*collectionSchemaCodec, error) |
| 97 | + |
| 98 | + // isSecondaryIndex indicates that this collection represents a secondary index |
| 99 | + // in the schema and should be excluded from the module's user facing schema. |
| 100 | + isSecondaryIndex() bool |
| 101 | +} |
| 102 | + |
| 103 | +// collectionSchemaCodec maps a collection to a schema object type and provides |
| 104 | +// decoders and encoders to and from schema values and raw kv-store bytes. |
| 105 | +type collectionSchemaCodec struct { |
| 106 | + coll Collection |
| 107 | + objectType schema.StateObjectType |
| 108 | + keyDecoder func([]byte) (any, error) |
| 109 | + valueDecoder func([]byte) (any, error) |
| 110 | +} |
| 111 | + |
| 112 | +// Prefix defines a segregation bytes namespace for specific collections objects. |
| 113 | +type Prefix []byte |
| 114 | + |
| 115 | +// Bytes returns the raw Prefix bytes. |
| 116 | +func (n Prefix) Bytes() []byte { return n } |
| 117 | + |
| 118 | +// NewPrefix returns a Prefix given the provided namespace identifier. |
| 119 | +// In the same module, no prefixes should share the same starting bytes |
| 120 | +// meaning that having two namespaces whose bytes representation is: |
| 121 | +// p1 := []byte("prefix") |
| 122 | +// p2 := []byte("prefix1") |
| 123 | +// yields to iterations of p1 overlapping over p2. |
| 124 | +// If a numeric prefix is provided, it must be between 0 and 255 (uint8). |
| 125 | +// If out of bounds this function will panic. |
| 126 | +// Reason for which this function is constrained to `int` instead of `uint8` is for |
| 127 | +// API ergonomics, golang's type inference will infer int properly but not uint8 |
| 128 | +// meaning that developers would need to write NewPrefix(uint8(number)) for numeric |
| 129 | +// prefixes. |
| 130 | +func NewPrefix[T interface{ int | string | []byte }](identifier T) Prefix { |
| 131 | + i := any(identifier) |
| 132 | + var prefix []byte |
| 133 | + switch c := i.(type) { |
| 134 | + case int: |
| 135 | + if c > math.MaxUint8 || c < 0 { |
| 136 | + panic("invalid integer prefix value: must be between 0 and 255") |
| 137 | + } |
| 138 | + prefix = []byte{uint8(c)} |
| 139 | + case string: |
| 140 | + prefix = []byte(c) |
| 141 | + case []byte: |
| 142 | + identifierCopy := make([]byte, len(c)) |
| 143 | + copy(identifierCopy, c) |
| 144 | + prefix = identifierCopy |
| 145 | + } |
| 146 | + return prefix |
| 147 | +} |
| 148 | + |
| 149 | +var _ Collection = (*collectionImpl[string, string])(nil) |
| 150 | + |
| 151 | +// collectionImpl wraps a Map and implements Collection. This properly splits |
| 152 | +// the generic and untyped Collection interface from the typed Map, which every |
| 153 | +// collection builds on. |
| 154 | +type collectionImpl[K, V any] struct { |
| 155 | + m Map[K, V] |
| 156 | +} |
| 157 | + |
| 158 | +func (c collectionImpl[K, V]) ValueCodec() codec.UntypedValueCodec { |
| 159 | + return codec.NewUntypedValueCodec(c.m.vc) |
| 160 | +} |
| 161 | + |
| 162 | +func (c collectionImpl[K, V]) GetName() string { return c.m.name } |
| 163 | + |
| 164 | +func (c collectionImpl[K, V]) GetPrefix() []byte { return NewPrefix(c.m.prefix) } |
| 165 | + |
| 166 | +func (c collectionImpl[K, V]) validateGenesis(r io.Reader) error { return c.m.validateGenesis(r) } |
| 167 | + |
| 168 | +func (c collectionImpl[K, V]) importGenesis(ctx context.Context, r io.Reader) error { |
| 169 | + return c.m.importGenesis(ctx, r) |
| 170 | +} |
| 171 | + |
| 172 | +func (c collectionImpl[K, V]) exportGenesis(ctx context.Context, w io.Writer) error { |
| 173 | + return c.m.exportGenesis(ctx, w) |
| 174 | +} |
| 175 | + |
| 176 | +func (c collectionImpl[K, V]) defaultGenesis(w io.Writer) error { return c.m.defaultGenesis(w) } |
| 177 | + |
| 178 | +func (c collectionImpl[K, V]) isSecondaryIndex() bool { return c.m.isSecondaryIndex } |
0 commit comments