2015-12-01 19:44:58 +01:00
|
|
|
// Copyright (c) 2014 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-11-10 01:31:38 +01:00
|
|
|
|
|
|
|
package bdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
2020-08-11 14:15:46 +02:00
|
|
|
"time"
|
2014-11-10 01:31:38 +01:00
|
|
|
|
2018-05-15 07:11:11 +02:00
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2020-06-15 23:24:49 +02:00
|
|
|
"go.etcd.io/bbolt"
|
2014-11-10 01:31:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// convertErr converts some bolt errors to the equivalent walletdb error.
|
|
|
|
func convertErr(err error) error {
|
|
|
|
switch err {
|
|
|
|
// Database open/create errors.
|
2018-11-30 03:43:31 +01:00
|
|
|
case bbolt.ErrDatabaseNotOpen:
|
2014-11-10 01:31:38 +01:00
|
|
|
return walletdb.ErrDbNotOpen
|
2018-11-30 03:43:31 +01:00
|
|
|
case bbolt.ErrInvalid:
|
2014-11-10 01:31:38 +01:00
|
|
|
return walletdb.ErrInvalid
|
|
|
|
|
|
|
|
// Transaction errors.
|
2018-11-30 03:43:31 +01:00
|
|
|
case bbolt.ErrTxNotWritable:
|
2014-11-10 01:31:38 +01:00
|
|
|
return walletdb.ErrTxNotWritable
|
2018-11-30 03:43:31 +01:00
|
|
|
case bbolt.ErrTxClosed:
|
2014-11-10 01:31:38 +01:00
|
|
|
return walletdb.ErrTxClosed
|
|
|
|
|
|
|
|
// Value/bucket errors.
|
2018-11-30 03:43:31 +01:00
|
|
|
case bbolt.ErrBucketNotFound:
|
2014-11-10 01:31:38 +01:00
|
|
|
return walletdb.ErrBucketNotFound
|
2018-11-30 03:43:31 +01:00
|
|
|
case bbolt.ErrBucketExists:
|
2014-11-10 01:31:38 +01:00
|
|
|
return walletdb.ErrBucketExists
|
2018-11-30 03:43:31 +01:00
|
|
|
case bbolt.ErrBucketNameRequired:
|
2014-11-10 01:31:38 +01:00
|
|
|
return walletdb.ErrBucketNameRequired
|
2018-11-30 03:43:31 +01:00
|
|
|
case bbolt.ErrKeyRequired:
|
2014-11-10 01:31:38 +01:00
|
|
|
return walletdb.ErrKeyRequired
|
2018-11-30 03:43:31 +01:00
|
|
|
case bbolt.ErrKeyTooLarge:
|
2014-11-10 01:31:38 +01:00
|
|
|
return walletdb.ErrKeyTooLarge
|
2018-11-30 03:43:31 +01:00
|
|
|
case bbolt.ErrValueTooLarge:
|
2014-11-10 01:31:38 +01:00
|
|
|
return walletdb.ErrValueTooLarge
|
2018-11-30 03:43:31 +01:00
|
|
|
case bbolt.ErrIncompatibleValue:
|
2014-11-10 01:31:38 +01:00
|
|
|
return walletdb.ErrIncompatibleValue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the original error if none of the above applies.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
// transaction represents a database transaction. It can either by read-only or
|
|
|
|
// read-write and implements the walletdb Tx interfaces. The transaction
|
|
|
|
// provides a root bucket against which all read and writes occur.
|
|
|
|
type transaction struct {
|
2018-11-30 03:43:31 +01:00
|
|
|
boltTx *bbolt.Tx
|
2017-01-17 01:19:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *transaction) ReadBucket(key []byte) walletdb.ReadBucket {
|
|
|
|
return tx.ReadWriteBucket(key)
|
|
|
|
}
|
|
|
|
|
2021-04-27 11:14:43 +02:00
|
|
|
// ForEachBucket will iterate through all top level buckets.
|
|
|
|
func (tx *transaction) ForEachBucket(fn func(key []byte) error) error {
|
|
|
|
return convertErr(tx.boltTx.ForEach(
|
|
|
|
func(name []byte, _ *bbolt.Bucket) error {
|
|
|
|
return fn(name)
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
func (tx *transaction) ReadWriteBucket(key []byte) walletdb.ReadWriteBucket {
|
|
|
|
boltBucket := tx.boltTx.Bucket(key)
|
|
|
|
if boltBucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return (*bucket)(boltBucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *transaction) CreateTopLevelBucket(key []byte) (walletdb.ReadWriteBucket, error) {
|
2020-01-15 13:51:15 +01:00
|
|
|
boltBucket, err := tx.boltTx.CreateBucketIfNotExists(key)
|
2017-01-17 01:19:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, convertErr(err)
|
|
|
|
}
|
|
|
|
return (*bucket)(boltBucket), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *transaction) DeleteTopLevelBucket(key []byte) error {
|
|
|
|
err := tx.boltTx.DeleteBucket(key)
|
|
|
|
if err != nil {
|
|
|
|
return convertErr(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit commits all changes that have been made through the root bucket and
|
|
|
|
// all of its sub-buckets to persistent storage.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadWriteTx interface implementation.
|
2017-01-17 01:19:02 +01:00
|
|
|
func (tx *transaction) Commit() error {
|
|
|
|
return convertErr(tx.boltTx.Commit())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback undoes all changes that have been made to the root bucket and all of
|
|
|
|
// its sub-buckets.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadTx interface implementation.
|
2017-01-17 01:19:02 +01:00
|
|
|
func (tx *transaction) Rollback() error {
|
|
|
|
return convertErr(tx.boltTx.Rollback())
|
|
|
|
}
|
|
|
|
|
2018-12-10 11:57:23 +01:00
|
|
|
// OnCommit takes a function closure that will be executed when the transaction
|
|
|
|
// successfully gets committed.
|
|
|
|
//
|
|
|
|
// This function is part of the walletdb.ReadWriteTx interface implementation.
|
|
|
|
func (tx *transaction) OnCommit(f func()) {
|
|
|
|
tx.boltTx.OnCommit(f)
|
|
|
|
}
|
|
|
|
|
2014-11-10 01:31:38 +01:00
|
|
|
// bucket is an internal type used to represent a collection of key/value pairs
|
2017-01-17 01:19:02 +01:00
|
|
|
// and implements the walletdb Bucket interfaces.
|
2018-11-30 03:43:31 +01:00
|
|
|
type bucket bbolt.Bucket
|
2014-11-10 01:31:38 +01:00
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
// Enforce bucket implements the walletdb Bucket interfaces.
|
|
|
|
var _ walletdb.ReadWriteBucket = (*bucket)(nil)
|
2014-11-10 01:31:38 +01:00
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
// NestedReadWriteBucket retrieves a nested bucket with the given key. Returns
|
|
|
|
// nil if the bucket does not exist.
|
2014-11-10 01:31:38 +01:00
|
|
|
//
|
2017-01-17 01:19:02 +01:00
|
|
|
// This function is part of the walletdb.ReadWriteBucket interface implementation.
|
|
|
|
func (b *bucket) NestedReadWriteBucket(key []byte) walletdb.ReadWriteBucket {
|
2018-11-30 03:43:31 +01:00
|
|
|
boltBucket := (*bbolt.Bucket)(b).Bucket(key)
|
2017-01-17 01:19:02 +01:00
|
|
|
// Don't return a non-nil interface to a nil pointer.
|
2014-11-10 01:31:38 +01:00
|
|
|
if boltBucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return (*bucket)(boltBucket)
|
|
|
|
}
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
func (b *bucket) NestedReadBucket(key []byte) walletdb.ReadBucket {
|
|
|
|
return b.NestedReadWriteBucket(key)
|
|
|
|
}
|
|
|
|
|
2014-11-10 01:31:38 +01:00
|
|
|
// CreateBucket creates and returns a new nested bucket with the given key.
|
|
|
|
// Returns ErrBucketExists if the bucket already exists, ErrBucketNameRequired
|
|
|
|
// if the key is empty, or ErrIncompatibleValue if the key value is otherwise
|
|
|
|
// invalid.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadWriteBucket interface implementation.
|
2017-01-17 01:19:02 +01:00
|
|
|
func (b *bucket) CreateBucket(key []byte) (walletdb.ReadWriteBucket, error) {
|
2018-11-30 03:43:31 +01:00
|
|
|
boltBucket, err := (*bbolt.Bucket)(b).CreateBucket(key)
|
2014-11-10 01:31:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, convertErr(err)
|
|
|
|
}
|
|
|
|
return (*bucket)(boltBucket), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBucketIfNotExists creates and returns a new nested bucket with the
|
|
|
|
// given key if it does not already exist. Returns ErrBucketNameRequired if the
|
|
|
|
// key is empty or ErrIncompatibleValue if the key value is otherwise invalid.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadWriteBucket interface implementation.
|
2017-01-17 01:19:02 +01:00
|
|
|
func (b *bucket) CreateBucketIfNotExists(key []byte) (walletdb.ReadWriteBucket, error) {
|
2018-11-30 03:43:31 +01:00
|
|
|
boltBucket, err := (*bbolt.Bucket)(b).CreateBucketIfNotExists(key)
|
2014-11-10 01:31:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, convertErr(err)
|
|
|
|
}
|
|
|
|
return (*bucket)(boltBucket), nil
|
|
|
|
}
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
// DeleteNestedBucket removes a nested bucket with the given key. Returns
|
2014-11-10 01:31:38 +01:00
|
|
|
// ErrTxNotWritable if attempted against a read-only transaction and
|
|
|
|
// ErrBucketNotFound if the specified bucket does not exist.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadWriteBucket interface implementation.
|
2017-01-17 01:19:02 +01:00
|
|
|
func (b *bucket) DeleteNestedBucket(key []byte) error {
|
2018-11-30 03:43:31 +01:00
|
|
|
return convertErr((*bbolt.Bucket)(b).DeleteBucket(key))
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ForEach invokes the passed function with every key/value pair in the bucket.
|
|
|
|
// This includes nested buckets, in which case the value is nil, but it does not
|
|
|
|
// include the key/value pairs within those nested buckets.
|
|
|
|
//
|
|
|
|
// NOTE: The values returned by this function are only valid during a
|
|
|
|
// transaction. Attempting to access them after a transaction has ended will
|
|
|
|
// likely result in an access violation.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadBucket interface implementation.
|
2014-11-10 01:31:38 +01:00
|
|
|
func (b *bucket) ForEach(fn func(k, v []byte) error) error {
|
2018-11-30 03:43:31 +01:00
|
|
|
return convertErr((*bbolt.Bucket)(b).ForEach(fn))
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put saves the specified key/value pair to the bucket. Keys that do not
|
|
|
|
// already exist are added and keys that already exist are overwritten. Returns
|
|
|
|
// ErrTxNotWritable if attempted against a read-only transaction.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadWriteBucket interface implementation.
|
2014-11-10 01:31:38 +01:00
|
|
|
func (b *bucket) Put(key, value []byte) error {
|
2018-11-30 03:43:31 +01:00
|
|
|
return convertErr((*bbolt.Bucket)(b).Put(key, value))
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the value for the given key. Returns nil if the key does
|
|
|
|
// not exist in this bucket (or nested buckets).
|
|
|
|
//
|
|
|
|
// NOTE: The value returned by this function is only valid during a
|
|
|
|
// transaction. Attempting to access it after a transaction has ended
|
|
|
|
// will likely result in an access violation.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadBucket interface implementation.
|
2014-11-10 01:31:38 +01:00
|
|
|
func (b *bucket) Get(key []byte) []byte {
|
2018-11-30 03:43:31 +01:00
|
|
|
return (*bbolt.Bucket)(b).Get(key)
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes the specified key from the bucket. Deleting a key that does
|
|
|
|
// not exist does not return an error. Returns ErrTxNotWritable if attempted
|
|
|
|
// against a read-only transaction.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadWriteBucket interface implementation.
|
2014-11-10 01:31:38 +01:00
|
|
|
func (b *bucket) Delete(key []byte) error {
|
2018-11-30 03:43:31 +01:00
|
|
|
return convertErr((*bbolt.Bucket)(b).Delete(key))
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
func (b *bucket) ReadCursor() walletdb.ReadCursor {
|
|
|
|
return b.ReadWriteCursor()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadWriteCursor returns a new cursor, allowing for iteration over the bucket's
|
2015-04-06 21:17:09 +02:00
|
|
|
// key/value pairs and nested buckets in forward or backward order.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadWriteBucket interface implementation.
|
2017-01-17 01:19:02 +01:00
|
|
|
func (b *bucket) ReadWriteCursor() walletdb.ReadWriteCursor {
|
2018-11-30 03:43:31 +01:00
|
|
|
return (*cursor)((*bbolt.Bucket)(b).Cursor())
|
2015-04-06 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 11:57:23 +01:00
|
|
|
// Tx returns the bucket's transaction.
|
|
|
|
//
|
|
|
|
// This function is part of the walletdb.ReadWriteBucket interface implementation.
|
|
|
|
func (b *bucket) Tx() walletdb.ReadWriteTx {
|
|
|
|
return &transaction{
|
|
|
|
(*bbolt.Bucket)(b).Tx(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:51:15 +01:00
|
|
|
// NextSequence returns an autoincrementing integer for the bucket.
|
|
|
|
func (b *bucket) NextSequence() (uint64, error) {
|
|
|
|
return (*bbolt.Bucket)(b).NextSequence()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSequence updates the sequence number for the bucket.
|
|
|
|
func (b *bucket) SetSequence(v uint64) error {
|
|
|
|
return (*bbolt.Bucket)(b).SetSequence(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sequence returns the current integer for the bucket without incrementing it.
|
|
|
|
func (b *bucket) Sequence() uint64 {
|
|
|
|
return (*bbolt.Bucket)(b).Sequence()
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:17:09 +02:00
|
|
|
// cursor represents a cursor over key/value pairs and nested buckets of a
|
|
|
|
// bucket.
|
|
|
|
//
|
|
|
|
// Note that open cursors are not tracked on bucket changes and any
|
|
|
|
// modifications to the bucket, with the exception of cursor.Delete, invalidate
|
|
|
|
// the cursor. After invalidation, the cursor must be repositioned, or the keys
|
|
|
|
// and values returned may be unpredictable.
|
2018-11-30 03:43:31 +01:00
|
|
|
type cursor bbolt.Cursor
|
2015-04-06 21:17:09 +02:00
|
|
|
|
|
|
|
// Delete removes the current key/value pair the cursor is at without
|
|
|
|
// invalidating the cursor. Returns ErrTxNotWritable if attempted on a read-only
|
|
|
|
// transaction, or ErrIncompatibleValue if attempted when the cursor points to a
|
|
|
|
// nested bucket.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadWriteCursor interface implementation.
|
2015-04-06 21:17:09 +02:00
|
|
|
func (c *cursor) Delete() error {
|
2018-11-30 03:43:31 +01:00
|
|
|
return convertErr((*bbolt.Cursor)(c).Delete())
|
2015-04-06 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// First positions the cursor at the first key/value pair and returns the pair.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadCursor interface implementation.
|
2015-04-06 21:17:09 +02:00
|
|
|
func (c *cursor) First() (key, value []byte) {
|
2018-11-30 03:43:31 +01:00
|
|
|
return (*bbolt.Cursor)(c).First()
|
2015-04-06 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Last positions the cursor at the last key/value pair and returns the pair.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadCursor interface implementation.
|
2015-04-06 21:17:09 +02:00
|
|
|
func (c *cursor) Last() (key, value []byte) {
|
2018-11-30 03:43:31 +01:00
|
|
|
return (*bbolt.Cursor)(c).Last()
|
2015-04-06 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next moves the cursor one key/value pair forward and returns the new pair.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadCursor interface implementation.
|
2015-04-06 21:17:09 +02:00
|
|
|
func (c *cursor) Next() (key, value []byte) {
|
2018-11-30 03:43:31 +01:00
|
|
|
return (*bbolt.Cursor)(c).Next()
|
2015-04-06 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prev moves the cursor one key/value pair backward and returns the new pair.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadCursor interface implementation.
|
2015-04-06 21:17:09 +02:00
|
|
|
func (c *cursor) Prev() (key, value []byte) {
|
2018-11-30 03:43:31 +01:00
|
|
|
return (*bbolt.Cursor)(c).Prev()
|
2015-04-06 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seek positions the cursor at the passed seek key. If the key does not exist,
|
|
|
|
// the cursor is moved to the next key after seek. Returns the new pair.
|
|
|
|
//
|
2018-12-10 11:57:23 +01:00
|
|
|
// This function is part of the walletdb.ReadCursor interface implementation.
|
2015-04-06 21:17:09 +02:00
|
|
|
func (c *cursor) Seek(seek []byte) (key, value []byte) {
|
2018-11-30 03:43:31 +01:00
|
|
|
return (*bbolt.Cursor)(c).Seek(seek)
|
2015-04-06 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
2014-11-10 01:31:38 +01:00
|
|
|
// db represents a collection of namespaces which are persisted and implements
|
|
|
|
// the walletdb.Db interface. All database access is performed through
|
|
|
|
// transactions which are obtained through the specific Namespace.
|
2018-11-30 03:43:31 +01:00
|
|
|
type db bbolt.DB
|
2014-11-10 01:31:38 +01:00
|
|
|
|
|
|
|
// Enforce db implements the walletdb.Db interface.
|
|
|
|
var _ walletdb.DB = (*db)(nil)
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
func (db *db) beginTx(writable bool) (*transaction, error) {
|
2018-11-30 03:43:31 +01:00
|
|
|
boltTx, err := (*bbolt.DB)(db).Begin(writable)
|
2014-11-10 01:31:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, convertErr(err)
|
|
|
|
}
|
2017-01-17 01:19:02 +01:00
|
|
|
return &transaction{boltTx: boltTx}, nil
|
|
|
|
}
|
2014-11-10 01:31:38 +01:00
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
func (db *db) BeginReadTx() (walletdb.ReadTx, error) {
|
|
|
|
return db.beginTx(false)
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
func (db *db) BeginReadWriteTx() (walletdb.ReadWriteTx, error) {
|
|
|
|
return db.beginTx(true)
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy writes a copy of the database to the provided writer. This call will
|
|
|
|
// start a read-only transaction to perform all operations.
|
|
|
|
//
|
|
|
|
// This function is part of the walletdb.Db interface implementation.
|
|
|
|
func (db *db) Copy(w io.Writer) error {
|
2018-11-30 03:43:31 +01:00
|
|
|
return convertErr((*bbolt.DB)(db).View(func(tx *bbolt.Tx) error {
|
2014-11-10 01:31:38 +01:00
|
|
|
return tx.Copy(w)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close cleanly shuts down the database and syncs all data.
|
|
|
|
//
|
|
|
|
// This function is part of the walletdb.Db interface implementation.
|
|
|
|
func (db *db) Close() error {
|
2018-11-30 03:43:31 +01:00
|
|
|
return convertErr((*bbolt.DB)(db).Close())
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 13:52:44 +01:00
|
|
|
// Batch is similar to the package-level Update method, but it will attempt to
|
|
|
|
// optismitcally combine the invocation of several transaction functions into a
|
|
|
|
// single db write transaction.
|
|
|
|
//
|
|
|
|
// This function is part of the walletdb.Db interface implementation.
|
|
|
|
func (db *db) Batch(f func(tx walletdb.ReadWriteTx) error) error {
|
|
|
|
return (*bbolt.DB)(db).Batch(func(btx *bbolt.Tx) error {
|
|
|
|
interfaceTx := transaction{btx}
|
|
|
|
|
|
|
|
return f(&interfaceTx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-10 01:31:38 +01:00
|
|
|
// filesExists reports whether the named file or directory exists.
|
|
|
|
func fileExists(name string) bool {
|
|
|
|
if _, err := os.Stat(name); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// openDB opens the database at the provided path. walletdb.ErrDbDoesNotExist
|
|
|
|
// is returned if the database doesn't exist and the create flag is not set.
|
2020-08-11 14:15:46 +02:00
|
|
|
func openDB(dbPath string, noFreelistSync bool,
|
|
|
|
create bool, timeout time.Duration) (walletdb.DB, error) {
|
|
|
|
|
2014-11-10 01:31:38 +01:00
|
|
|
if !create && !fileExists(dbPath) {
|
|
|
|
return nil, walletdb.ErrDbDoesNotExist
|
|
|
|
}
|
|
|
|
|
2019-08-02 04:15:53 +02:00
|
|
|
// Specify bbolt freelist options to reduce heap pressure in case the
|
|
|
|
// freelist grows to be very large.
|
|
|
|
options := &bbolt.Options{
|
2019-10-02 19:14:59 +02:00
|
|
|
NoFreelistSync: noFreelistSync,
|
2019-08-02 04:15:53 +02:00
|
|
|
FreelistType: bbolt.FreelistMapType,
|
2020-08-11 14:15:46 +02:00
|
|
|
Timeout: timeout,
|
2019-08-02 04:15:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
boltDB, err := bbolt.Open(dbPath, 0600, options)
|
2014-11-10 01:31:38 +01:00
|
|
|
return (*db)(boltDB), convertErr(err)
|
|
|
|
}
|