redisTs.go 3.3 KB

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