registry.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/plugin"
  17. "sync"
  18. )
  19. type registry struct {
  20. sync.RWMutex
  21. plugins map[string]*PluginInfo
  22. // mapping from symbol to plugin. Deduced from plugin set.
  23. sources map[string]string
  24. sinks map[string]string
  25. functions map[string]string
  26. }
  27. // Set prerequisite: the pluginInfo must have been validated that the names are valid
  28. func (r *registry) Set(name string, pi *PluginInfo) {
  29. r.Lock()
  30. defer r.Unlock()
  31. r.plugins[name] = pi
  32. for _, s := range pi.Sources {
  33. r.sources[s] = name
  34. }
  35. for _, s := range pi.Sinks {
  36. r.sinks[s] = name
  37. }
  38. for _, s := range pi.Functions {
  39. r.functions[s] = name
  40. }
  41. }
  42. func (r *registry) Get(name string) (*PluginInfo, bool) {
  43. r.RLock()
  44. defer r.RUnlock()
  45. result, ok := r.plugins[name]
  46. return result, ok
  47. }
  48. func (r *registry) GetSymbol(pt plugin.PluginType, symbolName string) (string, bool) {
  49. switch pt {
  50. case plugin.SOURCE:
  51. s, ok := r.sources[symbolName]
  52. return s, ok
  53. case plugin.SINK:
  54. s, ok := r.sinks[symbolName]
  55. return s, ok
  56. case plugin.FUNCTION:
  57. s, ok := r.functions[symbolName]
  58. return s, ok
  59. default:
  60. return "", false
  61. }
  62. }
  63. func (r *registry) List() []*PluginInfo {
  64. r.RLock()
  65. defer r.RUnlock()
  66. // return empty slice instead of nil to help json marshal
  67. result := make([]*PluginInfo, 0, len(r.plugins))
  68. for _, v := range r.plugins {
  69. result = append(result, v)
  70. }
  71. return result
  72. }
  73. func (r *registry) Delete(name string) {
  74. r.Lock()
  75. defer r.Unlock()
  76. pi, ok := r.plugins[name]
  77. if !ok {
  78. return
  79. }
  80. delete(r.plugins, name)
  81. for _, s := range pi.Sources {
  82. delete(r.sources, s)
  83. }
  84. for _, s := range pi.Sinks {
  85. delete(r.sinks, s)
  86. }
  87. for _, s := range pi.Functions {
  88. delete(r.functions, s)
  89. }
  90. }