package main import ( "fmt" "path/filepath" "sort" "strings" "time" "unsafe" "github.com/ying32/govcl/vcl" "go.etcd.io/bbolt" ) type boltLoadedDatabase struct { displayName string path string db *bbolt.DB nav *vcl.TTreeNode arena []*navData // keepalive } func (ld *boltLoadedDatabase) DisplayName() string { return ld.displayName } func (ld *boltLoadedDatabase) RootElement() *vcl.TTreeNode { return ld.nav } func (ld *boltLoadedDatabase) Keepalive(ndata *navData) { ld.arena = append(ld.arena, ndata) } func (ld *boltLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) { // Load properties bucketDisplayName := strings.Join(ndata.bucketPath, `/`) content := fmt.Sprintf("Selected database: %#v\n\n\nSelected bucket: %q\n", ld.db.Stats(), bucketDisplayName) f.propertiesBox.SetText(content) // Load data f.contentBox.SetEnabled(false) f.contentBox.Clear() err := ld.db.View(func(tx *bbolt.Tx) error { b := boltTargetBucket(tx, ndata.bucketPath) if b == nil { // no such bucket return nil } // Valid f.contentBox.Clear() c := b.Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { dataEntry := f.contentBox.Items().Add() dataEntry.SetCaption(formatUtf8(k)) dataEntry.SubItems().Add(formatUtf8(v)) } return nil }) if err != nil { vcl.ShowMessage(fmt.Sprintf("Failed to load data for bucket %q: %s", bucketDisplayName, err.Error())) return } // Valid f.contentBox.SetEnabled(true) } func (ld *boltLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { // In the bolt implementation, the nav is a recursive tree of child buckets return boltChildBucketNames(ld.db, ndata.bucketPath) } var _ loadedDatabase = &boltLoadedDatabase{} // interface assertion // func (f *TMainForm) addDatabaseFromFile(path string) { // TODO load in background thread to stop blocking the UI db, err := bbolt.Open(path, 0644, &bbolt.Options{Timeout: 1 * time.Second}) if err != nil { vcl.ShowMessage(fmt.Sprintf("Failed to load database '%s': %s", path, err.Error())) return } ld := &boltLoadedDatabase{ path: path, displayName: filepath.Base(path), db: db, } ld.nav = f.Buckets.Items().Add(nil, ld.displayName) ld.nav.SetHasChildren(true) // dynamically populate in OnNavExpanding navData := &navData{ ld: ld, childrenLoaded: false, // will be loaded dynamically bucketPath: []string{}, // empty = root } ld.nav.SetData(unsafe.Pointer(navData)) f.dbs = append(f.dbs, ld) ld.Keepalive(navData) } func boltTargetBucket(tx *bbolt.Tx, path []string) *bbolt.Bucket { // If we are already deep in buckets, go directly there to find children if len(path) == 0 { return nil } b := tx.Bucket([]byte(path[0])) if b == nil { return nil // unexpectedly missing } for i := 1; i < len(path); i += 1 { b = b.Bucket([]byte(path[i])) if b == nil { return nil // unexpectedly missing } } return b // OK } func boltChildBucketNames(db *bbolt.DB, path []string) ([]string, error) { var nextBucketNames []string err := db.View(func(tx *bbolt.Tx) error { // If we are already deep in buckets, go directly there to find children if len(path) > 0 { b := tx.Bucket([]byte(path[0])) if b == nil { return fmt.Errorf("Unexpected missing root bucket %q", path[0]) } for i := 1; i < len(path); i += 1 { b = b.Bucket([]byte(path[i])) if b == nil { return fmt.Errorf("Unexpected missing bucket %q", strings.Join(path[0:i], `/`)) } } // Find child buckets of this bucket b.ForEachBucket(func(bucketName []byte) error { nextBucketNames = append(nextBucketNames, string(bucketName)) return nil }) } else { // Find root bucket names return tx.ForEach(func(bucketName []byte, _ *bbolt.Bucket) error { nextBucketNames = append(nextBucketNames, string(bucketName)) return nil }) } // OK return nil }) if err != nil { return nil, err } sort.Strings(nextBucketNames) return nextBucketNames, nil }