2022-09-02 19:23:33 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2022-09-21 05:45:23 +00:00
|
|
|
"sync"
|
2022-09-02 19:23:33 +00:00
|
|
|
|
|
|
|
"go.k6.io/k6/js/modules"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RootModule is the global module object type. It is instantiated once per test
|
|
|
|
// run and will be used to create k6/x/neofs/registry module instances for each VU.
|
|
|
|
type RootModule struct {
|
2022-09-21 05:45:23 +00:00
|
|
|
// Stores object registry by path of database file. We should have only single instance
|
|
|
|
// of registry per each file
|
|
|
|
registries map[string]*ObjRegistry
|
|
|
|
// Mutex to sync access to repositories map
|
|
|
|
mu sync.Mutex
|
2022-09-02 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Registry represents an instance of the module for every VU.
|
|
|
|
type Registry struct {
|
|
|
|
vu modules.VU
|
|
|
|
root *RootModule
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the interfaces are implemented correctly.
|
|
|
|
var (
|
|
|
|
_ modules.Instance = &Registry{}
|
|
|
|
_ modules.Module = &RootModule{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-09-21 05:45:23 +00:00
|
|
|
rootModule := &RootModule{registries: make(map[string]*ObjRegistry)}
|
2022-09-02 19:23:33 +00:00
|
|
|
modules.Register("k6/x/neofs/registry", rootModule)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewModuleInstance implements the modules.Module interface and returns
|
|
|
|
// a new instance for each VU.
|
|
|
|
func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
|
|
|
|
mi := &Registry{vu: vu, root: r}
|
|
|
|
return mi
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exports implements the modules.Instance interface and returns the exports
|
|
|
|
// of the JS module.
|
|
|
|
func (r *Registry) Exports() modules.Exports {
|
|
|
|
return modules.Exports{Default: r}
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:45:23 +00:00
|
|
|
// Open creates a new instance of object registry that will store information about objects
|
|
|
|
// in the specified file. If repository instance for the file was previously created, then
|
|
|
|
// Open will return the existing instance of repository, because bolt database allows only
|
2022-09-22 16:57:21 +00:00
|
|
|
// one write connection at a time.
|
2022-09-21 05:45:23 +00:00
|
|
|
func (r *Registry) Open(dbFilePath string) *ObjRegistry {
|
|
|
|
r.root.mu.Lock()
|
|
|
|
defer r.root.mu.Unlock()
|
2022-09-02 19:23:33 +00:00
|
|
|
|
2022-09-21 05:45:23 +00:00
|
|
|
registry := r.root.registries[dbFilePath]
|
|
|
|
if registry == nil {
|
|
|
|
registry = NewObjRegistry(dbFilePath)
|
|
|
|
r.root.registries[dbFilePath] = registry
|
|
|
|
}
|
|
|
|
return registry
|
2022-09-02 19:23:33 +00:00
|
|
|
}
|