2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2014 The btcsuite developers
|
2013-10-13 08:59:55 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package memdb
|
|
|
|
|
|
|
|
import (
|
2014-01-20 03:01:31 +01:00
|
|
|
"fmt"
|
2014-07-03 02:47:24 +02:00
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
"github.com/btcsuite/btcd/database"
|
2015-01-16 18:15:32 +01:00
|
|
|
"github.com/btcsuite/btclog"
|
2013-10-13 08:59:55 +02:00
|
|
|
)
|
|
|
|
|
2013-11-21 16:48:16 +01:00
|
|
|
var log = btclog.Disabled
|
2013-10-13 08:59:55 +02:00
|
|
|
|
|
|
|
func init() {
|
2015-01-27 18:45:10 +01:00
|
|
|
driver := database.DriverDB{DbType: "memdb", CreateDB: CreateDB, OpenDB: OpenDB}
|
|
|
|
database.AddDBDriver(driver)
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
// parseArgs parses the arguments from the database package Open/Create methods.
|
2014-01-20 03:01:31 +01:00
|
|
|
func parseArgs(funcName string, args ...interface{}) error {
|
|
|
|
if len(args) != 0 {
|
|
|
|
return fmt.Errorf("memdb.%s does not accept any arguments",
|
|
|
|
funcName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-13 08:59:55 +02:00
|
|
|
// OpenDB opens an existing database for use.
|
2015-01-27 18:45:10 +01:00
|
|
|
func OpenDB(args ...interface{}) (database.Db, error) {
|
2014-01-20 03:01:31 +01:00
|
|
|
if err := parseArgs("OpenDB", args...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-10-13 08:59:55 +02:00
|
|
|
// A memory database is not persistent, so let CreateDB handle it.
|
2014-01-20 03:01:31 +01:00
|
|
|
return CreateDB()
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDB creates, initializes, and opens a database for use.
|
2015-01-27 18:45:10 +01:00
|
|
|
func CreateDB(args ...interface{}) (database.Db, error) {
|
2014-01-20 03:01:31 +01:00
|
|
|
if err := parseArgs("CreateDB", args...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
log = database.GetLog()
|
2013-10-13 08:59:55 +02:00
|
|
|
return newMemDb(), nil
|
|
|
|
}
|