forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Lens runtime config (sourcenetwork#2497)
## Relevant issue(s) Resolves sourcenetwork#2496 ## Description This PR adds a lens runtime option. It also includes some cleanup and additional tests. ## Tasks - [x] I made sure the code is well commented, particularly hard-to-understand areas. - [x] I made sure the repository-held documentation is changed accordingly. - [x] I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in [tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)). - [x] I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ... ## How has this been tested? `make test` Specify the platform(s) on which this was tested: - MacOS
- Loading branch information
Showing
4 changed files
with
159 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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 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" | ||
) | ||
|
||
const ( | ||
defaultMaxTxnRetries = 5 | ||
updateEventBufferSize = 100 | ||
) | ||
|
||
// Option is a funtion that sets a config value on the db. | ||
type Option func(*db) | ||
|
||
// WithACP enables access control. If path is empty then acp runs in-memory. | ||
func WithACP(path string) Option { | ||
return func(db *db) { | ||
var acpLocal acp.ACPLocal | ||
acpLocal.Init(context.Background(), path) | ||
db.acp = immutable.Some[acp.ACP](&acpLocal) | ||
} | ||
} | ||
|
||
// WithACPInMemory enables access control in-memory. | ||
func WithACPInMemory() Option { return WithACP("") } | ||
|
||
// WithUpdateEvents enables the update events channel. | ||
func WithUpdateEvents() Option { | ||
return func(db *db) { | ||
db.events = events.Events{ | ||
Updates: immutable.Some(events.New[events.Update](0, updateEventBufferSize)), | ||
} | ||
} | ||
} | ||
|
||
// WithMaxRetries sets the maximum number of retries per transaction. | ||
func WithMaxRetries(num int) Option { | ||
return func(db *db) { | ||
db.maxTxnRetries = immutable.Some(num) | ||
} | ||
} | ||
|
||
// 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.lensRuntime = immutable.Some(runtime) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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 db | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/lens-vm/lens/host-go/runtimes/wasmtime" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWithACP(t *testing.T) { | ||
d := &db{} | ||
WithACP("test")(d) | ||
assert.True(t, d.acp.HasValue()) | ||
} | ||
|
||
func TestWithACPInMemory(t *testing.T) { | ||
d := &db{} | ||
WithACPInMemory()(d) | ||
assert.True(t, d.acp.HasValue()) | ||
} | ||
|
||
func TestWithUpdateEvents(t *testing.T) { | ||
d := &db{} | ||
WithUpdateEvents()(d) | ||
assert.NotNil(t, d.events) | ||
} | ||
|
||
func TestWithMaxRetries(t *testing.T) { | ||
d := &db{} | ||
WithMaxRetries(10)(d) | ||
assert.True(t, d.maxTxnRetries.HasValue()) | ||
assert.Equal(t, 10, d.maxTxnRetries.Value()) | ||
} | ||
|
||
func TestWithLensPoolSize(t *testing.T) { | ||
d := &db{} | ||
WithLensPoolSize(10)(d) | ||
assert.Equal(t, 10, d.lensPoolSize.Value()) | ||
} | ||
|
||
func TestWithLensRuntime(t *testing.T) { | ||
d := &db{} | ||
WithLensRuntime(wasmtime.New())(d) | ||
assert.NotNil(t, d.lensRuntime.Value()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters