lookup.go 3.5 KB

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