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"
|
2014-06-06 23:14:17 +00:00
|
|
|
"encoding/binary"
|
2014-03-24 14:31:15 +00:00
|
|
|
"errors"
|
2014-02-23 06:08:30 +00:00
|
|
|
"fmt"
|
2015-12-27 22:27:39 +00:00
|
|
|
"log"
|
2014-06-02 21:26:58 +00:00
|
|
|
"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.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_Get_NonExistent(t *testing.T) {
|
2015-12-27 22:27:39 +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 v := b.Get([]byte("foo")); v != nil {
|
|
|
|
t.Fatal("expected nil value")
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-23 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 16:13:59 +00:00
|
|
|
// Ensure that a bucket can read a value that is not flushed yet.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_Get_FromNode(t *testing.T) {
|
2015-12-27 22:27:39 +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 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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-03-01 16:13:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
// Ensure that a bucket retrieved via Get() returns a nil.
|
|
|
|
func TestBucket_Get_IncompatibleValue(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 14:58:56 +00:00
|
|
|
// 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.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_Put(t *testing.T) {
|
2015-12-27 22:27:39 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-23 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
// Ensure that a bucket can rewrite a key in the same transaction.
|
|
|
|
func TestBucket_Put_Repeat(t *testing.T) {
|
2015-12-27 22:27:39 +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("foo"), []byte("baz")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
|
2015-12-27 22:27:39 +00:00
|
|
|
if !bytes.Equal([]byte("baz"), value) {
|
|
|
|
t.Fatalf("unexpected value: %v", value)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a bucket can write a bunch of large values.
|
|
|
|
func TestBucket_Put_Large(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
db := MustOpenDB()
|
|
|
|
defer db.MustClose()
|
2014-07-26 20:44:04 +00:00
|
|
|
|
|
|
|
count, factor := 100, 200
|
2015-12-27 22:27:39 +00:00
|
|
|
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++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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)))
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 21:08:59 +00:00
|
|
|
// 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
|
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
db := MustOpenDB()
|
|
|
|
defer db.MustClose()
|
2014-07-26 20:44:04 +00:00
|
|
|
|
|
|
|
for i := 0; i < n; i += batchN {
|
2015-12-27 22:27:39 +00:00
|
|
|
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))
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := b.Put(k, v); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
2014-07-23 21:08:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +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) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a setting a value while the transaction is closed returns an error.
|
|
|
|
func TestBucket_Put_Closed(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
2014-02-23 06:08:30 +00:00
|
|
|
// Ensure that setting a value on a read-only bucket returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_Put_ReadOnly(t *testing.T) {
|
2015-12-27 22:27:39 +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)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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"))
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-23 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a bucket can delete an existing key.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_Delete(t *testing.T) {
|
2015-12-27 22:27:39 +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.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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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) {
|
2015-12-27 22:27:39 +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)
|
|
|
|
}
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
|
|
}
|
2015-12-27 22:27:39 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := b.Delete([]byte(strconv.Itoa(i))); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-05-07 16:37:50 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 20:50:21 +00:00
|
|
|
// Deleting a very large list of keys will cause the freelist to use overflow.
|
|
|
|
func TestBucket_Delete_FreelistOverflow(t *testing.T) {
|
2014-06-06 23:14:17 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
db := MustOpenDB()
|
|
|
|
defer db.MustClose()
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
k := make([]byte, 16)
|
|
|
|
for i := uint64(0); i < 10000; i++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 20:44:04 +00:00
|
|
|
b, err := tx.CreateBucketIfNotExists([]byte("0"))
|
2014-06-06 23:14:17 +00:00
|
|
|
if err != nil {
|
2014-07-26 20:44:04 +00:00
|
|
|
t.Fatalf("bucket error: %s", err)
|
2014-06-06 23:14:17 +00:00
|
|
|
}
|
|
|
|
|
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-06-06 23:14:17 +00:00
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
|
2014-06-06 23:14:17 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all of them in one large transaction
|
2015-12-27 22:27:39 +00:00
|
|
|
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() {
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := c.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-06-06 23:14:17 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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"))
|
2015-12-27 22:27:39 +00:00
|
|
|
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"))
|
2015-12-27 22:27:39 +00:00
|
|
|
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.
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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.
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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.
|
2015-12-27 22:27:39 +00:00
|
|
|
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++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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.
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 20:44:04 +00:00
|
|
|
var b = tx.Bucket([]byte("widgets"))
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
db.MustCheck()
|
|
|
|
|
|
|
|
// Verify.
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := db.View(func(tx *bolt.Tx) error {
|
2014-07-26 20:44:04 +00:00
|
|
|
var b = tx.Bucket([]byte("widgets"))
|
2015-12-27 22:27:39 +00:00
|
|
|
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)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
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)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
for i := 0; i < 10000; i++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-05-02 19:59:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
// Ensure that deleting a bucket using Delete() returns an error.
|
|
|
|
func TestBucket_Delete_Bucket(t *testing.T) {
|
2015-12-27 22:27:39 +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.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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
2014-02-23 06:08:30 +00:00
|
|
|
// Ensure that deleting a key on a read-only bucket returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_Delete_ReadOnly(t *testing.T) {
|
2015-12-27 22:27:39 +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)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a deleting value while the transaction is closed returns an error.
|
|
|
|
func TestBucket_Delete_Closed(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that deleting a bucket causes nested buckets to be deleted.
|
|
|
|
func TestBucket_DeleteBucket_Nested(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that deleting a bucket causes nested buckets to be deleted after they have been committed.
|
|
|
|
func TestBucket_DeleteBucket_Nested2(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that deleting a child bucket with multiple pages causes all pages to get collected.
|
2015-12-27 22:27:39 +00:00
|
|
|
// NOTE: Consistency check in bolt_test.DB.Close() will panic if pages not freed properly.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_DeleteBucket_Large(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
|
2015-12-27 22:27:39 +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)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a simple value retrieved via Bucket() returns a nil.
|
|
|
|
func TestBucket_Bucket_IncompatibleValue(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that creating a bucket on an existing non-bucket key returns an error.
|
|
|
|
func TestBucket_CreateBucket_IncompatibleValue(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that deleting a bucket on an existing non-bucket key returns an error.
|
|
|
|
func TestBucket_DeleteBucket_IncompatibleValue(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_NextSequence(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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-03-21 15:46:03 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
// Make sure sequence increments.
|
2015-12-27 22:27:39 +00:00
|
|
|
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-03-21 15:46:03 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
// Buckets should be separate.
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-23 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 15:27:34 +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) {
|
2015-12-27 22:27:39 +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-02-02 15:27:34 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-02 15:27:34 +00:00
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := db.Update(func(tx *bolt.Tx) error {
|
|
|
|
if _, err := tx.Bucket([]byte("widgets")).NextSequence(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-02 15:27:34 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-02 15:27:34 +00:00
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := db.Update(func(tx *bolt.Tx) error {
|
2015-02-02 15:27:34 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-02 15:27:34 +00:00
|
|
|
}
|
|
|
|
|
2014-02-23 06:08:30 +00:00
|
|
|
// Ensure that retrieving the next sequence on a read-only bucket returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_NextSequence_ReadOnly(t *testing.T) {
|
2015-12-27 22:27:39 +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)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-23 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +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) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a user can loop over all key/value pairs in a bucket.
|
|
|
|
func TestBucket_ForEach(t *testing.T) {
|
2015-12-27 22:27:39 +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("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
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := b.ForEach(func(k, v []byte) error {
|
2014-07-26 20:44:04 +00:00
|
|
|
switch index {
|
|
|
|
case 0:
|
2015-12-27 22:27:39 +00:00
|
|
|
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:
|
2015-12-27 22:27:39 +00:00
|
|
|
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:
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if index != 3 {
|
|
|
|
t.Fatalf("unexpected index: %d", index)
|
|
|
|
}
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-23 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a database can stop iteration early.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_ForEach_ShortCircuit(t *testing.T) {
|
2015-12-27 22:27:39 +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("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-03-21 15:46:03 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
var index int
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-23 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
// Ensure that looping over a bucket on a closed database returns an error.
|
|
|
|
func TestBucket_ForEach_Closed(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
2014-02-23 06:08:30 +00:00
|
|
|
// Ensure that an error is returned when inserting with an empty key.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_Put_EmptyKey(t *testing.T) {
|
2015-12-27 22:27:39 +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(""), []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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_Put_KeyTooLarge(t *testing.T) {
|
2015-12-27 22:27:39 +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(make([]byte, 32769), []byte("bar")); err != bolt.ErrKeyTooLarge {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-23 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 20:35:27 +00:00
|
|
|
// Ensure that an error is returned when inserting a value that's too large.
|
|
|
|
func TestBucket_Put_ValueTooLarge(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
// Skip this test on DroneCI because the machine is resource constrained.
|
2015-06-04 17:48:43 +00:00
|
|
|
if os.Getenv("DRONE") == "true" {
|
|
|
|
t.Skip("not enough RAM for test")
|
|
|
|
}
|
|
|
|
|
2015-12-27 22:27:39 +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"), make([]byte, bolt.MaxValueSize+1)); err != bolt.ErrValueTooLarge {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2015-05-12 20:35:27 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-12 20:35:27 +00:00
|
|
|
}
|
|
|
|
|
2014-02-21 16:20:45 +00:00
|
|
|
// Ensure a bucket can calculate stats.
|
2014-04-22 21:11:49 +00:00
|
|
|
func TestBucket_Stats(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
db := MustOpenDB()
|
|
|
|
defer db.MustClose()
|
2014-07-26 20:44:04 +00:00
|
|
|
|
|
|
|
// Add bucket with fewer keys but one big value.
|
2015-12-27 22:27:39 +00:00
|
|
|
bigKey := []byte("really-big-value")
|
2014-07-26 20:44:04 +00:00
|
|
|
for i := 0; i < 500; i++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
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-06-02 21:26:58 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
db.MustCheck()
|
2015-12-27 22:27:39 +00:00
|
|
|
|
|
|
|
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)
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
if stats.LeafInuse != leafInuse {
|
|
|
|
t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
// Only check allocations for 4KB pages.
|
2014-07-26 20:44:04 +00:00
|
|
|
if os.Getpagesize() == 4096 {
|
2015-12-27 22:27:39 +00:00
|
|
|
if stats.BranchAlloc != 4096 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafAlloc != 36864 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if stats.BucketN != 1 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BucketN: %d", stats.BucketN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.InlineBucketN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} 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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-22 20:18:09 +00:00
|
|
|
}
|
|
|
|
|
2014-06-02 21:26:58 +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 {
|
2014-06-02 21:26:58 +00:00
|
|
|
t.Skip("invalid page size for test")
|
|
|
|
}
|
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
db := MustOpenDB()
|
|
|
|
defer db.MustClose()
|
2014-06-02 21:26:58 +00:00
|
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
for _, j := range rand.Perm(100) {
|
2014-07-26 20:44:04 +00:00
|
|
|
index := (j * 10000) + i
|
2015-12-27 22:27:39 +00:00
|
|
|
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++
|
|
|
|
}
|
2014-06-02 21:26:58 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
2015-12-27 22:27:39 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
db.MustCheck()
|
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
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)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
if stats.BranchPageN != 98 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.BranchOverflowN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.BranchInuse != 130984 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.BranchAlloc != 401408 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
if stats.LeafPageN != 3412 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafOverflowN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafInuse != 4742482 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafAlloc != 13975552 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-06-02 21:26:58 +00:00
|
|
|
}
|
|
|
|
|
2014-04-22 20:18:09 +00:00
|
|
|
// Ensure a bucket can calculate stats.
|
2014-04-22 21:11:49 +00:00
|
|
|
func TestBucket_Stats_Small(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
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"))
|
2015-12-27 22:27:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-22 20:29:37 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
db.MustCheck()
|
2015-12-27 22:27:39 +00:00
|
|
|
|
|
|
|
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()
|
2015-12-27 22:27:39 +00:00
|
|
|
if stats.BranchPageN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.BranchOverflowN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafPageN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafOverflowN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.KeyN != 1 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected KeyN: %d", stats.KeyN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.Depth != 1 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected Depth: %d", stats.Depth)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.BranchInuse != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafInuse != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
if os.Getpagesize() == 4096 {
|
2015-12-27 22:27:39 +00:00
|
|
|
if stats.BranchAlloc != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafAlloc != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if stats.BucketN != 1 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BucketN: %d", stats.BucketN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.InlineBucketN != 1 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} 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
|
|
|
}
|
2015-12-27 22:27:39 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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) {
|
2015-12-27 22:27:39 +00:00
|
|
|
db := MustOpenDB()
|
|
|
|
defer db.MustClose()
|
2014-05-12 18:22:45 +00:00
|
|
|
|
2015-12-27 22:27:39 +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.
|
2015-12-27 22:27:39 +00:00
|
|
|
if _, err := tx.CreateBucket([]byte("whozawhats")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
db.MustCheck()
|
2015-12-27 22:27:39 +00:00
|
|
|
|
|
|
|
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()
|
2015-12-27 22:27:39 +00:00
|
|
|
if stats.BranchPageN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.BranchOverflowN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafPageN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafOverflowN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.KeyN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected KeyN: %d", stats.KeyN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.Depth != 1 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected Depth: %d", stats.Depth)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.BranchInuse != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafInuse != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
if os.Getpagesize() == 4096 {
|
2015-12-27 22:27:39 +00:00
|
|
|
if stats.BranchAlloc != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafAlloc != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if stats.BucketN != 1 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BucketN: %d", stats.BucketN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.InlineBucketN != 1 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} 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
|
|
|
}
|
2015-12-27 22:27:39 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-05-12 18:22:45 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 18:16:38 +00:00
|
|
|
// Ensure a bucket can calculate stats.
|
|
|
|
func TestBucket_Stats_Nested(t *testing.T) {
|
2015-12-27 22:27:39 +00:00
|
|
|
db := MustOpenDB()
|
|
|
|
defer db.MustClose()
|
2014-05-14 18:06:31 +00:00
|
|
|
|
2015-12-27 22:27:39 +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"))
|
2015-12-27 22:27:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
|
|
}
|
2015-12-27 22:27:39 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
bar, err := b.CreateBucket([]byte("bar"))
|
2015-12-27 22:27:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
|
|
}
|
2015-12-27 22:27:39 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
baz, err := bar.CreateBucket([]byte("baz"))
|
2015-12-27 22:27:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
|
|
}
|
2015-12-27 22:27:39 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-05-14 18:06:31 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
db.MustCheck()
|
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
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()
|
2015-12-27 22:27:39 +00:00
|
|
|
if stats.BranchPageN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.BranchOverflowN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafPageN != 2 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafOverflowN != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.KeyN != 122 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected KeyN: %d", stats.KeyN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.Depth != 3 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected Depth: %d", stats.Depth)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.BranchInuse != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
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
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
if stats.LeafInuse != foo+bar+baz {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
if os.Getpagesize() == 4096 {
|
2015-12-27 22:27:39 +00:00
|
|
|
if stats.BranchAlloc != 0 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.LeafAlloc != 8192 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
2015-12-27 22:27:39 +00:00
|
|
|
|
|
|
|
if stats.BucketN != 3 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected BucketN: %d", stats.BucketN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.InlineBucketN != 1 {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN)
|
2015-12-27 22:27:39 +00:00
|
|
|
} else if stats.InlineBucketInuse != baz {
|
2016-01-08 08:31:45 +00:00
|
|
|
t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse)
|
2015-12-27 22:27:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-05-09 18:16:38 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
// Ensure a large bucket can calculate stats.
|
2014-04-22 21:11:49 +00:00
|
|
|
func TestBucket_Stats_Large(t *testing.T) {
|
2014-04-07 22:24:51 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
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++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
// 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++ {
|
2015-12-27 22:27:39 +00:00
|
|
|
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++
|
2014-04-22 20:29:37 +00:00
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
2015-12-27 22:27:39 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
db.MustCheck()
|
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
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 {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
|
|
}
|
2015-12-27 22:27:39 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 03:25:37 +00:00
|
|
|
// Ensure that a bucket can write random keys and values across multiple transactions.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_Put_Single(t *testing.T) {
|
2014-02-27 17:04:49 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
2014-02-23 06:08:30 +00:00
|
|
|
index := 0
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
|
|
|
2015-12-27 22:27:39 +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 {
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
|
|
|
|
// Verify all key/values so far.
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
2014-03-21 15:46:03 +00:00
|
|
|
|
2014-02-23 06:08:30 +00:00
|
|
|
index++
|
|
|
|
return true
|
2015-12-27 22:27:39 +00:00
|
|
|
}, 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.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_Put_Multiple(t *testing.T) {
|
2014-02-27 17:04:49 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
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.
|
2015-12-27 22:27:39 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := b.Put(item.Key, item.Value); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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.
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-02-23 06:08:30 +00:00
|
|
|
return true
|
2015-12-27 22:27:39 +00:00
|
|
|
}, 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.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestBucket_Delete_Quick(t *testing.T) {
|
2014-02-27 17:04:49 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
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.
|
2015-12-27 22:27:39 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := b.Put(item.Key, item.Value); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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 {
|
2015-12-27 22:27:39 +00:00
|
|
|
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)
|
2015-12-27 22:27:39 +00:00
|
|
|
}); 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.
|
2015-12-27 22:27:39 +00:00
|
|
|
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))
|
2014-05-19 18:08:33 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-02-23 06:08:30 +00:00
|
|
|
return true
|
2015-12-27 22:27:39 +00:00
|
|
|
}, qconfig()); err != nil {
|
2014-02-23 06:08:30 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleBucket_Put() {
|
|
|
|
// Open the database.
|
2015-12-27 22:27:39 +00:00
|
|
|
db, err := bolt.Open(tempfile(), 0666, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
defer os.Remove(db.Path())
|
|
|
|
|
|
|
|
// Start a write transaction.
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := db.Update(func(tx *bolt.Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
// Create a bucket.
|
2015-12-27 22:27:39 +00:00
|
|
|
b, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
|
|
|
|
// Set the value "bar" for the key "foo".
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
|
|
|
|
// Read value back in a different read-only transaction.
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := db.View(func(tx *bolt.Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
|
2014-04-25 21:38:42 +00:00
|
|
|
fmt.Printf("The value of 'foo' is: %s\n", value)
|
2014-04-07 22:24:51 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close database to release file lock.
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// The value of 'foo' is: bar
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleBucket_Delete() {
|
|
|
|
// Open the database.
|
2015-12-27 22:27:39 +00:00
|
|
|
db, err := bolt.Open(tempfile(), 0666, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
defer os.Remove(db.Path())
|
|
|
|
|
|
|
|
// Start a write transaction.
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := db.Update(func(tx *bolt.Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
// Create a bucket.
|
2015-12-27 22:27:39 +00:00
|
|
|
b, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
|
|
|
|
// Set the value "bar" for the key "foo".
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
|
|
|
|
// Retrieve the key back from the database and verify it.
|
|
|
|
value := b.Get([]byte("foo"))
|
2014-04-25 21:38:42 +00:00
|
|
|
fmt.Printf("The value of 'foo' was: %s\n", value)
|
2015-12-27 22:27:39 +00:00
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
|
|
|
|
// Delete the key in a different write transaction.
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := db.Update(func(tx *bolt.Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
return tx.Bucket([]byte("widgets")).Delete([]byte("foo"))
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
|
|
|
|
// Retrieve the key again.
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := db.View(func(tx *bolt.Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
|
|
|
|
if value == nil {
|
|
|
|
fmt.Printf("The value of 'foo' is now: nil\n")
|
|
|
|
}
|
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close database to release file lock.
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// The value of 'foo' was: bar
|
|
|
|
// The value of 'foo' is now: nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleBucket_ForEach() {
|
|
|
|
// Open the database.
|
2015-12-27 22:27:39 +00:00
|
|
|
db, err := bolt.Open(tempfile(), 0666, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
defer os.Remove(db.Path())
|
|
|
|
|
|
|
|
// Insert data into a bucket.
|
2015-12-27 22:27:39 +00:00
|
|
|
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
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
|
|
|
|
// Iterate over items in sorted key order.
|
2015-12-27 22:27:39 +00:00
|
|
|
if err := b.ForEach(func(k, v []byte) error {
|
2014-04-25 21:38:42 +00:00
|
|
|
fmt.Printf("A %s is %s.\n", k, v)
|
2014-04-07 22:24:51 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
return nil
|
2015-12-27 22:27:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close database to release file lock.
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// A cat is lame.
|
|
|
|
// A dog is fun.
|
|
|
|
// A liger is awesome.
|
2014-02-23 06:08:30 +00:00
|
|
|
}
|