Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove dependency on concrete datastore implementations from db package #51

Merged
merged 5 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions cli/defradb/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,37 @@
package cmd

import (
"github.com/sourcenetwork/defradb/db"
badgerds "github.com/ipfs/go-ds-badger"
)

type Config struct {
Database db.Options
Database Options
}

type DatabaseConfig struct {
URL string
storage string
// badger
type Options struct {
Address string
Store string
Memory MemoryOptions
Badger BadgerOptions
}

// BadgerOptions for the badger instance of the backing datastore
type BadgerOptions struct {
Path string
*badgerds.Options
}

// MemoryOptions for the memory instance of the backing datastore
type MemoryOptions struct {
Size uint64
}

var (
defaultConfig = Config{
Database: db.Options{
Database: Options{
Address: "localhost:9181",
Store: "badger",
Badger: db.BadgerOptions{
Badger: BadgerOptions{
Path: "$HOME/.defradb/data",
},
},
Expand Down
23 changes: 21 additions & 2 deletions cli/defradb/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

"github.com/sourcenetwork/defradb/db"

ds "github.com/ipfs/go-datastore"
badgerds "github.com/ipfs/go-ds-badger"
"github.com/spf13/cobra"
)

Expand All @@ -32,7 +34,24 @@ var startCmd = &cobra.Command{
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt)

db, err := db.NewDB(&config.Database)
var rootstore ds.Batching
var options interface{}
var err error
if config.Database.Store == "badger" {
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()
options = config.Database.Memory
}

db, err := db.NewDB(rootstore, options)
if err != nil {
log.Error("Failed to initiate database:", err)
os.Exit(1)
Expand All @@ -45,7 +64,7 @@ var startCmd = &cobra.Command{

// run the server listener in a seperate goroutine
go func() {
db.Listen()
db.Listen(config.Database.Address)
}()

// capture the interrupt signal, and gracefully exit
Expand Down
6 changes: 3 additions & 3 deletions db/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
api "github.com/sourcenetwork/defradb/api/http"
)

func (db *DB) Listen() {
db.log.Infof("Running HTTP API at http://%s. Try it out at > curl http://%s/graphql", db.options.Address, db.options.Address)
func (db *DB) Listen(address string) {
db.log.Infof("Running HTTP API at http://%s. Try it out at > curl http://%s/graphql", address, address)

s := api.NewServer(db)
s.Listen(db.options.Address)
s.Listen(address)
}

// func (db *DB) handlePing(w http.ResponseWriter, r *http.Request) {
Expand Down
50 changes: 5 additions & 45 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/ipfs/go-datastore/namespace"
"github.com/ipfs/go-datastore/query"
dsq "github.com/ipfs/go-datastore/query"
badgerds "github.com/ipfs/go-ds-badger"
logging "github.com/ipfs/go-log/v2"
)

Expand Down Expand Up @@ -81,46 +80,12 @@ type DB struct {

log logging.StandardLogger

options *Options
}

// Options for database
type Options struct {
Store string
Memory MemoryOptions
Badger BadgerOptions
Address string
}

// BadgerOptions for the badger instance of the backing datastore
type BadgerOptions struct {
Path string
*badgerds.Options
}

// MemoryOptions for the memory instance of the backing datastore
type MemoryOptions struct {
Size uint64
// The options used to init the database
options interface{}
}

// NewDB creates a new instance of the DB using the given options
func NewDB(options *Options) (*DB, error) {
var rootstore ds.Batching
var err error
if options == nil {
return nil, ErrOptionsEmpty
}
if options.Store == "badger" {
log.Info("opening badger store: ", options.Badger.Path)
rootstore, err = badgerds.NewDatastore(options.Badger.Path, options.Badger.Options)
if err != nil {
return nil, err
}
} else if options.Store == "memory" {
log.Info("building new memory store")
rootstore = ds.NewMapDatastore()
}

func NewDB(rootstore ds.Batching, options interface{}) (*DB, error) {
log.Debug("loading: internal datastores")
systemstore := namespace.Wrap(rootstore, systemStoreKey)
datastore := namespace.Wrap(rootstore, dataStoreKey)
Expand Down Expand Up @@ -161,8 +126,7 @@ func NewDB(options *Options) (*DB, error) {

schema: sm,
queryExecutor: exec,

options: options,
options: options,
}

return db, err
Expand Down Expand Up @@ -226,11 +190,7 @@ func (db *DB) PrintDump(ctx context.Context) {
// of resources (IE: Badger instance)
func (db *DB) Close() {
log.Info("Closing DefraDB process...")
if db.options.Store == "badger" {
if db.rootstore != nil {
db.rootstore.Close()
}
}
db.rootstore.Close()
log.Info("Succesfully closed running process")
}

Expand Down
32 changes: 7 additions & 25 deletions db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,14 @@ import (
"github.com/sourcenetwork/defradb/document/key"
"github.com/sourcenetwork/defradb/merkle/clock"

ds "github.com/ipfs/go-datastore"
dag "github.com/ipfs/go-merkledag"
"github.com/stretchr/testify/assert"
)

func newMemoryDB() (*DB, error) {
opts := &Options{
Store: "memory",
Memory: MemoryOptions{
Size: 1024 * 1000,
},
Badger: BadgerOptions{
Path: "test",
},
}

return NewDB(opts)
rootstore := ds.NewMapDatastore()
return NewDB(rootstore, struct{}{})
}

func newTestCollection(ctx context.Context, db *DB) (*Collection, error) {
Expand All @@ -47,29 +39,19 @@ func newTestCollection(ctx context.Context, db *DB) (*Collection, error) {
}

func TestNewDB(t *testing.T) {
opts := &Options{
Store: "memory",
Memory: MemoryOptions{
Size: 1024 * 1000,
},
}
rootstore := ds.NewMapDatastore()

_, err := NewDB(opts)
_, err := NewDB(rootstore, struct{}{})
if err != nil {
t.Error(err)
}
}

func TestNewDBWithCollection_Errors_GivenNoSchema(t *testing.T) {
ctx := context.Background()
opts := &Options{
Store: "memory",
Memory: MemoryOptions{
Size: 1024 * 1000,
},
}
rootstore := ds.NewMapDatastore()

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

ds "github.com/ipfs/go-datastore"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/db"
"github.com/sourcenetwork/defradb/document"
Expand All @@ -33,17 +34,8 @@ type QueryTestCase struct {
}

func NewMemoryDB() (*db.DB, error) {
opts := &db.Options{
Store: "memory",
Memory: db.MemoryOptions{
Size: 1024 * 1000,
},
Badger: db.BadgerOptions{
Path: "test",
},
}

return db.NewDB(opts)
rootstore := ds.NewMapDatastore()
return db.NewDB(rootstore, struct{}{})
}

func ExecuteQueryTestCase(t *testing.T, schema string, collectionNames []string, test QueryTestCase) {
Expand Down
10 changes: 0 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,27 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/fxamacker/cbor/v2 v2.2.0
github.com/go-chi/chi v1.5.2
github.com/gogo/protobuf v1.3.2
github.com/graphql-go/graphql v0.7.9
github.com/ipfs/go-block-format v0.0.3
github.com/ipfs/go-blockservice v0.2.0
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-datastore v0.5.0
github.com/ipfs/go-ds-badger v0.3.0
github.com/ipfs/go-ipfs-blockstore v1.1.0
github.com/ipfs/go-ipfs-ds-help v1.1.0
github.com/ipfs/go-ipfs-exchange-offline v0.1.0
github.com/ipfs/go-ipld-format v0.2.0
github.com/ipfs/go-log v1.0.5
github.com/ipfs/go-log/v2 v2.3.0
github.com/ipfs/go-merkledag v0.5.0
github.com/jbenet/goprocess v0.1.4
github.com/kr/text v0.2.0 // indirect
github.com/libp2p/go-testutil v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/multiformats/go-multibase v0.0.3
github.com/multiformats/go-multihash v0.0.15
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pkg/errors v0.9.1
github.com/satori/go.uuid v1.2.0
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.0
github.com/stretchr/testify v1.7.0
github.com/ugorji/go/codec v1.1.7
github.com/whyrusleeping/go-logging v0.0.1
github.com/whyrusleeping/go-notifier v0.0.0-20170827234753-097c5d47330f // indirect
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
Loading