source.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 memory
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/pkg/api"
  18. "regexp"
  19. "strings"
  20. )
  21. type source struct {
  22. topic string
  23. topicRegex *regexp.Regexp
  24. input <-chan api.SourceTuple
  25. }
  26. func (s *source) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, _ chan<- error) {
  27. ch := createSub(s.topic, s.topicRegex, fmt.Sprintf("%s_%s_%d", ctx.GetRuleId(), ctx.GetOpId(), ctx.GetInstanceId()))
  28. s.input = ch
  29. for {
  30. select {
  31. case v, opened := <-s.input:
  32. if !opened {
  33. return
  34. }
  35. consumer <- v
  36. case <-ctx.Done():
  37. return
  38. }
  39. }
  40. }
  41. func (s *source) Configure(datasource string, _ map[string]interface{}) error {
  42. s.topic = datasource
  43. if strings.ContainsAny(datasource, "+#") {
  44. r, err := getRegexp(datasource)
  45. if err != nil {
  46. return err
  47. }
  48. s.topicRegex = r
  49. }
  50. return nil
  51. }
  52. func getRegexp(topic string) (*regexp.Regexp, error) {
  53. if len(topic) == 0 {
  54. return nil, fmt.Errorf("invalid empty topic")
  55. }
  56. levels := strings.Split(topic, "/")
  57. for i, level := range levels {
  58. if level == "#" && i != len(levels)-1 {
  59. return nil, fmt.Errorf("invalid topic %s: # must at the last level", topic)
  60. }
  61. }
  62. regstr := strings.Replace(strings.ReplaceAll(topic, "+", "([^/]+)"), "#", ".", 1)
  63. return regexp.Compile(regstr)
  64. }
  65. func (s *source) Close(ctx api.StreamContext) error {
  66. ctx.GetLogger().Debugf("closing memory source")
  67. closeSourceConsumerChannel(s.topic, fmt.Sprintf("%s_%s_%d", ctx.GetRuleId(), ctx.GetOpId(), ctx.GetInstanceId()))
  68. return nil
  69. }