Skip to content

Commit

Permalink
SQUASH if it works
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Jun 4, 2024
1 parent ba2b06b commit 3c74848
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 30 deletions.
10 changes: 10 additions & 0 deletions client/lens.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)].
//
Expand Down
2 changes: 2 additions & 0 deletions http/client_lens.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type setMigrationRequest struct {
Config model.Lens
}

func (w *LensRegistry) Init(txnSource client.TxnSource) {}

Check warning on line 38 in http/client_lens.go

View check run for this annotation

Codecov / codecov/patch

http/client_lens.go#L38

Added line #L38 was not covered by tests

func (c *LensRegistry) SetMigration(ctx context.Context, collectionID uint32, config model.Lens) error {
methodURL := c.http.baseURL.JoinPath("lens", "registry")

Expand Down
4 changes: 4 additions & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func newDB(
opt(db)
}

if lens != nil {
lens.Init(db)
}

err = db.initialize(ctx)
if err != nil {
return nil, err
Expand Down
8 changes: 0 additions & 8 deletions internal/lens/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,13 @@ 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

// NewRegistry instantiates a new registery.
//
// 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 {
Expand All @@ -97,7 +90,6 @@ func NewRegistry(
}

return &implicitTxnLensRegistry{
db: db,
registry: registry,
}
}
Expand Down
11 changes: 5 additions & 6 deletions internal/lens/txn_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

type implicitTxnLensRegistry struct {
registry *lensRegistry
db TxnSource
db client.TxnSource
}

type explicitTxnLensRegistry struct {
Expand All @@ -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) {}

Check warning on line 40 in internal/lens/txn_registry.go

View check run for this annotation

Codecov / codecov/patch

internal/lens/txn_registry.go#L40

Added line #L40 was not covered by tests

func (r *explicitTxnLensRegistry) WithTxn(txn datastore.Txn) client.LensRegistry {
return &explicitTxnLensRegistry{
registry: r.registry,
Expand Down
13 changes: 0 additions & 13 deletions node/lens.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -63,7 +62,6 @@ func WithLensPoolSize(size int) Option {

func NewLens(
ctx context.Context,
txnSource *txnSource,
opts ...LenOpt,
) (client.LensRegistry, error) {
options := DefaultLensOptions()
Expand All @@ -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)
}
4 changes: 1 addition & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 133 in node/node.go

View check run for this annotation

Codecov / codecov/patch

node/node.go#L133

Added line #L133 was not covered by tests
}
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions tests/clients/cli/wrapper_lens.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down

0 comments on commit 3c74848

Please sign in to comment.