forked from ByteStorage/FlyDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_test.go
100 lines (85 loc) · 1.9 KB
/
merge_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
package flydb
import (
"github.com/qishenonly/flydb/utils"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
// 没有任何数据的情况下进行 merge
func TestDB_Merge(t *testing.T) {
opts := DefaultOptions
dir, _ := os.MkdirTemp("", "flydb-merge-1")
opts.DirPath = dir
db, err := Open(opts)
//defer destroyDB(db)
//assert.Nil(t, err)
t.Log(err)
//assert.NotNil(t, db)
err = db.Merge()
t.Log(err)
//assert.Nil(t, err)
}
// 全部都是有效的数据
func TestDB_Merge2(t *testing.T) {
opts := DefaultOptions
dir, _ := os.MkdirTemp("", "flydb-merge-2")
opts.DataFileSize = 32 * 1024 * 1024
opts.DirPath = dir
db, err := Open(opts)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
for i := 0; i < 50000; i++ {
err := db.Put(utils.GetTestKey(i), utils.RandomValue(1024))
assert.Nil(t, err)
}
err = db.Merge()
t.Log(err)
assert.Nil(t, err)
// 重启校验
err = db.Close()
assert.Nil(t, err)
db2, err := Open(opts)
defer func() {
_ = db2.Close()
}()
assert.Nil(t, err)
keys := db2.GetListKeys()
assert.Equal(t, 50000, len(keys))
for i := 0; i < 50000; i++ {
val, err := db2.Get(utils.GetTestKey(i))
assert.Nil(t, err)
assert.NotNil(t, val)
}
}
// 全部是无效的数据
func TestDB_Merge3(t *testing.T) {
opts := DefaultOptions
dir, _ := os.MkdirTemp("", "flydb-merge-3")
opts.DataFileSize = 32 * 1024 * 1024
opts.DirPath = dir
db, err := Open(opts)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
for i := 0; i < 50000; i++ {
err := db.Put(utils.GetTestKey(i), utils.RandomValue(1024))
assert.Nil(t, err)
}
for i := 0; i < 50000; i++ {
err := db.Delete(utils.GetTestKey(i))
assert.Nil(t, err)
}
err = db.Merge()
assert.Nil(t, err)
// 重启校验
err = db.Close()
assert.Nil(t, err)
db2, err := Open(opts)
defer func() {
_ = db2.Close()
}()
assert.Nil(t, err)
keys := db2.GetListKeys()
assert.Equal(t, 0, len(keys))
}