2014-03-09 03:25:37 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2014-04-04 13:51:01 +00:00
|
|
|
"errors"
|
2014-03-09 03:25:37 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2014-03-23 18:17:30 +00:00
|
|
|
// Ensure that committing a closed transaction returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_Commit_Closed(t *testing.T) {
|
2014-03-23 18:17:30 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
tx, _ := db.Begin(true)
|
2014-04-07 22:24:51 +00:00
|
|
|
tx.CreateBucket([]byte("foo"))
|
2014-03-23 18:17:30 +00:00
|
|
|
assert.NoError(t, tx.Commit())
|
|
|
|
assert.Equal(t, tx.Commit(), ErrTxClosed)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that rolling back a closed transaction returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_Rollback_Closed(t *testing.T) {
|
2014-03-23 18:17:30 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
tx, _ := db.Begin(true)
|
2014-03-23 18:17:30 +00:00
|
|
|
assert.NoError(t, tx.Rollback())
|
|
|
|
assert.Equal(t, tx.Rollback(), ErrTxClosed)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that committing a read-only transaction returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_Commit_ReadOnly(t *testing.T) {
|
2014-03-23 18:17:30 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
tx, _ := db.Begin(false)
|
2014-03-23 18:17:30 +00:00
|
|
|
assert.Equal(t, tx.Commit(), ErrTxNotWritable)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-03-09 03:25:37 +00:00
|
|
|
// Ensure that creating a bucket with a read-only transaction returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_CreateBucket_ReadOnly(t *testing.T) {
|
2014-03-09 03:25:37 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
db.View(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
b, err := tx.CreateBucket([]byte("foo"))
|
|
|
|
assert.Nil(t, b)
|
|
|
|
assert.Equal(t, ErrTxNotWritable, err)
|
2014-03-09 03:25:37 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-03-23 18:17:30 +00:00
|
|
|
// Ensure that creating a bucket on a closed transaction returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_CreateBucket_Closed(t *testing.T) {
|
2014-03-23 18:17:30 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
tx, _ := db.Begin(true)
|
2014-03-23 18:17:30 +00:00
|
|
|
tx.Commit()
|
2014-04-16 03:45:06 +00:00
|
|
|
b, err := tx.CreateBucket([]byte("foo"))
|
|
|
|
assert.Nil(t, b)
|
|
|
|
assert.Equal(t, ErrTxClosed, err)
|
2014-03-23 18:17:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-03-09 03:25:37 +00:00
|
|
|
// Ensure that a Tx can retrieve a bucket.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_Bucket(t *testing.T) {
|
2014-03-09 03:25:37 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
tx.CreateBucket([]byte("widgets"))
|
|
|
|
b := tx.Bucket([]byte("widgets"))
|
|
|
|
assert.NotNil(t, b)
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a Tx retrieving a non-existent key returns nil.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_Get_Missing(t *testing.T) {
|
2014-03-09 03:25:37 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
tx.CreateBucket([]byte("widgets"))
|
|
|
|
tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
|
|
|
|
value := tx.Bucket([]byte("widgets")).Get([]byte("no_such_key"))
|
2014-03-21 15:46:03 +00:00
|
|
|
assert.Nil(t, value)
|
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a bucket can be created and retrieved.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_CreateBucket(t *testing.T) {
|
2014-03-09 03:25:37 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
|
|
|
// Create a bucket.
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
b, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
assert.NotNil(t, b)
|
|
|
|
assert.NoError(t, err)
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
|
|
|
|
// Read the bucket through a separate transaction.
|
2014-03-24 17:43:06 +00:00
|
|
|
db.View(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
b := tx.Bucket([]byte("widgets"))
|
2014-03-21 15:46:03 +00:00
|
|
|
assert.NotNil(t, b)
|
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a bucket can be created if it doesn't already exist.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_CreateBucketIfNotExists(t *testing.T) {
|
2014-03-09 03:25:37 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
b, err := tx.CreateBucketIfNotExists([]byte("widgets"))
|
|
|
|
assert.NotNil(t, b)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
b, err = tx.CreateBucketIfNotExists([]byte("widgets"))
|
|
|
|
assert.NotNil(t, b)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
b, err = tx.CreateBucketIfNotExists([]byte{})
|
|
|
|
assert.Nil(t, b)
|
|
|
|
assert.Equal(t, ErrBucketNameRequired, err)
|
|
|
|
|
|
|
|
b, err = tx.CreateBucketIfNotExists(nil)
|
|
|
|
assert.Nil(t, b)
|
|
|
|
assert.Equal(t, ErrBucketNameRequired, err)
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
|
|
|
|
// Read the bucket through a separate transaction.
|
2014-03-24 17:43:06 +00:00
|
|
|
db.View(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
b := tx.Bucket([]byte("widgets"))
|
2014-03-21 15:46:03 +00:00
|
|
|
assert.NotNil(t, b)
|
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a bucket cannot be created twice.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_CreateBucket_Exists(t *testing.T) {
|
2014-03-09 03:25:37 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
|
|
|
// Create a bucket.
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
b, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
assert.NotNil(t, b)
|
|
|
|
assert.NoError(t, err)
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
|
|
|
|
// Create the same bucket again.
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
b, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
assert.Nil(t, b)
|
|
|
|
assert.Equal(t, ErrBucketExists, err)
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a bucket is created with a non-blank name.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_CreateBucket_NameRequired(t *testing.T) {
|
2014-03-09 03:25:37 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
b, err := tx.CreateBucket(nil)
|
|
|
|
assert.Nil(t, b)
|
|
|
|
assert.Equal(t, ErrBucketNameRequired, err)
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a bucket can be deleted.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_DeleteBucket(t *testing.T) {
|
2014-03-09 03:25:37 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
|
|
|
// Create a bucket and add a value.
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
tx.CreateBucket([]byte("widgets"))
|
|
|
|
tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
|
2014-03-21 15:46:03 +00:00
|
|
|
// Save root page id.
|
|
|
|
var root pgid
|
2014-03-24 17:43:06 +00:00
|
|
|
db.View(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
root = tx.Bucket([]byte("widgets")).root
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
|
|
|
|
// Delete the bucket and make sure we can't get the value.
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
assert.NoError(t, tx.DeleteBucket([]byte("widgets")))
|
|
|
|
assert.Nil(t, tx.Bucket([]byte("widgets")))
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-03-21 15:46:03 +00:00
|
|
|
// Verify that the bucket's page is free.
|
2014-03-31 14:52:13 +00:00
|
|
|
assert.Equal(t, []pgid{7, 6, root, 2}, db.freelist.all())
|
2014-03-21 15:46:03 +00:00
|
|
|
|
|
|
|
// Create the bucket again and make sure there's not a phantom value.
|
2014-04-16 03:45:06 +00:00
|
|
|
b, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
assert.NotNil(t, b)
|
|
|
|
assert.NoError(t, err)
|
2014-04-07 22:24:51 +00:00
|
|
|
assert.Nil(t, tx.Bucket([]byte("widgets")).Get([]byte("foo")))
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-03-23 18:17:30 +00:00
|
|
|
// Ensure that deleting a bucket on a closed transaction returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_DeleteBucket_Closed(t *testing.T) {
|
2014-03-23 18:17:30 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
tx, _ := db.Begin(true)
|
2014-03-23 18:17:30 +00:00
|
|
|
tx.Commit()
|
2014-04-07 22:24:51 +00:00
|
|
|
assert.Equal(t, tx.DeleteBucket([]byte("foo")), ErrTxClosed)
|
2014-03-23 18:17:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-03-09 03:25:37 +00:00
|
|
|
// Ensure that deleting a bucket with a read-only transaction returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestTx_DeleteBucket_ReadOnly(t *testing.T) {
|
2014-03-09 03:25:37 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
db.View(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
assert.Equal(t, tx.DeleteBucket([]byte("foo")), ErrTxNotWritable)
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-03-09 03:25:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
// Ensure that nothing happens when deleting a bucket that doesn't exist.
|
|
|
|
func TestTx_DeleteBucket_NotFound(t *testing.T) {
|
2014-03-09 03:25:37 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
assert.Equal(t, ErrBucketNotFound, tx.DeleteBucket([]byte("widgets")))
|
2014-03-13 20:39:28 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-04-04 13:51:01 +00:00
|
|
|
// Ensure that Tx commit handlers are called after a transaction successfully commits.
|
|
|
|
func TestTx_OnCommit(t *testing.T) {
|
|
|
|
var x int
|
|
|
|
withOpenDB(func(db *DB, path string) {
|
|
|
|
db.Update(func(tx *Tx) error {
|
|
|
|
tx.OnCommit(func() { x += 1 })
|
|
|
|
tx.OnCommit(func() { x += 2 })
|
2014-04-16 03:45:06 +00:00
|
|
|
_, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
return err
|
2014-04-04 13:51:01 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
assert.Equal(t, 3, x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that Tx commit handlers are NOT called after a transaction rolls back.
|
|
|
|
func TestTx_OnCommit_Rollback(t *testing.T) {
|
|
|
|
var x int
|
|
|
|
withOpenDB(func(db *DB, path string) {
|
|
|
|
db.Update(func(tx *Tx) error {
|
|
|
|
tx.OnCommit(func() { x += 1 })
|
|
|
|
tx.OnCommit(func() { x += 2 })
|
2014-04-07 22:24:51 +00:00
|
|
|
tx.CreateBucket([]byte("widgets"))
|
2014-04-04 13:51:01 +00:00
|
|
|
return errors.New("rollback this commit")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
assert.Equal(t, 0, x)
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
func ExampleTx_Rollback() {
|
|
|
|
// Open the database.
|
|
|
|
db, _ := Open(tempfile(), 0666)
|
|
|
|
defer os.Remove(db.Path())
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Create a bucket.
|
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
_, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
return err
|
2014-04-07 22:24:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Set a value for a key.
|
|
|
|
db.Update(func(tx *Tx) error {
|
|
|
|
return tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
|
|
|
|
})
|
|
|
|
|
|
|
|
// Update the key but rollback the transaction so it never saves.
|
|
|
|
tx, _ := db.Begin(true)
|
|
|
|
b := tx.Bucket([]byte("widgets"))
|
|
|
|
b.Put([]byte("foo"), []byte("baz"))
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
// Ensure that our original value is still set.
|
|
|
|
db.View(func(tx *Tx) error {
|
|
|
|
value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
|
|
|
|
fmt.Printf("The value for 'foo' is still: %s\n", string(value))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// The value for 'foo' is still: bar
|
|
|
|
}
|