-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathinterval_btree.go
1132 lines (1044 loc) · 27.3 KB
/
interval_btree.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
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package spanlatch
import (
"bytes"
"sort"
"strings"
"sync"
"sync/atomic"
"unsafe"
"github.com/cockroachdb/cockroach/pkg/roachpb"
)
const (
degree = 16
maxCmds = 2*degree - 1
minCmds = degree - 1
)
// TODO(nvanbenschoten): remove.
type cmd struct {
id int64
span roachpb.Span
}
// cmp returns a value indicating the sort order relationship between
// a and b. The comparison is performed lexicographically on
// (a.span.Key, a.span.EndKey, a.id)
// and
// (b.span.Key, b.span.EndKey, b.id)
// tuples.
//
// Given c = cmp(a, b):
//
// c == -1 if (a.span.Key, a.span.EndKey, a.id) < (b.span.Key, b.span.EndKey, b.id)
// c == 0 if (a.span.Key, a.span.EndKey, a.id) == (b.span.Key, b.span.EndKey, b.id)
// c == 1 if (a.span.Key, a.span.EndKey, a.id) > (b.span.Key, b.span.EndKey, b.id)
//
func cmp(a, b *cmd) int {
c := bytes.Compare(a.span.Key, b.span.Key)
if c != 0 {
return c
}
c = bytes.Compare(a.span.EndKey, b.span.EndKey)
if c != 0 {
return c
}
if a.id < b.id {
return -1
} else if a.id > b.id {
return 1
} else {
return 0
}
}
// keyBound represents the upper-bound of a key range.
type keyBound struct {
key roachpb.Key
inc bool
}
func (b keyBound) compare(o keyBound) int {
c := bytes.Compare(b.key, o.key)
if c != 0 {
return c
}
if b.inc == o.inc {
return 0
}
if b.inc {
return 1
}
return -1
}
func (b keyBound) contains(a *cmd) bool {
c := bytes.Compare(a.span.Key, b.key)
if c == 0 {
return b.inc
}
return c < 0
}
func upperBound(c *cmd) keyBound {
if len(c.span.EndKey) != 0 {
return keyBound{key: c.span.EndKey}
}
return keyBound{key: c.span.Key, inc: true}
}
type leafNode struct {
ref int32
count int16
leaf bool
max keyBound
cmds [maxCmds]*cmd
}
type node struct {
leafNode
children [maxCmds + 1]*node
}
func leafToNode(ln *leafNode) *node {
return (*node)(unsafe.Pointer(ln))
}
func nodeToLeaf(n *node) *leafNode {
return (*leafNode)(unsafe.Pointer(n))
}
var leafPool = sync.Pool{
New: func() interface{} {
return new(leafNode)
},
}
var nodePool = sync.Pool{
New: func() interface{} {
return new(node)
},
}
func newLeafNode() *node {
n := leafToNode(leafPool.Get().(*leafNode))
n.leaf = true
n.ref = 1
return n
}
func newNode() *node {
n := nodePool.Get().(*node)
n.ref = 1
return n
}
// mut creates and returns a mutable node reference. If the node is not shared
// with any other trees then it can be modified in place. Otherwise, it must be
// cloned to ensure unique ownership. In this way, we enforce a copy-on-write
// policy which transparently incorporates the idea of local mutations, like
// Clojure's transients or Haskell's ST monad, where nodes are only copied
// during the first time that they are modified between Clone operations.
//
// When a node is cloned, the provided pointer will be redirected to the new
// mutable node.
func mut(n **node) *node {
if atomic.LoadInt32(&(*n).ref) == 1 {
// Exclusive ownership. Can mutate in place.
return *n
}
// If we do not have unique ownership over the node then we
// clone it to gain unique ownership. After doing so, we can
// release our reference to the old node.
c := (*n).clone()
(*n).decRef(true /* recursive */)
*n = c
return *n
}
// incRef acquires a reference to the node.
func (n *node) incRef() {
atomic.AddInt32(&n.ref, 1)
}
// decRef releases a reference to the node. If requested, the method
// will recurse into child nodes and decrease their refcounts as well.
func (n *node) decRef(recursive bool) {
if atomic.AddInt32(&n.ref, -1) > 0 {
// Other references remain. Can't free.
return
}
// Clear and release node into memory pool.
if n.leaf {
ln := nodeToLeaf(n)
*ln = leafNode{}
leafPool.Put(ln)
} else {
// Release child references first, if requested.
if recursive {
for i := int16(0); i <= n.count; i++ {
n.children[i].decRef(true /* recursive */)
}
}
*n = node{}
nodePool.Put(n)
}
}
// clone creates a clone of the receiver with a single reference count.
func (n *node) clone() *node {
var c *node
if n.leaf {
c = newLeafNode()
} else {
c = newNode()
}
// NB: copy field-by-field without touching n.ref to avoid
// triggering the race detector and looking like a data race.
c.count = n.count
c.max = n.max
c.cmds = n.cmds
if !c.leaf {
// Copy children and increase each refcount.
c.children = n.children
for i := int16(0); i <= c.count; i++ {
c.children[i].incRef()
}
}
return c
}
func (n *node) insertAt(index int, c *cmd, nd *node) {
if index < int(n.count) {
copy(n.cmds[index+1:n.count+1], n.cmds[index:n.count])
if !n.leaf {
copy(n.children[index+2:n.count+2], n.children[index+1:n.count+1])
}
}
n.cmds[index] = c
if !n.leaf {
n.children[index+1] = nd
}
n.count++
}
func (n *node) pushBack(c *cmd, nd *node) {
n.cmds[n.count] = c
if !n.leaf {
n.children[n.count+1] = nd
}
n.count++
}
func (n *node) pushFront(c *cmd, nd *node) {
if !n.leaf {
copy(n.children[1:n.count+2], n.children[:n.count+1])
n.children[0] = nd
}
copy(n.cmds[1:n.count+1], n.cmds[:n.count])
n.cmds[0] = c
n.count++
}
// removeAt removes a value at a given index, pulling all subsequent values
// back.
func (n *node) removeAt(index int) (*cmd, *node) {
var child *node
if !n.leaf {
child = n.children[index+1]
copy(n.children[index+1:n.count], n.children[index+2:n.count+1])
n.children[n.count] = nil
}
n.count--
out := n.cmds[index]
copy(n.cmds[index:n.count], n.cmds[index+1:n.count+1])
n.cmds[n.count] = nil
return out, child
}
// popBack removes and returns the last element in the list.
func (n *node) popBack() (*cmd, *node) {
n.count--
out := n.cmds[n.count]
n.cmds[n.count] = nil
if n.leaf {
return out, nil
}
child := n.children[n.count+1]
n.children[n.count+1] = nil
return out, child
}
// popFront removes and returns the first element in the list.
func (n *node) popFront() (*cmd, *node) {
n.count--
var child *node
if !n.leaf {
child = n.children[0]
copy(n.children[:n.count+1], n.children[1:n.count+2])
n.children[n.count+1] = nil
}
out := n.cmds[0]
copy(n.cmds[:n.count], n.cmds[1:n.count+1])
n.cmds[n.count] = nil
return out, child
}
// find returns the index where the given cmd should be inserted into this
// list. 'found' is true if the cmd already exists in the list at the given
// index.
func (n *node) find(c *cmd) (index int, found bool) {
// Logic copied from sort.Search. Inlining this gave
// an 11% speedup on BenchmarkBTreeDeleteInsert.
i, j := 0, int(n.count)
for i < j {
h := int(uint(i+j) >> 1) // avoid overflow when computing h
// i ≤ h < j
v := cmp(c, n.cmds[h])
if v == 0 {
return h, true
} else if v > 0 {
i = h + 1
} else {
j = h
}
}
return i, false
}
// split splits the given node at the given index. The current node shrinks,
// and this function returns the cmd that existed at that index and a new node
// containing all cmds/children after it.
//
// Before:
//
// +-----------+
// | x y z |
// +--/-/-\-\--+
//
// After:
//
// +-----------+
// | y |
// +----/-\----+
// / \
// v v
// +-----------+ +-----------+
// | x | | z |
// +-----------+ +-----------+
//
func (n *node) split(i int) (*cmd, *node) {
out := n.cmds[i]
var next *node
if n.leaf {
next = newLeafNode()
} else {
next = newNode()
}
next.count = n.count - int16(i+1)
copy(next.cmds[:], n.cmds[i+1:n.count])
for j := int16(i); j < n.count; j++ {
n.cmds[j] = nil
}
if !n.leaf {
copy(next.children[:], n.children[i+1:n.count+1])
for j := int16(i + 1); j <= n.count; j++ {
n.children[j] = nil
}
}
n.count = int16(i)
next.max = next.findUpperBound()
if n.max.compare(next.max) != 0 && n.max.compare(upperBound(out)) != 0 {
// If upper bound wasn't from new node or cmd
// at index i, it must still be from old node.
} else {
n.max = n.findUpperBound()
}
return out, next
}
// insert inserts a cmd into the subtree rooted at this node, making sure no
// nodes in the subtree exceed maxCmds cmds. Returns true if an existing cmd was
// replaced and false if a command was inserted. Also returns whether the node's
// upper bound changes.
func (n *node) insert(c *cmd) (replaced, newBound bool) {
i, found := n.find(c)
if found {
n.cmds[i] = c
return true, false
}
if n.leaf {
n.insertAt(i, c, nil)
return false, n.adjustUpperBoundOnInsertion(c, nil)
}
if n.children[i].count >= maxCmds {
splitcmd, splitNode := mut(&n.children[i]).split(maxCmds / 2)
n.insertAt(i, splitcmd, splitNode)
switch cmp := cmp(c, n.cmds[i]); {
case cmp < 0:
// no change, we want first split node
case cmp > 0:
i++ // we want second split node
default:
n.cmds[i] = c
return true, false
}
}
replaced, newBound = mut(&n.children[i]).insert(c)
if newBound {
newBound = n.adjustUpperBoundOnInsertion(c, nil)
}
return replaced, newBound
}
// removeMax removes and returns the maximum cmd from the subtree rooted at
// this node.
func (n *node) removeMax() *cmd {
if n.leaf {
n.count--
out := n.cmds[n.count]
n.cmds[n.count] = nil
n.adjustUpperBoundOnRemoval(out, nil)
return out
}
child := mut(&n.children[n.count])
if child.count <= minCmds {
n.rebalanceOrMerge(int(n.count))
return n.removeMax()
}
return child.removeMax()
}
// remove removes a cmd from the subtree rooted at this node. Returns
// the cmd that was removed or nil if no matching command was found.
// Also returns whether the node's upper bound changes.
func (n *node) remove(c *cmd) (out *cmd, newBound bool) {
i, found := n.find(c)
if n.leaf {
if found {
out, _ = n.removeAt(i)
return out, n.adjustUpperBoundOnRemoval(out, nil)
}
return nil, false
}
if n.children[i].count <= minCmds {
// Child not large enough to remove from.
n.rebalanceOrMerge(i)
return n.remove(c)
}
child := mut(&n.children[i])
if found {
// Replace the cmd being removed with the max cmd in our left child.
out = n.cmds[i]
n.cmds[i] = child.removeMax()
return out, n.adjustUpperBoundOnRemoval(out, nil)
}
// Cmd is not in this node and child is large enough to remove from.
out, newBound = child.remove(c)
if newBound {
newBound = n.adjustUpperBoundOnRemoval(out, nil)
}
return out, newBound
}
// rebalanceOrMerge grows child 'i' to ensure it has sufficient room to remove
// a cmd from it while keeping it at or above minCmds.
func (n *node) rebalanceOrMerge(i int) {
switch {
case i > 0 && n.children[i-1].count > minCmds:
// Rebalance from left sibling.
//
// +-----------+
// | y |
// +----/-\----+
// / \
// v v
// +-----------+ +-----------+
// | x | | |
// +----------\+ +-----------+
// \
// v
// a
//
// After:
//
// +-----------+
// | x |
// +----/-\----+
// / \
// v v
// +-----------+ +-----------+
// | | | y |
// +-----------+ +/----------+
// /
// v
// a
//
left := mut(&n.children[i-1])
child := mut(&n.children[i])
xCmd, grandChild := left.popBack()
yCmd := n.cmds[i-1]
child.pushFront(yCmd, grandChild)
n.cmds[i-1] = xCmd
left.adjustUpperBoundOnRemoval(xCmd, grandChild)
child.adjustUpperBoundOnInsertion(yCmd, grandChild)
case i < int(n.count) && n.children[i+1].count > minCmds:
// Rebalance from right sibling.
//
// +-----------+
// | y |
// +----/-\----+
// / \
// v v
// +-----------+ +-----------+
// | | | x |
// +-----------+ +/----------+
// /
// v
// a
//
// After:
//
// +-----------+
// | x |
// +----/-\----+
// / \
// v v
// +-----------+ +-----------+
// | y | | |
// +----------\+ +-----------+
// \
// v
// a
//
right := mut(&n.children[i+1])
child := mut(&n.children[i])
xCmd, grandChild := right.popFront()
yCmd := n.cmds[i]
child.pushBack(yCmd, grandChild)
n.cmds[i] = xCmd
right.adjustUpperBoundOnRemoval(xCmd, grandChild)
child.adjustUpperBoundOnInsertion(yCmd, grandChild)
default:
// Merge with either the left or right sibling.
//
// +-----------+
// | u y v |
// +----/-\----+
// / \
// v v
// +-----------+ +-----------+
// | x | | z |
// +-----------+ +-----------+
//
// After:
//
// +-----------+
// | u v |
// +-----|-----+
// |
// v
// +-----------+
// | x y z |
// +-----------+
//
if i >= int(n.count) {
i = int(n.count - 1)
}
child := mut(&n.children[i])
// Make mergeChild mutable, bumping the refcounts on its children if necessary.
_ = mut(&n.children[i+1])
mergeCmd, mergeChild := n.removeAt(i)
child.cmds[child.count] = mergeCmd
copy(child.cmds[child.count+1:], mergeChild.cmds[:mergeChild.count])
if !child.leaf {
copy(child.children[child.count+1:], mergeChild.children[:mergeChild.count+1])
}
child.count += mergeChild.count + 1
child.adjustUpperBoundOnInsertion(mergeCmd, mergeChild)
mergeChild.decRef(false /* recursive */)
}
}
// findUpperBound returns the largest end key node range, assuming that its
// children have correct upper bounds already set.
func (n *node) findUpperBound() keyBound {
var max keyBound
for i := int16(0); i < n.count; i++ {
up := upperBound(n.cmds[i])
if max.compare(up) < 0 {
max = up
}
}
if !n.leaf {
for i := int16(0); i <= n.count; i++ {
up := n.children[i].max
if max.compare(up) < 0 {
max = up
}
}
}
return max
}
// adjustUpperBoundOnInsertion adjusts the upper key bound for this node
// given a cmd and an optional child node that was inserted. Returns true
// is the upper bound was changed and false if not.
func (n *node) adjustUpperBoundOnInsertion(c *cmd, child *node) bool {
up := upperBound(c)
if child != nil {
if up.compare(child.max) < 0 {
up = child.max
}
}
if n.max.compare(up) < 0 {
n.max = up
return true
}
return false
}
// adjustUpperBoundOnRemoval adjusts the upper key bound for this node
// given a cmd and an optional child node that were removed. Returns true
// is the upper bound was changed and false if not.
func (n *node) adjustUpperBoundOnRemoval(c *cmd, child *node) bool {
up := upperBound(c)
if child != nil {
if up.compare(child.max) < 0 {
up = child.max
}
}
if n.max.compare(up) == 0 {
n.max = n.findUpperBound()
return true
}
return false
}
// btree is an implementation of an augmented interval B-Tree.
//
// btree stores cmds in an ordered structure, allowing easy insertion,
// removal, and iteration. It represents intervals and permits an interval
// search operation following the approach laid out in CLRS, Chapter 14.
// The B-Tree stores cmds in order based on their start key and each B-Tree
// node maintains the upper-bound end key of all cmds in its subtree.
//
// Write operations are not safe for concurrent mutation by multiple
// goroutines, but Read operations are.
type btree struct {
root *node
length int
}
// Reset removes all cmds from the btree. In doing so, it allows memory
// held by the btree to be recycled. Failure to call this method before
// letting a btree be GCed is safe in that it won't cause a memory leak,
// but it will prevent btree nodes from being efficiently re-used.
func (t *btree) Reset() {
if t.root != nil {
t.root.decRef(true /* recursive */)
t.root = nil
}
t.length = 0
}
// Clone clones the btree, lazily.
func (t *btree) Clone() btree {
c := *t
if c.root != nil {
c.root.incRef()
}
return c
}
// Delete removes a cmd equal to the passed in cmd from the tree.
func (t *btree) Delete(c *cmd) {
if t.root == nil || t.root.count == 0 {
return
}
if out, _ := mut(&t.root).remove(c); out != nil {
t.length--
}
if t.root.count == 0 && !t.root.leaf {
old := t.root
t.root = t.root.children[0]
old.decRef(false /* recursive */)
}
}
// Set adds the given cmd to the tree. If a cmd in the tree already equals
// the given one, it is replaced with the new cmd.
func (t *btree) Set(c *cmd) {
if t.root == nil {
t.root = newLeafNode()
} else if t.root.count >= maxCmds {
splitcmd, splitNode := mut(&t.root).split(maxCmds / 2)
newRoot := newNode()
newRoot.count = 1
newRoot.cmds[0] = splitcmd
newRoot.children[0] = t.root
newRoot.children[1] = splitNode
newRoot.max = newRoot.findUpperBound()
t.root = newRoot
}
if replaced, _ := mut(&t.root).insert(c); !replaced {
t.length++
}
}
// MakeIter returns a new iterator object. It is not safe to continue using an
// iterator after modifications are made to the tree. If modifications are made,
// create a new iterator.
func (t *btree) MakeIter() iterator {
return iterator{r: t.root, pos: -1}
}
// Height returns the height of the tree.
func (t *btree) Height() int {
if t.root == nil {
return 0
}
h := 1
n := t.root
for !n.leaf {
n = n.children[0]
h++
}
return h
}
// Len returns the number of cmds currently in the tree.
func (t *btree) Len() int {
return t.length
}
// String returns a string description of the tree. The format is
// similar to the https://en.wikipedia.org/wiki/Newick_format.
func (t *btree) String() string {
if t.length == 0 {
return ";"
}
var b strings.Builder
t.root.writeString(&b)
return b.String()
}
func (n *node) writeString(b *strings.Builder) {
if n.leaf {
for i := int16(0); i < n.count; i++ {
if i != 0 {
b.WriteString(",")
}
b.WriteString(n.cmds[i].span.String())
}
return
}
for i := int16(0); i <= n.count; i++ {
b.WriteString("(")
n.children[i].writeString(b)
b.WriteString(")")
if i < n.count {
b.WriteString(n.cmds[i].span.String())
}
}
}
// iterStack represents a stack of (node, pos) tuples, which captures
// iteration state as an iterator descends a btree.
type iterStack struct {
a iterStackArr
aLen int16 // -1 when using s
s []iterFrame
}
// Used to avoid allocations for stacks below a certain size.
type iterStackArr [3]iterFrame
type iterFrame struct {
n *node
pos int16
}
func (is *iterStack) push(f iterFrame) {
if is.aLen == -1 {
is.s = append(is.s, f)
} else if int(is.aLen) == len(is.a) {
is.s = make([]iterFrame, int(is.aLen)+1, 2*int(is.aLen))
copy(is.s, is.a[:])
is.s[int(is.aLen)] = f
is.aLen = -1
} else {
is.a[is.aLen] = f
is.aLen++
}
}
func (is *iterStack) pop() iterFrame {
if is.aLen == -1 {
f := is.s[len(is.s)-1]
is.s = is.s[:len(is.s)-1]
return f
}
is.aLen--
return is.a[is.aLen]
}
func (is *iterStack) len() int {
if is.aLen == -1 {
return len(is.s)
}
return int(is.aLen)
}
func (is *iterStack) reset() {
if is.aLen == -1 {
is.s = is.s[:0]
} else {
is.aLen = 0
}
}
// iterator is responsible for search and traversal within a btree.
type iterator struct {
r *node
n *node
pos int16
s iterStack
o overlapScan
}
func (i *iterator) reset() {
i.n = i.r
i.pos = -1
i.s.reset()
i.o = overlapScan{}
}
func (i *iterator) descend(n *node, pos int16) {
i.s.push(iterFrame{n: n, pos: pos})
i.n = n.children[pos]
i.pos = 0
}
// ascend ascends up to the current node's parent and resets the position
// to the one previously set for this parent node.
func (i *iterator) ascend() {
f := i.s.pop()
i.n = f.n
i.pos = f.pos
}
// SeekGE seeks to the first cmd greater-than or equal to the provided cmd.
func (i *iterator) SeekGE(c *cmd) {
i.reset()
if i.n == nil {
return
}
for {
pos, found := i.n.find(c)
i.pos = int16(pos)
if found {
return
}
if i.n.leaf {
if i.pos == i.n.count {
i.Next()
}
return
}
i.descend(i.n, i.pos)
}
}
// SeekLT seeks to the first cmd less-than the provided cmd.
func (i *iterator) SeekLT(c *cmd) {
i.reset()
if i.n == nil {
return
}
for {
pos, found := i.n.find(c)
i.pos = int16(pos)
if found || i.n.leaf {
i.Prev()
return
}
i.descend(i.n, i.pos)
}
}
// First seeks to the first cmd in the btree.
func (i *iterator) First() {
i.reset()
if i.n == nil {
return
}
for !i.n.leaf {
i.descend(i.n, 0)
}
i.pos = 0
}
// Last seeks to the last cmd in the btree.
func (i *iterator) Last() {
i.reset()
if i.n == nil {
return
}
for !i.n.leaf {
i.descend(i.n, i.n.count)
}
i.pos = i.n.count - 1
}
// Next positions the iterator to the cmd immediately following
// its current position.
func (i *iterator) Next() {
if i.n == nil {
return
}
if i.n.leaf {
i.pos++
if i.pos < i.n.count {
return
}
for i.s.len() > 0 && i.pos >= i.n.count {
i.ascend()
}
return
}
i.descend(i.n, i.pos+1)
for !i.n.leaf {
i.descend(i.n, 0)
}
i.pos = 0
}
// Prev positions the iterator to the cmd immediately preceding
// its current position.
func (i *iterator) Prev() {
if i.n == nil {
return
}
if i.n.leaf {
i.pos--
if i.pos >= 0 {
return
}
for i.s.len() > 0 && i.pos < 0 {
i.ascend()
i.pos--
}
return
}
i.descend(i.n, i.pos)
for !i.n.leaf {
i.descend(i.n, i.n.count)
}
i.pos = i.n.count - 1
}
// Valid returns whether the iterator is positioned at a valid position.
func (i *iterator) Valid() bool {
return i.pos >= 0 && i.pos < i.n.count
}
// Cmd returns the cmd at the iterator's current position. It is illegal
// to call Cmd if the iterator is not valid.
func (i *iterator) Cmd() *cmd {
return i.n.cmds[i.pos]
}
// An overlap scan is a scan over all cmds that overlap with the provided cmd
// in order of the overlapping cmds' start keys. The goal of the scan is to
// minimize the number of key comparisons performed in total. The algorithm
// operates based on the following two invariants maintained by augmented
// interval btree:
// 1. all cmds are sorted in the btree based on their start key.
// 2. all btree nodes maintain the upper bound end key of all cmds
// in their subtree.
//
// The scan algorithm starts in "unconstrained minimum" and "unconstrained
// maximum" states. To enter a "constrained minimum" state, the scan must reach
// cmds in the tree with start keys above the search range's start key. Because
// cmds in the tree are sorted by start key, once the scan enters the
// "constrained minimum" state it will remain there. To enter a "constrained
// maximum" state, the scan must determine the first child btree node in a given
// subtree that can have cmds with start keys above the search range's end key.
// The scan then remains in the "constrained maximum" state until it traverse
// into this child node, at which point it moves to the "unconstrained maximum"
// state again.
//
// The scan algorithm works like a standard btree forward scan with the
// following augmentations: