-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathlockspanset.go
97 lines (83 loc) · 2.6 KB
/
lockspanset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2023 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 lockspanset
import (
"fmt"
"strings"
"sync"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock"
"github.com/cockroachdb/cockroach/pkg/roachpb"
)
const NumLockStrength = lock.MaxStrength + 1
type LockSpanSet struct {
spans [NumLockStrength][]roachpb.Span
}
var lockSpanSetPool = sync.Pool{
New: func() interface{} { return new(LockSpanSet) },
}
// New creates a new empty LockSpanSet.
func New() *LockSpanSet {
return lockSpanSetPool.Get().(*LockSpanSet)
}
// GetSpans returns a slice of spans with the given access.
func (l *LockSpanSet) GetSpans(access lock.Strength) []roachpb.Span {
return l.spans[access]
}
// Add adds the supplied span to the lock span set to be accessed with the given
// lock strength.
func (l *LockSpanSet) Add(access lock.Strength, span roachpb.Span) {
l.spans[access] = append(l.spans[access], span)
}
// SortAndDeDup sorts the spans in the SpanSet and removes any duplicates.
func (l *LockSpanSet) SortAndDeDup() {
for st := lock.Strength(0); st < NumLockStrength; st++ {
l.spans[st], _ /* distinct */ = roachpb.MergeSpans(&l.spans[st])
}
}
// Release releases the LockSpanSet and its underlying slices. The receiver
// should not be used after being released.
func (l *LockSpanSet) Release() {
for st := lock.Strength(0); st < NumLockStrength; st++ {
// Recycle slice if capacity below threshold.
const maxRecycleCap = 8
var recycle []roachpb.Span
if sl := l.spans[st]; cap(sl) <= maxRecycleCap {
for i := range sl {
sl[i] = roachpb.Span{}
}
recycle = sl[:0]
}
l.spans[st] = recycle
}
lockSpanSetPool.Put(l)
}
// Empty returns whether the set contains any spans across all lock strengths.
func (l *LockSpanSet) Empty() bool {
return l.Len() == 0
}
// 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) {
fmt.Fprintf(&buf, "%s: %s\n",
st, span)
}
}
return buf.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))
}
return count
}