forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquad.go
435 lines (388 loc) · 12.1 KB
/
quad.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package collections
import (
"encoding/json"
"fmt"
"strings"
"cosmossdk.io/collections/codec"
"cosmossdk.io/schema"
)
// Quad defines a multipart key composed of four keys.
type Quad[K1, K2, K3, K4 any] struct {
k1 *K1
k2 *K2
k3 *K3
k4 *K4
}
// Join4 instantiates a new Quad instance composed of the four provided keys, in order.
func Join4[K1, K2, K3, K4 any](k1 K1, k2 K2, k3 K3, k4 K4) Quad[K1, K2, K3, K4] {
return Quad[K1, K2, K3, K4]{&k1, &k2, &k3, &k4}
}
// K1 returns the first part of the key. If nil, the zero value is returned.
func (t Quad[K1, K2, K3, K4]) K1() (x K1) {
if t.k1 != nil {
return *t.k1
}
return x
}
// K2 returns the second part of the key. If nil, the zero value is returned.
func (t Quad[K1, K2, K3, K4]) K2() (x K2) {
if t.k2 != nil {
return *t.k2
}
return x
}
// K3 returns the third part of the key. If nil, the zero value is returned.
func (t Quad[K1, K2, K3, K4]) K3() (x K3) {
if t.k3 != nil {
return *t.k3
}
return x
}
// K4 returns the fourth part of the key. If nil, the zero value is returned.
func (t Quad[K1, K2, K3, K4]) K4() (x K4) {
if t.k4 != nil {
return *t.k4
}
return x
}
// QuadPrefix creates a new Quad instance composed only of the first part of the key.
func QuadPrefix[K1, K2, K3, K4 any](k1 K1) Quad[K1, K2, K3, K4] {
return Quad[K1, K2, K3, K4]{k1: &k1}
}
// QuadSuperPrefix creates a new Quad instance composed only of the first two parts of the key.
func QuadSuperPrefix[K1, K2, K3, K4 any](k1 K1, k2 K2) Quad[K1, K2, K3, K4] {
return Quad[K1, K2, K3, K4]{k1: &k1, k2: &k2}
}
// QuadSuperPrefix3 creates a new Quad instance composed only of the first three parts of the key.
func QuadSuperPrefix3[K1, K2, K3, K4 any](k1 K1, k2 K2, k3 K3) Quad[K1, K2, K3, K4] {
return Quad[K1, K2, K3, K4]{k1: &k1, k2: &k2, k3: &k3}
}
// QuadKeyCodec instantiates a new KeyCodec instance that can encode the Quad, given
// the KeyCodecs of the four parts of the key, in order.
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]] {
return quadKeyCodec[K1, K2, K3, K4]{
keyCodec1: keyCodec1,
keyCodec2: keyCodec2,
keyCodec3: keyCodec3,
keyCodec4: keyCodec4,
}
}
// 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 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]] {
return quadKeyCodec[K1, K2, K3, K4]{
name1: key1Name,
keyCodec1: keyCodec1,
name2: key2Name,
keyCodec2: keyCodec2,
name3: key3Name,
keyCodec3: keyCodec3,
name4: key4Name,
keyCodec4: keyCodec4,
}
}
type quadKeyCodec[K1, K2, K3, K4 any] struct {
name1, name2, name3, name4 string
keyCodec1 codec.KeyCodec[K1]
keyCodec2 codec.KeyCodec[K2]
keyCodec3 codec.KeyCodec[K3]
keyCodec4 codec.KeyCodec[K4]
}
type jsonQuadKey [4]json.RawMessage
// EncodeJSON encodes Quads to json
func (t quadKeyCodec[K1, K2, K3, K4]) EncodeJSON(value Quad[K1, K2, K3, K4]) ([]byte, error) {
json1, err := t.keyCodec1.EncodeJSON(*value.k1)
if err != nil {
return nil, err
}
json2, err := t.keyCodec2.EncodeJSON(*value.k2)
if err != nil {
return nil, err
}
json3, err := t.keyCodec3.EncodeJSON(*value.k3)
if err != nil {
return nil, err
}
json4, err := t.keyCodec4.EncodeJSON(*value.k4)
if err != nil {
return nil, err
}
return json.Marshal(jsonQuadKey{json1, json2, json3, json4})
}
// DecodeJSON decodes json to Quads
func (t quadKeyCodec[K1, K2, K3, K4]) DecodeJSON(b []byte) (Quad[K1, K2, K3, K4], error) {
var jsonKey jsonQuadKey
err := json.Unmarshal(b, &jsonKey)
if err != nil {
return Quad[K1, K2, K3, K4]{}, err
}
key1, err := t.keyCodec1.DecodeJSON(jsonKey[0])
if err != nil {
return Quad[K1, K2, K3, K4]{}, err
}
key2, err := t.keyCodec2.DecodeJSON(jsonKey[1])
if err != nil {
return Quad[K1, K2, K3, K4]{}, err
}
key3, err := t.keyCodec3.DecodeJSON(jsonKey[2])
if err != nil {
return Quad[K1, K2, K3, K4]{}, err
}
key4, err := t.keyCodec4.DecodeJSON(jsonKey[3])
if err != nil {
return Quad[K1, K2, K3, K4]{}, err
}
return Join4(key1, key2, key3, key4), nil
}
// Stringify converts Quads to string
func (t quadKeyCodec[K1, K2, K3, K4]) Stringify(key Quad[K1, K2, K3, K4]) string {
b := new(strings.Builder)
b.WriteByte('(')
if key.k1 != nil {
b.WriteByte('"')
b.WriteString(t.keyCodec1.Stringify(*key.k1))
b.WriteByte('"')
} else {
b.WriteString("<nil>")
}
b.WriteString(", ")
if key.k2 != nil {
b.WriteByte('"')
b.WriteString(t.keyCodec2.Stringify(*key.k2))
b.WriteByte('"')
} else {
b.WriteString("<nil>")
}
b.WriteString(", ")
if key.k3 != nil {
b.WriteByte('"')
b.WriteString(t.keyCodec3.Stringify(*key.k3))
b.WriteByte('"')
} else {
b.WriteString("<nil>")
}
b.WriteString(", ")
if key.k4 != nil {
b.WriteByte('"')
b.WriteString(t.keyCodec4.Stringify(*key.k4))
b.WriteByte('"')
} else {
b.WriteString("<nil>")
}
b.WriteByte(')')
return b.String()
}
func (t quadKeyCodec[K1, K2, K3, K4]) KeyType() string {
return fmt.Sprintf("Quad[%s,%s,%s,%s]", t.keyCodec1.KeyType(), t.keyCodec2.KeyType(), t.keyCodec3.KeyType(), t.keyCodec4.KeyType())
}
func (t quadKeyCodec[K1, K2, K3, K4]) Encode(buffer []byte, key Quad[K1, K2, K3, K4]) (int, error) {
writtenTotal := 0
if key.k1 != nil {
written, err := t.keyCodec1.EncodeNonTerminal(buffer, *key.k1)
if err != nil {
return 0, err
}
writtenTotal += written
}
if key.k2 != nil {
written, err := t.keyCodec2.EncodeNonTerminal(buffer[writtenTotal:], *key.k2)
if err != nil {
return 0, err
}
writtenTotal += written
}
if key.k3 != nil {
written, err := t.keyCodec3.EncodeNonTerminal(buffer[writtenTotal:], *key.k3)
if err != nil {
return 0, err
}
writtenTotal += written
}
if key.k4 != nil {
written, err := t.keyCodec4.Encode(buffer[writtenTotal:], *key.k4)
if err != nil {
return 0, err
}
writtenTotal += written
}
return writtenTotal, nil
}
func (t quadKeyCodec[K1, K2, K3, K4]) Decode(buffer []byte) (int, Quad[K1, K2, K3, K4], error) {
readTotal := 0
read, key1, err := t.keyCodec1.DecodeNonTerminal(buffer)
if err != nil {
return 0, Quad[K1, K2, K3, K4]{}, err
}
readTotal += read
read, key2, err := t.keyCodec2.DecodeNonTerminal(buffer[readTotal:])
if err != nil {
return 0, Quad[K1, K2, K3, K4]{}, err
}
readTotal += read
read, key3, err := t.keyCodec3.DecodeNonTerminal(buffer[readTotal:])
if err != nil {
return 0, Quad[K1, K2, K3, K4]{}, err
}
readTotal += read
read, key4, err := t.keyCodec4.Decode(buffer[readTotal:])
if err != nil {
return 0, Quad[K1, K2, K3, K4]{}, err
}
readTotal += read
return readTotal, Join4(key1, key2, key3, key4), nil
}
func (t quadKeyCodec[K1, K2, K3, K4]) Size(key Quad[K1, K2, K3, K4]) int {
size := 0
if key.k1 != nil {
size += t.keyCodec1.SizeNonTerminal(*key.k1)
}
if key.k2 != nil {
size += t.keyCodec2.SizeNonTerminal(*key.k2)
}
if key.k3 != nil {
size += t.keyCodec3.SizeNonTerminal(*key.k3)
}
if key.k4 != nil {
size += t.keyCodec4.Size(*key.k4)
}
return size
}
func (t quadKeyCodec[K1, K2, K3, K4]) EncodeNonTerminal(buffer []byte, key Quad[K1, K2, K3, K4]) (int, error) {
writtenTotal := 0
if key.k1 != nil {
written, err := t.keyCodec1.EncodeNonTerminal(buffer, *key.k1)
if err != nil {
return 0, err
}
writtenTotal += written
}
if key.k2 != nil {
written, err := t.keyCodec2.EncodeNonTerminal(buffer[writtenTotal:], *key.k2)
if err != nil {
return 0, err
}
writtenTotal += written
}
if key.k3 != nil {
written, err := t.keyCodec3.EncodeNonTerminal(buffer[writtenTotal:], *key.k3)
if err != nil {
return 0, err
}
writtenTotal += written
}
if key.k4 != nil {
written, err := t.keyCodec4.EncodeNonTerminal(buffer[writtenTotal:], *key.k4)
if err != nil {
return 0, err
}
writtenTotal += written
}
return writtenTotal, nil
}
func (t quadKeyCodec[K1, K2, K3, K4]) DecodeNonTerminal(buffer []byte) (int, Quad[K1, K2, K3, K4], error) {
readTotal := 0
read, key1, err := t.keyCodec1.DecodeNonTerminal(buffer)
if err != nil {
return 0, Quad[K1, K2, K3, K4]{}, err
}
readTotal += read
read, key2, err := t.keyCodec2.DecodeNonTerminal(buffer[readTotal:])
if err != nil {
return 0, Quad[K1, K2, K3, K4]{}, err
}
readTotal += read
read, key3, err := t.keyCodec3.DecodeNonTerminal(buffer[readTotal:])
if err != nil {
return 0, Quad[K1, K2, K3, K4]{}, err
}
readTotal += read
read, key4, err := t.keyCodec4.DecodeNonTerminal(buffer[readTotal:])
if err != nil {
return 0, Quad[K1, K2, K3, K4]{}, err
}
readTotal += read
return readTotal, Join4(key1, key2, key3, key4), nil
}
func (t quadKeyCodec[K1, K2, K3, K4]) SizeNonTerminal(key Quad[K1, K2, K3, K4]) int {
size := 0
if key.k1 != nil {
size += t.keyCodec1.SizeNonTerminal(*key.k1)
}
if key.k2 != nil {
size += t.keyCodec2.SizeNonTerminal(*key.k2)
}
if key.k3 != nil {
size += t.keyCodec3.SizeNonTerminal(*key.k3)
}
if key.k4 != nil {
size += t.keyCodec4.SizeNonTerminal(*key.k4)
}
return size
}
func (t quadKeyCodec[K1, K2, K3, K4]) SchemaCodec() (codec.SchemaCodec[Quad[K1, K2, K3, K4]], error) {
field1, err := getNamedKeyField(t.keyCodec1, t.name1)
if err != nil {
return codec.SchemaCodec[Quad[K1, K2, K3, K4]]{}, fmt.Errorf("error getting key1 field: %w", err)
}
field2, err := getNamedKeyField(t.keyCodec2, t.name2)
if err != nil {
return codec.SchemaCodec[Quad[K1, K2, K3, K4]]{}, fmt.Errorf("error getting key2 field: %w", err)
}
field3, err := getNamedKeyField(t.keyCodec3, t.name3)
if err != nil {
return codec.SchemaCodec[Quad[K1, K2, K3, K4]]{}, fmt.Errorf("error getting key3 field: %w", err)
}
field4, err := getNamedKeyField(t.keyCodec4, t.name4)
if err != nil {
return codec.SchemaCodec[Quad[K1, K2, K3, K4]]{}, fmt.Errorf("error getting key4 field: %w", err)
}
return codec.SchemaCodec[Quad[K1, K2, K3, K4]]{
Fields: []schema.Field{field1, field2, field3, field4},
ToSchemaType: func(q Quad[K1, K2, K3, K4]) (any, error) {
return []interface{}{q.K1(), q.K2(), q.K3(), q.K4()}, nil
},
FromSchemaType: func(a any) (Quad[K1, K2, K3, K4], error) {
aSlice, ok := a.([]interface{})
if !ok || len(aSlice) != 4 {
return Quad[K1, K2, K3, K4]{}, fmt.Errorf("expected slice of length 4, got %T", a)
}
return Join4(aSlice[0].(K1), aSlice[1].(K2), aSlice[2].(K3), aSlice[3].(K4)), nil
},
}, nil
}
// NewPrefixUntilQuadRange defines a collection query which ranges until the provided Quad prefix.
// Unstable: this API might change in the future.
func NewPrefixUntilQuadRange[K1, K2, K3, K4 any](k1 K1) Ranger[Quad[K1, K2, K3, K4]] {
key := QuadPrefix[K1, K2, K3, K4](k1)
return &Range[Quad[K1, K2, K3, K4]]{
end: RangeKeyPrefixEnd(key),
}
}
// NewPrefixedQuadRange provides a Range for all keys prefixed with the given
// first part of the Quad key.
func NewPrefixedQuadRange[K1, K2, K3, K4 any](k1 K1) Ranger[Quad[K1, K2, K3, K4]] {
key := QuadPrefix[K1, K2, K3, K4](k1)
return &Range[Quad[K1, K2, K3, K4]]{
start: RangeKeyExact(key),
end: RangeKeyPrefixEnd(key),
}
}
// NewSuperPrefixedQuadRange provides a Range for all keys prefixed with the given
// first and second parts of the Quad key.
func NewSuperPrefixedQuadRange[K1, K2, K3, K4 any](k1 K1, k2 K2) Ranger[Quad[K1, K2, K3, K4]] {
key := QuadSuperPrefix[K1, K2, K3, K4](k1, k2)
return &Range[Quad[K1, K2, K3, K4]]{
start: RangeKeyExact(key),
end: RangeKeyPrefixEnd(key),
}
}
// NewSuperPrefixedQuadRange3 provides a Range for all keys prefixed with the given
// first, second and third parts of the Quad key.
func NewSuperPrefixedQuadRange3[K1, K2, K3, K4 any](k1 K1, k2 K2, k3 K3) Ranger[Quad[K1, K2, K3, K4]] {
key := QuadSuperPrefix3[K1, K2, K3, K4](k1, k2, k3)
return &Range[Quad[K1, K2, K3, K4]]{
start: RangeKeyExact(key),
end: RangeKeyPrefixEnd(key),
}
}