diff --git a/client/lens.go b/client/lens.go index 3f5befc604..d74c57fe84 100644 --- a/client/lens.go +++ b/client/lens.go @@ -15,6 +15,8 @@ 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. @@ -38,9 +40,17 @@ type LensConfig struct { model.Lens } +// TxnSource represents an object capable of constructing the transactions that +// implicit-transaction registries need internally. +type TxnSource interface { + NewTxn(context.Context, bool) (datastore.Txn, error) +} + // LensRegistry exposes several useful thread-safe migration related functions which may // be used to manage migrations. type LensRegistry interface { + Init(TxnSource) + // 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)]. // diff --git a/http/client_lens.go b/http/client_lens.go index 34945a41d6..249eb99984 100644 --- a/http/client_lens.go +++ b/http/client_lens.go @@ -35,6 +35,8 @@ type setMigrationRequest struct { Config model.Lens } +func (w *LensRegistry) Init(txnSource client.TxnSource) {} + func (c *LensRegistry) SetMigration(ctx context.Context, collectionID uint32, config model.Lens) error { methodURL := c.http.baseURL.JoinPath("lens", "registry") diff --git a/internal/db/db.go b/internal/db/db.go index 011bf9c3f4..979626034c 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -109,6 +109,10 @@ func newDB( opt(db) } + if lens != nil { + lens.Init(db) + } + err = db.initialize(ctx) if err != nil { return nil, err diff --git a/internal/lens/registry.go b/internal/lens/registry.go index f90259af3c..c0fc87a14f 100644 --- a/internal/lens/registry.go +++ b/internal/lens/registry.go @@ -70,12 +70,6 @@ func newTxnCtx(txn datastore.Txn) *txnContext { } } -// TxnSource represents an object capable of constructing the transactions that -// implicit-transaction registries need internally. -type TxnSource interface { - NewTxn(context.Context, bool) (datastore.Txn, error) -} - // DefaultPoolSize is the default size of the lens pool for each schema version. const DefaultPoolSize int = 5 @@ -83,7 +77,6 @@ const DefaultPoolSize int = 5 // // It will be of size 5 (per schema version) if a size is not provided. func NewRegistry( - db TxnSource, poolSize int, runtime module.Runtime, ) client.LensRegistry { @@ -97,7 +90,6 @@ func NewRegistry( } return &implicitTxnLensRegistry{ - db: db, registry: registry, } } diff --git a/internal/lens/txn_registry.go b/internal/lens/txn_registry.go index 8093dedbdd..65ad12cf2b 100644 --- a/internal/lens/txn_registry.go +++ b/internal/lens/txn_registry.go @@ -22,7 +22,7 @@ import ( type implicitTxnLensRegistry struct { registry *lensRegistry - db TxnSource + db client.TxnSource } type explicitTxnLensRegistry struct { @@ -33,13 +33,12 @@ type explicitTxnLensRegistry struct { var _ client.LensRegistry = (*implicitTxnLensRegistry)(nil) var _ client.LensRegistry = (*explicitTxnLensRegistry)(nil) -func (r *implicitTxnLensRegistry) WithTxn(txn datastore.Txn) client.LensRegistry { - return &explicitTxnLensRegistry{ - registry: r.registry, - txn: txn, - } +func (r *implicitTxnLensRegistry) Init(txnSource client.TxnSource) { + r.db = txnSource } +func (r *explicitTxnLensRegistry) Init(txnSource client.TxnSource) {} + func (r *explicitTxnLensRegistry) WithTxn(txn datastore.Txn) client.LensRegistry { return &explicitTxnLensRegistry{ registry: r.registry, diff --git a/node/lens.go b/node/lens.go index b7dca0313b..c8e0c50139 100644 --- a/node/lens.go +++ b/node/lens.go @@ -16,7 +16,6 @@ import ( "github.com/lens-vm/lens/host-go/engine/module" "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/internal/lens" ) @@ -63,7 +62,6 @@ func WithLensPoolSize(size int) Option { func NewLens( ctx context.Context, - txnSource *txnSource, opts ...LenOpt, ) (client.LensRegistry, error) { options := DefaultLensOptions() @@ -79,18 +77,7 @@ func NewLens( } return lens.NewRegistry( - txnSource, options.lensPoolSize, runtime, ), nil } - -type txnSource struct { - db *client.DB -} - -var _ lens.TxnSource = (*txnSource)(nil) - -func (s txnSource) NewTxn(ctx context.Context, readonly bool) (datastore.Txn, error) { - return (*s.db).NewTxn(ctx, readonly) -} diff --git a/node/node.go b/node/node.go index b8ea081d95..215cf05fc7 100644 --- a/node/node.go +++ b/node/node.go @@ -128,8 +128,7 @@ func NewNode(ctx context.Context, opts ...Option) (*Node, error) { return nil, err } - txnSource := &txnSource{} - lens, err := NewLens(ctx, txnSource, lensOpts...) + lens, err := NewLens(ctx, lensOpts...) if err != nil { return nil, err } @@ -138,7 +137,6 @@ func NewNode(ctx context.Context, opts ...Option) (*Node, error) { if err != nil { return nil, err } - txnSource.db = &db var node *net.Node if !options.disableP2P { diff --git a/tests/clients/cli/wrapper_lens.go b/tests/clients/cli/wrapper_lens.go index a9f3e20bd1..3aac1ae392 100644 --- a/tests/clients/cli/wrapper_lens.go +++ b/tests/clients/cli/wrapper_lens.go @@ -28,6 +28,8 @@ type LensRegistry struct { cmd *cliWrapper } +func (w *LensRegistry) Init(txnSource client.TxnSource) {} + func (w *LensRegistry) SetMigration(ctx context.Context, collectionID uint32, config model.Lens) error { args := []string{"client", "schema", "migration", "set-registry"}