From 68c78f3f5fe202aabf0dfed262c241848c848a86 Mon Sep 17 00:00:00 2001 From: dengliming Date: Sat, 4 Nov 2023 19:08:54 +0800 Subject: [PATCH] Add support for TS.REVRANGE --- redistimeseries/README.md | 3 +- .../redistimeseries/RedisTimeSeries.java | 28 +++++++++++++++++++ .../protocol/RedisCommands.java | 2 +- .../redistimeseries/RedisTimeSeriesTest.java | 11 ++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/redistimeseries/README.md b/redistimeseries/README.md index d4330a0..db6609e 100644 --- a/redistimeseries/README.md +++ b/redistimeseries/README.md @@ -13,8 +13,9 @@ TS.DECRBY | RedisTimeSeries.
decrBy()
decrByAsync() | TS.CREATERULE | RedisTimeSeries.
createRule()
createRuleAsync() | TS.DELETERULE | RedisTimeSeries.
deleteRule()
deleteRuleAsync() | TS.RANGE | RedisTimeSeries.
range()
rangeAsync() | +TS.REVRANGE | RedisTimeSeries.
revRange()
revRangeAsync() | TS.MRANGE | RedisTimeSeries.
mrange()
mrangeAsync() | TS.GET | RedisTimeSeries.
get()
getAsync() | TS.MGET | RedisTimeSeries.
mget()
mgetAsync() | TS.INFO | RedisTimeSeries.
info()
infoAsync() | -TS.QUERYINDEX | RedisTimeSeries.
queryIndex()
queryIndexAsync() | \ No newline at end of file +TS.QUERYINDEX | RedisTimeSeries.
queryIndex()
queryIndexAsync() | diff --git a/redistimeseries/src/main/java/io/github/dengliming/redismodule/redistimeseries/RedisTimeSeries.java b/redistimeseries/src/main/java/io/github/dengliming/redismodule/redistimeseries/RedisTimeSeries.java index 8873f75..257b499 100644 --- a/redistimeseries/src/main/java/io/github/dengliming/redismodule/redistimeseries/RedisTimeSeries.java +++ b/redistimeseries/src/main/java/io/github/dengliming/redismodule/redistimeseries/RedisTimeSeries.java @@ -42,6 +42,7 @@ import static io.github.dengliming.redismodule.redistimeseries.protocol.RedisCommands.TS_MRANGE; import static io.github.dengliming.redismodule.redistimeseries.protocol.RedisCommands.TS_QUERYINDEX; import static io.github.dengliming.redismodule.redistimeseries.protocol.RedisCommands.TS_RANGE; +import static io.github.dengliming.redismodule.redistimeseries.protocol.RedisCommands.TS_REVRANGE; /** * @author dengliming @@ -263,6 +264,33 @@ public RFuture> rangeAsync(String key, long from, long to, RangeOpti return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, TS_RANGE, args.toArray()); } + /** + * Query a range in reverse direction. + * + * @param key + * @param from + * @param to + * @return + */ + public List revRange(String key, long from, long to) { + return this.revRange(key, from, to, null); + } + + public List revRange(String key, long from, long to, RangeOptions rangeOptions) { + return commandExecutor.get(revRangeAsync(key, from, to, rangeOptions)); + } + + public RFuture> revRangeAsync(String key, long from, long to, RangeOptions rangeOptions) { + List args = new ArrayList<>(); + args.add(key); + args.add(from); + args.add(to); + if (rangeOptions != null) { + rangeOptions.build(args); + } + return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, TS_REVRANGE, args.toArray()); + } + /** * Query a timestamp range across multiple time-series by filters. * diff --git a/redistimeseries/src/main/java/io/github/dengliming/redismodule/redistimeseries/protocol/RedisCommands.java b/redistimeseries/src/main/java/io/github/dengliming/redismodule/redistimeseries/protocol/RedisCommands.java index 2f1de07..b95e78c 100644 --- a/redistimeseries/src/main/java/io/github/dengliming/redismodule/redistimeseries/protocol/RedisCommands.java +++ b/redistimeseries/src/main/java/io/github/dengliming/redismodule/redistimeseries/protocol/RedisCommands.java @@ -46,5 +46,5 @@ public interface RedisCommands { RedisCommand TS_MGET = new RedisCommand<>("TS.MGET", new ListMultiDecoder2(new TimeSeriesDecoder(), new CodecDecoder(), new CodecDecoder(), new CodecDecoder())); RedisCommand TS_INFO = new RedisCommand<>("TS.INFO", new ListMultiDecoder2(new ObjectMapReplayDecoder(), new ObjectListReplayDecoder<>(), new ObjectListReplayDecoder<>())); RedisCommand TS_QUERYINDEX = new RedisCommand<>("TS.QUERYINDEX", new StringListReplayDecoder()); - + RedisCommand TS_REVRANGE = new RedisCommand<>("TS.REVRANGE", new ListMultiDecoder2(new ObjectListReplayDecoder<>(), new ValueDecoder())); } diff --git a/redistimeseries/src/test/java/io/github/dengliming/redismodule/redistimeseries/RedisTimeSeriesTest.java b/redistimeseries/src/test/java/io/github/dengliming/redismodule/redistimeseries/RedisTimeSeriesTest.java index d25896c..b480f9f 100644 --- a/redistimeseries/src/test/java/io/github/dengliming/redismodule/redistimeseries/RedisTimeSeriesTest.java +++ b/redistimeseries/src/test/java/io/github/dengliming/redismodule/redistimeseries/RedisTimeSeriesTest.java @@ -138,6 +138,17 @@ public void testRange() { List values = redisTimeSeries.range("temperature:2:33", timestamp, timestamp + 1); assertThat(values).hasSize(2); + assertThat(values.get(0).getTimestamp()).isEqualTo(timestamp); + assertThat(values.get(0).getValue()).isEqualTo(13); + assertThat(values.get(1).getTimestamp()).isEqualTo(timestamp + 1); + assertThat(values.get(1).getValue()).isEqualTo(26); + + values = redisTimeSeries.revRange("temperature:2:33", timestamp, timestamp + 1); + assertThat(values).hasSize(2); + assertThat(values.get(0).getTimestamp()).isEqualTo(timestamp + 1); + assertThat(values.get(0).getValue()).isEqualTo(26); + assertThat(values.get(1).getTimestamp()).isEqualTo(timestamp); + assertThat(values.get(1).getValue()).isEqualTo(13); List timeSeries = redisTimeSeries.mrange(timestamp, timestamp + 1, new RangeOptions() .max(3).withLabels(), "sensor_id=2");