forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 2
/
range_tree.go
857 lines (814 loc) · 22.1 KB
/
range_tree.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
// Copyright 2015 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. See the AUTHORS file
// for names of contributors.
//
// Author: Bram Gruneir (bram+code@cockroachlabs.com)
package storage
import (
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/keys"
"github.com/cockroachdb/cockroach/proto"
"github.com/cockroachdb/cockroach/storage/engine"
"github.com/cockroachdb/cockroach/util"
)
// cachedNode is an in memory cache for use during range tree manipulations.
type cachedNode struct {
node *proto.RangeTreeNode
dirty bool
}
// RangeTree is used to hold the relevant context information for any
// operations on the range tree.
type treeContext struct {
txn *client.Txn
tree *proto.RangeTree
dirty bool
nodes map[string]cachedNode
}
// SetupRangeTree creates a new RangeTree. This should only be called as part
// of store.BootstrapRange.
func SetupRangeTree(batch engine.Engine, ms *engine.MVCCStats, timestamp proto.Timestamp, startKey proto.Key) error {
tree := &proto.RangeTree{
RootKey: startKey,
}
node := &proto.RangeTreeNode{
Key: startKey,
Black: true,
}
if err := engine.MVCCPutProto(batch, ms, keys.RangeTreeRoot, timestamp, nil, tree); err != nil {
return err
}
if err := engine.MVCCPutProto(batch, ms, keys.RangeTreeNodeKey(startKey), timestamp, nil, node); err != nil {
return err
}
return nil
}
// flush writes all dirty nodes and the tree to the transaction.
func (tc *treeContext) flush(b *client.Batch) error {
if tc.dirty {
b.Put(keys.RangeTreeRoot, tc.tree)
}
for key, cachedNode := range tc.nodes {
if cachedNode.dirty {
if cachedNode.node == nil {
b.Del(keys.RangeTreeNodeKey(proto.Key(key)))
} else {
b.Put(keys.RangeTreeNodeKey(proto.Key(key)), cachedNode.node)
}
}
}
return nil
}
// GetRangeTree fetches the RangeTree proto and sets up the range tree context.
func getRangeTree(txn *client.Txn) (*treeContext, error) {
tree := new(proto.RangeTree)
if err := txn.GetProto(keys.RangeTreeRoot, tree); err != nil {
return nil, err
}
return &treeContext{
txn: txn,
tree: tree,
dirty: false,
nodes: map[string]cachedNode{},
}, nil
}
// setRoot sets the tree root key in the cache. It also marks the root for
// writing during a flush.
func (tc *treeContext) setRootKey(key proto.Key) {
tc.tree.RootKey = key
tc.dirty = true
}
// setNode sets the node in the cache so all subsequent reads will read this
// upated value. It also marks the node as dirty for writing during a flush.
func (tc *treeContext) setNode(node *proto.RangeTreeNode) {
tc.nodes[string(node.Key)] = cachedNode{
node: node,
dirty: true,
}
}
// dropNode sets a node in the cache to nil. It also marks the node as dirty for
// writing during a flush.
func (tc *treeContext) dropNode(key proto.Key) {
tc.nodes[string(key)] = cachedNode{
node: nil,
dirty: true,
}
}
// getNode returns the RangeTreeNode for the given key. If the key is nil, nil
// is returned.
func (tc *treeContext) getNode(key proto.Key) (*proto.RangeTreeNode, error) {
if key == nil {
return nil, nil
}
// First check to see if we have the node cached.
keyString := string(key)
cached, ok := tc.nodes[keyString]
if ok {
return cached.node, nil
}
// We don't have it cached so fetch it and add it to the cache.
node := new(proto.RangeTreeNode)
if err := tc.txn.GetProto(keys.RangeTreeNodeKey(key), node); err != nil {
return nil, err
}
tc.nodes[keyString] = cachedNode{
node: node,
dirty: false,
}
return node, nil
}
// isRed will return true only if node exists and is not set to black.
func isRed(node *proto.RangeTreeNode) bool {
if node == nil {
return false
}
return !node.Black
}
// getSibling returns the other child of the node's parent. Returns nil if the node
// has no parent or its parent has only one child.
func (tc *treeContext) getSibling(node *proto.RangeTreeNode) (*proto.RangeTreeNode, error) {
if node == nil || node.ParentKey == nil {
return nil, nil
}
parent, err := tc.getNode(node.ParentKey)
if err != nil {
return nil, err
}
if parent.LeftKey == nil || parent.RightKey == nil {
return nil, nil
}
if node.Key.Equal(parent.LeftKey) {
return tc.getNode(parent.RightKey)
}
return tc.getNode(parent.LeftKey)
}
// getUncle returns the node's parent's sibling. Or nil if the node doesn't have a
// grandparent or their parent has no sibling.
func (tc *treeContext) getUncle(node *proto.RangeTreeNode) (*proto.RangeTreeNode, error) {
if node == nil || node.ParentKey == nil {
return nil, nil
}
parent, err := tc.getNode(node.ParentKey)
if err != nil {
return nil, err
}
return tc.getSibling(parent)
}
// replaceNode cuts a node away form its parent, substituting a new node or
// nil. The updated new node is returned. Note that this does not in fact alter
// the old node in any way, but only the old node's parent and the new node.
func (tc *treeContext) replaceNode(oldNode, newNode *proto.RangeTreeNode) (*proto.RangeTreeNode, error) {
if oldNode.ParentKey == nil {
if newNode == nil {
return nil, util.Errorf("cannot replace the root node with nil")
}
// Update the root key if this was the root.
tc.setRootKey(newNode.Key)
} else {
oldParent, err := tc.getNode(oldNode.ParentKey)
if err != nil {
return nil, err
}
if oldParent.LeftKey != nil && oldNode.Key.Equal(oldParent.LeftKey) {
if newNode == nil {
oldParent.LeftKey = nil
} else {
oldParent.LeftKey = newNode.Key
}
} else {
if newNode == nil {
oldParent.RightKey = nil
} else {
oldParent.RightKey = newNode.Key
}
}
tc.setNode(oldParent)
}
if newNode != nil {
newNode.ParentKey = oldNode.ParentKey
tc.setNode(newNode)
}
return newNode, nil
}
// rotateLeft performs a left rotation around the node.
func (tc *treeContext) rotateLeft(node *proto.RangeTreeNode) (*proto.RangeTreeNode, error) {
right, err := tc.getNode(node.RightKey)
if err != nil {
return nil, err
}
right, err = tc.replaceNode(node, right)
if err != nil {
return nil, err
}
node.RightKey = right.LeftKey
if right.LeftKey != nil {
rightLeft, err := tc.getNode(right.LeftKey)
if err != nil {
return nil, err
}
rightLeft.ParentKey = node.Key
tc.setNode(rightLeft)
}
right.LeftKey = node.Key
node.ParentKey = right.Key
tc.setNode(right)
tc.setNode(node)
return right, nil
}
// rotateRight performs a right rotation around the node.
func (tc *treeContext) rotateRight(node *proto.RangeTreeNode) (*proto.RangeTreeNode, error) {
left, err := tc.getNode(node.LeftKey)
if err != nil {
return nil, err
}
left, err = tc.replaceNode(node, left)
if err != nil {
return nil, err
}
node.LeftKey = left.RightKey
if left.RightKey != nil {
leftRight, err := tc.getNode(left.RightKey)
if err != nil {
return nil, err
}
leftRight.ParentKey = node.Key
tc.setNode(leftRight)
}
left.RightKey = node.Key
node.ParentKey = left.Key
tc.setNode(left)
tc.setNode(node)
return left, nil
}
// InsertRange adds a new range to the RangeTree. This should only be called
// from operations that create new ranges, such as AdminSplit.
func InsertRange(txn *client.Txn, b *client.Batch, key proto.Key) error {
tc, err := getRangeTree(txn)
if err != nil {
return err
}
newNode := &proto.RangeTreeNode{
Key: key,
}
if err := tc.insert(newNode); err != nil {
return err
}
return tc.flush(b)
}
// insert performs the insertion of a new node into the tree. It walks the tree
// until it finds the correct location. It will fail if the node already exists
// as that case should not occur. After inserting the node, it checks all insert
// cases to ensure the tree is balanced and adjusts it if needed.
func (tc *treeContext) insert(node *proto.RangeTreeNode) error {
if tc.tree.RootKey == nil {
tc.setRootKey(node.Key)
} else {
// Walk the tree to find the right place to insert the new node.
currentKey := tc.tree.RootKey
for {
currentNode, err := tc.getNode(currentKey)
if err != nil {
return err
}
if node.Key.Equal(currentNode.Key) {
return util.Errorf("key %s already exists in the range tree", node.Key)
}
if node.Key.Less(currentNode.Key) {
if currentNode.LeftKey == nil {
currentNode.LeftKey = node.Key
tc.setNode(currentNode)
break
} else {
currentKey = currentNode.LeftKey
}
} else {
if currentNode.RightKey == nil {
currentNode.RightKey = node.Key
tc.setNode(currentNode)
break
} else {
currentKey = currentNode.RightKey
}
}
}
node.ParentKey = currentKey
tc.setNode(node)
}
return tc.insertCase1(node)
}
// insertCase1 handles the case when the inserted node is the root node.
func (tc *treeContext) insertCase1(node *proto.RangeTreeNode) error {
if node.ParentKey == nil {
node.Black = true
tc.setNode(node)
return nil
}
return tc.insertCase2(node)
}
// insertCase2 handles the case when the inserted node has a black parent.
// In this is the case, no work need to be done, the tree is already correct.
func (tc *treeContext) insertCase2(node *proto.RangeTreeNode) error {
parent, err := tc.getNode(node.ParentKey)
if err != nil {
return err
}
if !isRed(parent) {
return nil
}
return tc.insertCase3(node)
}
// insertCase3 handles the case in which the uncle node is red. If so, the uncle
// and parent become black and the grandparent becomes red.
func (tc *treeContext) insertCase3(node *proto.RangeTreeNode) error {
uncle, err := tc.getUncle(node)
if err != nil {
return nil
}
if isRed(uncle) {
parent, err := tc.getNode(node.ParentKey)
if err != nil {
return err
}
parent.Black = true
tc.setNode(parent)
uncle.Black = true
tc.setNode(uncle)
grandparent, err := tc.getNode(parent.ParentKey)
if err != nil {
return err
}
grandparent.Black = false
tc.setNode(grandparent)
return tc.insertCase1(grandparent)
}
return tc.insertCase4(node)
}
// insertCase4 handles two mirror cases. If the node is the right child of its
// parent who is the left child of the grandparent, a left rotation around the
// parent is required. Similarly, if the node is the left child of its parent
// who is the right child of the grandparent, a right rotation around the parent
// is required. This only prepares the tree for insertCase5.
func (tc *treeContext) insertCase4(node *proto.RangeTreeNode) error {
parent, err := tc.getNode(node.ParentKey)
if err != nil {
return err
}
grandparent, err := tc.getNode(parent.ParentKey)
if err != nil {
return err
}
if parent.RightKey != nil && grandparent.LeftKey != nil && node.Key.Equal(parent.RightKey) && parent.Key.Equal(grandparent.LeftKey) {
if _, err := tc.rotateLeft(parent); err != nil {
return err
}
node, err = tc.getNode(node.Key)
if err != nil {
return err
}
node, err = tc.getNode(node.LeftKey)
if err != nil {
return err
}
} else if parent.LeftKey != nil && grandparent.RightKey != nil && node.Key.Equal(parent.LeftKey) && parent.Key.Equal(grandparent.RightKey) {
node, err = tc.rotateRight(parent)
if err != nil {
return err
}
node, err = tc.getNode(node.Key)
if err != nil {
return err
}
node, err = tc.getNode(node.RightKey)
if err != nil {
return err
}
}
return tc.insertCase5(node)
}
// insertCase5 handles two mirror cases. If the node is the right child of its
// parent who is the right child of the grandparent, a right rotation around the
// grandparent is required. Similarly, if the node is the left child of its
// parent who is the left child of the grandparent, a left rotation around the
// grandparent is required.
func (tc *treeContext) insertCase5(node *proto.RangeTreeNode) error {
parent, err := tc.getNode(node.ParentKey)
if err != nil {
return err
}
parent.Black = true
tc.setNode(parent)
grandparent, err := tc.getNode(parent.ParentKey)
if err != nil {
return err
}
grandparent.Black = false
tc.setNode(grandparent)
if parent.LeftKey != nil && node.Key.Equal(parent.LeftKey) {
if _, err := tc.rotateRight(grandparent); err != nil {
return err
}
} else {
if _, err := tc.rotateLeft(grandparent); err != nil {
return err
}
}
return nil
}
// DeleteRange removes a range from the RangeTree. This should only be called
// from operations that remove ranges, such as AdminMerge.
func DeleteRange(txn *client.Txn, b *client.Batch, key proto.Key) error {
tc, err := getRangeTree(txn)
if err != nil {
return err
}
node, err := tc.getNode(key)
if err != nil {
return err
}
if node == nil {
return util.Errorf("could not find range tree node with the key %s", key)
}
if err := tc.delete(node); err != nil {
return err
}
return tc.flush(b)
}
// swapNodes swaps all aspects of nodes a and b except their keys. This function
// is needed in order to accommodate the in-place style delete.
func (tc *treeContext) swapNodes(a, b *proto.RangeTreeNode) (*proto.RangeTreeNode, *proto.RangeTreeNode, error) {
newA := &proto.RangeTreeNode{
Key: a.Key,
Black: b.Black,
ParentKey: b.ParentKey,
LeftKey: b.LeftKey,
RightKey: b.RightKey,
}
newB := &proto.RangeTreeNode{
Key: b.Key,
Black: a.Black,
ParentKey: a.ParentKey,
LeftKey: a.LeftKey,
RightKey: a.RightKey,
}
if a.ParentKey != nil {
// Only change a's parent's child key if the parent is not b.
if !a.ParentKey.Equal(b.Key) {
parent, err := tc.getNode(a.ParentKey)
if err != nil {
return nil, nil, err
}
if parent.LeftKey.Equal(a.Key) {
parent.LeftKey = b.Key
} else {
parent.RightKey = b.Key
}
tc.setNode(parent)
} else {
newB.ParentKey = a.Key
}
} else {
tc.setRootKey(b.Key)
}
if a.LeftKey != nil {
if !a.LeftKey.Equal(b.Key) {
left, err := tc.getNode(a.LeftKey)
if err != nil {
return nil, nil, err
}
left.ParentKey = b.Key
tc.setNode(left)
} else {
newB.LeftKey = a.Key
}
}
if a.RightKey != nil {
if !a.RightKey.Equal(b.Key) {
right, err := tc.getNode(a.RightKey)
if err != nil {
return nil, nil, err
}
right.ParentKey = b.Key
tc.setNode(right)
} else {
newB.RightKey = a.Key
}
}
if b.ParentKey != nil {
// Only change b's parent's child key if the parent is not a.
if !b.ParentKey.Equal(a.Key) {
parent, err := tc.getNode(b.ParentKey)
if err != nil {
return nil, nil, err
}
if parent.LeftKey.Equal(b.Key) {
parent.LeftKey = a.Key
} else {
parent.RightKey = a.Key
}
tc.setNode(parent)
} else {
newA.ParentKey = b.Key
}
} else {
tc.setRootKey(a.Key)
}
if b.LeftKey != nil {
if !b.LeftKey.Equal(a.Key) {
left, err := tc.getNode(b.LeftKey)
if err != nil {
return nil, nil, err
}
left.ParentKey = a.Key
tc.setNode(left)
} else {
newA.LeftKey = b.Key
}
}
if b.RightKey != nil {
if !b.RightKey.Equal(a.Key) {
right, err := tc.getNode(b.RightKey)
if err != nil {
return nil, nil, err
}
right.ParentKey = a.Key
tc.setNode(right)
} else {
newA.RightKey = b.Key
}
}
tc.setNode(newA)
tc.setNode(newB)
return newA, newB, nil
}
// delete removes a range from the range tree.
// Since this tree is not stored in memory but persisted through the ranges, in
// place deletion is not possible. Instead, we use the helper function
// swapNodes above.
func (tc *treeContext) delete(node *proto.RangeTreeNode) error {
key := node.Key
if node.LeftKey != nil && node.RightKey != nil {
left, err := tc.getNode(node.LeftKey)
if err != nil {
return err
}
predecessor, err := tc.getMaxNode(left)
if err != nil {
return err
}
node, _, err = tc.swapNodes(node, predecessor)
if err != nil {
return err
}
}
// Node will always have at most one child.
var child *proto.RangeTreeNode
var err error
if node.LeftKey != nil {
if child, err = tc.getNode(node.LeftKey); err != nil {
return err
}
} else if node.RightKey != nil {
if child, err = tc.getNode(node.RightKey); err != nil {
return err
}
}
if !isRed(node) {
// Paint the node to the color of the child node.
node.Black = !isRed(child)
tc.setNode(node)
if err := tc.deleteCase1(node); err != nil {
return err
}
}
if _, err := tc.replaceNode(node, child); err != nil {
return err
}
// Always set the root back to black
if node, err = tc.getNode(node.Key); err != nil {
return err
}
if child != nil && node.ParentKey == nil {
if child, err = tc.getNode(child.Key); err != nil {
return err
}
child.Black = true
tc.setNode(child)
}
tc.dropNode(key)
return nil
}
// getMaxNode walks the tree to the right until it gets to a node without a
// right child and returns that node.
func (tc *treeContext) getMaxNode(node *proto.RangeTreeNode) (*proto.RangeTreeNode, error) {
if node.RightKey == nil {
return node, nil
}
right, err := tc.getNode(node.RightKey)
if err != nil {
return nil, err
}
return tc.getMaxNode(right)
}
// deleteCase1 handles the case when node is the new root. If so, there is
// nothing to do.
func (tc *treeContext) deleteCase1(node *proto.RangeTreeNode) error {
if node.ParentKey == nil {
return nil
}
return tc.deleteCase2(node)
}
// deleteCase2 handles the case when node has a red sibling.
func (tc *treeContext) deleteCase2(node *proto.RangeTreeNode) error {
sibling, err := tc.getSibling(node)
if err != nil {
return err
}
if isRed(sibling) {
parent, err := tc.getNode(node.ParentKey)
if err != nil {
return err
}
parent.Black = false
sibling.Black = true
tc.setNode(parent)
tc.setNode(sibling)
if parent.LeftKey.Equal(node.Key) {
if _, err := tc.rotateLeft(parent); err != nil {
return err
}
} else {
if _, err := tc.rotateRight(parent); err != nil {
return err
}
}
}
return tc.deleteCase3(node)
}
// deleteCase3 handles the case when node's parent, sibling and sibling's
// children are all black.
func (tc *treeContext) deleteCase3(node *proto.RangeTreeNode) error {
// This check uses cascading ifs to limit the number of db reads.
parent, err := tc.getNode(node.ParentKey)
if err != nil {
return err
}
if !isRed(parent) {
sibling, err := tc.getSibling(node)
if err != nil {
return err
}
if !isRed(sibling) {
siblingLeft, err := tc.getNode(sibling.LeftKey)
if err != nil {
return err
}
if !isRed(siblingLeft) {
siblingRight, err := tc.getNode(sibling.RightKey)
if err != nil {
return err
}
if !isRed(siblingRight) {
sibling.Black = false
tc.setNode(sibling)
return tc.deleteCase1(parent)
}
}
}
}
return tc.deleteCase4(node)
}
// deleteCase4 handles the case when node's sibling and siblings children are
// black, but parent is red.
func (tc *treeContext) deleteCase4(node *proto.RangeTreeNode) error {
// This check uses cascading ifs to limit the number of db reads.
parent, err := tc.getNode(node.ParentKey)
if err != nil {
return err
}
if isRed(parent) {
sibling, err := tc.getSibling(node)
if err != nil {
return err
}
if !isRed(sibling) {
siblingLeft, err := tc.getNode(sibling.LeftKey)
if err != nil {
return err
}
if !isRed(siblingLeft) {
siblingRight, err := tc.getNode(sibling.RightKey)
if err != nil {
return err
}
if !isRed(siblingRight) {
sibling.Black = false
tc.setNode(sibling)
parent.Black = true
tc.setNode(parent)
// This corrects the tree, no need to continue.
return nil
}
}
}
}
return tc.deleteCase5(node)
}
// deleteCase5 handles two mirror cases. The first case is when node is the left
// child of its parent, node's sibling is black, node's sibling's right child is
// black but left child is red. Or similarly, when node is the right child of
// its parent, node's sibling is black, node's sibling's left child is black but
// right child is red. The operations here actually only prepare the tree for
// deleteCase6.
func (tc *treeContext) deleteCase5(node *proto.RangeTreeNode) error {
parent, err := tc.getNode(node.ParentKey)
if err != nil {
return err
}
sibling, err := tc.getSibling(node)
if err != nil {
return err
}
siblingLeft, err := tc.getNode(sibling.LeftKey)
if err != nil {
return err
}
siblingRight, err := tc.getNode(sibling.RightKey)
if err != nil {
return err
}
if parent.LeftKey.Equal(node.Key) &&
!isRed(sibling) &&
!isRed(siblingRight) &&
isRed(siblingLeft) {
sibling.Black = false
tc.setNode(sibling)
siblingLeft.Black = true
tc.setNode(siblingLeft)
if _, err := tc.rotateRight(sibling); err != nil {
return err
}
} else if parent.RightKey.Equal(node.Key) &&
!isRed(sibling) &&
!isRed(siblingLeft) &&
isRed(siblingRight) {
sibling.Black = false
tc.setNode(sibling)
siblingRight.Black = true
tc.setNode(siblingRight)
if _, err := tc.rotateLeft(sibling); err != nil {
return err
}
}
return tc.deleteCase6(node)
}
// deleteCase6 handles two mirror cases. The first case is when node is the left
// child of its parent, node's sibling is black and node's sibling's right child
// is red. Or similarly, when node is the right child of its parent, node's
// sibling is black and node's sibling's left child is red. No checks are
// needed here since we have already been forced into this case by deleteCase5.
func (tc *treeContext) deleteCase6(node *proto.RangeTreeNode) error {
parent, err := tc.getNode(node.ParentKey)
if err != nil {
return err
}
sibling, err := tc.getSibling(node)
if err != nil {
return err
}
sibling.Black = parent.Black
tc.setNode(sibling)
parent.Black = true
tc.setNode(parent)
if node.Key.Equal(parent.LeftKey) {
siblingRight, err := tc.getNode(sibling.RightKey)
if err != nil {
return err
}
siblingRight.Black = true
tc.setNode(siblingRight)
if _, err := tc.rotateLeft(parent); err != nil {
return err
}
} else {
siblingLeft, err := tc.getNode(sibling.LeftKey)
if err != nil {
return err
}
siblingLeft.Black = true
tc.setNode(siblingLeft)
if _, err := tc.rotateRight(parent); err != nil {
return err
}
}
return nil
}