Focus
Focus

Reputation: 1013

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
}

Upvotes: 1

Views: 2162

Answers (3)

lysS
lysS

Reputation: 85

func CreateNestedBuckets(fatherTable string, sonTables []string) error {
    db, dberr := bolt.Open("E:\\OneDrive\\code\\go\\project\\transmission\\static\\localstorage.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
    if dberr != nil {
        fmt.Println(dberr)
    }
    defer db.Close()

    err := db.Update(func(tx *bolt.Tx) error {
        var err error
        bkFather, err := tx.CreateBucketIfNotExists([]byte(fatherTable))

        for _, ta := range sonTables {
            fmt.Println(ta)

            _, err = bkFather.CreateBucketIfNotExists([]byte(ta))

            if err != nil {
                fmt.Println("error creating nested bucket")
                return err
            }
        }
        return nil
    })
    if err != nil {
        fmt.Println("error creating nested bucket!!!")
        return err
    }
    return nil
}

Upvotes: 0

lysS
lysS

Reputation: 85

fste89's demo has some debug; this right:

package main

import (
    "fmt"
    "time"

    "github.com/boltdb/bolt"
)

func CreateNestedBuckets(fatherTable string, sonTabls []string) error {
    db, dberr := bolt.Open("your file path", 0600, &bolt.Options{Timeout: 1 * time.Second})
    if dberr != nil {
        fmt.Println(dberr)
    }
    defer db.Close()

    err := db.Update(func(tx *bolt.Tx) error {
        var bkt *bolt.Bucket
        var err error
        bkFather, err = tx.CreateBucketIfNotExists([]byte(fatherTable))

        for _, ta := range sonTabls {
            fmt.Println(ta)

            _, err = bkFather.CreateBucketIfNotExists([]byte(ta))

            if err != nil {
                fmt.Println("error creating nested bucket")
                return err
            }
        }
        return nil
    })
    if err != nil {
        fmt.Println("error creating nested bucket!!!")
        return err
    }
    return nil
}

func main() {
    t := []string{"cc", "1", "2", "3"}
    fmt.Println(CreateNestedBuckets("sb", t))
}

echo:

cc
1
2
3
<nil>

Visible enter image description here

Upvotes: 0

jste89
jste89

Reputation: 446

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:

  • Are you checking the correct bolt database file? The botlt db file will be created in the directory you run your code from, unless you've specified an absolute path.
  • Does your input actually contain enough elements to create a nested structure?

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
}

Upvotes: 3

Related Questions