forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
467 lines (438 loc) · 13.4 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
// Copyright 2015 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.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package client
import (
"github.com/cockroachdb/cockroach/roachpb"
)
// 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 DB the batch is associated with. This field may be nil if the batch
// was not created via DB.NewBatch or Txn.NewBatch.
DB *DB
// 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
reqs []roachpb.Request
// If nonzero, limits the total amount of key/values returned by all Scan/ReverseScan operations
// in the batch. This can only be used if all requests are of the same type, and that type is
// Scan or ReverseScan.
MaxScanResults int64
// ReadConsistency specifies the consistency for read operations. The default
// is CONSISTENT. This value is ignored for write operations.
ReadConsistency roachpb.ReadConsistencyType
// We use pre-allocated buffers to avoid dynamic allocations for small batches.
resultsBuf [8]Result
rowsBuf [8]KeyValue
rowsIdx int
}
func (b *Batch) prepare() *roachpb.Error {
for _, r := range b.Results {
if pErr := r.PErr; pErr != nil {
return pErr
}
}
return nil
}
func (b *Batch) initResult(calls, numRows int, err error) {
// TODO(tschottdorf): assert that calls is 0 or 1?
r := Result{calls: calls, PErr: roachpb.NewError(err)}
if numRows > 0 {
if b.rowsIdx+numRows <= len(b.rowsBuf) {
r.Rows = b.rowsBuf[b.rowsIdx : b.rowsIdx+numRows]
b.rowsIdx += numRows
} else {
r.Rows = make([]KeyValue, numRows)
}
}
if b.Results == nil {
b.Results = b.resultsBuf[:0]
}
b.Results = append(b.Results, r)
}
func (b *Batch) fillResults(br *roachpb.BatchResponse, pErr *roachpb.Error) *roachpb.Error {
offset := 0
for i := range b.Results {
result := &b.Results[i]
for k := 0; k < result.calls; k++ {
args := b.reqs[offset+k]
var reply roachpb.Response
if result.PErr == nil {
result.PErr = pErr
if result.PErr == nil {
if br != nil && offset+k < len(br.Responses) {
reply = br.Responses[offset+k].GetInner()
} else if args.Method() != roachpb.EndTransaction {
// TODO(tschottdorf): EndTransaction is excepted here
// because it may be elided (r/o txns). Might prefer to
// simulate an EndTransaction response instead; this
// effectively just leaks here.
panic("not enough responses for calls")
}
}
}
switch req := args.(type) {
case *roachpb.GetRequest:
row := &result.Rows[k]
row.Key = []byte(req.Key)
if result.PErr == nil {
row.Value = reply.(*roachpb.GetResponse).Value
}
case *roachpb.PutRequest:
row := &result.Rows[k]
row.Key = []byte(req.Key)
if result.PErr == nil {
row.Value = &req.Value
}
case *roachpb.ConditionalPutRequest:
row := &result.Rows[k]
row.Key = []byte(req.Key)
if result.PErr == nil {
row.Value = &req.Value
}
case *roachpb.IncrementRequest:
row := &result.Rows[k]
row.Key = []byte(req.Key)
if result.PErr == nil {
t := reply.(*roachpb.IncrementResponse)
row.Value = &roachpb.Value{}
row.Value.SetInt(t.NewValue)
}
case *roachpb.ScanRequest:
if result.PErr == 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.PErr == 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.PErr == nil {
result.Keys = reply.(*roachpb.DeleteRangeResponse).Keys
}
case *roachpb.BeginTransactionRequest:
case *roachpb.EndTransactionRequest:
case *roachpb.AdminMergeRequest:
case *roachpb.AdminSplitRequest:
case *roachpb.HeartbeatTxnRequest:
case *roachpb.GCRequest:
case *roachpb.PushTxnRequest:
case *roachpb.RangeLookupRequest:
case *roachpb.ResolveIntentRequest:
case *roachpb.ResolveIntentRangeRequest:
case *roachpb.MergeRequest:
case *roachpb.TruncateLogRequest:
case *roachpb.LeaderLeaseRequest:
case *roachpb.CheckConsistencyRequest:
// Nothing to do for these methods as they do not generate any
// rows.
default:
if result.PErr == nil {
result.PErr = roachpb.NewErrorf("unsupported reply: %T", reply)
}
}
}
offset += result.calls
}
for i := range b.Results {
result := &b.Results[i]
if result.PErr != nil {
return result.PErr
}
}
return nil
}
// InternalAddRequest adds the specified requests to the batch. It is intended
// for internal use only.
func (b *Batch) InternalAddRequest(reqs ...roachpb.Request) {
for _, args := range reqs {
numRows := 0
switch args.(type) {
case *roachpb.GetRequest,
*roachpb.PutRequest,
*roachpb.ConditionalPutRequest,
*roachpb.IncrementRequest,
*roachpb.DeleteRequest:
numRows = 1
}
b.reqs = append(b.reqs, args)
b.initResult(1 /* calls */, numRows, 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, err)
return
}
b.reqs = append(b.reqs, roachpb.NewGet(k))
b.initResult(1, 1, nil)
}
func (b *Batch) put(key, value interface{}, inline bool) {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, 1, err)
return
}
v, err := marshalValue(value)
if err != nil {
b.initResult(0, 1, err)
return
}
if inline {
b.reqs = append(b.reqs, roachpb.NewPutInline(k, v))
} else {
b.reqs = append(b.reqs, roachpb.NewPut(k, v))
}
b.initResult(1, 1, 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
// proto.Message or any Go primitive type (bool, int, etc).
func (b *Batch) Put(key, value interface{}) {
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
// proto.Message or any Go primitive type (bool, int, etc).
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 there is no existing entry
// pass nil for expValue. Note that this must be an interface{}(nil), not a
// typed nil value (e.g. []byte(nil)).
//
// 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
// proto.Message or any Go primitive type (bool, int, etc).
func (b *Batch) CPut(key, value, expValue interface{}) {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, 1, err)
return
}
v, err := marshalValue(value)
if err != nil {
b.initResult(0, 1, err)
return
}
ev, err := marshalValue(expValue)
if err != nil {
b.initResult(0, 1, err)
return
}
b.reqs = append(b.reqs, roachpb.NewConditionalPut(k, v, ev))
b.initResult(1, 1, 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, err)
return
}
b.reqs = append(b.reqs, roachpb.NewIncrement(k, value))
b.initResult(1, 1, nil)
}
func (b *Batch) scan(s, e interface{}, maxRows int64, isReverse bool) {
begin, err := marshalKey(s)
if err != nil {
b.initResult(0, 0, err)
return
}
end, err := marshalKey(e)
if err != nil {
b.initResult(0, 0, err)
return
}
if !isReverse {
b.reqs = append(b.reqs, roachpb.NewScan(roachpb.Key(begin), roachpb.Key(end), maxRows))
} else {
b.reqs = append(b.reqs, roachpb.NewReverseScan(roachpb.Key(begin), roachpb.Key(end), maxRows))
}
b.initResult(1, 0, 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 up to maxRows
// "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{}, maxRows int64) {
b.scan(s, e, maxRows, false)
}
// 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 up to maxRows
// 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{}, maxRows int64) {
b.scan(s, e, maxRows, true)
}
// CheckConsistency creates a batch request to check the consistency of the
// ranges holding the span of keys from s to e. It logs a diff of all the
// keys that are inconsistent when withDiff is set to true.
func (b *Batch) CheckConsistency(s, e interface{}, withDiff bool) {
begin, err := marshalKey(s)
if err != nil {
b.initResult(0, 0, err)
return
}
end, err := marshalKey(e)
if err != nil {
b.initResult(0, 0, err)
return
}
b.reqs = append(b.reqs, roachpb.NewCheckConsistency(roachpb.Key(begin), roachpb.Key(end), withDiff))
b.initResult(1, 0, nil)
}
// 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{}) {
var reqs []roachpb.Request
for _, key := range keys {
k, err := marshalKey(key)
if err != nil {
b.initResult(0, len(keys), err)
return
}
reqs = append(reqs, roachpb.NewDelete(k))
}
b.reqs = append(b.reqs, reqs...)
b.initResult(len(reqs), len(reqs), 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, err)
return
}
end, err := marshalKey(e)
if err != nil {
b.initResult(0, 0, err)
return
}
b.reqs = append(b.reqs, roachpb.NewDeleteRange(roachpb.Key(begin), roachpb.Key(end), returnKeys))
b.initResult(1, 0, 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, err)
return
}
req := &roachpb.AdminMergeRequest{
Span: roachpb.Span{
Key: k,
},
}
b.reqs = append(b.reqs, req)
b.initResult(1, 0, nil)
}
// adminSplit is only exported on DB. It is here for symmetry with the
// other operations.
func (b *Batch) adminSplit(splitKey interface{}) {
k, err := marshalKey(splitKey)
if err != nil {
b.initResult(0, 0, err)
return
}
req := &roachpb.AdminSplitRequest{
Span: roachpb.Span{
Key: k,
},
}
req.SplitKey = k
b.reqs = append(b.reqs, req)
b.initResult(1, 0, nil)
}