-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathdb_test.go
483 lines (427 loc) · 10.6 KB
/
db_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
package pogreb
import (
"bufio"
"encoding/binary"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/akrylysov/pogreb/fs"
"github.com/akrylysov/pogreb/internal/assert"
)
const (
testDBName = "test.db"
)
var (
// File system used for all tests.
testFS fs.FileSystem
)
func TestMain(m *testing.M) {
flag.Parse()
if !testing.Verbose() {
SetLogger(log.New(io.Discard, "", 0))
}
// Run tests against all file systems.
for _, fsys := range []fs.FileSystem{fs.Mem, fs.OSMMap, fs.OS} {
testFS = fsys
if testing.Verbose() {
fmt.Printf("=== SET\tFS=%T\n", fsys)
}
if exitCode := m.Run(); exitCode != 0 {
fmt.Printf("DEBUG\tFS=%T\n", fsys)
os.Exit(exitCode)
}
_ = cleanDir(testDBName)
_ = cleanDir(testDBBackupName)
}
os.Exit(0)
}
func appendFile(path string, data []byte) error {
f, err := testFS.OpenFile(path, os.O_RDWR, os.FileMode(0640))
if err != nil {
return err
}
defer f.Close()
if _, err = f.Seek(0, io.SeekEnd); err != nil {
return err
}
_, err = f.Write(data)
return err
}
func align512(n uint32) uint32 {
return (n + 511) &^ 511
}
func TestBucketSize(t *testing.T) {
serializedSize := uint32(binary.Size(bucket{}))
if bucketSize != align512(serializedSize) {
t.Fatal("wrong bucketSize value", bucketSize)
}
if bucketSize-serializedSize > 32 {
t.Fatal("bucket is wasting too much space", bucketSize, serializedSize)
}
}
func TestHeaderSize(t *testing.T) {
if headerSize != align512(uint32(binary.Size(header{}))) || headerSize != 512 {
t.Fatal("wrong headerSize value", headerSize)
}
}
func cleanDir(path string) error {
files, err := testFS.ReadDir(path)
if err != nil {
return err
}
for _, file := range files {
_ = testFS.Remove(filepath.Join(path, file.Name()))
}
return nil
}
func createTestDB(opts *Options) (*DB, error) {
if opts == nil {
opts = &Options{FileSystem: testFS}
} else {
if opts.FileSystem == nil {
opts.FileSystem = testFS
}
}
if err := cleanDir(testDBName); err != nil {
return nil, err
}
return Open(testDBName, opts)
}
func TestEmpty(t *testing.T) {
opts := &Options{FileSystem: testFS}
db, err := createTestDB(opts)
assert.Nil(t, err)
assert.Nil(t, db.Close())
db, err = Open(testDBName, opts)
assert.Nil(t, err)
assert.Nil(t, db.Close())
}
func TestFull(t *testing.T) {
fullTest(t, func(db *DB, key []byte) ([]byte, error) {
return db.Get(key)
})
var buf []byte
fullTest(t, func(db *DB, key []byte) ([]byte, error) {
var err error
buf, err = db.GetAppend(key, buf[:0])
return buf, err
})
}
func fullTest(t *testing.T, getFunc func(db *DB, key []byte) ([]byte, error)) {
opts := &Options{
BackgroundSyncInterval: -1,
FileSystem: testFS,
maxSegmentSize: 1024,
}
db, err := createTestDB(opts)
assert.Nil(t, err)
var i byte
var n uint8 = 255
assert.Equal(t, uint32(0), db.Count())
for i = 0; i < n; i++ {
if has, err := db.Has([]byte{i}); has || err != nil {
t.Fatal(has, err)
}
}
assert.Nil(t, db.Delete([]byte{128}))
assert.Equal(t, uint32(0), db.Count())
for i = 0; i < n; i++ {
assert.Nil(t, db.Put([]byte{i}, []byte{i}))
}
assert.Equal(t, uint32(255), db.Count())
assert.Equal(t, int64(n), db.Metrics().Puts.Value())
assert.Nil(t, db.Sync())
sz, err := db.FileSize()
assert.Nil(t, err)
if sz <= 0 {
t.Fatal(sz)
}
assert.Nil(t, db.Delete([]byte{128}))
assert.Equal(t, uint32(254), db.Count())
if has, err := db.Has([]byte{128}); has || err != nil {
t.Fatal(has, err)
}
assert.Nil(t, db.Put([]byte{128}, []byte{128}))
assert.Equal(t, uint32(255), db.Count())
verifyKeysAndClose := func(valueOffset uint8) {
t.Helper()
assert.Equal(t, uint32(255), db.Count())
for i = 0; i < n; i++ {
if has, err := db.Has([]byte{i}); !has || err != nil {
t.Fatal(has, err)
}
if has, err := db.Has([]byte{0, i}); has || err != nil {
t.Fatal(has, err)
}
v, err := getFunc(db, []byte{i})
if err != nil {
t.Fatal(err)
}
assert.Equal(t, []byte{i + valueOffset}, v)
}
assert.Nil(t, db.Close())
}
expectedSegMetas := db.datalog.segmentMetas()
verifyKeysAndClose(0)
// Open and check again
db, err = Open(testDBName, opts)
assert.Nil(t, err)
verifyKeysAndClose(0)
// Simulate crash.
assert.Nil(t, touchFile(testFS, filepath.Join(testDBName, lockName)))
assert.Nil(t, testFS.Remove(filepath.Join(testDBName, segmentMetaName(0, 1))))
assert.Nil(t, testFS.Remove(filepath.Join(testDBName, indexMetaName)))
// Open and check again
db, err = Open(testDBName, opts)
assert.Nil(t, err)
verifyKeysAndClose(0)
assert.Equal(t, expectedSegMetas, db.datalog.segmentMetas())
// Update all items
db, err = Open(testDBName, opts)
assert.Nil(t, err)
for i = 0; i < n; i++ {
assert.Nil(t, db.Put([]byte{i}, []byte{i + 6}))
}
verifyKeysAndClose(6)
// Delete all items
db, err = Open(testDBName, &Options{BackgroundSyncInterval: time.Millisecond})
assert.Nil(t, err)
for i = 0; i < n; i++ {
assert.Nil(t, db.Delete([]byte{i}))
}
for i = 0; i < n; i++ {
if has, err := db.Has([]byte{i}); has || err != nil {
t.Fatal(has, err)
}
}
assert.Equal(t, uint32(0), db.Count())
assert.Nil(t, db.Close())
}
func TestLock(t *testing.T) {
opts := &Options{FileSystem: testFS}
db, err := createTestDB(opts)
assert.Nil(t, err)
// Opening already opened database returns an error.
db2, err2 := Open(testDBName, opts)
assert.Nil(t, db2)
assert.NotNil(t, err2)
assert.Nil(t, db.Close())
}
func TestEmptyKey(t *testing.T) {
db, err := createTestDB(nil)
assert.Nil(t, err)
if err := db.Put([]byte{}, []byte{1}); err != nil {
t.Fatal(err)
}
v, err := db.Get([]byte{})
assert.Nil(t, err)
assert.Equal(t, []byte{1}, v)
assert.Nil(t, db.Close())
}
func TestEmptyValue(t *testing.T) {
db, err := createTestDB(nil)
assert.Nil(t, err)
// Returns a nil value if key not found.
if v, err := db.Get([]byte{1}); err != nil || v != nil {
t.Fatal(err)
}
err = db.Put([]byte{1}, []byte{})
assert.Nil(t, err)
// Returns an empty slice if value is empty.
if v, err := db.Get([]byte{1}); err != nil || v == nil || len(v) != 0 {
t.Fatal(err)
}
assert.Nil(t, db.Close())
}
func TestEmptyKeyValue(t *testing.T) {
db, err := createTestDB(nil)
assert.Nil(t, err)
assert.Nil(t, db.Put([]byte{}, []byte{}))
v, err := db.Get([]byte{})
assert.Nil(t, err)
assert.Equal(t, []byte{}, v)
assert.Nil(t, db.Close())
}
func TestDataRecycle(t *testing.T) {
db, err := createTestDB(nil)
assert.Nil(t, err)
assert.Nil(t, db.Put([]byte{1}, []byte{8}))
v, err := db.Get([]byte{1})
assert.Nil(t, err)
assert.Equal(t, []byte{8}, v)
err = db.Delete([]byte{1})
assert.Nil(t, err)
err = db.Put([]byte{1}, []byte{9})
assert.Nil(t, err)
assert.Equal(t, []byte{8}, v)
assert.Nil(t, db.Close())
}
func TestClose(t *testing.T) {
db, err := createTestDB(nil)
assert.Nil(t, err)
assert.Nil(t, db.Close())
_, err = db.Get([]byte{1})
assert.NotNil(t, err)
assert.NotNil(t, db.Close())
}
func TestCorruptedIndex(t *testing.T) {
opts := &Options{FileSystem: testFS}
db, err := createTestDB(opts)
assert.Nil(t, err)
assert.Nil(t, db.Close())
f, err := testFS.OpenFile(filepath.Join(testDBName, indexMetaName), os.O_RDWR, 0)
assert.Nil(t, err)
_, err = f.Write([]byte("corrupted"))
assert.Nil(t, err)
assert.Nil(t, f.Close())
db, err = Open(testDBName, opts)
assert.Nil(t, db)
assert.NotNil(t, err)
}
func TestFileError(t *testing.T) {
db, err := createTestDB(nil)
assert.Nil(t, err)
assert.Nil(t, db.Put(nil, nil))
errf := &errfile{}
testDB := func(t *testing.T) {
v, err := db.Get(nil)
assert.Nil(t, v)
assert.Equal(t, errfileError, err)
assert.Equal(t, errfileError, db.Put(nil, nil))
assert.Equal(t, errfileError, db.Delete(nil))
has, err := db.Has(nil)
assert.Equal(t, false, has)
assert.Equal(t, errfileError, err)
it := db.Items()
k, v, err := it.Next()
assert.Nil(t, k)
assert.Nil(t, v)
assert.Equal(t, errfileError, err)
}
t.Run("segment error", func(t *testing.T) {
oldf := db.datalog.segments[0].File
db.datalog.segments[0].File = errf
testDB(t)
assert.Equal(t, errfileError, db.Close())
db.datalog.segments[0].File = oldf
})
t.Run("index error", func(t *testing.T) {
oldf := db.index.main.File
db.index.main.File = errf
testDB(t)
assert.Equal(t, errfileError, db.index.close())
db.index.main.File = oldf
})
errfs := &errfs{}
oldfs := db.opts.FileSystem
db.opts.FileSystem = errfs
assert.Equal(t, errfileError, db.Close())
assert.Equal(t, errfileError, db.index.close())
db.opts.FileSystem = oldfs
assert.Nil(t, db.Close())
}
func TestFSError(t *testing.T) {
db, err := createTestDB(&Options{FileSystem: &errfs{}})
assert.Nil(t, db)
assert.NotNil(t, err)
}
func TestWordsDict(t *testing.T) {
if testFS != fs.Mem {
t.Skip()
}
fwords, err := os.Open("/usr/share/dict/words")
if err != nil {
t.Skip("words file not found")
}
defer fwords.Close()
db, err := createTestDB(nil)
assert.Nil(t, err)
scanner := bufio.NewScanner(fwords)
items := make(map[string]string)
for scanner.Scan() {
k := scanner.Text()
v := strings.ToUpper(k)
items[k] = v
assert.Nil(t, db.Put([]byte(k), []byte(v)))
}
assert.Nil(t, scanner.Err())
for k, v := range items {
v2, err := db.Get([]byte(k))
if string(v2) != v {
t.Fatalf("expected %v; got value=%v, err=%v for key %v", v, string(v2), err, k)
}
}
assert.Nil(t, db.Close())
}
func BenchmarkPut(b *testing.B) {
db, err := createTestDB(nil)
assert.Nil(b, err)
b.ResetTimer()
k := []byte{1}
for i := 0; i < b.N; i++ {
if err := db.Put(k, k); err != nil {
b.Fail()
}
}
assert.Nil(b, db.Close())
}
func BenchmarkGet(b *testing.B) {
db, err := createTestDB(nil)
assert.Nil(b, err)
k := []byte{1}
if err := db.Put(k, make([]byte, 1024)); err != nil {
b.Fail()
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := db.Get(k); err != nil {
b.Fatal()
}
}
assert.Nil(b, db.Close())
}
func BenchmarkGetAppend(b *testing.B) {
db, err := createTestDB(nil)
assert.Nil(b, err)
k := []byte{1}
if err := db.Put(k, make([]byte, 1024)); err != nil {
b.Fail()
}
b.ResetTimer()
b.ReportAllocs()
buf := make([]byte, 0, 1024)
for i := 0; i < b.N; i++ {
value, err := db.GetAppend(k, buf[:0])
if err != nil {
b.Fatal()
}
buf = value
}
assert.Nil(b, db.Close())
}
func BenchmarkBucket_UnmarshalBinary(b *testing.B) {
testBucket := bucket{
slots: [slotsPerBucket]slot{},
}
for i := 0; i < slotsPerBucket; i++ {
testBucket.slots[i].hash = uint32(i)
testBucket.slots[i].keySize = uint16(i + 1)
testBucket.slots[i].valueSize = uint32(i + 17)
}
data, _ := testBucket.MarshalBinary()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tmp := bucket{}
err := tmp.UnmarshalBinary(data)
if err != nil {
b.Fatal()
}
}
}