@@ -17,7 +17,6 @@ import (
1717 "strings"
1818 "time"
1919
20- "github.com/RaduBerinde/btree" // TODO(#144504): switch to the newer btree
2120 "github.com/cockroachdb/cockroach/pkg/kv/kvserver"
2221 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/allocator"
2322 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/allocator/allocatorimpl"
@@ -34,6 +33,7 @@ import (
3433 "github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfigreporter"
3534 "github.com/cockroachdb/cockroach/pkg/util/hlc"
3635 "github.com/cockroachdb/cockroach/pkg/util/timeutil"
36+ "github.com/google/btree"
3737)
3838
3939type state struct {
@@ -88,28 +88,26 @@ func newState(settings *config.SimulationSettings) *state {
8888type rmap struct {
8989 // NB: Both rangeTree and rangeMap hold references to ranges. They must
9090 // both be updated on insertion and deletion to maintain consistent state.
91- rangeTree * btree.BTree
91+ rangeTree * btree.BTreeG [ * rng ]
9292 rangeMap map [RangeID ]* rng
9393
9494 // Unique ID generator for Ranges.
9595 rangeSeqGen RangeID
9696}
9797
9898func newRMap () * rmap {
99+ lessFn := func (a , b * rng ) bool {
100+ return a .startKey < b .startKey
101+ }
99102 rmap := & rmap {
100- rangeTree : btree .New ( 8 ),
103+ rangeTree : btree .NewG [ * rng ]( 8 , lessFn ),
101104 rangeMap : make (map [RangeID ]* rng ),
102105 }
103106
104107 rmap .initFirstRange ()
105108 return rmap
106109}
107110
108- // Less is part of the btree.Item interface.
109- func (r * rng ) Less (than btree.Item ) bool {
110- return r .startKey < than .(* rng ).startKey
111- }
112-
113111// initFirstRange initializes the first range within the rangemap, with
114112// [MinKey, MaxKey) start and end key. All other ranges are split from this.
115113func (rm * rmap ) initFirstRange () {
@@ -164,8 +162,7 @@ func (s *state) String() string {
164162 builder := & strings.Builder {}
165163
166164 orderedRanges := []* rng {}
167- s .ranges .rangeTree .Ascend (func (i btree.Item ) bool {
168- r := i .(* rng )
165+ s .ranges .rangeTree .Ascend (func (r * rng ) bool {
169166 orderedRanges = append (orderedRanges , r )
170167 return ! r .desc .EndKey .Equal (MaxKey .ToRKey ())
171168 })
@@ -330,8 +327,8 @@ func (s *state) rangeFor(key Key) *rng {
330327 var r * rng
331328 // If keyToFind equals to MinKey of the range, we found the right range, if
332329 // the range is less than keyToFind then this is the right range also.
333- s .ranges .rangeTree .DescendLessOrEqual (keyToFind , func (i btree. Item ) bool {
334- r = i .( * rng )
330+ s .ranges .rangeTree .DescendLessOrEqual (keyToFind , func (i * rng ) bool {
331+ r = i
335332 return false
336333 })
337334 return r
@@ -688,8 +685,7 @@ func (s *state) SetSpanConfig(span roachpb.Span, config *roachpb.SpanConfig) {
688685 // [f, z) - keeps old span config from [c,z)
689686
690687 splitsRequired := []Key {}
691- s .ranges .rangeTree .DescendLessOrEqual (& rng {startKey : startKey }, func (i btree.Item ) bool {
692- cur , _ := i .(* rng )
688+ s .ranges .rangeTree .DescendLessOrEqual (& rng {startKey : startKey }, func (cur * rng ) bool {
693689 rStart := cur .startKey
694690 // There are two cases we handle:
695691 // (1) rStart == startKey: We don't need to split.
@@ -702,8 +698,7 @@ func (s *state) SetSpanConfig(span roachpb.Span, config *roachpb.SpanConfig) {
702698 return false
703699 })
704700
705- s .ranges .rangeTree .DescendLessOrEqual (& rng {startKey : endKey }, func (i btree.Item ) bool {
706- cur , _ := i .(* rng )
701+ s .ranges .rangeTree .DescendLessOrEqual (& rng {startKey : endKey }, func (cur * rng ) bool {
707702 rEnd := cur .endKey
708703 rStart := cur .startKey
709704 if rStart == endKey {
@@ -732,8 +727,7 @@ func (s *state) SetSpanConfig(span roachpb.Span, config *roachpb.SpanConfig) {
732727 }
733728
734729 // Apply the span config to all the ranges affected.
735- s .ranges .rangeTree .AscendGreaterOrEqual (& rng {startKey : startKey }, func (i btree.Item ) bool {
736- cur , _ := i .(* rng )
730+ s .ranges .rangeTree .AscendGreaterOrEqual (& rng {startKey : startKey }, func (cur * rng ) bool {
737731 if cur .startKey == endKey {
738732 return false
739733 }
@@ -800,16 +794,15 @@ func (s *state) SplitRange(splitKey Key) (Range, Range, bool) {
800794 endKey := Key (math .MaxInt32 )
801795 failed := false
802796 // Find the sucessor range in the range map, to determine the endkey.
803- ranges .rangeTree .AscendGreaterOrEqual (r , func (i btree. Item ) bool {
797+ ranges .rangeTree .AscendGreaterOrEqual (r , func (i * rng ) bool {
804798 // The min key already exists in the range map, we cannot return a new
805799 // range.
806- if ! r . Less ( i ) {
800+ if r . startKey == i . startKey {
807801 failed = true
808802 return false
809803 }
810804
811- successorRange , _ := i .(* rng )
812- endKey = successorRange .startKey
805+ endKey = i .startKey
813806 return false
814807 })
815808
@@ -822,10 +815,10 @@ func (s *state) SplitRange(splitKey Key) (Range, Range, bool) {
822815 var predecessorRange * rng
823816 // Find the predecessor range, to update it's endkey to the new range's min
824817 // key.
825- ranges .rangeTree .DescendLessOrEqual (r , func (i btree. Item ) bool {
818+ ranges .rangeTree .DescendLessOrEqual (r , func (i * rng ) bool {
826819 // The case where the min key already exists cannot occur here, as the
827820 // failed flag will have been set above.
828- predecessorRange , _ = i .( * rng )
821+ predecessorRange = i
829822 return false
830823 })
831824
@@ -998,8 +991,7 @@ func (s *state) ApplyLoad(lb workload.LoadBatch) {
998991 // that range is not larger than the any key of the remaining load events.
999992 iter := n - 1
1000993 max := & rng {startKey : Key (lb [iter ].Key )}
1001- s .ranges .rangeTree .DescendLessOrEqual (max , func (i btree.Item ) bool {
1002- next , _ := i .(* rng )
994+ s .ranges .rangeTree .DescendLessOrEqual (max , func (next * rng ) bool {
1003995 for iter > - 1 && lb [iter ].Key >= int64 (next .startKey ) {
1004996 s .applyLoad (next , lb [iter ])
1005997 iter --
0 commit comments