factory.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  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 portable
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/conf"
  17. "github.com/lf-edge/ekuiper/internal/plugin"
  18. "github.com/lf-edge/ekuiper/internal/plugin/portable/runtime"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "sync"
  21. )
  22. func (m *Manager) Source(name string) (api.Source, error) {
  23. meta, ok := m.GetPluginMeta(plugin.SOURCE, name)
  24. if !ok {
  25. return nil, nil
  26. }
  27. return runtime.NewPortableSource(name, meta), nil
  28. }
  29. func (m *Manager) GetSourcePlugin(name string) (plugin.EXTENSION_TYPE, string, string) {
  30. pluginName, _ := m.reg.GetSymbol(plugin.SOURCE, name)
  31. var installScript = ""
  32. m.plgInstallDb.Get(pluginName, &installScript)
  33. return plugin.PORTABLE_EXTENSION, pluginName, installScript
  34. }
  35. func (m *Manager) LookupSource(_ string) (api.LookupSource, error) {
  36. // TODO add support
  37. return nil, nil
  38. }
  39. func (m *Manager) Sink(name string) (api.Sink, error) {
  40. meta, ok := m.GetPluginMeta(plugin.SINK, name)
  41. if !ok {
  42. return nil, nil
  43. }
  44. return runtime.NewPortableSink(name, meta), nil
  45. }
  46. func (m *Manager) GetSinkPlugin(name string) (plugin.EXTENSION_TYPE, string, string) {
  47. pluginName, _ := m.reg.GetSymbol(plugin.SINK, name)
  48. var installScript = ""
  49. m.plgInstallDb.Get(pluginName, &installScript)
  50. return plugin.PORTABLE_EXTENSION, pluginName, installScript
  51. }
  52. var funcInsMap = &sync.Map{}
  53. func (m *Manager) Function(name string) (api.Function, error) {
  54. ins, ok := funcInsMap.Load(name)
  55. if ok {
  56. return ins.(api.Function), nil
  57. }
  58. meta, ok := m.GetPluginMeta(plugin.FUNCTION, name)
  59. if !ok {
  60. return nil, nil
  61. }
  62. f, err := runtime.NewPortableFunc(name, meta)
  63. if err != nil {
  64. conf.Log.Errorf("Error creating function %v", err)
  65. return nil, err
  66. }
  67. funcInsMap.Store(name, f)
  68. return f, nil
  69. }
  70. func (m *Manager) HasFunctionSet(funcName string) bool {
  71. _, ok := m.reg.GetSymbol(plugin.FUNCTION, funcName)
  72. return ok
  73. }
  74. func (m *Manager) GetFunctionPlugin(funcName string) (plugin.EXTENSION_TYPE, string, string) {
  75. pluginName, _ := m.reg.GetSymbol(plugin.FUNCTION, funcName)
  76. var installScript = ""
  77. m.plgInstallDb.Get(pluginName, &installScript)
  78. return plugin.PORTABLE_EXTENSION, pluginName, installScript
  79. }
  80. func (m *Manager) ConvName(funcName string) (string, bool) {
  81. _, ok := m.GetPluginMeta(plugin.FUNCTION, funcName)
  82. return funcName, ok
  83. }
  84. // Clean up function map
  85. func (m *Manager) Clean() {
  86. funcInsMap.Range(func(_, ins interface{}) bool {
  87. f := ins.(*runtime.PortableFunc)
  88. _ = f.Close()
  89. return true
  90. })
  91. funcInsMap = &sync.Map{}
  92. }
  93. func (m *Manager) DeleteFunc(funcName string) {
  94. f, ok := funcInsMap.Load(funcName)
  95. if ok {
  96. _ = f.(*runtime.PortableFunc).Close()
  97. funcInsMap.Delete(funcName)
  98. }
  99. }