-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_test.go
167 lines (141 loc) · 3.72 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
package yojoudb
import (
"github.com/berylyvos/yojoudb/utils"
"github.com/stretchr/testify/assert"
"testing"
)
func TestBatch_Put_Normal(t *testing.T) {
// value 128B
batchPutAndIterate(t, 1*GB, 10000, 128)
// value 1KB
batchPutAndIterate(t, 1*GB, 10000, KB)
// value 32KB
batchPutAndIterate(t, 1*GB, 1000, 32*KB)
}
func TestBatch_Put_IncrSegmentFile(t *testing.T) {
batchPutAndIterate(t, 64*MB, 5000, 32*KB)
}
func TestBatch_Get_Normal(t *testing.T) {
options := DefaultOptions
db, err := Open(options)
assert.Nil(t, err)
defer destroyDB(db)
batch1 := db.NewBatch(DefaultBatchOptions)
err = batch1.Put(utils.TestKey(12), utils.RandValue(128))
assert.Nil(t, err)
val1, err := batch1.Get(utils.TestKey(12))
assert.Nil(t, err)
assert.NotNil(t, val1)
_ = batch1.Commit()
generateData(t, db, 400, 500, 4*KB)
batch2 := db.NewBatch(DefaultBatchOptions)
err = batch2.Delete(utils.TestKey(450))
assert.Nil(t, err)
val, err := batch2.Get(utils.TestKey(450))
assert.Nil(t, val)
assert.Equal(t, ErrKeyNotFound, err)
_ = batch2.Commit()
// reopen
_ = db.Close()
db2, err := Open(options)
assert.Nil(t, err)
defer func() {
_ = db2.Close()
}()
assertKeyExistOrNot(t, db2, utils.TestKey(12), true)
assertKeyExistOrNot(t, db2, utils.TestKey(450), false)
}
func TestBatch_Delete_Normal(t *testing.T) {
options := DefaultOptions
db, err := Open(options)
assert.Nil(t, err)
defer destroyDB(db)
err = db.Delete([]byte("not exist"))
assert.Nil(t, err)
generateData(t, db, 1, 100, 128)
err = db.Delete(utils.TestKey(99))
assert.Nil(t, err)
exist, err := db.Exist(utils.TestKey(99))
assert.Nil(t, err)
assert.False(t, exist)
batch := db.NewBatch(DefaultBatchOptions)
err = batch.Put(utils.TestKey(200), utils.RandValue(100))
assert.Nil(t, err)
err = batch.Delete(utils.TestKey(200))
assert.Nil(t, err)
exist1, err := batch.Exist(utils.TestKey(200))
assert.Nil(t, err)
assert.False(t, exist1)
_ = batch.Commit()
// reopen
_ = db.Close()
db2, err := Open(options)
assert.Nil(t, err)
defer func() {
_ = db2.Close()
}()
assertKeyExistOrNot(t, db2, utils.TestKey(200), false)
}
func TestBatch_Rollback(t *testing.T) {
options := DefaultOptions
db, err := Open(options)
assert.Nil(t, err)
defer destroyDB(db)
key := []byte("shingo")
value := []byte("gnix")
batch := db.NewBatch(DefaultBatchOptions)
err = batch.Put(key, value)
assert.Nil(t, err)
err = batch.Rollback()
assert.Nil(t, err)
resp, err := db.Get(key)
assert.Equal(t, ErrKeyNotFound, err)
assert.Empty(t, resp)
}
func batchPutAndIterate(t *testing.T, segmentSize int64, size int, valueLen int) {
options := DefaultOptions
options.SegmentSize = segmentSize
db, err := Open(options)
assert.Nil(t, err)
defer destroyDB(db)
batch := db.NewBatch(BatchOptions{})
for i := 0; i < size; i++ {
err := batch.Put(utils.TestKey(i), utils.RandValue(valueLen))
assert.Nil(t, err)
}
err = batch.Commit()
assert.Nil(t, err)
for i := 0; i < size; i++ {
value, err := db.Get(utils.TestKey(i))
assert.Nil(t, err)
assert.Equal(t, len(utils.RandValue(valueLen)), len(value))
}
// reopen
_ = db.Close()
db2, err := Open(options)
assert.Nil(t, err)
defer func() {
_ = db2.Close()
}()
for i := 0; i < size; i++ {
value, err := db2.Get(utils.TestKey(i))
assert.Nil(t, err)
assert.Equal(t, len(utils.RandValue(valueLen)), len(value))
}
}
func generateData(t *testing.T, db *DB, start, end int, valueLen int) {
for ; start < end; start++ {
err := db.Put(utils.TestKey(start), utils.RandValue(valueLen))
assert.Nil(t, err)
}
}
func assertKeyExistOrNot(t *testing.T, db *DB, key []byte, exist bool) {
val, err := db.Get(key)
if exist {
assert.Nil(t, err)
assert.NotNil(t, val)
} else {
assert.Nil(t, val)
assert.Equal(t, ErrKeyNotFound, err)
}
}