-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmap_test.go
326 lines (277 loc) · 7.35 KB
/
map_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
package buckets_test
import (
"bytes"
"fmt"
"os"
"testing"
"github.com/joyrexus/buckets"
)
// Ensure that we can apply functions to each k/v pair.
func TestMap(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
// Create a new bucket.
letters, err := bx.New([]byte("letters"))
if err != nil {
t.Error(err.Error())
}
// Setup items to insert.
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!")
}
// Setup slice of items to collect results.
type item struct {
Key, Value []byte
}
results := []item{}
// Anon func to apply to each item in bucket.
// Here, we're just going to collect the items just inserted.
do := func(k, v []byte) error {
results = append(results, item{k, v})
return nil
}
// Now map the `do` function over each item.
if err := letters.Map(do); err != nil {
t.Error(err.Error())
}
// Finally, check to see if our results match the originally
// inserted items.
for i, want := range items {
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)
}
}
}
// Ensure that we can apply a function to the k/v pairs
// of keys with a given prefix.
func TestMapPrefix(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 collect 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")},
}
// Setup slice of items to collect results.
type item struct {
Key, Value []byte
}
results := []item{}
// Anon func to map over matched keys.
do := func(k, v []byte) error {
results = append(results, item{k, v})
return nil
}
if err := things.MapPrefix(do, prefix); 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 apply a function to the k/v pairs
// of keys with a given prefix.
func ExampleBucket_MapPrefix() {
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 collect each item whose key starts with "A".
prefix := []byte("A")
// Setup slice of items.
type item struct {
Key, Value []byte
}
results := []item{}
// Anon func to map over matched keys.
do := func(k, v []byte) error {
results = append(results, item{k, v})
return nil
}
if err := things.MapPrefix(do, prefix); err != nil {
fmt.Printf("could not map items with prefix %s: %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 apply functions to the k/v pairs
// of keys within a given range.
func TestMapRange(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())
}
// Time range to map over.
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
}
// Setup slice of items to collect results.
type item struct {
Key, Value []byte
}
results := []item{}
// Anon func to map over matched keys.
do := func(k, v []byte) error {
results = append(results, item{k, v})
return nil
}
if err := years.MapRange(do, min, max); 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 apply a function to the k/v pairs
// of keys within a given range.
func ExampleBucket_MapRange() {
bx, _ := buckets.Open(tempfile())
defer os.Remove(bx.Path())
defer bx.Close()
// Delete any existing bucket named "years".
bx.Delete([]byte("years"))
// 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 to map over: 1990 <= key <= 2000.
min := []byte("1990")
max := []byte("2000")
// Setup slice of items to collect results.
type item struct {
Key, Value []byte
}
results := []item{}
// Anon func to map over matched keys.
do := func(k, v []byte) error {
results = append(results, item{k, v})
return nil
}
if err := years.MapRange(do, min, max); err != nil {
fmt.Printf("could not map 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
}