lookupsource.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 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 memory
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/topo/memory/store"
  17. "github.com/lf-edge/ekuiper/pkg/api"
  18. "github.com/lf-edge/ekuiper/pkg/cast"
  19. "regexp"
  20. "strings"
  21. )
  22. // lookupsource is a lookup source that reads data from memory
  23. // The memory lookup table reads a global memory store for data
  24. type lookupsource struct {
  25. topic string
  26. topicRegex *regexp.Regexp
  27. keys []string
  28. table *store.Table
  29. }
  30. func (s *lookupsource) Open(ctx api.StreamContext) error {
  31. ctx.GetLogger().Infof("lookup source %s is opened with keys %v", s.topic, s.keys)
  32. var err error
  33. s.table, err = store.Reg(s.topic, s.topicRegex, s.keys)
  34. return err
  35. }
  36. func (s *lookupsource) Configure(datasource string, props map[string]interface{}) error {
  37. s.topic = datasource
  38. if strings.ContainsAny(datasource, "+#") {
  39. r, err := getRegexp(datasource)
  40. if err != nil {
  41. return err
  42. }
  43. s.topicRegex = r
  44. }
  45. if c, ok := props["index"]; ok {
  46. if bl, err := cast.ToStringSlice(c, cast.CONVERT_SAMEKIND); err != nil {
  47. s.keys = bl
  48. }
  49. }
  50. return nil
  51. }
  52. func (s *lookupsource) Lookup(ctx api.StreamContext, _ []string, keys []string, values []interface{}) ([]api.SourceTuple, error) {
  53. ctx.GetLogger().Debugf("lookup source %s is looking up keys %v with values %v", s.topic, keys, values)
  54. return s.table.Read(keys, values)
  55. }
  56. func (s *lookupsource) Close(ctx api.StreamContext) error {
  57. ctx.GetLogger().Infof("lookup source %s is closing", s.topic)
  58. return store.Unreg(s.topic)
  59. }