go: simplify out a wrapper function
This commit is contained in:
parent
14510545d4
commit
14e7ebb59c
55
main.go
55
main.go
@ -50,6 +50,22 @@ func withBoltDBReference(b ObjectReference, fn func(db *bolt.DB) error) error {
|
|||||||
return fn(ptrDB)
|
return fn(ptrDB)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func walkBuckets(tx *bolt.Tx, browse []string) (*bolt.Bucket, error) {
|
||||||
|
bucket := tx.Bucket([]byte(browse[0]))
|
||||||
|
if bucket == nil {
|
||||||
|
return nil, errors.New("Unknown bucket")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 1; i < len(browse); i += 1 {
|
||||||
|
bucket = bucket.Bucket([]byte(browse[i]))
|
||||||
|
if bucket == nil {
|
||||||
|
return nil, errors.New("Unknown bucket")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bucket, nil
|
||||||
|
}
|
||||||
|
|
||||||
func withBrowse_ReadOnly(b_ref ObjectReference, browse []string, fn func(db *bolt.DB, tx *bolt.Tx, bucket *bolt.Bucket) error) error {
|
func withBrowse_ReadOnly(b_ref ObjectReference, browse []string, fn func(db *bolt.DB, tx *bolt.Tx, bucket *bolt.Bucket) error) error {
|
||||||
if len(browse) == 0 {
|
if len(browse) == 0 {
|
||||||
// not a bucket
|
// not a bucket
|
||||||
@ -59,16 +75,9 @@ func withBrowse_ReadOnly(b_ref ObjectReference, browse []string, fn func(db *bol
|
|||||||
return withBoltDBReference(b_ref, func(db *bolt.DB) error {
|
return withBoltDBReference(b_ref, func(db *bolt.DB) error {
|
||||||
return db.View(func(tx *bolt.Tx) error {
|
return db.View(func(tx *bolt.Tx) error {
|
||||||
|
|
||||||
bucket := tx.Bucket([]byte(browse[0]))
|
bucket, err := walkBuckets(tx, browse)
|
||||||
if bucket == nil {
|
if err != nil {
|
||||||
return errors.New("Unknown bucket")
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
for i := 1; i < len(browse); i += 1 {
|
|
||||||
bucket = bucket.Bucket([]byte(browse[i]))
|
|
||||||
if bucket == nil {
|
|
||||||
return errors.New("Unknown bucket")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Walked the bucket chain, now run the user callback
|
// Walked the bucket chain, now run the user callback
|
||||||
@ -99,19 +108,13 @@ func Bolt_CreateBucket(b_ref ObjectReference, browse []string, newBucket string)
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Deeper bucket
|
// Deeper bucket
|
||||||
bucket := tx.Bucket([]byte(browse[0]))
|
bucket, err := walkBuckets(tx, browse)
|
||||||
if bucket == nil {
|
if err != nil {
|
||||||
return errors.New("Unknown bucket")
|
return err
|
||||||
}
|
|
||||||
for i := 1; i < len(browse); i += 1 {
|
|
||||||
bucket = bucket.Bucket([]byte(browse[i]))
|
|
||||||
if bucket == nil {
|
|
||||||
return errors.New("Unknown bucket")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Walked the bucket chain, now create the new bucket
|
// Walked the bucket chain, now create the new bucket
|
||||||
_, err := bucket.CreateBucket([]byte(newBucket))
|
_, err = bucket.CreateBucket([]byte(newBucket))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -131,15 +134,9 @@ func Bolt_DeleteBucket(b_ref ObjectReference, browse []string, delBucket string)
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Deeper bucket
|
// Deeper bucket
|
||||||
bucket := tx.Bucket([]byte(browse[0]))
|
bucket, err := walkBuckets(tx, browse)
|
||||||
if bucket == nil {
|
if err != nil {
|
||||||
return errors.New("Unknown bucket")
|
return err
|
||||||
}
|
|
||||||
for i := 1; i < len(browse); i += 1 {
|
|
||||||
bucket = bucket.Bucket([]byte(browse[i]))
|
|
||||||
if bucket == nil {
|
|
||||||
return errors.New("Unknown bucket")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Walked the bucket chain, now delete the selected bucket
|
// Walked the bucket chain, now delete the selected bucket
|
||||||
|
Loading…
Reference in New Issue
Block a user