forked from bluesky-social/indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmst.go
988 lines (826 loc) · 26 KB
/
mst.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
// Package mst contains a Merkle Search Tree (MST) implementation for atproto.
//
// This implementation is a port of the Typescript implementation in the
// `atproto` git repo.
//
// ## Notes copied from TS repo
//
// This is an implementation of a Merkle Search Tree (MST)
// The data structure is described here: https://hal.inria.fr/hal-02303490/document
// The MST is an ordered, insert-order-independent, deterministic tree.
// Keys are laid out in alphabetic order.
// The key insight of an MST is that each key is hashed and starting 0s are counted
// to determine which layer it falls on (5 zeros for ~32 fanout).
// This is a merkle tree, so each subtree is referred to by it's hash (CID).
// When a leaf is changed, ever tree on the path to that leaf is changed as well,
// thereby updating the root hash.
//
// For atproto, we use SHA-256 as the key hashing algorithm, and ~16 fanout
// (4-bits of zero per layer).
//
// NOTE: currently keys are strings, not bytes. Because UTF-8 strings can't be
// safely split at arbitrary byte boundaries (the results are not necessarily
// valid UTF-8 strings), this means that "wide" characters not really supported
// in keys, particularly across programming language implementations. We
// recommend sticking with simple alphanumeric (ASCII) strings.
//
// A couple notes on CBOR encoding:
//
// There are never two neighboring subtrees.
// Therefore, we can represent a node as an array of
// leaves & pointers to their right neighbor (possibly null),
// along with a pointer to the left-most subtree (also possibly null).
//
// Most keys in a subtree will have overlap.
// We do compression on prefixes by describing keys as:
// - the length of the prefix that it shares in common with the preceding key
// - the rest of the string
//
// For example:
// If the first leaf in a tree is `bsky/posts/abcdefg` and the second is `bsky/posts/abcdehi`
// Then the first will be described as `prefix: 0, key: 'bsky/posts/abcdefg'`,
// and the second will be described as `prefix: 16, key: 'hi'.`
package mst
import (
"context"
"fmt"
"reflect"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
)
// nodeKind is the type of node in the MST.
type nodeKind uint8
const (
entryUndefined nodeKind = 0
entryLeaf nodeKind = 1
entryTree nodeKind = 2
)
// nodeEntry is a node in the MST.
//
// Following the Typescript implementation, this is basically a flexible
// "TreeEntry" (aka "leaf") which might also be the "Left" pointer on a
// NodeData (aka "tree"). This type flexibility is not idiomatic in Go, but
// we are keeping this a very direct port.
type nodeEntry struct {
Kind nodeKind
Key string
Val cid.Cid
Tree *MerkleSearchTree
}
func mkTreeEntry(t *MerkleSearchTree) nodeEntry {
return nodeEntry{
Kind: entryTree,
Tree: t,
}
}
func (ne nodeEntry) isTree() bool {
return ne.Kind == entryTree
}
func (ne nodeEntry) isLeaf() bool {
return ne.Kind == entryLeaf
}
func (ne nodeEntry) isUndefined() bool {
return ne.Kind == entryUndefined
}
// golang-specific helper to sanity check nodeEntry semantics
func checkTreeInvariant(ents []nodeEntry) {
for i := 0; i < len(ents)-1; i++ {
if ents[i].isTree() && ents[i+1].isTree() {
panic(fmt.Sprintf("two trees next to each other! %d %d", i, i+1))
}
}
}
// CBORTypes returns the types in this package that need to be registered with
// the CBOR codec.
func CBORTypes() []reflect.Type {
return []reflect.Type{
reflect.TypeOf(nodeData{}),
reflect.TypeOf(treeEntry{}),
}
}
// MST tree node as gets serialized to CBOR. Note that the CBOR fields are all
// single-character.
type nodeData struct {
Left *cid.Cid `cborgen:"l"` // [optional] pointer to lower-level subtree to the "left" of this path/key
Entries []treeEntry `cborgen:"e"` // ordered list of entries at this node
}
// treeEntry are elements of nodeData's Entries.
type treeEntry struct {
PrefixLen int64 `cborgen:"p"` // count of characters shared with previous path/key in tree
KeySuffix []byte `cborgen:"k"` // remaining part of path/key (appended to "previous key")
Val cid.Cid `cborgen:"v"` // CID pointer at this path/key
Tree *cid.Cid `cborgen:"t"` // [optional] pointer to lower-level subtree to the "right" of this path/key entry
}
// MerkleSearchTree represents an MST tree node (NodeData type). It can be in
// several levels of hydration: fully hydrated (entries and "pointer" (CID)
// computed); dirty (entries correct, but pointer (CID) not valid); virtual
// (pointer is defined, no entries have been pulled from blockstore)
//
// MerkleSearchTree values are immutable. Methods return copies with changes.
type MerkleSearchTree struct {
cst cbor.IpldStore
entries []nodeEntry // non-nil when "hydrated"
layer int
pointer cid.Cid
validPtr bool
}
// NewEmptyMST reports a new empty MST using cst as its storage.
func NewEmptyMST(cst cbor.IpldStore) *MerkleSearchTree {
return createMST(cst, cid.Undef, []nodeEntry{}, 0)
}
// Typescript: MST.create(storage, entries, layer, fanout) -> MST
func createMST(cst cbor.IpldStore, ptr cid.Cid, entries []nodeEntry, layer int) *MerkleSearchTree {
mst := &MerkleSearchTree{
cst: cst,
pointer: ptr,
layer: layer,
entries: entries,
validPtr: ptr.Defined(),
}
return mst
}
// TODO: Typescript: MST.fromData(storage, data, layer=null, fanout)
// This is poorly named in both implementations, because it is lazy
// Typescript: MST.load(storage, cid, layer=null, fanout) -> MST
func LoadMST(cst cbor.IpldStore, root cid.Cid) *MerkleSearchTree {
return createMST(cst, root, nil, -1)
}
// === "Immutability" ===
// "We never mutate an MST, we just return a new MST with updated values"
// Typescript: MST.newTree(entries) -> MST
func (mst *MerkleSearchTree) newTree(entries []nodeEntry) *MerkleSearchTree {
if entries == nil {
panic("nil entries passed to newTree")
}
return createMST(mst.cst, cid.Undef, entries, mst.layer)
}
// === "Getters (lazy load)" ===
// "We don't want to load entries of every subtree, just the ones we need"
// Typescript: MST.getEntries() -> nodeEntry[]
func (mst *MerkleSearchTree) getEntries(ctx context.Context) ([]nodeEntry, error) {
// if we are "hydrated", entries are available
if mst.entries != nil {
return mst.entries, nil
}
// otherwise this is a virtual/pointer struct and we need to hydrate from
// blockstore before returning entries
if mst.pointer != cid.Undef {
var nd nodeData
if err := mst.cst.Get(ctx, mst.pointer, &nd); err != nil {
return nil, err
}
// NOTE(bnewbold): Typescript version computes layer in-place here, but
// the entriesFromNodeData() helper does that for us in golang
entries, err := entriesFromNodeData(ctx, &nd, mst.cst)
if err != nil {
return nil, err
}
if entries == nil {
panic("got nil entries from node data decoding")
}
mst.entries = entries
return entries, nil
}
return nil, fmt.Errorf("no entries or self-pointer (CID) on MerkleSearchTree")
}
// golang-specific helper that calls in to deserializeNodeData
func entriesFromNodeData(ctx context.Context, nd *nodeData, cst cbor.IpldStore) ([]nodeEntry, error) {
layer := -1
if len(nd.Entries) > 0 {
// NOTE(bnewbold): can compute the layer on the first KeySuffix, because for the first entry that field is a complete key
firstLeaf := nd.Entries[0]
layer = leadingZerosOnHashBytes(firstLeaf.KeySuffix)
}
entries, err := deserializeNodeData(ctx, cst, nd, layer)
if err != nil {
return nil, err
}
return entries, nil
}
// "We don't hash the node on every mutation for performance reasons. Instead we keep track of whether the pointer is outdated and only (recursively) calculate when needed"
// Typescript: MST.getPointer() -> CID
func (mst *MerkleSearchTree) GetPointer(ctx context.Context) (cid.Cid, error) {
if mst.validPtr {
return mst.pointer, nil
}
// NOTE(bnewbold): this is a bit different from how Typescript version works
// update in-place; first ensure that mst.entries is hydrated
_, err := mst.getEntries(ctx)
if err != nil {
return cid.Undef, err
}
for i, e := range mst.entries {
if e.isTree() {
if !e.Tree.validPtr {
_, err := e.Tree.GetPointer(ctx)
if err != nil {
return cid.Undef, err
}
mst.entries[i] = e
}
}
}
nptr, err := cidForEntries(ctx, mst.entries, mst.cst)
if err != nil {
return cid.Undef, err
}
mst.pointer = nptr
mst.validPtr = true
return mst.pointer, nil
}
// "In most cases, we get the layer of a node from a hint on creation"
// "In the case of the topmost node in the tree, we look for a key in the node & determine the layer"
// "In the case where we don't find one, we recurse down until we do."
// "If we still can't find one, then we have an empty tree and the node is layer 0"
// Typescript: MST.getLayer() -> number
func (mst *MerkleSearchTree) getLayer(ctx context.Context) (int, error) {
layer, err := mst.attemptGetLayer(ctx)
if err != nil {
return -1, err
}
if layer < 0 {
mst.layer = 0
} else {
mst.layer = layer
}
return mst.layer, nil
}
// Typescript: MST.attemptGetLayer() -> number
func (mst *MerkleSearchTree) attemptGetLayer(ctx context.Context) (int, error) {
if mst.layer >= 0 {
return mst.layer, nil
}
entries, err := mst.getEntries(ctx)
if err != nil {
return -1, err
}
layer := layerForEntries(entries)
if layer < 0 {
// NOTE(bnewbold): updated this from typescript
for _, e := range entries {
if e.isTree() {
childLayer, err := e.Tree.attemptGetLayer(ctx)
if err != nil {
return -1, err
}
if childLayer >= 0 {
layer = childLayer + 1
break
}
}
}
}
if layer >= 0 {
mst.layer = layer
}
return mst.layer, nil
}
// === "Core functionality" ===
// NOTE: MST.getUnstoredBlocks() not needed; we are always working out of
// blockstore in this implementation
// "Adds a new leaf for the given key/value pair. Throws if a leaf with that key already exists"
// Typescript: MST.add(key, value, knownZeros?) -> MST
func (mst *MerkleSearchTree) Add(ctx context.Context, key string, val cid.Cid, knownZeros int) (*MerkleSearchTree, error) {
// NOTE(bnewbold): this is inefficient (recurses), but matches TS implementation
err := ensureValidMstKey(key)
if err != nil {
return nil, err
}
if val == cid.Undef {
return nil, fmt.Errorf("tried to insert an undef CID")
}
keyZeros := knownZeros
if keyZeros < 0 {
keyZeros = leadingZerosOnHash(key)
}
layer, err := mst.getLayer(ctx)
if err != nil {
return nil, fmt.Errorf("getting layer failed: %w", err)
}
newLeaf := nodeEntry{
Kind: entryLeaf,
Key: key,
Val: val,
}
if keyZeros == layer {
// it belongs in this layer
index, err := mst.findGtOrEqualLeafIndex(ctx, key)
if err != nil {
return nil, err
}
found, err := mst.atIndex(index)
if err != nil {
return nil, err
}
if found.isLeaf() && found.Key == key {
return nil, fmt.Errorf("value already set at key: %s", key)
}
prevNode, err := mst.atIndex(index - 1)
if err != nil {
return nil, err
}
if prevNode.isUndefined() || prevNode.isLeaf() {
// "if entry before is a leaf, (or we're on far left) we can just splice in"
return mst.spliceIn(ctx, newLeaf, index)
}
// "else we try to split the subtree around the key"
left, right, err := prevNode.Tree.splitAround(ctx, key)
if err != nil {
return nil, err
}
// NOTE(bnewbold): added this replaceWithSplit() call
return mst.replaceWithSplit(ctx, index-1, left, newLeaf, right)
} else if keyZeros < layer {
// "it belongs on a lower layer"
index, err := mst.findGtOrEqualLeafIndex(ctx, key)
if err != nil {
return nil, err
}
prevNode, err := mst.atIndex(index - 1)
if err != nil {
return nil, err
}
if !prevNode.isUndefined() && prevNode.isTree() {
// "if entry before is a tree, we add it to that tree"
newSubtree, err := prevNode.Tree.Add(ctx, key, val, keyZeros)
if err != nil {
return nil, err
}
return mst.updateEntry(ctx, index-1, mkTreeEntry(newSubtree))
} else {
subTree, err := mst.createChild(ctx)
if err != nil {
return nil, err
}
newSubTree, err := subTree.Add(ctx, key, val, keyZeros)
if err != nil {
return nil, fmt.Errorf("subtree add: %w", err)
}
return mst.spliceIn(ctx, mkTreeEntry(newSubTree), index)
}
} else {
// "it belongs on a higher layer & we must push the rest of the tree down"
left, right, err := mst.splitAround(ctx, key)
if err != nil {
return nil, err
}
// "if the newly added key has >=2 more leading zeros than the current highest layer then we need to add in structural nodes in between as well"
layer, err := mst.getLayer(ctx)
if err != nil {
return nil, fmt.Errorf("get layer in split case failed: %w", err)
}
extraLayersToAdd := keyZeros - layer
// "intentionally starting at 1, since first layer is taken care of by split"
for i := 1; i < extraLayersToAdd; i++ {
// seems bad if both left and right are non nil
if left != nil {
par, err := left.createParent(ctx)
if err != nil {
return nil, fmt.Errorf("create left parent: %w", err)
}
left = par
}
if right != nil {
par, err := right.createParent(ctx)
if err != nil {
return nil, fmt.Errorf("create right parent: %w", err)
}
right = par
}
}
var updated []nodeEntry
if left != nil {
updated = append(updated, mkTreeEntry(left))
}
updated = append(updated, nodeEntry{
Kind: entryLeaf,
Key: key,
Val: val,
})
if right != nil {
updated = append(updated, mkTreeEntry(right))
}
checkTreeInvariant(updated)
newRoot := createMST(mst.cst, cid.Undef, updated, keyZeros)
// NOTE(bnewbold): We do want to invalid the CID (because this node has
// changed, and we are "lazy" about recomputing). Setting this flag
// is redundant with passing cid.Undef to NewMST just above, but
// keeping because it is explicit and matches the explicit invalidation
// that happens in the Typescript code
newRoot.validPtr = false
return newRoot, nil
}
}
var ErrNotFound = fmt.Errorf("mst: not found")
// "Gets the value at the given key"
// Typescript: MST.get(key) -> (CID|null)
func (mst *MerkleSearchTree) Get(ctx context.Context, k string) (cid.Cid, error) {
index, err := mst.findGtOrEqualLeafIndex(ctx, k)
if err != nil {
return cid.Undef, err
}
found, err := mst.atIndex(index)
if err != nil {
return cid.Undef, err
}
if !found.isUndefined() && found.isLeaf() && found.Key == k {
return found.Val, nil
}
prev, err := mst.atIndex(index - 1)
if err != nil {
return cid.Undef, err
}
if !prev.isUndefined() && prev.isTree() {
return prev.Tree.Get(ctx, k)
}
return cid.Undef, ErrNotFound
}
// "Edits the value at the given key. Throws if the given key does not exist"
// Typescript: MST.update(key, value) -> MST
func (mst *MerkleSearchTree) Update(ctx context.Context, k string, val cid.Cid) (*MerkleSearchTree, error) {
// NOTE(bnewbold): this is inefficient (recurses), but matches TS implementation
err := ensureValidMstKey(k)
if err != nil {
return nil, err
}
if val == cid.Undef {
return nil, fmt.Errorf("tried to insert an undef CID")
}
index, err := mst.findGtOrEqualLeafIndex(ctx, k)
if err != nil {
return nil, err
}
found, err := mst.atIndex(index)
if err != nil {
return nil, err
}
if !found.isUndefined() && found.isLeaf() && found.Key == k {
// NOTE(bnewbold): updated here
return mst.updateEntry(ctx, index, nodeEntry{
Kind: entryLeaf,
Key: string(k),
Val: val,
})
}
prev, err := mst.atIndex(index - 1)
if err != nil {
return nil, err
}
if !prev.isUndefined() && prev.isTree() {
updatedTree, err := prev.Tree.Update(ctx, k, val)
if err != nil {
return nil, err
}
return mst.updateEntry(ctx, index-1, mkTreeEntry(updatedTree))
}
return nil, ErrNotFound
}
// "Deletes the value at the given key"
// Typescript: MST.delete(key) -> MST
func (mst *MerkleSearchTree) Delete(ctx context.Context, k string) (*MerkleSearchTree, error) {
altered, err := mst.deleteRecurse(ctx, k)
if err != nil {
return nil, err
}
return altered.trimTop(ctx)
}
// Typescript: MST.deleteRecurse(key) -> MST
func (mst *MerkleSearchTree) deleteRecurse(ctx context.Context, k string) (*MerkleSearchTree, error) {
ix, err := mst.findGtOrEqualLeafIndex(ctx, k)
if err != nil {
return nil, err
}
found, err := mst.atIndex(ix)
if err != nil {
return nil, err
}
// "if found, remove it on this level"
if found.isLeaf() && found.Key == k {
prev, err := mst.atIndex(ix - 1)
if err != nil {
return nil, err
}
next, err := mst.atIndex(ix + 1)
if err != nil {
return nil, err
}
if prev.isTree() && next.isTree() {
merged, err := prev.Tree.appendMerge(ctx, next.Tree)
if err != nil {
return nil, err
}
entries, err := mst.getEntries(ctx)
if err != nil {
return nil, err
}
return mst.newTree(append(append(entries[:ix-1], mkTreeEntry(merged)), entries[ix+2:]...)), nil
} else {
return mst.removeEntry(ctx, ix)
}
}
// "else recurse down to find it"
prev, err := mst.atIndex(ix - 1)
if err != nil {
return nil, err
}
if prev.isTree() {
subtree, err := prev.Tree.deleteRecurse(ctx, k)
if err != nil {
return nil, err
}
subtreeEntries, err := subtree.getEntries(ctx)
if err != nil {
return nil, err
}
if len(subtreeEntries) == 0 {
return mst.removeEntry(ctx, ix-1)
} else {
return mst.updateEntry(ctx, ix-1, mkTreeEntry(subtree))
}
} else {
return nil, fmt.Errorf("could not find record with key: %s", k)
}
}
// === "Simple Operations" ===
// "update entry in place"
// Typescript: MST.updateEntry(index, entry) -> MST
func (mst *MerkleSearchTree) updateEntry(ctx context.Context, ix int, entry nodeEntry) (*MerkleSearchTree, error) {
entries, err := mst.getEntries(ctx)
if err != nil {
return nil, err
}
nents := make([]nodeEntry, len(entries))
copy(nents, entries[:ix])
nents[ix] = entry
copy(nents[ix+1:], entries[ix+1:])
checkTreeInvariant(nents)
return mst.newTree(nents), nil
}
// "remove entry at index"
func (mst *MerkleSearchTree) removeEntry(ctx context.Context, ix int) (*MerkleSearchTree, error) {
entries, err := mst.getEntries(ctx)
if err != nil {
return nil, err
}
nents := make([]nodeEntry, len(entries)-1)
copy(nents, entries[:ix])
copy(nents[ix:], entries[ix+1:])
checkTreeInvariant(nents)
return mst.newTree(nents), nil
}
// "append entry to end of the node"
func (mst *MerkleSearchTree) append(ctx context.Context, ent nodeEntry) (*MerkleSearchTree, error) {
entries, err := mst.getEntries(ctx)
if err != nil {
return nil, err
}
nents := make([]nodeEntry, len(entries)+1)
copy(nents, entries)
nents[len(nents)-1] = ent
checkTreeInvariant(nents)
return mst.newTree(nents), nil
}
// "prepend entry to start of the node"
func (mst *MerkleSearchTree) prepend(ctx context.Context, ent nodeEntry) (*MerkleSearchTree, error) {
entries, err := mst.getEntries(ctx)
if err != nil {
return nil, err
}
nents := make([]nodeEntry, len(entries)+1)
copy(nents[1:], entries)
nents[0] = ent
checkTreeInvariant(nents)
return mst.newTree(nents), nil
}
// "returns entry at index"
// Apparently returns null if nothing at index, which seems brittle
func (mst *MerkleSearchTree) atIndex(ix int) (nodeEntry, error) {
entries, err := mst.getEntries(context.TODO())
if err != nil {
return nodeEntry{}, err
}
// TODO(bnewbold): same as Typescript, but shouldn't this error instead of returning null?
if ix < 0 || ix >= len(entries) {
return nodeEntry{}, nil
}
return entries[ix], nil
}
// NOTE(bnewbold): unlike Typescript, golang does not really need the slice(start?, end?) helper
// "inserts entry at index"
func (mst *MerkleSearchTree) spliceIn(ctx context.Context, entry nodeEntry, ix int) (*MerkleSearchTree, error) {
entries, err := mst.getEntries(ctx)
if err != nil {
return nil, err
}
nents := make([]nodeEntry, len(entries)+1)
copy(nents, entries[:ix])
nents[ix] = entry
copy(nents[ix+1:], entries[ix:])
checkTreeInvariant(nents)
return mst.newTree(nents), nil
}
// "replaces an entry with [ Maybe(tree), Leaf, Maybe(tree) ]"
func (mst *MerkleSearchTree) replaceWithSplit(ctx context.Context, ix int, left *MerkleSearchTree, nl nodeEntry, right *MerkleSearchTree) (*MerkleSearchTree, error) {
entries, err := mst.getEntries(ctx)
if err != nil {
return nil, err
}
checkTreeInvariant(entries)
var update []nodeEntry
update = append(update, entries[:ix]...)
if left != nil {
update = append(update, nodeEntry{
Kind: entryTree,
Tree: left,
})
}
update = append(update, nl)
if right != nil {
update = append(update, nodeEntry{
Kind: entryTree,
Tree: right,
})
}
update = append(update, entries[ix+1:]...)
checkTreeInvariant(update)
return mst.newTree(update), nil
}
// "if the topmost node in the tree only points to another tree, trim the top and return the subtree"
func (mst *MerkleSearchTree) trimTop(ctx context.Context) (*MerkleSearchTree, error) {
entries, err := mst.getEntries(ctx)
if err != nil {
return nil, err
}
if len(entries) == 1 && entries[0].isTree() {
return entries[0].Tree.trimTop(ctx)
} else {
return mst, nil
}
}
// === "Subtree & Splits" ===
// "Recursively splits a sub tree around a given key"
func (mst *MerkleSearchTree) splitAround(ctx context.Context, key string) (*MerkleSearchTree, *MerkleSearchTree, error) {
index, err := mst.findGtOrEqualLeafIndex(ctx, key)
if err != nil {
return nil, nil, err
}
entries, err := mst.getEntries(ctx)
if err != nil {
return nil, nil, err
}
leftData := entries[:index]
rightData := entries[index:]
left := mst.newTree(leftData)
right := mst.newTree(rightData)
// "if the far right of the left side is a subtree, we need to split it on the key as well"
if len(leftData) > 0 && leftData[len(leftData)-1].isTree() {
lastInLeft := leftData[len(leftData)-1]
nleft, err := left.removeEntry(ctx, len(leftData)-1)
if err != nil {
return nil, nil, err
}
left = nleft
subl, subr, err := lastInLeft.Tree.splitAround(ctx, key)
if err != nil {
return nil, nil, err
}
if subl != nil {
left, err = left.append(ctx, mkTreeEntry(subl))
if err != nil {
return nil, nil, err
}
}
if subr != nil {
right, err = right.prepend(ctx, mkTreeEntry(subr))
if err != nil {
return nil, nil, err
}
}
}
if left.entryCount() == 0 {
left = nil
}
if right.entryCount() == 0 {
right = nil
}
return left, right, nil
}
func (mst *MerkleSearchTree) entryCount() int {
entries, err := mst.getEntries(context.TODO())
if err != nil {
panic(err)
}
return len(entries)
}
// "The simple merge case where every key in the right tree is greater than every key in the left tree (used primarily for deletes)"
func (mst *MerkleSearchTree) appendMerge(ctx context.Context, omst *MerkleSearchTree) (*MerkleSearchTree, error) {
mylayer, err := mst.getLayer(ctx)
if err != nil {
return nil, err
}
olayer, err := omst.getLayer(ctx)
if err != nil {
return nil, err
}
if mylayer != olayer {
return nil, fmt.Errorf("trying to merge two nodes from different layers")
}
entries, err := mst.getEntries(ctx)
if err != nil {
return nil, err
}
tomergeEnts, err := omst.getEntries(ctx)
if err != nil {
return nil, err
}
lastInLeft := entries[len(entries)-1]
firstInRight := tomergeEnts[0] // NOTE(bnewbold): bug fixed here, I think
if lastInLeft.isTree() && firstInRight.isTree() {
merged, err := lastInLeft.Tree.appendMerge(ctx, firstInRight.Tree)
if err != nil {
return nil, err
}
return mst.newTree(append(append(entries[:len(entries)-1], mkTreeEntry(merged)), tomergeEnts[1:]...)), nil
} else {
return mst.newTree(append(entries, tomergeEnts...)), nil
}
}
// === "Create relatives" ===
func (mst *MerkleSearchTree) createChild(ctx context.Context) (*MerkleSearchTree, error) {
layer, err := mst.getLayer(ctx)
if err != nil {
return nil, err
}
return createMST(mst.cst, cid.Undef, []nodeEntry{}, layer-1), nil
}
func (mst *MerkleSearchTree) createParent(ctx context.Context) (*MerkleSearchTree, error) {
layer, err := mst.getLayer(ctx)
if err != nil {
return nil, err
}
return createMST(mst.cst, cid.Undef, []nodeEntry{mkTreeEntry(mst)}, layer+1), nil
}
// === "Finding insertion points" ===
// NOTE(@why): this smells inefficient
// "finds index of first leaf node that is greater than or equal to the value"
func (mst *MerkleSearchTree) findGtOrEqualLeafIndex(ctx context.Context, key string) (int, error) {
entries, err := mst.getEntries(ctx)
if err != nil {
return -1, err
}
for i, e := range entries {
//if e.isLeaf() && bytes.Compare(e.Key, key) > 0 {
if e.isLeaf() && e.Key >= key {
return i, nil
}
}
// "if we can't find, we're on the end"
return len(entries), nil
}
// === "List operations (partial tree traversal)" ===
// WalkLeavesFrom walks the leaves of the tree, calling the cb callback on each
// key that's greater than or equal to the provided from key.
// If cb returns an error, the walk is aborted and the error is returned.
func (mst *MerkleSearchTree) WalkLeavesFrom(ctx context.Context, from string, cb func(key string, val cid.Cid) error) error {
index, err := mst.findGtOrEqualLeafIndex(ctx, from)
if err != nil {
return err
}
entries, err := mst.getEntries(ctx)
if err != nil {
return err
}
if index > 0 {
prev := entries[index-1]
if !prev.isUndefined() && prev.isTree() {
if err := prev.Tree.WalkLeavesFrom(ctx, from, cb); err != nil {
return err
}
}
}
for _, e := range entries[index:] {
if e.isLeaf() {
if err := cb(e.Key, e.Val); err != nil {
return err
}
} else {
if err := e.Tree.WalkLeavesFrom(ctx, from, cb); err != nil {
return err
}
}
}
return nil
}
// TODO: Typescript: MST.list(count?, after?, before?) -> Leaf[]
// TODO: Typescript: MST.listWithPrefix(prefix, count?) -> Leaf[]
// "Walk full tree & emit nodes, consumer can bail at any point by returning false"
// TODO: Typescript: MST.walk() -> nodeEntry (iterator)
// TODO: Typescript: MST.paths() -> nodeEntry[][]
// TODO: Typescript: MST.allNodes() -> nodeEntry[]
// TODO: Typescript: MST.allCids() -> CidSet
// TODO: Typescript: MST.leaves() -> Leaf[]
// TODO: Typescript: MST.leafCount() -> number
// TODO: Typescript: MST.walkReachable() -> nodeEntry (iterator)
// TODO: Typescript: MST.reachableLeaves() -> Leaf[]
// TODO: Typescript: MST.writeToCarStream(car) -> ()
// TODO: Typescript: MST.cidsForPath(car) -> CID[]