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