boltbrowser/models/bucket.go

95 lines
2.1 KiB
Go
Raw Normal View History

2022-04-14 16:32:39 +00:00
package models
import (
"errors"
"strings"
)
/*
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
}
/*
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")
}