forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_test.go
494 lines (440 loc) · 11.5 KB
/
batch_test.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
// Copyright 2012 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package pebble
import (
"bytes"
"encoding/binary"
"fmt"
"strconv"
"strings"
"testing"
"github.com/petermattis/pebble/internal/base"
"github.com/petermattis/pebble/internal/datadriven"
"github.com/petermattis/pebble/vfs"
)
func TestBatch(t *testing.T) {
testCases := []struct {
kind InternalKeyKind
key, value string
}{
{InternalKeyKindSet, "roses", "red"},
{InternalKeyKindSet, "violets", "blue"},
{InternalKeyKindDelete, "roses", ""},
{InternalKeyKindSet, "", ""},
{InternalKeyKindSet, "", "non-empty"},
{InternalKeyKindDelete, "", ""},
{InternalKeyKindSet, "grass", "green"},
{InternalKeyKindSet, "grass", "greener"},
{InternalKeyKindSet, "eleventy", strings.Repeat("!!11!", 100)},
{InternalKeyKindDelete, "nosuchkey", ""},
{InternalKeyKindSet, "binarydata", "\x00"},
{InternalKeyKindSet, "binarydata", "\xff"},
{InternalKeyKindMerge, "merge", "mergedata"},
{InternalKeyKindMerge, "merge", ""},
{InternalKeyKindMerge, "", ""},
{InternalKeyKindRangeDelete, "a", "b"},
{InternalKeyKindRangeDelete, "", ""},
{InternalKeyKindLogData, "logdata", ""},
{InternalKeyKindLogData, "", ""},
}
var b Batch
for _, tc := range testCases {
switch tc.kind {
case InternalKeyKindSet:
_ = b.Set([]byte(tc.key), []byte(tc.value), nil)
case InternalKeyKindMerge:
_ = b.Merge([]byte(tc.key), []byte(tc.value), nil)
case InternalKeyKindDelete:
_ = b.Delete([]byte(tc.key), nil)
case InternalKeyKindRangeDelete:
_ = b.DeleteRange([]byte(tc.key), []byte(tc.value), nil)
case InternalKeyKindLogData:
_ = b.LogData([]byte(tc.key), nil)
}
}
r := b.Reader()
for _, tc := range testCases {
kind, k, v, ok := r.Next()
if !ok {
t.Fatalf("next returned !ok: test case = %v", tc)
}
key, value := string(k), string(v)
if kind != tc.kind || key != tc.key || value != tc.value {
t.Errorf("got (%d, %q, %q), want (%d, %q, %q)",
kind, key, value, tc.kind, tc.key, tc.value)
}
}
if len(r) != 0 {
t.Errorf("reader was not exhausted: remaining bytes = %q", r)
}
}
func TestBatchIncrement(t *testing.T) {
testCases := []uint32{
0x00000000,
0x00000001,
0x00000002,
0x0000007f,
0x00000080,
0x000000fe,
0x000000ff,
0x00000100,
0x00000101,
0x000001ff,
0x00000200,
0x00000fff,
0x00001234,
0x0000fffe,
0x0000ffff,
0x00010000,
0x00010001,
0x000100fe,
0x000100ff,
0x00020100,
0x03fffffe,
0x03ffffff,
0x04000000,
0x04000001,
0x7fffffff,
0xfffffffe,
0xffffffff,
}
for _, tc := range testCases {
var buf [12]byte
binary.LittleEndian.PutUint32(buf[8:12], tc)
var b Batch
b.storage.data = buf[:]
b.increment()
got := binary.LittleEndian.Uint32(buf[8:12])
want := tc + 1
if tc == 0xffffffff {
want = tc
}
if got != want {
t.Errorf("input=%d: got %d, want %d", tc, got, want)
}
}
}
func TestBatchGet(t *testing.T) {
for _, method := range []string{"build", "apply"} {
t.Run(method, func(t *testing.T) {
d, err := Open("", &Options{
FS: vfs.NewMem(),
})
if err != nil {
t.Fatalf("Open: %v", err)
}
defer d.Close()
var b *Batch
datadriven.RunTest(t, "testdata/batch_get", func(td *datadriven.TestData) string {
switch td.Cmd {
case "define":
switch method {
case "build":
b = d.NewIndexedBatch()
case "apply":
b = d.NewBatch()
}
if err := runBatchDefineCmd(td, b); err != nil {
return err.Error()
}
switch method {
case "apply":
tmp := d.NewIndexedBatch()
tmp.Apply(b, nil)
b = tmp
}
return ""
case "commit":
if err := b.Commit(nil); err != nil {
return err.Error()
}
return ""
case "get":
if len(td.CmdArgs) != 1 {
return fmt.Sprintf("%s expects 1 argument", td.Cmd)
}
v, err := b.Get([]byte(td.CmdArgs[0].String()))
if err != nil {
return err.Error()
}
return string(v)
default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
}
})
})
}
}
func TestBatchIter(t *testing.T) {
var b *Batch
for _, method := range []string{"build", "apply"} {
t.Run(method, func(t *testing.T) {
datadriven.RunTest(t, "testdata/internal_iter_next", func(d *datadriven.TestData) string {
switch d.Cmd {
case "define":
switch method {
case "build":
b = newIndexedBatch(nil, DefaultComparer)
case "apply":
b = newBatch(nil)
}
for _, key := range strings.Split(d.Input, "\n") {
j := strings.Index(key, ":")
ikey := base.ParseInternalKey(key[:j])
value := []byte(fmt.Sprint(ikey.SeqNum()))
b.Set(ikey.UserKey, value, nil)
}
switch method {
case "apply":
tmp := newIndexedBatch(nil, DefaultComparer)
tmp.Apply(b, nil)
b = tmp
}
return ""
case "iter":
iter := b.newInternalIter(nil)
defer iter.Close()
return runInternalIterCmd(d, iter)
default:
return fmt.Sprintf("unknown command: %s", d.Cmd)
}
})
})
}
}
func TestBatchDeleteRange(t *testing.T) {
var b *Batch
datadriven.RunTest(t, "testdata/batch_delete_range", func(td *datadriven.TestData) string {
switch td.Cmd {
case "clear":
b = nil
return ""
case "define":
if b == nil {
b = newIndexedBatch(nil, DefaultComparer)
}
if err := runBatchDefineCmd(td, b); err != nil {
return err.Error()
}
return ""
case "scan":
var iter internalIterAdapter
if len(td.CmdArgs) > 1 {
return fmt.Sprintf("%s expects at most 1 argument", td.Cmd)
}
if len(td.CmdArgs) == 1 {
if td.CmdArgs[0].String() != "range-del" {
return fmt.Sprintf("%s unknown argument %s", td.Cmd, td.CmdArgs[0])
}
iter.internalIterator = b.newRangeDelIter(nil)
} else {
iter.internalIterator = b.newInternalIter(nil)
}
defer iter.Close()
var buf bytes.Buffer
for valid := iter.First(); valid; valid = iter.Next() {
key := iter.Key()
key.SetSeqNum(key.SeqNum() &^ InternalKeySeqNumBatch)
fmt.Fprintf(&buf, "%s:%s\n", key, iter.Value())
}
return buf.String()
default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
}
})
}
func TestFlushableBatchIter(t *testing.T) {
var b *flushableBatch
datadriven.RunTest(t, "testdata/internal_iter_next", func(d *datadriven.TestData) string {
switch d.Cmd {
case "define":
batch := newBatch(nil)
for _, key := range strings.Split(d.Input, "\n") {
j := strings.Index(key, ":")
ikey := base.ParseInternalKey(key[:j])
value := []byte(fmt.Sprint(ikey.SeqNum()))
batch.Set(ikey.UserKey, value, nil)
}
b = newFlushableBatch(batch, DefaultComparer)
return ""
case "iter":
iter := b.newIter(nil)
defer iter.Close()
return runInternalIterCmd(d, iter)
default:
return fmt.Sprintf("unknown command: %s", d.Cmd)
}
})
}
func TestFlushableBatch(t *testing.T) {
var b *flushableBatch
datadriven.RunTest(t, "testdata/flushable_batch", func(d *datadriven.TestData) string {
switch d.Cmd {
case "define":
batch := newBatch(nil)
for _, key := range strings.Split(d.Input, "\n") {
j := strings.Index(key, ":")
ikey := base.ParseInternalKey(key[:j])
value := []byte(fmt.Sprint(ikey.SeqNum()))
switch ikey.Kind() {
case InternalKeyKindDelete:
batch.Delete(ikey.UserKey, nil)
case InternalKeyKindSet:
batch.Set(ikey.UserKey, value, nil)
case InternalKeyKindMerge:
batch.Merge(ikey.UserKey, value, nil)
}
}
b = newFlushableBatch(batch, DefaultComparer)
return ""
case "iter":
var opts IterOptions
for _, arg := range d.CmdArgs {
if len(arg.Vals) != 1 {
return fmt.Sprintf("%s: %s=<value>", d.Cmd, arg.Key)
}
switch arg.Key {
case "lower":
opts.LowerBound = []byte(arg.Vals[0])
case "upper":
opts.UpperBound = []byte(arg.Vals[0])
default:
return fmt.Sprintf("%s: unknown arg: %s", d.Cmd, arg.Key)
}
}
iter := b.newIter(&opts)
defer iter.Close()
return runInternalIterCmd(d, iter)
case "dump":
if len(d.CmdArgs) != 1 || len(d.CmdArgs[0].Vals) != 1 || d.CmdArgs[0].Key != "seq" {
return fmt.Sprintf("dump seq=<value>\n")
}
seqNum, err := strconv.Atoi(d.CmdArgs[0].Vals[0])
if err != nil {
return err.Error()
}
b.seqNum = uint64(seqNum)
iter := internalIterAdapter{b.newIter(nil)}
var buf bytes.Buffer
for valid := iter.First(); valid; valid = iter.Next() {
fmt.Fprintf(&buf, "%s:%s\n", iter.Key(), iter.Value())
}
iter.Close()
return buf.String()
default:
return fmt.Sprintf("unknown command: %s", d.Cmd)
}
})
}
func TestFlushableBatchDeleteRange(t *testing.T) {
var fb *flushableBatch
var input string
datadriven.RunTest(t, "testdata/delete_range", func(td *datadriven.TestData) string {
switch td.Cmd {
case "clear":
input = ""
return ""
case "define":
b := newBatch(nil)
// NB: We can't actually add to the flushable batch as we can to a
// memtable (which shares the "testdata/delete_range" data), so we fake
// it be concatenating the input and rebuilding the flushable batch from
// scratch.
input += "\n" + td.Input
td.Input = input
if err := runBatchDefineCmd(td, b); err != nil {
return err.Error()
}
fb = newFlushableBatch(b, DefaultComparer)
return ""
case "scan":
var iter internalIterAdapter
if len(td.CmdArgs) > 1 {
return fmt.Sprintf("%s expects at most 1 argument", td.Cmd)
}
if len(td.CmdArgs) == 1 {
if td.CmdArgs[0].String() != "range-del" {
return fmt.Sprintf("%s unknown argument %s", td.Cmd, td.CmdArgs[0])
}
iter.internalIterator = fb.newRangeDelIter(nil)
} else {
iter.internalIterator = fb.newIter(nil)
}
defer iter.Close()
var buf bytes.Buffer
for valid := iter.First(); valid; valid = iter.Next() {
fmt.Fprintf(&buf, "%s:%s\n", iter.Key(), iter.Value())
}
return buf.String()
default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
}
})
}
func TestFlushableBatchBytesIterated(t *testing.T) {
batch := newBatch(nil)
for j := 0; j < 1000; j++ {
key := make([]byte, 8 + j%3)
value := make([]byte, 7 + j%5)
batch.Set(key, value, nil)
fb := newFlushableBatch(batch, DefaultComparer)
var bytesIterated uint64
it := fb.newFlushIter(nil, &bytesIterated)
var prevIterated uint64
for it.First(); it.Valid(); it.Next() {
if bytesIterated < prevIterated {
t.Fatalf("bytesIterated moved backward: %d < %d", bytesIterated, prevIterated)
}
prevIterated = bytesIterated
}
expected := fb.totalBytes()
if bytesIterated != expected {
t.Fatalf("bytesIterated: got %d, want %d", bytesIterated, expected)
}
}
}
func BenchmarkBatchSet(b *testing.B) {
value := make([]byte, 10)
for i := range value {
value[i] = byte(i)
}
key := make([]byte, 8)
b.ResetTimer()
const batchSize = 1000
for i := 0; i < b.N; i += batchSize {
end := i + batchSize
if end > b.N {
end = b.N
}
batch := newBatch(nil)
for j := i; j < end; j++ {
binary.BigEndian.PutUint64(key, uint64(j))
batch.Set(key, value, nil)
}
batch.release()
}
b.StopTimer()
}
func BenchmarkIndexedBatchSet(b *testing.B) {
value := make([]byte, 10)
for i := range value {
value[i] = byte(i)
}
key := make([]byte, 8)
b.ResetTimer()
const batchSize = 1000
for i := 0; i < b.N; i += batchSize {
end := i + batchSize
if end > b.N {
end = b.N
}
batch := newIndexedBatch(nil, DefaultComparer)
for j := i; j < end; j++ {
binary.BigEndian.PutUint64(key, uint64(j))
batch.Set(key, value, nil)
}
batch.release()
}
b.StopTimer()
}