go: simplify out a wrapper function

This commit is contained in:
mappu 2017-05-21 17:26:09 +12:00
parent 14510545d4
commit 14e7ebb59c

55
main.go
View File

@ -50,6 +50,22 @@ func withBoltDBReference(b ObjectReference, fn func(db *bolt.DB) error) error {
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 {
if len(browse) == 0 {
// 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 db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(browse[0]))
if bucket == nil {
return errors.New("Unknown bucket")
}
for i := 1; i < len(browse); i += 1 {
bucket = bucket.Bucket([]byte(browse[i]))
if bucket == nil {
return errors.New("Unknown bucket")
}
bucket, err := walkBuckets(tx, browse)
if err != nil {
return err
}
// Walked the bucket chain, now run the user callback
@ -99,19 +108,13 @@ func Bolt_CreateBucket(b_ref ObjectReference, browse []string, newBucket string)
} else {
// Deeper bucket
bucket := tx.Bucket([]byte(browse[0]))
if bucket == nil {
return errors.New("Unknown bucket")
}
for i := 1; i < len(browse); i += 1 {
bucket = bucket.Bucket([]byte(browse[i]))
if bucket == nil {
return errors.New("Unknown bucket")
}
bucket, err := walkBuckets(tx, browse)
if err != nil {
return err
}
// Walked the bucket chain, now create the new bucket
_, err := bucket.CreateBucket([]byte(newBucket))
_, err = bucket.CreateBucket([]byte(newBucket))
return err
}
})
@ -131,15 +134,9 @@ func Bolt_DeleteBucket(b_ref ObjectReference, browse []string, delBucket string)
} else {
// Deeper bucket
bucket := tx.Bucket([]byte(browse[0]))
if bucket == nil {
return errors.New("Unknown bucket")
}
for i := 1; i < len(browse); i += 1 {
bucket = bucket.Bucket([]byte(browse[i]))
if bucket == nil {
return errors.New("Unknown bucket")
}
bucket, err := walkBuckets(tx, browse)
if err != nil {
return err
}
// Walked the bucket chain, now delete the selected bucket