|
@@ -21,7 +21,7 @@ import (
|
|
|
"bytes"
|
|
|
"encoding/gob"
|
|
|
"fmt"
|
|
|
- "github.com/gomodule/redigo/redis"
|
|
|
+ "github.com/go-redis/redis/v7"
|
|
|
kvEncoding "github.com/lf-edge/ekuiper/internal/pkg/store/encoding"
|
|
|
"strconv"
|
|
|
)
|
|
@@ -36,7 +36,7 @@ const (
|
|
|
)
|
|
|
|
|
|
type ts struct {
|
|
|
- redis *Instance
|
|
|
+ db *redis.Client
|
|
|
table string
|
|
|
last int64
|
|
|
key string
|
|
@@ -46,14 +46,14 @@ func init() {
|
|
|
gob.Register(make(map[string]interface{}))
|
|
|
}
|
|
|
|
|
|
-func createRedisTs(redis *Instance, table string) (error, *ts) {
|
|
|
+func createRedisTs(redis *redis.Client, table string) (error, *ts) {
|
|
|
key := fmt.Sprintf("%s:%s", TsPrefix, table)
|
|
|
- err, lastTs := getLast(redis, key)
|
|
|
+ lastTs, err := getLast(redis, key, nil)
|
|
|
if err != nil {
|
|
|
return err, nil
|
|
|
}
|
|
|
s := &ts{
|
|
|
- redis: redis,
|
|
|
+ db: redis,
|
|
|
table: table,
|
|
|
last: lastTs,
|
|
|
key: key,
|
|
@@ -69,124 +69,62 @@ func (t *ts) Set(key int64, value interface{}) (bool, error) {
|
|
|
if err != nil {
|
|
|
return false, err
|
|
|
}
|
|
|
- err = t.redis.Apply(func(conn redis.Conn) error {
|
|
|
- reply, err := conn.Do(AddToSortedSet, t.key, key, b)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- length, err := redis.Int(reply, err)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- if length == 0 {
|
|
|
- return fmt.Errorf("list at %s key should be non empty", t.key)
|
|
|
- }
|
|
|
- t.last = key
|
|
|
- return nil
|
|
|
- })
|
|
|
+ length, err := t.db.ZAdd(t.key, &redis.Z{Score: float64(key), Member: b}).Result()
|
|
|
if err != nil {
|
|
|
return false, err
|
|
|
}
|
|
|
+ if length == 0 {
|
|
|
+ return false, fmt.Errorf("list at %s key should be non empty", t.key)
|
|
|
+ }
|
|
|
+ t.last = key
|
|
|
return true, nil
|
|
|
}
|
|
|
|
|
|
-func (t ts) Get(key int64, value interface{}) (bool, error) {
|
|
|
- err := t.redis.Apply(func(conn redis.Conn) error {
|
|
|
- reply, err := conn.Do(ReversedRangeByScore, t.key, key, key)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- var tmp [][]byte
|
|
|
- tmp, err = redis.ByteSlices(reply, err)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- if len(tmp) == 0 {
|
|
|
- return fmt.Errorf("record under %s key and %d score not found", t.key, key)
|
|
|
- }
|
|
|
- dec := gob.NewDecoder(bytes.NewBuffer(tmp[0]))
|
|
|
- err = dec.Decode(value)
|
|
|
- return err
|
|
|
- })
|
|
|
+func (t *ts) Get(key int64, value interface{}) (bool, error) {
|
|
|
+ reply, err := t.db.ZRevRangeByScore(t.key, &redis.ZRangeBy{Min: strconv.FormatInt(key, 10), Max: strconv.FormatInt(key, 10)}).Result()
|
|
|
+ if len(reply) == 0 {
|
|
|
+ return false, fmt.Errorf("record under %s key and %d score not found", t.key, key)
|
|
|
+ }
|
|
|
+ dec := gob.NewDecoder(bytes.NewBuffer([]byte(reply[0])))
|
|
|
+ err = dec.Decode(value)
|
|
|
if err != nil {
|
|
|
return false, err
|
|
|
}
|
|
|
return true, nil
|
|
|
}
|
|
|
|
|
|
-func (t ts) Last(value interface{}) (int64, error) {
|
|
|
- var last int64 = 0
|
|
|
- err := t.redis.Apply(func(conn redis.Conn) error {
|
|
|
- reply, err := conn.Do(ReversedRange, t.key, 0, 0, "WITHSCORES")
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- var tmp [][]byte
|
|
|
- tmp, err = redis.ByteSlices(reply, err)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- if len(tmp) > 0 {
|
|
|
- dec := gob.NewDecoder(bytes.NewBuffer(tmp[0]))
|
|
|
- if err = dec.Decode(value); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- last, err = strconv.ParseInt(string(tmp[1]), 10, 64)
|
|
|
- }
|
|
|
- return err
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return 0, err
|
|
|
- }
|
|
|
- return last, nil
|
|
|
+func (t *ts) Last(value interface{}) (int64, error) {
|
|
|
+ return getLast(t.db, t.key, value)
|
|
|
}
|
|
|
|
|
|
-func (t ts) Delete(key int64) error {
|
|
|
- return t.redis.Apply(func(conn redis.Conn) error {
|
|
|
- _, err := conn.Do(RemoveRangeByScore, t.key, key, key)
|
|
|
- return err
|
|
|
- })
|
|
|
+func (t *ts) Delete(key int64) error {
|
|
|
+ return t.db.ZRemRangeByScore(t.key, strconv.FormatInt(key, 10), strconv.FormatInt(key, 10)).Err()
|
|
|
}
|
|
|
|
|
|
-func (t ts) DeleteBefore(key int64) error {
|
|
|
- return t.redis.Apply(func(conn redis.Conn) error {
|
|
|
- bound := fmt.Sprintf("(%d", key)
|
|
|
- _, err := conn.Do(RemoveRangeByScore, t.key, "-INF", bound)
|
|
|
- return err
|
|
|
- })
|
|
|
+func (t *ts) DeleteBefore(key int64) error {
|
|
|
+ return t.db.ZRemRangeByScore(t.key, "-inf", strconv.FormatInt(key, 10)).Err()
|
|
|
}
|
|
|
|
|
|
-func (t ts) Close() error {
|
|
|
+func (t *ts) Close() error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (t ts) Drop() error {
|
|
|
- return t.redis.Apply(func(conn redis.Conn) error {
|
|
|
- _, err := conn.Do(Delete, t.key)
|
|
|
- return err
|
|
|
- })
|
|
|
+func (t *ts) Drop() error {
|
|
|
+ return t.db.Del(t.key).Err()
|
|
|
}
|
|
|
|
|
|
-func getLast(db *Instance, key string) (error, int64) {
|
|
|
- var lastTs int64
|
|
|
- err := db.Apply(func(conn redis.Conn) error {
|
|
|
- reply, err := conn.Do(ReversedRange, key, 0, 0, "WITHSCORES")
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- var tmp [][]byte
|
|
|
- tmp, err = redis.ByteSlices(reply, err)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- if len(tmp) == 0 {
|
|
|
- return nil
|
|
|
+func getLast(db *redis.Client, key string, value interface{}) (int64, error) {
|
|
|
+ var last int64 = 0
|
|
|
+ reply, err := db.ZRevRangeWithScores(key, 0, 0).Result()
|
|
|
+ if len(reply) > 0 {
|
|
|
+ if value != nil {
|
|
|
+ v := reply[0].Member.(string)
|
|
|
+ dec := gob.NewDecoder(bytes.NewBuffer([]byte(v)))
|
|
|
+ if err = dec.Decode(value); err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
}
|
|
|
- lastTs, err = strconv.ParseInt(string(tmp[1]), 10, 64)
|
|
|
- return err
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return err, 0
|
|
|
+ last = int64(reply[0].Score)
|
|
|
}
|
|
|
- return nil, lastTs
|
|
|
+ return last, nil
|
|
|
}
|