diff --git a/cli/server_dump.go b/cli/server_dump.go index 9008c81730..d92a52e205 100644 --- a/cli/server_dump.go +++ b/cli/server_dump.go @@ -31,7 +31,7 @@ func MakeServerDumpCmd() *cobra.Command { return errors.New("server-side dump is only supported for the Badger datastore") } storeOpts := []node.StoreOpt{ - node.WithPath(cfg.GetString("datastore.badger.path")), + node.WithStorePath(cfg.GetString("datastore.badger.path")), } rootstore, err := node.NewStore(cmd.Context(), storeOpts...) if err != nil { diff --git a/cli/start.go b/cli/start.go index 970b857aa0..7e1966253c 100644 --- a/cli/start.go +++ b/cli/start.go @@ -57,8 +57,8 @@ func MakeStartCommand() *cobra.Command { } opts := []node.Option{ - node.WithPath(cfg.GetString("datastore.badger.path")), - node.WithInMemory(cfg.GetString("datastore.store") == configStoreMemory), + node.WithStorePath(cfg.GetString("datastore.badger.path")), + node.WithBadgerInMemory(cfg.GetString("datastore.store") == configStoreMemory), node.WithDisableP2P(cfg.GetBool("net.p2pDisabled")), node.WithACPType(node.LocalACPType), node.WithPeers(peers...), @@ -100,7 +100,7 @@ func MakeStartCommand() *cobra.Command { if err != nil && !errors.Is(err, keyring.ErrNotFound) { return err } - opts = append(opts, node.WithEncryptionKey(encryptionKey)) + opts = append(opts, node.WithBadgerEncryptionKey(encryptionKey)) } n, err := node.NewNode(cmd.Context(), opts...) diff --git a/datastore/badger/v4/datastore.go b/datastore/badger/v4/datastore.go index 14b841867f..caac4144a2 100644 --- a/datastore/badger/v4/datastore.go +++ b/datastore/badger/v4/datastore.go @@ -24,11 +24,6 @@ import ( var log = logger.Logger("badger") -var ( - ErrClosed = errors.New("datastore closed") - ErrTxnConflict = badger.ErrConflict -) - type Datastore struct { DB *badger.DB @@ -767,7 +762,8 @@ func (t *txn) Commit(ctx context.Context) error { } func (t *txn) commit() error { - return t.txn.Commit() + err := t.txn.Commit() + return convertError(err) } func (t *txn) Discard(ctx context.Context) { diff --git a/datastore/badger/v4/errors.go b/datastore/badger/v4/errors.go index ea78ae8b72..4a8a8d702f 100644 --- a/datastore/badger/v4/errors.go +++ b/datastore/badger/v4/errors.go @@ -12,12 +12,39 @@ package badger import ( dsq "github.com/ipfs/go-datastore/query" + badger "github.com/sourcenetwork/badger/v4" + datastoreErrors "github.com/sourcenetwork/defradb/datastore/errors" "github.com/sourcenetwork/defradb/errors" ) +var ( + ErrClosed = datastoreErrors.ErrClosed + ErrTxnConflict = datastoreErrors.ErrTxnConflict +) + const errOrderType string = "invalid order type" func ErrOrderType(orderType dsq.Order) error { return errors.New(errOrderType, errors.NewKV("Order type", orderType)) } + +// convertError converts badger specific errors into datastore errors. +func convertError(err error) error { + // The errors we are matching against are never wrapped. + // + //nolint:errorlint + switch err { + case badger.ErrConflict: + return datastoreErrors.ErrTxnConflict + + case badger.ErrReadOnlyTxn: + return datastoreErrors.ErrReadOnlyTxn + + case badger.ErrDiscardedTxn: + return datastoreErrors.ErrTxnDiscarded + + default: + return err + } +} diff --git a/datastore/blockstore_test.go b/datastore/blockstore_test.go index 29daffcc76..f3e3086893 100644 --- a/datastore/blockstore_test.go +++ b/datastore/blockstore_test.go @@ -91,7 +91,7 @@ func TestBStoreGetWithStoreClosed(t *testing.T) { require.NoError(t, err) _, err = bs.Get(ctx, cID) - require.ErrorIs(t, err, memory.ErrClosed) + require.ErrorIs(t, err, ErrClosed) } func TestBStoreGetWithReHash(t *testing.T) { @@ -190,5 +190,5 @@ func TestPutManyWithStoreClosed(t *testing.T) { require.NoError(t, err) err = bs.PutMany(ctx, []blocks.Block{b, b2}) - require.ErrorIs(t, err, memory.ErrClosed) + require.ErrorIs(t, err, ErrClosed) } diff --git a/datastore/concurrent_txt_test.go b/datastore/concurrent_txt_test.go index 1a1b43bbab..10fd5e278a 100644 --- a/datastore/concurrent_txt_test.go +++ b/datastore/concurrent_txt_test.go @@ -45,7 +45,7 @@ func TestNewConcurrentTxnFromWithStoreClosed(t *testing.T) { require.NoError(t, err) _, err = NewConcurrentTxnFrom(ctx, rootstore, 0, false) - require.ErrorIs(t, err, badgerds.ErrClosed) + require.ErrorIs(t, err, ErrClosed) } func TestNewConcurrentTxnFromNonIterable(t *testing.T) { @@ -67,7 +67,7 @@ func TestNewConcurrentTxnFromNonIterableWithStoreClosed(t *testing.T) { require.NoError(t, err) _, err = NewConcurrentTxnFrom(ctx, rootstore, 0, false) - require.ErrorIs(t, err, badgerds.ErrClosed) + require.ErrorIs(t, err, ErrClosed) } func TestConcurrentTxnSync(t *testing.T) { diff --git a/datastore/errors.go b/datastore/errors.go index b248ce6db8..17c1e773cf 100644 --- a/datastore/errors.go +++ b/datastore/errors.go @@ -11,6 +11,7 @@ package datastore import ( + datastoreErrors "github.com/sourcenetwork/defradb/datastore/errors" "github.com/sourcenetwork/defradb/errors" ) @@ -28,7 +29,9 @@ var ( // defradb/store.ErrNotFound => error // ipfs-blockstore.ErrNotFound => error // ErrNotFound is an error returned when a block is not found. - ErrNotFound = errors.New("blockstore: block not found") + ErrNotFound = errors.New("blockstore: block not found") + ErrClosed = datastoreErrors.ErrClosed + ErrTxnConflict = datastoreErrors.ErrTxnConflict ) // NewErrInvalidStoredValue returns a new error indicating that the stored diff --git a/datastore/errors/errors.go b/datastore/errors/errors.go new file mode 100644 index 0000000000..a2c8febbbf --- /dev/null +++ b/datastore/errors/errors.go @@ -0,0 +1,24 @@ +// Copyright 2024 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package errors + +import "errors" + +var ( + ErrClosed = errors.New("datastore closed") + // ErrConflict is returned when a transaction conflicts with another transaction. This can + // happen if the read rows had been updated concurrently by another transaction. + ErrTxnConflict = errors.New("transaction Conflict. Please retry") + // ErrDiscardedTxn is returned if a previously discarded transaction is re-used. + ErrTxnDiscarded = errors.New("this transaction has been discarded. Create a new one") + // ErrReadOnlyTxn is returned if an update function is called on a read-only transaction. + ErrReadOnlyTxn = errors.New("no sets or deletes are allowed in a read-only transaction") +) diff --git a/datastore/memory/errors.go b/datastore/memory/errors.go index 5ea7734c60..7d32cd156c 100644 --- a/datastore/memory/errors.go +++ b/datastore/memory/errors.go @@ -10,12 +10,13 @@ package memory -import "github.com/sourcenetwork/defradb/errors" +import ( + "github.com/sourcenetwork/defradb/datastore/errors" +) var ( - ErrReadOnlyTxn = errors.New("read only transaction") - ErrTxnDiscarded = errors.New("transaction discarded") - //nolint:revive - ErrTxnConflict = errors.New("Transaction Conflict. Please retry") - ErrClosed = errors.New("datastore closed") + ErrReadOnlyTxn = errors.ErrReadOnlyTxn + ErrTxnDiscarded = errors.ErrTxnDiscarded + ErrTxnConflict = errors.ErrTxnConflict + ErrClosed = errors.ErrClosed ) diff --git a/datastore/txn_test.go b/datastore/txn_test.go index cf3cdc5c1d..9f2012ad6d 100644 --- a/datastore/txn_test.go +++ b/datastore/txn_test.go @@ -47,7 +47,7 @@ func TestNewTxnFromWithStoreClosed(t *testing.T) { require.NoError(t, err) _, err = NewTxnFrom(ctx, rootstore, 0, false) - require.ErrorIs(t, err, badgerds.ErrClosed) + require.ErrorIs(t, err, ErrClosed) } func TestOnSuccess(t *testing.T) { @@ -107,7 +107,7 @@ func TestOnError(t *testing.T) { require.NoError(t, err) err = txn.Commit(ctx) - require.ErrorIs(t, err, badgerds.ErrClosed) + require.ErrorIs(t, err, ErrClosed) require.Equal(t, text, "Source Inc") } @@ -131,7 +131,7 @@ func TestOnErrorAsync(t *testing.T) { wg.Add(1) err = txn.Commit(ctx) - require.ErrorIs(t, err, badgerds.ErrClosed) + require.ErrorIs(t, err, ErrClosed) wg.Wait() } @@ -252,7 +252,7 @@ func TestMemoryStoreTxn_TwoTransactionsWithGetPutConflict_ShouldErrorWithConflic require.NoError(t, err) err = txn1.Commit(ctx) - require.ErrorIs(t, err, badger.ErrConflict) + require.ErrorIs(t, err, ErrTxnConflict) } func TestMemoryStoreTxn_TwoTransactionsWithHasPutConflict_ShouldErrorWithConflict(t *testing.T) { @@ -281,7 +281,7 @@ func TestMemoryStoreTxn_TwoTransactionsWithHasPutConflict_ShouldErrorWithConflic require.NoError(t, err) err = txn1.Commit(ctx) - require.ErrorIs(t, err, badger.ErrConflict) + require.ErrorIs(t, err, ErrTxnConflict) } func TestBadgerMemoryStoreTxn_TwoTransactionsWithPutConflict_ShouldSucceed(t *testing.T) { @@ -338,7 +338,7 @@ func TestBadgerMemoryStoreTxn_TwoTransactionsWithGetPutConflict_ShouldErrorWithC require.NoError(t, err) err = txn1.Commit(ctx) - require.ErrorIs(t, err, badger.ErrConflict) + require.ErrorIs(t, err, ErrTxnConflict) } func TestBadgerMemoryStoreTxn_TwoTransactionsWithHasPutConflict_ShouldErrorWithConflict(t *testing.T) { @@ -369,7 +369,7 @@ func TestBadgerMemoryStoreTxn_TwoTransactionsWithHasPutConflict_ShouldErrorWithC require.NoError(t, err) err = txn1.Commit(ctx) - require.ErrorIs(t, err, badger.ErrConflict) + require.ErrorIs(t, err, ErrTxnConflict) } func TestBadgerFileStoreTxn_TwoTransactionsWithPutConflict_ShouldSucceed(t *testing.T) { @@ -426,7 +426,7 @@ func TestBadgerFileStoreTxn_TwoTransactionsWithGetPutConflict_ShouldErrorWithCon require.NoError(t, err) err = txn1.Commit(ctx) - require.ErrorIs(t, err, badger.ErrConflict) + require.ErrorIs(t, err, ErrTxnConflict) } func TestBadgerFileStoreTxn_TwoTransactionsWithHasPutConflict_ShouldErrorWithConflict(t *testing.T) { @@ -457,7 +457,7 @@ func TestBadgerFileStoreTxn_TwoTransactionsWithHasPutConflict_ShouldErrorWithCon require.NoError(t, err) err = txn1.Commit(ctx) - require.ErrorIs(t, err, badger.ErrConflict) + require.ErrorIs(t, err, ErrTxnConflict) } func TestMemoryStoreTxn_TwoTransactionsWithQueryAndPut_ShouldOmmitNewPut(t *testing.T) { diff --git a/datastore/wrappedstore_test.go b/datastore/wrappedstore_test.go index a4ca65f4af..f68e7a39a6 100644 --- a/datastore/wrappedstore_test.go +++ b/datastore/wrappedstore_test.go @@ -85,7 +85,7 @@ func TestQueryWithStoreClosed(t *testing.T) { require.NoError(t, err) _, err = dsRW.Query(ctx, query.Query{}) - require.ErrorIs(t, err, badgerds.ErrClosed) + require.ErrorIs(t, err, ErrClosed) } func TestIteratePrefix(t *testing.T) { @@ -118,5 +118,5 @@ func TestIteratePrefixWithStoreClosed(t *testing.T) { require.NoError(t, err) _, err = iter.IteratePrefix(ctx, ds.NewKey("key1"), ds.NewKey("key1")) - require.ErrorIs(t, err, badgerds.ErrClosed) + require.ErrorIs(t, err, ErrClosed) } diff --git a/http/utils.go b/http/utils.go index 81aeac1b05..835382987b 100644 --- a/http/utils.go +++ b/http/utils.go @@ -17,7 +17,7 @@ import ( "net/http" "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/datastore/badger/v4" + "github.com/sourcenetwork/defradb/datastore" ) func requestJSON(req *http.Request, out any) error { @@ -44,8 +44,8 @@ func parseError(msg any) error { switch msg { case client.ErrDocumentNotFoundOrNotAuthorized.Error(): return client.ErrDocumentNotFoundOrNotAuthorized - case badger.ErrTxnConflict.Error(): - return badger.ErrTxnConflict + case datastore.ErrTxnConflict.Error(): + return datastore.ErrTxnConflict default: return fmt.Errorf("%s", msg) } diff --git a/internal/db/messages.go b/internal/db/messages.go index 1967c0e238..04b81cd210 100644 --- a/internal/db/messages.go +++ b/internal/db/messages.go @@ -16,7 +16,7 @@ import ( "github.com/sourcenetwork/corelog" - "github.com/sourcenetwork/defradb/datastore/badger/v4" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/event" ) @@ -48,7 +48,7 @@ func (db *db) handleMessages(ctx context.Context, sub *event.Subscription) { var err error for i := 0; i < db.MaxTxnRetries(); i++ { err = db.executeMerge(ctx, evt) - if errors.Is(err, badger.ErrTxnConflict) { + if errors.Is(err, datastore.ErrTxnConflict) { continue // retry merge } break // merge success or error diff --git a/node/errors.go b/node/errors.go index d19b53359b..2078915c0a 100644 --- a/node/errors.go +++ b/node/errors.go @@ -16,10 +16,18 @@ import ( const ( errLensRuntimeNotSupported string = "the selected lens runtime is not supported by this build" + errStoreTypeNotSupported string = "the selected store type is not supported by this build" ) -var ErrLensRuntimeNotSupported = errors.New(errLensRuntimeNotSupported) +var ( + ErrLensRuntimeNotSupported = errors.New(errLensRuntimeNotSupported) + ErrStoreTypeNotSupported = errors.New(errStoreTypeNotSupported) +) func NewErrLensRuntimeNotSupported(lens LensRuntimeType) error { return errors.New(errLensRuntimeNotSupported, errors.NewKV("Lens", lens)) } + +func NewErrStoreTypeNotSupported(store StoreType) error { + return errors.New(errStoreTypeNotSupported, errors.NewKV("Store", store)) +} diff --git a/node/store.go b/node/store.go index 1a5b46f8e0..373610b0e1 100644 --- a/node/store.go +++ b/node/store.go @@ -14,90 +14,66 @@ import ( "context" "github.com/sourcenetwork/defradb/datastore" - "github.com/sourcenetwork/defradb/datastore/badger/v4" - "github.com/sourcenetwork/defradb/datastore/memory" ) +type StoreType string + +const ( + // The Go-enum default StoreType. + // + // The actual store type that this resolves to depends on the build target. + DefaultStore StoreType = "" +) + +// storeConstructors is a map of [StoreType]s to store constructors. +// +// Is is populated by the `init` functions in the runtime-specific files - this +// allows it's population to be managed by build flags. +var storeConstructors = map[StoreType]func(ctx context.Context, options *StoreOptions) (datastore.Rootstore, error){} + // StoreOptions contains store configuration values. type StoreOptions struct { - path string - inMemory bool - defraStore bool - valueLogFileSize int64 - encryptionKey []byte + store StoreType + path string + badgerFileSize int64 + badgerEncryptionKey []byte + badgerInMemory bool } // DefaultStoreOptions returns new options with default values. func DefaultStoreOptions() *StoreOptions { return &StoreOptions{ - inMemory: false, - valueLogFileSize: 1 << 30, + badgerInMemory: false, + badgerFileSize: 1 << 30, } } // StoreOpt is a function for setting configuration values. type StoreOpt func(*StoreOptions) -// WithInMemory sets the in memory flag. -func WithInMemory(inMemory bool) StoreOpt { - return func(o *StoreOptions) { - o.inMemory = inMemory - } -} - -// 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 { +// WithStoreType sets the store type to use. +func WithStoreType(store StoreType) StoreOpt { return func(o *StoreOptions) { - o.defraStore = defraStore + o.store = store } } -// WithPath sets the datastore path. -func WithPath(path string) StoreOpt { +// WithStorePath sets the store path. +func WithStorePath(path string) StoreOpt { return func(o *StoreOptions) { o.path = path } } -// WithValueLogFileSize sets the badger value log file size. -func WithValueLogFileSize(size int64) StoreOpt { - return func(o *StoreOptions) { - o.valueLogFileSize = size - } -} - -// WithEncryptionKey sets the badger encryption key. -func WithEncryptionKey(encryptionKey []byte) StoreOpt { - return func(o *StoreOptions) { - o.encryptionKey = encryptionKey - } -} - // NewStore returns a new store with the given options. 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 + storeConstructor, ok := storeConstructors[options.store] + if ok { + return storeConstructor(ctx, options) } - - badgerOpts := badger.DefaultOptions - badgerOpts.InMemory = options.inMemory - badgerOpts.ValueLogFileSize = options.valueLogFileSize - badgerOpts.EncryptionKey = options.encryptionKey - - if len(options.encryptionKey) > 0 { - // Having a cache improves the performance. - // Otherwise, your reads would be very slow while encryption is enabled. - // https://dgraph.io/docs/badger/get-started/#encryption-mode - badgerOpts.IndexCacheSize = 100 << 20 - } - - return badger.NewDatastore(options.path, &badgerOpts) + return nil, NewErrStoreTypeNotSupported(options.store) } diff --git a/node/store_badger.go b/node/store_badger.go new file mode 100644 index 0000000000..5c252d3607 --- /dev/null +++ b/node/store_badger.go @@ -0,0 +1,64 @@ +// Copyright 2024 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +//go:build !js + +package node + +import ( + "context" + + "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/datastore/badger/v4" +) + +// BadgerStore specifies the badger datastore +const BadgerStore = StoreType("badger") + +func init() { + constructor := func(ctx context.Context, options *StoreOptions) (datastore.Rootstore, error) { + badgerOpts := badger.DefaultOptions + badgerOpts.InMemory = options.badgerInMemory + badgerOpts.ValueLogFileSize = options.badgerFileSize + badgerOpts.EncryptionKey = options.badgerEncryptionKey + + if len(options.badgerEncryptionKey) > 0 { + // Having a cache improves the performance. + // Otherwise, your reads would be very slow while encryption is enabled. + // https://dgraph.io/docs/badger/get-started/#encryption-mode + badgerOpts.IndexCacheSize = 100 << 20 + } + + return badger.NewDatastore(options.path, &badgerOpts) + } + storeConstructors[BadgerStore] = constructor + storeConstructors[DefaultStore] = constructor +} + +// WithBadgerInMemory sets the badger in memory option. +func WithBadgerInMemory(enable bool) StoreOpt { + return func(o *StoreOptions) { + o.badgerInMemory = enable + } +} + +// WithBadgerFileSize sets the badger value log file size. +func WithBadgerFileSize(size int64) StoreOpt { + return func(o *StoreOptions) { + o.badgerFileSize = size + } +} + +// WithBadgerEncryptionKey sets the badger encryption key. +func WithBadgerEncryptionKey(encryptionKey []byte) StoreOpt { + return func(o *StoreOptions) { + o.badgerEncryptionKey = encryptionKey + } +} diff --git a/node/store_badger_test.go b/node/store_badger_test.go new file mode 100644 index 0000000000..3278851af3 --- /dev/null +++ b/node/store_badger_test.go @@ -0,0 +1,43 @@ +// Copyright 2024 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +//go:build !js + +package node + +import ( + "crypto/rand" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestWithBadgerInMemory(t *testing.T) { + options := &StoreOptions{} + WithBadgerInMemory(true)(options) + assert.Equal(t, true, options.badgerInMemory) +} + +func TestWithBadgerFileSize(t *testing.T) { + options := &StoreOptions{} + WithBadgerFileSize(int64(5 << 30))(options) + assert.Equal(t, int64(5<<30), options.badgerFileSize) +} + +func TestWithBadgerEncryptionKey(t *testing.T) { + encryptionKey := make([]byte, 32) + _, err := rand.Read(encryptionKey) + require.NoError(t, err) + + options := &StoreOptions{} + WithBadgerEncryptionKey(encryptionKey)(options) + assert.Equal(t, encryptionKey, options.badgerEncryptionKey) +} diff --git a/node/store_memory.go b/node/store_memory.go new file mode 100644 index 0000000000..352381fa9d --- /dev/null +++ b/node/store_memory.go @@ -0,0 +1,32 @@ +// Copyright 2024 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package node + +import ( + "context" + + "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/datastore/memory" +) + +// MemoryStore specifies the defradb in memory datastore +const MemoryStore = StoreType("memory") + +func init() { + constructor := func(ctx context.Context, options *StoreOptions) (datastore.Rootstore, error) { + return memory.NewDatastore(ctx), nil + } + // don't override the default constructor if previously set + if _, ok := storeConstructors[DefaultStore]; !ok { + storeConstructors[DefaultStore] = constructor + } + storeConstructors[MemoryStore] = constructor +} diff --git a/node/store_test.go b/node/store_test.go index 69ce98e952..3f9185c7ed 100644 --- a/node/store_test.go +++ b/node/store_test.go @@ -11,37 +11,19 @@ package node import ( - "crypto/rand" "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) -func TestWithInMemory(t *testing.T) { +func TestWithStore(t *testing.T) { options := &StoreOptions{} - WithInMemory(true)(options) - assert.Equal(t, true, options.inMemory) + WithStoreType(MemoryStore)(options) + assert.Equal(t, MemoryStore, options.store) } -func TestWithPath(t *testing.T) { +func TestWithStorePath(t *testing.T) { options := &StoreOptions{} - WithPath("tmp")(options) - assert.Equal(t, "tmp", options.path) -} - -func TestWithValueLogFileSize(t *testing.T) { - options := &StoreOptions{} - WithValueLogFileSize(int64(5 << 30))(options) - assert.Equal(t, int64(5<<30), options.valueLogFileSize) -} - -func TestWithEncryptionKey(t *testing.T) { - encryptionKey := make([]byte, 32) - _, err := rand.Read(encryptionKey) - require.NoError(t, err) - - options := &StoreOptions{} - WithEncryptionKey(encryptionKey)(options) - assert.Equal(t, encryptionKey, options.encryptionKey) + WithStorePath("test")(options) + assert.Equal(t, "test", options.path) } diff --git a/tests/integration/db.go b/tests/integration/db.go index dbbbed7ffb..d2ecccf366 100644 --- a/tests/integration/db.go +++ b/tests/integration/db.go @@ -71,7 +71,7 @@ func init() { func NewBadgerMemoryDB(ctx context.Context) (client.DB, error) { opts := []node.Option{ - node.WithInMemory(true), + node.WithBadgerInMemory(true), } node, err := node.NewNode(ctx, opts...) @@ -86,7 +86,7 @@ func NewBadgerFileDB(ctx context.Context, t testing.TB) (client.DB, error) { path := t.TempDir() opts := []node.Option{ - node.WithPath(path), + node.WithStorePath(path), } node, err := node.NewNode(ctx, opts...) @@ -121,13 +121,13 @@ func setupNode(s *state) (*node.Node, string, error) { } if encryptionKey != nil { - opts = append(opts, node.WithEncryptionKey(encryptionKey)) + opts = append(opts, node.WithBadgerEncryptionKey(encryptionKey)) } var path string switch s.dbt { case badgerIMType: - opts = append(opts, node.WithInMemory(true)) + opts = append(opts, node.WithBadgerInMemory(true)) case badgerFileType: switch { @@ -144,10 +144,10 @@ func setupNode(s *state) (*node.Node, string, error) { path = s.t.TempDir() } - opts = append(opts, node.WithPath(path), node.WithACPPath(path)) + opts = append(opts, node.WithStorePath(path), node.WithACPPath(path)) case defraIMType: - opts = append(opts, node.WithDefraStore(true)) + opts = append(opts, node.WithStoreType(node.MemoryStore)) default: return nil, "", fmt.Errorf("invalid database type: %v", s.dbt) diff --git a/tests/integration/mutation/mix/with_txn_test.go b/tests/integration/mutation/mix/with_txn_test.go index b7c193b10b..5354b69eea 100644 --- a/tests/integration/mutation/mix/with_txn_test.go +++ b/tests/integration/mutation/mix/with_txn_test.go @@ -300,7 +300,7 @@ func TestMutationWithTxnDoesNotAllowUpdateInSecondTransactionUser(t *testing.T) }, testUtils.TransactionCommit{ TransactionID: 1, - ExpectedError: "Transaction Conflict. Please retry", + ExpectedError: "transaction Conflict. Please retry", }, testUtils.Request{ // Query after transactions have been commited: diff --git a/tests/integration/utils2.go b/tests/integration/utils2.go index 4ab68930cf..3f6585824d 100644 --- a/tests/integration/utils2.go +++ b/tests/integration/utils2.go @@ -32,7 +32,6 @@ import ( "github.com/sourcenetwork/defradb/client/request" "github.com/sourcenetwork/defradb/crypto" "github.com/sourcenetwork/defradb/datastore" - badgerds "github.com/sourcenetwork/defradb/datastore/badger/v4" "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/event" "github.com/sourcenetwork/defradb/internal/db" @@ -1692,7 +1691,7 @@ func withRetry( ) error { for i := 0; i < nodes[nodeID].MaxTxnRetries(); i++ { err := action() - if err != nil && errors.Is(err, badgerds.ErrTxnConflict) { + if errors.Is(err, datastore.ErrTxnConflict) { time.Sleep(100 * time.Millisecond) continue }