Skip to content
Snippets Groups Projects
Commit 09ce4c36 authored by gbe's avatar gbe
Browse files

Implement nested bucket delete in DB inspection tool

parent 22a5fcbb
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
// getBucket splits the given path using / as the separator and returns a nested bucket with the given path, if it exists. // getBucket splits the given path using / as the separator and returns a nested bucket with the given path, if it exists.
func getBucket(tx *bolt.Tx, path string) *bolt.Bucket { func getBucket(tx *bolt.Tx, path string) *bolt.Bucket {
parts := strings.Split(path, "/") parts := strings.Split(strings.Trim(path, "/"), "/")
if len(parts) == 0 { if len(parts) == 0 {
return nil return nil
} }
...@@ -21,7 +21,7 @@ func getBucket(tx *bolt.Tx, path string) *bolt.Bucket { ...@@ -21,7 +21,7 @@ func getBucket(tx *bolt.Tx, path string) *bolt.Bucket {
parts = parts[1:] parts = parts[1:]
for len(parts) > 0 { for len(parts) > 0 {
bucket = tx.Bucket([]byte(parts[0])) bucket = bucket.Bucket([]byte(parts[0]))
if bucket == nil { if bucket == nil {
return nil return nil
} }
...@@ -64,21 +64,35 @@ func commandList(db *bolt.DB, args []string) error { ...@@ -64,21 +64,35 @@ func commandList(db *bolt.DB, args []string) error {
}) })
} }
type bucketDeleter interface {
DeleteBucket([]byte) error
Bucket([]byte) *bolt.Bucket
}
func commandDeleteBucket(db *bolt.DB, args []string) error { func commandDeleteBucket(db *bolt.DB, args []string) error {
if len(args) != 1 { if len(args) != 1 {
return errors.New("expected exactly one argument") return errors.New("expected exactly one argument")
} }
path := strings.Split(args[0], "/") path := strings.Split(strings.Trim(args[0], "/"), "/")
fmt.Println("removing", path) fmt.Println("removing", path)
return db.Update(func(tx *bolt.Tx) error { return db.Update(func(tx *bolt.Tx) error {
if len(path) != 1 { var del bucketDeleter
return errors.New("don't know how to do nested delete yet")
del = tx
for len(path) > 1 {
del = del.Bucket([]byte(path[0]))
if del == nil {
return errors.New("not found")
}
path = path[1:]
} }
return tx.DeleteBucket([]byte(path[0])) return del.DeleteBucket([]byte(path[0]))
}) })
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment