Skip to content

Commit

Permalink
Merge pull request #606 from Elbehery/refactor_finding_last_bucket_in…
Browse files Browse the repository at this point in the history
…_subBuckets

fix: refactor findingLastBucket into a func
  • Loading branch information
ahrtr authored Nov 22, 2023
2 parents 574235f + b5aaaeb commit 231772f
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,19 +939,13 @@ func (cmd *keysCommand) Run(args ...string) error {
// Print keys.
return db.View(func(tx *bolt.Tx) error {
// Find bucket.
var lastbucket *bolt.Bucket = tx.Bucket([]byte(buckets[0]))
if lastbucket == nil {
return berrors.ErrBucketNotFound
}
for _, bucket := range buckets[1:] {
lastbucket = lastbucket.Bucket([]byte(bucket))
if lastbucket == nil {
return berrors.ErrBucketNotFound
}
lastBucket, err := findLastBucket(tx, buckets)
if err != nil {
return err
}

// Iterate over each key.
return lastbucket.ForEach(func(key, _ []byte) error {
return lastBucket.ForEach(func(key, _ []byte) error {
return writelnBytes(cmd.Stdout, key, *optionsFormat)
})
})
Expand Down Expand Up @@ -1030,19 +1024,13 @@ func (cmd *getCommand) Run(args ...string) error {
// Print value.
return db.View(func(tx *bolt.Tx) error {
// Find bucket.
var lastbucket *bolt.Bucket = tx.Bucket([]byte(buckets[0]))
if lastbucket == nil {
return berrors.ErrBucketNotFound
}
for _, bucket := range buckets[1:] {
lastbucket = lastbucket.Bucket([]byte(bucket))
if lastbucket == nil {
return berrors.ErrBucketNotFound
}
lastBucket, err := findLastBucket(tx, buckets)
if err != nil {
return err
}

// Find value for given key.
val := lastbucket.Get(key)
val := lastBucket.Get(key)
if val == nil {
return fmt.Errorf("Error %w for key: %q hex: \"%x\"", ErrKeyNotFound, key, string(key))
}
Expand Down Expand Up @@ -1718,3 +1706,17 @@ func (_ cmdKvStringer) ValueToString(value []byte) string {
func CmdKvStringer() bolt.KVStringer {
return cmdKvStringer{}
}

func findLastBucket(tx *bolt.Tx, bucketNames []string) (*bolt.Bucket, error) {
var lastbucket *bolt.Bucket = tx.Bucket([]byte(bucketNames[0]))
if lastbucket == nil {
return nil, berrors.ErrBucketNotFound
}
for _, bucket := range bucketNames[1:] {
lastbucket = lastbucket.Bucket([]byte(bucket))
if lastbucket == nil {
return nil, berrors.ErrBucketNotFound
}
}
return lastbucket, nil
}

0 comments on commit 231772f

Please sign in to comment.