-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
233 lines (224 loc) · 4.71 KB
/
writer_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
package cdb_test
import (
"bytes"
"fmt"
"github.com/orestonce/cdb"
"io/ioutil"
"os"
"strconv"
"testing"
)
func TestNewMemoryWriter(t *testing.T) {
writer := cdb.NewMemoryWriter()
err := writer.WriteKeyValue([]byte("key"), []byte("value"))
if err != nil {
panic(err)
}
db, err := writer.Freeze()
if err != nil {
panic(err)
}
it := db.BeginFindKey([]byte("key"))
_, value, err := it.ReadNextKeyValue()
if err != nil {
panic(err)
}
if string(value) != `value` {
panic("value check failed : " + string(value))
}
_, _, err = it.ReadNextKeyValue()
if err != cdb.ErrNoData {
panic("expect ErrNoData")
}
}
func TestNewMemoryWriter2(t *testing.T) {
writer := cdb.NewMemoryWriter()
err := writer.WriteKeyValue([]byte("ke"), []byte("value"))
if err != nil {
panic(err)
}
bn := writer.GetByteNum()
if bn != 2048+(2+5)+1*24 {
panic("unexpected ByteNum: " + strconv.Itoa(int(bn)))
}
data, err := writer.GetMemoryWriterBytes()
if err != nil {
panic(err)
}
if len(data) != int(bn) {
panic("invalid data length: " + strconv.Itoa(len(data)) + ", " + strconv.Itoa(int(bn)))
}
bn2 := writer.GetByteNum()
if bn != bn2 {
panic("invalid bn2: " + strconv.Itoa(int(bn2)))
}
}
func TestNewMemoryWriter3(t *testing.T) {
writer := cdb.NewMemoryWriter()
err := writer.WriteKeyValue([]byte("key"), []byte("v"))
if err != nil {
panic(err)
}
db, err := writer.Freeze()
if err != nil {
panic(err)
}
value, err := db.GetValue([]byte("key"))
if err != nil {
panic(err)
}
if !bytes.Equal(value, []byte("v")) {
panic("value not equal " + string(value))
}
err = writer.Close()
if err != nil {
panic(err)
}
}
func TestNewMemoryWriter4(t *testing.T) {
builder := cdb.NewMemoryWriter()
db, err := builder.Freeze()
if err != nil {
panic(err)
}
num := db.GetRecordNum()
if num != 0 {
panic("expected 0 num: " + strconv.Itoa(num))
}
it := db.BeginIterator()
if _, _, err = it.ReadNextKeyValue(); err != cdb.ErrNoData {
panic("expect !it.HasNext()")
}
err = builder.Close()
if err != nil {
panic(err)
}
}
var keyValueMap = map[string][]string{
"": {""},
"k1": {"value1", "value2"},
"user": {"password", "password9"},
"data": {"origin", "data", "run", "origin", "data2"},
}
func TestNewMemoryWriter5(t *testing.T) {
memoryWriter := cdb.NewMemoryWriter()
for key, valueList := range keyValueMap {
for _, value := range valueList {
err := memoryWriter.WriteKeyValue([]byte(key), []byte(value))
if err != nil {
panic(err)
}
}
}
db, err := memoryWriter.Freeze()
if err != nil {
panic(err)
}
assertDbEqual(db)
err = memoryWriter.Close()
if err != nil {
panic(err)
}
const testFileName = "testdata/TestNewMemoryWriter5.cdb"
data, err := memoryWriter.GetMemoryWriterBytes()
if err != nil {
panic(err)
}
err = ioutil.WriteFile(testFileName, data, 0777)
if err != nil {
panic(err)
}
db, err = cdb.OpenFile(testFileName)
if err != nil {
panic(err)
}
dbValue1, err := db.GetValue([]byte("k1"))
if err != nil {
panic(err)
}
if !bytes.Equal(dbValue1, []byte("value1")) {
panic("expect value1 " + string(dbValue1))
}
err = db.Close()
if err != nil {
panic(err)
}
err = os.Remove(testFileName)
if err != nil {
panic(err)
}
}
func TestNewFileWriter(t *testing.T) {
const testFileName = "testdata/TestNewFileWriter.cdb"
fileWriter, err := cdb.NewFileWriter(testFileName)
if err != nil {
panic(err)
}
for key, valueList := range keyValueMap {
for _, value := range valueList {
err = fileWriter.WriteKeyValue([]byte(key), []byte(value))
if err != nil {
panic(err)
}
}
}
err = fileWriter.Close()
if err != nil {
panic(err)
}
db, err := cdb.OpenFile(testFileName)
if err != nil {
panic(err)
}
assertDbEqual(db)
err = db.Close()
if err != nil {
panic(err)
}
err = os.Remove(testFileName)
if err != nil {
panic(err)
}
}
func assertDbEqual(db *cdb.Cdb) {
for key, valueList := range keyValueMap {
it := db.BeginFindKey([]byte(key))
for idx, value := range valueList {
_, dbValue, err := it.ReadNextKeyValue()
if err != nil {
panic(err)
}
if !bytes.Equal(dbValue, []byte(value)) {
panic("value not equal " + key + " " + strconv.Itoa(idx))
}
}
_, _, err := it.ReadNextKeyValue()
if err != cdb.ErrNoData {
panic(fmt.Sprint("unexpected error ", err))
}
}
it := db.BeginIterator()
for {
dbKey, dbValue, err := it.ReadNextKeyValue()
if err == cdb.ErrNoData {
break
}
if err != nil {
panic(err)
}
valueList, ok := keyValueMap[string(dbKey)]
if !ok {
panic("memory not exists " + string(dbKey))
}
existsValue := false
for _, v := range valueList {
if v == string(dbValue) {
existsValue = true
break
}
}
if !existsValue {
panic("memory not exists2 " + string(dbKey))
}
}
}