-
Notifications
You must be signed in to change notification settings - Fork 9
/
db_test.go
300 lines (285 loc) · 6.83 KB
/
db_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
// bagdb: Simple datastorage
// Copyright 2021 billy authors
// SPDX-License-Identifier: BSD-3-Clause
package billy
import (
"bytes"
"crypto/rand"
"errors"
"os"
"path/filepath"
"testing"
)
func TestGrowFile(t *testing.T) {
filename := filepath.Join(t.TempDir(), "testgrow.temp")
f, err := os.Create(filename)
if err != nil {
t.Fatal(err)
}
defer os.Remove(filename)
defer f.Close()
if _, err := f.Seek(55, 0); err != nil {
t.Fatal(err)
}
if _, err := f.Write([]byte("test")); err != nil {
t.Fatal(err)
}
if finfo, err := os.Stat(filename); err != nil {
t.Fatal(err)
} else if have, want := finfo.Size(), int64(59); have != want {
t.Fatalf("have %d want %d", have, want)
}
}
func TestGrowFile2(t *testing.T) {
filename := filepath.Join(t.TempDir(), "testgrow2.temp")
f, err := os.Create(filename)
if err != nil {
t.Fatal(err)
}
defer os.Remove(filename)
defer f.Close()
if _, err := f.WriteAt([]byte("test"), 55); err != nil {
t.Fatal(err)
}
if _, err := f.Write([]byte("test")); err != nil {
t.Fatal(err)
}
if finfo, err := os.Stat(filename); err != nil {
t.Fatal(err)
} else if have, want := finfo.Size(), int64(59); have != want {
t.Fatalf("have %d want %d", have, want)
}
}
func fill(data byte, size int) []byte {
a := make([]byte, size)
for i := range a {
a[i] = data
}
return a
}
func TestDBBasics(t *testing.T) {
db, err := Open(Options{Path: t.TempDir()}, SlotSizePowerOfTwo(128, 500), nil)
if err != nil {
t.Fatal(err)
}
k0, _ := db.Put(fill(0, 140))
k1, _ := db.Put(fill(1, 140))
k2, _ := db.Put(fill(2, 280))
k3, _ := db.Put(fill(3, 280))
if have, err := db.Get(k0); err != nil {
t.Fatal(err)
} else if want := fill(0, 140); !bytes.Equal(have, want) {
t.Fatalf(" have\n%x\n want\n%x", have, want)
}
if have := db.Size(k0); have != 256 {
t.Fatalf(" have\n%d\n want\n%d", have, 256)
}
if have, err := db.Get(k1); err != nil {
t.Fatal(err)
} else if want := fill(1, 140); !bytes.Equal(have, want) {
t.Fatalf(" have\n%x\n want\n%x", have, want)
}
if have := db.Size(k1); have != 256 {
t.Fatalf(" have\n%d\n want\n%d", have, 256)
}
if have, err := db.Get(k2); err != nil {
t.Fatal(err)
} else if want := fill(2, 280); !bytes.Equal(have, want) {
t.Fatalf(" have\n%x\n want\n%x", have, want)
}
if have := db.Size(k2); have != 512 {
t.Fatalf(" have\n%d\n want\n%d", have, 512)
}
if have, err := db.Get(k3); err != nil {
t.Fatal(err)
} else if want := fill(3, 280); !bytes.Equal(have, want) {
t.Fatalf(" have\n%x\n want\n%x", have, want)
}
if have := db.Size(k3); have != 512 {
t.Fatalf(" have\n%d\n want\n%d", have, 512)
}
}
func TestDbErrors(t *testing.T) {
// Create a db
p := t.TempDir()
db, err := Open(Options{Path: p}, SlotSizePowerOfTwo(128, 500), nil)
if err != nil {
t.Fatal(err)
}
_, _ = db.Put(fill(0, 140))
if err := db.Close(); err != nil {
t.Fatal(err)
}
if err := db.Iterate(nil); !errors.Is(err, ErrClosed) {
t.Fatalf("want %v, have %v", ErrClosed, err)
}
// Open readonly
if db, err = Open(Options{Path: p, Readonly: true}, SlotSizePowerOfTwo(128, 500), nil); err != nil {
t.Fatal(err)
}
if _, err := db.Put([]byte{}); !errors.Is(err, ErrReadonly) {
t.Fatalf("want %v, have %v", ErrReadonly, err)
}
// Open regular again
db, err = Open(Options{Path: p}, SlotSizePowerOfTwo(128, 500), nil)
if err != nil {
t.Fatal(err)
}
// We reach in and close one of the files to trigger an error on Close
_ = db.(*database).shelves[0].f.Close()
if err := db.Close(); err == nil {
t.Fatal("expected error due to double-close")
}
}
func TestCustomSlotSizesFailures(t *testing.T) {
a := uint32(500)
var b uint32
var c uint32
for i, tt := range []SlotSizeFn{
// Not increasing orders
func() (uint32, bool) {
a--
return a, false
},
// Too many buckets
func() (uint32, bool) {
b++
return 1024 + b, b > 10000
},
// Too small buckets
func() (uint32, bool) {
c++
return c, c > 5
},
} {
_, err := Open(Options{Path: t.TempDir()}, tt, nil)
if err == nil {
t.Errorf("test %d: expected error but got none", i)
}
t.Logf("error: %v", err)
}
}
func TestCustomSlotSizesOk(t *testing.T) {
a := 0
db, err := Open(Options{Path: t.TempDir()}, func() (uint32, bool) {
ret := 10 * (1 + a)
a++
return uint32(ret), ret >= 30
}, nil)
if err != nil {
t.Fatal(err)
}
if have, want := len(db.(*database).shelves), 3; have != want {
t.Fatalf("have %d buckets, want %d", have, want)
}
}
func TestSlotSizeLinear(t *testing.T) {
linearFn := SlotSizeLinear(10, 100)
for want := 10; want < 1000; want += 10 {
have, last := linearFn()
if int(have) != want {
t.Fatalf("have %d want %d", have, want)
}
if last {
t.Fatalf("ended too soon, val %d", have)
}
}
have, last := linearFn()
want := 1000
if int(have) != want {
t.Fatalf("have %d want %d", have, want)
}
if !last {
t.Fatalf("ended too late")
}
}
func TestSlotSizePowerOfTwo(t *testing.T) {
linearFn := SlotSizePowerOfTwo(2, 100)
for want := 2; want < 100; want *= 2 {
have, last := linearFn()
if int(have) != want {
t.Fatalf("have %d want %d", have, want)
}
if last {
t.Fatalf("ended too soon, val %d", have)
}
}
have, last := linearFn()
want := 128
if int(have) != want {
t.Fatalf("have %d want %d", have, want)
}
if !last {
t.Fatalf("ended too late")
}
}
func TestSizes(t *testing.T) {
a := 0
db, err := Open(Options{Path: t.TempDir()}, func() (uint32, bool) {
// Return 10, 20, 30
ret := 10 * (1 + a)
a++
return uint32(ret), ret >= 30
}, nil)
if err != nil {
t.Fatal(err)
}
min, max := db.Limits()
if min != 10 {
t.Fatalf("exp 10, have %d", min)
}
if max != 30 {
t.Fatalf("exp 30, have %d", max)
}
var kvdata = make(map[uint64]string)
for i := 1; i < 35; i++ {
var (
want = make([]byte, i)
have []byte
)
_, _ = rand.Read(want)
key, err := db.Put(want)
if i >= 27 && err != nil {
// It should reject size 27 and onwards
continue
}
if err != nil {
t.Errorf("test %d: %v", i, err)
continue
}
have, err = db.Get(key)
if err != nil {
t.Errorf("test %d: %v", i, err)
continue
}
if !bytes.Equal(have, want) {
t.Errorf("test %d\nhave: %x\nwant: %x\n", i, have, want)
}
kvdata[key] = string(have)
}
err = db.Iterate(func(key uint64, size uint32, data []byte) {
wantVal := kvdata[key]
wantSize := uint32(((len(wantVal) + itemHeaderSize + 9) / 10) * 10)
if string(data) != wantVal || size != wantSize {
t.Fatalf("iteration fail, key %d\nhave data: %x\nwant data: %x\nhave size: %d\nwant size: %d\n",
key, data, wantVal, size, wantSize)
}
})
if err != nil {
t.Fatal(err)
}
for key := range kvdata {
err = db.Delete(key)
if err != nil {
t.Errorf("delete key %d: %v", key, err)
continue
}
}
// Expect nothing to remaing
err = db.Iterate(func(key uint64, size uint32, data []byte) {
t.Fatalf("Expected empty db, key %d", key)
})
if err != nil {
t.Fatal(err)
}
}