duanjurong1347 2016-12-15 02:04
浏览 153
已采纳

我可以在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
}
  • 写回答

1条回答 默认 最新

  • duanqujing3863 2016-12-15 09:28
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)