-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
span_set.go
327 lines (273 loc) · 8.04 KB
/
span_set.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
//
// Author: Ben Darnell
package storage
import (
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/storage/engine/enginepb"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
)
// SpanAccess records the intended mode of access in SpanSet.
type SpanAccess int
// Constants for SpanAccess. Higher-valued accesses imply lower-level ones.
const (
SpanReadOnly SpanAccess = iota
SpanReadWrite
numSpanAccess
)
type spanScope int
const (
spanGlobal spanScope = iota
spanLocal
numSpanScope
)
// SpanSet tracks the set of key spans touched by a command. The set
// is divided into subsets for access type (read-only or read/write)
// and key scope (local or global; used to facilitate use by the
// separate local and global command queues).
type SpanSet struct {
spans [numSpanAccess][numSpanScope][]roachpb.Span
}
// reserve space for N additional keys.
func (ss *SpanSet) reserve(access SpanAccess, scope spanScope, n int) {
existing := ss.spans[access][scope]
ss.spans[access][scope] = make([]roachpb.Span, len(existing), n+cap(existing))
copy(ss.spans[access][scope], existing)
}
// Add a span to the set.
func (ss *SpanSet) Add(access SpanAccess, span roachpb.Span) {
scope := spanGlobal
if keys.IsLocal(span.Key) {
scope = spanLocal
}
ss.spans[access][scope] = append(ss.spans[access][scope], span)
}
// getSpans returns a slice of spans with the given parameters.
func (ss *SpanSet) getSpans(access SpanAccess, scope spanScope) []roachpb.Span {
return ss.spans[access][scope]
}
func (ss *SpanSet) checkAllowed(access SpanAccess, span roachpb.Span) error {
scope := spanGlobal
if keys.IsLocal(span.Key) {
scope = spanLocal
}
for ac := access; ac < numSpanAccess; ac++ {
for _, s := range ss.spans[ac][scope] {
if s.Key.Equal(keys.LocalMax) && s.EndKey.Equal(roachpb.KeyMax) {
continue
}
if s.Contains(span) {
return nil
}
}
}
action := "read"
if access == SpanReadWrite {
action = "write"
}
return errors.Errorf("cannot %s undeclared span %s", action, span)
}
type spanSetIterator struct {
i engine.Iterator
spans *SpanSet
err error
}
var _ engine.Iterator = spanSetIterator{}
func (s spanSetIterator) Close() {
s.i.Close()
}
func (s spanSetIterator) Seek(key engine.MVCCKey) {
if s.err == nil {
s.err = s.spans.checkAllowed(SpanReadOnly, roachpb.Span{Key: key.Key})
}
s.i.Seek(key)
}
func (s spanSetIterator) SeekReverse(key engine.MVCCKey) {
if s.err == nil {
s.err = s.spans.checkAllowed(SpanReadOnly, roachpb.Span{Key: key.Key})
}
s.i.SeekReverse(key)
}
func (s spanSetIterator) Valid() bool {
return s.err == nil && s.i.Valid()
}
func (s spanSetIterator) Next() {
s.i.Next()
if s.err == nil {
s.err = s.spans.checkAllowed(SpanReadOnly, roachpb.Span{Key: s.UnsafeKey().Key})
}
}
func (s spanSetIterator) Prev() {
s.i.Prev()
if s.err == nil {
s.err = s.spans.checkAllowed(SpanReadOnly, roachpb.Span{Key: s.UnsafeKey().Key})
}
}
func (s spanSetIterator) NextKey() {
s.i.NextKey()
if s.err == nil {
s.err = s.spans.checkAllowed(SpanReadOnly, roachpb.Span{Key: s.UnsafeKey().Key})
}
}
func (s spanSetIterator) PrevKey() {
s.i.PrevKey()
if s.err == nil {
s.err = s.spans.checkAllowed(SpanReadOnly, roachpb.Span{Key: s.UnsafeKey().Key})
}
}
func (s spanSetIterator) Key() engine.MVCCKey {
return s.i.Key()
}
func (s spanSetIterator) Value() []byte {
return s.i.Value()
}
func (s spanSetIterator) ValueProto(msg proto.Message) error {
return s.i.ValueProto(msg)
}
func (s spanSetIterator) UnsafeKey() engine.MVCCKey {
return s.i.UnsafeKey()
}
func (s spanSetIterator) UnsafeValue() []byte {
return s.i.UnsafeValue()
}
func (s spanSetIterator) Less(key engine.MVCCKey) bool {
return s.i.Less(key)
}
func (s spanSetIterator) Error() error {
if s.err != nil {
return s.err
}
return s.i.Error()
}
func (s spanSetIterator) ComputeStats(start, end engine.MVCCKey, nowNanos int64) (enginepb.MVCCStats, error) {
if err := s.spans.checkAllowed(SpanReadOnly, roachpb.Span{Key: start.Key, EndKey: end.Key}); err != nil {
return enginepb.MVCCStats{}, err
}
return s.i.ComputeStats(start, end, nowNanos)
}
type spanSetReader struct {
r engine.Reader
spans *SpanSet
}
var _ engine.Reader = spanSetReader{}
func (s spanSetReader) Close() {
s.r.Close()
}
func (s spanSetReader) Closed() bool {
return s.r.Closed()
}
func (s spanSetReader) Get(key engine.MVCCKey) ([]byte, error) {
if err := s.spans.checkAllowed(SpanReadOnly, roachpb.Span{Key: key.Key}); err != nil {
return nil, err
}
return s.r.Get(key)
}
func (s spanSetReader) GetProto(key engine.MVCCKey, msg proto.Message) (bool, int64, int64, error) {
if err := s.spans.checkAllowed(SpanReadOnly, roachpb.Span{Key: key.Key}); err != nil {
return false, 0, 0, err
}
return s.r.GetProto(key, msg)
}
func (s spanSetReader) Iterate(start, end engine.MVCCKey, f func(engine.MVCCKeyValue) (bool, error)) error {
if err := s.spans.checkAllowed(SpanReadOnly, roachpb.Span{Key: start.Key, EndKey: end.Key}); err != nil {
return err
}
return s.r.Iterate(start, end, f)
}
func (s spanSetReader) NewIterator(prefix bool) engine.Iterator {
return spanSetIterator{s.r.NewIterator(prefix), s.spans, nil}
}
type spanSetWriter struct {
w engine.Writer
spans *SpanSet
}
var _ engine.Writer = spanSetWriter{}
func (s spanSetWriter) ApplyBatchRepr(repr []byte, sync bool) error {
// Assume that the constructor of the batch has bounded it correctly.
return s.w.ApplyBatchRepr(repr, sync)
}
func (s spanSetWriter) Clear(key engine.MVCCKey) error {
if err := s.spans.checkAllowed(SpanReadWrite, roachpb.Span{Key: key.Key}); err != nil {
return err
}
return s.w.Clear(key)
}
func (s spanSetWriter) ClearRange(start, end engine.MVCCKey) error {
if err := s.spans.checkAllowed(SpanReadWrite, roachpb.Span{Key: start.Key, EndKey: end.Key}); err != nil {
return err
}
return s.w.ClearRange(start, end)
}
func (s spanSetWriter) ClearIterRange(iter engine.Iterator, start, end engine.MVCCKey) error {
if err := s.spans.checkAllowed(SpanReadWrite, roachpb.Span{Key: start.Key, EndKey: end.Key}); err != nil {
return err
}
return s.w.ClearIterRange(iter, start, end)
}
func (s spanSetWriter) Merge(key engine.MVCCKey, value []byte) error {
if err := s.spans.checkAllowed(SpanReadWrite, roachpb.Span{Key: key.Key}); err != nil {
return err
}
return s.w.Merge(key, value)
}
func (s spanSetWriter) Put(key engine.MVCCKey, value []byte) error {
if err := s.spans.checkAllowed(SpanReadWrite, roachpb.Span{Key: key.Key}); err != nil {
return err
}
return s.w.Put(key, value)
}
type spanSetReadWriter struct {
spanSetReader
spanSetWriter
}
var _ engine.ReadWriter = spanSetReadWriter{}
func makeSpanSetReadWriter(rw engine.ReadWriter, spans *SpanSet) spanSetReadWriter {
return spanSetReadWriter{
spanSetReader{
r: rw,
spans: spans,
},
spanSetWriter{
w: rw,
spans: spans,
},
}
}
type spanSetBatch struct {
spanSetReadWriter
b engine.Batch
spans *SpanSet
}
var _ engine.Batch = spanSetBatch{}
func (s spanSetBatch) Commit(sync bool) error {
return s.b.Commit(sync)
}
func (s spanSetBatch) Distinct() engine.ReadWriter {
return makeSpanSetReadWriter(s.b.Distinct(), s.spans)
}
func (s spanSetBatch) Repr() []byte {
return s.b.Repr()
}
func makeSpanSetBatch(b engine.Batch, spans *SpanSet) engine.Batch {
return &spanSetBatch{
makeSpanSetReadWriter(b, spans),
b,
spans,
}
}