mirror of https://github.com/etcd-io/bbolt.git
Merge pull request #97 from benbjohnson/cli
Refactor bolt commands into individual files.pull/34/head
commit
7dafeaa896
|
@ -0,0 +1,33 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Buckets prints a list of all buckets.
|
||||||
|
func Buckets(path string) {
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := bolt.Open(path, 0600)
|
||||||
|
if err != nil {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
err = db.View(func(tx *bolt.Tx) error {
|
||||||
|
for _, b := range tx.Buckets() {
|
||||||
|
println(b.Name())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package main_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
|
. "github.com/boltdb/bolt/cmd/bolt"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Ensure that a list of buckets can be retrieved.
|
||||||
|
func TestBuckets(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
open(func(db *bolt.DB) {
|
||||||
|
db.Update(func(tx *bolt.Tx) error {
|
||||||
|
tx.CreateBucket("woojits")
|
||||||
|
tx.CreateBucket("widgets")
|
||||||
|
tx.CreateBucket("whatchits")
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
output := run("buckets", db.Path())
|
||||||
|
assert.Equal(t, "whatchits\nwidgets\nwoojits", output)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that an error is reported if the database is not found.
|
||||||
|
func TestBucketsDBNotFound(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
output := run("buckets", "no/such/db")
|
||||||
|
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get retrieves the value for a given bucket/key.
|
||||||
|
func Get(path, name, key string) {
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := bolt.Open(path, 0600)
|
||||||
|
if err != nil {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
err = db.View(func(tx *bolt.Tx) error {
|
||||||
|
// Find bucket.
|
||||||
|
b := tx.Bucket(name)
|
||||||
|
if b == nil {
|
||||||
|
fatalf("bucket not found: %s", name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find value for a given key.
|
||||||
|
value := b.Get([]byte(key))
|
||||||
|
if value == nil {
|
||||||
|
fatalf("key not found: %s", key)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
println(string(value))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package main_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
|
. "github.com/boltdb/bolt/cmd/bolt"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Ensure that a value can be retrieved from the CLI.
|
||||||
|
func TestGet(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
open(func(db *bolt.DB) {
|
||||||
|
db.Update(func(tx *bolt.Tx) error {
|
||||||
|
tx.CreateBucket("widgets")
|
||||||
|
tx.Bucket("widgets").Put([]byte("foo"), []byte("bar"))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
output := run("get", db.Path(), "widgets", "foo")
|
||||||
|
assert.Equal(t, "bar", output)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that an error is reported if the database is not found.
|
||||||
|
func TestGetDBNotFound(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
output := run("get", "no/such/db", "widgets", "foo")
|
||||||
|
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that an error is reported if the bucket is not found.
|
||||||
|
func TestGetBucketNotFound(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
open(func(db *bolt.DB) {
|
||||||
|
output := run("get", db.Path(), "widgets", "foo")
|
||||||
|
assert.Equal(t, "bucket not found: widgets", output)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that an error is reported if the key is not found.
|
||||||
|
func TestGetKeyNotFound(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
open(func(db *bolt.DB) {
|
||||||
|
db.Update(func(tx *bolt.Tx) error {
|
||||||
|
return tx.CreateBucket("widgets")
|
||||||
|
})
|
||||||
|
output := run("get", db.Path(), "widgets", "foo")
|
||||||
|
assert.Equal(t, "key not found: foo", output)
|
||||||
|
})
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Keys retrieves a list of keys for a given bucket.
|
||||||
|
func Keys(path, name string) {
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := bolt.Open(path, 0600)
|
||||||
|
if err != nil {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
err = db.View(func(tx *bolt.Tx) error {
|
||||||
|
// Find bucket.
|
||||||
|
b := tx.Bucket(name)
|
||||||
|
if b == nil {
|
||||||
|
fatalf("bucket not found: %s", name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate over each key.
|
||||||
|
return b.ForEach(func(key, _ []byte) error {
|
||||||
|
println(string(key))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package main_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
|
. "github.com/boltdb/bolt/cmd/bolt"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Ensure that a list of keys can be retrieved for a given bucket.
|
||||||
|
func TestKeys(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
open(func(db *bolt.DB) {
|
||||||
|
db.Update(func(tx *bolt.Tx) error {
|
||||||
|
tx.CreateBucket("widgets")
|
||||||
|
tx.Bucket("widgets").Put([]byte("0002"), []byte(""))
|
||||||
|
tx.Bucket("widgets").Put([]byte("0001"), []byte(""))
|
||||||
|
tx.Bucket("widgets").Put([]byte("0003"), []byte(""))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
output := run("keys", db.Path(), "widgets")
|
||||||
|
assert.Equal(t, "0001\n0002\n0003", output)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that an error is reported if the database is not found.
|
||||||
|
func TestKeysDBNotFound(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
output := run("keys", "no/such/db", "widgets")
|
||||||
|
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that an error is reported if the bucket is not found.
|
||||||
|
func TestKeysBucketNotFound(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
open(func(db *bolt.DB) {
|
||||||
|
output := run("keys", db.Path(), "widgets")
|
||||||
|
assert.Equal(t, "bucket not found: widgets", output)
|
||||||
|
})
|
||||||
|
}
|
200
cmd/bolt/main.go
200
cmd/bolt/main.go
|
@ -5,9 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -21,208 +19,52 @@ func NewApp() *cli.App {
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Name = "bolt"
|
app.Name = "bolt"
|
||||||
app.Usage = "BoltDB toolkit"
|
app.Usage = "BoltDB toolkit"
|
||||||
|
app.Version = "0.1.0"
|
||||||
app.Commands = []cli.Command{
|
app.Commands = []cli.Command{
|
||||||
{
|
{
|
||||||
Name: "get",
|
Name: "get",
|
||||||
Usage: "Retrieve a value for given key in a bucket",
|
Usage: "Retrieve a value for given key in a bucket",
|
||||||
Action: GetCommand,
|
Action: func(c *cli.Context) {
|
||||||
|
path, name, key := c.Args().Get(0), c.Args().Get(1), c.Args().Get(2)
|
||||||
|
Get(path, name, key)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "set",
|
Name: "set",
|
||||||
Usage: "Sets a value for given key in a bucket",
|
Usage: "Sets a value for given key in a bucket",
|
||||||
Action: SetCommand,
|
Action: func(c *cli.Context) {
|
||||||
|
path, name, key, value := c.Args().Get(0), c.Args().Get(1), c.Args().Get(2), c.Args().Get(3)
|
||||||
|
Set(path, name, key, value)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "keys",
|
Name: "keys",
|
||||||
Usage: "Retrieve a list of all keys in a bucket",
|
Usage: "Retrieve a list of all keys in a bucket",
|
||||||
Action: KeysCommand,
|
Action: func(c *cli.Context) {
|
||||||
|
path, name := c.Args().Get(0), c.Args().Get(1)
|
||||||
|
Keys(path, name)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "buckets",
|
Name: "buckets",
|
||||||
Usage: "Retrieves a list of all buckets",
|
Usage: "Retrieves a list of all buckets",
|
||||||
Action: BucketsCommand,
|
Action: func(c *cli.Context) {
|
||||||
|
path := c.Args().Get(0)
|
||||||
|
Buckets(path)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "pages",
|
Name: "pages",
|
||||||
Usage: "Dumps page information for a database",
|
Usage: "Dumps page information for a database",
|
||||||
Action: PagesCommand,
|
Action: func(c *cli.Context) {
|
||||||
|
path := c.Args().Get(0)
|
||||||
|
Pages(path)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCommand retrieves the value for a given bucket/key.
|
|
||||||
func GetCommand(c *cli.Context) {
|
|
||||||
path, name, key := c.Args().Get(0), c.Args().Get(1), c.Args().Get(2)
|
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := bolt.Open(path, 0600)
|
|
||||||
if err != nil {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
err = db.View(func(tx *bolt.Tx) error {
|
|
||||||
// Find bucket.
|
|
||||||
b := tx.Bucket(name)
|
|
||||||
if b == nil {
|
|
||||||
fatalf("bucket not found: %s", name)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find value for a given key.
|
|
||||||
value := b.Get([]byte(key))
|
|
||||||
if value == nil {
|
|
||||||
fatalf("key not found: %s", key)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
println(string(value))
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetCommand sets the value for a given key in a bucket.
|
|
||||||
func SetCommand(c *cli.Context) {
|
|
||||||
path, name, key, value := c.Args().Get(0), c.Args().Get(1), c.Args().Get(2), c.Args().Get(3)
|
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := bolt.Open(path, 0600)
|
|
||||||
if err != nil {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
err = db.Update(func(tx *bolt.Tx) error {
|
|
||||||
// Find bucket.
|
|
||||||
b := tx.Bucket(name)
|
|
||||||
if b == nil {
|
|
||||||
fatalf("bucket not found: %s", name)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set value for a given key.
|
|
||||||
return b.Put([]byte(key), []byte(value))
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// KeysCommand retrieves a list of keys for a given bucket.
|
|
||||||
func KeysCommand(c *cli.Context) {
|
|
||||||
path, name := c.Args().Get(0), c.Args().Get(1)
|
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := bolt.Open(path, 0600)
|
|
||||||
if err != nil {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
err = db.View(func(tx *bolt.Tx) error {
|
|
||||||
// Find bucket.
|
|
||||||
b := tx.Bucket(name)
|
|
||||||
if b == nil {
|
|
||||||
fatalf("bucket not found: %s", name)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iterate over each key.
|
|
||||||
return b.ForEach(func(key, _ []byte) error {
|
|
||||||
println(string(key))
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BucketsCommand retrieves a list of all buckets.
|
|
||||||
func BucketsCommand(c *cli.Context) {
|
|
||||||
path := c.Args().Get(0)
|
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := bolt.Open(path, 0600)
|
|
||||||
if err != nil {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
err = db.View(func(tx *bolt.Tx) error {
|
|
||||||
for _, b := range tx.Buckets() {
|
|
||||||
println(b.Name())
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// PagesCommand prints a list of all pages in a database.
|
|
||||||
func PagesCommand(c *cli.Context) {
|
|
||||||
path := c.Args().Get(0)
|
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := bolt.Open(path, 0600)
|
|
||||||
if err != nil {
|
|
||||||
fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
println("ID TYPE ITEMS OVRFLW")
|
|
||||||
println("======== ========== ====== ======")
|
|
||||||
|
|
||||||
db.Update(func(tx *bolt.Tx) error {
|
|
||||||
var id int
|
|
||||||
for {
|
|
||||||
p, err := tx.Page(id)
|
|
||||||
if err != nil {
|
|
||||||
fatalf("page error: %d: %s", id, err)
|
|
||||||
} else if p == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
var overflow string
|
|
||||||
if p.OverflowCount > 0 {
|
|
||||||
overflow = strconv.Itoa(p.OverflowCount)
|
|
||||||
}
|
|
||||||
printf("%-8d %-10s %-6d %-6s\n", p.ID, p.Type, p.Count, overflow)
|
|
||||||
id += 1 + p.OverflowCount
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var logger = log.New(os.Stderr, "", 0)
|
var logger = log.New(os.Stderr, "", 0)
|
||||||
var logBuffer *bytes.Buffer
|
var logBuffer *bytes.Buffer
|
||||||
|
|
||||||
|
|
|
@ -4,138 +4,11 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
. "github.com/boltdb/bolt/cmd/bolt"
|
. "github.com/boltdb/bolt/cmd/bolt"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Ensure that a value can be retrieved from the CLI.
|
|
||||||
func TestGet(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
open(func(db *bolt.DB) {
|
|
||||||
db.Update(func(tx *bolt.Tx) error {
|
|
||||||
tx.CreateBucket("widgets")
|
|
||||||
tx.Bucket("widgets").Put([]byte("foo"), []byte("bar"))
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
output := run("get", db.Path(), "widgets", "foo")
|
|
||||||
assert.Equal(t, "bar", output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that an error is reported if the database is not found.
|
|
||||||
func TestGetDBNotFound(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
output := run("get", "no/such/db", "widgets", "foo")
|
|
||||||
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that an error is reported if the bucket is not found.
|
|
||||||
func TestGetBucketNotFound(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
open(func(db *bolt.DB) {
|
|
||||||
output := run("get", db.Path(), "widgets", "foo")
|
|
||||||
assert.Equal(t, "bucket not found: widgets", output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that an error is reported if the key is not found.
|
|
||||||
func TestGetKeyNotFound(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
open(func(db *bolt.DB) {
|
|
||||||
db.Update(func(tx *bolt.Tx) error {
|
|
||||||
return tx.CreateBucket("widgets")
|
|
||||||
})
|
|
||||||
output := run("get", db.Path(), "widgets", "foo")
|
|
||||||
assert.Equal(t, "key not found: foo", output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that a value can be set from the CLI.
|
|
||||||
func TestSet(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
open(func(db *bolt.DB) {
|
|
||||||
db.Update(func(tx *bolt.Tx) error {
|
|
||||||
tx.CreateBucket("widgets")
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
assert.Equal(t, "", run("set", db.Path(), "widgets", "foo", "bar"))
|
|
||||||
assert.Equal(t, "bar", run("get", db.Path(), "widgets", "foo"))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that an error is reported if the database is not found.
|
|
||||||
func TestSetDBNotFound(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
output := run("set", "no/such/db", "widgets", "foo", "bar")
|
|
||||||
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that an error is reported if the bucket is not found.
|
|
||||||
func TestSetBucketNotFound(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
open(func(db *bolt.DB) {
|
|
||||||
output := run("set", db.Path(), "widgets", "foo", "bar")
|
|
||||||
assert.Equal(t, "bucket not found: widgets", output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that a list of keys can be retrieved for a given bucket.
|
|
||||||
func TestKeys(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
open(func(db *bolt.DB) {
|
|
||||||
db.Update(func(tx *bolt.Tx) error {
|
|
||||||
tx.CreateBucket("widgets")
|
|
||||||
tx.Bucket("widgets").Put([]byte("0002"), []byte(""))
|
|
||||||
tx.Bucket("widgets").Put([]byte("0001"), []byte(""))
|
|
||||||
tx.Bucket("widgets").Put([]byte("0003"), []byte(""))
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
output := run("keys", db.Path(), "widgets")
|
|
||||||
assert.Equal(t, "0001\n0002\n0003", output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that an error is reported if the database is not found.
|
|
||||||
func TestKeysDBNotFound(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
output := run("keys", "no/such/db", "widgets")
|
|
||||||
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that an error is reported if the bucket is not found.
|
|
||||||
func TestKeysBucketNotFound(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
open(func(db *bolt.DB) {
|
|
||||||
output := run("keys", db.Path(), "widgets")
|
|
||||||
assert.Equal(t, "bucket not found: widgets", output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that a list of buckets can be retrieved.
|
|
||||||
func TestBuckets(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
open(func(db *bolt.DB) {
|
|
||||||
db.Update(func(tx *bolt.Tx) error {
|
|
||||||
tx.CreateBucket("woojits")
|
|
||||||
tx.CreateBucket("widgets")
|
|
||||||
tx.CreateBucket("whatchits")
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
output := run("buckets", db.Path())
|
|
||||||
assert.Equal(t, "whatchits\nwidgets\nwoojits", output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that an error is reported if the database is not found.
|
|
||||||
func TestBucketsDBNotFound(t *testing.T) {
|
|
||||||
SetTestMode(true)
|
|
||||||
output := run("buckets", "no/such/db")
|
|
||||||
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
|
||||||
}
|
|
||||||
|
|
||||||
// open creates and opens a Bolt database in the temp directory.
|
// open creates and opens a Bolt database in the temp directory.
|
||||||
func open(fn func(*bolt.DB)) {
|
func open(fn func(*bolt.DB)) {
|
||||||
f, _ := ioutil.TempFile("", "bolt-")
|
f, _ := ioutil.TempFile("", "bolt-")
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Pages prints a list of all pages in a database.
|
||||||
|
func Pages(path string) {
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := bolt.Open(path, 0600)
|
||||||
|
if err != nil {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
println("ID TYPE ITEMS OVRFLW")
|
||||||
|
println("======== ========== ====== ======")
|
||||||
|
|
||||||
|
db.Update(func(tx *bolt.Tx) error {
|
||||||
|
var id int
|
||||||
|
for {
|
||||||
|
p, err := tx.Page(id)
|
||||||
|
if err != nil {
|
||||||
|
fatalf("page error: %d: %s", id, err)
|
||||||
|
} else if p == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
var overflow string
|
||||||
|
if p.OverflowCount > 0 {
|
||||||
|
overflow = strconv.Itoa(p.OverflowCount)
|
||||||
|
}
|
||||||
|
printf("%-8d %-10s %-6d %-6s\n", p.ID, p.Type, p.Count, overflow)
|
||||||
|
id += 1 + p.OverflowCount
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Set sets the value for a given key in a bucket.
|
||||||
|
func Set(path, name, key, value string) {
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := bolt.Open(path, 0600)
|
||||||
|
if err != nil {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
err = db.Update(func(tx *bolt.Tx) error {
|
||||||
|
// Find bucket.
|
||||||
|
b := tx.Bucket(name)
|
||||||
|
if b == nil {
|
||||||
|
fatalf("bucket not found: %s", name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set value for a given key.
|
||||||
|
return b.Put([]byte(key), []byte(value))
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package main_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
|
. "github.com/boltdb/bolt/cmd/bolt"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Ensure that a value can be set from the CLI.
|
||||||
|
func TestSet(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
open(func(db *bolt.DB) {
|
||||||
|
db.Update(func(tx *bolt.Tx) error {
|
||||||
|
tx.CreateBucket("widgets")
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
assert.Equal(t, "", run("set", db.Path(), "widgets", "foo", "bar"))
|
||||||
|
assert.Equal(t, "bar", run("get", db.Path(), "widgets", "foo"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that an error is reported if the database is not found.
|
||||||
|
func TestSetDBNotFound(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
output := run("set", "no/such/db", "widgets", "foo", "bar")
|
||||||
|
assert.Equal(t, "stat no/such/db: no such file or directory", output)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that an error is reported if the bucket is not found.
|
||||||
|
func TestSetBucketNotFound(t *testing.T) {
|
||||||
|
SetTestMode(true)
|
||||||
|
open(func(db *bolt.DB) {
|
||||||
|
output := run("set", db.Path(), "widgets", "foo", "bar")
|
||||||
|
assert.Equal(t, "bucket not found: widgets", output)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue