lookup.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. //go:build redisdb || !core
  15. package redis
  16. import (
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "github.com/go-redis/redis/v7"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. "github.com/lf-edge/ekuiper/pkg/cast"
  23. )
  24. type conf struct {
  25. // host:port address.
  26. Addr string `json:"addr,omitempty"`
  27. Username string `json:"username,omitempty"`
  28. // Optional password. Must match the password specified in the
  29. Password string `json:"password,omitempty"`
  30. DataType string `json:"dataType,omitempty"`
  31. }
  32. type lookupSource struct {
  33. c *conf
  34. db int
  35. cli *redis.Client
  36. }
  37. func (s *lookupSource) Configure(datasource string, props map[string]interface{}) error {
  38. db, err := cast.ToInt(datasource, cast.CONVERT_ALL)
  39. if err != nil {
  40. return fmt.Errorf("invalid datasource, it must be an integer but got %s", datasource)
  41. }
  42. s.db = db
  43. cfg := &conf{}
  44. err = cast.MapToStruct(props, cfg)
  45. if err != nil {
  46. return err
  47. }
  48. if cfg.Addr == "" {
  49. return errors.New("redis addr is null")
  50. }
  51. if cfg.DataType != "string" && cfg.DataType != "list" {
  52. return errors.New("redis dataType must be string or list")
  53. }
  54. s.c = cfg
  55. return nil
  56. }
  57. func (s *lookupSource) Open(ctx api.StreamContext) error {
  58. ctx.GetLogger().Infof("Opening redis lookup source with conf %v", s.c)
  59. s.cli = redis.NewClient(&redis.Options{
  60. Addr: s.c.Addr,
  61. Username: s.c.Username,
  62. Password: s.c.Password,
  63. DB: s.db,
  64. })
  65. return nil
  66. }
  67. func (s *lookupSource) Lookup(ctx api.StreamContext, _ []string, keys []string, values []interface{}) ([]api.SourceTuple, error) {
  68. ctx.GetLogger().Debugf("Lookup redis %v", keys)
  69. if len(keys) != 1 {
  70. return nil, fmt.Errorf("redis lookup only support one key, but got %v", keys)
  71. }
  72. v := fmt.Sprintf("%v", values[0])
  73. if s.c.DataType == "string" {
  74. res, err := s.cli.Get(v).Result()
  75. if err != nil {
  76. if err == redis.Nil {
  77. return []api.SourceTuple{}, nil
  78. }
  79. return nil, err
  80. }
  81. m := make(map[string]interface{})
  82. err = json.Unmarshal([]byte(res), &m)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return []api.SourceTuple{api.NewDefaultSourceTuple(m, nil)}, nil
  87. } else {
  88. res, err := s.cli.LRange(v, 0, -1).Result()
  89. if err != nil {
  90. if err == redis.Nil {
  91. return []api.SourceTuple{}, nil
  92. }
  93. return nil, err
  94. }
  95. ret := make([]api.SourceTuple, 0, len(res))
  96. for _, r := range res {
  97. m := make(map[string]interface{})
  98. err = json.Unmarshal([]byte(r), &m)
  99. if err != nil {
  100. return nil, err
  101. }
  102. ret = append(ret, api.NewDefaultSourceTuple(m, nil))
  103. }
  104. return ret, nil
  105. }
  106. }
  107. func (s *lookupSource) Close(ctx api.StreamContext) error {
  108. ctx.GetLogger().Infof("Closing redis lookup source")
  109. return s.cli.Close()
  110. }
  111. func GetLookupSource() api.LookupSource {
  112. return &lookupSource{}
  113. }