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: DB transactions context #2513

Merged
merged 16 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 0 additions & 5 deletions cli/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/datastore"
)

func MakeCollectionCommand() *cobra.Command {
Expand Down Expand Up @@ -71,10 +70,6 @@ func MakeCollectionCommand() *cobra.Command {
}
col := cols[0]

if tx, ok := cmd.Context().Value(txContextKey).(datastore.Txn); ok {
col = col.WithTxn(tx)
}

ctx := context.WithValue(cmd.Context(), colContextKey, col)
cmd.SetContext(ctx)
return nil
Expand Down
4 changes: 0 additions & 4 deletions cli/index_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/datastore"
)

func MakeIndexCreateCommand() *cobra.Command {
Expand Down Expand Up @@ -52,9 +51,6 @@ Example: create a named index for 'Users' collection on 'name' field:
if err != nil {
return err
}
if tx, ok := cmd.Context().Value(txContextKey).(datastore.Txn); ok {
col = col.WithTxn(tx)
}
desc, err = col.CreateIndex(cmd.Context(), desc)
if err != nil {
return err
Expand Down
5 changes: 0 additions & 5 deletions cli/index_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ package cli

import (
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/datastore"
)

func MakeIndexDropCommand() *cobra.Command {
Expand All @@ -34,9 +32,6 @@ Example: drop the index 'UsersByName' for 'Users' collection:
if err != nil {
return err
}
if tx, ok := cmd.Context().Value(txContextKey).(datastore.Txn); ok {
col = col.WithTxn(tx)
}
return col.DropIndex(cmd.Context(), nameArg)
},
}
Expand Down
5 changes: 0 additions & 5 deletions cli/index_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ package cli

import (
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/datastore"
)

func MakeIndexListCommand() *cobra.Command {
Expand All @@ -38,9 +36,6 @@ Example: show all index for 'Users' collection:
if err != nil {
return err
}
if tx, ok := cmd.Context().Value(txContextKey).(datastore.Txn); ok {
col = col.WithTxn(tx)
}
indexes, err := col.GetIndexes(cmd.Context())
if err != nil {
return err
Expand Down
9 changes: 1 addition & 8 deletions cli/schema_migration_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (

"github.com/sourcenetwork/immutable/enumerable"
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/datastore"
)

func MakeSchemaMigrationDownCommand() *cobra.Command {
Expand Down Expand Up @@ -67,12 +65,7 @@ Example: migrate from stdin
if err := json.Unmarshal(srcData, &src); err != nil {
return err
}
lens := store.LensRegistry()
if tx, ok := cmd.Context().Value(txContextKey).(datastore.Txn); ok {
lens = lens.WithTxn(tx)
}

out, err := lens.MigrateDown(cmd.Context(), enumerable.New(src), collectionID)
out, err := store.LensRegistry().MigrateDown(cmd.Context(), enumerable.New(src), collectionID)
if err != nil {
return err
}
Expand Down
9 changes: 1 addition & 8 deletions cli/schema_migration_reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ package cli

import (
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/datastore"
)

func MakeSchemaMigrationReloadCommand() *cobra.Command {
Expand All @@ -23,12 +21,7 @@ func MakeSchemaMigrationReloadCommand() *cobra.Command {
Long: `Reload the schema migrations within DefraDB`,
RunE: func(cmd *cobra.Command, args []string) error {
store := mustGetContextStore(cmd)

lens := store.LensRegistry()
if tx, ok := cmd.Context().Value(txContextKey).(datastore.Txn); ok {
lens = lens.WithTxn(tx)
}
return lens.ReloadLenses(cmd.Context())
return store.LensRegistry().ReloadLenses(cmd.Context())
},
}
return cmd
Expand Down
9 changes: 1 addition & 8 deletions cli/schema_migration_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (

"github.com/sourcenetwork/immutable/enumerable"
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/datastore"
)

func MakeSchemaMigrationUpCommand() *cobra.Command {
Expand Down Expand Up @@ -67,12 +65,7 @@ Example: migrate from stdin
if err := json.Unmarshal(srcData, &src); err != nil {
return err
}
lens := store.LensRegistry()
if tx, ok := cmd.Context().Value(txContextKey).(datastore.Txn); ok {
lens = lens.WithTxn(tx)
}

out, err := lens.MigrateUp(cmd.Context(), enumerable.New(src), collectionID)
out, err := store.LensRegistry().MigrateUp(cmd.Context(), enumerable.New(src), collectionID)
if err != nil {
return err
}
Expand Down
6 changes: 0 additions & 6 deletions client/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"context"

"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/datastore"
)

// Collection represents a defradb collection.
Expand Down Expand Up @@ -192,10 +190,6 @@ type Collection interface {
showDeleted bool,
) (*Document, error)

// WithTxn returns a new instance of the collection, with a transaction
// handle instead of a raw DB handle.
WithTxn(datastore.Txn) Collection

// GetAllDocIDs returns all the document IDs that exist in the collection.
GetAllDocIDs(ctx context.Context, identity immutable.Option[string]) (<-chan DocIDResult, error)

Expand Down
3 changes: 0 additions & 3 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ type DB interface {
// can safely operate on it concurrently.
NewConcurrentTxn(context.Context, bool) (datastore.Txn, error)

// WithTxn returns a new [client.Store] that respects the given transaction.
WithTxn(datastore.Txn) Store

// Root returns the underlying root store, within which all data managed by DefraDB is held.
Root() datastore.RootStore

Expand Down
8 changes: 0 additions & 8 deletions client/lens.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (

"github.com/lens-vm/lens/host-go/config/model"
"github.com/sourcenetwork/immutable/enumerable"

"github.com/sourcenetwork/defradb/datastore"
)

// LensConfig represents the configuration of a Lens migration in Defra.
Expand All @@ -43,12 +41,6 @@ type LensConfig struct {
// LensRegistry exposes several useful thread-safe migration related functions which may
// be used to manage migrations.
type LensRegistry interface {
// WithTxn returns a new LensRegistry scoped to the given transaction.
//
// WARNING: Currently this does not provide snapshot isolation, if other transactions are committed
// after this has been created, the results of those commits will be visible within this scope.
WithTxn(datastore.Txn) LensRegistry

// SetMigration caches the migration for the given collection ID. It does not persist the migration in long
// term storage, for that one should call [Store.SetMigration(ctx, cfg)].
//
Expand Down
12 changes: 7 additions & 5 deletions db/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/db/session"
)

func (db *db) basicImport(ctx context.Context, txn datastore.Txn, filepath string) (err error) {
Expand Down Expand Up @@ -91,8 +92,9 @@ func (db *db) basicImport(ctx context.Context, txn datastore.Txn, filepath strin
return NewErrDocFromMap(err)
}

sess := session.New(ctx).WithTxn(txn)
// TODO-ACP: https://github.com/sourcenetwork/defradb/issues/2430 - Add identity ability to backup
err = col.WithTxn(txn).Create(ctx, acpIdentity.NoIdentity, doc)
err = col.Create(sess, acpIdentity.NoIdentity, doc)
if err != nil {
return NewErrDocCreate(err)
}
Expand All @@ -104,7 +106,7 @@ func (db *db) basicImport(ctx context.Context, txn datastore.Txn, filepath strin
return NewErrDocUpdate(err)
}
// TODO-ACP: https://github.com/sourcenetwork/defradb/issues/2430 - Add identity ability to backup
err = col.WithTxn(txn).Update(ctx, acpIdentity.NoIdentity, doc)
err = col.Update(sess, acpIdentity.NoIdentity, doc)
if err != nil {
return NewErrDocUpdate(err)
}
Expand Down Expand Up @@ -191,9 +193,9 @@ func (db *db) basicExport(ctx context.Context, txn datastore.Txn, config *client
if err != nil {
return err
}
colTxn := col.WithTxn(txn)
sess := session.New(ctx).WithTxn(txn)
// TODO-ACP: https://github.com/sourcenetwork/defradb/issues/2430 - Add identity ability to export
docIDsCh, err := colTxn.GetAllDocIDs(ctx, acpIdentity.NoIdentity)
docIDsCh, err := col.GetAllDocIDs(sess, acpIdentity.NoIdentity)
if err != nil {
return err
}
Expand All @@ -210,7 +212,7 @@ func (db *db) basicExport(ctx context.Context, txn datastore.Txn, config *client
}
}
// TODO-ACP: https://github.com/sourcenetwork/defradb/issues/2430 - Add identity ability to export
doc, err := colTxn.Get(ctx, acpIdentity.NoIdentity, docResultWithID.ID, false)
doc, err := col.Get(sess, acpIdentity.NoIdentity, docResultWithID.ID, false)
if err != nil {
return err
}
Expand Down
Loading
Loading