forked from lotusdblabs/lotusdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
465 lines (420 loc) · 10.2 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
package lotusdb
import (
"bytes"
"fmt"
"github.com/flower-corp/lotusdb/logger"
"math/rand"
"os"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestOpen(t *testing.T) {
opendb := func(opts Options) {
db, err := Open(opts)
defer destroyDB(db)
assert.Nil(t, err)
}
opts := DefaultOptions("/tmp" + separator + "lotusdb")
t.Run("default", func(t *testing.T) {
opendb(opts)
})
t.Run("spec-dir", func(t *testing.T) {
dir := "/tmp" + separator + "new-dir"
opts.CfOpts.IndexerDir = dir
opts.CfOpts.ValueLogDir = dir
opendb(opts)
_ = os.RemoveAll(dir)
})
t.Run("no-cf-name", func(t *testing.T) {
opts.CfOpts.CfName = ""
opendb(opts)
})
}
func TestLotusDB_Put(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
type fields struct {
db *LotusDB
}
type args struct {
key []byte
value []byte
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
"nil-key-val", fields{db: db}, args{key: nil, value: nil}, false,
},
{
"nil-key", fields{db: db}, args{key: nil, value: GetValue16B()}, false,
},
{
"nil-val", fields{db: db}, args{key: GetKey(4423), value: nil}, false,
},
{
"with-key-val", fields{db: db}, args{key: GetKey(990), value: GetValue16B()}, false,
},
{
"with-key-big-val", fields{db: db}, args{key: GetKey(44012), value: GetValue4K()}, false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.fields.db.Put(tt.args.key, tt.args.value); (err != nil) != tt.wantErr {
t.Errorf("Put() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestLotusDB_PutWithOptions(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
type fields struct {
db *LotusDB
}
type args struct {
key []byte
value []byte
opt *WriteOptions
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
"nil-options", fields{db: db}, args{key: GetKey(13), value: GetValue128B(), opt: nil}, false,
},
{
"with-sync", fields{db: db}, args{key: GetKey(99832), value: GetValue128B(), opt: &WriteOptions{Sync: true}}, false,
},
{
"with-disableWAL", fields{db: db}, args{key: GetKey(54221), value: GetValue128B(), opt: &WriteOptions{DisableWal: true}}, false,
},
{
"with-ttl", fields{db: db}, args{key: GetKey(9901), value: GetValue128B(), opt: &WriteOptions{ExpiredAt: time.Now().Add(time.Minute).Unix()}}, false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := tt.fields.db
if err := db.PutWithOptions(tt.args.key, tt.args.value, tt.args.opt); (err != nil) != tt.wantErr {
t.Errorf("PutWithOptions() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
// We will put data until the active memtable is full and be flushed.
// Then a new active memtable will be created.
func TestLotusDB_PutUntilMemtableFlush(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
// if you change the default memtable size, change the writeCount too.
// make sure the written data size is greater than memtable size.
opts.CfOpts.MemtableSize = 64 << 20
writeCount := 600000
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
for i := 0; i <= writeCount; i++ {
err := db.Put(GetKey(i), GetValue128B())
assert.Nil(t, err)
}
// make sure all data are written.
v1, err := db.Get(GetKey(0))
assert.Nil(t, err)
assert.Equal(t, len(v1), 128)
v2, err := db.Get(GetKey(writeCount))
assert.Nil(t, err)
assert.Equal(t, len(v2), 128)
}
func TestLotusDB_Get(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
// write some data for getting
for i := 0; i < 100; i++ {
err := db.Put(GetKey(i), GetValue16B())
if i == 43 {
err := db.Put(GetKey(i), []byte("lotusdb"))
assert.Nil(t, err)
}
assert.Nil(t, err)
}
type fields struct {
db *LotusDB
}
type args struct {
key []byte
}
tests := []struct {
name string
fields fields
args args
want []byte
wantErr bool
}{
{
"nil", fields{db: db}, args{key: nil}, nil, false,
},
{
"not-exist", fields{db: db}, args{key: GetKey(9903)}, nil, false,
},
{
"get-from-memtable", fields{db: db}, args{key: GetKey(43)}, []byte{108, 111, 116, 117, 115, 100, 98}, false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := tt.fields.db
got, err := db.Get(tt.args.key)
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Get() got = %v, want %v", got, tt.want)
}
})
}
}
func TestLotusDB_GetKeyFromIndexerAndValFromVLog(t *testing.T) {
testGetKV(t)
}
func testGetKV(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
var writeCount = 600000
for i := 0; i <= writeCount; i++ {
err := db.Put(GetKey(i), GetValue128B())
assert.Nil(t, err)
}
keys := [][]byte{
GetKey(0),
GetKey(9230),
GetKey(77842),
GetKey(200000),
GetKey(writeCount),
}
for _, tt := range keys {
v, err := db.Get(tt)
assert.Nil(t, err)
assert.Equal(t, len(v), 128)
}
}
func TestLotusDB_Delete(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
var writeCount = 100
// write some data.
for i := 0; i <= writeCount; i++ {
err := db.Put(GetKey(i), GetValue16B())
if i == 32 {
err := db.Put(GetKey(i), []byte("lotusdb"))
assert.Nil(t, err)
}
assert.Nil(t, err)
}
type fields struct {
db *LotusDB
}
type args struct {
key []byte
}
tests := []struct {
name string
fields fields
args args
wantErr bool
nilValue bool
}{
{
"nil", fields{db: db}, args{key: nil}, false, false,
},
{
"not-existed-key", fields{db: db}, args{key: []byte("not-exist")}, false, false,
},
// "existed-key-1" and "existed-key-2" are suitable for deleting keys before flush.
{
"existed-key-1", fields{db: db}, args{key: GetKey(0)}, false, true,
},
{
"existed-key-2", fields{db: db}, args{key: GetKey(writeCount)}, false, true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := tt.fields.db
if err := db.Delete(tt.args.key); (err != nil) != tt.wantErr {
t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.nilValue {
val, err := db.Get(tt.args.key)
assert.Nil(t, err)
if len(val) != 0 {
t.Errorf("Delete() val = %v, want a nil value", val)
}
}
})
}
}
func TestLotusDB_DeleteAfterFlush(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
// write enough data that can trigger flush operation.
var writeCount = 600000
for i := 0; i <= writeCount; i++ {
err := db.Put(GetKey(i), GetValue128B())
if i == 32 {
err := db.Put(GetKey(i), []byte("lotusdb"))
assert.Nil(t, err)
}
assert.Nil(t, err)
}
type fields struct {
db *LotusDB
}
type args struct {
key []byte
}
tests := []struct {
name string
fields fields
args args
wantErr bool
nilValue bool
}{
{
"after-flush-1", fields{db: db}, args{key: GetKey(0)}, false, true,
},
{
"after-flush-2", fields{db: db}, args{key: GetKey(200000)}, false, true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := tt.fields.db
if err := db.Delete(tt.args.key); (err != nil) != tt.wantErr {
t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.nilValue {
val, err := db.Get(tt.args.key)
assert.Nil(t, err)
if len(val) != 0 {
t.Errorf("Delete() val = %v, want a nil value", val)
}
}
})
}
}
func TestLotusDB_SyncAndClose(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
// write some data.
var writeCount = 600000
for i := 0; i <= writeCount; i++ {
err := db.Put(GetKey(i), GetValue128B())
assert.Nil(t, err)
}
err = db.Sync()
assert.Nil(t, err)
err = db.Close()
assert.Nil(t, err)
}
// write some data and reopen it.
func TestReOpenDB(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
// write some data.
var writeCount = 600000
for i := 0; i <= writeCount; i++ {
err := db.Put(GetKey(i), GetValue128B())
assert.Nil(t, err)
}
err = db.Close()
assert.Nil(t, err)
// reopen db.
db2, err := Open(opts)
assert.Nil(t, err)
// make sure all writes are valid.
v1, err := db2.Get(GetKey(0))
assert.Nil(t, err)
assert.NotNil(t, v1)
v2, err := db2.Get(GetKey(writeCount))
assert.Nil(t, err)
assert.NotNil(t, v2)
}
func TestBytesFlush(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
opts.CfOpts.WalBytesFlush = 200
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
for i := 0; i < 10; i++ {
err := db.Put(GetKey(i), GetValue128B())
assert.Nil(t, err)
}
}
func destroyDB(db *LotusDB) {
if db != nil {
if err := os.RemoveAll(db.opts.DBPath); err != nil {
logger.Errorf("remove db path err.%v", err)
}
if err := os.RemoveAll(db.opts.CfOpts.IndexerDir); err != nil {
logger.Errorf("remove indexer path err.%v", err)
}
if err := os.RemoveAll(db.opts.CfOpts.ValueLogDir); err != nil {
logger.Errorf("remove vlog path err.%v", err)
}
}
}
const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
func init() {
rand.Seed(time.Now().Unix())
}
// GetKey length: 32 Bytes
func GetKey(n int) []byte {
return []byte("kvstore-bench-key------" + fmt.Sprintf("%09d", n))
}
func GetValue16B() []byte {
var str bytes.Buffer
for i := 0; i < 16; i++ {
str.WriteByte(alphabet[rand.Int()%36])
}
return []byte(str.String())
}
func GetValue128B() []byte {
var str bytes.Buffer
for i := 0; i < 128; i++ {
str.WriteByte(alphabet[rand.Int()%36])
}
return []byte(str.String())
}
func GetValue4K() []byte {
var str bytes.Buffer
for i := 0; i < 4096; i++ {
str.WriteByte(alphabet[rand.Int()%36])
}
return []byte(str.String())
}