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

Support TS.INCRBY and TS.DECRBY commands #3022

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -3427,13 +3427,11 @@ public final CommandObject<String> tsAlter(String key, TSAlterParams alterParams
}

public final CommandObject<Long> tsAdd(String key, double value) {
return new CommandObject<>(commandArguments(TimeSeriesCommand.ADD).key(key)
.add(Protocol.BYTES_ASTERISK).add(value), BuilderFactory.LONG);
return new CommandObject<>(commandArguments(TimeSeriesCommand.ADD).key(key).add(Protocol.BYTES_ASTERISK).add(value), BuilderFactory.LONG);
}

public final CommandObject<Long> tsAdd(String key, long timestamp, double value) {
return new CommandObject<>(commandArguments(TimeSeriesCommand.ADD).key(key)
.add(timestamp).add(value), BuilderFactory.LONG);
return new CommandObject<>(commandArguments(TimeSeriesCommand.ADD).key(key).add(timestamp).add(value), BuilderFactory.LONG);
}

public final CommandObject<Long> tsAdd(String key, long timestamp, double value, TSCreateParams createParams) {
Expand All @@ -3449,6 +3447,24 @@ public final CommandObject<List<Long>> tsMAdd(Map.Entry<String, TSElement>... en
return new CommandObject<>(args, BuilderFactory.LONG_LIST);
}

public final CommandObject<Long> tsIncrBy(String key, double value) {
return new CommandObject<>(commandArguments(TimeSeriesCommand.INCRBY).key(key).add(value), BuilderFactory.LONG);
}

public final CommandObject<Long> tsIncrBy(String key, double value, long timestamp) {
return new CommandObject<>(commandArguments(TimeSeriesCommand.INCRBY).key(key).add(value)
.add(TimeSeriesKeyword.TIMESTAMP).add(timestamp), BuilderFactory.LONG);
}

public final CommandObject<Long> tsDecrBy(String key, double value) {
return new CommandObject<>(commandArguments(TimeSeriesCommand.DECRBY).key(key).add(value), BuilderFactory.LONG);
}

public final CommandObject<Long> tsDecrBy(String key, double value, long timestamp) {
return new CommandObject<>(commandArguments(TimeSeriesCommand.DECRBY).key(key).add(value)
.add(TimeSeriesKeyword.TIMESTAMP).add(timestamp), BuilderFactory.LONG);
}

public final CommandObject<List<TSElement>> tsRange(String key, long fromTimestamp, long toTimestamp) {
return new CommandObject<>(commandArguments(TimeSeriesCommand.RANGE).key(key)
.add(fromTimestamp).add(toTimestamp), BuilderFactory.TIMESERIES_ELEMENT_LIST);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/MultiNodePipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3720,6 +3720,26 @@ public Response<List<Long>> tsMAdd(Map.Entry<String, TSElement>... entries) {
return appendCommand(commandObjects.tsMAdd(entries));
}

@Override
public Response<Long> tsIncrBy(String key, double value) {
return appendCommand(commandObjects.tsIncrBy(key, value));
}

@Override
public Response<Long> tsIncrBy(String key, double value, long timestamp) {
return appendCommand(commandObjects.tsIncrBy(key, value, timestamp));
}

@Override
public Response<Long> tsDecrBy(String key, double value) {
return appendCommand(commandObjects.tsDecrBy(key, value));
}

@Override
public Response<Long> tsDecrBy(String key, double value, long timestamp) {
return appendCommand(commandObjects.tsDecrBy(key, value, timestamp));
}

@Override
public Response<List<TSElement>> tsRange(String key, long fromTimestamp, long toTimestamp) {
return appendCommand(commandObjects.tsRange(key, fromTimestamp, toTimestamp));
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3722,6 +3722,26 @@ public Response<List<Long>> tsMAdd(Map.Entry<String, TSElement>... entries) {
return appendCommand(commandObjects.tsMAdd(entries));
}

@Override
public Response<Long> tsIncrBy(String key, double value) {
return appendCommand(commandObjects.tsIncrBy(key, value));
}

@Override
public Response<Long> tsIncrBy(String key, double value, long timestamp) {
return appendCommand(commandObjects.tsIncrBy(key, value, timestamp));
}

@Override
public Response<Long> tsDecrBy(String key, double value) {
return appendCommand(commandObjects.tsDecrBy(key, value));
}

@Override
public Response<Long> tsDecrBy(String key, double value, long timestamp) {
return appendCommand(commandObjects.tsDecrBy(key, value, timestamp));
}

@Override
public Response<List<TSElement>> tsRange(String key, long fromTimestamp, long toTimestamp) {
return appendCommand(commandObjects.tsRange(key, fromTimestamp, toTimestamp));
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/TransactionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3790,6 +3790,26 @@ public Response<List<Long>> tsMAdd(Map.Entry<String, TSElement>... entries) {
return appendCommand(commandObjects.tsMAdd(entries));
}

@Override
public Response<Long> tsIncrBy(String key, double value) {
return appendCommand(commandObjects.tsIncrBy(key, value));
}

@Override
public Response<Long> tsIncrBy(String key, double value, long timestamp) {
return appendCommand(commandObjects.tsIncrBy(key, value, timestamp));
}

@Override
public Response<Long> tsDecrBy(String key, double value) {
return appendCommand(commandObjects.tsDecrBy(key, value));
}

@Override
public Response<Long> tsDecrBy(String key, double value, long timestamp) {
return appendCommand(commandObjects.tsDecrBy(key, value, timestamp));
}

@Override
public Response<List<TSElement>> tsRange(String key, long fromTimestamp, long toTimestamp) {
return appendCommand(commandObjects.tsRange(key, fromTimestamp, toTimestamp));
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3861,6 +3861,26 @@ public List<Long> tsMAdd(Map.Entry<String, TSElement>... entries) {
return executeCommand(commandObjects.tsMAdd(entries));
}

@Override
public long tsIncrBy(String key, double value) {
return executeCommand(commandObjects.tsIncrBy(key, value));
}

@Override
public long tsIncrBy(String key, double value, long timestamp) {
return executeCommand(commandObjects.tsIncrBy(key, value, timestamp));
}

@Override
public long tsDecrBy(String key, double value) {
return executeCommand(commandObjects.tsDecrBy(key, value));
}

@Override
public long tsDecrBy(String key, double value, long timestamp) {
return executeCommand(commandObjects.tsDecrBy(key, value, timestamp));
}

@Override
public List<TSElement> tsRange(String key, long fromTimestamp, long toTimestamp) {
return executeCommand(commandObjects.tsRange(key, fromTimestamp, toTimestamp));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public interface RedisTimeSeriesCommands {
*/
List<Long> tsMAdd(Map.Entry<String, TSElement>... entries);

long tsIncrBy(String key, double value);

long tsIncrBy(String key, double value, long timestamp);

long tsDecrBy(String key, double value);

long tsDecrBy(String key, double value, long timestamp);

/**
* {@code TS.RANGE key fromTimestamp toTimestamp}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public interface RedisTimeSeriesPipelineCommands {

Response<List<Long>> tsMAdd(Map.Entry<String, TSElement>... entries);

Response<Long> tsIncrBy(String key, double value);

Response<Long> tsIncrBy(String key, double value, long timestamp);

Response<Long> tsDecrBy(String key, double value);

Response<Long> tsDecrBy(String key, double value, long timestamp);

Response<List<TSElement>> tsRange(String key, long fromTimestamp, long toTimestamp);

Response<List<TSElement>> tsRange(String key, TSRangeParams rangeParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,29 +355,26 @@ public void testMadd() {
assertEquals(3.2, values2.get(0).getValue(), 0.001);
assertEquals(54.2, values2.get(1).getValue(), 0.001);
}
//
// @Test
// public void testIncrByDecrBy() throws InterruptedException {
// assertEquals("OK", client.tsCreate("seriesIncDec", 100 * 1000 /*100sec retentionTime*/));
// assertEquals(1L, client.tsAdd("seriesIncDec", 1L, 1, 10000, null), 0);
// assertEquals(2L, client.incrBy("seriesIncDec", 3, 2L), 0);
// assertEquals(3L, client.decrBy("seriesIncDec", 2, 3L), 0);
// List<TSElement> values = client.tsRange("seriesIncDec", 1L, 3L);
// assertEquals(3, values.size());
// assertEquals(2, values[2].getValue(), 0);
// if (moduleVersion >= 10400) {
// assertEquals(3L, client.decrBy("seriesIncDec", 2, 3L), 0);
// values = client.tsRange("seriesIncDec", 1L, Long.MAX_VALUE);
// assertEquals(3, values.size());
// } else {
// try {
// client.incrBy("seriesIncDec", 3, 0L);
// fail();
// } catch (JedisDataException e) {
// // Error on incrby in the past
// }
// }
// }

@Test
public void testIncrByDecrBy() throws InterruptedException {
assertEquals("OK", client.tsCreate("seriesIncDec",
TSCreateParams.createParams().retention(100 * 1000 /*100 sec*/)));

assertEquals(1L, client.tsAdd("seriesIncDec", 1L, 1), 0);
assertEquals(2L, client.tsIncrBy("seriesIncDec", 3, 2L), 0);
assertEquals(3L, client.tsDecrBy("seriesIncDec", 2, 3L), 0);
List<TSElement> values = client.tsRange("seriesIncDec", 1L, 3L);
assertEquals(3, values.size());
assertEquals(2, values.get(2).getValue(), 0);

assertEquals(3L, client.tsDecrBy("seriesIncDec", 2, 3L), 0);
values = client.tsRange("seriesIncDec", 1L, Long.MAX_VALUE);
assertEquals(3, values.size());

client.tsIncrBy("seriesIncDec", 100);
client.tsDecrBy("seriesIncDec", 33);
}

@Test
public void align() {
Expand Down