goboltdb

Can I have nested bucket under a nested bucket in boltdb?


This is what I have to create nested buckets. It does not return any error but fails at creating nested bucket under another nested bucket.

func CreateNestedBuckets(buckets []string) error {
err := db.Update(func(tx *bolt.Tx) error {
    var bkt *bolt.Bucket
    var err error
    first := true
    for _, bucket := range buckets {
        log.Error(bucket)
        if first == true {
            bkt, err = tx.CreateBucketIfNotExists([]byte(bucket))
            first = false
        } else {
            bkt, err = bkt.CreateBucketIfNotExists([]byte(bucket))
        }
        if err != nil {
            log.Error("error creating nested bucket")
            return err
        }
    }
    return nil
})
if err != nil {
    log.Error("error creating nested bucket!!!")
    return err
}
return nil
}

Solution

  • Short answer: yes! You can have nested buckets: https://twitter.com/boltdb/status/454730212010254336

    Long answer: your code works fine! Heres some things to check though:

    I've ran your code with the following setup (a couple of small changes but nothing major) and it works fine:

    package main
    
    import (
        "log"
        "os"
        "time"
    
        "github.com/boltdb/bolt"
    )
    
    var dbname = "test.bdb"
    var dbperms os.FileMode = 0770
    var options = &bolt.Options{Timeout: 1 * time.Second}
    
    func main() {
        var names []string
        names = append(names, "bucketOne")
        names = append(names, "bucketTwo")
        names = append(names, "bucketThree")
    
        if err := CreateNestedBuckets(names); err != nil {
            log.Fatal(err)
        }
    }
    
    // CreateNestedBuckets - Function to create
    // nested buckets from an array of Strings
    func CreateNestedBuckets(buckets []string) error {
        db, dberr := bolt.Open(dbname, dbperms, options)
        if dberr != nil {
            log.Fatal(dberr)
        }
        defer db.Close()
    
        err := db.Update(func(tx *bolt.Tx) error {
            var bkt *bolt.Bucket
            var err error
            first := true
            for _, bucket := range buckets {
                log.Println(bucket)
                if first == true {
                    bkt, err = tx.CreateBucketIfNotExists([]byte(bucket))
                    first = false
                } else {
                    bkt, err = bkt.CreateBucketIfNotExists([]byte(bucket))
                }
                if err != nil {
                    log.Println("error creating nested bucket")
                    return err
                }
            }
            return nil
        })
        if err != nil {
            log.Println("error creating nested bucket!!!")
            return err
        }
        return nil
    }
    

    To test you can cat the file through the strings command:

    cat test.bdb | strings
    bucketThree
    bucketTwo
    bucketOne
    

    If you're on Windows, I'm not sure what the equivalent command is, but you can open the file with Notepad and inspect it manually. It won't be pretty, but you should still see the name of your buckets in there.

    On another note, you error handling is going to result in very similar messages being printed in succession. Here's a slightly cleaner solution you can use:

    // CreateNestedBucketsNew - function to create
    // nested buckets from an array of Strings - my implementation
    func CreateNestedBucketsNew(buckets []string) (err error) {
        err = db.Update(func(tx *bolt.Tx) (err error) {
            var bkt *bolt.Bucket
    
            for index, bucket := range buckets {
                if index == 0 {
                    bkt, err = tx.CreateBucketIfNotExists([]byte(bucket))
                } else {
                    bkt, err = bkt.CreateBucketIfNotExists([]byte(bucket))
                }
    
                if err != nil {
                    return fmt.Errorf("Error creating nested bucket [%s]: %v", bucket, err)
                }
            }
            return err
        })
        return err
    }