walletdb/bdb: switch from bolt to bbolt

Fixes #545.
This commit is contained in:
Olaoluwa Osuntokun 2018-11-29 18:43:31 -08:00
parent b523fd5274
commit 20db88d8ff
No known key found for this signature in database
GPG key ID: CE58F7F8E20FD9A2

View file

@ -16,31 +16,31 @@ import (
func convertErr(err error) error { func convertErr(err error) error {
switch err { switch err {
// Database open/create errors. // Database open/create errors.
case bolt.ErrDatabaseNotOpen: case bbolt.ErrDatabaseNotOpen:
return walletdb.ErrDbNotOpen return walletdb.ErrDbNotOpen
case bolt.ErrInvalid: case bbolt.ErrInvalid:
return walletdb.ErrInvalid return walletdb.ErrInvalid
// Transaction errors. // Transaction errors.
case bolt.ErrTxNotWritable: case bbolt.ErrTxNotWritable:
return walletdb.ErrTxNotWritable return walletdb.ErrTxNotWritable
case bolt.ErrTxClosed: case bbolt.ErrTxClosed:
return walletdb.ErrTxClosed return walletdb.ErrTxClosed
// Value/bucket errors. // Value/bucket errors.
case bolt.ErrBucketNotFound: case bbolt.ErrBucketNotFound:
return walletdb.ErrBucketNotFound return walletdb.ErrBucketNotFound
case bolt.ErrBucketExists: case bbolt.ErrBucketExists:
return walletdb.ErrBucketExists return walletdb.ErrBucketExists
case bolt.ErrBucketNameRequired: case bbolt.ErrBucketNameRequired:
return walletdb.ErrBucketNameRequired return walletdb.ErrBucketNameRequired
case bolt.ErrKeyRequired: case bbolt.ErrKeyRequired:
return walletdb.ErrKeyRequired return walletdb.ErrKeyRequired
case bolt.ErrKeyTooLarge: case bbolt.ErrKeyTooLarge:
return walletdb.ErrKeyTooLarge return walletdb.ErrKeyTooLarge
case bolt.ErrValueTooLarge: case bbolt.ErrValueTooLarge:
return walletdb.ErrValueTooLarge return walletdb.ErrValueTooLarge
case bolt.ErrIncompatibleValue: case bbolt.ErrIncompatibleValue:
return walletdb.ErrIncompatibleValue return walletdb.ErrIncompatibleValue
} }
@ -52,7 +52,7 @@ func convertErr(err error) error {
// read-write and implements the walletdb Tx interfaces. The transaction // read-write and implements the walletdb Tx interfaces. The transaction
// provides a root bucket against which all read and writes occur. // provides a root bucket against which all read and writes occur.
type transaction struct { type transaction struct {
boltTx *bolt.Tx boltTx *bbolt.Tx
} }
func (tx *transaction) ReadBucket(key []byte) walletdb.ReadBucket { func (tx *transaction) ReadBucket(key []byte) walletdb.ReadBucket {
@ -101,7 +101,7 @@ func (tx *transaction) Rollback() error {
// bucket is an internal type used to represent a collection of key/value pairs // bucket is an internal type used to represent a collection of key/value pairs
// and implements the walletdb Bucket interfaces. // and implements the walletdb Bucket interfaces.
type bucket bolt.Bucket type bucket bbolt.Bucket
// Enforce bucket implements the walletdb Bucket interfaces. // Enforce bucket implements the walletdb Bucket interfaces.
var _ walletdb.ReadWriteBucket = (*bucket)(nil) var _ walletdb.ReadWriteBucket = (*bucket)(nil)
@ -111,7 +111,7 @@ var _ walletdb.ReadWriteBucket = (*bucket)(nil)
// //
// This function is part of the walletdb.ReadWriteBucket interface implementation. // This function is part of the walletdb.ReadWriteBucket interface implementation.
func (b *bucket) NestedReadWriteBucket(key []byte) walletdb.ReadWriteBucket { func (b *bucket) NestedReadWriteBucket(key []byte) walletdb.ReadWriteBucket {
boltBucket := (*bolt.Bucket)(b).Bucket(key) boltBucket := (*bbolt.Bucket)(b).Bucket(key)
// Don't return a non-nil interface to a nil pointer. // Don't return a non-nil interface to a nil pointer.
if boltBucket == nil { if boltBucket == nil {
return nil return nil
@ -130,7 +130,7 @@ func (b *bucket) NestedReadBucket(key []byte) walletdb.ReadBucket {
// //
// This function is part of the walletdb.Bucket interface implementation. // This function is part of the walletdb.Bucket interface implementation.
func (b *bucket) CreateBucket(key []byte) (walletdb.ReadWriteBucket, error) { func (b *bucket) CreateBucket(key []byte) (walletdb.ReadWriteBucket, error) {
boltBucket, err := (*bolt.Bucket)(b).CreateBucket(key) boltBucket, err := (*bbolt.Bucket)(b).CreateBucket(key)
if err != nil { if err != nil {
return nil, convertErr(err) return nil, convertErr(err)
} }
@ -143,7 +143,7 @@ func (b *bucket) CreateBucket(key []byte) (walletdb.ReadWriteBucket, error) {
// //
// This function is part of the walletdb.Bucket interface implementation. // This function is part of the walletdb.Bucket interface implementation.
func (b *bucket) CreateBucketIfNotExists(key []byte) (walletdb.ReadWriteBucket, error) { func (b *bucket) CreateBucketIfNotExists(key []byte) (walletdb.ReadWriteBucket, error) {
boltBucket, err := (*bolt.Bucket)(b).CreateBucketIfNotExists(key) boltBucket, err := (*bbolt.Bucket)(b).CreateBucketIfNotExists(key)
if err != nil { if err != nil {
return nil, convertErr(err) return nil, convertErr(err)
} }
@ -156,7 +156,7 @@ func (b *bucket) CreateBucketIfNotExists(key []byte) (walletdb.ReadWriteBucket,
// //
// This function is part of the walletdb.Bucket interface implementation. // This function is part of the walletdb.Bucket interface implementation.
func (b *bucket) DeleteNestedBucket(key []byte) error { func (b *bucket) DeleteNestedBucket(key []byte) error {
return convertErr((*bolt.Bucket)(b).DeleteBucket(key)) return convertErr((*bbolt.Bucket)(b).DeleteBucket(key))
} }
// ForEach invokes the passed function with every key/value pair in the bucket. // ForEach invokes the passed function with every key/value pair in the bucket.
@ -169,7 +169,7 @@ func (b *bucket) DeleteNestedBucket(key []byte) error {
// //
// This function is part of the walletdb.Bucket interface implementation. // This function is part of the walletdb.Bucket interface implementation.
func (b *bucket) ForEach(fn func(k, v []byte) error) error { func (b *bucket) ForEach(fn func(k, v []byte) error) error {
return convertErr((*bolt.Bucket)(b).ForEach(fn)) return convertErr((*bbolt.Bucket)(b).ForEach(fn))
} }
// Put saves the specified key/value pair to the bucket. Keys that do not // Put saves the specified key/value pair to the bucket. Keys that do not
@ -178,7 +178,7 @@ func (b *bucket) ForEach(fn func(k, v []byte) error) error {
// //
// This function is part of the walletdb.Bucket interface implementation. // This function is part of the walletdb.Bucket interface implementation.
func (b *bucket) Put(key, value []byte) error { func (b *bucket) Put(key, value []byte) error {
return convertErr((*bolt.Bucket)(b).Put(key, value)) return convertErr((*bbolt.Bucket)(b).Put(key, value))
} }
// Get returns the value for the given key. Returns nil if the key does // Get returns the value for the given key. Returns nil if the key does
@ -190,7 +190,7 @@ func (b *bucket) Put(key, value []byte) error {
// //
// This function is part of the walletdb.Bucket interface implementation. // This function is part of the walletdb.Bucket interface implementation.
func (b *bucket) Get(key []byte) []byte { func (b *bucket) Get(key []byte) []byte {
return (*bolt.Bucket)(b).Get(key) return (*bbolt.Bucket)(b).Get(key)
} }
// Delete removes the specified key from the bucket. Deleting a key that does // Delete removes the specified key from the bucket. Deleting a key that does
@ -199,7 +199,7 @@ func (b *bucket) Get(key []byte) []byte {
// //
// This function is part of the walletdb.Bucket interface implementation. // This function is part of the walletdb.Bucket interface implementation.
func (b *bucket) Delete(key []byte) error { func (b *bucket) Delete(key []byte) error {
return convertErr((*bolt.Bucket)(b).Delete(key)) return convertErr((*bbolt.Bucket)(b).Delete(key))
} }
func (b *bucket) ReadCursor() walletdb.ReadCursor { func (b *bucket) ReadCursor() walletdb.ReadCursor {
@ -211,7 +211,7 @@ func (b *bucket) ReadCursor() walletdb.ReadCursor {
// //
// This function is part of the walletdb.Bucket interface implementation. // This function is part of the walletdb.Bucket interface implementation.
func (b *bucket) ReadWriteCursor() walletdb.ReadWriteCursor { func (b *bucket) ReadWriteCursor() walletdb.ReadWriteCursor {
return (*cursor)((*bolt.Bucket)(b).Cursor()) return (*cursor)((*bbolt.Bucket)(b).Cursor())
} }
// cursor represents a cursor over key/value pairs and nested buckets of a // cursor represents a cursor over key/value pairs and nested buckets of a
@ -221,7 +221,7 @@ func (b *bucket) ReadWriteCursor() walletdb.ReadWriteCursor {
// modifications to the bucket, with the exception of cursor.Delete, invalidate // modifications to the bucket, with the exception of cursor.Delete, invalidate
// the cursor. After invalidation, the cursor must be repositioned, or the keys // the cursor. After invalidation, the cursor must be repositioned, or the keys
// and values returned may be unpredictable. // and values returned may be unpredictable.
type cursor bolt.Cursor type cursor bbolt.Cursor
// Delete removes the current key/value pair the cursor is at without // Delete removes the current key/value pair the cursor is at without
// invalidating the cursor. Returns ErrTxNotWritable if attempted on a read-only // invalidating the cursor. Returns ErrTxNotWritable if attempted on a read-only
@ -230,35 +230,35 @@ type cursor bolt.Cursor
// //
// This function is part of the walletdb.Cursor interface implementation. // This function is part of the walletdb.Cursor interface implementation.
func (c *cursor) Delete() error { func (c *cursor) Delete() error {
return convertErr((*bolt.Cursor)(c).Delete()) return convertErr((*bbolt.Cursor)(c).Delete())
} }
// First positions the cursor at the first key/value pair and returns the pair. // First positions the cursor at the first key/value pair and returns the pair.
// //
// This function is part of the walletdb.Cursor interface implementation. // This function is part of the walletdb.Cursor interface implementation.
func (c *cursor) First() (key, value []byte) { func (c *cursor) First() (key, value []byte) {
return (*bolt.Cursor)(c).First() return (*bbolt.Cursor)(c).First()
} }
// Last positions the cursor at the last key/value pair and returns the pair. // Last positions the cursor at the last key/value pair and returns the pair.
// //
// This function is part of the walletdb.Cursor interface implementation. // This function is part of the walletdb.Cursor interface implementation.
func (c *cursor) Last() (key, value []byte) { func (c *cursor) Last() (key, value []byte) {
return (*bolt.Cursor)(c).Last() return (*bbolt.Cursor)(c).Last()
} }
// Next moves the cursor one key/value pair forward and returns the new pair. // Next moves the cursor one key/value pair forward and returns the new pair.
// //
// This function is part of the walletdb.Cursor interface implementation. // This function is part of the walletdb.Cursor interface implementation.
func (c *cursor) Next() (key, value []byte) { func (c *cursor) Next() (key, value []byte) {
return (*bolt.Cursor)(c).Next() return (*bbolt.Cursor)(c).Next()
} }
// Prev moves the cursor one key/value pair backward and returns the new pair. // Prev moves the cursor one key/value pair backward and returns the new pair.
// //
// This function is part of the walletdb.Cursor interface implementation. // This function is part of the walletdb.Cursor interface implementation.
func (c *cursor) Prev() (key, value []byte) { func (c *cursor) Prev() (key, value []byte) {
return (*bolt.Cursor)(c).Prev() return (*bbolt.Cursor)(c).Prev()
} }
// Seek positions the cursor at the passed seek key. If the key does not exist, // Seek positions the cursor at the passed seek key. If the key does not exist,
@ -266,19 +266,19 @@ func (c *cursor) Prev() (key, value []byte) {
// //
// This function is part of the walletdb.Cursor interface implementation. // This function is part of the walletdb.Cursor interface implementation.
func (c *cursor) Seek(seek []byte) (key, value []byte) { func (c *cursor) Seek(seek []byte) (key, value []byte) {
return (*bolt.Cursor)(c).Seek(seek) return (*bbolt.Cursor)(c).Seek(seek)
} }
// db represents a collection of namespaces which are persisted and implements // db represents a collection of namespaces which are persisted and implements
// the walletdb.Db interface. All database access is performed through // the walletdb.Db interface. All database access is performed through
// transactions which are obtained through the specific Namespace. // transactions which are obtained through the specific Namespace.
type db bolt.DB type db bbolt.DB
// Enforce db implements the walletdb.Db interface. // Enforce db implements the walletdb.Db interface.
var _ walletdb.DB = (*db)(nil) var _ walletdb.DB = (*db)(nil)
func (db *db) beginTx(writable bool) (*transaction, error) { func (db *db) beginTx(writable bool) (*transaction, error) {
boltTx, err := (*bolt.DB)(db).Begin(writable) boltTx, err := (*bbolt.DB)(db).Begin(writable)
if err != nil { if err != nil {
return nil, convertErr(err) return nil, convertErr(err)
} }
@ -298,7 +298,7 @@ func (db *db) BeginReadWriteTx() (walletdb.ReadWriteTx, error) {
// //
// This function is part of the walletdb.Db interface implementation. // This function is part of the walletdb.Db interface implementation.
func (db *db) Copy(w io.Writer) error { func (db *db) Copy(w io.Writer) error {
return convertErr((*bolt.DB)(db).View(func(tx *bolt.Tx) error { return convertErr((*bbolt.DB)(db).View(func(tx *bbolt.Tx) error {
return tx.Copy(w) return tx.Copy(w)
})) }))
} }
@ -307,7 +307,7 @@ func (db *db) Copy(w io.Writer) error {
// //
// This function is part of the walletdb.Db interface implementation. // This function is part of the walletdb.Db interface implementation.
func (db *db) Close() error { func (db *db) Close() error {
return convertErr((*bolt.DB)(db).Close()) return convertErr((*bbolt.DB)(db).Close())
} }
// filesExists reports whether the named file or directory exists. // filesExists reports whether the named file or directory exists.
@ -327,6 +327,6 @@ func openDB(dbPath string, create bool) (walletdb.DB, error) {
return nil, walletdb.ErrDbDoesNotExist return nil, walletdb.ErrDbDoesNotExist
} }
boltDB, err := bolt.Open(dbPath, 0600, nil) boltDB, err := bbolt.Open(dbPath, 0600, nil)
return (*db)(boltDB), convertErr(err) return (*db)(boltDB), convertErr(err)
} }