dispatch/vendor/github.com/boltdb/bolt/tx_test.go

717 lines
16 KiB
Go
Raw Normal View History

2015-03-04 16:38:10 +00:00
package bolt_test
import (
2016-01-15 18:48:03 +00:00
"bytes"
2015-03-04 16:38:10 +00:00
"errors"
"fmt"
2016-01-15 18:48:03 +00:00
"log"
2015-03-04 16:38:10 +00:00
"os"
"testing"
2016-03-01 00:51:26 +00:00
"github.com/boltdb/bolt"
2015-03-04 16:38:10 +00:00
)
// Ensure that committing a closed transaction returns an error.
2016-01-15 18:48:03 +00:00
func TestTx_Commit_ErrTxClosed(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
tx, err := db.Begin(true)
if err != nil {
t.Fatal(err)
}
if _, err := tx.CreateBucket([]byte("foo")); err != nil {
t.Fatal(err)
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
if err := tx.Commit(); err != bolt.ErrTxClosed {
t.Fatalf("unexpected error: %s", err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that rolling back a closed transaction returns an error.
2016-01-15 18:48:03 +00:00
func TestTx_Rollback_ErrTxClosed(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
tx, err := db.Begin(true)
if err != nil {
t.Fatal(err)
}
if err := tx.Rollback(); err != nil {
t.Fatal(err)
}
if err := tx.Rollback(); err != bolt.ErrTxClosed {
t.Fatalf("unexpected error: %s", err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that committing a read-only transaction returns an error.
2016-01-15 18:48:03 +00:00
func TestTx_Commit_ErrTxNotWritable(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
tx, err := db.Begin(false)
if err != nil {
t.Fatal(err)
}
if err := tx.Commit(); err != bolt.ErrTxNotWritable {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that a transaction can retrieve a cursor on the root bucket.
func TestTx_Cursor(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
2016-01-15 18:48:03 +00:00
if _, err := tx.CreateBucket([]byte("woojits")); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
2016-01-15 18:48:03 +00:00
c := tx.Cursor()
if k, v := c.First(); !bytes.Equal(k, []byte("widgets")) {
t.Fatalf("unexpected key: %v", k)
} else if v != nil {
t.Fatalf("unexpected value: %v", v)
}
if k, v := c.Next(); !bytes.Equal(k, []byte("woojits")) {
t.Fatalf("unexpected key: %v", k)
} else if v != nil {
t.Fatalf("unexpected value: %v", v)
}
if k, v := c.Next(); k != nil {
t.Fatalf("unexpected key: %v", k)
} else if v != nil {
t.Fatalf("unexpected value: %v", k)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that creating a bucket with a read-only transaction returns an error.
2016-01-15 18:48:03 +00:00
func TestTx_CreateBucket_ErrTxNotWritable(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.View(func(tx *bolt.Tx) error {
_, err := tx.CreateBucket([]byte("foo"))
if err != bolt.ErrTxNotWritable {
t.Fatalf("unexpected error: %s", err)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that creating a bucket on a closed transaction returns an error.
2016-01-15 18:48:03 +00:00
func TestTx_CreateBucket_ErrTxClosed(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
tx, err := db.Begin(true)
if err != nil {
t.Fatal(err)
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
if _, err := tx.CreateBucket([]byte("foo")); err != bolt.ErrTxClosed {
t.Fatalf("unexpected error: %s", err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that a Tx can retrieve a bucket.
func TestTx_Bucket(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
t.Fatal(err)
}
if tx.Bucket([]byte("widgets")) == nil {
t.Fatal("expected bucket")
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that a Tx retrieving a non-existent key returns nil.
2016-01-15 18:48:03 +00:00
func TestTx_Get_NotFound(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
t.Fatal(err)
}
if b.Get([]byte("no_such_key")) != nil {
t.Fatal("expected nil value")
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that a bucket can be created and retrieved.
func TestTx_CreateBucket(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
2015-03-04 16:38:10 +00:00
// Create a bucket.
2016-01-15 18:48:03 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
2015-03-04 16:38:10 +00:00
b, err := tx.CreateBucket([]byte("widgets"))
2016-01-15 18:48:03 +00:00
if err != nil {
t.Fatal(err)
} else if b == nil {
t.Fatal("expected bucket")
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
// Read the bucket through a separate transaction.
2016-01-15 18:48:03 +00:00
if err := db.View(func(tx *bolt.Tx) error {
if tx.Bucket([]byte("widgets")) == nil {
t.Fatal("expected bucket")
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that a bucket can be created if it doesn't already exist.
func TestTx_CreateBucketIfNotExists(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
// Create bucket.
if b, err := tx.CreateBucketIfNotExists([]byte("widgets")); err != nil {
t.Fatal(err)
} else if b == nil {
t.Fatal("expected bucket")
}
// Create bucket again.
if b, err := tx.CreateBucketIfNotExists([]byte("widgets")); err != nil {
t.Fatal(err)
} else if b == nil {
t.Fatal("expected bucket")
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
// Read the bucket through a separate transaction.
2016-01-15 18:48:03 +00:00
if err := db.View(func(tx *bolt.Tx) error {
if tx.Bucket([]byte("widgets")) == nil {
t.Fatal("expected bucket")
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
}
// Ensure transaction returns an error if creating an unnamed bucket.
func TestTx_CreateBucketIfNotExists_ErrBucketNameRequired(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucketIfNotExists([]byte{}); err != bolt.ErrBucketNameRequired {
t.Fatalf("unexpected error: %s", err)
}
if _, err := tx.CreateBucketIfNotExists(nil); err != bolt.ErrBucketNameRequired {
t.Fatalf("unexpected error: %s", err)
}
return nil
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that a bucket cannot be created twice.
2016-01-15 18:48:03 +00:00
func TestTx_CreateBucket_ErrBucketExists(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
2015-03-04 16:38:10 +00:00
// Create a bucket.
2016-01-15 18:48:03 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
// Create the same bucket again.
2016-01-15 18:48:03 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucket([]byte("widgets")); err != bolt.ErrBucketExists {
t.Fatalf("unexpected error: %s", err)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that a bucket is created with a non-blank name.
2016-01-15 18:48:03 +00:00
func TestTx_CreateBucket_ErrBucketNameRequired(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucket(nil); err != bolt.ErrBucketNameRequired {
t.Fatalf("unexpected error: %s", err)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that a bucket can be deleted.
func TestTx_DeleteBucket(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
2015-03-04 16:38:10 +00:00
// Create a bucket and add a value.
2016-01-15 18:48:03 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
// Delete the bucket and make sure we can't get the value.
2016-01-15 18:48:03 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
if err := tx.DeleteBucket([]byte("widgets")); err != nil {
t.Fatal(err)
}
if tx.Bucket([]byte("widgets")) != nil {
t.Fatal("unexpected bucket")
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
2016-01-15 18:48:03 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
2015-03-04 16:38:10 +00:00
// Create the bucket again and make sure there's not a phantom value.
b, err := tx.CreateBucket([]byte("widgets"))
2016-01-15 18:48:03 +00:00
if err != nil {
t.Fatal(err)
}
if v := b.Get([]byte("foo")); v != nil {
t.Fatalf("unexpected phantom value: %v", v)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that deleting a bucket on a closed transaction returns an error.
2016-01-15 18:48:03 +00:00
func TestTx_DeleteBucket_ErrTxClosed(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
tx, err := db.Begin(true)
if err != nil {
t.Fatal(err)
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
if err := tx.DeleteBucket([]byte("foo")); err != bolt.ErrTxClosed {
t.Fatalf("unexpected error: %s", err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that deleting a bucket with a read-only transaction returns an error.
func TestTx_DeleteBucket_ReadOnly(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
if err := db.View(func(tx *bolt.Tx) error {
if err := tx.DeleteBucket([]byte("foo")); err != bolt.ErrTxNotWritable {
t.Fatalf("unexpected error: %s", err)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that nothing happens when deleting a bucket that doesn't exist.
func TestTx_DeleteBucket_NotFound(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
if err := tx.DeleteBucket([]byte("widgets")); err != bolt.ErrBucketNotFound {
t.Fatalf("unexpected error: %s", err)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
}
// Ensure that no error is returned when a tx.ForEach function does not return
// an error.
func TestTx_ForEach_NoError(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
t.Fatal(err)
}
if err := tx.ForEach(func(name []byte, b *bolt.Bucket) error {
return nil
}); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that an error is returned when a tx.ForEach function returns an error.
func TestTx_ForEach_WithError(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
t.Fatal(err)
}
marker := errors.New("marker")
if err := tx.ForEach(func(name []byte, b *bolt.Bucket) error {
return marker
}); err != marker {
t.Fatalf("unexpected error: %s", err)
}
return nil
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that Tx commit handlers are called after a transaction successfully commits.
func TestTx_OnCommit(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
2015-03-04 16:38:10 +00:00
var x int
2016-01-15 18:48:03 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
2015-03-04 16:38:10 +00:00
tx.OnCommit(func() { x += 1 })
tx.OnCommit(func() { x += 2 })
2016-01-15 18:48:03 +00:00
if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
} else if x != 3 {
t.Fatalf("unexpected x: %d", x)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that Tx commit handlers are NOT called after a transaction rolls back.
func TestTx_OnCommit_Rollback(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
2015-03-04 16:38:10 +00:00
var x int
2016-01-15 18:48:03 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
2015-03-04 16:38:10 +00:00
tx.OnCommit(func() { x += 1 })
tx.OnCommit(func() { x += 2 })
2016-01-15 18:48:03 +00:00
if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
return errors.New("rollback this commit")
2016-01-15 18:48:03 +00:00
}); err == nil || err.Error() != "rollback this commit" {
t.Fatalf("unexpected error: %s", err)
} else if x != 0 {
t.Fatalf("unexpected x: %d", x)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that the database can be copied to a file path.
func TestTx_CopyFile(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
path := tempfile()
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("baz"), []byte("bat")); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
2016-01-15 18:48:03 +00:00
if err := db.View(func(tx *bolt.Tx) error {
return tx.CopyFile(path, 0600)
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
2016-01-15 18:48:03 +00:00
db2, err := bolt.Open(path, 0600, nil)
if err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
2016-01-15 18:48:03 +00:00
if err := db2.View(func(tx *bolt.Tx) error {
if v := tx.Bucket([]byte("widgets")).Get([]byte("foo")); !bytes.Equal(v, []byte("bar")) {
t.Fatalf("unexpected value: %v", v)
}
if v := tx.Bucket([]byte("widgets")).Get([]byte("baz")); !bytes.Equal(v, []byte("bat")) {
t.Fatalf("unexpected value: %v", v)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
if err := db2.Close(); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
}
type failWriterError struct{}
func (failWriterError) Error() string {
return "error injected for tests"
}
type failWriter struct {
// fail after this many bytes
After int
}
func (f *failWriter) Write(p []byte) (n int, err error) {
n = len(p)
if n > f.After {
n = f.After
err = failWriterError{}
}
f.After -= n
return n, err
}
// Ensure that Copy handles write errors right.
func TestTx_CopyFile_Error_Meta(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("baz"), []byte("bat")); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
2016-01-15 18:48:03 +00:00
if err := db.View(func(tx *bolt.Tx) error {
return tx.Copy(&failWriter{})
}); err == nil || err.Error() != "meta copy: error injected for tests" {
t.Fatalf("unexpected error: %v", err)
}
2015-03-04 16:38:10 +00:00
}
// Ensure that Copy handles write errors right.
func TestTx_CopyFile_Error_Normal(t *testing.T) {
2016-01-15 18:48:03 +00:00
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("baz"), []byte("bat")); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
t.Fatal(err)
}
2015-03-04 16:38:10 +00:00
2016-01-15 18:48:03 +00:00
if err := db.View(func(tx *bolt.Tx) error {
return tx.Copy(&failWriter{3 * db.Info().PageSize})
}); err == nil || err.Error() != "error injected for tests" {
t.Fatalf("unexpected error: %v", err)
}
2015-03-04 16:38:10 +00:00
}
func ExampleTx_Rollback() {
// Open the database.
2016-01-15 18:48:03 +00:00
db, err := bolt.Open(tempfile(), 0666, nil)
if err != nil {
log.Fatal(err)
}
2015-03-04 16:38:10 +00:00
defer os.Remove(db.Path())
// Create a bucket.
2016-01-15 18:48:03 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
2015-03-04 16:38:10 +00:00
_, err := tx.CreateBucket([]byte("widgets"))
return err
2016-01-15 18:48:03 +00:00
}); err != nil {
log.Fatal(err)
}
2015-03-04 16:38:10 +00:00
// Set a value for a key.
2016-01-15 18:48:03 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
2015-03-04 16:38:10 +00:00
return tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
2016-01-15 18:48:03 +00:00
}); err != nil {
log.Fatal(err)
}
2015-03-04 16:38:10 +00:00
// Update the key but rollback the transaction so it never saves.
2016-01-15 18:48:03 +00:00
tx, err := db.Begin(true)
if err != nil {
log.Fatal(err)
}
2015-03-04 16:38:10 +00:00
b := tx.Bucket([]byte("widgets"))
2016-01-15 18:48:03 +00:00
if err := b.Put([]byte("foo"), []byte("baz")); err != nil {
log.Fatal(err)
}
if err := tx.Rollback(); err != nil {
log.Fatal(err)
}
2015-03-04 16:38:10 +00:00
// Ensure that our original value is still set.
2016-01-15 18:48:03 +00:00
if err := db.View(func(tx *bolt.Tx) error {
2015-03-04 16:38:10 +00:00
value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
fmt.Printf("The value for 'foo' is still: %s\n", value)
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
log.Fatal(err)
}
// Close database to release file lock.
if err := db.Close(); err != nil {
log.Fatal(err)
}
2015-03-04 16:38:10 +00:00
// Output:
// The value for 'foo' is still: bar
}
func ExampleTx_CopyFile() {
// Open the database.
2016-01-15 18:48:03 +00:00
db, err := bolt.Open(tempfile(), 0666, nil)
if err != nil {
log.Fatal(err)
}
2015-03-04 16:38:10 +00:00
defer os.Remove(db.Path())
// Create a bucket and a key.
2016-01-15 18:48:03 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
return err
}
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
return err
}
2015-03-04 16:38:10 +00:00
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
log.Fatal(err)
}
2015-03-04 16:38:10 +00:00
// Copy the database to another file.
toFile := tempfile()
2016-01-15 18:48:03 +00:00
if err := db.View(func(tx *bolt.Tx) error {
return tx.CopyFile(toFile, 0666)
}); err != nil {
log.Fatal(err)
}
2015-03-04 16:38:10 +00:00
defer os.Remove(toFile)
// Open the cloned database.
2016-01-15 18:48:03 +00:00
db2, err := bolt.Open(toFile, 0666, nil)
if err != nil {
log.Fatal(err)
}
2015-03-04 16:38:10 +00:00
// Ensure that the key exists in the copy.
2016-01-15 18:48:03 +00:00
if err := db2.View(func(tx *bolt.Tx) error {
2015-03-04 16:38:10 +00:00
value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
fmt.Printf("The value for 'foo' in the clone is: %s\n", value)
return nil
2016-01-15 18:48:03 +00:00
}); err != nil {
log.Fatal(err)
}
// Close database to release file lock.
if err := db.Close(); err != nil {
log.Fatal(err)
}
if err := db2.Close(); err != nil {
log.Fatal(err)
}
2015-03-04 16:38:10 +00:00
// Output:
// The value for 'foo' in the clone is: bar
}