-
Notifications
You must be signed in to change notification settings - Fork 30
/
prefixdb_test.go
229 lines (198 loc) · 5.6 KB
/
prefixdb_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
package db
import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
"path/filepath"
"sync"
"testing"
"github.com/stretchr/testify/require"
)
func mockDBWithStuff(t *testing.T) DB {
db := NewMemDB()
// Under "key" prefix
require.NoError(t, db.Set(bz("key"), bz("value")))
require.NoError(t, db.Set(bz("key1"), bz("value1")))
require.NoError(t, db.Set(bz("key2"), bz("value2")))
require.NoError(t, db.Set(bz("key3"), bz("value3")))
require.NoError(t, db.Set(bz("something"), bz("else")))
require.NoError(t, db.Set(bz("k"), bz("val")))
require.NoError(t, db.Set(bz("ke"), bz("valu")))
require.NoError(t, db.Set(bz("kee"), bz("valuu")))
return db
}
func taskKey(i, k int) []byte {
return []byte(fmt.Sprintf("task-%d-key-%d", i, k))
}
func randomValue() []byte {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
panic(fmt.Sprintf("random value generation failed: %v", err))
}
return b
}
func TestGolevelDB(t *testing.T) {
path := filepath.Join(t.TempDir(), "goleveldb")
db, err := NewGoLevelDB(path, "", nil)
require.NoError(t, err)
Run(t, db)
}
/* We don't seem to test badger anywhere.
func TestWithBadgerDB(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "badgerdb")
db, err := NewBadgerDB(path, "")
require.NoError(t, err)
t.Run("BadgerDB", func(t *testing.T) { Run(t, db) })
}
*/
func TestWithMemDB(t *testing.T) {
db := NewMemDB()
t.Run("MemDB", func(t *testing.T) { Run(t, db) })
}
// Run generates concurrent reads and writes to db so the race detector can
// verify concurrent operations are properly synchronized.
// The contents of db are garbage after Run returns.
func Run(t *testing.T, db DB) {
t.Helper()
const numWorkers = 10
const numKeys = 64
var wg sync.WaitGroup
for i := 0; i < numWorkers; i++ {
wg.Add(1)
i := i
go func() {
defer wg.Done()
// Insert a bunch of keys with random data.
for k := 1; k <= numKeys; k++ {
key := taskKey(i, k) // say, "task-<i>-key-<k>"
value := randomValue()
if err := db.Set(key, value); err != nil {
t.Errorf("Task %d: db.Set(%q=%q) failed: %v",
i, string(key), string(value), err)
}
}
// Iterate over the database to make sure our keys are there.
it, err := db.Iterator(nil, nil)
if err != nil {
t.Errorf("Iterator[%d]: %v", i, err)
return
}
found := make(map[string][]byte)
mine := []byte(fmt.Sprintf("task-%d-", i))
for {
if key := it.Key(); bytes.HasPrefix(key, mine) {
found[string(key)] = it.Value()
}
it.Next()
if !it.Valid() {
break
}
}
if err := it.Error(); err != nil {
t.Errorf("Iterator[%d] reported error: %v", i, err)
}
if err := it.Close(); err != nil {
t.Errorf("Close iterator[%d]: %v", i, err)
}
if len(found) != numKeys {
t.Errorf("Task %d: found %d keys, wanted %d", i, len(found), numKeys)
}
// Delete all the keys we inserted.
for key := range mine {
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, uint32(key))
if err := db.Delete(bs); err != nil {
t.Errorf("Delete %q: %v", key, err)
}
}
}()
}
wg.Wait()
}
func TestPrefixDBSimple(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
checkValue(t, pdb, bz("key"), nil)
checkValue(t, pdb, bz("key1"), nil)
checkValue(t, pdb, bz("1"), bz("value1"))
checkValue(t, pdb, bz("key2"), nil)
checkValue(t, pdb, bz("2"), bz("value2"))
checkValue(t, pdb, bz("key3"), nil)
checkValue(t, pdb, bz("3"), bz("value3"))
checkValue(t, pdb, bz("something"), nil)
checkValue(t, pdb, bz("k"), nil)
checkValue(t, pdb, bz("ke"), nil)
checkValue(t, pdb, bz("kee"), nil)
}
func TestPrefixDBIterator1(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
itr, err := pdb.Iterator(nil, nil)
require.NoError(t, err)
checkDomain(t, itr, nil, nil)
checkItem(t, itr, bz("1"), bz("value1"))
checkNext(t, itr, true)
checkItem(t, itr, bz("2"), bz("value2"))
checkNext(t, itr, true)
checkItem(t, itr, bz("3"), bz("value3"))
checkNext(t, itr, false)
checkInvalid(t, itr)
itr.Close()
}
func TestPrefixDBReverseIterator1(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
itr, err := pdb.ReverseIterator(nil, nil)
require.NoError(t, err)
checkDomain(t, itr, nil, nil)
checkItem(t, itr, bz("3"), bz("value3"))
checkNext(t, itr, true)
checkItem(t, itr, bz("2"), bz("value2"))
checkNext(t, itr, true)
checkItem(t, itr, bz("1"), bz("value1"))
checkNext(t, itr, false)
checkInvalid(t, itr)
itr.Close()
}
func TestPrefixDBReverseIterator5(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
itr, err := pdb.ReverseIterator(bz("1"), nil)
require.NoError(t, err)
checkDomain(t, itr, bz("1"), nil)
checkItem(t, itr, bz("3"), bz("value3"))
checkNext(t, itr, true)
checkItem(t, itr, bz("2"), bz("value2"))
checkNext(t, itr, true)
checkItem(t, itr, bz("1"), bz("value1"))
checkNext(t, itr, false)
checkInvalid(t, itr)
itr.Close()
}
func TestPrefixDBReverseIterator6(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
itr, err := pdb.ReverseIterator(bz("2"), nil)
require.NoError(t, err)
checkDomain(t, itr, bz("2"), nil)
checkItem(t, itr, bz("3"), bz("value3"))
checkNext(t, itr, true)
checkItem(t, itr, bz("2"), bz("value2"))
checkNext(t, itr, false)
checkInvalid(t, itr)
itr.Close()
}
func TestPrefixDBReverseIterator7(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
itr, err := pdb.ReverseIterator(nil, bz("2"))
require.NoError(t, err)
checkDomain(t, itr, nil, bz("2"))
checkItem(t, itr, bz("1"), bz("value1"))
checkNext(t, itr, false)
checkInvalid(t, itr)
itr.Close()
}