From fea388b03d67a26a6bb13e9b9363b15e72b7f2c5 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Thu, 27 Mar 2014 22:36:05 -0600 Subject: [PATCH] Refactor bolt commands into individual files. --- cmd/bolt/buckets.go | 33 ++++++ cmd/bolt/buckets_test.go | 31 ++++++ cmd/bolt/get.go | 45 ++++++++ cmd/bolt/get_test.go | 51 +++++++++ cmd/bolt/keys.go | 41 ++++++++ cmd/bolt/keys_test.go | 41 ++++++++ cmd/bolt/main.go | 220 ++++++--------------------------------- cmd/bolt/main_test.go | 127 ---------------------- cmd/bolt/pages.go | 46 ++++++++ cmd/bolt/set.go | 38 +++++++ cmd/bolt/set_test.go | 38 +++++++ 11 files changed, 395 insertions(+), 316 deletions(-) create mode 100644 cmd/bolt/buckets.go create mode 100644 cmd/bolt/buckets_test.go create mode 100644 cmd/bolt/get.go create mode 100644 cmd/bolt/get_test.go create mode 100644 cmd/bolt/keys.go create mode 100644 cmd/bolt/keys_test.go create mode 100644 cmd/bolt/pages.go create mode 100644 cmd/bolt/set.go create mode 100644 cmd/bolt/set_test.go diff --git a/cmd/bolt/buckets.go b/cmd/bolt/buckets.go new file mode 100644 index 00000000..10766a65 --- /dev/null +++ b/cmd/bolt/buckets.go @@ -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 + } +} diff --git a/cmd/bolt/buckets_test.go b/cmd/bolt/buckets_test.go new file mode 100644 index 00000000..771c8d8c --- /dev/null +++ b/cmd/bolt/buckets_test.go @@ -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) +} diff --git a/cmd/bolt/get.go b/cmd/bolt/get.go new file mode 100644 index 00000000..10216e37 --- /dev/null +++ b/cmd/bolt/get.go @@ -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 + } +} diff --git a/cmd/bolt/get_test.go b/cmd/bolt/get_test.go new file mode 100644 index 00000000..44980864 --- /dev/null +++ b/cmd/bolt/get_test.go @@ -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) + }) +} diff --git a/cmd/bolt/keys.go b/cmd/bolt/keys.go new file mode 100644 index 00000000..56245b89 --- /dev/null +++ b/cmd/bolt/keys.go @@ -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 + } +} diff --git a/cmd/bolt/keys_test.go b/cmd/bolt/keys_test.go new file mode 100644 index 00000000..c426836f --- /dev/null +++ b/cmd/bolt/keys_test.go @@ -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) + }) +} diff --git a/cmd/bolt/main.go b/cmd/bolt/main.go index 9a51ec5e..6b280600 100644 --- a/cmd/bolt/main.go +++ b/cmd/bolt/main.go @@ -5,9 +5,7 @@ import ( "fmt" "log" "os" - "strconv" - "github.com/boltdb/bolt" "github.com/codegangsta/cli" ) @@ -21,208 +19,52 @@ func NewApp() *cli.App { app := cli.NewApp() app.Name = "bolt" app.Usage = "BoltDB toolkit" + app.Version = "0.1.0" app.Commands = []cli.Command{ { - Name: "get", - Usage: "Retrieve a value for given key in a bucket", - Action: GetCommand, + Name: "get", + Usage: "Retrieve a value for given key in a bucket", + 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", - Usage: "Sets a value for given key in a bucket", - Action: SetCommand, + Name: "set", + Usage: "Sets a value for given key in a bucket", + 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", - Usage: "Retrieve a list of all keys in a bucket", - Action: KeysCommand, + Name: "keys", + Usage: "Retrieve a list of all keys in a bucket", + Action: func(c *cli.Context) { + path, name := c.Args().Get(0), c.Args().Get(1) + Keys(path, name) + }, }, { - Name: "buckets", - Usage: "Retrieves a list of all buckets", - Action: BucketsCommand, + Name: "buckets", + Usage: "Retrieves a list of all buckets", + Action: func(c *cli.Context) { + path := c.Args().Get(0) + Buckets(path) + }, }, { - Name: "pages", - Usage: "Dumps page information for a database", - Action: PagesCommand, + Name: "pages", + Usage: "Dumps page information for a database", + Action: func(c *cli.Context) { + path := c.Args().Get(0) + Pages(path) + }, }, } 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 logBuffer *bytes.Buffer diff --git a/cmd/bolt/main_test.go b/cmd/bolt/main_test.go index b203d2c2..51198c86 100644 --- a/cmd/bolt/main_test.go +++ b/cmd/bolt/main_test.go @@ -4,138 +4,11 @@ import ( "io/ioutil" "os" "strings" - "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) - }) -} - -// 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. func open(fn func(*bolt.DB)) { f, _ := ioutil.TempFile("", "bolt-") diff --git a/cmd/bolt/pages.go b/cmd/bolt/pages.go new file mode 100644 index 00000000..2b55c696 --- /dev/null +++ b/cmd/bolt/pages.go @@ -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 + }) +} diff --git a/cmd/bolt/set.go b/cmd/bolt/set.go new file mode 100644 index 00000000..ff12024e --- /dev/null +++ b/cmd/bolt/set.go @@ -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 + } +} diff --git a/cmd/bolt/set_test.go b/cmd/bolt/set_test.go new file mode 100644 index 00000000..d76b3c02 --- /dev/null +++ b/cmd/bolt/set_test.go @@ -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) + }) +}