diff --git a/cli/server_dump.go b/cli/server_dump.go index b311f04ec1..1d3c68e54a 100644 --- a/cli/server_dump.go +++ b/cli/server_dump.go @@ -33,7 +33,7 @@ func MakeServerDumpCmd() *cobra.Command { storeOpts := []node.StoreOpt{ node.WithPath(cfg.GetString("datastore.badger.path")), } - rootstore, err := node.NewStore(storeOpts...) + rootstore, err := node.NewStore(cmd.Context(), storeOpts...) if err != nil { return err } diff --git a/node/node.go b/node/node.go index 40bf572f65..1293468855 100644 --- a/node/node.go +++ b/node/node.go @@ -116,7 +116,8 @@ func NewNode(ctx context.Context, opts ...NodeOpt) (*Node, error) { for _, opt := range opts { opt(options) } - rootstore, err := NewStore(options.storeOpts...) + + rootstore, err := NewStore(ctx, options.storeOpts...) if err != nil { return nil, err } diff --git a/node/store.go b/node/store.go index 8e15b7aab1..8354c0f7df 100644 --- a/node/store.go +++ b/node/store.go @@ -11,14 +11,18 @@ package node import ( + "context" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/datastore/badger/v4" + "github.com/sourcenetwork/defradb/datastore/memory" ) // StoreOptions contains store configuration values. type StoreOptions struct { path string inMemory bool + defraStore bool valueLogFileSize int64 encryptionKey []byte } @@ -41,6 +45,16 @@ func WithInMemory(inMemory bool) StoreOpt { } } +// WithDefraStore sets the defra store flag. +// +// Setting this to true will result in the defra node being created with +// the a custom defra implementation of the rootstore instead of badger. +func WithDefraStore(defraStore bool) StoreOpt { + return func(o *StoreOptions) { + o.defraStore = defraStore + } +} + // WithPath sets the datastore path. func WithPath(path string) StoreOpt { return func(o *StoreOptions) { @@ -63,12 +77,16 @@ func WithEncryptionKey(encryptionKey []byte) StoreOpt { } // NewStore returns a new store with the given options. -func NewStore(opts ...StoreOpt) (datastore.RootStore, error) { +func NewStore(ctx context.Context, opts ...StoreOpt) (datastore.RootStore, error) { options := DefaultStoreOptions() for _, opt := range opts { opt(options) } + if options.defraStore { + return memory.NewDatastore(ctx), nil + } + badgerOpts := badger.DefaultOptions badgerOpts.InMemory = options.inMemory badgerOpts.ValueLogFileSize = options.valueLogFileSize diff --git a/tests/bench/bench_util.go b/tests/bench/bench_util.go index 186dbc0f3e..5993ee50f8 100644 --- a/tests/bench/bench_util.go +++ b/tests/bench/bench_util.go @@ -227,7 +227,7 @@ func newBenchStoreInfo(ctx context.Context, t testing.TB) (client.DB, error) { case "memory": db, err = testutils.NewBadgerMemoryDB(ctx) case "badger": - db, _, err = testutils.NewBadgerFileDB(ctx, t) + db, err = testutils.NewBadgerFileDB(ctx, t) default: return nil, errors.New(fmt.Sprintf("invalid storage engine backend: %s", storage)) } diff --git a/tests/integration/db.go b/tests/integration/db.go index cca6f2f263..329fdeffdd 100644 --- a/tests/integration/db.go +++ b/tests/integration/db.go @@ -17,12 +17,8 @@ import ( "strconv" "testing" - badger "github.com/sourcenetwork/badger/v4" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/crypto" - badgerds "github.com/sourcenetwork/defradb/datastore/badger/v4" - "github.com/sourcenetwork/defradb/datastore/memory" "github.com/sourcenetwork/defradb/internal/db" "github.com/sourcenetwork/defradb/node" changeDetector "github.com/sourcenetwork/defradb/tests/change_detector" @@ -74,93 +70,46 @@ func init() { } } -func NewBadgerMemoryDB(ctx context.Context, dbopts ...db.Option) (client.DB, error) { - opts := badgerds.Options{ - Options: badger.DefaultOptions("").WithInMemory(true), - } - if encryptionKey != nil { - opts.Options.EncryptionKey = encryptionKey - opts.Options.IndexCacheSize = 100 << 20 - } - rootstore, err := badgerds.NewDatastore("", &opts) - if err != nil { - return nil, err - } - - acp, err := node.NewACP(ctx) - if err != nil { - return nil, err - } - - db, err := db.NewDB(ctx, rootstore, acp, dbopts...) - if err != nil { - return nil, err +func NewBadgerMemoryDB(ctx context.Context) (client.DB, error) { + opts := []node.NodeOpt{ + node.WithStoreOpts(node.WithInMemory(true)), + node.WithDatabaseOpts(db.WithUpdateEvents()), } - return db, nil -} -func NewInMemoryDB(ctx context.Context, dbopts ...db.Option) (client.DB, error) { - acp, err := node.NewACP(ctx) + node, err := node.NewNode(ctx, opts...) if err != nil { return nil, err } - db, err := db.NewDB(ctx, memory.NewDatastore(ctx), acp, dbopts...) - if err != nil { - return nil, err - } - return db, nil + return node.DB, err } -func NewBadgerFileDB(ctx context.Context, t testing.TB, dbopts ...db.Option) (client.DB, string, error) { - var dbPath string - switch { - case databaseDir != "": - // restarting database - dbPath = databaseDir - - case changeDetector.Enabled: - // change detector - dbPath = changeDetector.DatabaseDir(t) - - default: - // default test case - dbPath = t.TempDir() - } - - opts := &badgerds.Options{ - Options: badger.DefaultOptions(dbPath), - } - if encryptionKey != nil { - opts.Options.EncryptionKey = encryptionKey - opts.Options.IndexCacheSize = 100 << 20 - } - rootstore, err := badgerds.NewDatastore(dbPath, opts) - if err != nil { - return nil, "", err - } +func NewBadgerFileDB(ctx context.Context, t testing.TB) (client.DB, error) { + path := t.TempDir() - acp, err := node.NewACP(ctx, node.WithACPPath(dbPath)) - if err != nil { - return nil, "", err + opts := []node.NodeOpt{ + node.WithStoreOpts(node.WithPath(path)), } - db, err := db.NewDB(ctx, rootstore, acp, dbopts...) + node, err := node.NewNode(ctx, opts...) if err != nil { - return nil, "", err + return nil, err } - return db, dbPath, err + return node.DB, err } // setupDatabase returns the database implementation for the current // testing state. The database type on the test state is used to // select the datastore implementation to use. -func setupDatabase(s *state) (impl client.DB, path string, err error) { - dbopts := []db.Option{ +func setupDatabase(s *state) (client.DB, string, error) { + dbOpts := []db.Option{ db.WithUpdateEvents(), db.WithLensPoolSize(lensPoolSize), } + storeOpts := []node.StoreOpt{} + acpOpts := []node.ACPOpt{} + opts := []node.NodeOpt{} if badgerEncryption && encryptionKey == nil { key, err := crypto.GenerateAES256() @@ -170,22 +119,48 @@ func setupDatabase(s *state) (impl client.DB, path string, err error) { encryptionKey = key } + if encryptionKey != nil { + storeOpts = append(storeOpts, node.WithEncryptionKey(encryptionKey)) + } + + var path string switch s.dbt { case badgerIMType: - impl, err = NewBadgerMemoryDB(s.ctx, dbopts...) + storeOpts = append(storeOpts, node.WithInMemory(true)) case badgerFileType: - impl, path, err = NewBadgerFileDB(s.ctx, s.t, dbopts...) + switch { + case databaseDir != "": + // restarting database + path = databaseDir + + case changeDetector.Enabled: + // change detector + path = changeDetector.DatabaseDir(s.t) + + default: + // default test case + path = s.t.TempDir() + } + + storeOpts = append(storeOpts, node.WithPath(path)) + acpOpts = append(acpOpts, node.WithACPPath(path)) case defraIMType: - impl, err = NewInMemoryDB(s.ctx, dbopts...) + storeOpts = append(storeOpts, node.WithDefraStore(true)) default: - err = fmt.Errorf("invalid database type: %v", s.dbt) + return nil, "", fmt.Errorf("invalid database type: %v", s.dbt) } + opts = append(opts, node.WithDatabaseOpts(dbOpts...)) + opts = append(opts, node.WithStoreOpts(storeOpts...)) + opts = append(opts, node.WithACPOpts(acpOpts...)) + + node, err := node.NewNode(s.ctx, opts...) if err != nil { return nil, "", err } - return + + return node.DB, path, nil } diff --git a/tests/integration/events/utils.go b/tests/integration/events/utils.go index d19a96a052..eb514bce2b 100644 --- a/tests/integration/events/utils.go +++ b/tests/integration/events/utils.go @@ -20,7 +20,6 @@ import ( "github.com/stretchr/testify/require" "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/internal/db" testUtils "github.com/sourcenetwork/defradb/tests/integration" ) @@ -70,7 +69,7 @@ func ExecuteRequestTestCase( ) { ctx := context.Background() - db, err := testUtils.NewBadgerMemoryDB(ctx, db.WithUpdateEvents()) + db, err := testUtils.NewBadgerMemoryDB(ctx) require.NoError(t, err) _, err = db.AddSchema(ctx, schema) diff --git a/tests/integration/net/order/utils.go b/tests/integration/net/order/utils.go index adac8c7535..c7075dae22 100644 --- a/tests/integration/net/order/utils.go +++ b/tests/integration/net/order/utils.go @@ -21,7 +21,6 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/errors" - coreDB "github.com/sourcenetwork/defradb/internal/db" "github.com/sourcenetwork/defradb/net" netutils "github.com/sourcenetwork/defradb/net/utils" testutils "github.com/sourcenetwork/defradb/tests/integration" @@ -81,7 +80,7 @@ func setupDefraNode( ctx := context.Background() log.InfoContext(ctx, "Building new memory store") - db, err := testutils.NewBadgerMemoryDB(ctx, coreDB.WithUpdateEvents()) + db, err := testutils.NewBadgerMemoryDB(ctx) if err != nil { return nil, nil, err }