factory.go 2.2 KB

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