diff --git a/src/main/java/redis/clients/jedis/CommandObjects.java b/src/main/java/redis/clients/jedis/CommandObjects.java index 7226a014a7..421b81c4e2 100644 --- a/src/main/java/redis/clients/jedis/CommandObjects.java +++ b/src/main/java/redis/clients/jedis/CommandObjects.java @@ -3946,9 +3946,15 @@ public final CommandObject tsAdd(String key, long timestamp, double value) return new CommandObject<>(commandArguments(TimeSeriesCommand.ADD).key(key).add(timestamp).add(value), BuilderFactory.LONG); } + @Deprecated public final CommandObject tsAdd(String key, long timestamp, double value, TSCreateParams createParams) { - return new CommandObject<>(commandArguments(TimeSeriesCommand.ADD).key(key) - .add(timestamp).add(value).addParams(createParams), BuilderFactory.LONG); + return new CommandObject<>(commandArguments(TimeSeriesCommand.ADD).key(key).add(timestamp).add(value) + .addParams(createParams), BuilderFactory.LONG); + } + + public final CommandObject tsAdd(String key, long timestamp, double value, TSAddParams addParams) { + return new CommandObject<>(commandArguments(TimeSeriesCommand.ADD).key(key).add(timestamp).add(value) + .addParams(addParams), BuilderFactory.LONG); } public final CommandObject> tsMAdd(Map.Entry... entries) { @@ -3968,6 +3974,11 @@ public final CommandObject tsIncrBy(String key, double value, long timesta .add(TimeSeriesKeyword.TIMESTAMP).add(timestamp), BuilderFactory.LONG); } + public final CommandObject tsIncrBy(String key, double addend, TSIncrOrDecrByParams incrByParams) { + return new CommandObject<>(commandArguments(TimeSeriesCommand.INCRBY).key(key).add(addend) + .addParams(incrByParams), BuilderFactory.LONG); + } + public final CommandObject tsDecrBy(String key, double value) { return new CommandObject<>(commandArguments(TimeSeriesCommand.DECRBY).key(key).add(value), BuilderFactory.LONG); } @@ -3977,6 +3988,11 @@ public final CommandObject tsDecrBy(String key, double value, long timesta .add(TimeSeriesKeyword.TIMESTAMP).add(timestamp), BuilderFactory.LONG); } + public final CommandObject tsDecrBy(String key, double subtrahend, TSIncrOrDecrByParams decrByParams) { + return new CommandObject<>(commandArguments(TimeSeriesCommand.DECRBY).key(key).add(subtrahend) + .addParams(decrByParams), BuilderFactory.LONG); + } + public final CommandObject> tsRange(String key, long fromTimestamp, long toTimestamp) { return new CommandObject<>(commandArguments(TimeSeriesCommand.RANGE).key(key) .add(fromTimestamp).add(toTimestamp), TimeSeriesBuilderFactory.TIMESERIES_ELEMENT_LIST); diff --git a/src/main/java/redis/clients/jedis/PipeliningBase.java b/src/main/java/redis/clients/jedis/PipeliningBase.java index 928126a704..9967a2e694 100644 --- a/src/main/java/redis/clients/jedis/PipeliningBase.java +++ b/src/main/java/redis/clients/jedis/PipeliningBase.java @@ -3948,6 +3948,11 @@ public Response tsAdd(String key, long timestamp, double value, TSCreatePa return appendCommand(commandObjects.tsAdd(key, timestamp, value, createParams)); } + @Override + public Response tsAdd(String key, long timestamp, double value, TSAddParams addParams) { + return appendCommand(commandObjects.tsAdd(key, timestamp, value, addParams)); + } + @Override public Response> tsMAdd(Map.Entry... entries) { return appendCommand(commandObjects.tsMAdd(entries)); @@ -3963,6 +3968,11 @@ public Response tsIncrBy(String key, double value, long timestamp) { return appendCommand(commandObjects.tsIncrBy(key, value, timestamp)); } + @Override + public Response tsIncrBy(String key, double addend, TSIncrOrDecrByParams incrByParams) { + return appendCommand(commandObjects.tsIncrBy(key, addend, incrByParams)); + } + @Override public Response tsDecrBy(String key, double value) { return appendCommand(commandObjects.tsDecrBy(key, value)); @@ -3973,6 +3983,11 @@ public Response tsDecrBy(String key, double value, long timestamp) { return appendCommand(commandObjects.tsDecrBy(key, value, timestamp)); } + @Override + public Response tsDecrBy(String key, double subtrahend, TSIncrOrDecrByParams decrByParams) { + return appendCommand(commandObjects.tsDecrBy(key, subtrahend, decrByParams)); + } + @Override public Response> tsRange(String key, long fromTimestamp, long toTimestamp) { return appendCommand(commandObjects.tsRange(key, fromTimestamp, toTimestamp)); diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index 2d6e77fcf0..87ba0d8a14 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -4473,6 +4473,11 @@ public long tsAdd(String key, long timestamp, double value, TSCreateParams creat return executeCommand(commandObjects.tsAdd(key, timestamp, value, createParams)); } + @Override + public long tsAdd(String key, long timestamp, double value, TSAddParams addParams) { + return executeCommand(commandObjects.tsAdd(key, timestamp, value, addParams)); + } + @Override public List tsMAdd(Map.Entry... entries) { return executeCommand(commandObjects.tsMAdd(entries)); @@ -4488,6 +4493,11 @@ public long tsIncrBy(String key, double value, long timestamp) { return executeCommand(commandObjects.tsIncrBy(key, value, timestamp)); } + @Override + public long tsIncrBy(String key, double addend, TSIncrOrDecrByParams incrByParams) { + return executeCommand(commandObjects.tsIncrBy(key, addend, incrByParams)); + } + @Override public long tsDecrBy(String key, double value) { return executeCommand(commandObjects.tsDecrBy(key, value)); @@ -4498,6 +4508,11 @@ public long tsDecrBy(String key, double value, long timestamp) { return executeCommand(commandObjects.tsDecrBy(key, value, timestamp)); } + @Override + public long tsDecrBy(String key, double subtrahend, TSIncrOrDecrByParams decrByParams) { + return executeCommand(commandObjects.tsDecrBy(key, subtrahend, decrByParams)); + } + @Override public List tsRange(String key, long fromTimestamp, long toTimestamp) { return executeCommand(commandObjects.tsRange(key, fromTimestamp, toTimestamp)); diff --git a/src/main/java/redis/clients/jedis/timeseries/EncodingFormat.java b/src/main/java/redis/clients/jedis/timeseries/EncodingFormat.java new file mode 100644 index 0000000000..5130d7da25 --- /dev/null +++ b/src/main/java/redis/clients/jedis/timeseries/EncodingFormat.java @@ -0,0 +1,24 @@ +package redis.clients.jedis.timeseries; + +import redis.clients.jedis.args.Rawable; +import redis.clients.jedis.util.SafeEncoder; + +/** + * Specifies the series samples encoding format. + */ +public enum EncodingFormat implements Rawable { + + COMPRESSED, + UNCOMPRESSED; + + private final byte[] raw; + + private EncodingFormat() { + raw = SafeEncoder.encode(name()); + } + + @Override + public byte[] getRaw() { + return raw; + } +} diff --git a/src/main/java/redis/clients/jedis/timeseries/RedisTimeSeriesCommands.java b/src/main/java/redis/clients/jedis/timeseries/RedisTimeSeriesCommands.java index c002b94c08..67c1b26fcf 100644 --- a/src/main/java/redis/clients/jedis/timeseries/RedisTimeSeriesCommands.java +++ b/src/main/java/redis/clients/jedis/timeseries/RedisTimeSeriesCommands.java @@ -59,16 +59,33 @@ public interface RedisTimeSeriesCommands { long tsAdd(String key, long timestamp, double value); /** - * {@code TS.ADD key timestamp value [RETENTION retentionTime] [ENCODING [COMPRESSED|UNCOMPRESSED]] [CHUNK_SIZE size] [ON_DUPLICATE policy] [LABELS label value..]} - * * @param key * @param timestamp * @param value * @param createParams * @return timestamp + * @deprecated Use {@link RedisTimeSeriesCommands#tsAdd(java.lang.String, long, double, redis.clients.jedis.timeseries.TSAddParams)}. */ + @Deprecated long tsAdd(String key, long timestamp, double value, TSCreateParams createParams); + /** + * {@code TS.ADD key timestamp value + * [RETENTION retentionTime] + * [ENCODING ] + * [CHUNK_SIZE size] + * [DUPLICATE_POLICY policy] + * [ON_DUPLICATE policy_ovr] + * [LABELS label value..]} + * + * @param key + * @param timestamp + * @param value + * @param addParams + * @return timestamp + */ + long tsAdd(String key, long timestamp, double value, TSAddParams addParams); + /** * {@code TS.MADD key timestamp value [key timestamp value ...]} * @@ -81,10 +98,44 @@ public interface RedisTimeSeriesCommands { long tsIncrBy(String key, double value, long timestamp); + /** + * {@code TS.INCRBY key addend + * [TIMESTAMP timestamp] + * [RETENTION retentionPeriod] + * [ENCODING ] + * [CHUNK_SIZE size] + * [DUPLICATE_POLICY policy] + * [IGNORE ignoreMaxTimediff ignoreMaxValDiff] + * [LABELS [label value ...]]} + * + * @param key + * @param addend + * @param incrByParams + * @return timestamp + */ + long tsIncrBy(String key, double addend, TSIncrOrDecrByParams incrByParams); + long tsDecrBy(String key, double value); long tsDecrBy(String key, double value, long timestamp); + /** + * {@code TS.DECRBY key subtrahend + * [TIMESTAMP timestamp] + * [RETENTION retentionPeriod] + * [ENCODING ] + * [CHUNK_SIZE size] + * [DUPLICATE_POLICY policy] + * [IGNORE ignoreMaxTimediff ignoreMaxValDiff] + * [LABELS [label value ...]]} + * + * @param key + * @param subtrahend + * @param decrByParams + * @return timestamp + */ + long tsDecrBy(String key, double subtrahend, TSIncrOrDecrByParams decrByParams); + /** * {@code TS.RANGE key fromTimestamp toTimestamp} * diff --git a/src/main/java/redis/clients/jedis/timeseries/RedisTimeSeriesPipelineCommands.java b/src/main/java/redis/clients/jedis/timeseries/RedisTimeSeriesPipelineCommands.java index 288b3f195e..b3304716dd 100644 --- a/src/main/java/redis/clients/jedis/timeseries/RedisTimeSeriesPipelineCommands.java +++ b/src/main/java/redis/clients/jedis/timeseries/RedisTimeSeriesPipelineCommands.java @@ -18,18 +18,25 @@ public interface RedisTimeSeriesPipelineCommands { Response tsAdd(String key, long timestamp, double value); + @Deprecated Response tsAdd(String key, long timestamp, double value, TSCreateParams createParams); + Response tsAdd(String key, long timestamp, double value, TSAddParams addParams); + Response> tsMAdd(Map.Entry... entries); Response tsIncrBy(String key, double value); Response tsIncrBy(String key, double value, long timestamp); + Response tsIncrBy(String key, double addend, TSIncrOrDecrByParams incrByParams); + Response tsDecrBy(String key, double value); Response tsDecrBy(String key, double value, long timestamp); + Response tsDecrBy(String key, double subtrahend, TSIncrOrDecrByParams decrByParams); + Response> tsRange(String key, long fromTimestamp, long toTimestamp); Response> tsRange(String key, TSRangeParams rangeParams); diff --git a/src/main/java/redis/clients/jedis/timeseries/TSAddParams.java b/src/main/java/redis/clients/jedis/timeseries/TSAddParams.java new file mode 100644 index 0000000000..0a9713cefb --- /dev/null +++ b/src/main/java/redis/clients/jedis/timeseries/TSAddParams.java @@ -0,0 +1,128 @@ +package redis.clients.jedis.timeseries; + +import static redis.clients.jedis.Protocol.toByteArray; +import static redis.clients.jedis.timeseries.TimeSeriesProtocol.TimeSeriesKeyword.*; + +import java.util.LinkedHashMap; +import java.util.Map; +import redis.clients.jedis.CommandArguments; +import redis.clients.jedis.params.IParams; + +/** + * Represents optional arguments of TS.ADD command. + */ +public class TSAddParams implements IParams { + + private Long retentionPeriod; + private EncodingFormat encoding; + private Long chunkSize; + private DuplicatePolicy duplicatePolicy; + private DuplicatePolicy onDuplicate; + + private boolean ignore; + private long ignoreMaxTimediff; + private double ignoreMaxValDiff; + + private Map labels; + + public TSAddParams() { + } + + public static TSAddParams addParams() { + return new TSAddParams(); + } + + public TSAddParams retention(long retentionPeriod) { + this.retentionPeriod = retentionPeriod; + return this; + } + + public TSAddParams encoding(EncodingFormat encoding) { + this.encoding = encoding; + return this; + } + + public TSAddParams chunkSize(long chunkSize) { + this.chunkSize = chunkSize; + return this; + } + + public TSAddParams duplicatePolicy(DuplicatePolicy duplicatePolicy) { + this.duplicatePolicy = duplicatePolicy; + return this; + } + + public TSAddParams onDuplicate(DuplicatePolicy onDuplicate) { + this.onDuplicate = onDuplicate; + return this; + } + + public TSAddParams ignore(long maxTimediff, double maxValDiff) { + this.ignore = true; + this.ignoreMaxTimediff = maxTimediff; + this.ignoreMaxValDiff = maxValDiff; + return this; + } + + /** + * Set label-value pairs + * + * @param labels label-value pairs + * @return the object itself + */ + public TSAddParams labels(Map labels) { + this.labels = labels; + return this; + } + + /** + * Add label-value pair. Multiple pairs can be added through chaining. + * @param label + * @param value + * @return the object itself + */ + public TSAddParams label(String label, String value) { + if (this.labels == null) { + this.labels = new LinkedHashMap<>(); + } + this.labels.put(label, value); + return this; + } + + @Override + public void addParams(CommandArguments args) { + + if (retentionPeriod != null) { + args.add(RETENTION).add(toByteArray(retentionPeriod)); + } + + if (encoding != null) { + args.add(ENCODING).add(encoding); + } + + if (chunkSize != null) { + args.add(CHUNK_SIZE).add(toByteArray(chunkSize)); + } + + if (duplicatePolicy != null) { + args.add(DUPLICATE_POLICY).add(duplicatePolicy); + } + + if (duplicatePolicy != null) { + args.add(DUPLICATE_POLICY).add(duplicatePolicy); + } + + if (onDuplicate != null) { + args.add(ON_DUPLICATE).add(onDuplicate); + } + + if (ignore) { + args.add(IGNORE).add(ignoreMaxTimediff).add(ignoreMaxValDiff); + } + + if (labels != null) { + args.add(LABELS); + labels.entrySet().forEach((entry) -> args.add(entry.getKey()).add(entry.getValue())); + } + } +} diff --git a/src/main/java/redis/clients/jedis/timeseries/TSAlterParams.java b/src/main/java/redis/clients/jedis/timeseries/TSAlterParams.java index 4576a1b6b7..50ba9723ac 100644 --- a/src/main/java/redis/clients/jedis/timeseries/TSAlterParams.java +++ b/src/main/java/redis/clients/jedis/timeseries/TSAlterParams.java @@ -17,6 +17,11 @@ public class TSAlterParams implements IParams { private Long retentionPeriod; private Long chunkSize; private DuplicatePolicy duplicatePolicy; + + private boolean ignore; + private long ignoreMaxTimediff; + private double ignoreMaxValDiff; + private Map labels; public TSAlterParams() { @@ -41,11 +46,30 @@ public TSAlterParams duplicatePolicy(DuplicatePolicy duplicatePolicy) { return this; } + public TSAlterParams ignore(long maxTimediff, double maxValDiff) { + this.ignore = true; + this.ignoreMaxTimediff = maxTimediff; + this.ignoreMaxValDiff = maxValDiff; + return this; + } + + /** + * Set label-value pairs + * + * @param labels label-value pairs + * @return the object itself + */ public TSAlterParams labels(Map labels) { this.labels = labels; return this; } + /** + * Add label-value pair. Multiple pairs can be added through chaining. + * @param label + * @param value + * @return the object itself + */ public TSAlterParams label(String label, String value) { if (this.labels == null) { this.labels = new LinkedHashMap<>(); @@ -73,6 +97,10 @@ public void addParams(CommandArguments args) { args.add(DUPLICATE_POLICY).add(duplicatePolicy); } + if (ignore) { + args.add(IGNORE).add(ignoreMaxTimediff).add(ignoreMaxValDiff); + } + if (labels != null) { args.add(LABELS); labels.entrySet().forEach((entry) -> args.add(entry.getKey()).add(entry.getValue())); diff --git a/src/main/java/redis/clients/jedis/timeseries/TSCreateParams.java b/src/main/java/redis/clients/jedis/timeseries/TSCreateParams.java index ca07de1f01..0611383d4d 100644 --- a/src/main/java/redis/clients/jedis/timeseries/TSCreateParams.java +++ b/src/main/java/redis/clients/jedis/timeseries/TSCreateParams.java @@ -14,10 +14,14 @@ public class TSCreateParams implements IParams { private Long retentionPeriod; - private boolean uncompressed; - private boolean compressed; + private EncodingFormat encoding; private Long chunkSize; private DuplicatePolicy duplicatePolicy; + + private boolean ignore; + private long ignoreMaxTimediff; + private double ignoreMaxValDiff; + private Map labels; public TSCreateParams() { @@ -32,13 +36,18 @@ public TSCreateParams retention(long retentionPeriod) { return this; } + // TODO: deprecate public TSCreateParams uncompressed() { - this.uncompressed = true; - return this; + return encoding(EncodingFormat.UNCOMPRESSED); } + // TODO: deprecate public TSCreateParams compressed() { - this.compressed = true; + return encoding(EncodingFormat.COMPRESSED); + } + + public TSCreateParams encoding(EncodingFormat encoding) { + this.encoding = encoding; return this; } @@ -52,6 +61,13 @@ public TSCreateParams duplicatePolicy(DuplicatePolicy duplicatePolicy) { return this; } + public TSCreateParams ignore(long maxTimediff, double maxValDiff) { + this.ignore = true; + this.ignoreMaxTimediff = maxTimediff; + this.ignoreMaxValDiff = maxValDiff; + return this; + } + /** * Set label-value pairs * @@ -65,6 +81,9 @@ public TSCreateParams labels(Map labels) { /** * Add label-value pair. Multiple pairs can be added through chaining. + * @param label + * @param value + * @return the object itself */ public TSCreateParams label(String label, String value) { if (this.labels == null) { @@ -81,10 +100,8 @@ public void addParams(CommandArguments args) { args.add(RETENTION).add(toByteArray(retentionPeriod)); } - if (uncompressed) { - args.add(ENCODING).add(UNCOMPRESSED); - } else if (compressed) { - args.add(ENCODING).add(COMPRESSED); + if (encoding != null) { + args.add(ENCODING).add(encoding); } if (chunkSize != null) { @@ -95,6 +112,10 @@ public void addParams(CommandArguments args) { args.add(DUPLICATE_POLICY).add(duplicatePolicy); } + if (ignore) { + args.add(IGNORE).add(ignoreMaxTimediff).add(ignoreMaxValDiff); + } + if (labels != null) { args.add(LABELS); labels.entrySet().forEach((entry) -> args.add(entry.getKey()).add(entry.getValue())); diff --git a/src/main/java/redis/clients/jedis/timeseries/TSIncrOrDecrByParams.java b/src/main/java/redis/clients/jedis/timeseries/TSIncrOrDecrByParams.java new file mode 100644 index 0000000000..fde848fb5a --- /dev/null +++ b/src/main/java/redis/clients/jedis/timeseries/TSIncrOrDecrByParams.java @@ -0,0 +1,132 @@ +package redis.clients.jedis.timeseries; + +import static redis.clients.jedis.Protocol.toByteArray; +import static redis.clients.jedis.timeseries.TimeSeriesProtocol.TimeSeriesKeyword.*; + +import java.util.LinkedHashMap; +import java.util.Map; +import redis.clients.jedis.CommandArguments; +import redis.clients.jedis.params.IParams; + +/** + * Represents optional arguments of TS.INCRBY or TS.DECRBY commands. + */ +public class TSIncrOrDecrByParams implements IParams { + + private Long timestamp; + private Long retentionPeriod; + private EncodingFormat encoding; + private Long chunkSize; + private DuplicatePolicy duplicatePolicy; + + private boolean ignore; + private long ignoreMaxTimediff; + private double ignoreMaxValDiff; + + private Map labels; + + public TSIncrOrDecrByParams() { + } + + 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) { + this.timestamp = timestamp; + return this; + } + + public TSIncrOrDecrByParams retention(long retentionPeriod) { + this.retentionPeriod = retentionPeriod; + return this; + } + + public TSIncrOrDecrByParams encoding(EncodingFormat encoding) { + this.encoding = encoding; + return this; + } + + public TSIncrOrDecrByParams chunkSize(long chunkSize) { + this.chunkSize = chunkSize; + return this; + } + + public TSIncrOrDecrByParams duplicatePolicy(DuplicatePolicy duplicatePolicy) { + this.duplicatePolicy = duplicatePolicy; + return this; + } + + public TSIncrOrDecrByParams ignore(long maxTimediff, double maxValDiff) { + this.ignore = true; + this.ignoreMaxTimediff = maxTimediff; + this.ignoreMaxValDiff = maxValDiff; + return this; + } + + /** + * Set label-value pairs + * + * @param labels label-value pairs + * @return the object itself + */ + public TSIncrOrDecrByParams labels(Map labels) { + this.labels = labels; + return this; + } + + /** + * Add label-value pair. Multiple pairs can be added through chaining. + * @param label + * @param value + * @return the object itself + */ + public TSIncrOrDecrByParams label(String label, String value) { + if (this.labels == null) { + this.labels = new LinkedHashMap<>(); + } + this.labels.put(label, value); + return this; + } + + @Override + public void addParams(CommandArguments args) { + + if (timestamp != null) { + args.add(TIMESTAMP).add(timestamp); + } + + if (retentionPeriod != null) { + args.add(RETENTION).add(toByteArray(retentionPeriod)); + } + + if (encoding != null) { + args.add(ENCODING).add(encoding); + } + + if (chunkSize != null) { + args.add(CHUNK_SIZE).add(toByteArray(chunkSize)); + } + + if (duplicatePolicy != null) { + args.add(DUPLICATE_POLICY).add(duplicatePolicy); + } + + if (ignore) { + args.add(IGNORE).add(ignoreMaxTimediff).add(ignoreMaxValDiff); + } + + if (labels != null) { + args.add(LABELS); + labels.entrySet().forEach((entry) -> args.add(entry.getKey()).add(entry.getValue())); + } + } +} diff --git a/src/main/java/redis/clients/jedis/timeseries/TimeSeriesProtocol.java b/src/main/java/redis/clients/jedis/timeseries/TimeSeriesProtocol.java index 2476979f0d..384a454921 100644 --- a/src/main/java/redis/clients/jedis/timeseries/TimeSeriesProtocol.java +++ b/src/main/java/redis/clients/jedis/timeseries/TimeSeriesProtocol.java @@ -57,6 +57,7 @@ public enum TimeSeriesKeyword implements Rawable { UNCOMPRESSED, CHUNK_SIZE, DUPLICATE_POLICY, + IGNORE, ON_DUPLICATE, ALIGN, FILTER_BY_TS, diff --git a/src/test/java/redis/clients/jedis/mocked/pipeline/PipeliningBaseTimeSeriesCommandsTest.java b/src/test/java/redis/clients/jedis/mocked/pipeline/PipeliningBaseTimeSeriesCommandsTest.java index 44e653c011..b8cfb85dc8 100644 --- a/src/test/java/redis/clients/jedis/mocked/pipeline/PipeliningBaseTimeSeriesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/mocked/pipeline/PipeliningBaseTimeSeriesCommandsTest.java @@ -3,6 +3,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.AbstractMap; @@ -11,17 +12,7 @@ import org.junit.Test; import redis.clients.jedis.Response; -import redis.clients.jedis.timeseries.AggregationType; -import redis.clients.jedis.timeseries.TSAlterParams; -import redis.clients.jedis.timeseries.TSCreateParams; -import redis.clients.jedis.timeseries.TSElement; -import redis.clients.jedis.timeseries.TSGetParams; -import redis.clients.jedis.timeseries.TSInfo; -import redis.clients.jedis.timeseries.TSMGetElement; -import redis.clients.jedis.timeseries.TSMGetParams; -import redis.clients.jedis.timeseries.TSMRangeElements; -import redis.clients.jedis.timeseries.TSMRangeParams; -import redis.clients.jedis.timeseries.TSRangeParams; +import redis.clients.jedis.timeseries.*; public class PipeliningBaseTimeSeriesCommandsTest extends PipeliningBaseMockedTestBase { @@ -57,6 +48,18 @@ public void testTsAddWithTimestampAndParams() { assertThat(response, is(predefinedResponse)); } + @Test + public void testTsAddWithParams() { + TSAddParams addParams = mock(TSAddParams.class); + + when(commandObjects.tsAdd("myTimeSeries", 1000L, 42.0, addParams)).thenReturn(longCommandObject); + + Response response = pipeliningBase.tsAdd("myTimeSeries", 1000L, 42.0, addParams); + + assertThat(commands, contains(longCommandObject)); + assertThat(response, is(predefinedResponse)); + } + @Test public void testTsAlter() { TSAlterParams alterParams = TSAlterParams.alterParams(); @@ -138,6 +141,17 @@ public void testTsDecrByWithTimestamp() { assertThat(response, is(predefinedResponse)); } + @Test + public void testTsDecrByWithParams() { + TSIncrOrDecrByParams DecrByParams = mock(TSIncrOrDecrByParams.class); + when(commandObjects.tsDecrBy("myTimeSeries", 1.0, DecrByParams)).thenReturn(longCommandObject); + + Response response = pipeliningBase.tsDecrBy("myTimeSeries", 1.0, DecrByParams); + + assertThat(commands, contains(longCommandObject)); + assertThat(response, is(predefinedResponse)); + } + @Test public void testTsDel() { when(commandObjects.tsDel("myTimeSeries", 1000L, 2000L)).thenReturn(longCommandObject); @@ -200,6 +214,17 @@ public void testTsIncrByWithTimestamp() { assertThat(response, is(predefinedResponse)); } + @Test + public void testTsIncrByWithParams() { + TSIncrOrDecrByParams incrByParams = mock(TSIncrOrDecrByParams.class); + when(commandObjects.tsIncrBy("myTimeSeries", 1.0, incrByParams)).thenReturn(longCommandObject); + + Response response = pipeliningBase.tsIncrBy("myTimeSeries", 1.0, incrByParams); + + assertThat(commands, contains(longCommandObject)); + assertThat(response, is(predefinedResponse)); + } + @Test public void testTsInfo() { when(commandObjects.tsInfo("myTimeSeries")).thenReturn(tsInfoCommandObject); diff --git a/src/test/java/redis/clients/jedis/mocked/unified/UnifiedJedisTimeSeriesCommandsTest.java b/src/test/java/redis/clients/jedis/mocked/unified/UnifiedJedisTimeSeriesCommandsTest.java index d9e06ce77c..53c673da49 100644 --- a/src/test/java/redis/clients/jedis/mocked/unified/UnifiedJedisTimeSeriesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/mocked/unified/UnifiedJedisTimeSeriesCommandsTest.java @@ -15,17 +15,7 @@ import java.util.Map; import org.junit.Test; -import redis.clients.jedis.timeseries.AggregationType; -import redis.clients.jedis.timeseries.TSAlterParams; -import redis.clients.jedis.timeseries.TSCreateParams; -import redis.clients.jedis.timeseries.TSElement; -import redis.clients.jedis.timeseries.TSGetParams; -import redis.clients.jedis.timeseries.TSInfo; -import redis.clients.jedis.timeseries.TSMGetElement; -import redis.clients.jedis.timeseries.TSMGetParams; -import redis.clients.jedis.timeseries.TSMRangeElements; -import redis.clients.jedis.timeseries.TSMRangeParams; -import redis.clients.jedis.timeseries.TSRangeParams; +import redis.clients.jedis.timeseries.*; public class UnifiedJedisTimeSeriesCommandsTest extends UnifiedJedisMockedTestBase { @@ -83,6 +73,25 @@ public void testTsAddWithTimestampAndParams() { verify(commandObjects).tsAdd(key, timestamp, value, createParams); } + @Test + public void testTsAddWithParams() { + String key = "testKey"; + long timestamp = 1582605077000L; + double value = 123.45; + TSAddParams createParams = mock(TSAddParams.class); + long expectedResponse = timestamp; // Timestamp of the added value + + when(commandObjects.tsAdd(key, timestamp, value, createParams)).thenReturn(longCommandObject); + when(commandExecutor.executeCommand(longCommandObject)).thenReturn(expectedResponse); + + long result = jedis.tsAdd(key, timestamp, value, createParams); + + assertEquals(expectedResponse, result); + + verify(commandExecutor).executeCommand(longCommandObject); + verify(commandObjects).tsAdd(key, timestamp, value, createParams); + } + @Test public void testTsAlter() { String key = "testKey"; @@ -194,7 +203,7 @@ public void testTsDecrByWithTimestamp() { String key = "testKey"; double value = 1.5; long timestamp = 1582605077000L; - long expectedResponse = -1L; // Assuming the decrement results in a total of -1 + long expectedResponse = 5L; when(commandObjects.tsDecrBy(key, value, timestamp)).thenReturn(longCommandObject); when(commandExecutor.executeCommand(longCommandObject)).thenReturn(expectedResponse); @@ -207,6 +216,24 @@ public void testTsDecrByWithTimestamp() { verify(commandObjects).tsDecrBy(key, value, timestamp); } + @Test + public void testTsDecrByWithParams() { + String key = "testKey"; + double value = 1.5; + TSIncrOrDecrByParams decrByParams = mock(TSIncrOrDecrByParams.class); + long expectedResponse = 5L; + + when(commandObjects.tsDecrBy(key, value, decrByParams)).thenReturn(longCommandObject); + when(commandExecutor.executeCommand(longCommandObject)).thenReturn(expectedResponse); + + long result = jedis.tsDecrBy(key, value, decrByParams); + + assertEquals(expectedResponse, result); + + verify(commandExecutor).executeCommand(longCommandObject); + verify(commandObjects).tsDecrBy(key, value, decrByParams); + } + @Test public void testTsDel() { String key = "testKey"; @@ -297,7 +324,7 @@ public void testTsIncrByWithTimestamp() { String key = "testKey"; double value = 2.5; long timestamp = 1582605077000L; - long expectedResponse = 5L; // Assuming the increment results in a total of 5 + long expectedResponse = 5L; when(commandObjects.tsIncrBy(key, value, timestamp)).thenReturn(longCommandObject); when(commandExecutor.executeCommand(longCommandObject)).thenReturn(expectedResponse); @@ -310,6 +337,24 @@ public void testTsIncrByWithTimestamp() { verify(commandObjects).tsIncrBy(key, value, timestamp); } + @Test + public void testTsIncrByWithParams() { + String key = "testKey"; + double value = 2.5; + TSIncrOrDecrByParams incrByParams = mock(TSIncrOrDecrByParams.class); + long expectedResponse = 5L; + + when(commandObjects.tsIncrBy(key, value, incrByParams)).thenReturn(longCommandObject); + when(commandExecutor.executeCommand(longCommandObject)).thenReturn(expectedResponse); + + long result = jedis.tsIncrBy(key, value, incrByParams); + + assertEquals(expectedResponse, result); + + verify(commandExecutor).executeCommand(longCommandObject); + verify(commandObjects).tsIncrBy(key, value, incrByParams); + } + @Test public void testTsInfo() { String key = "testKey"; diff --git a/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java b/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java index fe0f7d1604..dd0688f080 100644 --- a/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java +++ b/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java @@ -122,6 +122,23 @@ public void testAlter() { assertEquals("v33", info.getLabel("l3")); } + @Test + public void createAndAlterParams() { + Map labels = new HashMap<>(); + labels.put("l1", "v1"); + labels.put("l2", "v2"); + + assertEquals("OK", client.tsCreate("ts-params", + TSCreateParams.createParams().retention(60000).encoding(EncodingFormat.UNCOMPRESSED).chunkSize(4096) + .duplicatePolicy(DuplicatePolicy.BLOCK).ignore(50, 12.5).labels(labels))); + + labels.put("l1", "v11"); + labels.remove("l2"); + labels.put("l3", "v33"); + assertEquals("OK", client.tsAlter("ts-params", TSAlterParams.alterParams().retention(15000).chunkSize(8192) + .duplicatePolicy(DuplicatePolicy.SUM).ignore(50, 12.5).labels(labels))); + } + @Test public void testRule() { assertEquals("OK", client.tsCreate("source")); @@ -147,6 +164,21 @@ public void testRule() { } } + @Test + public void addParams() { + Map labels = new HashMap<>(); + labels.put("l1", "v1"); + labels.put("l2", "v2"); + + assertEquals(1000L, client.tsAdd("add1", 1000L, 1.1, + TSAddParams.addParams().retention(10000).encoding(EncodingFormat.UNCOMPRESSED).chunkSize(1000) + .duplicatePolicy(DuplicatePolicy.FIRST).onDuplicate(DuplicatePolicy.LAST).ignore(50, 12.5).labels(labels))); + + assertEquals(1000L, client.tsAdd("add2", 1000L, 1.1, + TSAddParams.addParams().retention(10000).encoding(EncodingFormat.COMPRESSED).chunkSize(1000) + .duplicatePolicy(DuplicatePolicy.MIN).onDuplicate(DuplicatePolicy.MAX).ignore(50, 12.5).labels(labels))); + } + @Test public void testAdd() { Map labels = new HashMap<>(); @@ -414,6 +446,29 @@ public void testIncrByDecrBy() throws InterruptedException { client.tsDecrBy("seriesIncDec", 33); } + @Test + public void incrByDecrByParams() { + Map labels = new HashMap<>(); + labels.put("l1", "v1"); + labels.put("l2", "v2"); + + assertEquals(1000L, client.tsIncrBy("incr1", 1.1, + TSIncrOrDecrByParams.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) + .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) + .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) + .chunkSize(1000).duplicatePolicy(DuplicatePolicy.MAX).ignore(50, 12.5).labels(labels))); + } + @Test public void align() { client.tsAdd("align", 1, 10d);