Skip to content

Commit 2efdf20

Browse files
committed
span: make frontierHeap type-safe
This patch makes `frontierHeap` type-safe by replacing the usage of the go standard library heap package with the cockroach copy of the heap package that uses generics. Release note: None
1 parent 15c9b15 commit 2efdf20

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

pkg/util/span/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ go_library(
1515
"//pkg/keys",
1616
"//pkg/roachpb",
1717
"//pkg/util/buildutil",
18+
"//pkg/util/container/heap",
1819
"//pkg/util/hlc",
1920
"//pkg/util/interval",
2021
"//pkg/util/syncutil",
@@ -34,6 +35,7 @@ go_test(
3435
embed = [":span"],
3536
deps = [
3637
"//pkg/roachpb",
38+
"//pkg/util/container/heap",
3739
"//pkg/util/hlc",
3840
"//pkg/util/leaktest",
3941
"//pkg/util/log",

pkg/util/span/frontier.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package span
77

88
import (
9-
"container/heap"
109
"fmt"
1110
"iter"
1211
"strings"
@@ -17,6 +16,7 @@ import (
1716
_ "github.com/cockroachdb/cockroach/pkg/keys"
1817
"github.com/cockroachdb/cockroach/pkg/roachpb"
1918
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
19+
"github.com/cockroachdb/cockroach/pkg/util/container/heap"
2020
"github.com/cockroachdb/cockroach/pkg/util/hlc"
2121
"github.com/cockroachdb/cockroach/pkg/util/interval"
2222
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
@@ -135,6 +135,8 @@ type btreeFrontier struct {
135135
disallowMutationWhileIterating atomic.Bool
136136
}
137137

138+
var _ Frontier = (*btreeFrontier)(nil)
139+
138140
// btreeFrontierEntry represents a timestamped span. It is used as the nodes in both
139141
// the tree and heap needed to keep the Frontier.
140142
// btreeFrontierEntry implements interval/generic interface.
@@ -652,6 +654,8 @@ func (e *btreeFrontierEntry) isEmptyRange() bool {
652654
// of the heap.
653655
type frontierHeap []*btreeFrontierEntry
654656

657+
var _ heap.Interface[*btreeFrontierEntry] = (*frontierHeap)(nil)
658+
655659
// Len implements heap.Interface.
656660
func (h frontierHeap) Len() int { return len(h) }
657661

@@ -670,15 +674,14 @@ func (h frontierHeap) Swap(i, j int) {
670674
}
671675

672676
// Push implements heap.Interface.
673-
func (h *frontierHeap) Push(x interface{}) {
677+
func (h *frontierHeap) Push(x *btreeFrontierEntry) {
674678
n := len(*h)
675-
entry := x.(*btreeFrontierEntry)
676-
entry.heapIdx = n
677-
*h = append(*h, entry)
679+
x.heapIdx = n
680+
*h = append(*h, x)
678681
}
679682

680683
// Pop implements heap.Interface.
681-
func (h *frontierHeap) Pop() interface{} {
684+
func (h *frontierHeap) Pop() *btreeFrontierEntry {
682685
old := *h
683686
n := len(old)
684687
entry := old[n-1]

pkg/util/span/frontier_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
package span
77

88
import (
9-
"container/heap"
109
"context"
1110
"fmt"
1211
"math/rand"
1312
"strings"
1413
"testing"
1514

1615
"github.com/cockroachdb/cockroach/pkg/roachpb"
16+
"github.com/cockroachdb/cockroach/pkg/util/container/heap"
1717
"github.com/cockroachdb/cockroach/pkg/util/hlc"
1818
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
1919
"github.com/cockroachdb/cockroach/pkg/util/log"

0 commit comments

Comments
 (0)