registry.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright erfenjiao, 630166475@qq.com.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package wasm
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "sync"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "github.com/lf-edge/ekuiper/internal/plugin"
  21. )
  22. type registry struct {
  23. sync.RWMutex
  24. plugins map[string]*PluginInfo
  25. functions map[string]string
  26. }
  27. // Set prerequisite: the pluginInfo must have been validated that the names are valid
  28. func (r *registry) Set(name string, pi *PluginInfo) {
  29. r.Lock()
  30. defer r.Unlock()
  31. pluginDir, err := conf.GetPluginsLoc()
  32. if err != nil {
  33. fmt.Println("[internal][wasm] cannot find plugins folder:", err)
  34. }
  35. wasmPath := filepath.Join(pluginDir, "wasm", name, name+".wasm")
  36. pi.WasmFile = wasmPath
  37. r.plugins[name] = pi
  38. for _, s := range pi.Functions {
  39. r.functions[s] = name
  40. }
  41. }
  42. func (r *registry) Get(name string) (*PluginInfo, bool) {
  43. r.RLock()
  44. defer r.RUnlock()
  45. result, ok := r.plugins[name]
  46. return result, ok
  47. }
  48. func (r *registry) GetSymbol(pt plugin.PluginType, symbolName string) (string, bool) {
  49. switch pt {
  50. case plugin.FUNCTION:
  51. s, ok := r.functions[symbolName]
  52. return s, ok
  53. default:
  54. return "", false
  55. }
  56. }
  57. func (r *registry) List() []*PluginInfo {
  58. r.RLock()
  59. defer r.RUnlock()
  60. // return empty slice instead of nil to help json marshal
  61. result := make([]*PluginInfo, 0, len(r.plugins))
  62. for _, v := range r.plugins {
  63. result = append(result, v)
  64. }
  65. return result
  66. }
  67. func (r *registry) Delete(name string) {
  68. r.Lock()
  69. defer r.Unlock()
  70. pi, ok := r.plugins[name]
  71. if !ok {
  72. return
  73. }
  74. delete(r.plugins, name)
  75. for _, s := range pi.Functions {
  76. delete(r.functions, s)
  77. }
  78. }