-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbucket_test.go
522 lines (431 loc) · 11.8 KB
/
bucket_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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package buckets_test
import (
"bytes"
"fmt"
"os"
"testing"
"github.com/joyrexus/buckets"
)
// Ensure that we can create and delete a bucket.
func TestBucket(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
_, err := bx.New([]byte("things"))
if err != nil {
t.Error(err.Error())
}
if err := bx.Delete([]byte("things")); err != nil {
t.Error(err.Error())
}
}
// Ensure we can put an item in a bucket.
func TestPut(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
things, err := bx.New([]byte("things"))
if err != nil {
t.Error(err.Error())
}
key, value := []byte("A"), []byte("alpha")
// Put key/value into the `things` bucket.
if err := things.Put(key, value); err != nil {
t.Error(err.Error())
}
// Read value back in a different read-only transaction.
got, err := things.Get(key)
if err != nil && !bytes.Equal(got, value) {
t.Error(err.Error())
}
}
// Show we can put an item in a bucket and get it back out.
func ExampleBucket_Put() {
bx, _ := buckets.Open(tempfile())
defer os.Remove(bx.Path())
defer bx.Close()
// Create a new `things` bucket.
bucket := []byte("things")
things, _ := bx.New(bucket)
// Put key/value into the `things` bucket.
key, value := []byte("A"), []byte("alpha")
if err := things.Put(key, value); err != nil {
fmt.Printf("could not insert item: %v", err)
}
// Read value back in a different read-only transaction.
got, _ := things.Get(key)
fmt.Printf("The value of %q in `%s` is %q\n", key, bucket, got)
// Output:
// The value of "A" in `things` is "alpha"
}
// Ensure we don't overwrite existing items when using PutNX.
func TestPutNX(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
things, err := bx.New([]byte("things"))
if err != nil {
t.Error(err.Error())
}
key := []byte("A")
a, b := []byte("alpha"), []byte("beta")
// Put key/a-value into the `things` bucket.
if err := things.PutNX(key, a); err != nil {
t.Error(err.Error())
}
// Read value back in a different read-only transaction.
got, err := things.Get(key)
if err != nil && !bytes.Equal(got, a) {
t.Error(err.Error())
}
// Try putting key/b-value into the `things` bucket.
if err := things.PutNX(key, b); err != nil {
t.Error(err.Error())
}
// Value for key should still be a, not b.
got, err = things.Get(key)
if err != nil && !bytes.Equal(got, a) {
t.Error(err.Error())
}
}
// Show we don't overwrite existing values when using PutNX.
func ExampleBucket_PutNX() {
bx, _ := buckets.Open(tempfile())
defer os.Remove(bx.Path())
defer bx.Close()
// Create a new `things` bucket.
bucket := []byte("things")
things, _ := bx.New(bucket)
// Put key/value into the `things` bucket.
key, value := []byte("A"), []byte("alpha")
if err := things.Put(key, value); err != nil {
fmt.Printf("could not insert item: %v", err)
}
// Read value back in a different read-only transaction.
got, _ := things.Get(key)
fmt.Printf("The value of %q in `%s` is %q\n", key, bucket, got)
// Try putting another value with same key.
things.PutNX(key, []byte("beta"))
// Read value back in a different read-only transaction.
got, _ = things.Get(key)
fmt.Printf("The value of %q in `%s` is still %q\n", key, bucket, got)
// Output:
// The value of "A" in `things` is "alpha"
// The value of "A" in `things` is still "alpha"
}
// Ensure that a bucket that gets a non-existent key returns nil.
func TestGetMissing(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
things, err := bx.New([]byte("things"))
if err != nil {
t.Error(err.Error())
}
key := []byte("missing")
if got, _ := things.Get(key); got != nil {
t.Errorf("not expecting value for key %q: got %q", key, got)
}
}
// Ensure that we can delete stuff in a bucket.
func TestDelete(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
things, err := bx.New([]byte("things"))
if err != nil {
t.Error(err.Error())
}
k, v := []byte("foo"), []byte("bar")
if err = things.Put(k, v); err != nil {
t.Error(err.Error())
}
if err = things.Delete(k); err != nil {
t.Error(err.Error())
}
}
// Ensure we can insert items into a bucket and get them back out.
func TestInsert(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
paths, err := bx.New([]byte("paths"))
// k, v pairs to put in `paths` bucket
items := []struct {
Key, Value []byte
}{
{[]byte("foo/"), []byte("foo")},
{[]byte("foo/bar/"), []byte("bar")},
{[]byte("foo/bar/baz/"), []byte("baz")},
{[]byte("food/"), []byte("")},
{[]byte("good/"), []byte("")},
{[]byte("goo/"), []byte("")},
}
err = paths.Insert(items)
if err != nil {
t.Error(err.Error())
}
gotItems, err := paths.Items()
if err != nil {
t.Error(err.Error())
}
// expected k/v mapping
expected := map[string][]byte{
"foo/": []byte("foo"),
"foo/bar/": []byte("bar"),
"foo/bar/baz/": []byte("baz"),
"food/": []byte(""),
"good/": []byte(""),
"goo/": []byte(""),
}
for _, item := range gotItems {
want := expected[string(item.Key)]
if !bytes.Equal(item.Value, want) {
t.Errorf("got %v, want %v", item.Value, want)
}
}
}
// Show we can insert items into a bucket and get them back out.
func ExampleBucket_Insert() {
bx, _ := buckets.Open(tempfile())
defer os.Remove(bx.Path())
defer bx.Close()
letters, _ := bx.New([]byte("letters"))
// Setup items to insert in `letters` bucket.
items := []struct {
Key, Value []byte
}{
{[]byte("A"), []byte("alpha")},
{[]byte("B"), []byte("beta")},
{[]byte("C"), []byte("gamma")},
}
// Insert items into `letters` bucket.
if err := letters.Insert(items); err != nil {
fmt.Println("could not insert items!")
}
// Get items back out in separate read-only transaction.
results, _ := letters.Items()
for _, item := range results {
fmt.Printf("%s -> %s\n", item.Key, item.Value)
}
// Output:
// A -> alpha
// B -> beta
// C -> gamma
}
// Ensure we can safely insert items into a bucket without overwriting
// existing items.
func TestInsertNX(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
bk, err := bx.New([]byte("test"))
// Put k/v into the `bk` bucket.
k, v := []byte("A"), []byte("alpha")
if err := bk.Put(k, v); err != nil {
t.Error(err.Error())
}
// k/v pairs to put-if-not-exists
items := []struct {
Key, Value []byte
}{
{[]byte("A"), []byte("ALPHA")}, // key exists, so don't update
{[]byte("B"), []byte("beta")},
{[]byte("C"), []byte("gamma")},
}
err = bk.InsertNX(items)
if err != nil {
t.Error(err.Error())
}
gotItems, err := bk.Items()
if err != nil {
t.Error(err.Error())
}
// expected items
expected := []struct {
Key, Value []byte
}{
{[]byte("A"), []byte("alpha")}, // existing value not updated
{[]byte("B"), []byte("beta")},
{[]byte("C"), []byte("gamma")},
}
for i, got := range gotItems {
want := expected[i]
if !bytes.Equal(got.Value, want.Value) {
t.Errorf("key %q: got %v, want %v", got.Key, got.Value, want.Value)
}
}
}
// Ensure that we can get items for all keys with a given prefix.
func TestPrefixItems(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
// Create a new things bucket.
things, err := bx.New([]byte("things"))
if err != nil {
t.Error(err.Error())
}
// Setup items to insert.
items := []struct {
Key, Value []byte
}{
{[]byte("A"), []byte("1")}, // `A` prefix match
{[]byte("AA"), []byte("2")}, // match
{[]byte("AAA"), []byte("3")}, // match
{[]byte("AAB"), []byte("2")}, // match
{[]byte("B"), []byte("O")},
{[]byte("BA"), []byte("0")},
{[]byte("BAA"), []byte("0")},
}
// Insert 'em.
if err := things.Insert(items); err != nil {
t.Error(err.Error())
}
// Now get each item whose key starts with "A".
prefix := []byte("A")
// Expected items for keys with prefix "A".
expected := []struct {
Key, Value []byte
}{
{[]byte("A"), []byte("1")},
{[]byte("AA"), []byte("2")},
{[]byte("AAA"), []byte("3")},
{[]byte("AAB"), []byte("2")},
}
results, err := things.PrefixItems(prefix)
if err != nil {
t.Error(err.Error())
}
for i, want := range expected {
got := results[i]
if !bytes.Equal(got.Key, want.Key) {
t.Errorf("got %v, want %v", got.Key, want.Key)
}
if !bytes.Equal(got.Value, want.Value) {
t.Errorf("got %v, want %v", got.Value, want.Value)
}
}
}
// Show that we can get items for all keys with a given prefix.
func ExampleBucket_PrefixItems() {
bx, _ := buckets.Open(tempfile())
defer os.Remove(bx.Path())
defer bx.Close()
// Create a new things bucket.
things, _ := bx.New([]byte("things"))
// Setup items to insert.
items := []struct {
Key, Value []byte
}{
{[]byte("A"), []byte("1")}, // `A` prefix match
{[]byte("AA"), []byte("2")}, // match
{[]byte("AAA"), []byte("3")}, // match
{[]byte("AAB"), []byte("2")}, // match
{[]byte("B"), []byte("O")},
{[]byte("BA"), []byte("0")},
{[]byte("BAA"), []byte("0")},
}
// Insert 'em.
if err := things.Insert(items); err != nil {
fmt.Printf("could not insert items in `things` bucket: %v\n", err)
}
// Now get items whose key starts with "A".
prefix := []byte("A")
results, err := things.PrefixItems(prefix)
if err != nil {
fmt.Printf("could not get items with prefix %q: %v\n", prefix, err)
}
for _, item := range results {
fmt.Printf("%s -> %s\n", item.Key, item.Value)
}
// Output:
// A -> 1
// AA -> 2
// AAA -> 3
// AAB -> 2
}
// Ensure we can get items for all keys within a given range.
func TestRangeItems(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
years, err := bx.New([]byte("years"))
if err != nil {
t.Error(err.Error())
}
// Setup items to insert in `years` bucket
items := []struct {
Key, Value []byte
}{
{[]byte("1970"), []byte("70")},
{[]byte("1975"), []byte("75")},
{[]byte("1980"), []byte("80")},
{[]byte("1985"), []byte("85")},
{[]byte("1990"), []byte("90")}, // min = 1990
{[]byte("1995"), []byte("95")}, // min < 1995 < max
{[]byte("2000"), []byte("00")}, // max = 2000
{[]byte("2005"), []byte("05")},
{[]byte("2010"), []byte("10")},
}
// Insert 'em.
if err := years.Insert(items); err != nil {
t.Error(err.Error())
}
// Now get each item whose key is in the 1990 to 2000 range.
min := []byte("1990")
max := []byte("2000")
// Expected items within time range: 1990 <= key <= 2000.
expected := []struct {
Key, Value []byte
}{
{[]byte("1990"), []byte("90")}, // min = 1990
{[]byte("1995"), []byte("95")}, // min < 1995 < max
{[]byte("2000"), []byte("00")}, // max = 2000
}
// Get items for keys within min/max range.
results, err := years.RangeItems(min, max)
if err != nil {
t.Error(err.Error())
}
for i, want := range expected {
got := results[i]
if !bytes.Equal(got.Key, want.Key) {
t.Errorf("got %v, want %v", got.Key, want.Key)
}
if !bytes.Equal(got.Value, want.Value) {
t.Errorf("got %v, want %v", got.Value, want.Value)
}
}
}
// Show that we get items for keys within a given range.
func ExampleBucket_RangeItems() {
bx, _ := buckets.Open(tempfile())
defer os.Remove(bx.Path())
defer bx.Close()
// Create a new bucket named "years".
years, _ := bx.New([]byte("years"))
// Setup items to insert in `years` bucket
items := []struct {
Key, Value []byte
}{
{[]byte("1970"), []byte("70")},
{[]byte("1975"), []byte("75")},
{[]byte("1980"), []byte("80")},
{[]byte("1985"), []byte("85")},
{[]byte("1990"), []byte("90")}, // min = 1990
{[]byte("1995"), []byte("95")}, // min < 1995 < max
{[]byte("2000"), []byte("00")}, // max = 2000
{[]byte("2005"), []byte("05")},
{[]byte("2010"), []byte("10")},
}
// Insert 'em.
if err := years.Insert(items); err != nil {
fmt.Printf("could not insert items in `years` bucket: %v\n", err)
}
// Time range: 1990 <= key <= 2000.
min := []byte("1990")
max := []byte("2000")
results, err := years.RangeItems(min, max)
if err != nil {
fmt.Printf("could not get items within range: %v\n", err)
}
for _, item := range results {
fmt.Printf("%s -> %s\n", item.Key, item.Value)
}
// Output:
// 1990 -> 90
// 1995 -> 95
// 2000 -> 00
}