@@ -13,11 +13,11 @@ import (
1313 "strings"
1414 "sync/atomic"
1515
16- "github.com/RaduBerinde/btree" // TODO(#144504): switch to the newer btree
1716 "github.com/cockroachdb/cockroach/pkg/util/cache"
1817 "github.com/cockroachdb/cockroach/pkg/util/metric"
1918 "github.com/cockroachdb/cockroach/pkg/util/syncutil"
2019 "github.com/cockroachdb/errors"
20+ "github.com/google/btree"
2121 io_prometheus_client "github.com/prometheus/client_model/go"
2222)
2323
@@ -86,8 +86,21 @@ type childSet struct {
8686
8787func (cs * childSet ) initWithBTreeStorageType (labels []string ) {
8888 cs .labels = labels
89+
90+ lessFn := func (a , b MetricItem ) bool {
91+ av , bv := a .labelValues (), b .labelValues ()
92+ if len (av ) != len (bv ) {
93+ panic (errors .AssertionFailedf ("mismatch in label values lengths %v vs %v" , av , bv ))
94+ }
95+ for i := range av {
96+ if cmp := strings .Compare (av [i ], bv [i ]); cmp != 0 {
97+ return cmp < 0
98+ }
99+ }
100+ return false
101+ }
89102 cs .mu .children = & BtreeWrapper {
90- tree : btree .New ( 8 ),
103+ tree : btree .NewG [ MetricItem ]( 8 , lessFn ),
91104 }
92105}
93106
@@ -280,11 +293,6 @@ type MetricItem interface {
280293 labelValuer
281294}
282295
283- type BtreeMetricItem interface {
284- btree.Item
285- MetricItem
286- }
287-
288296type CacheMetricItem interface {
289297 MetricItem
290298}
@@ -364,34 +372,33 @@ func (ucw *UnorderedCacheWrapper) Clear() {
364372}
365373
366374type BtreeWrapper struct {
367- tree * btree.BTree
375+ tree * btree.BTreeG [ MetricItem ]
368376}
369377
370378func (b BtreeWrapper ) Get (labelVals ... string ) (ChildMetric , bool ) {
371379 key := labelValuesSlice (labelVals )
372- cm := b .tree .Get (& key )
373- if cm == nil {
380+ cm , ok := b .tree .Get (& key )
381+ if ! ok {
374382 return nil , false
375383 }
376384 return cm .(ChildMetric ), true
377385}
378386
379387func (b BtreeWrapper ) Add (metric ChildMetric ) {
380- if b .tree .Has (metric .( BtreeMetricItem ) ) {
388+ if b .tree .Has (metric ) {
381389 panic (errors .AssertionFailedf ("child %v already exists" , metric .labelValues ()))
382390 }
383- b .tree .ReplaceOrInsert (metric .( BtreeMetricItem ) )
391+ b .tree .ReplaceOrInsert (metric )
384392}
385393
386394func (b BtreeWrapper ) Del (metric ChildMetric ) {
387- if existing := b .tree .Delete (metric .(btree.Item )); existing == nil {
388- panic (errors .AssertionFailedf (
389- "child %v does not exists" , metric .labelValues ()))
395+ if _ , ok := b .tree .Delete (metric ); ! ok {
396+ panic (errors .AssertionFailedf ("child %v does not exist" , metric .labelValues ()))
390397 }
391398}
392399
393400func (b BtreeWrapper ) ForEach (f func (metric ChildMetric )) {
394- b .tree .Ascend (func (i btree. Item ) bool {
401+ b .tree .Ascend (func (i MetricItem ) bool {
395402 f (i .(ChildMetric ))
396403 return true
397404 })
@@ -400,17 +407,3 @@ func (b BtreeWrapper) ForEach(f func(metric ChildMetric)) {
400407func (b BtreeWrapper ) Clear () {
401408 b .tree .Clear (false )
402409}
403-
404- func (lv * labelValuesSlice ) Less (o btree.Item ) bool {
405- ov := o .(labelValuer ).labelValues ()
406- if len (ov ) != len (* lv ) {
407- panic (errors .AssertionFailedf ("mismatch in label values lengths %v vs %v" ,
408- ov , * lv ))
409- }
410- for i := range ov {
411- if cmp := strings .Compare ((* lv )[i ], ov [i ]); cmp != 0 {
412- return cmp < 0
413- }
414- }
415- return false // eq
416- }
0 commit comments