-
Notifications
You must be signed in to change notification settings - Fork 1
/
bucket.go
719 lines (587 loc) · 15.2 KB
/
bucket.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
package puredb
import (
"github.com/dgraph-io/badger"
"fmt"
"encoding/binary"
"time"
"log"
)
type MarshalFn func(v interface{}) ([]byte, error)
type UnmarshalFn func(data []byte, v *interface{}) error
type BucketCallback func(bucket *Bucket, k interface{}, v interface{}) error
type BucketPredicate func(bucket *Bucket, k interface{}, v interface{}) (bool, error)
type BucketOpts struct {
MarshalKeyFn MarshalFn
UnmarshalKeyFn UnmarshalFn
MarshalValueFn MarshalFn
UnmarshalValueFn UnmarshalFn
PreAddFn BucketCallback
}
//type BucketInterface interface {
// Setup(db *PureDB, name string, opts BucketOpts) error
// Cleanup()
//
// GetName() string
// GetOpts() *BucketOpts
//
// MarshalKey(v interface{}) ([]byte, error)
// UnmarshalKey(data []byte, v *interface{}) error
// MarshalValue(v interface{}) ([]byte, error)
// UnmarshalValue(data []byte, v *interface{}) error
//
// Add(v interface{}) (int64, error)
// Set(k interface{}, v interface{}) error
// Get(k interface{}) (interface{}, error)
// //Has(k interface{}) (bool, error)
// Delete(k interface{}) error
// //DeleteWith(p BucketPredicate) (int, error)
// Pop(last bool) (interface{}, interface{}, error)
//
// Iterate(fn BucketCallback) error
// First() (interface{}, interface{}, error)
// Last() (interface{}, interface{}, error)
// Search(v interface{}, fn BucketCallback) (interface{}, error)
// SearchOne(v interface{}, cmpFn BucketPredicate, reverse bool) (interface{}, interface{}, error)
// SearchAll(v interface{}, cmpFn BucketPredicate, reverse bool) ([]interface{}, []interface{}, error)
//
// //FirstWith(p BucketPredicate) (interface{}, interface{}, error)
// //LastWith(p BucketPredicate) (interface{}, interface{}, error)
//
// // Count
// // CountWith
// // Empty
//}
type Bucket struct {
DB *PureDB
badgerDB *badger.DB
Name string
Opts BucketOpts
Seq *badger.Sequence
MarshalKeyFn MarshalFn
UnmarshalKeyFn UnmarshalFn
MarshalValueFn MarshalFn
UnmarshalValueFn UnmarshalFn
}
func (bucket *Bucket) Badger() *badger.DB {
return bucket.badgerDB
}
func (bucket *Bucket) Setup(db *PureDB, name string, opts BucketOpts) error {
bucket.DB = db
bucket.badgerDB = db.DB
bucket.Name = name
bucket.Opts = opts
seq, err := bucket.badgerDB.GetSequence([]byte(bucket.Name), 100)
bucket.Seq = seq
bucket.MarshalKeyFn = bucket.Opts.MarshalKeyFn
bucket.UnmarshalKeyFn = bucket.Opts.UnmarshalKeyFn
bucket.MarshalValueFn = bucket.Opts.MarshalValueFn
bucket.UnmarshalValueFn = bucket.Opts.UnmarshalValueFn
return err
}
func (bucket *Bucket) Cleanup() {
bucket.Seq.Release()
bucket.Seq = nil
}
func (bucket *Bucket) GetName() string {
return bucket.Name
}
func (bucket *Bucket) GetOpts() *BucketOpts {
return &bucket.Opts
}
func (bucket *Bucket) MarshalKey(v interface{}) ([]byte, error) {
return bucket.MarshalKeyFn(v)
}
func (bucket *Bucket) UnmarshalKey(data []byte, v *interface{}) error {
return bucket.UnmarshalKeyFn(data, v)
}
func (bucket *Bucket) MarshalValue(v interface{}) ([]byte, error) {
return bucket.MarshalValueFn(v)
}
func (bucket *Bucket) UnmarshalValue(data []byte, v *interface{}) error {
return bucket.UnmarshalValueFn(data, v)
}
func (bucket *Bucket) Add(v interface{}) (int64, error) {
db := bucket.badgerDB
var id uint64
err := db.Update(func(txn *badger.Txn) error {
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
num, err := bucket.Seq.Next()
if err != nil {
return err
}
k_b := u64tob(num)
id = num
if bucket.Opts.PreAddFn != nil {
err := bucket.Opts.PreAddFn(bucket, int64(num), v)
if err != nil {
return err
}
}
v_b, err := bucket.MarshalValue(v)
if err != nil {
return err
}
k_prefixed := append(prefix, k_b...)
return txn.Set(k_prefixed, v_b)
})
return int64(id), err
}
func (bucket *Bucket) Set(k interface{}, v interface{}) error {
db := bucket.badgerDB
err := db.Update(func(txn *badger.Txn) error {
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
k_b, err := bucket.MarshalKey(k)
if err != nil {
return err
}
v_b, err := bucket.MarshalValue(v)
if err != nil {
return err
}
k_prefixed := append(prefix, k_b...)
return txn.Set(k_prefixed, v_b)
})
return err
}
func (bucket *Bucket) Get(k interface{}) (interface{}, error) {
db := bucket.badgerDB
k_b, err := bucket.MarshalKey(k)
if err != nil {
return 0, err
}
var v interface{}
err = db.View(func(txn *badger.Txn) error {
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
k_prefixed := append(prefix, k_b...)
item, err := txn.Get(k_prefixed)
if err != nil {
return err
}
v_b, err := item.Value()
if err != nil {
return err
}
err = bucket.UnmarshalValue(v_b, &v)
if err != nil {
return err
}
return nil
})
return v, err
}
func (bucket *Bucket) Delete(k interface{}) error {
db := bucket.badgerDB
k_b, err := bucket.MarshalKey(k)
if err != nil {
return err
}
err = db.Update(func(txn *badger.Txn) error {
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
k_prefixed := append(prefix, k_b...)
return txn.Delete(k_prefixed)
})
return err
}
func (bucket *Bucket) Pop(last bool) (interface{}, interface{}, error) {
db := bucket.badgerDB
var k interface{}
var v interface{}
err := db.Update(func(txn *badger.Txn) error {
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 1
opts.Reverse = last
it := txn.NewIterator(opts)
defer it.Close()
if last {
prefix = prefixBeyondEnd(prefix)
}
it.Seek(prefix)
if (! it.ValidForPrefix(prefix)) {
// empty set
return fmt.Errorf("empty bucket")
}
item := it.Item()
k_prefixed := item.Key()
v_b, err := item.Value()
if err != nil {
return err
}
k_b := k_prefixed[len(prefix):]
err = bucket.UnmarshalKey(k_b, &k)
if err != nil {
return err
}
err = bucket.UnmarshalValue(v_b, &v)
if err != nil {
return err
}
return txn.Delete(k_prefixed)
})
return k, v, err
}
func (bucket *Bucket) Iterate(fn BucketCallback) error {
db := bucket.badgerDB
err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := txn.NewIterator(opts)
defer it.Close()
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
k_prefixed := item.Key()
v_b, err := item.Value()
if err != nil {
return err
}
k_b := k_prefixed[len(prefix):]
var k_i interface{}
var v_i interface{}
err = bucket.UnmarshalKey(k_b, &k_i)
if err != nil {
return err
}
err = bucket.UnmarshalValue(v_b, &v_i)
if err != nil {
return err
}
err = fn(bucket, k_i, v_i)
if err != nil {
return err
}
}
return nil
})
return err
}
func (bucket *Bucket) First() (interface{}, interface{}, error) {
db := bucket.badgerDB
var first_k interface{}
var first_v interface{}
err := db.View(func(txn *badger.Txn) error {
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 1
it := txn.NewIterator(opts)
defer it.Close()
it.Seek(prefix)
if (! it.ValidForPrefix(prefix)) {
// empty set
return fmt.Errorf("empty bucket")
}
item := it.Item()
k_prefixed := item.Key()
v_b, err := item.Value()
if err != nil {
return err
}
k_b := k_prefixed[len(prefix):]
err = bucket.UnmarshalKey(k_b, &first_k)
if err != nil {
return err
}
err = bucket.UnmarshalValue(v_b, &first_v)
if err != nil {
return err
}
return nil
})
return first_k, first_v, err
}
func (bucket *Bucket) Last() (interface{}, interface{}, error) {
db := bucket.badgerDB
var last_k interface{}
var last_v interface{}
err := db.View(func(txn *badger.Txn) error {
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 1
opts.Reverse = true
it := txn.NewIterator(opts)
defer it.Close()
it.Seek(prefixBeyondEnd(prefix))
if (! it.ValidForPrefix(prefix)) {
// empty set
return fmt.Errorf("empty bucket")
}
item := it.Item()
k_prefixed := item.Key()
v_b, err := item.Value()
if err != nil {
return err
}
k_b := k_prefixed[len(prefix):]
err = bucket.UnmarshalKey(k_b, &last_k)
if err != nil {
return err
}
err = bucket.UnmarshalValue(v_b, &last_v)
if err != nil {
return err
}
return nil
})
return last_k, last_v, err
}
func (bucket *Bucket) Search(v interface{}, fn BucketCallback) (interface{}, error) {
db := bucket.badgerDB
var found_at interface{}
err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := txn.NewIterator(opts)
defer it.Close()
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
k_prefixed := item.Key()
v_b, err := item.Value()
if err != nil {
return err
}
k_b := k_prefixed[len(prefix):]
var k_i interface{}
var v_i interface{}
err = bucket.UnmarshalKey(k_b, &k_i)
if err != nil {
return err
}
err = bucket.UnmarshalValue(v_b, &v_i)
if err != nil {
return err
}
if v == v_i {
found_at = k_i
if fn != nil {
err = fn(bucket, k_i, v_i)
}
}
}
return nil
})
return found_at, err
}
// SearchOne(cmpFn BucketPredicate, reverse bool) (v interface{}, interface{}, interface{}, error)
// SearchAll(cmpFn BucketPredicate, reverse bool) ([]interface{}, []interface{}, error)
func (bucket *Bucket) SearchOne(v interface{}, cmpFn BucketPredicate, reverse bool) (interface{}, interface{}, error) {
db := bucket.badgerDB
var found_k interface{}
var found_v interface{}
err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
opts.Reverse = reverse
it := txn.NewIterator(opts)
defer it.Close()
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
if reverse {
prefix = prefixBeyondEnd(prefix)
}
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
k_prefixed := item.Key()
v_b, err := item.Value()
if err != nil {
return err
}
k_b := k_prefixed[len(prefix):]
var k_i interface{}
var v_i interface{}
err = bucket.UnmarshalKey(k_b, &k_i)
if err != nil {
return err
}
err = bucket.UnmarshalValue(v_b, &v_i)
if err != nil {
return err
}
if cmpFn != nil {
found, err := cmpFn(bucket, k_i, v_i)
if err != nil {
log.Printf("Bucket.SearchOne - error in cmpFn - k:%v v:%v err:%v", k_i, v_i, err)
return err
}
if found {
found_k = k_i
found_v = v_i
break
}
} else {
if v == v_i {
found_k = k_i
found_v = v_i
break
}
}
}
return nil
})
return found_k, found_v, err
}
func (bucket *Bucket) SearchAll(v interface{}, cmpFn BucketPredicate, reverse bool) ([]interface{}, []interface{}, error) {
db := bucket.badgerDB
var found_k []interface{}
var found_v []interface{}
err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
opts.Reverse = reverse
it := txn.NewIterator(opts)
defer it.Close()
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
if reverse {
prefix = prefixBeyondEnd(prefix)
}
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
k_prefixed := item.Key()
v_b, err := item.Value()
if err != nil {
return err
}
k_b := k_prefixed[len(prefix):]
var k_i interface{}
var v_i interface{}
err = bucket.UnmarshalKey(k_b, &k_i)
if err != nil {
return err
}
err = bucket.UnmarshalValue(v_b, &v_i)
if err != nil {
return err
}
if cmpFn != nil {
found, err := cmpFn(bucket, k_i, v_i)
if err != nil {
log.Printf("Bucket.SearchAll - error in cmpFn k:%v v:%v err:%v", k_i, v_i, err)
return err
}
if found {
found_k = append(found_k, k_i)
found_v = append(found_v, v_i)
}
} else {
if v == v_i {
found_k = append(found_k, k_i)
found_v = append(found_v, v_i)
}
}
}
return nil
})
return found_k, found_v, err
}
// // Count
// // CountWith
// // Empty
func (bucket *Bucket) Count() (int, error) {
db := bucket.badgerDB
count := 0
err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false // key-only iteration
it := txn.NewIterator(opts)
defer it.Close()
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
it.Seek(prefix)
for it.ValidForPrefix(prefix) {
count++
it.Next()
}
return nil
})
return count, err
}
func (bucket *Bucket) Empty() (bool, error) {
db := bucket.badgerDB
empty := true
err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false // key-only iteration
it := txn.NewIterator(opts)
defer it.Close()
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
it.Seek(prefix)
empty = (! it.ValidForPrefix(prefix))
return nil
})
return empty, err
}
// itob returns an 8-byte big endian representation of v.
func itob(v int) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(v))
return b
}
// i64tob returns an 8-byte big endian representation of v.
func i64tob(v int64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(v))
return b
}
// u64tob returns an 8-byte big endian representation of v.
func u64tob(v uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
return b
}
var BucketOptsIntInt = BucketOpts{
MarshalKeyFn: func (v interface{}) ([]byte, error) {
return i64tob(v.(int64)), nil
},
UnmarshalKeyFn: func (data []byte, v *interface{}) error {
*v = int64(binary.BigEndian.Uint64(data))
return nil
},
MarshalValueFn: func (v interface{}) ([]byte, error) {
return i64tob(v.(int64)), nil
},
UnmarshalValueFn: func (data []byte, v *interface{}) error {
*v = int64(binary.BigEndian.Uint64(data))
return nil
},
}
var BucketOptsTimeInt = BucketOpts{
MarshalKeyFn: func (v interface{}) ([]byte, error) {
t, ok := v.(time.Time)
if ! ok {
return nil, fmt.Errorf("not a valid time object: %v", v)
}
//// WARNING: time.RFC3339Nano is not suibucket because it has variable length!
//frac_ns := fmt.Sprintf("%09d", t.Nanosecond())
//val := []byte(fmt.Sprintf("%s$%s", t.Format(time.RFC3339), frac_ns))
//return val, nil
return t.MarshalBinary()
},
UnmarshalKeyFn: func (data []byte, v *interface{}) error {
//s := string(data)
//
//ss := strings.SplitN(s, "$", 2)
//t_base, err := time.Parse(time.RFC3339, ss[0])
//if err != nil {
// return err
//}
//frac_ns, _ := strconv.ParseInt(ss[1], 10, 64)
//t := t_base.Add(time.Nanosecond * time.Duration(frac_ns))
//*v = t
//return nil
t := time.Time{}.UTC()
err := t.UnmarshalBinary(data)
if err != nil {
return err
}
*v = t
return nil
},
MarshalValueFn: func (v interface{}) ([]byte, error) {
return i64tob(v.(int64)), nil
},
UnmarshalValueFn: func (data []byte, v *interface{}) error {
*v = int64(binary.BigEndian.Uint64(data))
return nil
},
}
func prefixBeyondEnd(prefix []byte) []byte {
return append(prefix, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}...) // trick, see https://github.com/dgraph-io/badger/issues/436#issuecomment-400095559
}