2014-07-26 21:11:47 +00:00
|
|
|
package bolt_test
|
2014-01-12 05:51:01 +00:00
|
|
|
|
|
|
|
import (
|
2014-03-24 14:31:15 +00:00
|
|
|
"errors"
|
2014-04-02 21:36:53 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-01-12 05:51:01 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-04-02 21:36:53 +00:00
|
|
|
"regexp"
|
2014-06-21 20:44:22 +00:00
|
|
|
"runtime"
|
2014-06-03 19:21:28 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2014-01-12 05:51:01 +00:00
|
|
|
"testing"
|
2014-04-02 21:36:53 +00:00
|
|
|
"time"
|
2014-01-12 05:51:01 +00:00
|
|
|
|
2014-07-26 21:11:47 +00:00
|
|
|
"github.com/boltdb/bolt"
|
2014-01-12 05:51:01 +00:00
|
|
|
)
|
|
|
|
|
2014-04-02 21:36:53 +00:00
|
|
|
var statsFlag = flag.Bool("stats", false, "show performance stats")
|
|
|
|
|
2014-02-26 23:32:05 +00:00
|
|
|
// Ensure that opening a database with a bad path returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestOpen_BadPath(t *testing.T) {
|
2014-07-26 21:11:47 +00:00
|
|
|
db, err := bolt.Open("", 0666, nil)
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, err != nil, "err: %s", err)
|
|
|
|
assert(t, db == nil, "")
|
2014-02-26 23:32:05 +00:00
|
|
|
}
|
|
|
|
|
2014-01-12 22:30:09 +00:00
|
|
|
// Ensure that a database can be opened without error.
|
2014-06-21 20:44:22 +00:00
|
|
|
func TestOpen(t *testing.T) {
|
2014-07-26 05:14:17 +00:00
|
|
|
path := tempfile()
|
|
|
|
defer os.Remove(path)
|
2014-07-26 21:11:47 +00:00
|
|
|
db, err := bolt.Open(path, 0666, nil)
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, db != nil, "")
|
|
|
|
ok(t, err)
|
|
|
|
equals(t, db.Path(), path)
|
|
|
|
ok(t, db.Close())
|
2014-01-12 22:30:09 +00:00
|
|
|
}
|
|
|
|
|
2014-06-21 20:44:22 +00:00
|
|
|
// Ensure that opening an already open database file will timeout.
|
|
|
|
func TestOpen_Timeout(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("timeout not supported on windows")
|
|
|
|
}
|
|
|
|
|
2014-07-26 05:14:17 +00:00
|
|
|
path := tempfile()
|
|
|
|
defer os.Remove(path)
|
2014-06-21 20:44:22 +00:00
|
|
|
|
2014-07-26 05:14:17 +00:00
|
|
|
// Open a data file.
|
2014-07-26 21:11:47 +00:00
|
|
|
db0, err := bolt.Open(path, 0666, nil)
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, db0 != nil, "")
|
|
|
|
ok(t, err)
|
2014-07-26 05:14:17 +00:00
|
|
|
|
|
|
|
// Attempt to open the database again.
|
|
|
|
start := time.Now()
|
2014-07-26 21:11:47 +00:00
|
|
|
db1, err := bolt.Open(path, 0666, &bolt.Options{Timeout: 100 * time.Millisecond})
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, db1 == nil, "")
|
|
|
|
equals(t, bolt.ErrTimeout, err)
|
|
|
|
assert(t, time.Since(start) > 100*time.Millisecond, "")
|
2014-07-26 05:14:17 +00:00
|
|
|
|
|
|
|
db0.Close()
|
2014-06-21 20:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that opening an already open database file will wait until its closed.
|
|
|
|
func TestOpen_Wait(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("timeout not supported on windows")
|
|
|
|
}
|
|
|
|
|
2014-07-26 05:14:17 +00:00
|
|
|
path := tempfile()
|
|
|
|
defer os.Remove(path)
|
2014-06-21 20:44:22 +00:00
|
|
|
|
2014-07-26 05:14:17 +00:00
|
|
|
// Open a data file.
|
2014-07-26 21:11:47 +00:00
|
|
|
db0, err := bolt.Open(path, 0666, nil)
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, db0 != nil, "")
|
|
|
|
ok(t, err)
|
2014-07-26 05:14:17 +00:00
|
|
|
|
|
|
|
// Close it in just a bit.
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() { db0.Close() })
|
|
|
|
|
|
|
|
// Attempt to open the database again.
|
|
|
|
start := time.Now()
|
2014-07-26 21:11:47 +00:00
|
|
|
db1, err := bolt.Open(path, 0666, &bolt.Options{Timeout: 200 * time.Millisecond})
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, db1 != nil, "")
|
|
|
|
ok(t, err)
|
|
|
|
assert(t, time.Since(start) > 100*time.Millisecond, "")
|
2014-06-21 20:44:22 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 16:29:27 +00:00
|
|
|
// Ensure that opening a database does not increase its size.
|
|
|
|
// https://github.com/boltdb/bolt/issues/291
|
|
|
|
func TestOpen_Size(t *testing.T) {
|
|
|
|
// Open a data file.
|
|
|
|
db := NewTestDB()
|
|
|
|
path := db.Path()
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Insert until we get above the minimum 4MB size.
|
2015-01-28 16:52:08 +00:00
|
|
|
ok(t, db.Update(func(tx *bolt.Tx) error {
|
2015-01-28 16:29:27 +00:00
|
|
|
b, _ := tx.CreateBucketIfNotExists([]byte("data"))
|
|
|
|
for i := 0; i < 10000; i++ {
|
2015-01-28 16:52:08 +00:00
|
|
|
ok(t, b.Put([]byte(fmt.Sprintf("%04d", i)), make([]byte, 1000)))
|
2015-01-28 16:29:27 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-01-28 16:52:08 +00:00
|
|
|
}))
|
2015-01-28 16:29:27 +00:00
|
|
|
|
|
|
|
// Close database and grab the size.
|
|
|
|
db.DB.Close()
|
|
|
|
sz := fileSize(path)
|
2015-01-28 16:52:08 +00:00
|
|
|
if sz == 0 {
|
|
|
|
t.Fatalf("unexpected new file size: %d", sz)
|
|
|
|
}
|
2015-01-28 16:29:27 +00:00
|
|
|
|
|
|
|
// Reopen database, update, and check size again.
|
2015-01-28 16:52:08 +00:00
|
|
|
db0, err := bolt.Open(path, 0666, nil)
|
|
|
|
ok(t, err)
|
|
|
|
ok(t, db0.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("data")).Put([]byte{0}, []byte{0}) }))
|
|
|
|
ok(t, db0.Close())
|
2015-01-28 16:29:27 +00:00
|
|
|
newSz := fileSize(path)
|
2015-01-28 16:52:08 +00:00
|
|
|
if newSz == 0 {
|
|
|
|
t.Fatalf("unexpected new file size: %d", newSz)
|
|
|
|
}
|
2015-01-28 16:29:27 +00:00
|
|
|
|
|
|
|
// Compare the original size with the new size.
|
|
|
|
if sz != newSz {
|
|
|
|
t.Fatalf("unexpected file growth: %d => %d", sz, newSz)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 14:52:13 +00:00
|
|
|
// Ensure that a re-opened database is consistent.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestOpen_Check(t *testing.T) {
|
2014-07-26 05:14:17 +00:00
|
|
|
path := tempfile()
|
|
|
|
defer os.Remove(path)
|
2014-03-31 14:52:13 +00:00
|
|
|
|
2014-07-26 21:11:47 +00:00
|
|
|
db, err := bolt.Open(path, 0666, nil)
|
2014-07-26 23:17:03 +00:00
|
|
|
ok(t, err)
|
|
|
|
ok(t, db.View(func(tx *bolt.Tx) error { return <-tx.Check() }))
|
2014-07-26 05:14:17 +00:00
|
|
|
db.Close()
|
|
|
|
|
2014-07-26 21:11:47 +00:00
|
|
|
db, err = bolt.Open(path, 0666, nil)
|
2014-07-26 23:17:03 +00:00
|
|
|
ok(t, err)
|
|
|
|
ok(t, db.View(func(tx *bolt.Tx) error { return <-tx.Check() }))
|
2014-07-26 05:14:17 +00:00
|
|
|
db.Close()
|
2014-03-31 14:52:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-23 21:40:08 +00:00
|
|
|
// Ensure that the database returns an error if the file handle cannot be open.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_Open_FileError(t *testing.T) {
|
2014-07-26 05:14:17 +00:00
|
|
|
path := tempfile()
|
|
|
|
defer os.Remove(path)
|
|
|
|
|
2014-07-26 21:11:47 +00:00
|
|
|
_, err := bolt.Open(path+"/youre-not-my-real-parent", 0666, nil)
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, err.(*os.PathError) != nil, "")
|
|
|
|
equals(t, path+"/youre-not-my-real-parent", err.(*os.PathError).Path)
|
|
|
|
equals(t, "open", err.(*os.PathError).Op)
|
2014-03-23 21:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that write errors to the meta file handler during initialization are returned.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_Open_MetaInitWriteError(t *testing.T) {
|
2014-03-31 17:22:27 +00:00
|
|
|
t.Skip("pending")
|
2014-03-23 21:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a database that is too small returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_Open_FileTooSmall(t *testing.T) {
|
2014-07-26 05:14:17 +00:00
|
|
|
path := tempfile()
|
|
|
|
defer os.Remove(path)
|
2014-03-24 14:31:15 +00:00
|
|
|
|
2014-07-26 21:11:47 +00:00
|
|
|
db, err := bolt.Open(path, 0666, nil)
|
2014-07-26 23:17:03 +00:00
|
|
|
ok(t, err)
|
2014-07-26 05:14:17 +00:00
|
|
|
db.Close()
|
2014-03-23 21:40:08 +00:00
|
|
|
|
2014-07-26 05:14:17 +00:00
|
|
|
// corrupt the database
|
2014-07-26 23:17:03 +00:00
|
|
|
ok(t, os.Truncate(path, int64(os.Getpagesize())))
|
2014-07-26 05:14:17 +00:00
|
|
|
|
2014-07-26 21:11:47 +00:00
|
|
|
db, err = bolt.Open(path, 0666, nil)
|
2014-07-26 23:17:03 +00:00
|
|
|
equals(t, errors.New("file size too small"), err)
|
2014-03-23 21:40:08 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 21:11:47 +00:00
|
|
|
// TODO(benbjohnson): Test corruption at every byte of the first two pages.
|
|
|
|
|
2014-01-13 17:35:04 +00:00
|
|
|
// Ensure that a database cannot open a transaction when it's not open.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_Begin_DatabaseNotOpen(t *testing.T) {
|
2014-07-26 21:11:47 +00:00
|
|
|
var db bolt.DB
|
2014-03-31 17:22:27 +00:00
|
|
|
tx, err := db.Begin(false)
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, tx == nil, "")
|
|
|
|
equals(t, err, bolt.ErrDatabaseNotOpen)
|
2014-01-13 17:35:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 03:25:37 +00:00
|
|
|
// Ensure that a read-write transaction can be retrieved.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_BeginRW(t *testing.T) {
|
2014-07-26 20:44:04 +00:00
|
|
|
db := NewTestDB()
|
|
|
|
defer db.Close()
|
|
|
|
tx, err := db.Begin(true)
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, tx != nil, "")
|
|
|
|
ok(t, err)
|
|
|
|
assert(t, tx.DB() == db.DB, "")
|
|
|
|
equals(t, tx.Writable(), true)
|
|
|
|
ok(t, tx.Commit())
|
2014-03-09 03:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that opening a transaction while the DB is closed returns an error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_BeginRW_Closed(t *testing.T) {
|
2014-07-26 21:11:47 +00:00
|
|
|
var db bolt.DB
|
2014-03-31 17:22:27 +00:00
|
|
|
tx, err := db.Begin(true)
|
2014-07-26 23:17:03 +00:00
|
|
|
equals(t, err, bolt.ErrDatabaseNotOpen)
|
|
|
|
assert(t, tx == nil, "")
|
2014-03-09 03:25:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 21:54:45 +00:00
|
|
|
// Ensure a database can provide a transactional block.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_Update(t *testing.T) {
|
2014-07-26 20:44:04 +00:00
|
|
|
db := NewTestDB()
|
|
|
|
defer db.Close()
|
2014-07-26 21:11:47 +00:00
|
|
|
err := db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 20:44:04 +00:00
|
|
|
tx.CreateBucket([]byte("widgets"))
|
|
|
|
b := tx.Bucket([]byte("widgets"))
|
|
|
|
b.Put([]byte("foo"), []byte("bar"))
|
|
|
|
b.Put([]byte("baz"), []byte("bat"))
|
|
|
|
b.Delete([]byte("foo"))
|
|
|
|
return nil
|
|
|
|
})
|
2014-07-26 23:17:03 +00:00
|
|
|
ok(t, err)
|
2014-07-26 21:11:47 +00:00
|
|
|
err = db.View(func(tx *bolt.Tx) error {
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, tx.Bucket([]byte("widgets")).Get([]byte("foo")) == nil, "")
|
|
|
|
equals(t, []byte("bat"), tx.Bucket([]byte("widgets")).Get([]byte("baz")))
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2014-02-15 21:54:45 +00:00
|
|
|
})
|
2014-07-26 23:17:03 +00:00
|
|
|
ok(t, err)
|
2014-02-15 21:54:45 +00:00
|
|
|
}
|
|
|
|
|
2014-02-16 06:38:03 +00:00
|
|
|
// Ensure a closed database returns an error while running a transaction block
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_Update_Closed(t *testing.T) {
|
2014-07-26 21:11:47 +00:00
|
|
|
var db bolt.DB
|
|
|
|
err := db.Update(func(tx *bolt.Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
tx.CreateBucket([]byte("widgets"))
|
2014-03-31 17:22:27 +00:00
|
|
|
return nil
|
2014-02-16 06:38:03 +00:00
|
|
|
})
|
2014-07-26 23:17:03 +00:00
|
|
|
equals(t, err, bolt.ErrDatabaseNotOpen)
|
2014-02-16 06:38:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-23 16:34:53 +00:00
|
|
|
// Ensure a panic occurs while trying to commit a managed transaction.
|
2014-07-26 23:17:03 +00:00
|
|
|
func TestDB_Update_ManualCommit(t *testing.T) {
|
|
|
|
db := NewTestDB()
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
var ok bool
|
2014-07-26 21:11:47 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 23:17:03 +00:00
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
tx.Commit()
|
|
|
|
}()
|
2014-03-31 17:22:27 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, ok, "expected panic")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a panic occurs while trying to rollback a managed transaction.
|
|
|
|
func TestDB_Update_ManualRollback(t *testing.T) {
|
|
|
|
db := NewTestDB()
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
tx.Rollback()
|
|
|
|
}()
|
2014-03-31 17:22:27 +00:00
|
|
|
return nil
|
2014-03-23 16:34:53 +00:00
|
|
|
})
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, ok, "expected panic")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a panic occurs while trying to commit a managed transaction.
|
|
|
|
func TestDB_View_ManualCommit(t *testing.T) {
|
|
|
|
db := NewTestDB()
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
tx.Commit()
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
assert(t, ok, "expected panic")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a panic occurs while trying to rollback a managed transaction.
|
|
|
|
func TestDB_View_ManualRollback(t *testing.T) {
|
|
|
|
db := NewTestDB()
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
tx.Rollback()
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
assert(t, ok, "expected panic")
|
2014-03-23 16:34:53 +00:00
|
|
|
}
|
|
|
|
|
2014-07-11 15:54:10 +00:00
|
|
|
// Ensure a write transaction that panics does not hold open locks.
|
|
|
|
func TestDB_Update_Panic(t *testing.T) {
|
2014-07-26 20:44:04 +00:00
|
|
|
db := NewTestDB()
|
|
|
|
defer db.Close()
|
2014-07-11 15:54:10 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2014-07-26 21:11:47 +00:00
|
|
|
t.Log("recover: update", r)
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
|
|
|
}()
|
2014-07-26 21:11:47 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 20:44:04 +00:00
|
|
|
tx.CreateBucket([]byte("widgets"))
|
|
|
|
panic("omg")
|
2014-07-11 15:54:10 +00:00
|
|
|
})
|
2014-07-26 20:44:04 +00:00
|
|
|
}()
|
2014-07-11 15:54:10 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
// Verify we can update again.
|
2014-07-26 21:11:47 +00:00
|
|
|
err := db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 20:44:04 +00:00
|
|
|
_, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
return err
|
|
|
|
})
|
2014-07-26 23:17:03 +00:00
|
|
|
ok(t, err)
|
2014-07-26 20:44:04 +00:00
|
|
|
|
|
|
|
// Verify that our change persisted.
|
2014-07-26 21:11:47 +00:00
|
|
|
err = db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, tx.Bucket([]byte("widgets")) != nil, "")
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2014-07-11 15:54:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
// Ensure a database can return an error through a read-only transactional block.
|
|
|
|
func TestDB_View_Error(t *testing.T) {
|
2014-07-26 20:44:04 +00:00
|
|
|
db := NewTestDB()
|
|
|
|
defer db.Close()
|
2014-07-26 21:11:47 +00:00
|
|
|
err := db.View(func(tx *bolt.Tx) error {
|
2014-07-26 20:44:04 +00:00
|
|
|
return errors.New("xxx")
|
2014-04-07 22:24:51 +00:00
|
|
|
})
|
2014-07-26 23:17:03 +00:00
|
|
|
equals(t, errors.New("xxx"), err)
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
2014-07-11 15:54:10 +00:00
|
|
|
// Ensure a read transaction that panics does not hold open locks.
|
|
|
|
func TestDB_View_Panic(t *testing.T) {
|
2014-07-26 20:44:04 +00:00
|
|
|
db := NewTestDB()
|
|
|
|
defer db.Close()
|
2014-07-26 21:11:47 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 20:44:04 +00:00
|
|
|
tx.CreateBucket([]byte("widgets"))
|
|
|
|
return nil
|
|
|
|
})
|
2014-07-11 15:54:10 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2014-07-26 21:11:47 +00:00
|
|
|
t.Log("recover: view", r)
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
2014-07-11 15:54:10 +00:00
|
|
|
}()
|
2014-07-26 21:11:47 +00:00
|
|
|
db.View(func(tx *bolt.Tx) error {
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, tx.Bucket([]byte("widgets")) != nil, "")
|
2014-07-26 20:44:04 +00:00
|
|
|
panic("omg")
|
2014-07-11 15:54:10 +00:00
|
|
|
})
|
2014-07-26 20:44:04 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Verify that we can still use read transactions.
|
2014-07-26 21:11:47 +00:00
|
|
|
db.View(func(tx *bolt.Tx) error {
|
2014-07-26 23:17:03 +00:00
|
|
|
assert(t, tx.Bucket([]byte("widgets")) != nil, "")
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2014-07-11 15:54:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-02-09 22:52:19 +00:00
|
|
|
// Ensure that an error is returned when a database write fails.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_Commit_WriteFail(t *testing.T) {
|
2014-02-09 22:52:19 +00:00
|
|
|
t.Skip("pending") // TODO(benbjohnson)
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
// Ensure that DB stats can be returned.
|
|
|
|
func TestDB_Stats(t *testing.T) {
|
2014-07-26 20:44:04 +00:00
|
|
|
db := NewTestDB()
|
|
|
|
defer db.Close()
|
2014-07-26 21:11:47 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 20:44:04 +00:00
|
|
|
_, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
return err
|
2014-04-07 22:24:51 +00:00
|
|
|
})
|
2014-07-26 20:44:04 +00:00
|
|
|
stats := db.Stats()
|
2014-07-26 23:17:03 +00:00
|
|
|
equals(t, 2, stats.TxStats.PageCount)
|
|
|
|
equals(t, 0, stats.FreePageN)
|
|
|
|
equals(t, 2, stats.PendingPageN)
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 13:25:00 +00:00
|
|
|
// Ensure that database pages are in expected order and type.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_Consistency(t *testing.T) {
|
2014-07-26 20:44:04 +00:00
|
|
|
db := NewTestDB()
|
|
|
|
defer db.Close()
|
2014-07-26 21:11:47 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 20:44:04 +00:00
|
|
|
_, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
return err
|
|
|
|
})
|
2014-03-25 13:25:00 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
2014-07-26 21:11:47 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 23:17:03 +00:00
|
|
|
ok(t, tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")))
|
2014-03-25 13:25:00 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-07-26 20:44:04 +00:00
|
|
|
}
|
2014-07-26 21:11:47 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2014-07-26 23:17:03 +00:00
|
|
|
p, _ := tx.Page(0)
|
|
|
|
assert(t, p != nil, "")
|
|
|
|
equals(t, "meta", p.Type)
|
|
|
|
|
|
|
|
p, _ = tx.Page(1)
|
|
|
|
assert(t, p != nil, "")
|
|
|
|
equals(t, "meta", p.Type)
|
|
|
|
|
|
|
|
p, _ = tx.Page(2)
|
|
|
|
assert(t, p != nil, "")
|
|
|
|
equals(t, "free", p.Type)
|
|
|
|
|
|
|
|
p, _ = tx.Page(3)
|
|
|
|
assert(t, p != nil, "")
|
|
|
|
equals(t, "free", p.Type)
|
|
|
|
|
|
|
|
p, _ = tx.Page(4)
|
|
|
|
assert(t, p != nil, "")
|
|
|
|
equals(t, "leaf", p.Type)
|
|
|
|
|
|
|
|
p, _ = tx.Page(5)
|
|
|
|
assert(t, p != nil, "")
|
|
|
|
equals(t, "freelist", p.Type)
|
|
|
|
|
|
|
|
p, _ = tx.Page(6)
|
|
|
|
assert(t, p == nil, "")
|
2014-07-26 20:44:04 +00:00
|
|
|
return nil
|
2014-03-25 13:25:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
// Ensure that DB stats can be substracted from one another.
|
|
|
|
func TestDBStats_Sub(t *testing.T) {
|
2014-07-26 21:11:47 +00:00
|
|
|
var a, b bolt.Stats
|
2014-04-07 22:24:51 +00:00
|
|
|
a.TxStats.PageCount = 3
|
2014-06-27 17:38:06 +00:00
|
|
|
a.FreePageN = 4
|
2014-04-07 22:24:51 +00:00
|
|
|
b.TxStats.PageCount = 10
|
2014-06-27 17:38:06 +00:00
|
|
|
b.FreePageN = 14
|
2014-04-07 22:24:51 +00:00
|
|
|
diff := b.Sub(&a)
|
2014-07-26 23:17:03 +00:00
|
|
|
equals(t, 7, diff.TxStats.PageCount)
|
2014-06-27 17:38:06 +00:00
|
|
|
// free page stats are copied from the receiver and not subtracted
|
2014-07-26 23:17:03 +00:00
|
|
|
equals(t, 14, diff.FreePageN)
|
2014-02-28 22:13:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
func ExampleDB_Update() {
|
|
|
|
// Open the database.
|
2014-07-26 21:11:47 +00:00
|
|
|
db, _ := bolt.Open(tempfile(), 0666, nil)
|
2014-04-07 22:24:51 +00:00
|
|
|
defer os.Remove(db.Path())
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Execute several commands within a write transaction.
|
2014-07-26 21:11:47 +00:00
|
|
|
err := db.Update(func(tx *bolt.Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
b, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
if err != nil {
|
2014-04-07 22:24:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// If our transactional block didn't return an error then our data is saved.
|
|
|
|
if err == nil {
|
2014-07-26 21:11:47 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// The value of 'foo' is: bar
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleDB_View() {
|
|
|
|
// Open the database.
|
2014-07-26 21:11:47 +00:00
|
|
|
db, _ := bolt.Open(tempfile(), 0666, nil)
|
2014-04-07 22:24:51 +00:00
|
|
|
defer os.Remove(db.Path())
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Insert data into a bucket.
|
2014-07-26 21:11:47 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
tx.CreateBucket([]byte("people"))
|
|
|
|
b := tx.Bucket([]byte("people"))
|
|
|
|
b.Put([]byte("john"), []byte("doe"))
|
|
|
|
b.Put([]byte("susy"), []byte("que"))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Access data from within a read-only transactional block.
|
2014-07-26 21:11:47 +00:00
|
|
|
db.View(func(tx *bolt.Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
v := tx.Bucket([]byte("people")).Get([]byte("john"))
|
2014-04-25 21:38:42 +00:00
|
|
|
fmt.Printf("John's last name is %s.\n", v)
|
2014-04-07 22:24:51 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// John's last name is doe.
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleDB_Begin_ReadOnly() {
|
|
|
|
// Open the database.
|
2014-07-26 21:11:47 +00:00
|
|
|
db, _ := bolt.Open(tempfile(), 0666, nil)
|
2014-04-07 22:24:51 +00:00
|
|
|
defer os.Remove(db.Path())
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Create a bucket.
|
2014-07-26 21:11:47 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
_, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
return err
|
2014-04-07 22:24:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Create several keys in a transaction.
|
|
|
|
tx, _ := db.Begin(true)
|
|
|
|
b := tx.Bucket([]byte("widgets"))
|
|
|
|
b.Put([]byte("john"), []byte("blue"))
|
|
|
|
b.Put([]byte("abby"), []byte("red"))
|
|
|
|
b.Put([]byte("zephyr"), []byte("purple"))
|
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
// Iterate over the values in sorted key order.
|
|
|
|
tx, _ = db.Begin(false)
|
|
|
|
c := tx.Bucket([]byte("widgets")).Cursor()
|
|
|
|
for k, v := c.First(); k != nil; k, v = c.Next() {
|
2014-04-25 21:38:42 +00:00
|
|
|
fmt.Printf("%s likes %s\n", k, v)
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// abby likes red
|
|
|
|
// john likes blue
|
|
|
|
// zephyr likes purple
|
|
|
|
}
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
// TestDB represents a wrapper around a Bolt DB to handle temporary file
|
|
|
|
// creation and automatic cleanup on close.
|
|
|
|
type TestDB struct {
|
2014-07-26 21:11:47 +00:00
|
|
|
*bolt.DB
|
2014-04-07 22:24:51 +00:00
|
|
|
}
|
2014-01-12 05:51:01 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
// NewTestDB returns a new instance of TestDB.
|
|
|
|
func NewTestDB() *TestDB {
|
2014-07-26 21:11:47 +00:00
|
|
|
db, err := bolt.Open(tempfile(), 0666, nil)
|
2014-07-26 05:14:17 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("cannot open db: " + err.Error())
|
|
|
|
}
|
2014-07-26 20:44:04 +00:00
|
|
|
return &TestDB{db}
|
|
|
|
}
|
2014-04-02 21:36:53 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
// Close closes the database and deletes the underlying file.
|
|
|
|
func (db *TestDB) Close() {
|
2014-07-26 05:14:17 +00:00
|
|
|
// Log statistics.
|
|
|
|
if *statsFlag {
|
2014-07-26 20:44:04 +00:00
|
|
|
db.PrintStats()
|
2014-07-26 05:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check database consistency after every test.
|
2014-07-26 20:44:04 +00:00
|
|
|
db.MustCheck()
|
|
|
|
|
|
|
|
// Close database and remove file.
|
|
|
|
defer os.Remove(db.Path())
|
|
|
|
db.DB.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintStats prints the database stats
|
|
|
|
func (db *TestDB) PrintStats() {
|
|
|
|
var stats = db.Stats()
|
|
|
|
fmt.Printf("[db] %-20s %-20s %-20s\n",
|
|
|
|
fmt.Sprintf("pg(%d/%d)", stats.TxStats.PageCount, stats.TxStats.PageAlloc),
|
|
|
|
fmt.Sprintf("cur(%d)", stats.TxStats.CursorCount),
|
|
|
|
fmt.Sprintf("node(%d/%d)", stats.TxStats.NodeCount, stats.TxStats.NodeDeref),
|
|
|
|
)
|
|
|
|
fmt.Printf(" %-20s %-20s %-20s\n",
|
|
|
|
fmt.Sprintf("rebal(%d/%v)", stats.TxStats.Rebalance, truncDuration(stats.TxStats.RebalanceTime)),
|
|
|
|
fmt.Sprintf("spill(%d/%v)", stats.TxStats.Spill, truncDuration(stats.TxStats.SpillTime)),
|
|
|
|
fmt.Sprintf("w(%d/%v)", stats.TxStats.Write, truncDuration(stats.TxStats.WriteTime)),
|
|
|
|
)
|
2014-01-13 17:35:04 +00:00
|
|
|
}
|
2014-02-16 04:50:34 +00:00
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
// MustCheck runs a consistency check on the database and panics if any errors are found.
|
|
|
|
func (db *TestDB) MustCheck() {
|
2014-07-26 21:11:47 +00:00
|
|
|
db.View(func(tx *bolt.Tx) error {
|
2014-07-23 21:08:59 +00:00
|
|
|
// Collect all the errors.
|
|
|
|
var errors []error
|
|
|
|
for err := range tx.Check() {
|
|
|
|
errors = append(errors, err)
|
|
|
|
if len(errors) > 10 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If errors occurred, copy the DB and print the errors.
|
|
|
|
if len(errors) > 0 {
|
|
|
|
var path = tempfile()
|
|
|
|
tx.CopyFile(path, 0600)
|
|
|
|
|
|
|
|
// Print errors.
|
|
|
|
fmt.Print("\n\n")
|
|
|
|
fmt.Printf("consistency check failed (%d errors)\n", len(errors))
|
|
|
|
for _, err := range errors {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("db saved to:")
|
|
|
|
fmt.Println(path)
|
|
|
|
fmt.Print("\n\n")
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-05-27 17:31:55 +00:00
|
|
|
})
|
2014-03-28 06:07:05 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 20:44:04 +00:00
|
|
|
// CopyTempFile copies a database to a temporary file.
|
|
|
|
func (db *TestDB) CopyTempFile() {
|
|
|
|
path := tempfile()
|
2014-07-26 21:11:47 +00:00
|
|
|
db.View(func(tx *bolt.Tx) error { return tx.CopyFile(path, 0600) })
|
2014-07-26 20:44:04 +00:00
|
|
|
fmt.Println("db copied to: ", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// tempfile returns a temporary file path.
|
|
|
|
func tempfile() string {
|
|
|
|
f, _ := ioutil.TempFile("", "bolt-")
|
|
|
|
f.Close()
|
|
|
|
os.Remove(f.Name())
|
|
|
|
return f.Name()
|
|
|
|
}
|
|
|
|
|
2014-06-03 19:21:28 +00:00
|
|
|
// mustContainKeys checks that a bucket contains a given set of keys.
|
2014-07-26 21:11:47 +00:00
|
|
|
func mustContainKeys(b *bolt.Bucket, m map[string]string) {
|
2014-06-03 19:21:28 +00:00
|
|
|
found := make(map[string]string)
|
|
|
|
b.ForEach(func(k, _ []byte) error {
|
|
|
|
found[string(k)] = ""
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Check for keys found in bucket that shouldn't be there.
|
|
|
|
var keys []string
|
|
|
|
for k, _ := range found {
|
|
|
|
if _, ok := m[string(k)]; !ok {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(keys) > 0 {
|
|
|
|
sort.Strings(keys)
|
|
|
|
panic(fmt.Sprintf("keys found(%d): %s", len(keys), strings.Join(keys, ",")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for keys not found in bucket that should be there.
|
|
|
|
for k, _ := range m {
|
|
|
|
if _, ok := found[string(k)]; !ok {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(keys) > 0 {
|
|
|
|
sort.Strings(keys)
|
|
|
|
panic(fmt.Sprintf("keys not found(%d): %s", len(keys), strings.Join(keys, ",")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:50:34 +00:00
|
|
|
func trunc(b []byte, length int) []byte {
|
|
|
|
if length < len(b) {
|
|
|
|
return b[:length]
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
2014-04-02 21:36:53 +00:00
|
|
|
|
|
|
|
func truncDuration(d time.Duration) string {
|
|
|
|
return regexp.MustCompile(`^(\d+)(\.\d+)`).ReplaceAllString(d.String(), "$1")
|
|
|
|
}
|
2015-01-28 16:29:27 +00:00
|
|
|
|
|
|
|
func fileSize(path string) int64 {
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return fi.Size()
|
|
|
|
}
|
2015-02-02 15:27:34 +00:00
|
|
|
|
|
|
|
func warn(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) }
|
|
|
|
func warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", v...) }
|