我可以在 boltdb 的嵌套桶下放置嵌套桶吗?
Can I have nested bucket under a nested bucket in boltdb?
这就是我创建嵌套存储桶所必须的。它不会 return 任何错误,但无法在另一个嵌套存储桶下创建嵌套存储桶。
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
}
简短回答:是的!您可以嵌套存储桶:https://twitter.com/boltdb/status/454730212010254336
长答案:您的代码工作正常!不过,这里有一些要检查的事项:
- 您是否正在检查正确的螺栓数据库文件? botlt db 文件将在您 运行 您的代码所在的目录中创建,除非您指定了绝对路径。
- 您的输入实际上是否包含足够的元素来创建嵌套结构?
我运行你的代码有以下设置(一些小的变化,但没什么大不了的)并且它工作正常:
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
}
要进行测试,您可以通过字符串命令对文件进行 cat:
cat test.bdb | strings
bucketThree
bucketTwo
bucketOne
如果您使用 Windows,我不确定等效命令是什么,但您可以使用记事本打开文件并手动检查。它不会很漂亮,但您应该仍会在其中看到您的存储桶的名称。
另一方面,您的错误处理将导致连续打印非常相似的消息。您可以使用以下更简洁的解决方案:
// 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
}
fste89的demo有一些debug;
这对:
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))
}
回声:
cc
1
2
3
<nil>
可见
enter image description here
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
}
这就是我创建嵌套存储桶所必须的。它不会 return 任何错误,但无法在另一个嵌套存储桶下创建嵌套存储桶。
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
}
简短回答:是的!您可以嵌套存储桶:https://twitter.com/boltdb/status/454730212010254336
长答案:您的代码工作正常!不过,这里有一些要检查的事项:
- 您是否正在检查正确的螺栓数据库文件? botlt db 文件将在您 运行 您的代码所在的目录中创建,除非您指定了绝对路径。
- 您的输入实际上是否包含足够的元素来创建嵌套结构?
我运行你的代码有以下设置(一些小的变化,但没什么大不了的)并且它工作正常:
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
}
要进行测试,您可以通过字符串命令对文件进行 cat:
cat test.bdb | strings
bucketThree
bucketTwo
bucketOne
如果您使用 Windows,我不确定等效命令是什么,但您可以使用记事本打开文件并手动检查。它不会很漂亮,但您应该仍会在其中看到您的存储桶的名称。
另一方面,您的错误处理将导致连续打印非常相似的消息。您可以使用以下更简洁的解决方案:
// 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
}
fste89的demo有一些debug; 这对:
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))
}
回声:
cc
1
2
3
<nil>
可见 enter image description here
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
}