Skip to content

Commit

Permalink
lockspanset: cleanup some loops
Browse files Browse the repository at this point in the history
Followup from cockroachdb#102396.
This patch converts some loops to be more idiomatic for go that that
patch mistakenly didn't.

Epic: none

Release note: None
  • Loading branch information
arulajmani committed May 1, 2023
1 parent e45aac9 commit eb2380d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
7 changes: 7 additions & 0 deletions pkg/kv/kvserver/concurrency/lock/locking.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ var ExclusiveLocksBlockNonLockingReads = settings.RegisterBoolSetting(
// MaxStrength is the maximum value in the Strength enum.
const MaxStrength = Intent

// NumLockStrength is the total number of lock strengths in the Strength enum.
const NumLockStrength = MaxStrength + 1

// MaxDurability is the maximum value in the Durability enum.
const MaxDurability = Unreplicated

Expand All @@ -62,6 +65,10 @@ func init() {
panic(fmt.Sprintf("Strength (%s) with value larger than MaxDurability", st))
}
}
if int(NumLockStrength) != len(Strength_name) {
panic(fmt.Sprintf("mismatched numer of lock strengths: NumLockStrength %d, lock strengths %d",
int(NumLockStrength), len(Strength_name)))
}
for v := range Durability_name {
if d := Durability(v); d > MaxDurability {
panic(fmt.Sprintf("Durability (%s) with value larger than MaxDurability", d))
Expand Down
1 change: 1 addition & 0 deletions pkg/kv/kvserver/lockspanset/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ go_test(
args = ["-test.timeout=295s"],
embed = [":lockspanset"],
deps = [
"//pkg/keys",
"//pkg/kv/kvserver/concurrency/lock",
"//pkg/roachpb",
"//pkg/util/leaktest",
Expand Down
14 changes: 6 additions & 8 deletions pkg/kv/kvserver/lockspanset/lockspanset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/roachpb"
)

const NumLockStrength = lock.MaxStrength + 1

type LockSpanSet struct {
spans [NumLockStrength][]roachpb.Span
spans [lock.NumLockStrength][]roachpb.Span
}

var lockSpanSetPool = sync.Pool{
Expand Down Expand Up @@ -78,10 +76,10 @@ func (l *LockSpanSet) Empty() bool {
// String prints a string representation of the LockSpanSet.
func (l *LockSpanSet) String() string {
var buf strings.Builder
for st := lock.Strength(0); st < NumLockStrength; st++ {
for _, span := range l.GetSpans(st) {
for st, spans := range l.spans {
for _, span := range spans {
fmt.Fprintf(&buf, "%s: %s\n",
st, span)
lock.Strength(st), span)
}
}
return buf.String()
Expand All @@ -90,8 +88,8 @@ func (l *LockSpanSet) String() string {
// Len returns the total number of spans tracked across all strengths.
func (l *LockSpanSet) Len() int {
var count int
for st := lock.Strength(0); st < NumLockStrength; st++ {
count += len(l.GetSpans(st))
for _, spans := range l.spans {
count += len(spans)
}
return count
}
Expand Down

0 comments on commit eb2380d

Please sign in to comment.