mirror of https://github.com/etcd-io/bbolt.git
Rename errors.
parent
6a7be8879b
commit
149afc8c9b
8
db.go
8
db.go
|
@ -17,7 +17,7 @@ const maxMmapStep = 1 << 30 // 1GB
|
|||
|
||||
// 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 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 {
|
||||
os _os
|
||||
syscall _syscall
|
||||
|
@ -71,7 +71,7 @@ func (db *DB) Open(path string, mode os.FileMode) error {
|
|||
|
||||
// Exit if the database is currently open.
|
||||
if db.opened {
|
||||
return DatabaseOpenError
|
||||
return ErrDatabaseOpen
|
||||
}
|
||||
|
||||
// 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.
|
||||
if !db.opened {
|
||||
return nil, DatabaseNotOpenError
|
||||
return nil, ErrDatabaseNotOpen
|
||||
}
|
||||
|
||||
// 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.
|
||||
if !db.opened {
|
||||
db.rwlock.Unlock()
|
||||
return nil, DatabaseNotOpenError
|
||||
return nil, ErrDatabaseNotOpen
|
||||
}
|
||||
|
||||
// Create a transaction associated with the database.
|
||||
|
|
18
db_test.go
18
db_test.go
|
@ -27,7 +27,7 @@ func TestDBReopen(t *testing.T) {
|
|||
withDB(func(db *DB, path string) {
|
||||
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.
|
||||
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.
|
||||
func TestDBTransactionDatabaseNotOpenError(t *testing.T) {
|
||||
func TestDBTransactionErrDatabaseNotOpen(t *testing.T) {
|
||||
withDB(func(db *DB, path string) {
|
||||
txn, err := db.Transaction()
|
||||
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) {
|
||||
withOpenDB(func(db *DB, path string) {
|
||||
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")
|
||||
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) {
|
||||
withDB(func(db *DB, path string) {
|
||||
b, err := db.Bucket("widgets")
|
||||
assert.Equal(t, err, DatabaseNotOpenError)
|
||||
assert.Equal(t, err, ErrDatabaseNotOpen)
|
||||
assert.Nil(t, b)
|
||||
})
|
||||
}
|
||||
|
@ -228,7 +228,7 @@ func TestDBBucketWhileClosed(t *testing.T) {
|
|||
func TestDBBucketsWhileClosed(t *testing.T) {
|
||||
withDB(func(db *DB, path string) {
|
||||
b, err := db.Buckets()
|
||||
assert.Equal(t, err, DatabaseNotOpenError)
|
||||
assert.Equal(t, err, ErrDatabaseNotOpen)
|
||||
assert.Nil(t, b)
|
||||
})
|
||||
}
|
||||
|
@ -237,7 +237,7 @@ func TestDBBucketsWhileClosed(t *testing.T) {
|
|||
func TestDBGetWhileClosed(t *testing.T) {
|
||||
withDB(func(db *DB, path string) {
|
||||
value, err := db.Get("widgets", []byte("foo"))
|
||||
assert.Equal(t, err, DatabaseNotOpenError)
|
||||
assert.Equal(t, err, ErrDatabaseNotOpen)
|
||||
assert.Nil(t, value)
|
||||
})
|
||||
}
|
||||
|
|
44
error.go
44
error.go
|
@ -1,43 +1,43 @@
|
|||
package bolt
|
||||
|
||||
var (
|
||||
// InvalidError is returned when a data file is not a Bolt-formatted database.
|
||||
InvalidError = &Error{"Invalid database", nil}
|
||||
// ErrInvalid is returned when a data file is not a Bolt-formatted database.
|
||||
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.
|
||||
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.
|
||||
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.
|
||||
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.
|
||||
BucketNotFoundError = &Error{"bucket not found", nil}
|
||||
ErrBucketNotFound = &Error{"bucket not found", nil}
|
||||
|
||||
// BucketExistsError is returned when creating a bucket that already exists.
|
||||
BucketExistsError = &Error{"bucket already exists", nil}
|
||||
// ErrBucketExists is returned when creating a bucket that already exists.
|
||||
ErrBucketExists = &Error{"bucket already exists", nil}
|
||||
|
||||
// BucketNameRequiredError is returned when creating a bucket with a blank name.
|
||||
BucketNameRequiredError = &Error{"bucket name required", nil}
|
||||
// ErrBucketNameRequired is returned when creating a bucket with a blank name.
|
||||
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.
|
||||
BucketNameTooLargeError = &Error{"bucket name too large", nil}
|
||||
ErrBucketNameTooLarge = &Error{"bucket name too large", nil}
|
||||
|
||||
// KeyRequiredError is returned when inserting a zero-length key.
|
||||
KeyRequiredError = &Error{"key required", nil}
|
||||
// ErrKeyRequired is returned when inserting a zero-length key.
|
||||
ErrKeyRequired = &Error{"key required", nil}
|
||||
|
||||
// KeyTooLargeError is returned when inserting a key that is larger than MaxKeySize.
|
||||
KeyTooLargeError = &Error{"key too large", nil}
|
||||
// ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize.
|
||||
ErrKeyTooLarge = &Error{"key too large", nil}
|
||||
|
||||
// ValueTooLargeError is returned when inserting a value that is larger than MaxValueSize.
|
||||
ValueTooLargeError = &Error{"value too large", nil}
|
||||
// ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize.
|
||||
ErrValueTooLarge = &Error{"value too large", nil}
|
||||
)
|
||||
|
||||
// Error represents an error condition caused by Bolt.
|
||||
|
|
|
@ -47,7 +47,7 @@ func TestParallelTransactions(t *testing.T) {
|
|||
local := current
|
||||
txn, err := db.Transaction()
|
||||
mutex.RUnlock()
|
||||
if err == DatabaseNotOpenError {
|
||||
if err == ErrDatabaseNotOpen {
|
||||
wg.Done()
|
||||
return
|
||||
} else if !assert.NoError(t, err) {
|
||||
|
|
4
meta.go
4
meta.go
|
@ -16,9 +16,9 @@ type meta struct {
|
|||
// validate checks the marker bytes and version of the meta page to ensure it matches this binary.
|
||||
func (m *meta) validate() error {
|
||||
if m.magic != magic {
|
||||
return InvalidError
|
||||
return ErrInvalid
|
||||
} else if m.version != version {
|
||||
return VersionMismatchError
|
||||
return ErrVersionMismatch
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -8,11 +8,11 @@ import (
|
|||
// Ensure that meta with bad magic is invalid.
|
||||
func TestMetaValidateMagic(t *testing.T) {
|
||||
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.
|
||||
func TestMetaValidateVersion(t *testing.T) {
|
||||
m := &meta{magic: magic, version: 200}
|
||||
assert.Equal(t, m.validate(), VersionMismatchError)
|
||||
assert.Equal(t, m.validate(), ErrVersionMismatch)
|
||||
}
|
||||
|
|
|
@ -29,11 +29,11 @@ func (t *RWTransaction) init(db *DB) {
|
|||
func (t *RWTransaction) CreateBucket(name string) error {
|
||||
// Check if bucket already exists.
|
||||
if b := t.Bucket(name); b != nil {
|
||||
return BucketExistsError
|
||||
return ErrBucketExists
|
||||
} else if len(name) == 0 {
|
||||
return BucketNameRequiredError
|
||||
return ErrBucketNameRequired
|
||||
} else if len(name) > MaxBucketNameSize {
|
||||
return BucketNameTooLargeError
|
||||
return ErrBucketNameTooLarge
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (t *RWTransaction) DeleteBucket(name string) error {
|
||||
if b := t.Bucket(name); b == nil {
|
||||
return BucketNotFoundError
|
||||
return ErrBucketNotFound
|
||||
}
|
||||
|
||||
// Remove from buckets page.
|
||||
|
@ -69,7 +69,7 @@ func (t *RWTransaction) NextSequence(name string) (int, error) {
|
|||
// Check if bucket already exists.
|
||||
b := t.Bucket(name)
|
||||
if b == nil {
|
||||
return 0, BucketNotFoundError
|
||||
return 0, ErrBucketNotFound
|
||||
}
|
||||
|
||||
// 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 {
|
||||
b := t.Bucket(name)
|
||||
if b == nil {
|
||||
return BucketNotFoundError
|
||||
return ErrBucketNotFound
|
||||
}
|
||||
|
||||
// Validate the key and data size.
|
||||
if len(key) == 0 {
|
||||
return KeyRequiredError
|
||||
return ErrKeyRequired
|
||||
} else if len(key) > MaxKeySize {
|
||||
return KeyTooLargeError
|
||||
return ErrKeyTooLarge
|
||||
} else if len(value) > MaxValueSize {
|
||||
return ValueTooLargeError
|
||||
return ErrValueTooLarge
|
||||
}
|
||||
|
||||
// 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 {
|
||||
b := t.Bucket(name)
|
||||
if b == nil {
|
||||
return BucketNotFoundError
|
||||
return ErrBucketNotFound
|
||||
}
|
||||
|
||||
// Move cursor to correct position.
|
||||
|
|
|
@ -25,7 +25,7 @@ func TestRWTransaction(t *testing.T) {
|
|||
func TestRWTransactionOpenWithClosedDB(t *testing.T) {
|
||||
withDB(func(db *DB, path string) {
|
||||
txn, err := db.RWTransaction()
|
||||
assert.Equal(t, err, DatabaseNotOpenError)
|
||||
assert.Equal(t, err, ErrDatabaseNotOpen)
|
||||
assert.Nil(t, txn)
|
||||
})
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ func TestRWTransactionRecreateBucket(t *testing.T) {
|
|||
|
||||
// Create the same bucket again.
|
||||
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) {
|
||||
withOpenDB(func(db *DB, path string) {
|
||||
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)
|
||||
|
||||
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.
|
||||
assert.NoError(t, db.DeleteBucket("widgets"))
|
||||
value, err := db.Get("widgets", []byte("foo"))
|
||||
assert.Equal(t, err, BucketNotFoundError)
|
||||
assert.Equal(t, err, ErrBucketNotFound)
|
||||
assert.Nil(t, 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.
|
||||
seq, err = db.NextSequence("no_such_bucket")
|
||||
assert.Equal(t, err, BucketNotFoundError)
|
||||
assert.Equal(t, err, ErrBucketNotFound)
|
||||
assert.Equal(t, seq, 0)
|
||||
})
|
||||
}
|
||||
|
@ -127,7 +127,7 @@ func TestRWTransactionNextSequence(t *testing.T) {
|
|||
func TestRWTransactionPutBucketNotFound(t *testing.T) {
|
||||
withOpenDB(func(db *DB, path string) {
|
||||
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) {
|
||||
db.CreateBucket("widgets")
|
||||
err := db.Put("widgets", []byte(""), []byte("bar"))
|
||||
assert.Equal(t, err, KeyRequiredError)
|
||||
assert.Equal(t, err, ErrKeyRequired)
|
||||
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) {
|
||||
db.CreateBucket("widgets")
|
||||
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) {
|
||||
withOpenDB(func(db *DB, path string) {
|
||||
err := db.DeleteBucket("widgets")
|
||||
assert.Equal(t, err, BucketNotFoundError)
|
||||
assert.Equal(t, err, ErrBucketNotFound)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -77,7 +77,7 @@ func (t *Transaction) Buckets() []*Bucket {
|
|||
func (t *Transaction) Cursor(name string) (*Cursor, error) {
|
||||
b := t.Bucket(name)
|
||||
if b == nil {
|
||||
return nil, BucketNotFoundError
|
||||
return nil, ErrBucketNotFound
|
||||
}
|
||||
return b.cursor(), nil
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ func TestTransactionCursorMissingBucket(t *testing.T) {
|
|||
txn, _ := db.Transaction()
|
||||
c, err := txn.Cursor("woojits")
|
||||
assert.Nil(t, c)
|
||||
assert.Equal(t, err, BucketNotFoundError)
|
||||
assert.Equal(t, err, ErrBucketNotFound)
|
||||
txn.Close()
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue