This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
leveldb_test.go
131 lines (127 loc) · 3.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
package main
import (
"github.com/pingcap/goleveldb/leveldb"
"reflect"
"testing"
)
var keys = []string{
"aaaq325hdjjhsf",
"bbbczvafagsheerqatw",
"cccafshdgahs",
"d1122233aaa",
"e223344444",
}
var values = []string{
"111tw6u6i7jybeahtnyrjum",
"222hansyjeww",
"3334523473562374837564q623748",
"aaaaaaaaaaaaaa",
"bbbbbbbbbbbb",
}
func TestNewLeveldbStorage(t *testing.T) {
db, err := NewLeveldbStorage("test/store/new")
defer db.Close()
perr(err)
db.Put([]byte("aa"), []byte("111"), nil)
v, err := db.Get([]byte("aa"), nil)
perr(err)
s := string(v)
if s != "111" {
t.Fatalf("expect 111 but get %s", s)
}
}
func TestLeveldbStorage_Save(t *testing.T) {
db, err := NewLeveldbStorage("test/store/save")
perr(err)
for i := range keys {
err := db.Save([]byte(keys[i]), []byte(values[i]))
perr(err)
}
db.Close()
newDb, newErr := leveldb.OpenFile("test/store/save", nil)
defer newDb.Close()
perr(newErr)
iter := newDb.NewIterator(nil, nil)
i := 0
for iter.Next() {
if string(iter.Key()) != keys[i] {
t.Fatalf("expect key %s but get %s", keys[i], string(iter.Key()))
} else if string(iter.Value()) != values[i] {
t.Fatalf("expect value %s but get %s", values[i], string(iter.Value()))
}
i++
}
}
func TestLeveldbStorage_Load(t *testing.T) {
db, err := leveldb.OpenFile("test/store/save", nil)
perr(err)
for i := range keys {
err := db.Put([]byte(keys[i]), []byte(values[i]), nil)
perr(err)
}
db.Close()
newDb, newErr := NewLeveldbStorage("test/store/save")
defer newDb.Close()
perr(newErr)
v, newErrLoad := newDb.Load([]byte(keys[0]))
perr(newErrLoad)
if v != values[0] {
t.Fatalf("expect %s but get %s", values[0], v)
}
v, err = newDb.Load([]byte("aa"))
if err == nil {
t.Fatalf("aa should not be found, expect error but get none")
}
}
func TestLeveldb_Search(t *testing.T) {
db, err := leveldb.OpenFile("test/store/search", nil)
perr(err)
for i := range keys {
err := db.Put([]byte(keys[i]), []byte(values[i]), nil)
perr(err)
}
db.Close()
newDb, newErr := NewLeveldbStorage("test/store/save")
defer newDb.Close()
perr(newErr)
iter := newDb.Search([]byte("aabb"))
newIter := newDb.NewIterator(nil, nil)
newIter.Next()
newIter.Next()
if string(newIter.Key()) != string(iter.Key()) || string(newIter.Value()) != string(iter.Value()) {
t.Fatalf("error search, expect key: %s, value: %s; but get key: %s, value: %s", string(newIter.Key()), string(newIter.Value()), string(iter.Key()), string(iter.Value()))
}
}
func TestLeveldbStorage_Traversal(t *testing.T) {
db, err := leveldb.OpenFile("test/store/traversal", nil)
perr(err)
for i := range keys {
err := db.Put([]byte(keys[i]), []byte(values[i]), nil)
perr(err)
}
db.Close()
newDb, newErr := NewLeveldbStorage("test/store/save")
defer newDb.Close()
perr(newErr)
newValues := newDb.Traversal()
if !reflect.DeepEqual(newValues, values) {
t.Fatalf("expect\n%v\nbut got\n%v", values, newValues)
}
}
func TestLeveldbStorage_LoadRange(t *testing.T) {
db, err := leveldb.OpenFile("test/store/loadrange", nil)
perr(err)
for i := range keys {
err := db.Put([]byte(keys[i]), []byte(values[i]), nil)
perr(err)
}
db.Close()
newDb, newErr := NewLeveldbStorage("test/store/loadrange")
defer newDb.Close()
perr(newErr)
newKeys, newValues, newErr := newDb.LoadRange([]byte(keys[1]), []byte(keys[4]))
perr(newErr)
if !reflect.DeepEqual(newValues, values[1:5]) || !reflect.DeepEqual(newKeys, keys[1:5]) {
t.Fatalf("error loadrange, get keys:%v", newKeys)
}
}