factory.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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) LookupSource(_ string) (api.LookupSource, error) {
  30. // TODO add support
  31. return nil, nil
  32. }
  33. func (m *Manager) Sink(name string) (api.Sink, error) {
  34. meta, ok := m.GetPluginMeta(plugin.SINK, name)
  35. if !ok {
  36. return nil, nil
  37. }
  38. return runtime.NewPortableSink(name, meta), nil
  39. }
  40. // The function instance are kept forever even after deletion
  41. // The instance is actually a wrapper of the nng channel which is dependant from the plugin instance
  42. // Even updated plugin instance can reuse the channel if the function name is not changed
  43. // It is not used to check if the function is bound, use ConvName which checks the meta
  44. var funcInsMap = &sync.Map{}
  45. func (m *Manager) Function(name string) (api.Function, error) {
  46. ins, ok := funcInsMap.Load(name)
  47. if ok {
  48. return ins.(api.Function), nil
  49. }
  50. meta, ok := m.GetPluginMeta(plugin.FUNCTION, name)
  51. if !ok {
  52. return nil, nil
  53. }
  54. f, err := runtime.NewPortableFunc(name, meta)
  55. if err != nil {
  56. conf.Log.Errorf("Error creating portable function %v", err)
  57. return nil, err
  58. }
  59. funcInsMap.Store(name, f)
  60. return f, nil
  61. }
  62. func (m *Manager) HasFunctionSet(funcName string) bool {
  63. _, ok := m.reg.GetSymbol(plugin.FUNCTION, funcName)
  64. return ok
  65. }
  66. func (m *Manager) ConvName(funcName string) (string, bool) {
  67. _, ok := m.GetPluginMeta(plugin.FUNCTION, funcName)
  68. return funcName, ok
  69. }