2014-11-10 01:31:38 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Conformal Systems LLC <info@conformal.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package bdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2015-01-17 07:25:53 +01: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.
|
|
|
|
func parseArgs(funcName string, args ...interface{}) (string, error) {
|
|
|
|
if len(args) != 1 {
|
|
|
|
return "", fmt.Errorf("invalid arguments to %s.%s -- "+
|
|
|
|
"expected database path", dbType, funcName)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbPath, ok := args[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("first argument to %s.%s is invalid -- "+
|
|
|
|
"expected database path string", dbType, funcName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbPath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// openDBDriver is the callback provided during driver registration that opens
|
|
|
|
// an existing database for use.
|
|
|
|
func openDBDriver(args ...interface{}) (walletdb.DB, error) {
|
|
|
|
dbPath, err := parseArgs("Open", args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return openDB(dbPath, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// createDBDriver is the callback provided during driver registration that
|
|
|
|
// creates, initializes, and opens a database for use.
|
|
|
|
func createDBDriver(args ...interface{}) (walletdb.DB, error) {
|
|
|
|
dbPath, err := parseArgs("Create", args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return openDB(dbPath, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|