Rename errors.

master
Ben Johnson 2014-02-16 12:18:44 -07:00
parent 6a7be8879b
commit 149afc8c9b
10 changed files with 63 additions and 63 deletions

8
db.go
View File

@ -17,7 +17,7 @@ const maxMmapStep = 1 << 30 // 1GB
// DB represents a collection of buckets persisted to a file on disk. // DB represents a collection of buckets persisted to a file on disk.
// All data access is performed through transactions which can be obtained through the DB. // All data access is performed through transactions which can be obtained through the DB.
// All the functions on DB will return a DatabaseNotOpenError if accessed before Open() is called. // All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called.
type DB struct { type DB struct {
os _os os _os
syscall _syscall syscall _syscall
@ -71,7 +71,7 @@ func (db *DB) Open(path string, mode os.FileMode) error {
// Exit if the database is currently open. // Exit if the database is currently open.
if db.opened { if db.opened {
return DatabaseOpenError return ErrDatabaseOpen
} }
// Open data file and separate sync handler for metadata writes. // Open data file and separate sync handler for metadata writes.
@ -274,7 +274,7 @@ func (db *DB) Transaction() (*Transaction, error) {
// Exit if the database is not open yet. // Exit if the database is not open yet.
if !db.opened { if !db.opened {
return nil, DatabaseNotOpenError return nil, ErrDatabaseNotOpen
} }
// Create a transaction associated with the database. // Create a transaction associated with the database.
@ -300,7 +300,7 @@ func (db *DB) RWTransaction() (*RWTransaction, error) {
// Exit if the database is not open yet. // Exit if the database is not open yet.
if !db.opened { if !db.opened {
db.rwlock.Unlock() db.rwlock.Unlock()
return nil, DatabaseNotOpenError return nil, ErrDatabaseNotOpen
} }
// Create a transaction associated with the database. // Create a transaction associated with the database.

View File

@ -27,7 +27,7 @@ func TestDBReopen(t *testing.T) {
withDB(func(db *DB, path string) { withDB(func(db *DB, path string) {
db.Open(path, 0666) db.Open(path, 0666)
err := db.Open(path, 0666) err := db.Open(path, 0666)
assert.Equal(t, err, DatabaseOpenError) assert.Equal(t, err, ErrDatabaseOpen)
}) })
} }
@ -127,16 +127,16 @@ func TestDBCorruptMeta0(t *testing.T) {
// Open the database. // Open the database.
err := db.Open(path, 0666) err := db.Open(path, 0666)
assert.Equal(t, err, &Error{"meta error", InvalidError}) assert.Equal(t, err, &Error{"meta error", ErrInvalid})
}) })
} }
// 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 TestDBTransactionDatabaseNotOpenError(t *testing.T) { func TestDBTransactionErrDatabaseNotOpen(t *testing.T) {
withDB(func(db *DB, path string) { withDB(func(db *DB, path string) {
txn, err := db.Transaction() txn, err := db.Transaction()
assert.Nil(t, txn) assert.Nil(t, txn)
assert.Equal(t, err, DatabaseNotOpenError) assert.Equal(t, err, ErrDatabaseNotOpen)
}) })
} }
@ -182,7 +182,7 @@ func TestDBDelete(t *testing.T) {
func TestDBDeleteFromMissingBucket(t *testing.T) { func TestDBDeleteFromMissingBucket(t *testing.T) {
withOpenDB(func(db *DB, path string) { withOpenDB(func(db *DB, path string) {
err := db.Delete("widgets", []byte("foo")) err := db.Delete("widgets", []byte("foo"))
assert.Equal(t, err, BucketNotFoundError) assert.Equal(t, err, ErrBucketNotFound)
}) })
} }
@ -211,7 +211,7 @@ func TestDBTransactionBlockWhileClosed(t *testing.T) {
txn.CreateBucket("widgets") txn.CreateBucket("widgets")
return nil return nil
}) })
assert.Equal(t, err, DatabaseNotOpenError) assert.Equal(t, err, ErrDatabaseNotOpen)
}) })
} }
@ -219,7 +219,7 @@ func TestDBTransactionBlockWhileClosed(t *testing.T) {
func TestDBBucketWhileClosed(t *testing.T) { func TestDBBucketWhileClosed(t *testing.T) {
withDB(func(db *DB, path string) { withDB(func(db *DB, path string) {
b, err := db.Bucket("widgets") b, err := db.Bucket("widgets")
assert.Equal(t, err, DatabaseNotOpenError) assert.Equal(t, err, ErrDatabaseNotOpen)
assert.Nil(t, b) assert.Nil(t, b)
}) })
} }
@ -228,7 +228,7 @@ func TestDBBucketWhileClosed(t *testing.T) {
func TestDBBucketsWhileClosed(t *testing.T) { func TestDBBucketsWhileClosed(t *testing.T) {
withDB(func(db *DB, path string) { withDB(func(db *DB, path string) {
b, err := db.Buckets() b, err := db.Buckets()
assert.Equal(t, err, DatabaseNotOpenError) assert.Equal(t, err, ErrDatabaseNotOpen)
assert.Nil(t, b) assert.Nil(t, b)
}) })
} }
@ -237,7 +237,7 @@ func TestDBBucketsWhileClosed(t *testing.T) {
func TestDBGetWhileClosed(t *testing.T) { func TestDBGetWhileClosed(t *testing.T) {
withDB(func(db *DB, path string) { withDB(func(db *DB, path string) {
value, err := db.Get("widgets", []byte("foo")) value, err := db.Get("widgets", []byte("foo"))
assert.Equal(t, err, DatabaseNotOpenError) assert.Equal(t, err, ErrDatabaseNotOpen)
assert.Nil(t, value) assert.Nil(t, value)
}) })
} }

View File

@ -1,43 +1,43 @@
package bolt package bolt
var ( var (
// InvalidError is returned when a data file is not a Bolt-formatted database. // ErrInvalid is returned when a data file is not a Bolt-formatted database.
InvalidError = &Error{"Invalid database", nil} ErrInvalid = &Error{"Invalid database", nil}
// VersionMismatchError is returned when the data file was created with a // ErrVersionMismatch is returned when the data file was created with a
// different version of Bolt. // different version of Bolt.
VersionMismatchError = &Error{"version mismatch", nil} ErrVersionMismatch = &Error{"version mismatch", nil}
// DatabaseNotOpenError is returned when a DB instance is accessed before it // ErrDatabaseNotOpen is returned when a DB instance is accessed before it
// is opened or after it is closed. // is opened or after it is closed.
DatabaseNotOpenError = &Error{"database not open", nil} ErrDatabaseNotOpen = &Error{"database not open", nil}
// DatabaseOpenError is returned when opening a database that is // ErrDatabaseOpen is returned when opening a database that is
// already open. // already open.
DatabaseOpenError = &Error{"database already open", nil} ErrDatabaseOpen = &Error{"database already open", nil}
// BucketNotFoundError is returned when trying to access a bucket that has // ErrBucketNotFound is returned when trying to access a bucket that has
// not been created yet. // not been created yet.
BucketNotFoundError = &Error{"bucket not found", nil} ErrBucketNotFound = &Error{"bucket not found", nil}
// BucketExistsError is returned when creating a bucket that already exists. // ErrBucketExists is returned when creating a bucket that already exists.
BucketExistsError = &Error{"bucket already exists", nil} ErrBucketExists = &Error{"bucket already exists", nil}
// BucketNameRequiredError is returned when creating a bucket with a blank name. // ErrBucketNameRequired is returned when creating a bucket with a blank name.
BucketNameRequiredError = &Error{"bucket name required", nil} ErrBucketNameRequired = &Error{"bucket name required", nil}
// BucketNameTooLargeError is returned when creating a bucket with a name // ErrBucketNameTooLarge is returned when creating a bucket with a name
// that is longer than MaxBucketNameSize. // that is longer than MaxBucketNameSize.
BucketNameTooLargeError = &Error{"bucket name too large", nil} ErrBucketNameTooLarge = &Error{"bucket name too large", nil}
// KeyRequiredError is returned when inserting a zero-length key. // ErrKeyRequired is returned when inserting a zero-length key.
KeyRequiredError = &Error{"key required", nil} ErrKeyRequired = &Error{"key required", nil}
// KeyTooLargeError is returned when inserting a key that is larger than MaxKeySize. // ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize.
KeyTooLargeError = &Error{"key too large", nil} ErrKeyTooLarge = &Error{"key too large", nil}
// ValueTooLargeError is returned when inserting a value that is larger than MaxValueSize. // ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize.
ValueTooLargeError = &Error{"value too large", nil} ErrValueTooLarge = &Error{"value too large", nil}
) )
// Error represents an error condition caused by Bolt. // Error represents an error condition caused by Bolt.

View File

@ -47,7 +47,7 @@ func TestParallelTransactions(t *testing.T) {
local := current local := current
txn, err := db.Transaction() txn, err := db.Transaction()
mutex.RUnlock() mutex.RUnlock()
if err == DatabaseNotOpenError { if err == ErrDatabaseNotOpen {
wg.Done() wg.Done()
return return
} else if !assert.NoError(t, err) { } else if !assert.NoError(t, err) {

View File

@ -16,9 +16,9 @@ type meta struct {
// validate checks the marker bytes and version of the meta page to ensure it matches this binary. // validate checks the marker bytes and version of the meta page to ensure it matches this binary.
func (m *meta) validate() error { func (m *meta) validate() error {
if m.magic != magic { if m.magic != magic {
return InvalidError return ErrInvalid
} else if m.version != version { } else if m.version != version {
return VersionMismatchError return ErrVersionMismatch
} }
return nil return nil
} }

View File

@ -8,11 +8,11 @@ import (
// Ensure that meta with bad magic is invalid. // Ensure that meta with bad magic is invalid.
func TestMetaValidateMagic(t *testing.T) { func TestMetaValidateMagic(t *testing.T) {
m := &meta{magic: 0x01234567} m := &meta{magic: 0x01234567}
assert.Equal(t, m.validate(), InvalidError) assert.Equal(t, m.validate(), ErrInvalid)
} }
// Ensure that meta with a bad version is invalid. // Ensure that meta with a bad version is invalid.
func TestMetaValidateVersion(t *testing.T) { func TestMetaValidateVersion(t *testing.T) {
m := &meta{magic: magic, version: 200} m := &meta{magic: magic, version: 200}
assert.Equal(t, m.validate(), VersionMismatchError) assert.Equal(t, m.validate(), ErrVersionMismatch)
} }

View File

@ -29,11 +29,11 @@ func (t *RWTransaction) init(db *DB) {
func (t *RWTransaction) CreateBucket(name string) error { func (t *RWTransaction) CreateBucket(name string) error {
// Check if bucket already exists. // Check if bucket already exists.
if b := t.Bucket(name); b != nil { if b := t.Bucket(name); b != nil {
return BucketExistsError return ErrBucketExists
} else if len(name) == 0 { } else if len(name) == 0 {
return BucketNameRequiredError return ErrBucketNameRequired
} else if len(name) > MaxBucketNameSize { } else if len(name) > MaxBucketNameSize {
return BucketNameTooLargeError return ErrBucketNameTooLarge
} }
// Create a blank root leaf page. // Create a blank root leaf page.
@ -53,7 +53,7 @@ func (t *RWTransaction) CreateBucket(name string) error {
// Returns an error if the bucket cannot be found. // Returns an error if the bucket cannot be found.
func (t *RWTransaction) DeleteBucket(name string) error { func (t *RWTransaction) DeleteBucket(name string) error {
if b := t.Bucket(name); b == nil { if b := t.Bucket(name); b == nil {
return BucketNotFoundError return ErrBucketNotFound
} }
// Remove from buckets page. // Remove from buckets page.
@ -69,7 +69,7 @@ func (t *RWTransaction) NextSequence(name string) (int, error) {
// Check if bucket already exists. // Check if bucket already exists.
b := t.Bucket(name) b := t.Bucket(name)
if b == nil { if b == nil {
return 0, BucketNotFoundError return 0, ErrBucketNotFound
} }
// Increment and return the sequence. // Increment and return the sequence.
@ -84,16 +84,16 @@ func (t *RWTransaction) NextSequence(name string) (int, error) {
func (t *RWTransaction) Put(name string, key []byte, value []byte) error { func (t *RWTransaction) Put(name string, key []byte, value []byte) error {
b := t.Bucket(name) b := t.Bucket(name)
if b == nil { if b == nil {
return BucketNotFoundError return ErrBucketNotFound
} }
// Validate the key and data size. // Validate the key and data size.
if len(key) == 0 { if len(key) == 0 {
return KeyRequiredError return ErrKeyRequired
} else if len(key) > MaxKeySize { } else if len(key) > MaxKeySize {
return KeyTooLargeError return ErrKeyTooLarge
} else if len(value) > MaxValueSize { } else if len(value) > MaxValueSize {
return ValueTooLargeError return ErrValueTooLarge
} }
// Move cursor to correct position. // Move cursor to correct position.
@ -112,7 +112,7 @@ func (t *RWTransaction) Put(name string, key []byte, value []byte) error {
func (t *RWTransaction) Delete(name string, key []byte) error { func (t *RWTransaction) Delete(name string, key []byte) error {
b := t.Bucket(name) b := t.Bucket(name)
if b == nil { if b == nil {
return BucketNotFoundError return ErrBucketNotFound
} }
// Move cursor to correct position. // Move cursor to correct position.

View File

@ -25,7 +25,7 @@ func TestRWTransaction(t *testing.T) {
func TestRWTransactionOpenWithClosedDB(t *testing.T) { func TestRWTransactionOpenWithClosedDB(t *testing.T) {
withDB(func(db *DB, path string) { withDB(func(db *DB, path string) {
txn, err := db.RWTransaction() txn, err := db.RWTransaction()
assert.Equal(t, err, DatabaseNotOpenError) assert.Equal(t, err, ErrDatabaseNotOpen)
assert.Nil(t, txn) assert.Nil(t, txn)
}) })
} }
@ -53,7 +53,7 @@ func TestRWTransactionRecreateBucket(t *testing.T) {
// Create the same bucket again. // Create the same bucket again.
err = db.CreateBucket("widgets") err = db.CreateBucket("widgets")
assert.Equal(t, err, BucketExistsError) assert.Equal(t, err, ErrBucketExists)
}) })
} }
@ -61,7 +61,7 @@ func TestRWTransactionRecreateBucket(t *testing.T) {
func TestRWTransactionCreateBucketWithoutName(t *testing.T) { func TestRWTransactionCreateBucketWithoutName(t *testing.T) {
withOpenDB(func(db *DB, path string) { withOpenDB(func(db *DB, path string) {
err := db.CreateBucket("") err := db.CreateBucket("")
assert.Equal(t, err, BucketNameRequiredError) assert.Equal(t, err, ErrBucketNameRequired)
}) })
} }
@ -72,7 +72,7 @@ func TestRWTransactionCreateBucketWithLongName(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
err = db.CreateBucket(strings.Repeat("X", 256)) err = db.CreateBucket(strings.Repeat("X", 256))
assert.Equal(t, err, BucketNameTooLargeError) assert.Equal(t, err, ErrBucketNameTooLarge)
}) })
} }
@ -86,7 +86,7 @@ func TestRWTransactionDeleteBucket(t *testing.T) {
// Delete the bucket and make sure we can't get the value. // Delete the bucket and make sure we can't get the value.
assert.NoError(t, db.DeleteBucket("widgets")) assert.NoError(t, db.DeleteBucket("widgets"))
value, err := db.Get("widgets", []byte("foo")) value, err := db.Get("widgets", []byte("foo"))
assert.Equal(t, err, BucketNotFoundError) assert.Equal(t, err, ErrBucketNotFound)
assert.Nil(t, value) assert.Nil(t, value)
// Create the bucket again and make sure there's not a phantom value. // Create the bucket again and make sure there's not a phantom value.
@ -118,7 +118,7 @@ func TestRWTransactionNextSequence(t *testing.T) {
// Missing buckets return an error. // Missing buckets return an error.
seq, err = db.NextSequence("no_such_bucket") seq, err = db.NextSequence("no_such_bucket")
assert.Equal(t, err, BucketNotFoundError) assert.Equal(t, err, ErrBucketNotFound)
assert.Equal(t, seq, 0) assert.Equal(t, seq, 0)
}) })
} }
@ -127,7 +127,7 @@ func TestRWTransactionNextSequence(t *testing.T) {
func TestRWTransactionPutBucketNotFound(t *testing.T) { func TestRWTransactionPutBucketNotFound(t *testing.T) {
withOpenDB(func(db *DB, path string) { withOpenDB(func(db *DB, path string) {
err := db.Put("widgets", []byte("foo"), []byte("bar")) err := db.Put("widgets", []byte("foo"), []byte("bar"))
assert.Equal(t, err, BucketNotFoundError) assert.Equal(t, err, ErrBucketNotFound)
}) })
} }
@ -136,9 +136,9 @@ func TestRWTransactionPutEmptyKey(t *testing.T) {
withOpenDB(func(db *DB, path string) { withOpenDB(func(db *DB, path string) {
db.CreateBucket("widgets") db.CreateBucket("widgets")
err := db.Put("widgets", []byte(""), []byte("bar")) err := db.Put("widgets", []byte(""), []byte("bar"))
assert.Equal(t, err, KeyRequiredError) assert.Equal(t, err, ErrKeyRequired)
err = db.Put("widgets", nil, []byte("bar")) err = db.Put("widgets", nil, []byte("bar"))
assert.Equal(t, err, KeyRequiredError) assert.Equal(t, err, ErrKeyRequired)
}) })
} }
@ -147,7 +147,7 @@ func TestRWTransactionPutKeyTooLarge(t *testing.T) {
withOpenDB(func(db *DB, path string) { withOpenDB(func(db *DB, path string) {
db.CreateBucket("widgets") db.CreateBucket("widgets")
err := db.Put("widgets", make([]byte, 32769), []byte("bar")) err := db.Put("widgets", make([]byte, 32769), []byte("bar"))
assert.Equal(t, err, KeyTooLargeError) assert.Equal(t, err, ErrKeyTooLarge)
}) })
} }
@ -155,7 +155,7 @@ func TestRWTransactionPutKeyTooLarge(t *testing.T) {
func TestRWTransactionDeleteBucketNotFound(t *testing.T) { func TestRWTransactionDeleteBucketNotFound(t *testing.T) {
withOpenDB(func(db *DB, path string) { withOpenDB(func(db *DB, path string) {
err := db.DeleteBucket("widgets") err := db.DeleteBucket("widgets")
assert.Equal(t, err, BucketNotFoundError) assert.Equal(t, err, ErrBucketNotFound)
}) })
} }

View File

@ -77,7 +77,7 @@ func (t *Transaction) Buckets() []*Bucket {
func (t *Transaction) Cursor(name string) (*Cursor, error) { func (t *Transaction) Cursor(name string) (*Cursor, error) {
b := t.Bucket(name) b := t.Bucket(name)
if b == nil { if b == nil {
return nil, BucketNotFoundError return nil, ErrBucketNotFound
} }
return b.cursor(), nil return b.cursor(), nil
} }

View File

@ -69,7 +69,7 @@ func TestTransactionCursorMissingBucket(t *testing.T) {
txn, _ := db.Transaction() txn, _ := db.Transaction()
c, err := txn.Cursor("woojits") c, err := txn.Cursor("woojits")
assert.Nil(t, c) assert.Nil(t, c)
assert.Equal(t, err, BucketNotFoundError) assert.Equal(t, err, ErrBucketNotFound)
txn.Close() txn.Close()
}) })
} }