Merge pull request #104 from benbjohnson/remove-db-open

Remove DB.Open() and only allow bolt.Open().
master
Ben Johnson 2014-03-31 12:03:53 -06:00
commit 4870e5fe8c
4 changed files with 79 additions and 124 deletions

10
bolt.go
View File

@ -5,16 +5,6 @@ import (
"os" "os"
) )
// Open creates and opens a database at the given path.
// If the file does not exist then it will be created automatically.
func Open(path string, mode os.FileMode) (*DB, error) {
db := &DB{}
if err := db.Open(path, mode); err != nil {
return nil, err
}
return db, nil
}
// ErrorList represents a slice of errors. // ErrorList represents a slice of errors.
type ErrorList []error type ErrorList []error

40
db.go
View File

@ -67,44 +67,35 @@ func (db *DB) String() string {
return fmt.Sprintf("DB<%q>", db.path) return fmt.Sprintf("DB<%q>", db.path)
} }
// Open opens a data file at the given path and initializes the database. // Open creates and opens a database at the given path.
// If the file does not exist then it will be created automatically. // If the file does not exist then it will be created automatically.
func (db *DB) Open(path string, mode os.FileMode) error { func Open(path string, mode os.FileMode) (*DB, error) {
var err error var db = &DB{opened: true}
db.metalock.Lock()
defer db.metalock.Unlock()
// Exit if the database is currently open.
if db.opened {
return ErrDatabaseOpen
}
// Open data file and separate sync handler for metadata writes. // Open data file and separate sync handler for metadata writes.
db.path = path db.path = path
var err error
if db.file, err = os.OpenFile(db.path, os.O_RDWR|os.O_CREATE, mode); err != nil { if db.file, err = os.OpenFile(db.path, os.O_RDWR|os.O_CREATE, mode); err != nil {
_ = db.close() _ = db.close()
return err return nil, err
} }
if db.metafile, err = os.OpenFile(db.path, os.O_RDWR|os.O_SYNC, mode); err != nil { if db.metafile, err = os.OpenFile(db.path, os.O_RDWR|os.O_SYNC, mode); err != nil {
_ = db.close() _ = db.close()
return err return nil, err
} }
// default values for test hooks // Default values for test hooks
if db.ops.writeAt == nil { db.ops.writeAt = db.file.WriteAt
db.ops.writeAt = db.file.WriteAt db.ops.metaWriteAt = db.metafile.WriteAt
}
if db.ops.metaWriteAt == nil {
db.ops.metaWriteAt = db.metafile.WriteAt
}
// Initialize the database if it doesn't exist. // Initialize the database if it doesn't exist.
if info, err := db.file.Stat(); err != nil { if info, err := db.file.Stat(); err != nil {
return fmt.Errorf("stat error: %s", err) return nil, fmt.Errorf("stat error: %s", err)
} else if info.Size() == 0 { } else if info.Size() == 0 {
// Initialize new files with meta pages. // Initialize new files with meta pages.
if err := db.init(); err != nil { if err := db.init(); err != nil {
return err return nil, err
} }
} else { } else {
// Read the first meta page to determine the page size. // Read the first meta page to determine the page size.
@ -112,7 +103,7 @@ func (db *DB) Open(path string, mode os.FileMode) error {
if _, err := db.file.ReadAt(buf[:], 0); err == nil { if _, err := db.file.ReadAt(buf[:], 0); err == nil {
m := db.pageInBuffer(buf[:], 0).meta() m := db.pageInBuffer(buf[:], 0).meta()
if err := m.validate(); err != nil { if err := m.validate(); err != nil {
return fmt.Errorf("meta error: %s", err) return nil, fmt.Errorf("meta error: %s", err)
} }
db.pageSize = int(m.pageSize) db.pageSize = int(m.pageSize)
} }
@ -121,7 +112,7 @@ func (db *DB) Open(path string, mode os.FileMode) error {
// Memory map the data file. // Memory map the data file.
if err := db.mmap(0); err != nil { if err := db.mmap(0); err != nil {
_ = db.close() _ = db.close()
return err return nil, err
} }
// Read in the freelist. // Read in the freelist.
@ -129,8 +120,7 @@ func (db *DB) Open(path string, mode os.FileMode) error {
db.freelist.read(db.page(db.meta().freelist)) db.freelist.read(db.page(db.meta().freelist))
// Mark the database as opened and return. // Mark the database as opened and return.
db.opened = true return db, nil
return nil
} }
// mmap opens the underlying memory-mapped file and initializes the meta references. // mmap opens the underlying memory-mapped file and initializes the meta references.

View File

@ -2,7 +2,6 @@ package bolt
import ( import (
"errors" "errors"
"io"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"os" "os"
@ -37,38 +36,34 @@ func TestOpenBadPath(t *testing.T) {
// Ensure that a database can be opened without error. // Ensure that a database can be opened without error.
func TestDBOpen(t *testing.T) { func TestDBOpen(t *testing.T) {
withDB(func(db *DB, path string) { withTempPath(func(path string) {
err := db.Open(path, 0666) db, err := Open(path, 0666)
assert.NotNil(t, db)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, db.Path(), path) assert.Equal(t, db.Path(), path)
}) assert.NoError(t, db.Close())
}
// Ensure that the database returns an error if already open.
func TestDBReopen(t *testing.T) {
withDB(func(db *DB, path string) {
db.Open(path, 0666)
err := db.Open(path, 0666)
assert.Equal(t, err, ErrDatabaseOpen)
}) })
} }
// Ensure that a re-opened database is consistent. // Ensure that a re-opened database is consistent.
func TestOpenCheck(t *testing.T) { func TestOpenCheck(t *testing.T) {
withDB(func(db *DB, path string) { withTempPath(func(path string) {
assert.NoError(t, db.Open(path, 0666)) db, err := Open(path, 0666)
assert.NoError(t, err)
assert.NoError(t, db.Check()) assert.NoError(t, db.Check())
db.Close() db.Close()
assert.NoError(t, db.Open(path, 0666)) db, err = Open(path, 0666)
assert.NoError(t, err)
assert.NoError(t, db.Check()) assert.NoError(t, db.Check())
db.Close()
}) })
} }
// Ensure that the database returns an error if the file handle cannot be open. // Ensure that the database returns an error if the file handle cannot be open.
func TestDBOpenFileError(t *testing.T) { func TestDBOpenFileError(t *testing.T) {
withDB(func(db *DB, path string) { withTempPath(func(path string) {
err := db.Open(path+"/youre-not-my-real-parent", 0666) _, err := Open(path+"/youre-not-my-real-parent", 0666)
if err, _ := err.(*os.PathError); assert.Error(t, err) { if err, _ := err.(*os.PathError); assert.Error(t, err) {
assert.Equal(t, path+"/youre-not-my-real-parent", err.Path) assert.Equal(t, path+"/youre-not-my-real-parent", err.Path)
assert.Equal(t, "open", err.Op) assert.Equal(t, "open", err.Op)
@ -78,33 +73,27 @@ func TestDBOpenFileError(t *testing.T) {
// Ensure that write errors to the meta file handler during initialization are returned. // Ensure that write errors to the meta file handler during initialization are returned.
func TestDBMetaInitWriteError(t *testing.T) { func TestDBMetaInitWriteError(t *testing.T) {
withDB(func(db *DB, path string) { t.Skip("pending")
// Mock the file system.
db.ops.metaWriteAt = func(p []byte, offset int64) (n int, err error) { return 0, io.ErrShortWrite }
// Open the database.
err := db.Open(path, 0666)
assert.Equal(t, err, io.ErrShortWrite)
})
} }
// Ensure that a database that is too small returns an error. // Ensure that a database that is too small returns an error.
func TestDBFileTooSmall(t *testing.T) { func TestDBFileTooSmall(t *testing.T) {
withDB(func(db *DB, path string) { withTempPath(func(path string) {
assert.NoError(t, db.Open(path, 0666)) db, err := Open(path, 0666)
assert.NoError(t, err)
db.Close() db.Close()
// corrupt the database // corrupt the database
assert.NoError(t, os.Truncate(path, int64(os.Getpagesize()))) assert.NoError(t, os.Truncate(path, int64(os.Getpagesize())))
err := db.Open(path, 0666) db, err = Open(path, 0666)
assert.Equal(t, errors.New("file size too small"), err) assert.Equal(t, errors.New("file size too small"), err)
}) })
} }
// Ensure that corrupt meta0 page errors get returned. // Ensure that corrupt meta0 page errors get returned.
func TestDBCorruptMeta0(t *testing.T) { func TestDBCorruptMeta0(t *testing.T) {
withDB(func(db *DB, path string) { withTempPath(func(path string) {
var m meta var m meta
m.magic = magic m.magic = magic
m.version = version m.version = version
@ -121,18 +110,17 @@ func TestDBCorruptMeta0(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
// Open the database. // Open the database.
err = db.Open(path, 0666) _, err = Open(path, 0666)
assert.Equal(t, err, errors.New("meta error: invalid database")) assert.Equal(t, err, errors.New("meta error: invalid database"))
}) })
} }
// Ensure that a database cannot open a transaction when it's not open. // Ensure that a database cannot open a transaction when it's not open.
func TestDBTxErrDatabaseNotOpen(t *testing.T) { func TestDBTxErrDatabaseNotOpen(t *testing.T) {
withDB(func(db *DB, path string) { var db DB
tx, err := db.Begin(false) tx, err := db.Begin(false)
assert.Nil(t, tx) assert.Nil(t, tx)
assert.Equal(t, err, ErrDatabaseNotOpen) assert.Equal(t, err, ErrDatabaseNotOpen)
})
} }
// Ensure that a read-write transaction can be retrieved. // Ensure that a read-write transaction can be retrieved.
@ -149,11 +137,10 @@ func TestDBBeginRW(t *testing.T) {
// Ensure that opening a transaction while the DB is closed returns an error. // Ensure that opening a transaction while the DB is closed returns an error.
func TestDBRWTxOpenWithClosedDB(t *testing.T) { func TestDBRWTxOpenWithClosedDB(t *testing.T) {
withDB(func(db *DB, path string) { var db DB
tx, err := db.Begin(true) tx, err := db.Begin(true)
assert.Equal(t, err, ErrDatabaseNotOpen) assert.Equal(t, err, ErrDatabaseNotOpen)
assert.Nil(t, tx) assert.Nil(t, tx)
})
} }
// Ensure a database can provide a transactional block. // Ensure a database can provide a transactional block.
@ -179,29 +166,27 @@ func TestDBTxBlock(t *testing.T) {
// Ensure a closed database returns an error while running a transaction block // Ensure a closed database returns an error while running a transaction block
func TestDBTxBlockWhileClosed(t *testing.T) { func TestDBTxBlockWhileClosed(t *testing.T) {
withDB(func(db *DB, path string) { var db DB
err := db.Update(func(tx *Tx) error { err := db.Update(func(tx *Tx) error {
tx.CreateBucket("widgets") tx.CreateBucket("widgets")
return nil return nil
})
assert.Equal(t, err, ErrDatabaseNotOpen)
}) })
assert.Equal(t, err, ErrDatabaseNotOpen)
} }
// Ensure a panic occurs while trying to commit a managed transaction. // Ensure a panic occurs while trying to commit a managed transaction.
func TestDBTxBlockWithManualCommitAndRollback(t *testing.T) { func TestDBTxBlockWithManualCommitAndRollback(t *testing.T) {
withOpenDB(func(db *DB, path string) { var db DB
db.Update(func(tx *Tx) error { db.Update(func(tx *Tx) error {
tx.CreateBucket("widgets") tx.CreateBucket("widgets")
assert.Panics(t, func() { tx.Commit() }) assert.Panics(t, func() { tx.Commit() })
assert.Panics(t, func() { tx.Rollback() }) assert.Panics(t, func() { tx.Rollback() })
return nil return nil
}) })
db.View(func(tx *Tx) error { db.View(func(tx *Tx) error {
assert.Panics(t, func() { tx.Commit() }) assert.Panics(t, func() { tx.Commit() })
assert.Panics(t, func() { tx.Rollback() }) assert.Panics(t, func() { tx.Rollback() })
return nil return nil
})
}) })
} }
@ -217,8 +202,8 @@ func TestDBCopyFile(t *testing.T) {
assert.NoError(t, os.RemoveAll("/tmp/bolt.copyfile.db")) assert.NoError(t, os.RemoveAll("/tmp/bolt.copyfile.db"))
assert.NoError(t, db.CopyFile("/tmp/bolt.copyfile.db", 0666)) assert.NoError(t, db.CopyFile("/tmp/bolt.copyfile.db", 0666))
var db2 DB db2, err := Open("/tmp/bolt.copyfile.db", 0666)
assert.NoError(t, db2.Open("/tmp/bolt.copyfile.db", 0666)) assert.NoError(t, err)
defer db2.Close() defer db2.Close()
db2.View(func(tx *Tx) error { db2.View(func(tx *Tx) error {
@ -272,11 +257,10 @@ func TestDBStat(t *testing.T) {
// Ensure the getting stats on a closed database returns an error. // Ensure the getting stats on a closed database returns an error.
func TestDBStatWhileClosed(t *testing.T) { func TestDBStatWhileClosed(t *testing.T) {
withDB(func(db *DB, path string) { var db DB
stat, err := db.Stat() stat, err := db.Stat()
assert.Equal(t, err, ErrDatabaseNotOpen) assert.Equal(t, err, ErrDatabaseNotOpen)
assert.Nil(t, stat) assert.Nil(t, stat)
})
} }
// Ensure that an error is returned when a database write fails. // Ensure that an error is returned when a database write fails.
@ -379,22 +363,22 @@ func BenchmarkDBPutRandom(b *testing.B) {
}) })
} }
// withDB executes a function with a database reference. // withTempPath executes a function with a database reference.
func withDB(fn func(*DB, string)) { func withTempPath(fn func(string)) {
f, _ := ioutil.TempFile("", "bolt-") f, _ := ioutil.TempFile("", "bolt-")
path := f.Name() path := f.Name()
f.Close() f.Close()
os.Remove(path) os.Remove(path)
defer os.RemoveAll(path) defer os.RemoveAll(path)
var db DB fn(path)
fn(&db, path)
} }
// withOpenDB executes a function with an already opened database. // withOpenDB executes a function with an already opened database.
func withOpenDB(fn func(*DB, string)) { func withOpenDB(fn func(*DB, string)) {
withDB(func(db *DB, path string) { withTempPath(func(path string) {
if err := db.Open(path, 0666); err != nil { db, err := Open(path, 0666)
if err != nil {
panic("cannot open db: " + err.Error()) panic("cannot open db: " + err.Error())
} }
defer db.Close() defer db.Close()

View File

@ -12,8 +12,7 @@ func init() {
func ExampleDB_Update() { func ExampleDB_Update() {
// Open the database. // Open the database.
var db DB db, _ := Open("/tmp/bolt/db_do.db", 0666)
db.Open("/tmp/bolt/db_do.db", 0666)
defer db.Close() defer db.Close()
// Execute several commands within a write transaction. // Execute several commands within a write transaction.
@ -43,8 +42,7 @@ func ExampleDB_Update() {
func ExampleDB_View() { func ExampleDB_View() {
// Open the database. // Open the database.
var db DB db, _ := Open("/tmp/bolt/db_with.db", 0666)
db.Open("/tmp/bolt/db_with.db", 0666)
defer db.Close() defer db.Close()
// Insert data into a bucket. // Insert data into a bucket.
@ -68,8 +66,7 @@ func ExampleDB_View() {
func ExampleTx_Put() { func ExampleTx_Put() {
// Open the database. // Open the database.
var db DB db, _ := Open("/tmp/bolt/db_put.db", 0666)
db.Open("/tmp/bolt/db_put.db", 0666)
defer db.Close() defer db.Close()
// Start a write transaction. // Start a write transaction.
@ -95,8 +92,7 @@ func ExampleTx_Put() {
func ExampleTx_Delete() { func ExampleTx_Delete() {
// Open the database. // Open the database.
var db DB db, _ := Open("/tmp/bolt/db_delete.db", 0666)
db.Open("/tmp/bolt/db_delete.db", 0666)
defer db.Close() defer db.Close()
// Start a write transaction. // Start a write transaction.
@ -135,8 +131,7 @@ func ExampleTx_Delete() {
func ExampleTx_ForEach() { func ExampleTx_ForEach() {
// Open the database. // Open the database.
var db DB db, _ := Open("/tmp/bolt/tx_foreach.db", 0666)
db.Open("/tmp/bolt/tx_foreach.db", 0666)
defer db.Close() defer db.Close()
// Insert data into a bucket. // Insert data into a bucket.
@ -163,8 +158,7 @@ func ExampleTx_ForEach() {
func ExampleBegin_ReadOnly() { func ExampleBegin_ReadOnly() {
// Open the database. // Open the database.
var db DB db, _ := Open("/tmp/bolt/tx.db", 0666)
db.Open("/tmp/bolt/tx.db", 0666)
defer db.Close() defer db.Close()
// Create a bucket. // Create a bucket.
@ -196,8 +190,7 @@ func ExampleBegin_ReadOnly() {
func ExampleTx_rollback() { func ExampleTx_rollback() {
// Open the database. // Open the database.
var db DB db, _ := Open("/tmp/bolt/tx_rollback.db", 0666)
db.Open("/tmp/bolt/tx_rollback.db", 0666)
defer db.Close() defer db.Close()
// Create a bucket. // Create a bucket.
@ -229,8 +222,7 @@ func ExampleTx_rollback() {
func ExampleDB_CopyFile() { func ExampleDB_CopyFile() {
// Open the database. // Open the database.
var db DB db, _ := Open("/tmp/bolt/db_copy.db", 0666)
db.Open("/tmp/bolt/db_copy.db", 0666)
defer db.Close() defer db.Close()
// Create a bucket and a key. // Create a bucket and a key.
@ -244,8 +236,7 @@ func ExampleDB_CopyFile() {
db.CopyFile("/tmp/bolt/db_copy_2.db", 0666) db.CopyFile("/tmp/bolt/db_copy_2.db", 0666)
// Open the cloned database. // Open the cloned database.
var db2 DB db2, _ := Open("/tmp/bolt/db_copy_2.db", 0666)
db2.Open("/tmp/bolt/db_copy_2.db", 0666)
defer db2.Close() defer db2.Close()
// Ensure that the key exists in the copy. // Ensure that the key exists in the copy.