From f12ca203721c5d8942c78ab05bc16803f4f59a22 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sat, 26 Oct 2013 23:10:27 -0500 Subject: [PATCH] Enable memdb support. This commit adds the btcdb memdb backend as a supported database type. Note that users will NOT want to run in this mode because, being memory only, it obviously does not persist the database when shutdown. It is being added for testing purposes to help prevent constant abuse to developer's hard drive when churning the block database multiple times a day. --- blockmanager.go | 34 ++++++++++++++++++++++++++++++---- config.go | 1 + 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/blockmanager.go b/blockmanager.go index f5db49eb5..e085fe627 100644 --- a/blockmanager.go +++ b/blockmanager.go @@ -975,8 +975,24 @@ func warnMultipeDBs() { } } -// loadBlockDB opens the block database and returns a handle to it. -func loadBlockDB() (btcdb.Db, error) { +// setupBlockDB loads (or creates when needed) the block database taking into +// account the selected database backend. It also contains additional logic +// such warning the user if there are multiple databases which consume space on +// the file system and ensuring the regression test database is clean when in +// regression test mode. +func setupBlockDB() (btcdb.Db, error) { + // The memdb backend does not have a file path associated with it, so + // handle it uniquely. We also don't want to worry about the multiple + // database type warnings when running with the memory database. + if cfg.DbType == "memdb" { + btcdLog.Infof("Creating block database in memory.") + db, err := btcdb.CreateDB(cfg.DbType) + if err != nil { + return nil, err + } + return db, nil + } + warnMultipeDBs() // The database name is based on the database type. @@ -989,8 +1005,8 @@ func loadBlockDB() (btcdb.Db, error) { btcdLog.Infof("Loading block database from '%s'", dbPath) db, err := btcdb.OpenDB(cfg.DbType, dbPath) if err != nil { - // Return the error if it's not because the database doesn't - // exist. + // Return the error if it's not because the database + // doesn't exist. if err != btcdb.DbDoesNotExist { return nil, err } @@ -1006,6 +1022,16 @@ func loadBlockDB() (btcdb.Db, error) { } } + return db, nil +} + +// loadBlockDB opens the block database and returns a handle to it. +func loadBlockDB() (btcdb.Db, error) { + db, err := setupBlockDB() + if err != nil { + return nil, err + } + // Get the latest block height from the database. _, height, err := db.NewestSha() if err != nil { diff --git a/config.go b/config.go index ff077905a..a1b11b694 100644 --- a/config.go +++ b/config.go @@ -9,6 +9,7 @@ import ( "fmt" "github.com/conformal/btcdb" _ "github.com/conformal/btcdb/ldb" + _ "github.com/conformal/btcdb/memdb" "github.com/conformal/btcutil" "github.com/conformal/btcwire" "github.com/conformal/go-flags"