Skip to content

Commit b2b74b6

Browse files
committed
Alternate option for '+' and '-' in RANGE/MRANGE
1 parent 942cc7d commit b2b74b6

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

src/main/java/com/redislabs/redistimeseries/MultiRangeParams.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,20 @@ public MultiRangeParams withLabels(boolean withLabels) {
4141
return this;
4242
}
4343

44-
public byte[][] getByteParams(long from, long to, String... filters) {
44+
public byte[][] getByteParams(Long from, Long to, String... filters) {
4545
List<byte[]> params = new ArrayList<>();
46-
params.add(Protocol.toByteArray(from));
47-
params.add(Protocol.toByteArray(to));
46+
47+
if (from == null) {
48+
params.add("-".getBytes());
49+
} else {
50+
params.add(Protocol.toByteArray(from));
51+
}
52+
53+
if (to == null) {
54+
params.add("+".getBytes());
55+
} else {
56+
params.add(Protocol.toByteArray(to));
57+
}
4858

4959
if (count != null) {
5060
params.add(Keyword.COUNT.getRaw());

src/main/java/com/redislabs/redistimeseries/RangeParams.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,21 @@ public RangeParams aggregation(Aggregation aggregation, long timeBucket) {
2727
return this;
2828
}
2929

30-
public byte[][] getByteParams(String key, long from, long to) {
30+
public byte[][] getByteParams(String key, Long from, Long to) {
3131
List<byte[]> params = new ArrayList<>();
3232
params.add(SafeEncoder.encode(key));
33-
params.add(Protocol.toByteArray(from));
34-
params.add(Protocol.toByteArray(to));
33+
34+
if (from == null) {
35+
params.add("-".getBytes());
36+
} else {
37+
params.add(Protocol.toByteArray(from));
38+
}
39+
40+
if (to == null) {
41+
params.add("+".getBytes());
42+
} else {
43+
params.add(Protocol.toByteArray(to));
44+
}
3545

3646
if (count != null) {
3747
params.add(Keyword.COUNT.getRaw());

src/main/java/com/redislabs/redistimeseries/RedisTimeSeries.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -692,12 +692,12 @@ public Value[] range(
692692
* TS.RANGE key fromTimestamp toTimestamp [COUNT count] [AGGREGATION aggregationType timeBucket]
693693
*
694694
* @param key
695-
* @param from
696-
* @param to
695+
* @param from timestamp. {@code null} represents {@code "-"}
696+
* @param to timestamp. {@code null} represents {@code "+"}
697697
* @param rangeParams
698698
* @return
699699
*/
700-
public Value[] range(String key, long from, long to, RangeParams rangeParams) {
700+
public Value[] range(String key, Long from, Long to, RangeParams rangeParams) {
701701
try (Jedis conn = getConnection()) {
702702
Object obj =
703703
sendCommand(conn, Command.RANGE, rangeParams.getByteParams(key, from, to)).getOne();
@@ -793,12 +793,12 @@ public Value[] revrange(
793793
* timeBucket]
794794
*
795795
* @param key
796-
* @param from
797-
* @param to
796+
* @param from timestamp. {@code null} represents {@code "-"}
797+
* @param to timestamp. {@code null} represents {@code "+"}
798798
* @param rangeParams
799799
* @return
800800
*/
801-
public Value[] revrange(String key, long from, long to, RangeParams rangeParams) {
801+
public Value[] revrange(String key, Long from, Long to, RangeParams rangeParams) {
802802
try (Jedis conn = getConnection()) {
803803
Object obj =
804804
sendCommand(conn, Command.REVRANGE, rangeParams.getByteParams(key, from, to)).getOne();
@@ -948,13 +948,13 @@ private Range[] multiRange(
948948
* TS.MRANGE fromTimestamp toTimestamp [COUNT count] [AGGREGATION aggregationType timeBucket]
949949
* [WITHLABELS] FILTER filter...
950950
*
951-
* @param from
952-
* @param to
951+
* @param from timestamp. {@code null} represents {@code "-"}
952+
* @param to timestamp. {@code null} represents {@code "+"}
953953
* @param multiRangeParams
954954
* @param filters
955955
* @return
956956
*/
957-
public Range[] mrange(long from, long to, MultiRangeParams multiRangeParams, String... filters) {
957+
public Range[] mrange(Long from, Long to, MultiRangeParams multiRangeParams, String... filters) {
958958
try (Jedis conn = getConnection()) {
959959
Object obj =
960960
sendCommand(conn, Command.MRANGE, multiRangeParams.getByteParams(from, to, filters))
@@ -1060,14 +1060,14 @@ public Range[] mrevrange(
10601060
* TS.MREVRANGE fromTimestamp toTimestamp [COUNT count] [AGGREGATION aggregationType timeBucket]
10611061
* [WITHLABELS] FILTER filter...
10621062
*
1063-
* @param from
1064-
* @param to
1063+
* @param from timestamp. {@code null} represents {@code "-"}
1064+
* @param to timestamp. {@code null} represents {@code "+"}
10651065
* @param multiRangeParams
10661066
* @param filters
10671067
* @return
10681068
*/
10691069
public Range[] mrevrange(
1070-
long from, long to, MultiRangeParams multiRangeParams, String... filters) {
1070+
Long from, Long to, MultiRangeParams multiRangeParams, String... filters) {
10711071
try (Jedis conn = getConnection()) {
10721072
Object obj =
10731073
sendCommand(conn, Command.MREVRANGE, multiRangeParams.getByteParams(from, to, filters))

src/test/java/com/redislabs/redistimeseries/RedisTimeSeriesTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.redislabs.redistimeseries;
22

3+
import static org.junit.Assert.assertArrayEquals;
34
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertTrue;
56
import static org.junit.Assert.fail;
@@ -141,6 +142,8 @@ public void testAdd() {
141142
labels.put("l1", "v1");
142143
labels.put("l2", "v2");
143144
Assert.assertTrue(client.create("seriesAdd", 10000L /*retentionTime*/, labels));
145+
assertArrayEquals(
146+
new Value[0], client.range("seriesAdd", null, null, RangeParams.rangeParams()));
144147

145148
Assert.assertEquals(1000L, client.add("seriesAdd", 1000L, 1.1, 10000, null));
146149
Assert.assertEquals(2000L, client.add("seriesAdd", 2000L, 0.9, (Map<String, String>) null));
@@ -160,6 +163,7 @@ public void testAdd() {
160163
values = client.range("seriesAdd", 800L, 5000L);
161164
Assert.assertEquals(4, values.length);
162165
Assert.assertArrayEquals(rawValues, values);
166+
assertArrayEquals(rawValues, client.range("seriesAdd", null, null, RangeParams.rangeParams()));
163167

164168
Value[] expectedCountValues =
165169
new Value[] {new Value(2000L, 1), new Value(3200L, 1), new Value(4500L, 1)};
@@ -193,6 +197,9 @@ public void testAdd() {
193197
Assert.assertEquals(1, values.length);
194198
Assert.assertArrayEquals(expectedOverallMaxValues, values);
195199

200+
// MRANGE
201+
assertArrayEquals(
202+
new Range[0], client.mrange(null, null, MultiRangeParams.multiRangeParams(), "l=v"));
196203
try {
197204
client.mrange(500L, 4600L, Aggregation.COUNT, 1);
198205
Assert.fail();
@@ -550,6 +557,8 @@ public void testRevRange() {
550557
labels.put("l1", "v1");
551558
labels.put("l2", "v2");
552559
Assert.assertTrue(client.create("seriesAdd", 10000L /*retentionTime*/, labels));
560+
assertArrayEquals(
561+
new Value[0], client.revrange("seriesAdd", null, null, RangeParams.rangeParams()));
553562

554563
Assert.assertEquals(1000L, client.add("seriesRevRange", 1000L, 1.1, 10000, null));
555564
Assert.assertEquals(
@@ -570,6 +579,8 @@ public void testRevRange() {
570579
values = client.revrange("seriesRevRange", 800L, 5000L);
571580
Assert.assertEquals(4, values.length);
572581
Assert.assertArrayEquals(rawValues, values);
582+
assertArrayEquals(
583+
rawValues, client.revrange("seriesRevRange", null, null, RangeParams.rangeParams()));
573584

574585
Value[] expectedCountValues =
575586
new Value[] {new Value(4500L, 1), new Value(3200L, 1), new Value(2000L, 1)};
@@ -587,6 +598,8 @@ public void testRevRange() {
587598
@Test
588599
public void testMRevRange() {
589600
if (moduleVersion < 10300) return;
601+
assertArrayEquals(
602+
new Range[0], client.mrevrange(null, null, MultiRangeParams.multiRangeParams(), "l=v"));
590603

591604
Map<String, String> labels1 = new HashMap<>();
592605
labels1.put("l3", "v3");

0 commit comments

Comments
 (0)