2015-04-23 19:55:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-01-29 19:47:48 +00:00
|
|
|
"github.com/br0xen/bolt"
|
2015-04-23 19:55:16 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
args := os.Args[1:]
|
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Println("Usage: printbolt <Database File> [build]")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-05-12 21:02:39 +00:00
|
|
|
filename := args[0]
|
|
|
|
|
2016-01-29 19:47:48 +00:00
|
|
|
var db *bolt.DB
|
|
|
|
var err error
|
|
|
|
|
2015-05-12 21:02:39 +00:00
|
|
|
if len(args) > 1 && args[1] == "build" {
|
2016-01-29 19:47:48 +00:00
|
|
|
if _, stat_err := os.Stat(filename); stat_err == nil {
|
2015-05-12 21:02:39 +00:00
|
|
|
fmt.Printf("Cowardly refusing to overwrite file: %s\n", filename)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-04-23 19:55:16 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2016-01-29 19:47:48 +00:00
|
|
|
|
|
|
|
//opts := bolt.Options{0, true}
|
|
|
|
//db, err := bolt.Open(filename, 0600, &opts)
|
|
|
|
db, err = bolt.Open(filename, 0600, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error opening DB: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
2015-04-23 19:55:16 +00:00
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
if len(args) > 1 {
|
2015-05-12 21:02:39 +00:00
|
|
|
if args[1] == "build" {
|
|
|
|
createTestDatabase(db)
|
2015-04-23 19:55:16 +00:00
|
|
|
} else if args[1] == "json" {
|
|
|
|
jsonDatabase(db)
|
2016-01-29 19:47:48 +00:00
|
|
|
} else if args[1] == "go" {
|
|
|
|
fmt.Print(db.GoString())
|
2015-04-23 19:55:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printDatabase(db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printDatabase(db *bolt.DB) error {
|
|
|
|
db.View(func(tx *bolt.Tx) error {
|
|
|
|
tx.ForEach(func(nm []byte, b *bolt.Bucket) error {
|
|
|
|
fmt.Printf("%s => \n", string(nm))
|
|
|
|
printBucket(b, 0)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printBucket(b *bolt.Bucket, lvl int) {
|
|
|
|
b.ForEach(func(k, v []byte) error {
|
|
|
|
fmt.Printf("%s. %s => %s\n", strings.Repeat(".", lvl), k, v)
|
|
|
|
if v == nil {
|
|
|
|
printBucket(b.Bucket(k), lvl+1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonDatabase(db *bolt.DB) error {
|
|
|
|
out := "{"
|
|
|
|
db.View(func(tx *bolt.Tx) error {
|
|
|
|
tx.ForEach(func(nm []byte, b *bolt.Bucket) error {
|
|
|
|
bkt := jsonBucket(b)
|
|
|
|
out = fmt.Sprintf("%s\"%s\":%s,", out, string(nm), bkt[:len(bkt)-1])
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
out = fmt.Sprintf("%s}", out[:len(out)-1])
|
|
|
|
fmt.Print(out)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonBucket(b *bolt.Bucket) string {
|
|
|
|
ret := "{"
|
|
|
|
b.ForEach(func(k, v []byte) error {
|
|
|
|
ret = fmt.Sprintf("%s\"%s\":", ret, string(k))
|
|
|
|
if v == nil {
|
|
|
|
ret = fmt.Sprintf("%s%s", ret, jsonBucket(b.Bucket(k)))
|
|
|
|
} else {
|
|
|
|
ret = fmt.Sprintf("%s\"%s\",", ret, string(v))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
ret = fmt.Sprintf("%s},", ret[:len(ret)-1])
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestDatabase(db *bolt.DB) error {
|
|
|
|
fmt.Println("Generating Database")
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
fmt.Printf("Creating Bucket %d & Values", i)
|
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
|
|
|
b, err := tx.CreateBucketIfNotExists([]byte(fmt.Sprintf("Bucket%d", i)))
|
|
|
|
fmt.Print(".")
|
|
|
|
ba, err := b.CreateBucketIfNotExists([]byte(fmt.Sprintf("Bucket%da", i)))
|
|
|
|
bb, err := b.CreateBucketIfNotExists([]byte(fmt.Sprintf("Bucket%db", i)))
|
|
|
|
for j := 0; j < 10; j++ {
|
|
|
|
fmt.Print(".")
|
|
|
|
b.Put([]byte(fmt.Sprintf("key%d", j)), []byte(fmt.Sprintf("value%d", j)))
|
|
|
|
ba.Put([]byte(fmt.Sprintf("key%d", j)), []byte(fmt.Sprintf("value%d", j)))
|
|
|
|
bb.Put([]byte(fmt.Sprintf("key%d", j)), []byte(fmt.Sprintf("value%d", j)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
fmt.Print("\n")
|
|
|
|
return nil
|
|
|
|
}
|