From 1c3df875880360c9ca93f0c217c054e532c34df9 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Fri, 14 Jun 2024 10:16:47 +0600 Subject: [PATCH] Support IGNORE and other optional arguments for timeseries commands (#3860) * Re-implement TS.ADD command with optional arguments * Implement TS.INCRBY and TS.DECRBY commands with optional arguments * Support IGNORE argument for TS.[ CREATE | ALTER | ADD | INCRBY | DECRBY] commands --- * Cover optional arguments for timeseries commands - Re-implement TS.ADD command with optional arguments - Implement TS.INCRBY and TS.DECRBY commands with optional arguments * Introduce EncodingFormat enum for * Support IGNORE option and rename to TSIncrOrDecrByParams --- .../redis/clients/jedis/CommandObjects.java | 20 ++- .../redis/clients/jedis/PipeliningBase.java | 15 ++ .../redis/clients/jedis/UnifiedJedis.java | 15 ++ .../jedis/timeseries/EncodingFormat.java | 24 ++++ .../timeseries/RedisTimeSeriesCommands.java | 55 +++++++- .../RedisTimeSeriesPipelineCommands.java | 7 + .../clients/jedis/timeseries/TSAddParams.java | 128 +++++++++++++++++ .../jedis/timeseries/TSAlterParams.java | 28 ++++ .../jedis/timeseries/TSCreateParams.java | 39 ++++-- .../timeseries/TSIncrOrDecrByParams.java | 132 ++++++++++++++++++ .../jedis/timeseries/TimeSeriesProtocol.java | 1 + .../modules/timeseries/TimeSeriesTest.java | 55 ++++++++ 12 files changed, 506 insertions(+), 13 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/timeseries/EncodingFormat.java create mode 100644 src/main/java/redis/clients/jedis/timeseries/TSAddParams.java create mode 100644 src/main/java/redis/clients/jedis/timeseries/TSIncrOrDecrByParams.java diff --git a/src/main/java/redis/clients/jedis/CommandObjects.java b/src/main/java/redis/clients/jedis/CommandObjects.java index b87256c3a2..f93932becb 100644 --- a/src/main/java/redis/clients/jedis/CommandObjects.java +++ b/src/main/java/redis/clients/jedis/CommandObjects.java @@ -3941,9 +3941,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) { @@ -3963,6 +3969,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); } @@ -3972,6 +3983,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 e1666f486a..d37e11ad1a 100644 --- a/src/main/java/redis/clients/jedis/PipeliningBase.java +++ b/src/main/java/redis/clients/jedis/PipeliningBase.java @@ -3930,6 +3930,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)); @@ -3945,6 +3950,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)); @@ -3955,6 +3965,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 a501990361..c319cb8326 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -4461,6 +4461,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)); @@ -4476,6 +4481,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)); @@ -4486,6 +4496,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/modules/timeseries/TimeSeriesTest.java b/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java index 12c424f9e1..3f8eb66e7c 100644 --- a/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java +++ b/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java @@ -115,6 +115,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")); @@ -140,6 +157,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<>(); @@ -407,6 +439,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);