mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-13 17:46:39 +00:00
Allow config file and command line to select between sqlite and levdldb
This commit is contained in:
parent
33b65f943f
commit
af7aa39624
@ -8,7 +8,6 @@ import (
|
|||||||
"container/list"
|
"container/list"
|
||||||
"github.com/conformal/btcchain"
|
"github.com/conformal/btcchain"
|
||||||
"github.com/conformal/btcdb"
|
"github.com/conformal/btcdb"
|
||||||
_ "github.com/conformal/btcdb/sqlite3"
|
|
||||||
"github.com/conformal/btcutil"
|
"github.com/conformal/btcutil"
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
"os"
|
"os"
|
||||||
@ -397,7 +396,7 @@ func newBlockManager(s *server) *blockManager {
|
|||||||
func loadBlockDB() (btcdb.Db, error) {
|
func loadBlockDB() (btcdb.Db, error) {
|
||||||
dbPath := filepath.Join(cfg.DbDir, activeNetParams.dbName)
|
dbPath := filepath.Join(cfg.DbDir, activeNetParams.dbName)
|
||||||
log.Infof("[BMGR] Loading block database from '%s'", dbPath)
|
log.Infof("[BMGR] Loading block database from '%s'", dbPath)
|
||||||
db, err := btcdb.OpenDB("sqlite", dbPath)
|
db, err := btcdb.OpenDB(cfg.DbType, dbPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Return the error if it's not because the database doesn't
|
// Return the error if it's not because the database doesn't
|
||||||
// exist.
|
// exist.
|
||||||
@ -410,7 +409,7 @@ func loadBlockDB() (btcdb.Db, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
db, err = btcdb.CreateDB("sqlite", dbPath)
|
db, err = btcdb.CreateDB(cfg.DbType, dbPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
41
config.go
41
config.go
@ -9,6 +9,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
"github.com/conformal/go-flags"
|
"github.com/conformal/go-flags"
|
||||||
|
_ "github.com/conformal/btcdb/sqlite3"
|
||||||
|
_ "github.com/conformal/btcdb/ldb"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -56,6 +58,7 @@ type config struct {
|
|||||||
UseTor bool `long:"tor" description:"Specifies the proxy server used is a Tor node"`
|
UseTor bool `long:"tor" description:"Specifies the proxy server used is a Tor node"`
|
||||||
TestNet3 bool `long:"testnet" description:"Use the test network"`
|
TestNet3 bool `long:"testnet" description:"Use the test network"`
|
||||||
RegressionTest bool `long:"regtest" description:"Use the regression test network"`
|
RegressionTest bool `long:"regtest" description:"Use the regression test network"`
|
||||||
|
DbType string `long:"dbtype" description:"DB backend to use for Block Chain"`
|
||||||
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"`
|
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -303,5 +306,43 @@ func loadConfig() (*config, []string, error) {
|
|||||||
cfg.ConnectPeers =
|
cfg.ConnectPeers =
|
||||||
normalizeAndRemoveDuplicateAddresses(cfg.ConnectPeers)
|
normalizeAndRemoveDuplicateAddresses(cfg.ConnectPeers)
|
||||||
|
|
||||||
|
|
||||||
|
// determine which database backend to use
|
||||||
|
// would be interesting of btcdb had a 'supportedDBs() []string'
|
||||||
|
// API to populate this field.
|
||||||
|
knownDbs := []string { "leveldb", "sqlite" }
|
||||||
|
defaultDb := "sqlite"
|
||||||
|
|
||||||
|
if len(cfg.DbType) == 0 {
|
||||||
|
// if db was not specified, use heuristic to see what
|
||||||
|
// type the database is (if the specified database is a
|
||||||
|
// file then it is sqlite3, if it is a directory assume
|
||||||
|
// it is leveldb, if not found default to type _____
|
||||||
|
dbPath := filepath.Join(cfg.DbDir, activeNetParams.dbName)
|
||||||
|
|
||||||
|
fi, err := os.Stat(dbPath)
|
||||||
|
if err == nil {
|
||||||
|
if fi.IsDir() {
|
||||||
|
cfg.DbType = "leveldb"
|
||||||
|
} else {
|
||||||
|
cfg.DbType = "sqlite"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cfg.DbType = defaultDb
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// does checking this here really make sense ?
|
||||||
|
typeVerified := false
|
||||||
|
for _, dbtype := range knownDbs {
|
||||||
|
if cfg.DbType == dbtype {
|
||||||
|
typeVerified = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if typeVerified == false {
|
||||||
|
err := fmt.Errorf("Specified database type [%v] not in list of supported databases: %v", cfg.DbType, knownDbs)
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
return &cfg, remainingArgs, nil
|
return &cfg, remainingArgs, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user