redisTs.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. //go:build redisdb || !core
  15. // +build redisdb !core
  16. package redis
  17. import (
  18. "bytes"
  19. "encoding/gob"
  20. "fmt"
  21. "github.com/go-redis/redis/v7"
  22. kvEncoding "github.com/lf-edge/ekuiper/internal/pkg/store/encoding"
  23. "strconv"
  24. )
  25. const (
  26. TsPrefix = "KV:TS"
  27. AddToSortedSet = "ZADD"
  28. ReversedRangeByScore = "ZREVRANGEBYSCORE"
  29. RemoveRangeByScore = "ZREMRANGEBYSCORE"
  30. Delete = "DEL"
  31. ReversedRange = "ZREVRANGE"
  32. )
  33. type ts struct {
  34. db *redis.Client
  35. table string
  36. last int64
  37. key string
  38. }
  39. func init() {
  40. gob.Register(make(map[string]interface{}))
  41. }
  42. func createRedisTs(redis *redis.Client, table string) (error, *ts) {
  43. key := fmt.Sprintf("%s:%s", TsPrefix, table)
  44. lastTs, err := getLast(redis, key, nil)
  45. if err != nil {
  46. return err, nil
  47. }
  48. s := &ts{
  49. db: redis,
  50. table: table,
  51. last: lastTs,
  52. key: key,
  53. }
  54. return nil, s
  55. }
  56. func (t *ts) Set(key int64, value interface{}) (bool, error) {
  57. if key <= t.last {
  58. return false, nil
  59. }
  60. err, b := kvEncoding.Encode(value)
  61. if err != nil {
  62. return false, err
  63. }
  64. length, err := t.db.ZAdd(t.key, &redis.Z{Score: float64(key), Member: b}).Result()
  65. if err != nil {
  66. return false, err
  67. }
  68. if length == 0 {
  69. return false, fmt.Errorf("list at %s key should be non empty", t.key)
  70. }
  71. t.last = key
  72. return true, nil
  73. }
  74. func (t *ts) Get(key int64, value interface{}) (bool, error) {
  75. reply, err := t.db.ZRevRangeByScore(t.key, &redis.ZRangeBy{Min: strconv.FormatInt(key, 10), Max: strconv.FormatInt(key, 10)}).Result()
  76. if len(reply) == 0 {
  77. return false, fmt.Errorf("record under %s key and %d score not found", t.key, key)
  78. }
  79. dec := gob.NewDecoder(bytes.NewBuffer([]byte(reply[0])))
  80. err = dec.Decode(value)
  81. if err != nil {
  82. return false, err
  83. }
  84. return true, nil
  85. }
  86. func (t *ts) Last(value interface{}) (int64, error) {
  87. return getLast(t.db, t.key, value)
  88. }
  89. func (t *ts) Delete(key int64) error {
  90. return t.db.ZRemRangeByScore(t.key, strconv.FormatInt(key, 10), strconv.FormatInt(key, 10)).Err()
  91. }
  92. func (t *ts) DeleteBefore(key int64) error {
  93. return t.db.ZRemRangeByScore(t.key, "-inf", strconv.FormatInt(key, 10)).Err()
  94. }
  95. func (t *ts) Close() error {
  96. return nil
  97. }
  98. func (t *ts) Drop() error {
  99. return t.db.Del(t.key).Err()
  100. }
  101. func getLast(db *redis.Client, key string, value interface{}) (int64, error) {
  102. var last int64 = 0
  103. reply, err := db.ZRevRangeWithScores(key, 0, 0).Result()
  104. if len(reply) > 0 {
  105. if value != nil {
  106. v := reply[0].Member.(string)
  107. dec := gob.NewDecoder(bytes.NewBuffer([]byte(v)))
  108. if err = dec.Decode(value); err != nil {
  109. return 0, err
  110. }
  111. }
  112. last = int64(reply[0].Score)
  113. }
  114. return last, nil
  115. }