-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmbuckets.go
526 lines (428 loc) · 13.1 KB
/
mbuckets.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
523
524
525
526
/*
Package mbuckets provides a thin wrapper over Bolt DB.
It allows easy operations on multi-level (nested) buckets.
See https://github.com/boltdb/bolt for Bolt DB.
*/
package mbuckets
import (
"bytes"
"fmt"
"os"
"time"
"github.com/boltdb/bolt"
)
// DB embeds a bolt.DB
type DB struct {
*bolt.DB
}
// Open creates/opens a bolt.DB at specified path, and returns a DB enclosing the same
func Open(path string) (*DB, error) {
database, err := bolt.Open(path, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return nil, err
}
return &DB{database}, nil
}
// OpenWith creates/opens a bolt.DB at specified path with given permissions and options, and returns a DB enclosing the same
func OpenWith(path string, mode os.FileMode, options *bolt.Options) (*DB, error) {
if options == nil {
options = &bolt.Options{Timeout: 1 * time.Second}
}
database, err := bolt.Open(path, mode, options)
if err != nil {
return nil, err
}
return &DB{database}, nil
}
// Close closes the embedded bolt.DB
func (db *DB) Close() error {
return db.DB.Close()
}
// Map applies read only function `fn` on all the top level buckets in this DB
func (db *DB) Map(fn func([]byte, *bolt.Bucket) error) error {
return db.View(func(tx *bolt.Tx) error {
return tx.ForEach(fn)
})
}
// GetRootBucketNames returns all the top level bolt.Bucket names in this DB
func (db *DB) GetRootBucketNames() ([][]byte, error) {
var bucketNames [][]byte
err := db.Map(func(name []byte, _ *bolt.Bucket) error {
bucketName := make([]byte, len(name))
copy(bucketName, name)
bucketNames = append(bucketNames, bucketName)
return nil
})
return bucketNames, err
}
// GetAllBucketNames recursively finds and returns all the bolt.Bucket names in this DB
func (db *DB) GetAllBucketNames() ([][]byte, error) {
bucketNames, err := db.GetRootBucketNames()
if err != nil {
return nil, err
}
var allBucketNames [][]byte
for _, bucketName := range bucketNames {
bucket := db.Bucket(bucketName)
allBucketNames = append(allBucketNames, bucketName)
subBucketNames, err := bucket.GetAllBucketNames()
if err != nil {
return allBucketNames, err
}
allBucketNames = append(allBucketNames, subBucketNames...)
}
return allBucketNames, nil
}
// GetAllBucketNamesWithSeparator recursively finds and returns all the bolt.Bucket names in this DB using specified separator
func (db *DB) GetAllBucketNamesWithSeparator(separator []byte) ([][]byte, error) {
bucketNames, err := db.GetRootBucketNames()
if err != nil {
return nil, err
}
var allBucketNames [][]byte
for _, bucketName := range bucketNames {
bucket := db.Bucket(bucketName).WithSeparator(separator)
allBucketNames = append(allBucketNames, bucketName)
subBucketNames, err := bucket.GetAllBucketNames()
if err != nil {
return allBucketNames, err
}
allBucketNames = append(allBucketNames, subBucketNames...)
}
return allBucketNames, nil
}
// Bucket represents a logical entity used to access a bolt.Bucket inside a DB
type Bucket struct {
DB *DB
// Complete hierarchial name of the Bucket
Name []byte
// The Bucket Name separator
Separator []byte
}
// Bucket returns a pointer to a Bucket in this DB
func (db *DB) Bucket(name []byte) *Bucket {
return &Bucket{db, name, []byte("/")}
}
// BucketString is a convenience wrapper over Bucket for string name
func (db *DB) BucketString(name string) *Bucket {
return db.Bucket([]byte(name))
}
// WithSeparator overrides the separator for this Bucket with the given separator and returns a pointer to this Bucket.
//
// The name of this Bucket and all Buckets under it must be separated by the given custom separator.
// Note that it is an error to mix different separators and can lead to unexpected behavior.
func (b *Bucket) WithSeparator(separator []byte) *Bucket {
b.Separator = separator
return b
}
// Update performs an update operation specified by function `fn` on this Bucket
func (b *Bucket) Update(fn func(*bolt.Bucket, *bolt.Tx) error) error {
buckets := bytes.Split(b.Name, b.Separator)
return b.DB.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists(buckets[0])
if err != nil {
return err
}
if len(buckets) > 1 {
for idx, bucketName := range buckets {
if idx == 0 {
continue
}
subBucket, err := bucket.CreateBucketIfNotExists(bucketName)
if err != nil {
return err
}
bucket = subBucket
}
}
return fn(bucket, tx)
})
}
// View performs a view operation specified by function `fn` on this Bucket
func (b *Bucket) View(fn func(*bolt.Bucket, *bolt.Tx) error) error {
buckets := bytes.Split(b.Name, b.Separator)
return b.DB.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(buckets[0])
if bucket == nil {
return fmt.Errorf("Bucket not found: %s", b.Name)
}
if len(buckets) > 1 {
for idx, bucketName := range buckets {
if idx == 0 {
continue
}
subBucket := bucket.Bucket(bucketName)
if subBucket == nil {
return fmt.Errorf("Bucket not found: %s", b.Name)
}
bucket = subBucket
}
}
return fn(bucket, tx)
})
}
// CreateBucket cretes the bolt.Bucket specified by this Bucket
func (b *Bucket) CreateBucket() error {
return b.Update(func(*bolt.Bucket, *bolt.Tx) error {
return nil
})
}
// DeleteBucket deletes the bolt.Bucket specified by this Bucket
func (b *Bucket) DeleteBucket() error {
buckets := bytes.Split(b.Name, b.Separator)
return b.DB.Update(func(tx *bolt.Tx) error {
if len(buckets) == 1 {
return tx.DeleteBucket(buckets[0])
}
bucket := tx.Bucket(buckets[0])
if bucket == nil {
return fmt.Errorf("Bucket not found: %s", b.Name)
}
for idx, bucketName := range buckets {
if idx == 0 {
continue
}
if idx == len(buckets)-1 {
return bucket.DeleteBucket(bucketName)
}
subBucket := bucket.Bucket(bucketName)
if subBucket == nil {
return fmt.Errorf("Bucket not found: %s", b.Name)
}
bucket = subBucket
}
return nil
})
}
// Map performs a view operation specified by function `fn` on all key value pairs in this Bucket
func (b *Bucket) Map(fn func([]byte, []byte) error) error {
return b.View(func(bucket *bolt.Bucket, tx *bolt.Tx) error {
return bucket.ForEach(fn)
})
}
// MapPrefix performs a view operation specified by function `fn` on all key value pairs in this Bucket with the given prefix
func (b *Bucket) MapPrefix(prefix []byte, fn func([]byte, []byte) error) error {
return b.View(func(bucket *bolt.Bucket, tx *bolt.Tx) error {
cursor := bucket.Cursor()
for k, v := cursor.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = cursor.Next() {
err := fn(k, v)
if err != nil {
return err
}
}
return nil
})
}
// MapRange performs a view operation specified by function `fn` on all key value pairs in this Bucket within the given range
func (b *Bucket) MapRange(min, max []byte, fn func([]byte, []byte) error) error {
return b.View(func(bucket *bolt.Bucket, tx *bolt.Tx) error {
cursor := bucket.Cursor()
for k, v := cursor.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = cursor.Next() {
err := fn(k, v)
if err != nil {
return err
}
}
return nil
})
}
// Item represents a holder for a key value pair
type Item struct {
Key []byte
Value []byte
}
// Insert puts a single key/value pair in the bolt.Bucket specified by this Bucket
func (b *Bucket) Insert(key, value []byte) error {
return b.Update(func(bucket *bolt.Bucket, tx *bolt.Tx) error {
return bucket.Put(key, value)
})
}
// InsertString is a convenience wrapper over Insert for string key value pair
func (b *Bucket) InsertString(key, value string) error {
return b.Insert([]byte(key), []byte(value))
}
// InsertAll puts multiple key/value pairs in the bolt.Bucket specified by this Bucket
func (b *Bucket) InsertAll(items []Item) error {
return b.Update(func(bucket *bolt.Bucket, tx *bolt.Tx) error {
for _, item := range items {
err := bucket.Put(item.Key, item.Value)
if err != nil {
return err
}
}
return nil
})
}
// InsertAllString is a convenience method to Insert string key value pairs
func (b *Bucket) InsertAllString(items map[string]string) error {
return b.Update(func(bucket *bolt.Bucket, tx *bolt.Tx) error {
for key, value := range items {
err := bucket.Put([]byte(key), []byte(value))
if err != nil {
return err
}
}
return nil
})
}
// Get retrieves the value for given a key from the bolt.Bucket specified by this Bucket
func (b *Bucket) Get(key []byte) (value []byte, err error) {
err = b.View(func(bucket *bolt.Bucket, tx *bolt.Tx) error {
v := bucket.Get(key)
if v == nil {
return fmt.Errorf("Key not found: %s", key)
}
value = make([]byte, len(v))
copy(value, v)
return nil
})
return value, err
}
// GetString is a convenience wrapper over Get for string key value pair
func (b *Bucket) GetString(key string) (value string, err error) {
err = b.View(func(bucket *bolt.Bucket, tx *bolt.Tx) error {
v := bucket.Get([]byte(key))
if v == nil {
return fmt.Errorf("Key not found: %s", key)
}
value = string(v)
return nil
})
return value, err
}
// GetAll retrieves all the key/value pairs from the bolt.Bucket specified by this Bucket
func (b *Bucket) GetAll() ([]Item, error) {
var items []Item
err := b.Map(func(k, v []byte) error {
if v != nil {
key := make([]byte, len(k))
copy(key, k)
value := make([]byte, len(v))
copy(value, v)
items = append(items, Item{key, value})
}
return nil
})
return items, err
}
// GetAllString is a convenience method to GetAll string key value pairs
func (b *Bucket) GetAllString() (map[string]string, error) {
items := make(map[string]string)
err := b.Map(func(k, v []byte) error {
if v != nil {
items[string(k)] = string(v)
}
return nil
})
return items, err
}
// GetPrefix retrieves all the key/value pairs from the bolt.Bucket specified by this Bucket with the given prefix
func (b *Bucket) GetPrefix(prefix []byte) ([]Item, error) {
var items []Item
err := b.MapPrefix(prefix, func(k, v []byte) error {
if v != nil {
key := make([]byte, len(k))
copy(key, k)
value := make([]byte, len(v))
copy(value, v)
items = append(items, Item{key, value})
}
return nil
})
return items, err
}
// GetPrefixString is a convenience method to GetPrefix for string key value pairs
func (b *Bucket) GetPrefixString(prefix string) (map[string]string, error) {
items := make(map[string]string)
err := b.MapPrefix([]byte(prefix), func(k, v []byte) error {
if v != nil {
items[string(k)] = string(v)
}
return nil
})
return items, err
}
// GetRange retrieves all the key/value pairs from the bolt.Bucket specified by this Bucket within the given range
func (b *Bucket) GetRange(min, max []byte) ([]Item, error) {
var items []Item
err := b.MapRange(min, max, func(k, v []byte) error {
if v != nil {
key := make([]byte, len(k))
copy(key, k)
value := make([]byte, len(v))
copy(value, v)
items = append(items, Item{key, value})
}
return nil
})
return items, err
}
// GetRangeString is a convenience method to GetRange for string key value pairs
func (b *Bucket) GetRangeString(min, max string) (map[string]string, error) {
items := make(map[string]string)
err := b.MapRange([]byte(min), []byte(max), func(k, v []byte) error {
if v != nil {
items[string(k)] = string(v)
}
return nil
})
return items, err
}
// Delete removes the given key from the bolt.Bucket specified by this Bucket
func (b *Bucket) Delete(key []byte) error {
return b.Update(func(bucket *bolt.Bucket, tx *bolt.Tx) error {
return bucket.Delete(key)
})
}
// DeleteString is a convenience wrapper over Delete for string key
func (b *Bucket) DeleteString(key string) error {
return b.Delete([]byte(key))
}
// GetRootBucketNames returns all the top level bolt.Bucket names under the bolt.Bucket specified by this Bucket
func (b *Bucket) GetRootBucketNames() ([][]byte, error) {
var names [][]byte
err := b.Map(func(key []byte, value []byte) error {
if value == nil {
name := make([]byte, len(key))
copy(name, key)
names = append(names, name)
}
return nil
})
if err != nil {
return nil, err
}
bucketNames := make([][]byte, 0, len(names))
for _, name := range names {
bucketName := bytes.NewBuffer(b.Name)
bucketName.Write(b.Separator)
bucketName.Write(name)
bucketNames = append(bucketNames, bucketName.Bytes())
}
return bucketNames, nil
}
// GetAllBucketNames recursively finds and returns all the bolt.Bucket names under the bolt.Bucket specified by this Bucket
func (b *Bucket) GetAllBucketNames() ([][]byte, error) {
var allBucketNames [][]byte
bucketNames, err := b.GetRootBucketNames()
if err != nil {
return nil, err
}
numBucketsToProcess := len(bucketNames)
var bucketName []byte
for numBucketsToProcess > 0 {
bucketName, bucketNames = bucketNames[0], bucketNames[1:]
numBucketsToProcess--
allBucketNames = append(allBucketNames, bucketName)
bucket := b.DB.Bucket(bucketName).WithSeparator(b.Separator)
subBucketNames, err := bucket.GetRootBucketNames()
if err != nil {
return allBucketNames, err
}
if subBucketNames != nil && len(subBucketNames) > 0 {
numBucketsToProcess = numBucketsToProcess + len(subBucketNames)
bucketNames = append(bucketNames, subBucketNames...)
}
}
return allBucketNames, nil
}