bolt/bucket_test.go

1910 lines
46 KiB
Go
Raw Normal View History

2014-07-26 21:11:47 +00:00
package bolt_test
2014-02-21 16:20:45 +00:00
import (
2014-02-23 06:08:30 +00:00
"bytes"
"encoding/binary"
"errors"
2014-02-23 06:08:30 +00:00
"fmt"
"log"
"math/rand"
2014-02-23 06:08:30 +00:00
"os"
2014-02-21 16:20:45 +00:00
"strconv"
"strings"
"testing"
2014-02-23 06:08:30 +00:00
"testing/quick"
2014-02-21 16:20:45 +00:00
2014-07-26 21:11:47 +00:00
"github.com/boltdb/bolt"
2014-02-21 16:20:45 +00:00
)
2014-02-23 06:08:30 +00:00
// Ensure that a bucket that gets a non-existent key returns nil.
func TestBucket_Get_NonExistent(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 v := b.Get([]byte("foo")); v != nil {
t.Fatal("expected nil value")
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
}
// Ensure that a bucket can read a value that is not flushed yet.
func TestBucket_Get_FromNode(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 v := b.Get([]byte("foo")); !bytes.Equal(v, []byte("bar")) {
t.Fatalf("unexpected value: %v", v)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that a bucket retrieved via Get() returns a nil.
func TestBucket_Get_IncompatibleValue(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if _, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo")); err != nil {
t.Fatal(err)
}
if tx.Bucket([]byte("widgets")).Get([]byte("foo")) != nil {
t.Fatal("expected nil value")
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that a slice returned from a bucket has a capacity equal to its length.
// This also allows slices to be appended to since it will require a realloc by Go.
//
// https://github.com/boltdb/bolt/issues/544
func TestBucket_Get_Capacity(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
// Write key to a bucket.
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("bucket"))
if err != nil {
return err
}
return b.Put([]byte("key"), []byte("val"))
}); err != nil {
t.Fatal(err)
}
// Retrieve value and attempt to append to it.
if err := db.Update(func(tx *bolt.Tx) error {
k, v := tx.Bucket([]byte("bucket")).Cursor().First()
// Verify capacity.
if len(k) != cap(k) {
t.Fatalf("unexpected key slice capacity: %d", cap(k))
} else if len(v) != cap(v) {
t.Fatalf("unexpected value slice capacity: %d", cap(v))
}
// Ensure slice can be appended to without a segfault.
k = append(k, []byte("123")...)
v = append(v, []byte("123")...)
return nil
}); err != nil {
t.Fatal(err)
}
}
2014-02-23 06:08:30 +00:00
// Ensure that a bucket can write a key/value.
func TestBucket_Put(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)
}
v := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
if !bytes.Equal([]byte("bar"), v) {
t.Fatalf("unexpected value: %v", v)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
}
// Ensure that a bucket can rewrite a key in the same transaction.
func TestBucket_Put_Repeat(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 := b.Put([]byte("foo"), []byte("baz")); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
if !bytes.Equal([]byte("baz"), value) {
t.Fatalf("unexpected value: %v", value)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that a bucket can write a bunch of large values.
func TestBucket_Put_Large(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
2014-07-26 20:44:04 +00:00
count, factor := 100, 200
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
for i := 1; i < count; i++ {
if err := b.Put([]byte(strings.Repeat("0", i*factor)), []byte(strings.Repeat("X", (count-i)*factor))); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.View(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
b := tx.Bucket([]byte("widgets"))
for i := 1; i < count; i++ {
value := b.Get([]byte(strings.Repeat("0", i*factor)))
if !bytes.Equal(value, []byte(strings.Repeat("X", (count-i)*factor))) {
t.Fatalf("unexpected value: %v", value)
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that a database can perform multiple large appends safely.
func TestDB_Put_VeryLarge(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
n, batchN := 400000, 200000
ksize, vsize := 8, 500
db := MustOpenDB()
defer db.MustClose()
2014-07-26 20:44:04 +00:00
for i := 0; i < n; i += batchN {
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
for j := 0; j < batchN; j++ {
k, v := make([]byte, ksize), make([]byte, vsize)
binary.BigEndian.PutUint32(k, uint32(i+j))
if err := b.Put(k, v); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
}
// Ensure that a setting a value on a key with a bucket value returns an error.
func TestBucket_Put_IncompatibleValue(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
b0, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if _, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo")); err != nil {
t.Fatal(err)
}
if err := b0.Put([]byte("foo"), []byte("bar")); err != bolt.ErrIncompatibleValue {
t.Fatalf("unexpected error: %s", err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that a setting a value while the transaction is closed returns an error.
func TestBucket_Put_Closed(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
tx, err := db.Begin(true)
if err != nil {
t.Fatal(err)
}
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := tx.Rollback(); err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("foo"), []byte("bar")); err != bolt.ErrTxClosed {
t.Fatalf("unexpected error: %s", err)
}
}
2014-02-23 06:08:30 +00:00
// Ensure that setting a value on a read-only bucket returns an error.
func TestBucket_Put_ReadOnly(t *testing.T) {
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)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.View(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
b := tx.Bucket([]byte("widgets"))
if err := b.Put([]byte("foo"), []byte("bar")); err != bolt.ErrTxNotWritable {
t.Fatalf("unexpected error: %s", err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
}
// Ensure that a bucket can delete an existing key.
func TestBucket_Delete(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 := b.Delete([]byte("foo")); err != nil {
t.Fatal(err)
}
if v := b.Get([]byte("foo")); v != nil {
t.Fatalf("unexpected value: %v", v)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
}
2014-05-07 16:37:50 +00:00
// Ensure that deleting a large set of keys will work correctly.
func TestBucket_Delete_Large(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)
}
2014-07-26 20:44:04 +00:00
for i := 0; i < 100; i++ {
if err := b.Put([]byte(strconv.Itoa(i)), []byte(strings.Repeat("*", 1024))); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("widgets"))
2014-07-26 20:44:04 +00:00
for i := 0; i < 100; i++ {
if err := b.Delete([]byte(strconv.Itoa(i))); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("widgets"))
2014-07-26 20:44:04 +00:00
for i := 0; i < 100; i++ {
if v := b.Get([]byte(strconv.Itoa(i))); v != nil {
t.Fatalf("unexpected value: %v, i=%d", v, i)
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-05-07 16:37:50 +00:00
}
// Deleting a very large list of keys will cause the freelist to use overflow.
func TestBucket_Delete_FreelistOverflow(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
db := MustOpenDB()
defer db.MustClose()
2014-07-26 20:44:04 +00:00
k := make([]byte, 16)
for i := uint64(0); i < 10000; i++ {
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
b, err := tx.CreateBucketIfNotExists([]byte("0"))
if err != nil {
2014-07-26 20:44:04 +00:00
t.Fatalf("bucket error: %s", err)
}
2014-07-26 20:44:04 +00:00
for j := uint64(0); j < 1000; j++ {
binary.BigEndian.PutUint64(k[:8], i)
binary.BigEndian.PutUint64(k[8:], j)
if err := b.Put(k, nil); err != nil {
t.Fatalf("put error: %s", err)
}
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
2014-07-26 20:44:04 +00:00
}
}
// Delete all of them in one large transaction
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
b := tx.Bucket([]byte("0"))
c := b.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
if err := c.Delete(); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
}
2014-05-02 19:59:23 +00:00
// Ensure that accessing and updating nested buckets is ok across transactions.
func TestBucket_Nested(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
// Create a widgets bucket.
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
2014-05-02 19:59:23 +00:00
2014-07-26 20:44:04 +00:00
// Create a widgets/foo bucket.
_, err = b.CreateBucket([]byte("foo"))
if err != nil {
t.Fatal(err)
}
2014-05-02 19:59:23 +00:00
2014-07-26 20:44:04 +00:00
// Create a widgets/bar key.
if err := b.Put([]byte("bar"), []byte("0000")); err != nil {
t.Fatal(err)
}
2014-05-02 19:59:23 +00:00
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
db.MustCheck()
2014-05-02 19:59:23 +00:00
2014-07-26 20:44:04 +00:00
// Update widgets/bar.
if err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("widgets"))
if err := b.Put([]byte("bar"), []byte("xxxx")); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
db.MustCheck()
2014-05-02 19:59:23 +00:00
2014-07-26 20:44:04 +00:00
// Cause a split.
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
var b = tx.Bucket([]byte("widgets"))
for i := 0; i < 10000; i++ {
if err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
db.MustCheck()
2014-05-02 19:59:23 +00:00
2014-07-26 20:44:04 +00:00
// Insert into widgets/foo/baz.
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
var b = tx.Bucket([]byte("widgets"))
if err := b.Bucket([]byte("foo")).Put([]byte("baz"), []byte("yyyy")); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
db.MustCheck()
// Verify.
if err := db.View(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
var b = tx.Bucket([]byte("widgets"))
if v := b.Bucket([]byte("foo")).Get([]byte("baz")); !bytes.Equal(v, []byte("yyyy")) {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected value: %v", v)
}
2016-01-08 08:31:45 +00:00
if v := b.Get([]byte("bar")); !bytes.Equal(v, []byte("xxxx")) {
t.Fatalf("unexpected value: %v", v)
}
2014-07-26 20:44:04 +00:00
for i := 0; i < 10000; i++ {
if v := b.Get([]byte(strconv.Itoa(i))); !bytes.Equal(v, []byte(strconv.Itoa(i))) {
t.Fatalf("unexpected value: %v", v)
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-05-02 19:59:23 +00:00
}
// Ensure that deleting a bucket using Delete() returns an error.
func TestBucket_Delete_Bucket(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.CreateBucket([]byte("foo")); err != nil {
t.Fatal(err)
}
if err := b.Delete([]byte("foo")); err != bolt.ErrIncompatibleValue {
t.Fatalf("unexpected error: %s", err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
2014-02-23 06:08:30 +00:00
// Ensure that deleting a key on a read-only bucket returns an error.
func TestBucket_Delete_ReadOnly(t *testing.T) {
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)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.View(func(tx *bolt.Tx) error {
if err := tx.Bucket([]byte("widgets")).Delete([]byte("foo")); err != bolt.ErrTxNotWritable {
t.Fatalf("unexpected error: %s", err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that a deleting value while the transaction is closed returns an error.
func TestBucket_Delete_Closed(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
tx, err := db.Begin(true)
if err != nil {
t.Fatal(err)
}
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := tx.Rollback(); err != nil {
t.Fatal(err)
}
if err := b.Delete([]byte("foo")); err != bolt.ErrTxClosed {
t.Fatalf("unexpected error: %s", err)
}
}
// Ensure that deleting a bucket causes nested buckets to be deleted.
func TestBucket_DeleteBucket_Nested(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
widgets, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
foo, err := widgets.CreateBucket([]byte("foo"))
if err != nil {
t.Fatal(err)
}
bar, err := foo.CreateBucket([]byte("bar"))
if err != nil {
t.Fatal(err)
}
if err := bar.Put([]byte("baz"), []byte("bat")); err != nil {
t.Fatal(err)
}
if err := tx.Bucket([]byte("widgets")).DeleteBucket([]byte("foo")); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that deleting a bucket causes nested buckets to be deleted after they have been committed.
func TestBucket_DeleteBucket_Nested2(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
widgets, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
foo, err := widgets.CreateBucket([]byte("foo"))
if err != nil {
t.Fatal(err)
}
bar, err := foo.CreateBucket([]byte("bar"))
if err != nil {
t.Fatal(err)
}
if err := bar.Put([]byte("baz"), []byte("bat")); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.Update(func(tx *bolt.Tx) error {
widgets := tx.Bucket([]byte("widgets"))
if widgets == nil {
t.Fatal("expected widgets bucket")
}
foo := widgets.Bucket([]byte("foo"))
if foo == nil {
t.Fatal("expected foo bucket")
}
bar := foo.Bucket([]byte("bar"))
if bar == nil {
t.Fatal("expected bar bucket")
}
if v := bar.Get([]byte("baz")); !bytes.Equal(v, []byte("bat")) {
t.Fatalf("unexpected value: %v", v)
}
if err := tx.DeleteBucket([]byte("widgets")); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.View(func(tx *bolt.Tx) error {
if tx.Bucket([]byte("widgets")) != nil {
t.Fatal("expected bucket to be deleted")
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that deleting a child bucket with multiple pages causes all pages to get collected.
// NOTE: Consistency check in bolt_test.DB.Close() will panic if pages not freed properly.
func TestBucket_DeleteBucket_Large(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
widgets, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
foo, err := widgets.CreateBucket([]byte("foo"))
if err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
for i := 0; i < 1000; i++ {
if err := foo.Put([]byte(fmt.Sprintf("%d", i)), []byte(fmt.Sprintf("%0100d", i))); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
if err := tx.DeleteBucket([]byte("widgets")); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that a simple value retrieved via Bucket() returns a nil.
func TestBucket_Bucket_IncompatibleValue(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
widgets, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := widgets.Put([]byte("foo"), []byte("bar")); err != nil {
t.Fatal(err)
}
if b := tx.Bucket([]byte("widgets")).Bucket([]byte("foo")); b != nil {
t.Fatal("expected nil bucket")
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that creating a bucket on an existing non-bucket key returns an error.
func TestBucket_CreateBucket_IncompatibleValue(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
widgets, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := widgets.Put([]byte("foo"), []byte("bar")); err != nil {
t.Fatal(err)
}
if _, err := widgets.CreateBucket([]byte("foo")); err != bolt.ErrIncompatibleValue {
t.Fatalf("unexpected error: %s", err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure that deleting a bucket on an existing non-bucket key returns an error.
func TestBucket_DeleteBucket_IncompatibleValue(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
widgets, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := widgets.Put([]byte("foo"), []byte("bar")); err != nil {
t.Fatal(err)
}
if err := tx.Bucket([]byte("widgets")).DeleteBucket([]byte("foo")); err != bolt.ErrIncompatibleValue {
t.Fatalf("unexpected error: %s", err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
}
2016-09-01 21:34:35 +00:00
// Ensure bucket can set and update its sequence number.
func TestBucket_Sequence(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
bkt, err := tx.CreateBucket([]byte("0"))
if err != nil {
t.Fatal(err)
}
// Retrieve sequence.
if v := bkt.Sequence(); v != 0 {
t.Fatalf("unexpected sequence: %d", v)
}
// Update sequence.
if err := bkt.SetSequence(1000); err != nil {
t.Fatal(err)
}
// Read sequence again.
if v := bkt.Sequence(); v != 1000 {
t.Fatalf("unexpected sequence: %d", v)
}
return nil
}); err != nil {
t.Fatal(err)
}
// Verify sequence in separate transaction.
if err := db.View(func(tx *bolt.Tx) error {
if v := tx.Bucket([]byte("0")).Sequence(); v != 1000 {
t.Fatalf("unexpected sequence: %d", v)
}
return nil
}); err != nil {
t.Fatal(err)
}
}
2014-02-23 06:08:30 +00:00
// Ensure that a bucket can return an autoincrementing sequence.
func TestBucket_NextSequence(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
widgets, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
woojits, err := tx.CreateBucket([]byte("woojits"))
if err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
// Make sure sequence increments.
if seq, err := widgets.NextSequence(); err != nil {
t.Fatal(err)
} else if seq != 1 {
t.Fatalf("unexpecte sequence: %d", seq)
}
if seq, err := widgets.NextSequence(); err != nil {
t.Fatal(err)
} else if seq != 2 {
t.Fatalf("unexpected sequence: %d", seq)
}
2014-07-26 20:44:04 +00:00
// Buckets should be separate.
if seq, err := woojits.NextSequence(); err != nil {
t.Fatal(err)
} else if seq != 1 {
t.Fatalf("unexpected sequence: %d", 1)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
}
// Ensure that a bucket will persist an autoincrementing sequence even if its
// the only thing updated on the bucket.
// https://github.com/boltdb/bolt/issues/296
func TestBucket_NextSequence_Persist(t *testing.T) {
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)
}
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.Update(func(tx *bolt.Tx) error {
if _, err := tx.Bucket([]byte("widgets")).NextSequence(); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.Update(func(tx *bolt.Tx) error {
seq, err := tx.Bucket([]byte("widgets")).NextSequence()
if err != nil {
t.Fatalf("unexpected error: %s", err)
} else if seq != 2 {
t.Fatalf("unexpected sequence: %d", seq)
}
return nil
}); err != nil {
t.Fatal(err)
}
}
2014-02-23 06:08:30 +00:00
// Ensure that retrieving the next sequence on a read-only bucket returns an error.
func TestBucket_NextSequence_ReadOnly(t *testing.T) {
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)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.View(func(tx *bolt.Tx) error {
_, err := tx.Bucket([]byte("widgets")).NextSequence()
if err != bolt.ErrTxNotWritable {
t.Fatalf("unexpected error: %s", err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
}
// Ensure that retrieving the next sequence for a bucket on a closed database return an error.
func TestBucket_NextSequence_Closed(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
tx, err := db.Begin(true)
if err != nil {
t.Fatal(err)
}
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := tx.Rollback(); err != nil {
t.Fatal(err)
}
if _, err := b.NextSequence(); err != bolt.ErrTxClosed {
t.Fatal(err)
}
}
// Ensure a user can loop over all key/value pairs in a bucket.
func TestBucket_ForEach(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("0000")); err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("baz"), []byte("0001")); err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("bar"), []byte("0002")); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
var index int
if err := b.ForEach(func(k, v []byte) error {
2014-07-26 20:44:04 +00:00
switch index {
case 0:
if !bytes.Equal(k, []byte("bar")) {
t.Fatalf("unexpected key: %v", k)
} else if !bytes.Equal(v, []byte("0002")) {
t.Fatalf("unexpected value: %v", v)
}
2014-07-26 20:44:04 +00:00
case 1:
if !bytes.Equal(k, []byte("baz")) {
t.Fatalf("unexpected key: %v", k)
} else if !bytes.Equal(v, []byte("0001")) {
t.Fatalf("unexpected value: %v", v)
}
2014-07-26 20:44:04 +00:00
case 2:
if !bytes.Equal(k, []byte("foo")) {
t.Fatalf("unexpected key: %v", k)
} else if !bytes.Equal(v, []byte("0000")) {
t.Fatalf("unexpected value: %v", v)
}
2014-07-26 20:44:04 +00:00
}
index++
2014-02-23 06:08:30 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
if index != 3 {
t.Fatalf("unexpected index: %d", index)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
}
// Ensure a database can stop iteration early.
func TestBucket_ForEach_ShortCircuit(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("bar"), []byte("0000")); err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("baz"), []byte("0000")); err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("foo"), []byte("0000")); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
var index int
if err := tx.Bucket([]byte("widgets")).ForEach(func(k, v []byte) error {
2014-07-26 20:44:04 +00:00
index++
if bytes.Equal(k, []byte("baz")) {
return errors.New("marker")
}
2014-02-23 06:08:30 +00:00
return nil
}); err == nil || err.Error() != "marker" {
t.Fatalf("unexpected error: %s", err)
}
if index != 2 {
t.Fatalf("unexpected index: %d", index)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
}
// Ensure that looping over a bucket on a closed database returns an error.
func TestBucket_ForEach_Closed(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
tx, err := db.Begin(true)
if err != nil {
t.Fatal(err)
}
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
if err := tx.Rollback(); err != nil {
t.Fatal(err)
}
if err := b.ForEach(func(k, v []byte) error { return nil }); err != bolt.ErrTxClosed {
t.Fatalf("unexpected error: %s", err)
}
}
2014-02-23 06:08:30 +00:00
// Ensure that an error is returned when inserting with an empty key.
func TestBucket_Put_EmptyKey(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(""), []byte("bar")); err != bolt.ErrKeyRequired {
t.Fatalf("unexpected error: %s", err)
}
if err := b.Put(nil, []byte("bar")); err != bolt.ErrKeyRequired {
t.Fatalf("unexpected error: %s", err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
}
// Ensure that an error is returned when inserting with a key that's too large.
func TestBucket_Put_KeyTooLarge(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(make([]byte, 32769), []byte("bar")); err != bolt.ErrKeyTooLarge {
t.Fatalf("unexpected error: %s", err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
}
// Ensure that an error is returned when inserting a value that's too large.
func TestBucket_Put_ValueTooLarge(t *testing.T) {
// Skip this test on DroneCI because the machine is resource constrained.
if os.Getenv("DRONE") == "true" {
t.Skip("not enough RAM for test")
}
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"), make([]byte, bolt.MaxValueSize+1)); err != bolt.ErrValueTooLarge {
t.Fatalf("unexpected error: %s", err)
}
return nil
}); err != nil {
t.Fatal(err)
}
}
2014-02-21 16:20:45 +00:00
// Ensure a bucket can calculate stats.
func TestBucket_Stats(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
2014-07-26 20:44:04 +00:00
// Add bucket with fewer keys but one big value.
bigKey := []byte("really-big-value")
2014-07-26 20:44:04 +00:00
for i := 0; i < 500; i++ {
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("woojits"))
if err != nil {
t.Fatal(err)
}
if err := b.Put([]byte(fmt.Sprintf("%03d", i)), []byte(strconv.Itoa(i))); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
}
}
if err := db.Update(func(tx *bolt.Tx) error {
if err := tx.Bucket([]byte("woojits")).Put(bigKey, []byte(strings.Repeat("*", 10000))); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
2014-07-26 20:44:04 +00:00
}
2014-07-26 20:44:04 +00:00
db.MustCheck()
if err := db.View(func(tx *bolt.Tx) error {
stats := tx.Bucket([]byte("woojits")).Stats()
if stats.BranchPageN != 1 {
t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
} else if stats.BranchOverflowN != 0 {
t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
} else if stats.LeafPageN != 7 {
t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
} else if stats.LeafOverflowN != 2 {
t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
} else if stats.KeyN != 501 {
t.Fatalf("unexpected KeyN: %d", stats.KeyN)
} else if stats.Depth != 2 {
t.Fatalf("unexpected Depth: %d", stats.Depth)
}
2014-07-26 20:44:04 +00:00
2014-07-26 21:11:47 +00:00
branchInuse := 16 // branch page header
branchInuse += 7 * 16 // branch elements
branchInuse += 7 * 3 // branch keys (6 3-byte keys)
if stats.BranchInuse != branchInuse {
t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
}
2014-07-26 20:44:04 +00:00
2014-07-26 21:11:47 +00:00
leafInuse := 7 * 16 // leaf page header
leafInuse += 501 * 16 // leaf elements
leafInuse += 500*3 + len(bigKey) // leaf keys
2014-07-26 20:44:04 +00:00
leafInuse += 1*10 + 2*90 + 3*400 + 10000 // leaf values
if stats.LeafInuse != leafInuse {
t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
}
2014-07-26 20:44:04 +00:00
// Only check allocations for 4KB pages.
2014-07-26 20:44:04 +00:00
if os.Getpagesize() == 4096 {
if stats.BranchAlloc != 4096 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
} else if stats.LeafAlloc != 36864 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
}
}
if stats.BucketN != 1 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BucketN: %d", stats.BucketN)
} else if stats.InlineBucketN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
} else if stats.InlineBucketInuse != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse)
2014-07-26 20:44:04 +00:00
}
2014-05-14 18:06:31 +00:00
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-04-22 20:18:09 +00:00
}
// Ensure a bucket with random insertion utilizes fill percentage correctly.
func TestBucket_Stats_RandomFill(t *testing.T) {
2014-06-03 19:21:28 +00:00
if testing.Short() {
t.Skip("skipping test in short mode.")
2014-07-26 20:44:04 +00:00
} else if os.Getpagesize() != 4096 {
t.Skip("invalid page size for test")
}
db := MustOpenDB()
defer db.MustClose()
2014-07-26 20:44:04 +00:00
// Add a set of values in random order. It will be the same random
// order so we can maintain consistency between test runs.
var count int
rand := rand.New(rand.NewSource(42))
for _, i := range rand.Perm(1000) {
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("woojits"))
if err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
b.FillPercent = 0.9
for _, j := range rand.Perm(100) {
2014-07-26 20:44:04 +00:00
index := (j * 10000) + i
if err := b.Put([]byte(fmt.Sprintf("%d000000000000000", index)), []byte("0000000000")); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
count++
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
2014-07-26 20:44:04 +00:00
db.MustCheck()
if err := db.View(func(tx *bolt.Tx) error {
stats := tx.Bucket([]byte("woojits")).Stats()
if stats.KeyN != 100000 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected KeyN: %d", stats.KeyN)
}
2014-07-26 20:44:04 +00:00
if stats.BranchPageN != 98 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
} else if stats.BranchOverflowN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
} else if stats.BranchInuse != 130984 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
} else if stats.BranchAlloc != 401408 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
}
2014-07-26 20:44:04 +00:00
if stats.LeafPageN != 3412 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
} else if stats.LeafOverflowN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
} else if stats.LeafInuse != 4742482 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
} else if stats.LeafAlloc != 13975552 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
2014-04-22 20:18:09 +00:00
// Ensure a bucket can calculate stats.
func TestBucket_Stats_Small(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
// Add a bucket that fits on a single root leaf.
b, err := tx.CreateBucket([]byte("whozawhats"))
if err != nil {
t.Fatal(err)
}
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
db.MustCheck()
if err := db.View(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
b := tx.Bucket([]byte("whozawhats"))
stats := b.Stats()
if stats.BranchPageN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
} else if stats.BranchOverflowN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
} else if stats.LeafPageN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
} else if stats.LeafOverflowN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
} else if stats.KeyN != 1 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected KeyN: %d", stats.KeyN)
} else if stats.Depth != 1 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected Depth: %d", stats.Depth)
} else if stats.BranchInuse != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
} else if stats.LeafInuse != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
}
2014-07-26 20:44:04 +00:00
if os.Getpagesize() == 4096 {
if stats.BranchAlloc != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
} else if stats.LeafAlloc != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
}
}
if stats.BucketN != 1 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BucketN: %d", stats.BucketN)
} else if stats.InlineBucketN != 1 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
} else if stats.InlineBucketInuse != 16+16+6 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse)
2014-07-26 20:44:04 +00:00
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-21 16:20:45 +00:00
}
2014-02-23 06:08:30 +00:00
2014-05-12 18:22:45 +00:00
func TestBucket_Stats_EmptyBucket(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
2014-05-12 18:22:45 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
// Add a bucket that fits on a single root leaf.
if _, err := tx.CreateBucket([]byte("whozawhats")); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
db.MustCheck()
if err := db.View(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
b := tx.Bucket([]byte("whozawhats"))
stats := b.Stats()
if stats.BranchPageN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
} else if stats.BranchOverflowN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
} else if stats.LeafPageN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
} else if stats.LeafOverflowN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
} else if stats.KeyN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected KeyN: %d", stats.KeyN)
} else if stats.Depth != 1 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected Depth: %d", stats.Depth)
} else if stats.BranchInuse != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
} else if stats.LeafInuse != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
}
2014-07-26 20:44:04 +00:00
if os.Getpagesize() == 4096 {
if stats.BranchAlloc != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
} else if stats.LeafAlloc != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
}
}
if stats.BucketN != 1 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BucketN: %d", stats.BucketN)
} else if stats.InlineBucketN != 1 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
} else if stats.InlineBucketInuse != 16 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse)
2014-07-26 20:44:04 +00:00
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-05-12 18:22:45 +00:00
}
// Ensure a bucket can calculate stats.
func TestBucket_Stats_Nested(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()
2014-05-14 18:06:31 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
b, err := tx.CreateBucket([]byte("foo"))
if err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
for i := 0; i < 100; i++ {
if err := b.Put([]byte(fmt.Sprintf("%02d", i)), []byte(fmt.Sprintf("%02d", i))); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
2014-07-26 20:44:04 +00:00
bar, err := b.CreateBucket([]byte("bar"))
if err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
for i := 0; i < 10; i++ {
if err := bar.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
2014-07-26 20:44:04 +00:00
baz, err := bar.CreateBucket([]byte("baz"))
if err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
for i := 0; i < 10; i++ {
if err := baz.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-05-14 18:06:31 +00:00
2014-07-26 20:44:04 +00:00
db.MustCheck()
if err := db.View(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
b := tx.Bucket([]byte("foo"))
stats := b.Stats()
if stats.BranchPageN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
} else if stats.BranchOverflowN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
} else if stats.LeafPageN != 2 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
} else if stats.LeafOverflowN != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
} else if stats.KeyN != 122 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected KeyN: %d", stats.KeyN)
} else if stats.Depth != 3 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected Depth: %d", stats.Depth)
} else if stats.BranchInuse != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
}
2014-07-26 20:44:04 +00:00
2014-07-26 21:11:47 +00:00
foo := 16 // foo (pghdr)
foo += 101 * 16 // foo leaf elements
foo += 100*2 + 100*2 // foo leaf key/values
foo += 3 + 16 // foo -> bar key/value
2014-07-26 20:44:04 +00:00
2014-07-26 21:11:47 +00:00
bar := 16 // bar (pghdr)
bar += 11 * 16 // bar leaf elements
bar += 10 + 10 // bar leaf key/values
bar += 3 + 16 // bar -> baz key/value
2014-07-26 20:44:04 +00:00
2014-07-26 21:11:47 +00:00
baz := 16 // baz (inline) (pghdr)
baz += 10 * 16 // baz leaf elements
baz += 10 + 10 // baz leaf key/values
2014-07-26 20:44:04 +00:00
if stats.LeafInuse != foo+bar+baz {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
}
2014-07-26 20:44:04 +00:00
if os.Getpagesize() == 4096 {
if stats.BranchAlloc != 0 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
} else if stats.LeafAlloc != 8192 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
}
2014-07-26 20:44:04 +00:00
}
if stats.BucketN != 3 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected BucketN: %d", stats.BucketN)
} else if stats.InlineBucketN != 1 {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
} else if stats.InlineBucketInuse != baz {
2016-01-08 08:31:45 +00:00
t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
// Ensure a large bucket can calculate stats.
func TestBucket_Stats_Large(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
db := MustOpenDB()
defer db.MustClose()
2014-05-02 19:59:23 +00:00
2014-07-26 20:44:04 +00:00
var index int
for i := 0; i < 100; i++ {
// Add bucket with lots of keys.
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("widgets"))
if err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
for i := 0; i < 1000; i++ {
if err := b.Put([]byte(strconv.Itoa(index)), []byte(strconv.Itoa(index))); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
index++
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
2014-07-26 20:44:04 +00:00
db.MustCheck()
if err := db.View(func(tx *bolt.Tx) error {
stats := tx.Bucket([]byte("widgets")).Stats()
if stats.BranchPageN != 13 {
t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
} else if stats.BranchOverflowN != 0 {
t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
} else if stats.LeafPageN != 1196 {
t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
} else if stats.LeafOverflowN != 0 {
t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
} else if stats.KeyN != 100000 {
t.Fatalf("unexpected KeyN: %d", stats.KeyN)
} else if stats.Depth != 3 {
t.Fatalf("unexpected Depth: %d", stats.Depth)
} else if stats.BranchInuse != 25257 {
t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
} else if stats.LeafInuse != 2596916 {
t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
}
2014-07-26 20:44:04 +00:00
if os.Getpagesize() == 4096 {
if stats.BranchAlloc != 53248 {
t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
} else if stats.LeafAlloc != 4898816 {
t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
}
}
if stats.BucketN != 1 {
t.Fatalf("unexpected BucketN: %d", stats.BucketN)
} else if stats.InlineBucketN != 0 {
t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
} else if stats.InlineBucketInuse != 0 {
t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse)
2014-07-26 20:44:04 +00:00
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
}
2014-03-09 03:25:37 +00:00
// Ensure that a bucket can write random keys and values across multiple transactions.
func TestBucket_Put_Single(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2014-02-23 06:08:30 +00:00
index := 0
if err := quick.Check(func(items testdata) bool {
db := MustOpenDB()
defer db.MustClose()
2014-07-26 20:44:04 +00:00
m := make(map[string][]byte)
2014-02-23 06:08:30 +00:00
if err := db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
for _, item := range items {
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
if err := tx.Bucket([]byte("widgets")).Put(item.Key, item.Value); err != nil {
panic("put error: " + err.Error())
}
m[string(item.Key)] = item.Value
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
// Verify all key/values so far.
if err := db.View(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
i := 0
for k, v := range m {
value := tx.Bucket([]byte("widgets")).Get([]byte(k))
if !bytes.Equal(value, v) {
t.Logf("value mismatch [run %d] (%d of %d):\nkey: %x\ngot: %x\nexp: %x", index, i, len(m), []byte(k), value, v)
db.CopyTempFile()
t.FailNow()
2014-02-23 06:08:30 +00:00
}
2014-07-26 20:44:04 +00:00
i++
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
2014-02-23 06:08:30 +00:00
index++
return true
}, nil); err != nil {
2014-02-23 06:08:30 +00:00
t.Error(err)
}
}
// Ensure that a transaction can insert multiple key/value pairs at once.
func TestBucket_Put_Multiple(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
if err := quick.Check(func(items testdata) bool {
db := MustOpenDB()
defer db.MustClose()
2014-07-26 20:44:04 +00:00
// Bulk insert all values.
if err := db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
b := tx.Bucket([]byte("widgets"))
for _, item := range items {
if err := b.Put(item.Key, item.Value); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
2014-07-26 20:44:04 +00:00
// Verify all items exist.
if err := db.View(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
b := tx.Bucket([]byte("widgets"))
for _, item := range items {
value := b.Get(item.Key)
2014-07-26 23:17:03 +00:00
if !bytes.Equal(item.Value, value) {
2014-07-26 20:44:04 +00:00
db.CopyTempFile()
2014-07-26 23:17:03 +00:00
t.Fatalf("exp=%x; got=%x", item.Value, value)
2014-02-23 06:08:30 +00:00
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
return true
}, qconfig()); err != nil {
2014-02-23 06:08:30 +00:00
t.Error(err)
}
}
// Ensure that a transaction can delete all key/value pairs and return to a single leaf page.
func TestBucket_Delete_Quick(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
if err := quick.Check(func(items testdata) bool {
db := MustOpenDB()
defer db.MustClose()
2014-07-26 20:44:04 +00:00
// Bulk insert all values.
if err := db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
b := tx.Bucket([]byte("widgets"))
for _, item := range items {
if err := b.Put(item.Key, item.Value); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
// Remove items one at a time and check consistency.
for _, item := range items {
if err := db.Update(func(tx *bolt.Tx) error {
2014-07-26 20:44:04 +00:00
return tx.Bucket([]byte("widgets")).Delete(item.Key)
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
}
2014-02-23 06:08:30 +00:00
2014-07-26 20:44:04 +00:00
// Anything before our deletion index should be nil.
if err := db.View(func(tx *bolt.Tx) error {
if err := tx.Bucket([]byte("widgets")).ForEach(func(k, v []byte) error {
2014-07-26 20:44:04 +00:00
t.Fatalf("bucket should be empty; found: %06x", trunc(k, 3))
return nil
}); err != nil {
t.Fatal(err)
}
2014-07-26 20:44:04 +00:00
return nil
}); err != nil {
t.Fatal(err)
}
2014-02-23 06:08:30 +00:00
return true
}, qconfig()); err != nil {
2014-02-23 06:08:30 +00:00
t.Error(err)
}
}
func ExampleBucket_Put() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
if err != nil {
log.Fatal(err)
}
defer os.Remove(db.Path())
// Start a write transaction.
if err := db.Update(func(tx *bolt.Tx) error {
// Create a bucket.
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
return err
}
// Set the value "bar" for the key "foo".
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
return err
}
return nil
}); err != nil {
log.Fatal(err)
}
// Read value back in a different read-only transaction.
if err := db.View(func(tx *bolt.Tx) error {
value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
fmt.Printf("The value of 'foo' is: %s\n", value)
return nil
}); err != nil {
log.Fatal(err)
}
// Close database to release file lock.
if err := db.Close(); err != nil {
log.Fatal(err)
}
// Output:
// The value of 'foo' is: bar
}
func ExampleBucket_Delete() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
if err != nil {
log.Fatal(err)
}
defer os.Remove(db.Path())
// Start a write transaction.
if err := db.Update(func(tx *bolt.Tx) error {
// Create a bucket.
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
return err
}
// Set the value "bar" for the key "foo".
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
return err
}
// Retrieve the key back from the database and verify it.
value := b.Get([]byte("foo"))
fmt.Printf("The value of 'foo' was: %s\n", value)
return nil
}); err != nil {
log.Fatal(err)
}
// Delete the key in a different write transaction.
if err := db.Update(func(tx *bolt.Tx) error {
return tx.Bucket([]byte("widgets")).Delete([]byte("foo"))
}); err != nil {
log.Fatal(err)
}
// Retrieve the key again.
if err := db.View(func(tx *bolt.Tx) error {
value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
if value == nil {
fmt.Printf("The value of 'foo' is now: nil\n")
}
return nil
}); err != nil {
log.Fatal(err)
}
// Close database to release file lock.
if err := db.Close(); err != nil {
log.Fatal(err)
}
// Output:
// The value of 'foo' was: bar
// The value of 'foo' is now: nil
}
func ExampleBucket_ForEach() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
if err != nil {
log.Fatal(err)
}
defer os.Remove(db.Path())
// Insert data into a bucket.
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("animals"))
if err != nil {
return err
}
if err := b.Put([]byte("dog"), []byte("fun")); err != nil {
return err
}
if err := b.Put([]byte("cat"), []byte("lame")); err != nil {
return err
}
if err := b.Put([]byte("liger"), []byte("awesome")); err != nil {
return err
}
// Iterate over items in sorted key order.
if err := b.ForEach(func(k, v []byte) error {
fmt.Printf("A %s is %s.\n", k, v)
return nil
}); err != nil {
return err
}
return nil
}); err != nil {
log.Fatal(err)
}
// Close database to release file lock.
if err := db.Close(); err != nil {
log.Fatal(err)
}
// Output:
// A cat is lame.
// A dog is fun.
// A liger is awesome.
2014-02-23 06:08:30 +00:00
}