A wazero host module, ABI and guest SDK providing memory mapped databases for WASI modules.
First register the host module with the runtime
import (
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/pantopic/ext-mdb/host-wazero-lmdb"
)
func main() {
ctx := context.Background()
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig())
wasi_snapshot_preview1.MustInstantiate(ctx, r)
module := wazero_lmdb.New()
module.Register(ctx, r)
// ...
}Then you can import the guest SDK into your WASI module to create and manage LMDB environments from WASM.
package main
import (
"unsafe"
"github.com/pantopic/ext-mdb/sdk-go"
)
func main() {}
//export set
func set() {
mdb.Update(func(txn *lmdb.Txn) error {
dbi, _ := txn.DbCreate("test")
return txn.Put(dbi, []byte(`hello`), []byte(`world`))
})
}
//export get
func get() uint64 {
var val []byte
mdb.View(func(txn *lmdb.Txn) (err error) {
dbi, _ := txn.DbOpen("test")
val, err = txn.Get(dbi, []byte(`hello`))
return
})
return uint64(uintptr(unsafe.Pointer(&val[0])))<<32 + uint64(len(val))
}The guest SDK has no dependencies outside the Go std lib. The ABI is ~130 lines of code and the SDK is ~400 lines of code so it should be simple to port this guest SDK if you want to use the Host Module from WASM modules written in other guest languages (i.e. Rust). Contributions welcome.
Wazero prides itself on having no dependencies and neither does lmdb-go (apart from CGO), so your go.sum should remain tidy.
This project is in alpha. Breaking API changes should be expected until Beta.
v0.0.x- Alpha- Stabilize API
v0.x.x- Beta- Finalize API
- Test in production
v1.x.x- General Availability- Proven long term stability in production