Skip to content

Commit

Permalink
Replace MapDatastore with badger InMemory store
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Nov 23, 2021
1 parent b30761b commit 53c1aa4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
13 changes: 8 additions & 5 deletions cli/defradb/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/sourcenetwork/defradb/db"

badger "github.com/dgraph-io/badger/v3"
ds "github.com/ipfs/go-datastore"
badgerds "github.com/sourcenetwork/defradb/datastores/badger/v3"
"github.com/spf13/cobra"
Expand All @@ -41,16 +42,18 @@ var startCmd = &cobra.Command{
log.Info("opening badger store: ", config.Database.Badger.Path)
rootstore, err = badgerds.NewDatastore(config.Database.Badger.Path, config.Database.Badger.Options)
options = config.Database.Badger
if err != nil {
log.Error("Failed to initiate database:", err)
os.Exit(1)
}
} else if config.Database.Store == "memory" {
log.Info("building new memory store")
rootstore = ds.NewMapDatastore()
opts := badgerds.Options{Options: badger.DefaultOptions("").WithInMemory(true)}
rootstore, err = badgerds.NewDatastore("", &opts)
options = config.Database.Memory
}

if err != nil {
log.Error("Failed to initiate datastore:", err)
os.Exit(1)
}

db, err := db.NewDB(rootstore, options)
if err != nil {
log.Error("Failed to initiate database:", err)
Expand Down
17 changes: 14 additions & 3 deletions db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ import (
"github.com/sourcenetwork/defradb/document/key"
"github.com/sourcenetwork/defradb/merkle/clock"

badger "github.com/dgraph-io/badger/v3"
ds "github.com/ipfs/go-datastore"
dag "github.com/ipfs/go-merkledag"
badgerds "github.com/sourcenetwork/defradb/datastores/badger/v3"
"github.com/stretchr/testify/assert"
)

func newMemoryDB() (*DB, error) {
rootstore := ds.NewMapDatastore()
opts := badgerds.Options{Options: badger.DefaultOptions("").WithInMemory(true)}
rootstore, err := badgerds.NewDatastore("", &opts)
if err != nil {
return nil, err
}
return NewDB(rootstore, struct{}{})
}

Expand All @@ -39,9 +45,14 @@ func newTestCollection(ctx context.Context, db *DB) (*Collection, error) {
}

func TestNewDB(t *testing.T) {
rootstore := ds.NewMapDatastore()
opts := badgerds.Options{Options: badger.DefaultOptions("").WithInMemory(true)}
rootstore, err := badgerds.NewDatastore("", &opts)
if err != nil {
t.Error(err)
return
}

_, err := NewDB(rootstore, struct{}{})
_, err = NewDB(rootstore, struct{}{})
if err != nil {
t.Error(err)
}
Expand Down
10 changes: 8 additions & 2 deletions db/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
"fmt"
"testing"

ds "github.com/ipfs/go-datastore"
badger "github.com/dgraph-io/badger/v3"
"github.com/sourcenetwork/defradb/client"
badgerds "github.com/sourcenetwork/defradb/datastores/badger/v3"
"github.com/sourcenetwork/defradb/db"
"github.com/sourcenetwork/defradb/document"
"github.com/stretchr/testify/assert"
Expand All @@ -34,7 +35,12 @@ type QueryTestCase struct {
}

func NewMemoryDB() (*db.DB, error) {
rootstore := ds.NewMapDatastore()
opts := badgerds.Options{Options: badger.DefaultOptions("").WithInMemory(true)}
rootstore, err := badgerds.NewDatastore("", &opts)
if err != nil {
return nil, err
}

return db.NewDB(rootstore, struct{}{})
}

Expand Down

0 comments on commit 53c1aa4

Please sign in to comment.