-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
batch.go
761 lines (714 loc) · 22 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
// Copyright 2015 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 kv
import (
"context"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/errors"
)
const (
raw = true
notRaw = false
)
// Batch provides for the parallel execution of a number of database
// operations. Operations are added to the Batch and then the Batch is executed
// via either DB.Run, Txn.Run or Txn.Commit.
//
// TODO(pmattis): Allow a timestamp to be specified which is applied to all
// operations within the batch.
type Batch struct {
// The Txn the batch is associated with. This field may be nil if the batch
// was not created via Txn.NewBatch.
txn *Txn
// Results contains an entry for each operation added to the batch. The order
// of the results matches the order the operations were added to the
// batch. For example:
//
// b := db.NewBatch()
// b.Put("a", "1")
// b.Put("b", "2")
// _ = db.Run(b)
// // string(b.Results[0].Rows[0].Key) == "a"
// // string(b.Results[1].Rows[0].Key) == "b"
Results []Result
// The Header which will be used to send the resulting BatchRequest.
// To be modified directly.
Header roachpb.Header
reqs []roachpb.RequestUnion
// Set when AddRawRequest is used, in which case using the "other"
// operations renders the batch unusable.
raw bool
// Once received, the response from a successful batch.
response *roachpb.BatchResponse
// Once received, any error encountered sending the batch.
pErr *roachpb.Error
// We use pre-allocated buffers to avoid dynamic allocations for small batches.
resultsBuf [8]Result
rowsBuf []KeyValue
rowsStaticBuf [8]KeyValue
rowsStaticIdx int
}
// RawResponse returns the BatchResponse which was the result of a successful
// execution of the batch, and nil otherwise.
func (b *Batch) RawResponse() *roachpb.BatchResponse {
return b.response
}
// MustPErr returns the structured error resulting from a failed execution of
// the batch, asserting that that error is non-nil.
func (b *Batch) MustPErr() *roachpb.Error {
if b.pErr == nil {
panic(errors.Errorf("expected non-nil pErr for batch %+v", b))
}
return b.pErr
}
func (b *Batch) prepare() error {
for _, r := range b.Results {
if r.Err != nil {
return r.Err
}
}
return nil
}
func (b *Batch) initResult(calls, numRows int, raw bool, err error) {
if err == nil && b.raw && !raw {
err = errors.Errorf("must not use non-raw operations on a raw batch")
}
// TODO(tschottdorf): assert that calls is 0 or 1?
r := Result{calls: calls, Err: err}
if numRows > 0 && !b.raw {
if b.rowsStaticIdx+numRows <= len(b.rowsStaticBuf) {
r.Rows = b.rowsStaticBuf[b.rowsStaticIdx : b.rowsStaticIdx+numRows]
b.rowsStaticIdx += numRows
} else {
// Most requests produce 0 (unknown) or 1 result rows, so optimize for
// that case.
switch numRows {
case 1:
// Use a buffer to batch allocate the result rows.
if cap(b.rowsBuf)-len(b.rowsBuf) == 0 {
const minSize = 16
const maxSize = 128
size := cap(b.rowsBuf) * 2
if size < minSize {
size = minSize
} else if size > maxSize {
size = maxSize
}
b.rowsBuf = make([]KeyValue, 0, size)
}
pos := len(b.rowsBuf)
r.Rows = b.rowsBuf[pos : pos+1 : pos+1]
b.rowsBuf = b.rowsBuf[:pos+1]
default:
r.Rows = make([]KeyValue, numRows)
}
}
}
if b.Results == nil {
b.Results = b.resultsBuf[:0]
}
b.Results = append(b.Results, r)
}
// fillResults walks through the results and updates them either with the
// data or error which was the result of running the batch previously.
func (b *Batch) fillResults(ctx context.Context) {
// No-op if Batch is raw.
if b.raw {
return
}
offset := 0
for i := range b.Results {
result := &b.Results[i]
for k := 0; k < result.calls; k++ {
args := b.reqs[offset+k].GetInner()
var reply roachpb.Response
// It's possible that result.Err was populated early, for example
// when PutProto is called and the proto marshaling errored out.
// In that case, we don't want to mutate this result's error
// further.
if result.Err == nil {
// The outcome of each result is that of the batch as a whole.
result.Err = b.pErr.GoError()
if result.Err == nil {
// For a successful request, load the reply to populate in
// this pass.
if b.response != nil && offset+k < len(b.response.Responses) {
reply = b.response.Responses[offset+k].GetInner()
} else if args.Method() != roachpb.EndTxn {
// TODO(tschottdorf): EndTxn is special-cased here
// because it may be elided (r/o txns). Might prefer to
// simulate an EndTxn response instead; this effectively
// just leaks here. TODO(tschottdorf): returning an
// error here seems to get swallowed.
panic(errors.Errorf("not enough responses for calls: (%T) %+v\nresponses: %+v",
args, args, b.response))
}
}
}
switch req := args.(type) {
case *roachpb.GetRequest:
row := &result.Rows[k]
row.Key = []byte(req.Key)
if result.Err == nil {
row.Value = reply.(*roachpb.GetResponse).Value
}
case *roachpb.PutRequest:
row := &result.Rows[k]
row.Key = []byte(req.Key)
if result.Err == nil {
row.Value = &req.Value
}
case *roachpb.ConditionalPutRequest:
row := &result.Rows[k]
row.Key = []byte(req.Key)
if result.Err == nil {
row.Value = &req.Value
}
case *roachpb.InitPutRequest:
row := &result.Rows[k]
row.Key = []byte(req.Key)
if result.Err == nil {
row.Value = &req.Value
}
case *roachpb.IncrementRequest:
row := &result.Rows[k]
row.Key = []byte(req.Key)
if result.Err == nil {
t := reply.(*roachpb.IncrementResponse)
row.Value = &roachpb.Value{}
row.Value.SetInt(t.NewValue)
}
case *roachpb.ScanRequest:
if result.Err == nil {
t := reply.(*roachpb.ScanResponse)
result.Rows = make([]KeyValue, len(t.Rows))
for j := range t.Rows {
src := &t.Rows[j]
dst := &result.Rows[j]
dst.Key = src.Key
dst.Value = &src.Value
}
}
case *roachpb.ReverseScanRequest:
if result.Err == nil {
t := reply.(*roachpb.ReverseScanResponse)
result.Rows = make([]KeyValue, len(t.Rows))
for j := range t.Rows {
src := &t.Rows[j]
dst := &result.Rows[j]
dst.Key = src.Key
dst.Value = &src.Value
}
}
case *roachpb.DeleteRequest:
row := &result.Rows[k]
row.Key = []byte(args.(*roachpb.DeleteRequest).Key)
case *roachpb.DeleteRangeRequest:
if result.Err == nil {
result.Keys = reply.(*roachpb.DeleteRangeResponse).Keys
}
default:
if result.Err == nil {
result.Err = errors.Errorf("unsupported reply: %T for %T",
reply, args)
}
// Nothing to do for all methods below as they do not generate
// any rows.
case *roachpb.EndTxnRequest:
case *roachpb.AdminMergeRequest:
case *roachpb.AdminSplitRequest:
case *roachpb.AdminUnsplitRequest:
case *roachpb.AdminTransferLeaseRequest:
case *roachpb.AdminChangeReplicasRequest:
case *roachpb.AdminRelocateRangeRequest:
case *roachpb.HeartbeatTxnRequest:
case *roachpb.GCRequest:
case *roachpb.LeaseInfoRequest:
case *roachpb.PushTxnRequest:
case *roachpb.QueryTxnRequest:
case *roachpb.QueryIntentRequest:
case *roachpb.ResolveIntentRequest:
case *roachpb.ResolveIntentRangeRequest:
case *roachpb.MergeRequest:
case *roachpb.TruncateLogRequest:
case *roachpb.RequestLeaseRequest:
case *roachpb.CheckConsistencyRequest:
case *roachpb.WriteBatchRequest:
case *roachpb.ImportRequest:
case *roachpb.AdminScatterRequest:
case *roachpb.AddSSTableRequest:
}
// Fill up the resume span.
if result.Err == nil && reply != nil && reply.Header().ResumeSpan != nil {
result.ResumeSpan = reply.Header().ResumeSpan
result.ResumeReason = reply.Header().ResumeReason
// The ResumeReason might be missing when talking to a 1.1 node; assume
// it's the key limit (which was the only reason why 1.1 would return a
// resume span). This can be removed in 2.1.
if result.ResumeReason == roachpb.RESUME_UNKNOWN {
result.ResumeReason = roachpb.RESUME_KEY_LIMIT
}
}
}
offset += result.calls
}
}
// resultErr walks through the result slice and returns the first error found,
// if one exists.
func (b *Batch) resultErr() error {
for i := range b.Results {
if err := b.Results[i].Err; err != nil {
return err
}
}
return nil
}
func (b *Batch) growReqs(n int) {
if len(b.reqs)+n > cap(b.reqs) {
newSize := 2 * cap(b.reqs)
if newSize == 0 {
newSize = 8
}
for newSize < len(b.reqs)+n {
newSize *= 2
}
newReqs := make([]roachpb.RequestUnion, len(b.reqs), newSize)
copy(newReqs, b.reqs)
b.reqs = newReqs
}
b.reqs = b.reqs[:len(b.reqs)+n]
}
func (b *Batch) appendReqs(args ...roachpb.Request) {
n := len(b.reqs)
b.growReqs(len(args))
for i := range args {
b.reqs[n+i].MustSetInner(args[i])
}
}
// AddRawRequest adds the specified requests to the batch. No responses will
// be allocated for them, and using any of the non-raw operations will result
// in an error when running the batch.
func (b *Batch) AddRawRequest(reqs ...roachpb.Request) {
b.raw = true
for _, args := range reqs {
numRows := 0
switch args.(type) {
case *roachpb.GetRequest,
*roachpb.PutRequest,
*roachpb.ConditionalPutRequest,
*roachpb.IncrementRequest,
*roachpb.DeleteRequest:
numRows = 1
}
b.appendReqs(args)
b.initResult(1 /* calls */, numRows, raw, nil)
}
}
// Get retrieves the value for a key. A new result will be appended to the
// batch which will contain a single row.
//
// r, err := db.Get("a")
// // string(r.Rows[0].Key) == "a"
//
// key can be either a byte slice or a string.
func (b *Batch) Get(key interface{}) {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, 1, notRaw, err)
return
}
b.appendReqs(roachpb.NewGet(k))
b.initResult(1, 1, notRaw, nil)
}
func (b *Batch) put(key, value interface{}, inline bool) {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, 1, notRaw, err)
return
}
v, err := marshalValue(value)
if err != nil {
b.initResult(0, 1, notRaw, err)
return
}
if inline {
b.appendReqs(roachpb.NewPutInline(k, v))
} else {
b.appendReqs(roachpb.NewPut(k, v))
}
b.initResult(1, 1, notRaw, nil)
}
// Put sets the value for a key.
//
// A new result will be appended to the batch which will contain a single row
// and Result.Err will indicate success or failure.
//
// key can be either a byte slice or a string. value can be any key type, a
// protoutil.Message or any Go primitive type (bool, int, etc).
func (b *Batch) Put(key, value interface{}) {
if value == nil {
// Empty values are used as deletion tombstones, so one can't write an empty
// value. If the intention was indeed to delete the key, use Del() instead.
panic("can't Put an empty Value; did you mean to Del() instead?")
}
b.put(key, value, false)
}
// PutInline sets the value for a key, but does not maintain
// multi-version values. The most recent value is always overwritten.
// Inline values cannot be mutated transactionally and should be used
// with caution.
//
// A new result will be appended to the batch which will contain a single row
// and Result.Err will indicate success or failure.
//
// key can be either a byte slice or a string. value can be any key type, a
// protoutil.Message or any Go primitive type (bool, int, etc).
//
// A nil value can be used to delete the respective key, since there is no
// DelInline(). This is different from Put().
func (b *Batch) PutInline(key, value interface{}) {
b.put(key, value, true)
}
// CPut conditionally sets the value for a key if the existing value is equal to
// expValue. To conditionally set a value only if the key doesn't currently
// exist, pass an empty expValue.
//
// A new result will be appended to the batch which will contain a single row
// and Result.Err will indicate success or failure.
//
// key can be either a byte slice or a string.
//
// value can be any key type, a protoutil.Message or any Go primitive type
// (bool, int, etc). A nil value means delete the key.
//
// An empty expValue means that the key is expected to not exist. If not empty,
// expValue needs to correspond to a Value.TagAndDataBytes() - i.e. a key's
// value without the checksum (as the checksum includes the key too).
func (b *Batch) CPut(key, value interface{}, expValue []byte) {
b.cputInternal(key, value, expValue, false)
}
// CPutAllowingIfNotExists is like CPut except it also allows the Put when the
// existing entry does not exist -- i.e. it succeeds if there is no existing
// entry or the existing entry has the expected value.
func (b *Batch) CPutAllowingIfNotExists(key, value interface{}, expValue []byte) {
b.cputInternal(key, value, expValue, true)
}
func (b *Batch) cputInternal(key, value interface{}, expValue []byte, allowNotExist bool) {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, 1, notRaw, err)
return
}
v, err := marshalValue(value)
if err != nil {
b.initResult(0, 1, notRaw, err)
return
}
b.appendReqs(roachpb.NewConditionalPut(k, v, expValue, allowNotExist))
b.initResult(1, 1, notRaw, nil)
}
// InitPut sets the first value for a key to value. An ConditionFailedError is
// reported if a value already exists for the key and it's not equal to the
// value passed in. If failOnTombstones is set to true, tombstones will return
// a ConditionFailedError just like a mismatched value.
//
// key can be either a byte slice or a string. value can be any key type, a
// protoutil.Message or any Go primitive type (bool, int, etc). It is illegal
// to set value to nil.
func (b *Batch) InitPut(key, value interface{}, failOnTombstones bool) {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, 1, notRaw, err)
return
}
v, err := marshalValue(value)
if err != nil {
b.initResult(0, 1, notRaw, err)
return
}
b.appendReqs(roachpb.NewInitPut(k, v, failOnTombstones))
b.initResult(1, 1, notRaw, nil)
}
// Inc increments the integer value at key. If the key does not exist it will
// be created with an initial value of 0 which will then be incremented. If the
// key exists but was set using Put or CPut an error will be returned.
//
// A new result will be appended to the batch which will contain a single row
// and Result.Err will indicate success or failure.
//
// key can be either a byte slice or a string.
func (b *Batch) Inc(key interface{}, value int64) {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, 1, notRaw, err)
return
}
b.appendReqs(roachpb.NewIncrement(k, value))
b.initResult(1, 1, notRaw, nil)
}
func (b *Batch) scan(s, e interface{}, isReverse, forUpdate bool) {
begin, err := marshalKey(s)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
end, err := marshalKey(e)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
if !isReverse {
b.appendReqs(roachpb.NewScan(begin, end, forUpdate))
} else {
b.appendReqs(roachpb.NewReverseScan(begin, end, forUpdate))
}
b.initResult(1, 0, notRaw, nil)
}
// Scan retrieves the key/values between begin (inclusive) and end (exclusive) in
// ascending order.
//
// A new result will be appended to the batch which will contain "rows" (each
// row is a key/value pair) and Result.Err will indicate success or failure.
//
// key can be either a byte slice or a string.
func (b *Batch) Scan(s, e interface{}) {
b.scan(s, e, false /* isReverse */, false /* forUpdate */)
}
// ScanForUpdate retrieves the key/values between begin (inclusive) and end
// (exclusive) in ascending order. Unreplicated, exclusive locks are acquired on
// each of the returned keys.
//
// A new result will be appended to the batch which will contain "rows" (each
// row is a key/value pair) and Result.Err will indicate success or failure.
//
// key can be either a byte slice or a string.
func (b *Batch) ScanForUpdate(s, e interface{}) {
b.scan(s, e, false /* isReverse */, true /* forUpdate */)
}
// ReverseScan retrieves the rows between begin (inclusive) and end (exclusive)
// in descending order.
//
// A new result will be appended to the batch which will contain "rows" (each
// "row" is a key/value pair) and Result.Err will indicate success or failure.
//
// key can be either a byte slice or a string.
func (b *Batch) ReverseScan(s, e interface{}) {
b.scan(s, e, true /* isReverse */, false /* forUpdate */)
}
// ReverseScanForUpdate retrieves the rows between begin (inclusive) and end
// (exclusive) in descending order. Unreplicated, exclusive locks are acquired
// on each of the returned keys.
//
// A new result will be appended to the batch which will contain "rows" (each
// "row" is a key/value pair) and Result.Err will indicate success or failure.
//
// key can be either a byte slice or a string.
func (b *Batch) ReverseScanForUpdate(s, e interface{}) {
b.scan(s, e, true /* isReverse */, true /* forUpdate */)
}
// Del deletes one or more keys.
//
// A new result will be appended to the batch and each key will have a
// corresponding row in the returned Result.
//
// key can be either a byte slice or a string.
func (b *Batch) Del(keys ...interface{}) {
reqs := make([]roachpb.Request, 0, len(keys))
for _, key := range keys {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, len(keys), notRaw, err)
return
}
reqs = append(reqs, roachpb.NewDelete(k))
}
b.appendReqs(reqs...)
b.initResult(len(reqs), len(reqs), notRaw, nil)
}
// DelRange deletes the rows between begin (inclusive) and end (exclusive).
//
// A new result will be appended to the batch which will contain 0 rows and
// Result.Err will indicate success or failure.
//
// key can be either a byte slice or a string.
func (b *Batch) DelRange(s, e interface{}, returnKeys bool) {
begin, err := marshalKey(s)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
end, err := marshalKey(e)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
b.appendReqs(roachpb.NewDeleteRange(begin, end, returnKeys))
b.initResult(1, 0, notRaw, nil)
}
// adminMerge is only exported on DB. It is here for symmetry with the
// other operations.
func (b *Batch) adminMerge(key interface{}) {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
req := &roachpb.AdminMergeRequest{
RequestHeader: roachpb.RequestHeader{
Key: k,
},
}
b.appendReqs(req)
b.initResult(1, 0, notRaw, nil)
}
// adminSplit is only exported on DB. It is here for symmetry with the
// other operations.
func (b *Batch) adminSplit(splitKeyIn interface{}, expirationTime hlc.Timestamp) {
splitKey, err := marshalKey(splitKeyIn)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
req := &roachpb.AdminSplitRequest{
RequestHeader: roachpb.RequestHeader{
Key: splitKey,
},
SplitKey: splitKey,
ExpirationTime: expirationTime,
}
b.appendReqs(req)
b.initResult(1, 0, notRaw, nil)
}
func (b *Batch) adminUnsplit(splitKeyIn interface{}) {
splitKey, err := marshalKey(splitKeyIn)
if err != nil {
b.initResult(0, 0, notRaw, err)
}
req := &roachpb.AdminUnsplitRequest{
RequestHeader: roachpb.RequestHeader{
Key: splitKey,
},
}
b.appendReqs(req)
b.initResult(1, 0, notRaw, nil)
}
// adminTransferLease is only exported on DB. It is here for symmetry with the
// other operations.
func (b *Batch) adminTransferLease(key interface{}, target roachpb.StoreID) {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
req := &roachpb.AdminTransferLeaseRequest{
RequestHeader: roachpb.RequestHeader{
Key: k,
},
Target: target,
}
b.appendReqs(req)
b.initResult(1, 0, notRaw, nil)
}
// adminChangeReplicas is only exported on DB. It is here for symmetry with the
// other operations.
func (b *Batch) adminChangeReplicas(
key interface{}, expDesc roachpb.RangeDescriptor, chgs []roachpb.ReplicationChange,
) {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
req := &roachpb.AdminChangeReplicasRequest{
RequestHeader: roachpb.RequestHeader{
Key: k,
},
ExpDesc: expDesc,
}
req.AddChanges(chgs...)
b.appendReqs(req)
b.initResult(1, 0, notRaw, nil)
}
// adminRelocateRange is only exported on DB. It is here for symmetry with the
// other operations.
func (b *Batch) adminRelocateRange(
key interface{},
voterTargets []roachpb.ReplicationTarget,
nonVoterTargets []roachpb.ReplicationTarget,
) {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
req := &roachpb.AdminRelocateRangeRequest{
RequestHeader: roachpb.RequestHeader{
Key: k,
},
VoterTargets: voterTargets,
NonVoterTargets: nonVoterTargets,
}
b.appendReqs(req)
b.initResult(1, 0, notRaw, nil)
}
// writeBatch is only exported on DB.
func (b *Batch) writeBatch(s, e interface{}, data []byte) {
begin, err := marshalKey(s)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
end, err := marshalKey(e)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
span := roachpb.Span{Key: begin, EndKey: end}
req := &roachpb.WriteBatchRequest{
RequestHeader: roachpb.RequestHeaderFromSpan(span),
DataSpan: span,
Data: data,
}
b.appendReqs(req)
b.initResult(1, 0, notRaw, nil)
}
// addSSTable is only exported on DB.
func (b *Batch) addSSTable(
s, e interface{},
data []byte,
disallowShadowing bool,
stats *enginepb.MVCCStats,
ingestAsWrites bool,
) {
begin, err := marshalKey(s)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
end, err := marshalKey(e)
if err != nil {
b.initResult(0, 0, notRaw, err)
return
}
req := &roachpb.AddSSTableRequest{
RequestHeader: roachpb.RequestHeader{
Key: begin,
EndKey: end,
},
Data: data,
DisallowShadowing: disallowShadowing,
MVCCStats: stats,
IngestAsWrites: ingestAsWrites,
}
b.appendReqs(req)
b.initResult(1, 0, notRaw, nil)
}