bolt: support adding/removing child buckets

This commit is contained in:
mappu 2024-06-28 11:53:07 +12:00
parent 975e120530
commit 15b29b32ce

View File

@ -94,8 +94,52 @@ func (ld *boltLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {
return boltChildBucketNames(ld.db, ndata.bucketPath) return boltChildBucketNames(ld.db, ndata.bucketPath)
} }
func (ld *boltLoadedDatabase) NavContext(ndata *navData) ([]contextAction, error) { func (ld *boltLoadedDatabase) NavContext(ndata *navData) (ret []contextAction, err error) {
return nil, nil // No special actions are supported ret = append(ret, contextAction{"Add bucket...", ld.AddChildBucket})
if len(ndata.bucketPath) > 0 {
ret = append(ret, contextAction{"Delete bucket", ld.DeleteBucket})
}
return
}
func (ld *boltLoadedDatabase) AddChildBucket(ndata *navData) {
bucketName := ""
if !vcl.InputQuery(APPNAME, "Enter a name for the new bucket:", &bucketName) {
return // cancel
}
err := ld.db.Update(func(tx *bbolt.Tx) error {
parent := boltTargetBucket(tx, ndata.bucketPath)
if parent != nil {
_, err := parent.CreateBucket([]byte(bucketName))
return err
}
// Top-level
_, err := tx.CreateBucket([]byte(bucketName))
return err
})
if err != nil {
vcl.ShowMessageFmt("Error adding bucket: %v", err)
}
}
func (ld *boltLoadedDatabase) DeleteBucket(ndata *navData) {
err := ld.db.Update(func(tx *bbolt.Tx) error {
// Find parent of this bucket.
if len(ndata.bucketPath) >= 2 {
// child bucket
parent := boltTargetBucket(tx, ndata.bucketPath[0:len(ndata.bucketPath)-1])
return parent.DeleteBucket([]byte(ndata.bucketPath[len(ndata.bucketPath)-1]))
} else {
// top-level bucket
return tx.DeleteBucket([]byte(ndata.bucketPath[0]))
}
})
if err != nil {
vcl.ShowMessageFmt("Error deleting bucket %q: %v", strings.Join(ndata.bucketPath, `/`), err)
}
} }
func (ld *boltLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView) { func (ld *boltLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView) {