factory.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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) SourcePluginInfo(name string) (plugin.EXTENSION_TYPE, string, string) {
  30. pluginName, ok := m.reg.GetSymbol(plugin.SOURCE, name)
  31. if ok {
  32. var installScript = ""
  33. m.plgInstallDb.Get(pluginName, &installScript)
  34. return plugin.PORTABLE_EXTENSION, pluginName, installScript
  35. } else {
  36. return plugin.NONE_EXTENSION, "", ""
  37. }
  38. }
  39. func (m *Manager) LookupSource(_ string) (api.LookupSource, error) {
  40. // TODO add support
  41. return nil, nil
  42. }
  43. func (m *Manager) Sink(name string) (api.Sink, error) {
  44. meta, ok := m.GetPluginMeta(plugin.SINK, name)
  45. if !ok {
  46. return nil, nil
  47. }
  48. return runtime.NewPortableSink(name, meta), nil
  49. }
  50. func (m *Manager) SinkPluginInfo(name string) (plugin.EXTENSION_TYPE, string, string) {
  51. pluginName, ok := m.reg.GetSymbol(plugin.SINK, name)
  52. if ok {
  53. var installScript = ""
  54. m.plgInstallDb.Get(pluginName, &installScript)
  55. return plugin.PORTABLE_EXTENSION, pluginName, installScript
  56. } else {
  57. return plugin.NONE_EXTENSION, "", ""
  58. }
  59. }
  60. // The function instance are kept forever even after deletion
  61. // The instance is actually a wrapper of the nng channel which is dependant from the plugin instance
  62. // Even updated plugin instance can reuse the channel if the function name is not changed
  63. // It is not used to check if the function is bound, use ConvName which checks the meta
  64. var funcInsMap = &sync.Map{}
  65. func (m *Manager) Function(name string) (api.Function, error) {
  66. ins, ok := funcInsMap.Load(name)
  67. if ok {
  68. return ins.(api.Function), nil
  69. }
  70. meta, ok := m.GetPluginMeta(plugin.FUNCTION, name)
  71. if !ok {
  72. return nil, nil
  73. }
  74. f, err := runtime.NewPortableFunc(name, meta)
  75. if err != nil {
  76. conf.Log.Errorf("Error creating portable function %v", err)
  77. return nil, err
  78. }
  79. funcInsMap.Store(name, f)
  80. return f, nil
  81. }
  82. func (m *Manager) HasFunctionSet(funcName string) bool {
  83. _, ok := m.reg.GetSymbol(plugin.FUNCTION, funcName)
  84. return ok
  85. }
  86. func (m *Manager) FunctionPluginInfo(funcName string) (plugin.EXTENSION_TYPE, string, string) {
  87. pluginName, ok := m.reg.GetSymbol(plugin.FUNCTION, funcName)
  88. if ok {
  89. var installScript = ""
  90. m.plgInstallDb.Get(pluginName, &installScript)
  91. return plugin.PORTABLE_EXTENSION, pluginName, installScript
  92. } else {
  93. return plugin.NONE_EXTENSION, "", ""
  94. }
  95. }
  96. func (m *Manager) ConvName(funcName string) (string, bool) {
  97. _, ok := m.GetPluginMeta(plugin.FUNCTION, funcName)
  98. return funcName, ok
  99. }