Skip to content

Commit

Permalink
Polish #3860: Separate params for TS.INCRBY and TS.DECRBY (#3863)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 authored Jun 14, 2024
1 parent ac18fd0 commit 5cba18c
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -3974,7 +3974,7 @@ public final CommandObject<Long> tsIncrBy(String key, double value, long timesta
.add(TimeSeriesKeyword.TIMESTAMP).add(timestamp), BuilderFactory.LONG);
}

public final CommandObject<Long> tsIncrBy(String key, double addend, TSIncrOrDecrByParams incrByParams) {
public final CommandObject<Long> tsIncrBy(String key, double addend, TSIncrByParams incrByParams) {
return new CommandObject<>(commandArguments(TimeSeriesCommand.INCRBY).key(key).add(addend)
.addParams(incrByParams), BuilderFactory.LONG);
}
Expand All @@ -3988,7 +3988,7 @@ public final CommandObject<Long> tsDecrBy(String key, double value, long timesta
.add(TimeSeriesKeyword.TIMESTAMP).add(timestamp), BuilderFactory.LONG);
}

public final CommandObject<Long> tsDecrBy(String key, double subtrahend, TSIncrOrDecrByParams decrByParams) {
public final CommandObject<Long> tsDecrBy(String key, double subtrahend, TSDecrByParams decrByParams) {
return new CommandObject<>(commandArguments(TimeSeriesCommand.DECRBY).key(key).add(subtrahend)
.addParams(decrByParams), BuilderFactory.LONG);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/jedis/PipeliningBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3969,7 +3969,7 @@ public Response<Long> tsIncrBy(String key, double value, long timestamp) {
}

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

Expand All @@ -3984,7 +3984,7 @@ public Response<Long> tsDecrBy(String key, double value, long timestamp) {
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -4534,7 +4534,7 @@ public long tsIncrBy(String key, double value, long timestamp) {
}

@Override
public long tsIncrBy(String key, double addend, TSIncrOrDecrByParams incrByParams) {
public long tsIncrBy(String key, double addend, TSIncrByParams incrByParams) {
return executeCommand(commandObjects.tsIncrBy(key, addend, incrByParams));
}

Expand All @@ -4549,7 +4549,7 @@ public long tsDecrBy(String key, double value, long timestamp) {
}

@Override
public long tsDecrBy(String key, double subtrahend, TSIncrOrDecrByParams decrByParams) {
public long tsDecrBy(String key, double subtrahend, TSDecrByParams decrByParams) {
return executeCommand(commandObjects.tsDecrBy(key, subtrahend, decrByParams));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public interface RedisTimeSeriesCommands {
* @param incrByParams
* @return timestamp
*/
long tsIncrBy(String key, double addend, TSIncrOrDecrByParams incrByParams);
long tsIncrBy(String key, double addend, TSIncrByParams incrByParams);

long tsDecrBy(String key, double value);

Expand All @@ -134,7 +134,7 @@ public interface RedisTimeSeriesCommands {
* @param decrByParams
* @return timestamp
*/
long tsDecrBy(String key, double subtrahend, TSIncrOrDecrByParams decrByParams);
long tsDecrBy(String key, double subtrahend, TSDecrByParams decrByParams);

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

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

Response<Long> tsIncrBy(String key, double addend, TSIncrOrDecrByParams incrByParams);
Response<Long> tsIncrBy(String key, double addend, TSIncrByParams incrByParams);

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

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

Response<Long> tsDecrBy(String key, double subtrahend, TSIncrOrDecrByParams decrByParams);
Response<Long> tsDecrBy(String key, double subtrahend, TSDecrByParams decrByParams);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Represents optional arguments of TS.INCRBY or TS.DECRBY commands.
*/
public class TSIncrOrDecrByParams implements IParams {
class TSArithByParams<T extends TSArithByParams<?>> implements IParams {

private Long timestamp;
private Long retentionPeriod;
Expand All @@ -25,51 +25,39 @@ public class TSIncrOrDecrByParams implements IParams {

private Map<String, String> labels;

public TSIncrOrDecrByParams() {
TSArithByParams() {
}

public static TSIncrOrDecrByParams params() {
return new TSIncrOrDecrByParams();
}

public static TSIncrOrDecrByParams incrByParams() {
return new TSIncrOrDecrByParams();
}

public static TSIncrOrDecrByParams decrByParams() {
return new TSIncrOrDecrByParams();
}

public TSIncrOrDecrByParams timestamp(long timestamp) {
public T timestamp(long timestamp) {
this.timestamp = timestamp;
return this;
return (T) this;
}

public TSIncrOrDecrByParams retention(long retentionPeriod) {
public T retention(long retentionPeriod) {
this.retentionPeriod = retentionPeriod;
return this;
return (T) this;
}

public TSIncrOrDecrByParams encoding(EncodingFormat encoding) {
public T encoding(EncodingFormat encoding) {
this.encoding = encoding;
return this;
return (T) this;
}

public TSIncrOrDecrByParams chunkSize(long chunkSize) {
public T chunkSize(long chunkSize) {
this.chunkSize = chunkSize;
return this;
return (T) this;
}

public TSIncrOrDecrByParams duplicatePolicy(DuplicatePolicy duplicatePolicy) {
public T duplicatePolicy(DuplicatePolicy duplicatePolicy) {
this.duplicatePolicy = duplicatePolicy;
return this;
return (T) this;
}

public TSIncrOrDecrByParams ignore(long maxTimediff, double maxValDiff) {
public T ignore(long maxTimediff, double maxValDiff) {
this.ignore = true;
this.ignoreMaxTimediff = maxTimediff;
this.ignoreMaxValDiff = maxValDiff;
return this;
return (T) this;
}

/**
Expand All @@ -78,9 +66,9 @@ public TSIncrOrDecrByParams ignore(long maxTimediff, double maxValDiff) {
* @param labels label-value pairs
* @return the object itself
*/
public TSIncrOrDecrByParams labels(Map<String, String> labels) {
public T labels(Map<String, String> labels) {
this.labels = labels;
return this;
return (T) this;
}

/**
Expand All @@ -89,12 +77,12 @@ public TSIncrOrDecrByParams labels(Map<String, String> labels) {
* @param value
* @return the object itself
*/
public TSIncrOrDecrByParams label(String label, String value) {
public T label(String label, String value) {
if (this.labels == null) {
this.labels = new LinkedHashMap<>();
}
this.labels.put(label, value);
return this;
return (T) this;
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/redis/clients/jedis/timeseries/TSDecrByParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package redis.clients.jedis.timeseries;

/**
* Represents optional arguments of TS.DECRBY command.
*/
public class TSDecrByParams extends TSArithByParams<TSDecrByParams> {

public TSDecrByParams() {
}

public static TSDecrByParams decrByParams() {
return new TSDecrByParams();
}
}
14 changes: 14 additions & 0 deletions src/main/java/redis/clients/jedis/timeseries/TSIncrByParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package redis.clients.jedis.timeseries;

/**
* Represents optional arguments of TS.INCRBY command.
*/
public class TSIncrByParams extends TSArithByParams<TSIncrByParams> {

public TSIncrByParams() {
}

public static TSIncrByParams incrByParams() {
return new TSIncrByParams();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ public void testTsDecrByWithTimestamp() {

@Test
public void testTsDecrByWithParams() {
TSIncrOrDecrByParams DecrByParams = mock(TSIncrOrDecrByParams.class);
when(commandObjects.tsDecrBy("myTimeSeries", 1.0, DecrByParams)).thenReturn(longCommandObject);
TSDecrByParams decrByParams = mock(TSDecrByParams.class);
when(commandObjects.tsDecrBy("myTimeSeries", 1.0, decrByParams)).thenReturn(longCommandObject);

Response<Long> response = pipeliningBase.tsDecrBy("myTimeSeries", 1.0, DecrByParams);
Response<Long> response = pipeliningBase.tsDecrBy("myTimeSeries", 1.0, decrByParams);

assertThat(commands, contains(longCommandObject));
assertThat(response, is(predefinedResponse));
Expand Down Expand Up @@ -216,7 +216,7 @@ public void testTsIncrByWithTimestamp() {

@Test
public void testTsIncrByWithParams() {
TSIncrOrDecrByParams incrByParams = mock(TSIncrOrDecrByParams.class);
TSIncrByParams incrByParams = mock(TSIncrByParams.class);
when(commandObjects.tsIncrBy("myTimeSeries", 1.0, incrByParams)).thenReturn(longCommandObject);

Response<Long> response = pipeliningBase.tsIncrBy("myTimeSeries", 1.0, incrByParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void testTsDecrByWithTimestamp() {
public void testTsDecrByWithParams() {
String key = "testKey";
double value = 1.5;
TSIncrOrDecrByParams decrByParams = mock(TSIncrOrDecrByParams.class);
TSDecrByParams decrByParams = mock(TSDecrByParams.class);
long expectedResponse = 5L;

when(commandObjects.tsDecrBy(key, value, decrByParams)).thenReturn(longCommandObject);
Expand Down Expand Up @@ -341,7 +341,7 @@ public void testTsIncrByWithTimestamp() {
public void testTsIncrByWithParams() {
String key = "testKey";
double value = 2.5;
TSIncrOrDecrByParams incrByParams = mock(TSIncrOrDecrByParams.class);
TSIncrByParams incrByParams = mock(TSIncrByParams.class);
long expectedResponse = 5L;

when(commandObjects.tsIncrBy(key, value, incrByParams)).thenReturn(longCommandObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,19 +453,19 @@ public void incrByDecrByParams() {
labels.put("l2", "v2");

assertEquals(1000L, client.tsIncrBy("incr1", 1.1,
TSIncrOrDecrByParams.incrByParams().timestamp(1000).retention(10000).encoding(EncodingFormat.UNCOMPRESSED)
TSIncrByParams.incrByParams().timestamp(1000).retention(10000).encoding(EncodingFormat.UNCOMPRESSED)
.chunkSize(1000).duplicatePolicy(DuplicatePolicy.FIRST).ignore(50, 12.5).labels(labels)));

assertEquals(1000L, client.tsIncrBy("incr2", 1.1,
TSIncrOrDecrByParams.incrByParams().timestamp(1000).retention(10000).encoding(EncodingFormat.COMPRESSED)
TSIncrByParams.incrByParams().timestamp(1000).retention(10000).encoding(EncodingFormat.COMPRESSED)
.chunkSize(1000).duplicatePolicy(DuplicatePolicy.MIN).ignore(50, 12.5).labels(labels)));

assertEquals(1000L, client.tsDecrBy("decr1", 1.1,
TSIncrOrDecrByParams.decrByParams().timestamp(1000).retention(10000).encoding(EncodingFormat.COMPRESSED)
TSDecrByParams.decrByParams().timestamp(1000).retention(10000).encoding(EncodingFormat.COMPRESSED)
.chunkSize(1000).duplicatePolicy(DuplicatePolicy.LAST).ignore(50, 12.5).labels(labels)));

assertEquals(1000L, client.tsDecrBy("decr2", 1.1,
TSIncrOrDecrByParams.decrByParams().timestamp(1000).retention(10000).encoding(EncodingFormat.UNCOMPRESSED)
TSDecrByParams.decrByParams().timestamp(1000).retention(10000).encoding(EncodingFormat.UNCOMPRESSED)
.chunkSize(1000).duplicatePolicy(DuplicatePolicy.MAX).ignore(50, 12.5).labels(labels)));
}

Expand Down

0 comments on commit 5cba18c

Please sign in to comment.