Skip to content

Commit

Permalink
dispatching db constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
roysc committed Apr 7, 2022
1 parent 138bd43 commit 305e802
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 10 deletions.
8 changes: 8 additions & 0 deletions db/badgerdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ type versionManager struct {
lastTs uint64
}

func init() {
creator := func(name string, dir string) (db.DBConnection, error) {
dir = filepath.Join(dir, name)
return NewDB(dir)
}
db.RegisterCreator(db.BadgerDBBackend, creator, false)
}

// NewDB creates or loads a BadgerDB key-value database inside the given directory.
// If dir does not exist, it will be created.
func NewDB(dir string) (*BadgerDB, error) {
Expand Down
55 changes: 55 additions & 0 deletions db/creator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package db

import (
"fmt"
"strings"
)

type BackendType string

// These are valid backend types.
const (
// MemDBBackend represents in-memory key value store, which is mostly used
// for testing.
MemDBBackend BackendType = "memdb"
// RocksDBBackend represents rocksdb (uses github.com/cosmos/gorocksdb)
// - EXPERIMENTAL
// - requires gcc
// - use rocksdb build tag (go build -tags rocksdb)
RocksDBBackend BackendType = "rocksdb"
// BadgerDBBackend represents BadgerDB
// - pure Go
// - requires badgerdb build tag
BadgerDBBackend BackendType = "badgerdb"
)

type DBCreator func(name string, dir string) (DBConnection, error)

var backends = map[BackendType]DBCreator{}

func RegisterCreator(backend BackendType, creator DBCreator, force bool) {
_, ok := backends[backend]
if !force && ok {
return
}
backends[backend] = creator
}

// NewDB creates a new database of type backend with the given name.
func NewDB(name string, backend BackendType, dir string) (DBConnection, error) {
creator, ok := backends[backend]
if !ok {
keys := make([]string, 0, len(backends))
for k := range backends {
keys = append(keys, string(k))
}
return nil, fmt.Errorf("unknown db_backend %s, expected one of %v",
backend, strings.Join(keys, ","))
}

db, err := creator(name, dir)
if err != nil {
return nil, fmt.Errorf("failed to initialize database: %w", err)
}
return db, nil
}
7 changes: 7 additions & 0 deletions db/memdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ const (
bTreeDegree = 32
)

func init() {
creator := func(name string, dir string) (db.DBConnection, error) {
return NewDB(), nil
}
db.RegisterCreator(db.MemDBBackend, creator, false)
}

// MemDB is an in-memory database backend using a B-tree for storage.
//
// For performance reasons, all given and returned keys and values are pointers to the in-memory
Expand Down
8 changes: 8 additions & 0 deletions db/rocksdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ type dbOptions struct {
wo *gorocksdb.WriteOptions
}

func init() {
creator := func(name string, dir string) (DBConnection, error) {
dir = filepath.Join(dir, name)
return NewDB(dir)
}
db.RegisterCreator(db.RocksDBBackend, creator, false)
}

// NewDB creates a new RocksDB key-value database with inside the given directory.
// If dir does not exist, it will be created.
func NewDB(dir string) (*dbManager, error) {
Expand Down
4 changes: 2 additions & 2 deletions server/constructors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (

"github.com/stretchr/testify/require"

dbm "github.com/tendermint/tm-db"
dbm "github.com/cosmos/cosmos-sdk/db"
)

func Test_openDB(t *testing.T) {
t.Parallel()
_, err := openDB(t.TempDir(), dbm.GoLevelDBBackend)
_, err := openDB(t.TempDir(), dbm.BadgerDBBackend)
require.NoError(t, err)
}

Expand Down
1 change: 0 additions & 1 deletion server/mock/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"

bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down
9 changes: 4 additions & 5 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/cosmos/cosmos-sdk/client/flags"
dbm "github.com/cosmos/cosmos-sdk/db"
"github.com/cosmos/cosmos-sdk/db/badgerdb"
"github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -365,7 +364,7 @@ func GetAppDBBackend(opts types.AppOptions) dbm.BackendType {
if len(rv) != 0 {
return dbm.BackendType(rv)
}
return dbm.GoLevelDBBackend
return dbm.BadgerDBBackend
}

func skipInterface(iface net.Interface) bool {
Expand All @@ -392,9 +391,9 @@ func addrToIP(addr net.Addr) net.IP {
return ip
}

func openDB(rootDir string) (dbm.DBConnection, error) {
dir := filepath.Join(rootDir, "data", "application")
return badgerdb.NewDB(dir)
func openDB(rootDir string, backendType dbm.BackendType) (dbm.DBConnection, error) {
dataDir := filepath.Join(rootDir, "data")
return dbm.NewDB("application", backendType, dataDir)
}

func openTraceWriter(traceWriterFile string) (w io.Writer, err error) {
Expand Down
4 changes: 2 additions & 2 deletions server/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tmcfg "github.com/tendermint/tendermint/config"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
dbm "github.com/cosmos/cosmos-sdk/db"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
Expand Down Expand Up @@ -466,7 +466,7 @@ func TestGetAppDBBackend(t *testing.T) {
name: "nothing set",
dbBack: "",
opts: mapGetter{},
exp: dbm.GoLevelDBBackend,
exp: dbm.BadgerDBBackend,
},

{
Expand Down

0 comments on commit 305e802

Please sign in to comment.