124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
package models
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"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
|
|
}
|
|
|
|
func (b *BoltBucket) GetName() string { return b.name }
|
|
func (b *BoltBucket) SetName(nm string) { b.name = nm }
|
|
func (b *BoltBucket) GetBuckets() []BoltBucket { return b.buckets }
|
|
func (b *BoltBucket) GetPairs() []BoltPair { return b.pairs }
|
|
|
|
/*
|
|
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")
|
|
}
|
|
|
|
func (b BoltBucket) Lines() []string {
|
|
var ret []string
|
|
bktPrefix := strings.Repeat(" ", len(b.GetPath())*2)
|
|
wrk := "+ "
|
|
if b.expanded {
|
|
wrk = "- "
|
|
}
|
|
wrk = fmt.Sprintf("%s%s%s", bktPrefix, wrk, b.name)
|
|
ret = append(ret, wrk)
|
|
if b.expanded {
|
|
for i := range b.buckets {
|
|
ret = append(ret, b.buckets[i].Lines()...)
|
|
}
|
|
for _, bp := range b.pairs {
|
|
prPrefix := strings.Repeat(" ", len(bp.GetPath())*2)
|
|
ret = append(ret, fmt.Sprintf("%s%s", prPrefix, bp.String()))
|
|
}
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func (b BoltBucket) IsExpanded() bool { return b.expanded }
|