2014-01-12 05:51:01 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
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-01-12 05:51:01 +00:00
|
|
|
"testing"
|
2014-04-02 21:36:53 +00:00
|
|
|
"time"
|
2014-03-24 14:31:15 +00:00
|
|
|
"unsafe"
|
2014-01-12 05:51:01 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
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 a database can be opened without error.
|
|
|
|
func TestOpen(t *testing.T) {
|
|
|
|
f, _ := ioutil.TempFile("", "bolt-")
|
|
|
|
path := f.Name()
|
|
|
|
f.Close()
|
|
|
|
os.Remove(path)
|
|
|
|
defer os.RemoveAll(path)
|
|
|
|
|
|
|
|
db, err := Open(path, 0666)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, db)
|
|
|
|
db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-02-26 23:32:05 +00:00
|
|
|
db, err := Open("/../bad-path", 0666)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, db)
|
|
|
|
}
|
|
|
|
|
2014-01-12 22:30:09 +00:00
|
|
|
// Ensure that a database can be opened without error.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_Open(t *testing.T) {
|
2014-03-31 17:22:27 +00:00
|
|
|
withTempPath(func(path string) {
|
|
|
|
db, err := Open(path, 0666)
|
|
|
|
assert.NotNil(t, db)
|
2014-01-12 05:51:01 +00:00
|
|
|
assert.NoError(t, err)
|
2014-01-12 22:30:09 +00:00
|
|
|
assert.Equal(t, db.Path(), path)
|
2014-03-31 17:22:27 +00:00
|
|
|
assert.NoError(t, db.Close())
|
2014-01-12 22:30:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-03-31 17:22:27 +00:00
|
|
|
withTempPath(func(path string) {
|
|
|
|
db, err := Open(path, 0666)
|
|
|
|
assert.NoError(t, err)
|
2014-03-31 14:52:13 +00:00
|
|
|
assert.NoError(t, db.Check())
|
|
|
|
db.Close()
|
|
|
|
|
2014-03-31 17:22:27 +00:00
|
|
|
db, err = Open(path, 0666)
|
|
|
|
assert.NoError(t, err)
|
2014-03-31 14:52:13 +00:00
|
|
|
assert.NoError(t, db.Check())
|
2014-03-31 17:22:27 +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-03-31 17:22:27 +00:00
|
|
|
withTempPath(func(path string) {
|
|
|
|
_, err := Open(path+"/youre-not-my-real-parent", 0666)
|
2014-03-24 14:31:15 +00:00
|
|
|
if err, _ := err.(*os.PathError); assert.Error(t, err) {
|
|
|
|
assert.Equal(t, path+"/youre-not-my-real-parent", err.Path)
|
|
|
|
assert.Equal(t, "open", err.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-03-31 17:22:27 +00:00
|
|
|
withTempPath(func(path string) {
|
|
|
|
db, err := Open(path, 0666)
|
|
|
|
assert.NoError(t, err)
|
2014-03-24 14:31:15 +00:00
|
|
|
db.Close()
|
|
|
|
|
2014-03-23 21:40:08 +00:00
|
|
|
// corrupt the database
|
2014-03-24 14:31:15 +00:00
|
|
|
assert.NoError(t, os.Truncate(path, int64(os.Getpagesize())))
|
2014-03-23 21:40:08 +00:00
|
|
|
|
2014-03-31 17:22:27 +00:00
|
|
|
db, err = Open(path, 0666)
|
2014-03-24 14:31:15 +00:00
|
|
|
assert.Equal(t, errors.New("file size too small"), err)
|
2014-03-23 21:40:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that corrupt meta0 page errors get returned.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_Open_CorruptMeta0(t *testing.T) {
|
2014-03-31 17:22:27 +00:00
|
|
|
withTempPath(func(path string) {
|
2014-03-23 21:40:08 +00:00
|
|
|
var m meta
|
|
|
|
m.magic = magic
|
|
|
|
m.version = version
|
|
|
|
m.pageSize = 0x8000
|
|
|
|
|
|
|
|
// Create a file with bad magic.
|
|
|
|
b := make([]byte, 0x10000)
|
|
|
|
p0, p1 := (*page)(unsafe.Pointer(&b[0x0000])), (*page)(unsafe.Pointer(&b[0x8000]))
|
|
|
|
p0.meta().magic = 0
|
|
|
|
p0.meta().version = version
|
|
|
|
p1.meta().magic = magic
|
|
|
|
p1.meta().version = version
|
|
|
|
err := ioutil.WriteFile(path, b, 0666)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Open the database.
|
2014-03-31 17:22:27 +00:00
|
|
|
_, err = Open(path, 0666)
|
2014-04-02 22:56:16 +00:00
|
|
|
assert.Equal(t, err, errors.New("meta0 error: invalid database"))
|
2014-03-23 21:40:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-04-02 22:56:16 +00:00
|
|
|
// Ensure that a corrupt meta page checksum causes the open to fail.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_Open_MetaChecksumError(t *testing.T) {
|
2014-04-02 22:56:16 +00:00
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
withTempPath(func(path string) {
|
|
|
|
db, err := Open(path, 0600)
|
|
|
|
pageSize := db.pageSize
|
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
_, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
return err
|
2014-04-02 22:56:16 +00:00
|
|
|
})
|
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
_, err := tx.CreateBucket([]byte("woojits"))
|
|
|
|
return err
|
2014-04-02 22:56:16 +00:00
|
|
|
})
|
|
|
|
db.Close()
|
|
|
|
|
|
|
|
// Change a single byte in the meta page.
|
|
|
|
f, _ := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
|
|
|
|
f.WriteAt([]byte{1}, int64((i*pageSize)+(pageHeaderSize+12)))
|
|
|
|
f.Sync()
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
// Reopen the database.
|
|
|
|
_, err = Open(path, 0600)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
if i == 0 {
|
|
|
|
assert.Equal(t, "meta0 error: checksum error", err.Error())
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "meta1 error: checksum error", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-31 17:22:27 +00:00
|
|
|
var db DB
|
|
|
|
tx, err := db.Begin(false)
|
|
|
|
assert.Nil(t, tx)
|
|
|
|
assert.Equal(t, err, 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-03-09 03:25:37 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
tx, err := db.Begin(true)
|
2014-03-09 03:25:37 +00:00
|
|
|
assert.NotNil(t, tx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tx.DB(), db)
|
|
|
|
assert.Equal(t, tx.Writable(), true)
|
2014-03-28 06:07:05 +00:00
|
|
|
assert.NoError(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-03-31 17:22:27 +00:00
|
|
|
var db DB
|
|
|
|
tx, err := db.Begin(true)
|
|
|
|
assert.Equal(t, err, ErrDatabaseNotOpen)
|
|
|
|
assert.Nil(t, tx)
|
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-02-15 21:54:45 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-24 17:43:06 +00:00
|
|
|
err := db.Update(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
tx.CreateBucket([]byte("widgets"))
|
|
|
|
b := tx.Bucket([]byte("widgets"))
|
2014-02-23 06:08:30 +00:00
|
|
|
b.Put([]byte("foo"), []byte("bar"))
|
|
|
|
b.Put([]byte("baz"), []byte("bat"))
|
|
|
|
b.Delete([]byte("foo"))
|
2014-02-15 21:54:45 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2014-03-24 17:43:06 +00:00
|
|
|
err = db.View(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
assert.Nil(t, tx.Bucket([]byte("widgets")).Get([]byte("foo")))
|
|
|
|
assert.Equal(t, []byte("bat"), tx.Bucket([]byte("widgets")).Get([]byte("baz")))
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
assert.NoError(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-03-31 17:22:27 +00:00
|
|
|
var db DB
|
|
|
|
err := db.Update(func(tx *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-03-31 17:22:27 +00:00
|
|
|
assert.Equal(t, err, 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-04-07 22:24:51 +00:00
|
|
|
func TestDB_Update_ManualCommitAndRollback(t *testing.T) {
|
2014-03-31 17:22:27 +00:00
|
|
|
var db DB
|
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
tx.CreateBucket([]byte("widgets"))
|
2014-03-31 17:22:27 +00:00
|
|
|
assert.Panics(t, func() { tx.Commit() })
|
|
|
|
assert.Panics(t, func() { tx.Rollback() })
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
db.View(func(tx *Tx) error {
|
|
|
|
assert.Panics(t, func() { tx.Commit() })
|
|
|
|
assert.Panics(t, func() { tx.Rollback() })
|
|
|
|
return nil
|
2014-03-23 16:34:53 +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) {
|
|
|
|
withOpenDB(func(db *DB, path string) {
|
|
|
|
err := db.View(func(tx *Tx) error {
|
|
|
|
return errors.New("xxx")
|
|
|
|
})
|
|
|
|
assert.Equal(t, errors.New("xxx"), err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-02-09 22:52:19 +00:00
|
|
|
// Ensure that the database can be copied to a file path.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_CopyFile(t *testing.T) {
|
2014-02-16 06:38:03 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-04-07 22:24:51 +00:00
|
|
|
var dest = tempfile()
|
2014-03-24 17:43:06 +00:00
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
tx.CreateBucket([]byte("widgets"))
|
|
|
|
tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
|
|
|
|
tx.Bucket([]byte("widgets")).Put([]byte("baz"), []byte("bat"))
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-04-07 22:24:51 +00:00
|
|
|
assert.NoError(t, db.CopyFile(dest, 0600))
|
2014-02-09 22:52:19 +00:00
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
db2, err := Open(dest, 0600)
|
2014-03-31 17:22:27 +00:00
|
|
|
assert.NoError(t, err)
|
2014-02-16 06:38:03 +00:00
|
|
|
defer db2.Close()
|
|
|
|
|
2014-03-24 17:43:06 +00:00
|
|
|
db2.View(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
assert.Equal(t, []byte("bar"), tx.Bucket([]byte("widgets")).Get([]byte("foo")))
|
|
|
|
assert.Equal(t, []byte("bat"), tx.Bucket([]byte("widgets")).Get([]byte("baz")))
|
2014-03-21 15:46:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-02-16 06:38:03 +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) {
|
|
|
|
withOpenDB(func(db *DB, path string) {
|
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
_, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
return err
|
2014-04-07 22:24:51 +00:00
|
|
|
})
|
|
|
|
stats := db.Stats()
|
|
|
|
assert.Equal(t, 3, stats.TxStats.PageCount)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-02-11 19:16:12 +00:00
|
|
|
// Ensure that the mmap grows appropriately.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_mmapSize(t *testing.T) {
|
2014-02-11 19:16:12 +00:00
|
|
|
db := &DB{pageSize: 4096}
|
|
|
|
assert.Equal(t, db.mmapSize(0), minMmapSize)
|
|
|
|
assert.Equal(t, db.mmapSize(16384), minMmapSize)
|
|
|
|
assert.Equal(t, db.mmapSize(minMmapSize-1), minMmapSize)
|
|
|
|
assert.Equal(t, db.mmapSize(minMmapSize), minMmapSize*2)
|
|
|
|
assert.Equal(t, db.mmapSize(10000000), 20000768)
|
|
|
|
assert.Equal(t, db.mmapSize((1<<30)-1), 2147483648)
|
|
|
|
assert.Equal(t, db.mmapSize(1<<30), 1<<31)
|
|
|
|
}
|
|
|
|
|
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-03-25 13:25:00 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
_, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
return err
|
2014-03-25 13:25:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-07 22:24:51 +00:00
|
|
|
assert.NoError(t, tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")))
|
2014-03-25 13:25:00 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
db.Update(func(tx *Tx) error {
|
|
|
|
if p, _ := tx.Page(0); assert.NotNil(t, p) {
|
|
|
|
assert.Equal(t, "meta", p.Type)
|
|
|
|
}
|
|
|
|
if p, _ := tx.Page(1); assert.NotNil(t, p) {
|
|
|
|
assert.Equal(t, "meta", p.Type)
|
|
|
|
}
|
|
|
|
if p, _ := tx.Page(2); assert.NotNil(t, p) {
|
2014-03-31 14:52:13 +00:00
|
|
|
assert.Equal(t, "free", p.Type)
|
2014-03-25 13:25:00 +00:00
|
|
|
}
|
|
|
|
if p, _ := tx.Page(3); assert.NotNil(t, p) {
|
|
|
|
assert.Equal(t, "free", p.Type)
|
|
|
|
}
|
|
|
|
if p, _ := tx.Page(4); assert.NotNil(t, p) {
|
2014-03-31 14:52:13 +00:00
|
|
|
assert.Equal(t, "freelist", p.Type)
|
2014-03-25 13:25:00 +00:00
|
|
|
}
|
|
|
|
if p, _ := tx.Page(5); assert.NotNil(t, p) {
|
2014-04-07 22:24:51 +00:00
|
|
|
assert.Equal(t, "leaf", p.Type) // root leaf
|
2014-03-25 13:25:00 +00:00
|
|
|
}
|
|
|
|
if p, _ := tx.Page(6); assert.NotNil(t, p) {
|
2014-03-31 14:52:13 +00:00
|
|
|
assert.Equal(t, "leaf", p.Type)
|
|
|
|
}
|
|
|
|
if p, _ := tx.Page(7); assert.NotNil(t, p) {
|
2014-03-25 13:25:00 +00:00
|
|
|
assert.Equal(t, "free", p.Type)
|
|
|
|
}
|
2014-03-31 14:52:13 +00:00
|
|
|
p, _ := tx.Page(8)
|
2014-03-25 13:25:00 +00:00
|
|
|
assert.Nil(t, p)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-02-28 22:13:00 +00:00
|
|
|
// Ensure that a database can return a string representation of itself.
|
2014-04-07 22:24:51 +00:00
|
|
|
func TestDB_String(t *testing.T) {
|
|
|
|
db := &DB{path: "/foo/bar"}
|
|
|
|
assert.Equal(t, db.String(), `DB<"/foo/bar">`)
|
|
|
|
assert.Equal(t, db.GoString(), `bolt.DB{path:"/foo/bar"}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that DB stats can be substracted from one another.
|
|
|
|
func TestDBStats_Sub(t *testing.T) {
|
|
|
|
var a, b Stats
|
|
|
|
a.TxStats.PageCount = 3
|
|
|
|
b.TxStats.PageCount = 10
|
|
|
|
diff := b.Sub(&a)
|
|
|
|
assert.Equal(t, 7, diff.TxStats.PageCount)
|
2014-02-28 22:13:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
func ExampleDB_Update() {
|
|
|
|
// Open the database.
|
|
|
|
db, _ := Open(tempfile(), 0666)
|
|
|
|
defer os.Remove(db.Path())
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Execute several commands within a write transaction.
|
|
|
|
err := db.Update(func(tx *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 {
|
|
|
|
db.View(func(tx *Tx) error {
|
|
|
|
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.
|
|
|
|
db, _ := Open(tempfile(), 0666)
|
|
|
|
defer os.Remove(db.Path())
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Insert data into a bucket.
|
|
|
|
db.Update(func(tx *Tx) error {
|
|
|
|
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.
|
|
|
|
db.View(func(tx *Tx) error {
|
|
|
|
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.
|
|
|
|
db, _ := Open(tempfile(), 0666)
|
|
|
|
defer os.Remove(db.Path())
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Create a bucket.
|
|
|
|
db.Update(func(tx *Tx) error {
|
2014-04-16 03:45:06 +00:00
|
|
|
_, err := tx.CreateBucket([]byte("widgets"))
|
|
|
|
return err
|
2014-04-07 22:24:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleDB_CopyFile() {
|
|
|
|
// Open the database.
|
|
|
|
db, _ := Open(tempfile(), 0666)
|
|
|
|
defer os.Remove(db.Path())
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Create a bucket and a key.
|
|
|
|
db.Update(func(tx *Tx) error {
|
|
|
|
tx.CreateBucket([]byte("widgets"))
|
|
|
|
tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Copy the database to another file.
|
|
|
|
toFile := tempfile()
|
|
|
|
db.CopyFile(toFile, 0666)
|
|
|
|
defer os.Remove(toFile)
|
|
|
|
|
|
|
|
// Open the cloned database.
|
|
|
|
db2, _ := Open(toFile, 0666)
|
|
|
|
defer db2.Close()
|
|
|
|
|
|
|
|
// Ensure that the key exists in the copy.
|
|
|
|
db2.View(func(tx *Tx) error {
|
|
|
|
value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
|
2014-04-25 21:38:42 +00:00
|
|
|
fmt.Printf("The value for 'foo' in the clone is: %s\n", value)
|
2014-04-07 22:24:51 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// The value for 'foo' in the clone is: bar
|
|
|
|
}
|
|
|
|
|
|
|
|
// tempfile returns a temporary file path.
|
|
|
|
func tempfile() string {
|
2014-01-12 05:51:01 +00:00
|
|
|
f, _ := ioutil.TempFile("", "bolt-")
|
|
|
|
f.Close()
|
2014-04-07 22:24:51 +00:00
|
|
|
os.Remove(f.Name())
|
|
|
|
return f.Name()
|
|
|
|
}
|
2014-01-12 05:51:01 +00:00
|
|
|
|
2014-04-07 22:24:51 +00:00
|
|
|
// withTempPath executes a function with a database reference.
|
|
|
|
func withTempPath(fn func(string)) {
|
|
|
|
path := tempfile()
|
|
|
|
defer os.RemoveAll(path)
|
2014-03-31 17:22:27 +00:00
|
|
|
fn(path)
|
2014-01-12 05:51:01 +00:00
|
|
|
}
|
2014-01-12 22:30:09 +00:00
|
|
|
|
2014-01-13 17:35:04 +00:00
|
|
|
// withOpenDB executes a function with an already opened database.
|
|
|
|
func withOpenDB(fn func(*DB, string)) {
|
2014-03-31 17:22:27 +00:00
|
|
|
withTempPath(func(path string) {
|
|
|
|
db, err := Open(path, 0666)
|
|
|
|
if err != nil {
|
2014-01-13 17:35:04 +00:00
|
|
|
panic("cannot open db: " + err.Error())
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
fn(db, path)
|
2014-03-28 06:07:05 +00:00
|
|
|
|
2014-04-02 21:36:53 +00:00
|
|
|
// Log statistics.
|
|
|
|
if *statsFlag {
|
|
|
|
logStats(db)
|
|
|
|
}
|
|
|
|
|
2014-03-28 06:07:05 +00:00
|
|
|
// Check database consistency after every test.
|
|
|
|
mustCheck(db)
|
2014-01-13 17:35:04 +00:00
|
|
|
})
|
|
|
|
}
|
2014-02-16 04:50:34 +00:00
|
|
|
|
2014-03-28 06:07:05 +00:00
|
|
|
// mustCheck runs a consistency check on the database and panics if any errors are found.
|
|
|
|
func mustCheck(db *DB) {
|
|
|
|
if err := db.Check(); err != nil {
|
|
|
|
// Copy db off first.
|
2014-04-07 22:24:51 +00:00
|
|
|
var path = tempfile()
|
|
|
|
db.CopyFile(path, 0600)
|
2014-03-28 06:07:05 +00:00
|
|
|
|
|
|
|
if errors, ok := err.(ErrorList); ok {
|
|
|
|
for _, err := range errors {
|
|
|
|
warn(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
warn(err)
|
2014-04-07 22:24:51 +00:00
|
|
|
panic("check failure: " + path)
|
2014-03-28 06:07:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// writes the current database stats to the testing log.
|
|
|
|
func logStats(db *DB) {
|
|
|
|
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)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func truncDuration(d time.Duration) string {
|
|
|
|
return regexp.MustCompile(`^(\d+)(\.\d+)`).ReplaceAllString(d.String(), "$1")
|
|
|
|
}
|
2014-04-07 22:24:51 +00:00
|
|
|
|
|
|
|
// copyAndFailNow copies a database to a new location and then fails then test.
|
|
|
|
func copyAndFailNow(t *testing.T, db *DB) {
|
|
|
|
path := tempfile()
|
|
|
|
db.CopyFile(path, 0600)
|
|
|
|
fmt.Println("db copied to: ", path)
|
|
|
|
t.FailNow()
|
|
|
|
}
|