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 (
|
|
|
|
"fmt"
|
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"
|
2014-11-10 01:31:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dbType = "bdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// parseArgs parses the arguments from the walletdb Open/Create methods.
|
2020-08-11 14:15:46 +02:00
|
|
|
func parseArgs(funcName string,
|
|
|
|
args ...interface{}) (string, bool, time.Duration, error) {
|
|
|
|
|
|
|
|
if len(args) != 3 {
|
|
|
|
return "", false, 0, fmt.Errorf("invalid arguments to %s.%s "+
|
|
|
|
"-- expected database path, no-freelist-sync and "+
|
|
|
|
"timeout option",
|
2019-10-02 19:14:59 +02:00
|
|
|
dbType, funcName)
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dbPath, ok := args[0].(string)
|
|
|
|
if !ok {
|
2020-08-11 14:15:46 +02:00
|
|
|
return "", false, 0, fmt.Errorf("first argument to %s.%s is "+
|
2019-10-02 19:14:59 +02:00
|
|
|
"invalid -- expected database path string", dbType,
|
|
|
|
funcName)
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
2019-10-02 19:14:59 +02:00
|
|
|
noFreelistSync, ok := args[1].(bool)
|
|
|
|
if !ok {
|
2020-08-11 14:15:46 +02:00
|
|
|
return "", false, 0, fmt.Errorf("second argument to %s.%s is "+
|
2019-10-02 19:14:59 +02:00
|
|
|
"invalid -- expected no-freelist-sync bool", dbType,
|
|
|
|
funcName)
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:15:46 +02:00
|
|
|
timeout, ok := args[2].(time.Duration)
|
|
|
|
if !ok {
|
|
|
|
return "", false, 0, fmt.Errorf("third argument to %s.%s is "+
|
|
|
|
"invalid -- expected timeout time.Duration", dbType,
|
|
|
|
funcName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbPath, noFreelistSync, timeout, nil
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// openDBDriver is the callback provided during driver registration that opens
|
|
|
|
// an existing database for use.
|
|
|
|
func openDBDriver(args ...interface{}) (walletdb.DB, error) {
|
2020-08-11 14:15:46 +02:00
|
|
|
dbPath, noFreelistSync, timeout, err := parseArgs("Open", args...)
|
2014-11-10 01:31:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:15:46 +02:00
|
|
|
return openDB(dbPath, noFreelistSync, false, timeout)
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// createDBDriver is the callback provided during driver registration that
|
|
|
|
// creates, initializes, and opens a database for use.
|
|
|
|
func createDBDriver(args ...interface{}) (walletdb.DB, error) {
|
2020-08-11 14:15:46 +02:00
|
|
|
dbPath, noFreelistSync, timeout, err := parseArgs("Create", args...)
|
2014-11-10 01:31:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:15:46 +02:00
|
|
|
return openDB(dbPath, noFreelistSync, true, timeout)
|
2014-11-10 01:31:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Register the driver.
|
|
|
|
driver := walletdb.Driver{
|
|
|
|
DbType: dbType,
|
|
|
|
Create: createDBDriver,
|
|
|
|
Open: openDBDriver,
|
|
|
|
}
|
|
|
|
if err := walletdb.RegisterDriver(driver); err != nil {
|
|
|
|
panic(fmt.Sprintf("Failed to regiser database driver '%s': %v",
|
|
|
|
dbType, err))
|
|
|
|
}
|
|
|
|
}
|