Skip to content

Commit

Permalink
add mongodb implementation for the db package
Browse files Browse the repository at this point in the history
The db interface has now an implementation for mongodb. So instead
of pebble a user or consumer of the vocdoni-node API can choose
to use mongodb. Its not faster than local KV, but useful when the host
runing the database has no persistent storage available, so a remote
storage is required.

The mongodb url must be set on a env var, such as:

MONGODB_URL="mongodb://root:vocdoni@localhost:27017/" go run ./cmd/node -t mongodb -c dev

Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Sep 8, 2023
1 parent 8ce1665 commit ddea83d
Show file tree
Hide file tree
Showing 15 changed files with 406 additions and 27 deletions.
9 changes: 7 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"go.vocdoni.io/dvote/data"
"go.vocdoni.io/dvote/db"
"go.vocdoni.io/dvote/db/metadb"
"go.vocdoni.io/dvote/db/prefixeddb"
"go.vocdoni.io/dvote/httprouter"
"go.vocdoni.io/dvote/httprouter/apirest"
"go.vocdoni.io/dvote/vochain"
Expand Down Expand Up @@ -75,10 +76,13 @@ type API struct {
}

// NewAPI creates a new instance of the API. Attach must be called next.
func NewAPI(router *httprouter.HTTProuter, baseRoute, dataDir string) (*API, error) {
func NewAPI(router *httprouter.HTTProuter, baseRoute, dataDir, dbType string) (*API, error) {
if router == nil {
return nil, ErrHTTPRouterIsNil
}
if dbType == "" {
dbType = db.TypePebble
}
if len(baseRoute) == 0 || baseRoute[0] != '/' {
return nil, fmt.Errorf("%w (invalid given: %s)", ErrBaseRouteInvalid, baseRoute)
}
Expand All @@ -95,10 +99,11 @@ func NewAPI(router *httprouter.HTTProuter, baseRoute, dataDir string) (*API, err
if err != nil {
return nil, err
}
api.db, err = metadb.New(db.TypePebble, filepath.Join(dataDir, "db"))
mdb, err := metadb.New(dbType, filepath.Join(dataDir, "db"))
if err != nil {
return nil, err
}
api.db = prefixeddb.NewPrefixedDatabase(mdb, []byte("api/"))
return &api, nil
}

Expand Down
6 changes: 3 additions & 3 deletions api/census_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestCensus(t *testing.T) {
addr, err := url.Parse("http://" + path.Join(router.Address().String(), "censuses"))
qt.Assert(t, err, qt.IsNil)

api, err := NewAPI(&router, "/", t.TempDir())
api, err := NewAPI(&router, "/", t.TempDir(), db.TypePebble)
qt.Assert(t, err, qt.IsNil)

// Create local key value database
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestCensusProof(t *testing.T) {
addr, err := url.Parse("http://" + path.Join(router.Address().String(), "censuses"))
qt.Assert(t, err, qt.IsNil)

api, err := NewAPI(&router, "/", t.TempDir())
api, err := NewAPI(&router, "/", t.TempDir(), db.TypePebble)
qt.Assert(t, err, qt.IsNil)
// Create local key value database
db, err := metadb.New(db.TypePebble, t.TempDir())
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestCensusZk(t *testing.T) {
addr, err := url.Parse("http://" + path.Join(router.Address().String(), "censuses"))
c.Assert(err, qt.IsNil)

testApi, err := NewAPI(&router, "/", t.TempDir())
testApi, err := NewAPI(&router, "/", t.TempDir(), db.TypePebble)
c.Assert(err, qt.IsNil)

// Create local key value database
Expand Down
2 changes: 1 addition & 1 deletion benchmark/zk_census_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func BenchmarkZkCensus(b *testing.B) {
router := httprouter.HTTProuter{}
router.Init("127.0.0.1", 0)

vocApi, err := api.NewAPI(&router, "/", b.TempDir())
vocApi, err := api.NewAPI(&router, "/", b.TempDir(), db.TypePebble)
qt.Assert(b, err, qt.IsNil)

// Create local key value database
Expand Down
8 changes: 2 additions & 6 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func newConfig() (*config.Config, config.Error) {
flag.StringVarP(&globalCfg.DataDir, "dataDir", "d", home+"/.vocdoni",
"directory where data is stored")
flag.StringVarP(&globalCfg.Vochain.DBType, "dbType", "t", db.TypePebble,
fmt.Sprintf("key-value db type (%s)", db.TypePebble))
fmt.Sprintf("key-value db type [%s,%s,%s]", db.TypePebble, db.TypeLevelDB, db.TypeMongo))
flag.StringVarP(&globalCfg.Vochain.Chain, "chain", "c", "dev",
fmt.Sprintf("vocdoni blockchain to connect with: %q", genesis.AvailableChains()))
flag.BoolVar(&globalCfg.Dev, "dev", false,
Expand Down Expand Up @@ -361,10 +361,6 @@ func main() {
if !globalCfg.ValidMode() {
log.Fatalf("mode %s is invalid", globalCfg.Mode)
}
// Check the dbType is valid
if !globalCfg.Vochain.ValidDBType() {
log.Fatalf("dbType %s is invalid. Valid ones: %s", globalCfg.Vochain.DBType, db.TypePebble)
}

// If dev enabled, expose debugging profiles under an http server
// If PprofPort is not set, a random port between 61000 and 61100 is choosed.
Expand Down Expand Up @@ -531,7 +527,7 @@ func main() {
// HTTP API REST service
if globalCfg.EnableAPI {
log.Info("enabling API")
uAPI, err := urlapi.NewAPI(srv.Router, "/v2", globalCfg.DataDir)
uAPI, err := urlapi.NewAPI(srv.Router, "/v2", globalCfg.DataDir, globalCfg.Vochain.DBType)
if err != nil {
log.Fatal(err)
}
Expand Down
12 changes: 0 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"go.vocdoni.io/dvote/db"
"go.vocdoni.io/dvote/types"
)

Expand Down Expand Up @@ -61,17 +60,6 @@ func (c *Config) ValidMode() bool {
return true
}

// ValidDBType checks if the configured dbType is valid
func (c *VochainCfg) ValidDBType() bool {
switch c.DBType {
case db.TypePebble:

default:
return false
}
return true
}

// NewConfig initializes the fields in the config stuct.
func NewConfig() *Config {
return &Config{
Expand Down
1 change: 1 addition & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
const (
TypePebble = "pebble"
TypeLevelDB = "leveldb"
TypeMongo = "mongodb"
)

// ErrKeyNotFound is used to indicate that a key does not exist in the db.
Expand Down
2 changes: 2 additions & 0 deletions db/internal/dbtest/dbtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func TestWriteTx(t *testing.T, database db.Database) {
// Discard should not give any problem
wTx.Discard()

qt.Assert(t, err, qt.IsNil)

// get value from a new db after the previous commit
wTx = database.WriteTx()
v, err = wTx.Get([]byte("a"))
Expand Down
9 changes: 8 additions & 1 deletion db/metadb/metadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"go.vocdoni.io/dvote/db"
"go.vocdoni.io/dvote/db/goleveldb"
"go.vocdoni.io/dvote/db/mongodb"
"go.vocdoni.io/dvote/db/pebbledb"
)

Expand All @@ -25,8 +26,14 @@ func New(typ, dir string) (db.Database, error) {
if err != nil {
return nil, err
}
case db.TypeMongo:
database, err = mongodb.New(opts)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("invalid dbType: %q. Available types: %q", typ, db.TypePebble)
return nil, fmt.Errorf("invalid dbType: %q. Available types: %q %q %q",
typ, db.TypePebble, db.TypeLevelDB, db.TypeMongo)
}
return database, nil
}
Expand Down
22 changes: 22 additions & 0 deletions db/mongodb/docker-compose-mongo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3.1'

services:

mongo:
image: 'bitnami/mongodb:latest'
restart: ${RESTART:-no}
ports:
- 27017:27017
environment:
- MONGODB_USERNAME=root
- MONGODB_PASSWORD=vocdoni
- MONGODB_DATABASE=vocdoni
- MONGODB_ROOT_PASSWORD=vocdoni
volumes:
- mongodb:/bitnami/mongodb

# MONGODB_URL=mongodb://root:vocdoni@mongo:27017/
# MONGODB_URL=mongodb://root:[email protected]:27017/

volumes:
mongodb: {}
Loading

0 comments on commit ddea83d

Please sign in to comment.