Skip to content

Commit

Permalink
Add support for TS.REVRANGE
Browse files Browse the repository at this point in the history
  • Loading branch information
dengliming committed Nov 4, 2023
1 parent 823a245 commit 68c78f3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
3 changes: 2 additions & 1 deletion redistimeseries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ TS.DECRBY | RedisTimeSeries.<br/>decrBy()<br/>decrByAsync() |
TS.CREATERULE | RedisTimeSeries.<br/>createRule()<br/>createRuleAsync() |
TS.DELETERULE | RedisTimeSeries.<br/>deleteRule()<br/>deleteRuleAsync() |
TS.RANGE | RedisTimeSeries.<br/>range()<br/>rangeAsync() |
TS.REVRANGE | RedisTimeSeries.<br/>revRange()<br/>revRangeAsync() |
TS.MRANGE | RedisTimeSeries.<br/>mrange()<br/>mrangeAsync() |
TS.GET | RedisTimeSeries.<br/>get()<br/>getAsync() |
TS.MGET | RedisTimeSeries.<br/>mget()<br/>mgetAsync() |
TS.INFO | RedisTimeSeries.<br/>info()<br/>infoAsync() |
TS.QUERYINDEX | RedisTimeSeries.<br/>queryIndex()<br/>queryIndexAsync() |
TS.QUERYINDEX | RedisTimeSeries.<br/>queryIndex()<br/>queryIndexAsync() |
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -263,6 +264,33 @@ public RFuture<List<Value>> 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<Value> revRange(String key, long from, long to) {
return this.revRange(key, from, to, null);
}

public List<Value> revRange(String key, long from, long to, RangeOptions rangeOptions) {
return commandExecutor.get(revRangeAsync(key, from, to, rangeOptions));
}

public RFuture<List<Value>> revRangeAsync(String key, long from, long to, RangeOptions rangeOptions) {
List<Object> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ public void testRange() {

List<Value> 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> timeSeries = redisTimeSeries.mrange(timestamp, timestamp + 1, new RangeOptions()
.max(3).withLabels(), "sensor_id=2");
Expand Down

0 comments on commit 68c78f3

Please sign in to comment.