Skip to content

Commit

Permalink
move lens config to db
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Apr 8, 2024
1 parent fb13730 commit 0d16c11
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 80 deletions.
17 changes: 13 additions & 4 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ package db
import (
"context"

"github.com/lens-vm/lens/host-go/engine/module"
"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/acp"
"github.com/sourcenetwork/defradb/events"
"github.com/sourcenetwork/defradb/lens"
)

const (
Expand Down Expand Up @@ -56,9 +56,18 @@ func WithMaxRetries(num int) Option {
}
}

// WithLensOptions sets the lens registry options for the db.
func WithLensOptions(opts ...lens.Option) Option {
// WithLensPoolSize sets the maximum number of cached migrations instances to preserve per schema version.
//
// Will default to `5` if not set.
func WithLensPoolSize(size int) Option {
return func(db *db) {
db.lensPoolSize = immutable.Some(size)
}
}

// WithLensRuntime returns an option that sets the lens registry runtime.
func WithLensRuntime(runtime module.Runtime) Option {
return func(db *db) {
db.lensOptions = opts
db.lensRuntime = immutable.Some(runtime)
}
}
16 changes: 10 additions & 6 deletions db/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ package db
import (
"testing"

"github.com/lens-vm/lens/host-go/runtimes/wasmtime"
"github.com/stretchr/testify/assert"

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

func TestWithACP(t *testing.T) {
Expand Down Expand Up @@ -43,9 +42,14 @@ func TestWithMaxRetries(t *testing.T) {
assert.Equal(t, 10, d.maxTxnRetries.Value())
}

func TestWithLensOptions(t *testing.T) {
lensOpts := []lens.Option{lens.WithPoolSize(20)}
func TestWithLensPoolSize(t *testing.T) {
d := &db{}
WithLensPoolSize(10)(d)
assert.Equal(t, 10, d.lensPoolSize.Value())
}

func TestWithLensRuntime(t *testing.T) {
d := &db{}
WithLensOptions(lensOpts...)(d)
assert.Equal(t, d.lensOptions, lensOpts)
WithLensRuntime(wasmtime.New())(d)
assert.NotNil(t, d.lensRuntime.Value())
}
10 changes: 7 additions & 3 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
blockstore "github.com/ipfs/boxo/blockstore"
ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
"github.com/lens-vm/lens/host-go/engine/module"
"github.com/sourcenetwork/corelog"
"github.com/sourcenetwork/immutable"

Expand Down Expand Up @@ -56,7 +57,10 @@ type db struct {

parser core.Parser

lensOptions []lens.Option
// The maximum number of cached migrations instances to preserve per schema version.
lensPoolSize immutable.Option[int]
lensRuntime immutable.Option[module.Runtime]

lensRegistry client.LensRegistry

// The maximum number of retries per transaction.
Expand Down Expand Up @@ -106,9 +110,9 @@ func newDB(
opt(db)
}

// lens options may be set by `WithLensOptions`, and because they are funcs on db
// lens options may be set by `WithLens` funcs, and because they are funcs on db
// we have to mutate `db` here to set the registry.
db.lensRegistry = lens.NewRegistry(db, db.lensOptions...)
db.lensRegistry = lens.NewRegistry(db, db.lensPoolSize, db.lensRuntime)

err = db.initialize(ctx)
if err != nil {
Expand Down
32 changes: 0 additions & 32 deletions lens/config.go

This file was deleted.

30 changes: 0 additions & 30 deletions lens/config_test.go

This file was deleted.

14 changes: 11 additions & 3 deletions lens/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/lens-vm/lens/host-go/config/model"
"github.com/lens-vm/lens/host-go/engine/module"
"github.com/lens-vm/lens/host-go/runtimes/wasmtime"
"github.com/sourcenetwork/immutable"
"github.com/sourcenetwork/immutable/enumerable"

"github.com/sourcenetwork/defradb/client"
Expand Down Expand Up @@ -83,7 +84,11 @@ 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, opts ...Option) client.LensRegistry {
func NewRegistry(
db TxnSource,
poolSize immutable.Option[int],
runtime immutable.Option[module.Runtime],
) client.LensRegistry {
registry := &lensRegistry{
poolSize: DefaultPoolSize,
runtime: wasmtime.New(),
Expand All @@ -93,8 +98,11 @@ func NewRegistry(db TxnSource, opts ...Option) client.LensRegistry {
txnCtxs: map[uint64]*txnContext{},
}

for _, opt := range opts {
opt(registry)
if poolSize.HasValue() {
registry.poolSize = poolSize.Value()
}
if runtime.HasValue() {
registry.runtime = runtime.Value()
}

return &implicitTxnLensRegistry{
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
badgerds "github.com/sourcenetwork/defradb/datastore/badger/v4"
"github.com/sourcenetwork/defradb/datastore/memory"
"github.com/sourcenetwork/defradb/db"
"github.com/sourcenetwork/defradb/lens"
changeDetector "github.com/sourcenetwork/defradb/tests/change_detector"
)

Expand Down Expand Up @@ -134,7 +133,7 @@ func NewBadgerFileDB(ctx context.Context, t testing.TB, dbopts ...db.Option) (cl
func setupDatabase(s *state) (impl client.DB, path string, err error) {
dbopts := []db.Option{
db.WithUpdateEvents(),
db.WithLensOptions(lens.WithPoolSize(lensPoolSize)),
db.WithLensPoolSize(lensPoolSize),
}

switch s.dbt {
Expand Down

0 comments on commit 0d16c11

Please sign in to comment.