Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

roachpb: extract keysbase to break some dependencies #77318

Merged
merged 1 commit into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
/pkg/internal/team/ @cockroachdb/test-eng
/pkg/jobs/ @cockroachdb/cdc-prs
/pkg/keys/ @cockroachdb/kv-prs
/pkg/keysbase/ @cockroachdb/kv-prs
# Don't ping KV on updates to reserved descriptor IDs and such.
/pkg/keys/constants.go @cockroachdb/kv-prs-noreview
/pkg/migration/ @cockroachdb/kv-prs-noreview @cockroachdb/sql-schema
Expand Down
8 changes: 8 additions & 0 deletions pkg/keysbase/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "keysbase",
srcs = ["data.go"],
importpath = "github.com/cockroachdb/cockroach/pkg/keysbase",
visibility = ["//visibility:public"],
)
36 changes: 36 additions & 0 deletions pkg/keysbase/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package keysbase

// KeyMax is a maximum key value which sorts after all other keys.
var KeyMax = []byte{0xff, 0xff}

// PrefixEnd determines the end key given b as a prefix, that is the key that
// sorts precisely behind all keys starting with prefix: "1" is added to the
// final byte and the carry propagated. The special cases of nil and KeyMin
// always returns KeyMax.
func PrefixEnd(b []byte) []byte {
if len(b) == 0 {
return KeyMax
}
// Switched to "make and copy" pattern in #4963 for performance.
end := make([]byte, len(b))
copy(end, b)
for i := len(end) - 1; i >= 0; i-- {
end[i] = end[i] + 1
if end[i] != 0 {
return end[:i+1]
}
}
// This statement will only be reached if the key is already a maximal byte
// string (i.e. already \xff...).
return b
}
1 change: 1 addition & 0 deletions pkg/roachpb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/geo/geopb",
"//pkg/keysbase",
"//pkg/kv/kvserver/concurrency/lock",
"//pkg/storage/enginepb",
"//pkg/util",
Expand Down
28 changes: 4 additions & 24 deletions pkg/roachpb/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/cockroachdb/apd/v3"
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/cockroachdb/cockroach/pkg/keysbase"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util"
Expand Down Expand Up @@ -57,7 +58,7 @@ var (
// KeyMin is a minimum key value which sorts before all other keys.
KeyMin = Key(RKeyMin)
// RKeyMax is a maximum key value which sorts after all other keys.
RKeyMax = RKey{0xff, 0xff}
RKeyMax = RKey(keysbase.KeyMax)
// KeyMax is a maximum key value which sorts after all other keys.
KeyMax = Key(RKeyMax)

Expand Down Expand Up @@ -123,10 +124,7 @@ func (rk RKey) Next() RKey {
// is added to the final byte and the carry propagated. The special
// cases of nil and KeyMin always returns KeyMax.
func (rk RKey) PrefixEnd() RKey {
if len(rk) == 0 {
return RKeyMax
}
return RKey(bytesPrefixEnd(rk))
return RKey(keysbase.PrefixEnd(rk))
}

func (rk RKey) String() string {
Expand Down Expand Up @@ -159,21 +157,6 @@ func BytesNext(b []byte) []byte {
return bn
}

func bytesPrefixEnd(b []byte) []byte {
// Switched to "make and copy" pattern in #4963 for performance.
end := make([]byte, len(b))
copy(end, b)
for i := len(end) - 1; i >= 0; i-- {
end[i] = end[i] + 1
if end[i] != 0 {
return end[:i+1]
}
}
// This statement will only be reached if the key is already a
// maximal byte string (i.e. already \xff...).
return b
}

// Clone returns a copy of the key.
func (k Key) Clone() Key {
if k == nil {
Expand Down Expand Up @@ -202,10 +185,7 @@ func (k Key) IsPrev(m Key) bool {
// is added to the final byte and the carry propagated. The special
// cases of nil and KeyMin always returns KeyMax.
func (k Key) PrefixEnd() Key {
if len(k) == 0 {
return Key(RKeyMax)
}
return Key(bytesPrefixEnd(k))
return Key(keysbase.PrefixEnd(k))
}

// Equal returns whether two keys are identical.
Expand Down
8 changes: 6 additions & 2 deletions pkg/sql/inverted/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/sql/inverted",
visibility = ["//visibility:public"],
deps = [
"//pkg/roachpb",
"//pkg/keysbase",
"//pkg/util/treeprinter",
"@com_github_cockroachdb_errors//:errors",
],
Expand All @@ -18,11 +18,15 @@ go_library(
go_test(
name = "inverted_test",
size = "small",
srcs = ["expression_test.go"],
srcs = [
"dep_test.go",
"expression_test.go",
],
data = glob(["testdata/**"]),
embed = [":inverted"],
deps = [
"//pkg/testutils",
"//pkg/testutils/buildutil",
"//pkg/util/encoding",
"//pkg/util/leaktest",
"//pkg/util/treeprinter",
Expand Down
26 changes: 26 additions & 0 deletions pkg/sql/inverted/dep_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package inverted

import (
"testing"

"github.com/cockroachdb/cockroach/pkg/testutils/buildutil"
)

func TestNoLinkForbidden(t *testing.T) {
buildutil.VerifyNoImports(t,
"github.com/cockroachdb/cockroach/pkg/sql/inverted", true,
[]string{
"github.com/cockroachdb/cockroach/pkg/roachpb",
}, nil,
)
}
6 changes: 3 additions & 3 deletions pkg/sql/inverted/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"fmt"
"strconv"

"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/keysbase"
"github.com/cockroachdb/cockroach/pkg/util/treeprinter"
"github.com/cockroachdb/errors"
)
Expand Down Expand Up @@ -100,13 +100,13 @@ type Span struct {

// MakeSingleValSpan constructs a span equivalent to [val, val].
func MakeSingleValSpan(val EncVal) Span {
end := EncVal(roachpb.Key(val).PrefixEnd())
end := EncVal(keysbase.PrefixEnd(val))
return Span{Start: val, End: end}
}

// IsSingleVal returns true iff the span is equivalent to [val, val].
func (s Span) IsSingleVal() bool {
return bytes.Equal(roachpb.Key(s.Start).PrefixEnd(), s.End)
return bytes.Equal(keysbase.PrefixEnd(s.Start), s.End)
}

// Equals returns true if this span has the same start and end as the given
Expand Down
4 changes: 3 additions & 1 deletion pkg/util/json/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ go_library(
deps = [
"//pkg/geo",
"//pkg/geo/geopb",
"//pkg/roachpb",
"//pkg/keysbase",
"//pkg/sql/inverted",
"//pkg/sql/pgwire/pgcode",
"//pkg/sql/pgwire/pgerror",
Expand All @@ -34,6 +34,7 @@ go_test(
name = "json_test",
size = "small",
srcs = [
"dep_test.go",
"encode_test.go",
"json_test.go",
],
Expand All @@ -43,6 +44,7 @@ go_test(
"//pkg/sql/inverted",
"//pkg/sql/pgwire/pgerror",
"//pkg/testutils",
"//pkg/testutils/buildutil",
"//pkg/util/encoding",
"//pkg/util/randutil",
"//pkg/util/timeutil",
Expand Down
26 changes: 26 additions & 0 deletions pkg/util/json/dep_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package json

import (
"testing"

"github.com/cockroachdb/cockroach/pkg/testutils/buildutil"
)

func TestNoLinkForbidden(t *testing.T) {
buildutil.VerifyNoImports(t,
"github.com/cockroachdb/cockroach/pkg/util/json", true,
[]string{
"github.com/cockroachdb/cockroach/pkg/roachpb",
}, nil,
)
}
4 changes: 2 additions & 2 deletions pkg/util/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/cockroachdb/apd/v3"
"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/keysbase"
"github.com/cockroachdb/cockroach/pkg/sql/inverted"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
Expand Down Expand Up @@ -1341,7 +1341,7 @@ func encodeContainingInvertedIndexSpansFromLeaf(
// for JSON objects.
Start: inverted.EncVal(encoding.EncodeJSONObjectSpanStartAscending(prefix)),
// This end key is equal to jsonInvertedIndex + 1.
End: inverted.EncVal(roachpb.Key(prefix).PrefixEnd()),
End: inverted.EncVal(keysbase.PrefixEnd(prefix)),
}, true /* tight */))

default:
Expand Down