-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathbolt_model.go
791 lines (750 loc) · 18.3 KB
/
bolt_model.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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
package main
import (
"errors"
"fmt"
"os"
"strings"
"go.etcd.io/bbolt"
)
/*
BoltDB A Database, basically a collection of buckets
*/
type BoltDB struct {
buckets []BoltBucket
}
/*
BoltBucket is just a struct representation of a Bucket in the Bolt DB
*/
type BoltBucket struct {
name string
pairs []BoltPair
buckets []BoltBucket
parent *BoltBucket
expanded bool
errorFlag bool
isRoot bool
}
/*
BoltPair is just a struct representation of a Pair in the Bolt DB
*/
type BoltPair struct {
parent *BoltBucket
key string
val string
}
func (bd *BoltDB) getGenericFromPath(path []string) (*BoltBucket, *BoltPair, error) {
// Check if 'path' leads to a pair
p, err := bd.getPairFromPath(path)
if err == nil {
return nil, p, nil
}
// Nope, check if it leads to a bucket
b, err := bd.getBucketFromPath(path)
if err == nil {
return b, nil, nil
}
// Nope, error
return nil, nil, errors.New("Invalid Path")
}
func (bd *BoltDB) getBucketFromPath(path []string) (*BoltBucket, error) {
if len(path) > 0 {
// Find the BoltBucket with a path == path
var b *BoltBucket
var err error
// Find the root bucket
b, err = memBolt.getBucket(path[0])
if err != nil {
return nil, err
}
if len(path) > 1 {
for p := 1; p < len(path); p++ {
b, err = b.getBucket(path[p])
if err != nil {
return nil, err
}
}
}
return b, nil
}
return nil, errors.New("Invalid Path")
}
func (bd *BoltDB) getPairFromPath(path []string) (*BoltPair, error) {
if len(path) <= 0 {
return nil, errors.New("No Path")
}
b, err := bd.getBucketFromPath(path[:len(path)-1])
if err != nil {
return nil, err
}
// Found the bucket, pull out the pair
p, err := b.getPair(path[len(path)-1])
return p, err
}
func (bd *BoltDB) getVisibleItemCount(path []string) (int, error) {
vis := 0
var retErr error
if len(path) == 0 {
for i := range bd.buckets {
n, err := bd.getVisibleItemCount(bd.buckets[i].GetPath())
if err != nil {
return 0, err
}
vis += n
}
} else {
b, err := bd.getBucketFromPath(path)
if err != nil {
return 0, err
}
// 1 for the bucket
vis++
if b.expanded {
// This bucket is expanded, add up it's children
// * 1 for each pair
vis += len(b.pairs)
// * recurse for buckets
for i := range b.buckets {
n, err := bd.getVisibleItemCount(b.buckets[i].GetPath())
if err != nil {
return 0, err
}
vis += n
}
}
}
return vis, retErr
}
func (bd *BoltDB) buildVisiblePathSlice(filter string) ([][]string, error) {
var retSlice [][]string
var retErr error
// The root path, recurse for root buckets
for i := range bd.buckets {
bktS, bktErr := bd.buckets[i].buildVisiblePathSlice([]string{}, filter)
if bktErr == nil {
retSlice = append(retSlice, bktS...)
} else {
// Something went wrong, set the error flag
bd.buckets[i].errorFlag = true
}
}
return retSlice, retErr
}
func (bd *BoltDB) isVisiblePath(path []string, filter string) bool {
visPaths, err := bd.buildVisiblePathSlice(filter)
if err != nil {
return false
}
for _, pth := range visPaths {
if len(pth) != len(path) {
continue
}
isVisible := true
for i := range path {
if path[i] != pth[i] {
isVisible = false
break
}
}
if isVisible {
return true
}
}
return false
}
func (bd *BoltDB) getPrevVisiblePath(path []string, filter string) []string {
visPaths, err := bd.buildVisiblePathSlice(filter)
if path == nil {
if len(visPaths) > 0 {
return visPaths[len(visPaths)-1]
}
return nil
}
if err == nil {
for idx, pth := range visPaths {
isCurPath := true
for i := range path {
if len(pth) <= i || path[i] != pth[i] {
isCurPath = false
break
}
}
if isCurPath && idx > 0 {
return visPaths[idx-1]
}
}
}
return nil
}
func (bd *BoltDB) getNextVisiblePath(path []string, filter string) []string {
visPaths, err := bd.buildVisiblePathSlice(filter)
if path == nil {
if len(visPaths) > 0 {
return visPaths[0]
}
return nil
}
if err == nil {
for idx, pth := range visPaths {
isCurPath := true
for i := range path {
if len(pth) <= i || path[i] != pth[i] {
isCurPath = false
break
}
}
if isCurPath && len(visPaths) > idx+1 {
return visPaths[idx+1]
}
}
}
return nil
}
func (bd *BoltDB) toggleOpenBucket(path []string) error {
// Find the BoltBucket with a path == path
b, err := bd.getBucketFromPath(path)
if err == nil {
b.expanded = !b.expanded
}
return err
}
func (bd *BoltDB) closeBucket(path []string) error {
// Find the BoltBucket with a path == path
b, err := bd.getBucketFromPath(path)
if err == nil {
b.expanded = false
}
return err
}
func (bd *BoltDB) openBucket(path []string) error {
// Find the BoltBucket with a path == path
b, err := bd.getBucketFromPath(path)
if err == nil {
b.expanded = true
}
return err
}
func (bd *BoltDB) getBucket(k string) (*BoltBucket, error) {
for i := range bd.buckets {
if bd.buckets[i].name == k {
return &bd.buckets[i], nil
}
}
return nil, errors.New("Bucket Not Found")
}
func (bd *BoltDB) openAllBuckets() {
for i := range bd.buckets {
bd.buckets[i].openAllBuckets()
bd.buckets[i].expanded = true
}
}
func (bd *BoltDB) syncOpenBuckets(shadow *BoltDB) {
// First test this bucket
for i := range bd.buckets {
for j := range shadow.buckets {
if bd.buckets[i].name == shadow.buckets[j].name {
bd.buckets[i].syncOpenBuckets(&shadow.buckets[j])
}
}
}
}
func (bd *BoltDB) refreshDatabase() *BoltDB {
// Reload the database into memBolt
memBolt = new(BoltDB)
db.View(func(tx *bbolt.Tx) error {
err := tx.ForEach(func(nm []byte, b *bbolt.Bucket) error {
bb, err := readBucket(b)
if err == nil {
bb.name = string(nm)
bb.expanded = false
memBolt.buckets = append(memBolt.buckets, *bb)
return nil
}
return err
})
if err != nil {
memBolt = new(BoltDB)
// Check if there are key/values directly in the root
bb, err := readBucket(tx.Cursor().Bucket())
if err == nil {
bb.isRoot = true
bb.expanded = true
memBolt.buckets = append(memBolt.buckets, *bb)
return nil
}
return err
}
return err
})
return memBolt
}
/*
GetPath returns the database path leading to this BoltBucket
*/
func (b *BoltBucket) GetPath() []string {
if b.parent != nil {
return append(b.parent.GetPath(), b.name)
}
return []string{b.name}
}
/*
buildVisiblePathSlice builds a slice of string slices containing all visible paths in this bucket
The passed prefix is the path leading to the current bucket
*/
func (b *BoltBucket) buildVisiblePathSlice(prefix []string, filter string) ([][]string, error) {
var retSlice [][]string
var retErr error
retSlice = append(retSlice, append(prefix, b.name))
if b.expanded {
// Add subbuckets
for i := range b.buckets {
bktS, bktErr := b.buckets[i].buildVisiblePathSlice(append(prefix, b.name), filter)
if bktErr != nil {
return retSlice, bktErr
}
retSlice = append(retSlice, bktS...)
}
// Add pairs
for i := range b.pairs {
if filter != "" && !strings.Contains(b.pairs[i].key, filter) {
continue
}
retSlice = append(retSlice, append(append(prefix, b.name), b.pairs[i].key))
}
}
return retSlice, retErr
}
func (b *BoltBucket) syncOpenBuckets(shadow *BoltBucket) {
// First test this bucket
b.expanded = shadow.expanded
for i := range b.buckets {
for j := range shadow.buckets {
if b.buckets[i].name == shadow.buckets[j].name {
b.buckets[i].syncOpenBuckets(&shadow.buckets[j])
}
}
}
}
func (b *BoltBucket) openAllBuckets() {
for i := range b.buckets {
b.buckets[i].openAllBuckets()
b.buckets[i].expanded = true
}
}
func (b *BoltBucket) getBucket(k string) (*BoltBucket, error) {
for i := range b.buckets {
if b.buckets[i].name == k {
return &b.buckets[i], nil
}
}
return nil, errors.New("Bucket Not Found")
}
func (b *BoltBucket) getPair(k string) (*BoltPair, error) {
for i := range b.pairs {
if b.pairs[i].key == k {
return &b.pairs[i], nil
}
}
return nil, errors.New("Pair Not Found")
}
/*
GetPath Returns the path of the BoltPair
*/
func (p *BoltPair) GetPath() []string {
return append(p.parent.GetPath(), p.key)
}
/* This is a go-between function (between the boltbrowser structs
* above, and the bolt convenience functions below)
* for taking a boltbrowser bucket and recursively adding it
* and all of it's children into the database.
* Mainly used for moving a bucket from one path to another
* as in the 'renameBucket' function below.
*/
func addBucketFromBoltBucket(path []string, bb *BoltBucket) error {
if err := insertBucket(path, bb.name); err == nil {
bucketPath := append(path, bb.name)
for i := range bb.pairs {
if err = insertPair(bucketPath, bb.pairs[i].key, bb.pairs[i].val); err != nil {
return err
}
}
for i := range bb.buckets {
if err = addBucketFromBoltBucket(bucketPath, &bb.buckets[i]); err != nil {
return err
}
}
}
return nil
}
func deleteKey(path []string) error {
if AppArgs.ReadOnly {
return errors.New("DB is in Read-Only Mode")
}
err := db.Update(func(tx *bbolt.Tx) error {
// len(b.path)-1 is the key we need to delete,
// the rest are buckets leading to that key
if len(path) == 1 {
// Deleting a root bucket
return tx.DeleteBucket([]byte(path[0]))
}
b := tx.Bucket([]byte(path[0]))
if b == nil {
// Invalid path, try for the root bucket
b = tx.Cursor().Bucket()
}
if b != nil {
if len(path) > 1 {
for i := range path[1 : len(path)-1] {
b = b.Bucket([]byte(path[i+1]))
if b == nil {
return errors.New("deleteKey: Invalid Path")
}
}
}
// Now delete the last key in the path
var err error
if deleteBkt := b.Bucket([]byte(path[len(path)-1])); deleteBkt == nil {
// Must be a pair
err = b.Delete([]byte(path[len(path)-1]))
} else {
err = b.DeleteBucket([]byte(path[len(path)-1]))
}
return err
}
return errors.New("deleteKey: Invalid Path")
})
return err
}
func readBucket(b *bbolt.Bucket) (*BoltBucket, error) {
bb := new(BoltBucket)
if b == nil {
return nil, errors.New("No bucket passed")
}
b.ForEach(func(k, v []byte) error {
if v == nil {
tb, err := readBucket(b.Bucket(k))
tb.parent = bb
if err == nil {
tb.name = string(k)
bb.buckets = append(bb.buckets, *tb)
}
} else {
tp := BoltPair{key: string(k), val: string(v)}
tp.parent = bb
bb.pairs = append(bb.pairs, tp)
}
return nil
})
return bb, nil
}
func renameBucket(path []string, name string) error {
if name == path[len(path)-1] {
// No change requested
return nil
}
var bb *BoltBucket // For caching the current bucket
err := db.View(func(tx *bbolt.Tx) error {
// len(b.path)-1 is the key we need to delete,
// the rest are buckets leading to that key
b := tx.Bucket([]byte(path[0]))
if b == nil {
// Invalid path, try for the root bucket
b = tx.Cursor().Bucket()
}
if b != nil {
if len(path) > 1 {
for i := range path[1:] {
b = b.Bucket([]byte(path[i+1]))
if b == nil {
return errors.New("renameBucket: Invalid Path")
}
}
}
var err error
// Ok, cache b
bb, err = readBucket(b)
if err != nil {
return err
}
} else {
return errors.New("renameBucket: Invalid Bucket")
}
return nil
})
if err != nil {
return err
}
if bb == nil {
return errors.New("renameBucket: Couldn't find Bucket")
}
// Ok, we have the bucket cached, now delete the current instance
if err = deleteKey(path); err != nil {
return err
}
// Rechristen our cached bucket
bb.name = name
// And re-add it
parentPath := path[:len(path)-1]
if err = addBucketFromBoltBucket(parentPath, bb); err != nil {
return err
}
return nil
}
func updatePairKey(path []string, k string) error {
if AppArgs.ReadOnly {
return errors.New("DB is in Read-Only Mode")
}
err := db.Update(func(tx *bbolt.Tx) error {
// len(b.path)-1 is the key for the pair we're updating,
// the rest are buckets leading to that key
b := tx.Bucket([]byte(path[0]))
if b == nil {
// Invalid path, try for the root bucket
b = tx.Cursor().Bucket()
}
if b != nil {
if len(path) > 0 {
for i := range path[1 : len(path)-1] {
b = b.Bucket([]byte(path[i+1]))
if b == nil {
return errors.New("updatePairValue: Invalid Path")
}
}
}
bk := []byte(path[len(path)-1])
v := b.Get(bk)
err := b.Delete(bk)
if err == nil {
// Old pair has been deleted, now add the new one
err = b.Put([]byte(k), v)
}
// Now update the last key in the path
return err
}
return errors.New("updatePairValue: Invalid Path")
})
return err
}
func updatePairValue(path []string, v string) error {
if AppArgs.ReadOnly {
return errors.New("DB is in Read-Only Mode")
}
err := db.Update(func(tx *bbolt.Tx) error {
// len(b.GetPath())-1 is the key for the pair we're updating,
// the rest are buckets leading to that key
b := tx.Bucket([]byte(path[0]))
if b == nil {
// Invalid path, try for the root bucket
b = tx.Cursor().Bucket()
}
if b != nil {
if len(path) > 0 {
for i := range path[1 : len(path)-1] {
b = b.Bucket([]byte(path[i+1]))
if b == nil {
return errors.New("updatePairValue: Invalid Path")
}
}
}
// Now update the last key in the path
err := b.Put([]byte(path[len(path)-1]), []byte(v))
return err
}
return errors.New("updatePairValue: Invalid Path")
})
return err
}
func insertBucket(path []string, n string) error {
if AppArgs.ReadOnly {
return errors.New("DB is in Read-Only Mode")
}
// Inserts a new bucket named 'n' at 'path'
err := db.Update(func(tx *bbolt.Tx) error {
if len(path) == 0 || path[0] == "" {
// insert at root
_, err := tx.CreateBucket([]byte(n))
if err != nil {
return fmt.Errorf("insertBucket: %s", err)
}
} else {
rootBucket, path := path[0], path[1:]
b := tx.Bucket([]byte(rootBucket))
if b != nil {
for len(path) > 0 {
tstBucket := ""
tstBucket, path = path[0], path[1:]
nB := b.Bucket([]byte(tstBucket))
if nB == nil {
// Not a bucket, if we're out of path, just move on
if len(path) != 0 {
// Out of path, error
return errors.New("insertBucket: Invalid Path 1")
}
} else {
b = nB
}
}
_, err := b.CreateBucket([]byte(n))
return err
}
return fmt.Errorf("insertBucket: Invalid Path %s", rootBucket)
}
return nil
})
return err
}
func insertPair(path []string, k string, v string) error {
if AppArgs.ReadOnly {
return errors.New("DB is in Read-Only Mode")
}
// Insert a new pair k => v at path
err := db.Update(func(tx *bbolt.Tx) error {
if len(path) == 0 {
// We cannot insert a pair at root
return errors.New("insertPair: Cannot insert pair at root")
}
var err error
b := tx.Bucket([]byte(path[0]))
if b == nil {
// Invalid path, try for the root bucket
b = tx.Cursor().Bucket()
}
if b != nil {
if len(path) > 0 {
for i := 1; i < len(path); i++ {
b = b.Bucket([]byte(path[i]))
if b == nil {
return fmt.Errorf("insertPair: %s", err)
}
}
}
err := b.Put([]byte(k), []byte(v))
if err != nil {
return fmt.Errorf("insertPair: %s", err)
}
}
return nil
})
return err
}
func exportValue(path []string, fName string) error {
return db.View(func(tx *bbolt.Tx) error {
// len(b.path)-1 is the key whose value we want to export
// the rest are buckets leading to that key
b := tx.Bucket([]byte(path[0]))
if b == nil {
// Invalid path, try for the root bucket
b = tx.Cursor().Bucket()
}
if b != nil {
if len(path) > 1 {
for i := range path[1 : len(path)-1] {
b = b.Bucket([]byte(path[i+1]))
if b == nil {
return errors.New("exportValue: Invalid Path: " + strings.Join(path, "/"))
}
}
}
bk := []byte(path[len(path)-1])
v := b.Get(bk)
return writeToFile(fName, string(v), os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
}
return errors.New("exportValue: Invalid Bucket")
})
}
func exportJSON(path []string, fName string) error {
return db.View(func(tx *bbolt.Tx) error {
// len(b.path)-1 is the key whose value we want to export
// the rest are buckets leading to that key
b := tx.Bucket([]byte(path[0]))
if b == nil {
// Invalid path, try for the root bucket
b = tx.Cursor().Bucket()
}
if b != nil {
if len(path) > 1 {
for i := range path[1 : len(path)-1] {
b = b.Bucket([]byte(path[i+1]))
if b == nil {
return errors.New("exportValue: Invalid Path: " + strings.Join(path, "/"))
}
}
}
bk := []byte(path[len(path)-1])
if v := b.Get(bk); v != nil {
return writeToFile(fName, "{\""+string(bk)+"\":\""+string(v)+"\"}", os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
}
if b.Bucket(bk) != nil {
return writeToFile(fName, genJSONString(b.Bucket(bk)), os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
}
return writeToFile(fName, genJSONString(b), os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
}
return errors.New("exportValue: Invalid Bucket")
})
}
func genJSONString(b *bbolt.Bucket) string {
ret := "{"
b.ForEach(func(k, v []byte) error {
ret = fmt.Sprintf("%s\"%s\":", ret, string(k))
if v == nil {
ret = fmt.Sprintf("%s%s,", ret, genJSONString(b.Bucket(k)))
} else {
ret = fmt.Sprintf("%s\"%s\",", ret, string(v))
}
return nil
})
ret = fmt.Sprintf("%s}", ret[:len(ret)-1])
return ret
}
func logToFile(s string) error {
return writeToFile("bolt-log", s+"\n", os.O_RDWR|os.O_APPEND)
}
func writeToFile(fn, s string, mode int) error {
var f *os.File
var err error
if f == nil {
f, err = os.OpenFile(fn, mode, 0660)
}
defer f.Close()
if err != nil {
return err
}
if _, err = f.WriteString(s); err != nil {
return err
}
if err = f.Sync(); err != nil {
return err
}
return nil
}
func importValue(path []string, fName string) error {
if AppArgs.ReadOnly {
return errors.New("DB is in Read-Only Mode")
}
return db.Update(func(tx *bbolt.Tx) error {
// len(b.GetPath())-1 is the key for the pair we're updating,
// the rest are buckets leading to that key
b := tx.Bucket([]byte(path[0]))
if b == nil {
// Invalid path, try for the root bucket
b = tx.Cursor().Bucket()
}
if b != nil {
if len(path) > 0 {
for i := range path[1 : len(path)-1] {
b = b.Bucket([]byte(path[i+1]))
if b == nil {
return errors.New("updatePairValue: Invalid Path")
}
}
}
// Now update the last key in the path
bk := []byte(path[len(path)-1])
v, err := os.ReadFile(fName)
if err != nil {
return err
}
return b.Put(bk, v)
}
return errors.New("importValue: Invalid Bucket")
})
}