-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
batch.go
763 lines (657 loc) · 21.7 KB
/
batch.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
// Copyright 2017 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 spanset
import (
"context"
"io"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/pebble"
)
// MVCCIterator wraps an storage.MVCCIterator and ensures that it can
// only be used to access spans in a SpanSet.
type MVCCIterator struct {
i storage.MVCCIterator
spans *SpanSet
// spansOnly controls whether or not timestamps associated with the
// spans are considered when ensuring access. If set to true,
// only span boundaries are checked.
spansOnly bool
// Timestamp the access is taking place. If timestamp is zero, access is
// considered non-MVCC. If spansOnly is set to true, ts is not consulted.
ts hlc.Timestamp
// Seeking to an invalid key puts the iterator in an error state.
err error
// Reaching an out-of-bounds key with Next/Prev invalidates the
// iterator but does not set err.
invalid bool
}
var _ storage.MVCCIterator = &MVCCIterator{}
// NewIterator constructs an iterator that verifies access of the underlying
// iterator against the given SpanSet. Timestamps associated with the spans
// in the spanset are not considered, only the span boundaries are checked.
func NewIterator(iter storage.MVCCIterator, spans *SpanSet) *MVCCIterator {
return &MVCCIterator{i: iter, spans: spans, spansOnly: true}
}
// NewIteratorAt constructs an iterator that verifies access of the underlying
// iterator against the given SpanSet at the given timestamp.
func NewIteratorAt(iter storage.MVCCIterator, spans *SpanSet, ts hlc.Timestamp) *MVCCIterator {
return &MVCCIterator{i: iter, spans: spans, ts: ts}
}
// Close is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) Close() {
i.i.Close()
}
// Iterator returns the underlying storage.MVCCIterator.
func (i *MVCCIterator) Iterator() storage.MVCCIterator {
return i.i
}
// Valid is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) Valid() (bool, error) {
if i.err != nil {
return false, i.err
}
ok, err := i.i.Valid()
if err != nil {
return false, err
}
return ok && !i.invalid, nil
}
// SeekGE is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) SeekGE(key storage.MVCCKey) {
i.i.SeekGE(key)
i.checkAllowed(roachpb.Span{Key: key.Key}, true)
}
// SeekIntentGE is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) SeekIntentGE(key roachpb.Key, txnUUID uuid.UUID) {
i.i.SeekIntentGE(key, txnUUID)
i.checkAllowed(roachpb.Span{Key: key}, true)
}
// SeekLT is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) SeekLT(key storage.MVCCKey) {
i.i.SeekLT(key)
// CheckAllowed{At} supports the span representation of [,key), which
// corresponds to the span [key.Prev(),).
i.checkAllowed(roachpb.Span{EndKey: key.Key}, true)
}
// Next is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) Next() {
i.i.Next()
i.checkAllowed(roachpb.Span{Key: i.UnsafeKey().Key}, false)
}
// Prev is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) Prev() {
i.i.Prev()
i.checkAllowed(roachpb.Span{Key: i.UnsafeKey().Key}, false)
}
// NextKey is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) NextKey() {
i.i.NextKey()
i.checkAllowed(roachpb.Span{Key: i.UnsafeKey().Key}, false)
}
func (i *MVCCIterator) checkAllowed(span roachpb.Span, errIfDisallowed bool) {
i.invalid = false
i.err = nil
if ok, _ := i.i.Valid(); !ok {
// If the iterator is invalid after the operation, there's nothing to
// check. We allow uses of iterators to exceed the declared span bounds
// as long as the iterator itself is configured with proper boundaries.
return
}
var err error
if i.spansOnly {
err = i.spans.CheckAllowed(SpanReadOnly, span)
} else {
err = i.spans.CheckAllowedAt(SpanReadOnly, span, i.ts)
}
if errIfDisallowed {
i.err = err
} else {
i.invalid = err != nil
}
}
// Key is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) Key() storage.MVCCKey {
return i.i.Key()
}
// Value is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) Value() []byte {
return i.i.Value()
}
// ValueProto is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) ValueProto(msg protoutil.Message) error {
return i.i.ValueProto(msg)
}
// UnsafeKey is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) UnsafeKey() storage.MVCCKey {
return i.i.UnsafeKey()
}
// UnsafeRawKey is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) UnsafeRawKey() []byte {
return i.i.UnsafeRawKey()
}
// UnsafeRawMVCCKey is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) UnsafeRawMVCCKey() []byte {
return i.i.UnsafeRawMVCCKey()
}
// UnsafeValue is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) UnsafeValue() []byte {
return i.i.UnsafeValue()
}
// IsCurIntentSeparated implements the MVCCIterator interface.
func (i *MVCCIterator) IsCurIntentSeparated() bool {
return i.i.IsCurIntentSeparated()
}
// ComputeStats is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) ComputeStats(
start, end roachpb.Key, nowNanos int64,
) (enginepb.MVCCStats, error) {
if i.spansOnly {
if err := i.spans.CheckAllowed(SpanReadOnly, roachpb.Span{Key: start, EndKey: end}); err != nil {
return enginepb.MVCCStats{}, err
}
} else {
if err := i.spans.CheckAllowedAt(SpanReadOnly, roachpb.Span{Key: start, EndKey: end}, i.ts); err != nil {
return enginepb.MVCCStats{}, err
}
}
return i.i.ComputeStats(start, end, nowNanos)
}
// FindSplitKey is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) FindSplitKey(
start, end, minSplitKey roachpb.Key, targetSize int64,
) (storage.MVCCKey, error) {
if i.spansOnly {
if err := i.spans.CheckAllowed(SpanReadOnly, roachpb.Span{Key: start, EndKey: end}); err != nil {
return storage.MVCCKey{}, err
}
} else {
if err := i.spans.CheckAllowedAt(SpanReadOnly, roachpb.Span{Key: start, EndKey: end}, i.ts); err != nil {
return storage.MVCCKey{}, err
}
}
return i.i.FindSplitKey(start, end, minSplitKey, targetSize)
}
// CheckForKeyCollisions is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) CheckForKeyCollisions(
sstData []byte, start, end roachpb.Key,
) (enginepb.MVCCStats, error) {
return i.i.CheckForKeyCollisions(sstData, start, end)
}
// SetUpperBound is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) SetUpperBound(key roachpb.Key) {
i.i.SetUpperBound(key)
}
// Stats is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) Stats() storage.IteratorStats {
return i.i.Stats()
}
// SupportsPrev is part of the storage.MVCCIterator interface.
func (i *MVCCIterator) SupportsPrev() bool {
return i.i.SupportsPrev()
}
// EngineIterator wraps a storage.EngineIterator and ensures that it can
// only be used to access spans in a SpanSet.
type EngineIterator struct {
i storage.EngineIterator
spans *SpanSet
}
// Close is part of the storage.EngineIterator interface.
func (i *EngineIterator) Close() {
i.i.Close()
}
// SeekEngineKeyGE is part of the storage.EngineIterator interface.
func (i *EngineIterator) SeekEngineKeyGE(key storage.EngineKey) (valid bool, err error) {
valid, err = i.i.SeekEngineKeyGE(key)
if !valid {
return valid, err
}
if err = i.spans.CheckAllowed(SpanReadOnly, roachpb.Span{Key: key.Key}); err != nil {
return false, err
}
return valid, err
}
// SeekEngineKeyLT is part of the storage.EngineIterator interface.
func (i *EngineIterator) SeekEngineKeyLT(key storage.EngineKey) (valid bool, err error) {
valid, err = i.i.SeekEngineKeyLT(key)
if !valid {
return valid, err
}
if err = i.spans.CheckAllowed(SpanReadOnly, roachpb.Span{EndKey: key.Key}); err != nil {
return false, err
}
return valid, err
}
// NextEngineKey is part of the storage.EngineIterator interface.
func (i *EngineIterator) NextEngineKey() (valid bool, err error) {
valid, err = i.i.NextEngineKey()
if !valid {
return valid, err
}
return i.checkKeyAllowed()
}
// PrevEngineKey is part of the storage.EngineIterator interface.
func (i *EngineIterator) PrevEngineKey() (valid bool, err error) {
valid, err = i.i.PrevEngineKey()
if !valid {
return valid, err
}
return i.checkKeyAllowed()
}
// SeekEngineKeyGEWithLimit is part of the storage.EngineIterator interface.
func (i *EngineIterator) SeekEngineKeyGEWithLimit(
key storage.EngineKey, limit roachpb.Key,
) (state pebble.IterValidityState, err error) {
state, err = i.i.SeekEngineKeyGEWithLimit(key, limit)
if state != pebble.IterValid {
return state, err
}
if err = i.spans.CheckAllowed(SpanReadOnly, roachpb.Span{Key: key.Key}); err != nil {
return pebble.IterExhausted, err
}
return state, err
}
// SeekEngineKeyLTWithLimit is part of the storage.EngineIterator interface.
func (i *EngineIterator) SeekEngineKeyLTWithLimit(
key storage.EngineKey, limit roachpb.Key,
) (state pebble.IterValidityState, err error) {
state, err = i.i.SeekEngineKeyLTWithLimit(key, limit)
if state != pebble.IterValid {
return state, err
}
if err = i.spans.CheckAllowed(SpanReadOnly, roachpb.Span{EndKey: key.Key}); err != nil {
return pebble.IterExhausted, err
}
return state, err
}
// NextEngineKeyWithLimit is part of the storage.EngineIterator interface.
func (i *EngineIterator) NextEngineKeyWithLimit(
limit roachpb.Key,
) (state pebble.IterValidityState, err error) {
return i.i.NextEngineKeyWithLimit(limit)
}
// PrevEngineKeyWithLimit is part of the storage.EngineIterator interface.
func (i *EngineIterator) PrevEngineKeyWithLimit(
limit roachpb.Key,
) (state pebble.IterValidityState, err error) {
return i.i.PrevEngineKeyWithLimit(limit)
}
func (i *EngineIterator) checkKeyAllowed() (valid bool, err error) {
key, err := i.i.UnsafeEngineKey()
if err != nil {
return false, err
}
if err = i.spans.CheckAllowed(SpanReadOnly, roachpb.Span{Key: key.Key}); err != nil {
// Invalid, but no error.
return false, nil // nolint:returnerrcheck
}
return true, nil
}
// UnsafeEngineKey is part of the storage.EngineIterator interface.
func (i *EngineIterator) UnsafeEngineKey() (storage.EngineKey, error) {
return i.i.UnsafeEngineKey()
}
// UnsafeValue is part of the storage.EngineIterator interface.
func (i *EngineIterator) UnsafeValue() []byte {
return i.i.UnsafeValue()
}
// EngineKey is part of the storage.EngineIterator interface.
func (i *EngineIterator) EngineKey() (storage.EngineKey, error) {
return i.i.EngineKey()
}
// Value is part of the storage.EngineIterator interface.
func (i *EngineIterator) Value() []byte {
return i.i.Value()
}
// UnsafeRawEngineKey is part of the storage.EngineIterator interface.
func (i *EngineIterator) UnsafeRawEngineKey() []byte {
return i.i.UnsafeRawEngineKey()
}
// SetUpperBound is part of the storage.EngineIterator interface.
func (i *EngineIterator) SetUpperBound(key roachpb.Key) {
i.i.SetUpperBound(key)
}
// GetRawIter is part of the storage.EngineIterator interface.
func (i *EngineIterator) GetRawIter() *pebble.Iterator {
return i.i.GetRawIter()
}
// Stats is part of the storage.EngineIterator interface.
func (i *EngineIterator) Stats() storage.IteratorStats {
return i.i.Stats()
}
type spanSetReader struct {
r storage.Reader
spans *SpanSet
spansOnly bool
ts hlc.Timestamp
}
var _ storage.Reader = spanSetReader{}
func (s spanSetReader) Close() {
s.r.Close()
}
func (s spanSetReader) Closed() bool {
return s.r.Closed()
}
// ExportMVCCToSst is part of the engine.Reader interface.
func (s spanSetReader) ExportMVCCToSst(
startKey, endKey roachpb.Key,
startTS, endTS hlc.Timestamp,
exportAllRevisions bool,
targetSize, maxSize uint64,
useTBI bool,
dest io.Writer,
) (roachpb.BulkOpSummary, roachpb.Key, error) {
return s.r.ExportMVCCToSst(startKey, endKey, startTS, endTS, exportAllRevisions, targetSize,
maxSize, useTBI, dest)
}
func (s spanSetReader) MVCCGet(key storage.MVCCKey) ([]byte, error) {
if s.spansOnly {
if err := s.spans.CheckAllowed(SpanReadOnly, roachpb.Span{Key: key.Key}); err != nil {
return nil, err
}
} else {
if err := s.spans.CheckAllowedAt(SpanReadOnly, roachpb.Span{Key: key.Key}, s.ts); err != nil {
return nil, err
}
}
//lint:ignore SA1019 implementing deprecated interface function (Get) is OK
return s.r.MVCCGet(key)
}
func (s spanSetReader) MVCCGetProto(
key storage.MVCCKey, msg protoutil.Message,
) (bool, int64, int64, error) {
if s.spansOnly {
if err := s.spans.CheckAllowed(SpanReadOnly, roachpb.Span{Key: key.Key}); err != nil {
return false, 0, 0, err
}
} else {
if err := s.spans.CheckAllowedAt(SpanReadOnly, roachpb.Span{Key: key.Key}, s.ts); err != nil {
return false, 0, 0, err
}
}
//lint:ignore SA1019 implementing deprecated interface function (MVCCGetProto) is OK
return s.r.MVCCGetProto(key, msg)
}
func (s spanSetReader) MVCCIterate(
start, end roachpb.Key, iterKind storage.MVCCIterKind, f func(storage.MVCCKeyValue) error,
) error {
if s.spansOnly {
if err := s.spans.CheckAllowed(SpanReadOnly, roachpb.Span{Key: start, EndKey: end}); err != nil {
return err
}
} else {
if err := s.spans.CheckAllowedAt(SpanReadOnly, roachpb.Span{Key: start, EndKey: end}, s.ts); err != nil {
return err
}
}
return s.r.MVCCIterate(start, end, iterKind, f)
}
func (s spanSetReader) NewMVCCIterator(
iterKind storage.MVCCIterKind, opts storage.IterOptions,
) storage.MVCCIterator {
if s.spansOnly {
return NewIterator(s.r.NewMVCCIterator(iterKind, opts), s.spans)
}
return NewIteratorAt(s.r.NewMVCCIterator(iterKind, opts), s.spans, s.ts)
}
func (s spanSetReader) NewEngineIterator(opts storage.IterOptions) storage.EngineIterator {
if !s.spansOnly {
panic("cannot do timestamp checking for EngineIterator")
}
return &EngineIterator{
i: s.r.NewEngineIterator(opts),
spans: s.spans,
}
}
func (s spanSetReader) ConsistentIterators() bool {
return s.r.ConsistentIterators()
}
func (s spanSetReader) PinEngineStateForIterators() error {
return s.r.PinEngineStateForIterators()
}
// GetDBEngine recursively searches for the underlying rocksDB engine.
func GetDBEngine(reader storage.Reader, span roachpb.Span) storage.Reader {
switch v := reader.(type) {
case ReadWriter:
return GetDBEngine(getSpanReader(v, span), span)
case *spanSetBatch:
return GetDBEngine(getSpanReader(v.ReadWriter, span), span)
default:
return reader
}
}
// getSpanReader is a getter to access the engine.Reader field of the
// spansetReader.
func getSpanReader(r ReadWriter, span roachpb.Span) storage.Reader {
if err := r.spanSetReader.spans.CheckAllowed(SpanReadOnly, span); err != nil {
panic("Not in the span")
}
return r.spanSetReader.r
}
type spanSetWriter struct {
w storage.Writer
spans *SpanSet
spansOnly bool
ts hlc.Timestamp
}
var _ storage.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) checkAllowed(key roachpb.Key) error {
if s.spansOnly {
if err := s.spans.CheckAllowed(SpanReadWrite, roachpb.Span{Key: key}); err != nil {
return err
}
} else {
if err := s.spans.CheckAllowedAt(SpanReadWrite, roachpb.Span{Key: key}, s.ts); err != nil {
return err
}
}
return nil
}
func (s spanSetWriter) ClearMVCC(key storage.MVCCKey) error {
if err := s.checkAllowed(key.Key); err != nil {
return err
}
return s.w.ClearMVCC(key)
}
func (s spanSetWriter) ClearUnversioned(key roachpb.Key) error {
if err := s.checkAllowed(key); err != nil {
return err
}
return s.w.ClearUnversioned(key)
}
func (s spanSetWriter) ClearIntent(
key roachpb.Key, state storage.PrecedingIntentState, txnDidNotUpdateMeta bool, txnUUID uuid.UUID,
) (int, error) {
if err := s.checkAllowed(key); err != nil {
return 0, err
}
return s.w.ClearIntent(key, state, txnDidNotUpdateMeta, txnUUID)
}
func (s spanSetWriter) ClearEngineKey(key storage.EngineKey) error {
if !s.spansOnly {
panic("cannot do timestamp checking for clearing EngineKey")
}
if err := s.spans.CheckAllowed(SpanReadWrite, roachpb.Span{Key: key.Key}); err != nil {
return err
}
return s.w.ClearEngineKey(key)
}
func (s spanSetWriter) SingleClearEngineKey(key storage.EngineKey) error {
// Pass-through, since single clear is only used for the lock table, which
// is not in the spans.
return s.w.SingleClearEngineKey(key)
}
func (s spanSetWriter) checkAllowedRange(start, end roachpb.Key) error {
if s.spansOnly {
if err := s.spans.CheckAllowed(SpanReadWrite, roachpb.Span{Key: start, EndKey: end}); err != nil {
return err
}
} else {
if err := s.spans.CheckAllowedAt(SpanReadWrite, roachpb.Span{Key: start, EndKey: end}, s.ts); err != nil {
return err
}
}
return nil
}
func (s spanSetWriter) ClearRawRange(start, end roachpb.Key) error {
if err := s.checkAllowedRange(start, end); err != nil {
return err
}
return s.w.ClearRawRange(start, end)
}
func (s spanSetWriter) ClearMVCCRangeAndIntents(start, end roachpb.Key) error {
if err := s.checkAllowedRange(start, end); err != nil {
return err
}
return s.w.ClearMVCCRangeAndIntents(start, end)
}
func (s spanSetWriter) ClearMVCCRange(start, end storage.MVCCKey) error {
if err := s.checkAllowedRange(start.Key, end.Key); err != nil {
return err
}
return s.w.ClearMVCCRange(start, end)
}
func (s spanSetWriter) ClearIterRange(iter storage.MVCCIterator, start, end roachpb.Key) error {
if err := s.checkAllowedRange(start, end); err != nil {
return err
}
return s.w.ClearIterRange(iter, start, end)
}
func (s spanSetWriter) Merge(key storage.MVCCKey, value []byte) error {
if s.spansOnly {
if err := s.spans.CheckAllowed(SpanReadWrite, roachpb.Span{Key: key.Key}); err != nil {
return err
}
} else {
if err := s.spans.CheckAllowedAt(SpanReadWrite, roachpb.Span{Key: key.Key}, s.ts); err != nil {
return err
}
}
return s.w.Merge(key, value)
}
func (s spanSetWriter) PutMVCC(key storage.MVCCKey, value []byte) error {
if err := s.checkAllowed(key.Key); err != nil {
return err
}
return s.w.PutMVCC(key, value)
}
func (s spanSetWriter) PutUnversioned(key roachpb.Key, value []byte) error {
if err := s.checkAllowed(key); err != nil {
return err
}
return s.w.PutUnversioned(key, value)
}
func (s spanSetWriter) PutIntent(
ctx context.Context,
key roachpb.Key,
value []byte,
state storage.PrecedingIntentState,
txnDidNotUpdateMeta bool,
txnUUID uuid.UUID,
) (int, error) {
if err := s.checkAllowed(key); err != nil {
return 0, err
}
return s.w.PutIntent(ctx, key, value, state, txnDidNotUpdateMeta, txnUUID)
}
func (s spanSetWriter) PutEngineKey(key storage.EngineKey, value []byte) error {
if !s.spansOnly {
panic("cannot do timestamp checking for putting EngineKey")
}
if err := s.spans.CheckAllowed(SpanReadWrite, roachpb.Span{Key: key.Key}); err != nil {
return err
}
return s.w.PutEngineKey(key, value)
}
func (s spanSetWriter) SafeToWriteSeparatedIntents(ctx context.Context) (bool, error) {
return s.w.SafeToWriteSeparatedIntents(ctx)
}
func (s spanSetWriter) LogData(data []byte) error {
return s.w.LogData(data)
}
func (s spanSetWriter) LogLogicalOp(
op storage.MVCCLogicalOpType, details storage.MVCCLogicalOpDetails,
) {
s.w.LogLogicalOp(op, details)
}
// ReadWriter is used outside of the spanset package internally, in ccl.
type ReadWriter struct {
spanSetReader
spanSetWriter
}
var _ storage.ReadWriter = ReadWriter{}
func makeSpanSetReadWriter(rw storage.ReadWriter, spans *SpanSet) ReadWriter {
return ReadWriter{
spanSetReader: spanSetReader{r: rw, spans: spans, spansOnly: true},
spanSetWriter: spanSetWriter{w: rw, spans: spans, spansOnly: true},
}
}
func makeSpanSetReadWriterAt(rw storage.ReadWriter, spans *SpanSet, ts hlc.Timestamp) ReadWriter {
return ReadWriter{
spanSetReader: spanSetReader{r: rw, spans: spans, ts: ts},
spanSetWriter: spanSetWriter{w: rw, spans: spans, ts: ts},
}
}
// NewReadWriterAt returns an engine.ReadWriter that asserts access of the
// underlying ReadWriter against the given SpanSet at a given timestamp.
// If zero timestamp is provided, accesses are considered non-MVCC.
func NewReadWriterAt(rw storage.ReadWriter, spans *SpanSet, ts hlc.Timestamp) storage.ReadWriter {
return makeSpanSetReadWriterAt(rw, spans, ts)
}
type spanSetBatch struct {
ReadWriter
b storage.Batch
spans *SpanSet
spansOnly bool
ts hlc.Timestamp
}
var _ storage.Batch = spanSetBatch{}
func (s spanSetBatch) Commit(sync bool) error {
return s.b.Commit(sync)
}
func (s spanSetBatch) Empty() bool {
return s.b.Empty()
}
func (s spanSetBatch) Len() int {
return s.b.Len()
}
func (s spanSetBatch) Repr() []byte {
return s.b.Repr()
}
// NewBatch returns an engine.Batch that asserts access of the underlying
// Batch against the given SpanSet. We only consider span boundaries, associated
// timestamps are not considered.
func NewBatch(b storage.Batch, spans *SpanSet) storage.Batch {
return &spanSetBatch{
ReadWriter: makeSpanSetReadWriter(b, spans),
b: b,
spans: spans,
spansOnly: true,
}
}
// NewBatchAt returns an engine.Batch that asserts access of the underlying
// Batch against the given SpanSet at the given timestamp.
// If the zero timestamp is used, all accesses are considered non-MVCC.
func NewBatchAt(b storage.Batch, spans *SpanSet, ts hlc.Timestamp) storage.Batch {
return &spanSetBatch{
ReadWriter: makeSpanSetReadWriterAt(b, spans, ts),
b: b,
spans: spans,
ts: ts,
}
}