-
Notifications
You must be signed in to change notification settings - Fork 30
/
backend_test.go
468 lines (384 loc) · 14 KB
/
backend_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
package db
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Register a test backend for PrefixDB as well, with some unrelated junk data
func init() {
registerDBCreator("prefixdb", func(name, dir string, opts Options) (DB, error) {
mdb := NewMemDB()
mdb.Set([]byte("a"), []byte{1}) //nolint:errcheck
mdb.Set([]byte("b"), []byte{2}) //nolint:errcheck
mdb.Set([]byte("t"), []byte{20}) //nolint:errcheck
mdb.Set([]byte("test"), []byte{0}) //nolint:errcheck
mdb.Set([]byte("u"), []byte{21}) //nolint:errcheck
mdb.Set([]byte("z"), []byte{26}) //nolint:errcheck
return NewPrefixDB(mdb, []byte("test/")), nil
}, false)
}
func cleanupDBDir(dir, name string) {
err := os.RemoveAll(filepath.Join(dir, name) + DBFileSuffix)
if err != nil {
panic(err)
}
}
func testBackendGetSetDelete(t *testing.T, backend BackendType) {
// Default
dirname, err := os.MkdirTemp("", fmt.Sprintf("test_backend_%s_", backend))
require.Nil(t, err)
db, err := NewDB("testdb", backend, dirname)
require.NoError(t, err)
defer cleanupDBDir(dirname, "testdb")
// A nonexistent key should return nil.
value, err := db.Get([]byte("a"))
require.NoError(t, err)
require.Nil(t, value)
ok, err := db.Has([]byte("a"))
require.NoError(t, err)
require.False(t, ok)
// Set and get a value.
err = db.Set([]byte("a"), []byte{0x01})
require.NoError(t, err)
ok, err = db.Has([]byte("a"))
require.NoError(t, err)
require.True(t, ok)
value, err = db.Get([]byte("a"))
require.NoError(t, err)
require.Equal(t, []byte{0x01}, value)
err = db.SetSync([]byte("b"), []byte{0x02})
require.NoError(t, err)
value, err = db.Get([]byte("b"))
require.NoError(t, err)
require.Equal(t, []byte{0x02}, value)
// Deleting a non-existent value is fine.
err = db.Delete([]byte("x"))
require.NoError(t, err)
err = db.DeleteSync([]byte("x"))
require.NoError(t, err)
// Delete a value.
err = db.Delete([]byte("a"))
require.NoError(t, err)
value, err = db.Get([]byte("a"))
require.NoError(t, err)
require.Nil(t, value)
err = db.DeleteSync([]byte("b"))
require.NoError(t, err)
value, err = db.Get([]byte("b"))
require.NoError(t, err)
require.Nil(t, value)
// Setting, getting, and deleting an empty key should error.
_, err = db.Get([]byte{})
require.Equal(t, errKeyEmpty, err)
_, err = db.Get(nil)
require.Equal(t, errKeyEmpty, err)
_, err = db.Has([]byte{})
require.Equal(t, errKeyEmpty, err)
_, err = db.Has(nil)
require.Equal(t, errKeyEmpty, err)
err = db.Set([]byte{}, []byte{0x01})
require.Equal(t, errKeyEmpty, err)
err = db.Set(nil, []byte{0x01})
require.Equal(t, errKeyEmpty, err)
err = db.SetSync([]byte{}, []byte{0x01})
require.Equal(t, errKeyEmpty, err)
err = db.SetSync(nil, []byte{0x01})
require.Equal(t, errKeyEmpty, err)
err = db.Delete([]byte{})
require.Equal(t, errKeyEmpty, err)
err = db.Delete(nil)
require.Equal(t, errKeyEmpty, err)
err = db.DeleteSync([]byte{})
require.Equal(t, errKeyEmpty, err)
err = db.DeleteSync(nil)
require.Equal(t, errKeyEmpty, err)
// Setting a nil value should error, but an empty value is fine.
err = db.Set([]byte("x"), nil)
require.Equal(t, errValueNil, err)
err = db.SetSync([]byte("x"), nil)
require.Equal(t, errValueNil, err)
err = db.Set([]byte("x"), []byte{})
require.NoError(t, err)
err = db.SetSync([]byte("x"), []byte{})
require.NoError(t, err)
value, err = db.Get([]byte("x"))
require.NoError(t, err)
require.Equal(t, []byte{}, value)
}
func TestBackendsGetSetDelete(t *testing.T) {
for dbType := range backends {
t.Run(string(dbType), func(t *testing.T) {
testBackendGetSetDelete(t, dbType)
})
}
}
func TestGoLevelDBBackend(t *testing.T) {
name := fmt.Sprintf("test_%x", randStr(12))
db, err := NewDB(name, GoLevelDBBackend, "")
require.NoError(t, err)
defer cleanupDBDir("", name)
_, ok := db.(*GoLevelDB)
assert.True(t, ok)
}
func TestDBIterator(t *testing.T) {
for dbType := range backends {
t.Run(string(dbType), func(t *testing.T) {
testDBIterator(t, dbType)
})
}
}
func testDBIterator(t *testing.T, backend BackendType) {
name := fmt.Sprintf("test_%x", randStr(12))
dir := os.TempDir()
db, err := NewDB(name, backend, dir)
require.NoError(t, err)
defer cleanupDBDir(dir, name)
for i := 0; i < 10; i++ {
if i != 6 { // but skip 6.
err := db.Set(int642Bytes(int64(i)), []byte{})
require.NoError(t, err)
}
}
// Blank iterator keys should error
_, err = db.Iterator([]byte{}, nil)
require.Equal(t, errKeyEmpty, err)
_, err = db.Iterator(nil, []byte{})
require.Equal(t, errKeyEmpty, err)
_, err = db.ReverseIterator([]byte{}, nil)
require.Equal(t, errKeyEmpty, err)
_, err = db.ReverseIterator(nil, []byte{})
require.Equal(t, errKeyEmpty, err)
itr, err := db.Iterator(nil, nil)
require.NoError(t, err)
verifyIterator(t, itr, []int64{0, 1, 2, 3, 4, 5, 7, 8, 9}, "forward iterator")
ritr, err := db.ReverseIterator(nil, nil)
require.NoError(t, err)
verifyIterator(t, ritr, []int64{9, 8, 7, 5, 4, 3, 2, 1, 0}, "reverse iterator")
itr, err = db.Iterator(nil, int642Bytes(0))
require.NoError(t, err)
verifyIterator(t, itr, []int64(nil), "forward iterator to 0")
ritr, err = db.ReverseIterator(int642Bytes(10), nil)
require.NoError(t, err)
verifyIterator(t, ritr, []int64(nil), "reverse iterator from 10 (ex)")
itr, err = db.Iterator(int642Bytes(0), nil)
require.NoError(t, err)
verifyIterator(t, itr, []int64{0, 1, 2, 3, 4, 5, 7, 8, 9}, "forward iterator from 0")
itr, err = db.Iterator(int642Bytes(1), nil)
require.NoError(t, err)
verifyIterator(t, itr, []int64{1, 2, 3, 4, 5, 7, 8, 9}, "forward iterator from 1")
ritr, err = db.ReverseIterator(nil, int642Bytes(10))
require.NoError(t, err)
verifyIterator(t, ritr,
[]int64{9, 8, 7, 5, 4, 3, 2, 1, 0}, "reverse iterator from 10 (ex)")
ritr, err = db.ReverseIterator(nil, int642Bytes(9))
require.NoError(t, err)
verifyIterator(t, ritr,
[]int64{8, 7, 5, 4, 3, 2, 1, 0}, "reverse iterator from 9 (ex)")
ritr, err = db.ReverseIterator(nil, int642Bytes(8))
require.NoError(t, err)
verifyIterator(t, ritr,
[]int64{7, 5, 4, 3, 2, 1, 0}, "reverse iterator from 8 (ex)")
itr, err = db.Iterator(int642Bytes(5), int642Bytes(6))
require.NoError(t, err)
verifyIterator(t, itr, []int64{5}, "forward iterator from 5 to 6")
itr, err = db.Iterator(int642Bytes(5), int642Bytes(7))
require.NoError(t, err)
verifyIterator(t, itr, []int64{5}, "forward iterator from 5 to 7")
itr, err = db.Iterator(int642Bytes(5), int642Bytes(8))
require.NoError(t, err)
verifyIterator(t, itr, []int64{5, 7}, "forward iterator from 5 to 8")
itr, err = db.Iterator(int642Bytes(6), int642Bytes(7))
require.NoError(t, err)
verifyIterator(t, itr, []int64(nil), "forward iterator from 6 to 7")
itr, err = db.Iterator(int642Bytes(6), int642Bytes(8))
require.NoError(t, err)
verifyIterator(t, itr, []int64{7}, "forward iterator from 6 to 8")
itr, err = db.Iterator(int642Bytes(7), int642Bytes(8))
require.NoError(t, err)
verifyIterator(t, itr, []int64{7}, "forward iterator from 7 to 8")
ritr, err = db.ReverseIterator(int642Bytes(4), int642Bytes(5))
require.NoError(t, err)
verifyIterator(t, ritr, []int64{4}, "reverse iterator from 5 (ex) to 4")
ritr, err = db.ReverseIterator(int642Bytes(4), int642Bytes(6))
require.NoError(t, err)
verifyIterator(t, ritr,
[]int64{5, 4}, "reverse iterator from 6 (ex) to 4")
ritr, err = db.ReverseIterator(int642Bytes(4), int642Bytes(7))
require.NoError(t, err)
verifyIterator(t, ritr,
[]int64{5, 4}, "reverse iterator from 7 (ex) to 4")
ritr, err = db.ReverseIterator(int642Bytes(5), int642Bytes(6))
require.NoError(t, err)
verifyIterator(t, ritr, []int64{5}, "reverse iterator from 6 (ex) to 5")
ritr, err = db.ReverseIterator(int642Bytes(5), int642Bytes(7))
require.NoError(t, err)
verifyIterator(t, ritr, []int64{5}, "reverse iterator from 7 (ex) to 5")
ritr, err = db.ReverseIterator(int642Bytes(6), int642Bytes(7))
require.NoError(t, err)
verifyIterator(t, ritr,
[]int64(nil), "reverse iterator from 7 (ex) to 6")
ritr, err = db.ReverseIterator(int642Bytes(10), nil)
require.NoError(t, err)
verifyIterator(t, ritr, []int64(nil), "reverse iterator to 10")
ritr, err = db.ReverseIterator(int642Bytes(6), nil)
require.NoError(t, err)
verifyIterator(t, ritr, []int64{9, 8, 7}, "reverse iterator to 6")
ritr, err = db.ReverseIterator(int642Bytes(5), nil)
require.NoError(t, err)
verifyIterator(t, ritr, []int64{9, 8, 7, 5}, "reverse iterator to 5")
ritr, err = db.ReverseIterator(int642Bytes(8), int642Bytes(9))
require.NoError(t, err)
verifyIterator(t, ritr, []int64{8}, "reverse iterator from 9 (ex) to 8")
ritr, err = db.ReverseIterator(int642Bytes(2), int642Bytes(4))
require.NoError(t, err)
verifyIterator(t, ritr,
[]int64{3, 2}, "reverse iterator from 4 (ex) to 2")
ritr, err = db.ReverseIterator(int642Bytes(4), int642Bytes(2))
require.NoError(t, err)
verifyIterator(t, ritr,
[]int64(nil), "reverse iterator from 2 (ex) to 4")
// Ensure that the iterators don't panic with an empty database.
dir2, err := os.MkdirTemp("", "tm-db-test")
require.NoError(t, err)
db2, err := NewDB(name, backend, dir2)
require.NoError(t, err)
defer cleanupDBDir(dir2, name)
itr, err = db2.Iterator(nil, nil)
require.NoError(t, err)
verifyIterator(t, itr, nil, "forward iterator with empty db")
ritr, err = db2.ReverseIterator(nil, nil)
require.NoError(t, err)
verifyIterator(t, ritr, nil, "reverse iterator with empty db")
}
func verifyIterator(t *testing.T, itr Iterator, expected []int64, msg string) {
var list []int64
for itr.Valid() {
key := itr.Key()
list = append(list, bytes2Int64(key))
itr.Next()
}
assert.Equal(t, expected, list, msg)
}
func TestDBBatchGetByteSize(t *testing.T) {
for dbType := range backends {
t.Run(fmt.Sprintf("%v", dbType), func(t *testing.T) {
testDBBatchGetByteSize(t, dbType)
})
}
}
func testDBBatchGetByteSize(t *testing.T, backend BackendType) {
name := fmt.Sprintf("test_%x", randStr(12))
dir := os.TempDir()
db, err := NewDB(name, backend, dir)
require.NoError(t, err)
defer cleanupDBDir(dir, name)
// create a new batch
batch := db.NewBatch()
batchSize, err := batch.GetByteSize()
require.NoError(t, err)
// size of newly created batch should be 0 or negligible because of the metadata in the batch,
// for example peppble's batchHeaderLen is 12 so
// peppble's batch size will always be equal or greater than 12 even for empty batch
require.LessOrEqual(t, batchSize, 32)
totalSizeOfKeyAndValue := 0
// set 100 random keys and values
for i := 0; i < 100; i++ {
keySize := rand.Intn(32) + 1 //nolint:gosec
valueSize := rand.Intn(32) + 1 //nolint:gosec
totalSizeOfKeyAndValue += keySize + valueSize
require.NoError(t, batch.Set([]byte(randStr(keySize)), []byte(randStr(valueSize))))
}
batchSize, err = batch.GetByteSize()
require.NoError(t, err)
// because we set a lot of keys and values with considerable size,
// ratio of batchSize / totalSizeOfKeyAndValue should be roughly 1
require.Equal(t, 1, batchSize/totalSizeOfKeyAndValue)
err = batch.Write()
require.NoError(t, err)
_, err = batch.GetByteSize()
// calling GetByteSize on a written batch should error
require.Error(t, err)
}
func TestDBBatchOperations(t *testing.T) {
for dbType := range backends {
t.Run(fmt.Sprintf("%v", dbType), func(t *testing.T) {
testDBBatchOperations(t, dbType)
})
}
}
func testDBBatchOperations(t *testing.T, backend BackendType) {
name := fmt.Sprintf("test_%x", randStr(12))
dir := os.TempDir()
db, err := NewDB(name, backend, dir)
require.NoError(t, err)
defer cleanupDBDir(dir, name)
// create a new batch, and some items - they should not be visible until we write
batch := db.NewBatch()
require.NoError(t, batch.Set([]byte("a"), []byte{1}))
require.NoError(t, batch.Set([]byte("b"), []byte{2}))
require.NoError(t, batch.Set([]byte("c"), []byte{3}))
assertKeyValues(t, db, map[string][]byte{})
err = batch.Write()
require.NoError(t, err)
assertKeyValues(t, db, map[string][]byte{"a": {1}, "b": {2}, "c": {3}})
// trying to modify or rewrite a written batch should error, but closing it should work
require.Error(t, batch.Set([]byte("a"), []byte{9}))
require.Error(t, batch.Delete([]byte("a")))
require.Error(t, batch.Write())
require.Error(t, batch.WriteSync())
require.NoError(t, batch.Close())
// batches should write changes in order
batch = db.NewBatch()
require.NoError(t, batch.Delete([]byte("a")))
require.NoError(t, batch.Set([]byte("a"), []byte{1}))
require.NoError(t, batch.Set([]byte("b"), []byte{1}))
require.NoError(t, batch.Set([]byte("b"), []byte{2}))
require.NoError(t, batch.Set([]byte("c"), []byte{3}))
require.NoError(t, batch.Delete([]byte("c")))
require.NoError(t, batch.Write())
require.NoError(t, batch.Close())
assertKeyValues(t, db, map[string][]byte{"a": {1}, "b": {2}})
// empty and nil keys, as well as nil values, should be disallowed
batch = db.NewBatch()
err = batch.Set([]byte{}, []byte{0x01})
require.Equal(t, errKeyEmpty, err)
err = batch.Set(nil, []byte{0x01})
require.Equal(t, errKeyEmpty, err)
err = batch.Set([]byte("a"), nil)
require.Equal(t, errValueNil, err)
err = batch.Delete([]byte{})
require.Equal(t, errKeyEmpty, err)
err = batch.Delete(nil)
require.Equal(t, errKeyEmpty, err)
err = batch.Close()
require.NoError(t, err)
// it should be possible to write an empty batch
batch = db.NewBatch()
err = batch.Write()
require.NoError(t, err)
assertKeyValues(t, db, map[string][]byte{"a": {1}, "b": {2}})
// it should be possible to close an empty batch, and to re-close a closed batch
batch = db.NewBatch()
batch.Close()
batch.Close()
// all other operations on a closed batch should error
require.Error(t, batch.Set([]byte("a"), []byte{9}))
require.Error(t, batch.Delete([]byte("a")))
require.Error(t, batch.Write())
require.Error(t, batch.WriteSync())
}
func assertKeyValues(t *testing.T, db DB, expect map[string][]byte) {
iter, err := db.Iterator(nil, nil)
require.NoError(t, err)
defer iter.Close()
actual := make(map[string][]byte)
for ; iter.Valid(); iter.Next() {
require.NoError(t, iter.Error())
actual[string(iter.Key())] = iter.Value()
}
assert.Equal(t, expect, actual)
}