lookup.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2022-2023 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. if datasource != "/$$TEST_CONNECTION$$" {
  39. db, err := cast.ToInt(datasource, cast.CONVERT_ALL)
  40. if err != nil {
  41. return fmt.Errorf("invalid datasource, it must be an integer but got %s", datasource)
  42. }
  43. s.db = db
  44. } else {
  45. s.db = 0
  46. }
  47. cfg := &conf{}
  48. err := cast.MapToStruct(props, cfg)
  49. if err != nil {
  50. return err
  51. }
  52. if cfg.Addr == "" {
  53. return errors.New("redis addr is null")
  54. }
  55. if cfg.DataType != "string" && cfg.DataType != "list" {
  56. return errors.New("redis dataType must be string or list")
  57. }
  58. s.c = cfg
  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. _, err = s.cli.Ping().Result()
  66. return err
  67. }
  68. func (s *lookupSource) Open(ctx api.StreamContext) error {
  69. ctx.GetLogger().Infof("Opening redis lookup source with conf %v", s.c)
  70. return nil
  71. }
  72. func (s *lookupSource) Lookup(ctx api.StreamContext, _ []string, keys []string, values []interface{}) ([]api.SourceTuple, error) {
  73. ctx.GetLogger().Debugf("Lookup redis %v", keys)
  74. if len(keys) != 1 {
  75. return nil, fmt.Errorf("redis lookup only support one key, but got %v", keys)
  76. }
  77. v := fmt.Sprintf("%v", values[0])
  78. if s.c.DataType == "string" {
  79. res, err := s.cli.Get(v).Result()
  80. if err != nil {
  81. if err == redis.Nil {
  82. return []api.SourceTuple{}, nil
  83. }
  84. return nil, err
  85. }
  86. m := make(map[string]interface{})
  87. err = json.Unmarshal([]byte(res), &m)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return []api.SourceTuple{api.NewDefaultSourceTuple(m, nil)}, nil
  92. } else {
  93. res, err := s.cli.LRange(v, 0, -1).Result()
  94. if err != nil {
  95. if err == redis.Nil {
  96. return []api.SourceTuple{}, nil
  97. }
  98. return nil, err
  99. }
  100. ret := make([]api.SourceTuple, 0, len(res))
  101. for _, r := range res {
  102. m := make(map[string]interface{})
  103. err = json.Unmarshal([]byte(r), &m)
  104. if err != nil {
  105. return nil, err
  106. }
  107. ret = append(ret, api.NewDefaultSourceTuple(m, nil))
  108. }
  109. return ret, nil
  110. }
  111. }
  112. func (s *lookupSource) Close(ctx api.StreamContext) error {
  113. ctx.GetLogger().Infof("Closing redis lookup source")
  114. return s.cli.Close()
  115. }
  116. func GetLookupSource() api.LookupSource {
  117. return &lookupSource{}
  118. }