forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_test.go
192 lines (178 loc) · 4.47 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
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
package nutsdb
import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/xujiajun/utils/time2"
)
var bucket string
const (
fileDir = "/tmp/nutsdb/"
fileDir1 = "/tmp/nutsdb/nutsdb_batch_write1"
fileDir2 = "/tmp/nutsdb/nutsdb_batch_write2"
fileDir3 = "/tmp/nutsdb/nutsdb_batch_write3"
fileDir4 = "/tmp/nutsdb/nutsdb_batch_write4"
fileDir5 = "/tmp/nutsdb/nutsdb_batch_write5"
fileDir6 = "/tmp/nutsdb/nutsdb_batch_write6"
fileDir7 = "/tmp/nutsdb/nutsdb_batch_write7"
fileDir8 = "/tmp/nutsdb/nutsdb_batch_write8"
fileDir9 = "/tmp/nutsdb/nutsdb_batch_write9"
N = 100
)
func init() {
bucket = "bucketForBatchWrite"
files, err := ioutil.ReadDir(fileDir)
if err != nil {
return
}
for _, f := range files {
name := f.Name()
if name != "" {
filePath := fmt.Sprintf("%s/%s", fileDir, name)
err := os.RemoveAll(filePath)
if err != nil {
log.Fatal(err)
}
}
}
}
func TestBatchWrite(t *testing.T) {
TestFlushPanic := func(t *testing.T, db *DB) {
wb, err := db.NewWriteBatch()
require.NoError(t, err)
require.NoError(t, wb.Flush())
require.Error(t, ErrCommitAfterFinish, wb.Flush())
wb, err = db.NewWriteBatch()
require.NoError(t, err)
require.NoError(t, wb.Cancel())
require.Error(t, ErrCommitAfterFinish, wb.Flush())
}
testEmptyWrite := func(t *testing.T, db *DB) {
wb, err := db.NewWriteBatch()
require.NoError(t, err)
require.NoError(t, wb.Flush())
wb, err = db.NewWriteBatch()
require.NoError(t, err)
require.NoError(t, wb.Flush())
wb, err = db.NewWriteBatch()
require.NoError(t, err)
require.NoError(t, wb.Flush())
}
testWrite := func(t *testing.T, db *DB) {
txCreateBucket(t, db, DataStructureBTree, bucket, nil)
key := func(i int) []byte {
return []byte(fmt.Sprintf("%10d", i))
}
val := func(i int) []byte {
return []byte(fmt.Sprintf("%128d", i))
}
wb, err := db.NewWriteBatch()
require.NoError(t, err)
time2.Start()
for i := 0; i < N; i++ {
require.NoError(t, wb.Put(bucket, key(i), val(i), 0))
}
require.NoError(t, wb.Flush())
// fmt.Printf("Time taken via batch write %v keys: %v\n", N, time2.End())
time2.Start()
if err := db.View(
func(tx *Tx) error {
for i := 0; i < N; i++ {
key := key(i)
value, err := tx.Get(bucket, key)
if err != nil {
return err
}
require.Equal(t, val(i), value)
}
return nil
}); err != nil {
log.Println(err)
}
// fmt.Printf("Time taken via db.View %v keys: %v\n", N, time2.End())
// err = wb.Reset()
wb, err = db.NewWriteBatch()
require.NoError(t, err)
time2.Start()
for i := 0; i < N; i++ {
require.NoError(t, wb.Delete(bucket, key(i)))
}
require.NoError(t, wb.Flush())
// fmt.Printf("Time taken via batch delete %v keys: %v\n", N, time2.End())
if err := db.View(
func(tx *Tx) error {
for i := 0; i < N; i++ {
key := key(i)
_, err := tx.Get(bucket, key)
require.Error(t, ErrNotFoundKey, err)
}
return nil
}); err != nil {
log.Println(err)
}
}
dbs := make([]*DB, 6)
dbs[0], _ = Open(
DefaultOptions,
WithDir(fileDir1),
WithEntryIdxMode(HintKeyValAndRAMIdxMode),
)
dbs[1], _ = Open(
DefaultOptions,
WithDir(fileDir2),
WithEntryIdxMode(HintKeyAndRAMIdxMode),
)
dbs[2], _ = Open(
DefaultOptions,
WithDir(fileDir4),
WithEntryIdxMode(HintKeyValAndRAMIdxMode),
WithMaxBatchCount(35),
)
dbs[3], _ = Open(
DefaultOptions,
WithDir(fileDir5),
WithEntryIdxMode(HintKeyAndRAMIdxMode),
WithMaxBatchCount(35),
)
dbs[4], _ = Open(
DefaultOptions,
WithDir(fileDir7),
WithEntryIdxMode(HintKeyValAndRAMIdxMode),
WithMaxBatchSize(20), // change to 1000, unit test is not ok, 1000000 is ok
)
dbs[5], _ = Open(
DefaultOptions,
WithDir(fileDir8),
WithEntryIdxMode(HintKeyAndRAMIdxMode),
WithMaxBatchSize(20),
)
for _, db := range dbs {
require.NotEqual(t, db, nil)
testWrite(t, db)
testEmptyWrite(t, db)
TestFlushPanic(t, db)
}
}
func TestWriteBatch_SetMaxPendingTxns(t *testing.T) {
max := 10
db, err := Open(
DefaultOptions,
WithDir("/tmp"),
)
require.NoError(t, err)
wb, err := db.NewWriteBatch()
require.NoError(t, err)
wb.SetMaxPendingTxns(max)
if wb.throttle == nil {
t.Error("Expected throttle to be initialized, but it was nil")
}
if cap(wb.throttle.ch) != max {
t.Errorf("Expected channel length to be %d, but got %d", max, len(wb.throttle.ch))
}
if cap(wb.throttle.errCh) != max {
t.Errorf("Expected error channel length to be %d, but got %d", max, len(wb.throttle.errCh))
}
}