Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref #8 add TS.GET, TS.MGET, TS.ALTER and TS.QUERYINDEX #9

Merged
merged 9 commits into from
Jan 28, 2020
Merged
4 changes: 3 additions & 1 deletion src/main/java/com/redislabs/redistimeseries/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public enum Command implements ProtocolCommand{
MADD("TS.MADD"),
INCRBY("TS.INCRBY"),
DECRBY("TS.DECRBY"),
INFO("TS.INFO");
INFO("TS.INFO"),
GET("TS.GET"),
MGET("TS.MGET");

private final byte[] raw;

Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/redislabs/redistimeseries/RedisTimeSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,75 @@ public Range[] mrange(long from, long to, Aggregation aggregation, long timeBuck
return ranges;
}
}

/**
* TS.GET key
*
* @param key
* @return
*/
public Value get(String key) {
try (Jedis conn = getConnection()) {
List<?> touple = sendCommand(conn, Command.GET, SafeEncoder.encode(key)).getObjectMultiBulkReply();
if(touple.isEmpty()) {
return null;
}
return new Value((Long)touple.get(0), Double.parseDouble(SafeEncoder.encode((byte[])touple.get(1))));
}
}

/**
* TS.MGET [WITHLABELS] FILTER filter...
*
* @param key
* @param withLabels
* @param filters
* @return
*/
public Range[] mget(boolean withLabels, String... filters) {
try (Jedis conn = getConnection()) {
byte[][] args = new byte[1 + (withLabels?1:0) + (filters==null?0:filters.length)][];
int i=0;

if(withLabels) {
args[i++] = Keyword.WITHLABELS.getRaw();
}

args[i++] = Keyword.FILTER.getRaw();
if(filters != null) {
for(String label : filters) {
args[i++] = SafeEncoder.encode(label);
}
}

List<?> result = sendCommand(conn, Command.MGET, args).getObjectMultiBulkReply();
Range[] ranges = new Range[result.size()];
for(int j=0; j<ranges.length; ++j) {
List<?> series = (List<?>)result.get(j);

String resKey = SafeEncoder.encode((byte[])series.get(0));

List<?> resLables = (List<?>)series.get(1);
Map<String, String> rangeLabels = new HashMap<>();
for(int l=0; l<resLables.size(); ++l) {
List<byte[]> label = (List<byte[]>)resLables.get(l);
rangeLabels.put( SafeEncoder.encode(label.get(0)), SafeEncoder.encode(label.get(1)));
}

List<?> touple = (List<?>)series.get(2);
Value[] values;
if(touple.isEmpty()) {
values = new Value[0];
filipecosta90 marked this conversation as resolved.
Show resolved Hide resolved
} else {
values = new Value[1];
values[0] = new Value((Long)touple.get(0), Double.parseDouble(SafeEncoder.encode((byte[])touple.get(1))));
}

ranges[j] = new Range(resKey, rangeLabels, values);
}
return ranges;
}
}

/**
* TS.INCRBY key value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,35 @@ public void testIncDec() throws InterruptedException {
Assert.assertEquals(3, values[4].getValue(), 0);
}

@Test
public void testGet(){
Assert.assertTrue(client.create("seriesGet", 100*1000/*100sec retentionTime*/));
Assert.assertNull(client.get("seriesGet"));


}

@Test
public void testMGet(){
Map<String, String> labels = new HashMap<>();
labels.put("l1", "v1");
labels.put("l2", "v2");
Assert.assertTrue(client.create("seriesMGet1", 100*1000/*100sec retentionTime*/, labels));
Assert.assertTrue(client.create("seriesMGet2", 100*1000/*100sec retentionTime*/, labels));

Range[] ranges1 = client.mget(false, "l1=v2");
Assert.assertEquals(0, ranges1.length);

Range[] ranges2 = client.mget(true, "l1=v1");
Assert.assertEquals(2, ranges2.length);

client.add("seriesMGet", 1500, 1.3);
Range[] ranges3 = client.mget(false, "l1=v1");
Assert.assertEquals(2, ranges3.length);

}


@Test
public void testInfo() {
Map<String, String> labels = new HashMap<>();
Expand Down