forked from ByteStorage/FlyDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_test.go
110 lines (88 loc) · 2.37 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
package flydb
import (
"github.com/qishenonly/flydb/utils"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
func TestDB_WriteBatch(t *testing.T) {
opts := DefaultOptions
dir, _ := os.MkdirTemp("", "flydb-batch-1")
opts.DirPath = dir
db, err := NewFlyDB(opts)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
// 写数据之后不提交
wb := db.NewWriteBatch(DefaultWriteBatchOptions)
err = wb.Put(utils.GetTestKey(1), utils.RandomValue(10))
assert.Nil(t, err)
err = wb.Delete(utils.GetTestKey(2))
assert.Nil(t, err)
_, err = db.Get(utils.GetTestKey(1))
assert.Equal(t, ErrKeyNotFound, err)
// 正常提交数据
err = wb.Commit()
assert.Nil(t, err)
val, err := db.Get(utils.GetTestKey(1))
assert.NotNil(t, val)
assert.Nil(t, err)
wb2 := db.NewWriteBatch(DefaultWriteBatchOptions)
err = wb2.Delete(utils.GetTestKey(1))
assert.Nil(t, err)
err = wb2.Commit()
assert.Nil(t, err)
_, err = db.Get(utils.GetTestKey(1))
assert.Equal(t, ErrKeyNotFound, err)
}
func TestDB_WriteBatchRestart(t *testing.T) {
opts := DefaultOptions
dir, _ := os.MkdirTemp("", "flydb-batch-2")
opts.DirPath = dir
db, err := NewFlyDB(opts)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
err = db.Put(utils.GetTestKey(1), utils.RandomValue(10))
assert.Nil(t, err)
wb := db.NewWriteBatch(DefaultWriteBatchOptions)
err = wb.Put(utils.GetTestKey(2), utils.RandomValue(10))
assert.Nil(t, err)
err = wb.Delete(utils.GetTestKey(1))
assert.Nil(t, err)
err = wb.Commit()
assert.Nil(t, err)
err = wb.Put(utils.GetTestKey(3), utils.RandomValue(10))
assert.Nil(t, err)
err = wb.Commit()
assert.Nil(t, err)
// 重启
err = db.Close()
assert.Nil(t, err)
db2, err := NewFlyDB(opts)
assert.Nil(t, err)
_, err = db2.Get(utils.GetTestKey(1))
assert.Equal(t, ErrKeyNotFound, err)
// 判断事务序列号
assert.Equal(t, uint64(2), db.transSeqNo)
}
func TestDB_WriteBatch1(t *testing.T) {
opts := DefaultOptions
dir := "/tmp/batch-3"
opts.DirPath = dir
db, err := NewFlyDB(opts)
assert.Nil(t, err)
assert.NotNil(t, db)
//keys := db.GetListKeys()
//t.Log(len(keys))
// 批量提交中间手动停止
wbopt := DefaultWriteBatchOptions
wbopt.MaxBatchNum = 1000000
wb := db.NewWriteBatch(wbopt)
for i := 0; i < 500000; i++ {
err = wb.Put(utils.GetTestKey(i), utils.RandomValue(1024))
assert.Nil(t, err)
}
err = wb.Commit()
assert.Nil(t, err)
}