factory.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2021 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) Sink(name string) (api.Sink, error) {
  30. meta, ok := m.GetPluginMeta(plugin.SINK, name)
  31. if !ok {
  32. return nil, nil
  33. }
  34. return runtime.NewPortableSink(name, meta), nil
  35. }
  36. var funcInsMap = &sync.Map{}
  37. func (m *Manager) Function(name string) (api.Function, error) {
  38. ins, ok := funcInsMap.Load(name)
  39. if ok {
  40. return ins.(api.Function), nil
  41. }
  42. meta, ok := m.GetPluginMeta(plugin.FUNCTION, name)
  43. if !ok {
  44. return nil, nil
  45. }
  46. f, err := runtime.NewPortableFunc(name, meta)
  47. if err != nil {
  48. conf.Log.Errorf("Error creating function %v", err)
  49. return nil, err
  50. }
  51. funcInsMap.Store(name, f)
  52. return f, nil
  53. }
  54. func (m *Manager) HasFunctionSet(funcName string) bool {
  55. _, ok := m.reg.GetSymbol(plugin.FUNCTION, funcName)
  56. return ok
  57. }
  58. func (m *Manager) ConvName(funcName string) (string, bool) {
  59. _, ok := m.GetPluginMeta(plugin.FUNCTION, funcName)
  60. return funcName, ok
  61. }
  62. // Clean up function map
  63. func (m *Manager) Clean() {
  64. funcInsMap.Range(func(_, ins interface{}) bool {
  65. f := ins.(*runtime.PortableFunc)
  66. _ = f.Close()
  67. return true
  68. })
  69. }