-
Notifications
You must be signed in to change notification settings - Fork 82
/
leveldb_test.go
359 lines (326 loc) · 9.43 KB
/
leveldb_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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package levigo
import (
"bytes"
"fmt"
"math/rand"
"os"
"path/filepath"
"testing"
"time"
)
func init() {
rand.Seed(int64(time.Now().Nanosecond()))
}
// This testcase is a port of leveldb's c_test.c.
func TestC(t *testing.T) {
if GetLevelDBMajorVersion() <= 0 {
t.Errorf("Major version cannot be less than zero")
}
dbname := tempDir(t)
defer deleteDBDirectory(t, dbname)
env := NewDefaultEnv()
cache := NewLRUCache(1 << 20)
options := NewOptions()
// options.SetComparator(cmp)
options.SetErrorIfExists(true)
options.SetCache(cache)
options.SetEnv(env)
options.SetInfoLog(nil)
options.SetWriteBufferSize(1 << 20)
options.SetParanoidChecks(true)
options.SetMaxOpenFiles(10)
options.SetBlockSize(1024)
options.SetBlockRestartInterval(8)
options.SetCompression(NoCompression)
roptions := NewReadOptions()
roptions.SetVerifyChecksums(true)
roptions.SetFillCache(false)
woptions := NewWriteOptions()
woptions.SetSync(true)
_ = DestroyDatabase(dbname, options)
db, err := Open(dbname, options)
if err == nil {
t.Errorf("Open on missing db should have failed")
}
options.SetCreateIfMissing(true)
db, err = Open(dbname, options)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
putKey := []byte("foo")
putValue := []byte("hello")
err = db.Put(woptions, putKey, putValue)
if err != nil {
t.Errorf("Put failed: %v", err)
}
CheckGet(t, "after Put", db, roptions, putKey, putValue)
wb := NewWriteBatch()
wb.Put([]byte("foo"), []byte("a"))
wb.Clear()
wb.Put([]byte("bar"), []byte("b"))
wb.Put([]byte("box"), []byte("c"))
wb.Delete([]byte("bar"))
err = db.Write(woptions, wb)
if err != nil {
t.Errorf("Write batch failed: %v", err)
}
CheckGet(t, "after WriteBatch", db, roptions, []byte("foo"), []byte("hello"))
CheckGet(t, "after WriteBatch", db, roptions, []byte("bar"), nil)
CheckGet(t, "after WriteBatch", db, roptions, []byte("box"), []byte("c"))
// TODO: WriteBatch iteration isn't easy. Suffers same problems as
// Comparator.
// wbiter := &TestWBIter{t: t}
// wb.Iterate(wbiter)
// if wbiter.pos != 3 {
// t.Errorf("After Iterate, on the wrong pos: %d", wbiter.pos)
// }
wb.Close()
iter := db.NewIterator(roptions)
if iter.Valid() {
t.Errorf("Read iterator should not be valid, yet")
}
iter.SeekToFirst()
if !iter.Valid() {
t.Errorf("Read iterator should be valid after seeking to first record")
}
CheckIter(t, iter, []byte("box"), []byte("c"))
iter.Next()
CheckIter(t, iter, []byte("foo"), []byte("hello"))
iter.Prev()
CheckIter(t, iter, []byte("box"), []byte("c"))
iter.Prev()
if iter.Valid() {
t.Errorf("Read iterator should not be valid after go back past the first record")
}
iter.SeekToLast()
CheckIter(t, iter, []byte("foo"), []byte("hello"))
iter.Seek([]byte("b"))
CheckIter(t, iter, []byte("box"), []byte("c"))
if iter.GetError() != nil {
t.Errorf("Read iterator has an error we didn't expect: %v", iter.GetError())
}
iter.Close()
// approximate sizes
n := 20000
for i := 0; i < n; i++ {
keybuf := []byte(fmt.Sprintf("k%020d", i))
valbuf := []byte(fmt.Sprintf("v%020d", i))
err := db.Put(woptions, keybuf, valbuf)
if err != nil {
t.Errorf("Put error in approximate size test: %v", err)
}
}
ranges := []Range{
{[]byte("a"), []byte("k00000000000000010000")},
{[]byte("k00000000000000010000"), []byte("z")},
}
sizes := db.GetApproximateSizes(ranges)
if len(sizes) == 2 {
if sizes[0] <= 0 {
t.Errorf("First size range was %d", sizes[0])
}
if sizes[1] <= 0 {
t.Errorf("Second size range was %d", sizes[1])
}
} else {
t.Errorf("Expected 2 approx. sizes back, got %d", len(sizes))
}
// property
prop := db.PropertyValue("nosuchprop")
if prop != "" {
t.Errorf("property nosuchprop should not have a value")
}
prop = db.PropertyValue("leveldb.stats")
if prop == "" {
t.Errorf("property leveldb.stats should have a value")
}
// snapshot
snap := db.NewSnapshot()
err = db.Delete(woptions, []byte("foo"))
if err != nil {
t.Errorf("Delete during snapshot test errored: %v", err)
}
roptions.SetSnapshot(snap)
CheckGet(t, "from snapshot", db, roptions, []byte("foo"), []byte("hello"))
roptions.SetSnapshot(nil)
CheckGet(t, "from snapshot", db, roptions, []byte("foo"), nil)
db.ReleaseSnapshot(snap)
// repair
db.Close()
options.SetCreateIfMissing(false)
options.SetErrorIfExists(false)
err = RepairDatabase(dbname, options)
if err != nil {
t.Errorf("Repairing db failed: %v", err)
}
db, err = Open(dbname, options)
if err != nil {
t.Errorf("Unable to open repaired db: %v", err)
}
CheckGet(t, "repair", db, roptions, []byte("foo"), nil)
CheckGet(t, "repair", db, roptions, []byte("bar"), nil)
CheckGet(t, "repair", db, roptions, []byte("box"), []byte("c"))
options.SetCreateIfMissing(true)
options.SetErrorIfExists(true)
// filter
policy := NewBloomFilter(10)
db.Close()
DestroyDatabase(dbname, options)
options.SetFilterPolicy(policy)
db, err = Open(dbname, options)
if err != nil {
t.Fatalf("Unable to recreate db for filter tests: %v", err)
}
err = db.Put(woptions, []byte("foo"), []byte("foovalue"))
if err != nil {
t.Errorf("Unable to put 'foo' with filter: %v", err)
}
err = db.Put(woptions, []byte("bar"), []byte("barvalue"))
if err != nil {
t.Errorf("Unable to put 'bar' with filter: %v", err)
}
db.CompactRange(Range{nil, nil})
CheckGet(t, "filter", db, roptions, []byte("foo"), []byte("foovalue"))
CheckGet(t, "filter", db, roptions, []byte("bar"), []byte("barvalue"))
options.SetFilterPolicy(nil)
policy.Close()
// cleanup
db.Close()
options.Close()
roptions.Close()
woptions.Close()
cache.Close()
// DestroyComparator(cmp)
env.Close()
}
func TestNilSlicesInDb(t *testing.T) {
dbname := tempDir(t)
defer deleteDBDirectory(t, dbname)
options := NewOptions()
options.SetErrorIfExists(true)
options.SetCreateIfMissing(true)
ro := NewReadOptions()
_ = DestroyDatabase(dbname, options)
db, err := Open(dbname, options)
if err != nil {
t.Fatalf("Database could not be opened: %v", err)
}
defer db.Close()
val, err := db.Get(ro, []byte("missing"))
if err != nil {
t.Errorf("Get failed: %v", err)
}
if val != nil {
t.Errorf("A key not in the db should return nil, not %v", val)
}
wo := NewWriteOptions()
db.Put(wo, nil, []byte("love"))
val, err = db.Get(ro, nil)
if !bytes.Equal([]byte("love"), val) {
t.Errorf("Get should see the nil key: %v", val)
}
val, err = db.Get(ro, []byte{})
if !bytes.Equal([]byte("love"), val) {
t.Errorf("Get shouldn't distinguish between nil key and empty slice key: %v", val)
}
err = db.Put(wo, []byte("nilvalue"), nil)
if err != nil {
t.Errorf("nil value Put errored: %v", err)
}
// Compare with the []byte("missing") case. We expect Get to return a
// []byte{} here, but expect a nil returned there.
CheckGet(t, "nil value Put", db, ro, []byte("nilvalue"), []byte{})
err = db.Put(wo, []byte("emptyvalue"), []byte{})
if err != nil {
t.Errorf("empty value Put errored: %v", err)
}
CheckGet(t, "empty value Put", db, ro, []byte("emptyvalue"), []byte{})
err = db.Delete(wo, nil)
if err != nil {
t.Errorf("nil key Delete errored: %v", err)
}
err = db.Delete(wo, []byte{})
if err != nil {
t.Errorf("empty slice key Delete errored: %v", err)
}
}
func TestIterationValidityLimits(t *testing.T) {
dbname := tempDir(t)
defer deleteDBDirectory(t, dbname)
options := NewOptions()
options.SetErrorIfExists(true)
options.SetCreateIfMissing(true)
ro := NewReadOptions()
wo := NewWriteOptions()
_ = DestroyDatabase(dbname, options)
db, err := Open(dbname, options)
if err != nil {
t.Fatalf("Database could not be opened: %v", err)
}
defer db.Close()
db.Put(wo, []byte("bat"), []byte("somedata"))
db.Put(wo, []byte("done"), []byte("somedata"))
it := db.NewIterator(ro)
defer it.Close()
if it.Valid() {
t.Errorf("new Iterator was valid")
}
it.Seek([]byte("bat"))
if !it.Valid() {
t.Errorf("Seek to %#v failed.", []byte("bat"))
}
if !bytes.Equal([]byte("bat"), it.Key()) {
t.Errorf("did not seek to []byte(\"bat\")")
}
key := it.Key()
it.Next()
if bytes.Equal(key, it.Key()) {
t.Errorf("key should be a copy of last key")
}
it.Next()
if it.Valid() {
t.Errorf("iterating off the db should result in an invalid iterator")
}
err = it.GetError()
if err != nil {
t.Errorf("should not have seen an error on an invalid iterator")
}
it.Seek([]byte("bat"))
if !it.Valid() {
t.Errorf("Iterator should be valid again")
}
}
func CheckGet(t *testing.T, where string, db *DB, roptions *ReadOptions, key, expected []byte) {
getValue, err := db.Get(roptions, key)
if err != nil {
t.Errorf("%s, Get failed: %v", where, err)
}
if !bytes.Equal(getValue, expected) {
t.Errorf("%s, expected Get value %v, got %v", where, expected, getValue)
}
}
func WBIterCheckEqual(t *testing.T, where string, which string, pos int, expected, given []byte) {
if !bytes.Equal(expected, given) {
t.Errorf("%s at pos %d, %s expected: %v, got: %v", where, pos, which, expected, given)
}
}
func CheckIter(t *testing.T, it *Iterator, key, value []byte) {
if !bytes.Equal(key, it.Key()) {
t.Errorf("Iterator: expected key %v, got %v", key, it.Key())
}
if !bytes.Equal(value, it.Value()) {
t.Errorf("Iterator: expected value %v, got %v", value, it.Value())
}
}
func deleteDBDirectory(t *testing.T, dirPath string) {
err := os.RemoveAll(dirPath)
if err != nil {
t.Errorf("Unable to remove database directory: %s", dirPath)
}
}
func tempDir(t *testing.T) string {
bottom := fmt.Sprintf("levigo-test-%d", rand.Int())
path := filepath.Join(os.TempDir(), bottom)
deleteDBDirectory(t, path)
return path
}