From 3ad03e6c4469780358f62933452e1725f9f1fe01 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Tue, 23 Mar 2021 19:49:38 +0600 Subject: [PATCH] Add/modify Keyed... response objects --- .../java/redis/clients/jedis/BinaryJedis.java | 13 +++-- .../clients/jedis/BinaryJedisCluster.java | 16 +++--- .../redis/clients/jedis/BuilderFactory.java | 25 +++++++-- src/main/java/redis/clients/jedis/Jedis.java | 52 ++++++++++--------- .../redis/clients/jedis/JedisCluster.java | 39 +++++++------- .../java/redis/clients/jedis/KeyedTuple.java | 47 ----------------- .../clients/jedis/MultiKeyPipelineBase.java | 28 +++++----- .../redis/clients/jedis/ShardedJedis.java | 5 +- .../jedis/commands/JedisClusterCommands.java | 5 +- .../clients/jedis/commands/JedisCommands.java | 5 +- .../commands/MultiKeyBinaryCommands.java | 7 ++- .../MultiKeyBinaryJedisClusterCommands.java | 7 ++- .../commands/MultiKeyBinaryRedisPipeline.java | 7 ++- .../jedis/commands/MultiKeyCommands.java | 13 ++--- .../commands/MultiKeyCommandsPipeline.java | 12 ++--- .../MultiKeyJedisClusterCommands.java | 12 ++--- .../clients/jedis/resps/KeyedListElement.java | 45 ++++++++++++++++ .../clients/jedis/resps/KeyedZSetElement.java | 43 +++++++++++++++ .../tests/commands/ListCommandsTest.java | 10 ++-- .../tests/commands/SortedSetCommandsTest.java | 18 +++---- 20 files changed, 236 insertions(+), 173 deletions(-) delete mode 100644 src/main/java/redis/clients/jedis/KeyedTuple.java create mode 100644 src/main/java/redis/clients/jedis/resps/KeyedListElement.java create mode 100644 src/main/java/redis/clients/jedis/resps/KeyedZSetElement.java diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index b1faf7aeec4..3ce3cb7c681 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -21,9 +21,7 @@ import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLSocketFactory; -import redis.clients.jedis.args.ListDirection; -import redis.clients.jedis.args.FlushMode; -import redis.clients.jedis.args.UnblockType; +import redis.clients.jedis.args.*; import redis.clients.jedis.commands.AdvancedBinaryJedisCommands; import redis.clients.jedis.commands.BasicCommands; import redis.clients.jedis.commands.BinaryJedisCommands; @@ -34,6 +32,7 @@ import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.params.*; +import redis.clients.jedis.resps.*; import redis.clients.jedis.util.JedisURIHelper; public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKeyBinaryCommands, @@ -2660,24 +2659,24 @@ private byte[][] getKeysAndTimeout(double timeout, byte[][] keys) { } @Override - public KeyedTuple bzpopmax(final double timeout, final byte[]... keys) { + public List bzpopmax(final double timeout, final byte[]... keys) { checkIsInMultiOrPipeline(); client.bzpopmax(timeout, keys); client.setTimeoutInfinite(); try { - return BuilderFactory.KEYED_TUPLE.build(client.getBinaryMultiBulkReply()); + return client.getBinaryMultiBulkReply(); } finally { client.rollbackTimeout(); } } @Override - public KeyedTuple bzpopmin(final double timeout, final byte[]... keys) { + public List bzpopmin(final double timeout, final byte[]... keys) { checkIsInMultiOrPipeline(); client.bzpopmin(timeout, keys); client.setTimeoutInfinite(); try { - return BuilderFactory.KEYED_TUPLE.build(client.getBinaryMultiBulkReply()); + return client.getBinaryMultiBulkReply(); } finally { client.rollbackTimeout(); } diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCluster.java b/src/main/java/redis/clients/jedis/BinaryJedisCluster.java index 91b0dbb09ae..f60ff3c1b83 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCluster.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCluster.java @@ -1,12 +1,12 @@ package redis.clients.jedis; -import redis.clients.jedis.args.ListDirection; -import redis.clients.jedis.args.FlushMode; +import redis.clients.jedis.args.*; import redis.clients.jedis.commands.BinaryJedisClusterCommands; import redis.clients.jedis.commands.JedisClusterBinaryScriptingCommands; import redis.clients.jedis.commands.MultiKeyBinaryJedisClusterCommands; import redis.clients.jedis.commands.ProtocolCommand; import redis.clients.jedis.params.*; +import redis.clients.jedis.resps.*; import redis.clients.jedis.util.JedisClusterHashTagUtil; import redis.clients.jedis.util.KeyMergeUtil; import redis.clients.jedis.util.SafeEncoder; @@ -1826,20 +1826,20 @@ public List execute(Jedis connection) { } @Override - public KeyedTuple bzpopmax(double timeout, byte[]... keys) { - return new JedisClusterCommand(connectionHandler, maxAttempts) { + public List bzpopmax(double timeout, byte[]... keys) { + return new JedisClusterCommand>(connectionHandler, maxAttempts) { @Override - public KeyedTuple execute(Jedis connection) { + public List execute(Jedis connection) { return connection.bzpopmax(timeout, keys); } }.runBinary(keys.length, keys); } @Override - public KeyedTuple bzpopmin(double timeout, byte[]... keys) { - return new JedisClusterCommand(connectionHandler, maxAttempts) { + public List bzpopmin(double timeout, byte[]... keys) { + return new JedisClusterCommand>(connectionHandler, maxAttempts) { @Override - public KeyedTuple execute(Jedis connection) { + public List execute(Jedis connection) { return connection.bzpopmin(timeout, keys); } }.runBinary(keys.length, keys); diff --git a/src/main/java/redis/clients/jedis/BuilderFactory.java b/src/main/java/redis/clients/jedis/BuilderFactory.java index 1a26f57b18b..2c49fff00e4 100644 --- a/src/main/java/redis/clients/jedis/BuilderFactory.java +++ b/src/main/java/redis/clients/jedis/BuilderFactory.java @@ -10,6 +10,7 @@ import java.util.Map; import java.util.Set; +import redis.clients.jedis.resps.*; import redis.clients.jedis.util.JedisByteHashMap; import redis.clients.jedis.util.SafeEncoder; @@ -356,6 +357,21 @@ public String toString() { }; + public static final Builder KEYED_LIST_ELEMENT = new Builder() { + @Override + @SuppressWarnings("unchecked") + public KeyedListElement build(Object data) { + if (data == null) return null; + List l = (List) data; + return new KeyedListElement(l.get(0), l.get(1)); + } + + @Override + public String toString() { + return "KeyedListElement"; + } + }; + public static final Builder TUPLE = new Builder() { @Override @SuppressWarnings("unchecked") @@ -374,22 +390,21 @@ public String toString() { }; - public static final Builder KEYED_TUPLE = new Builder() { + public static final Builder KEYED_ZSET_ELEMENT = new Builder() { @Override @SuppressWarnings("unchecked") - public KeyedTuple build(Object data) { + public KeyedZSetElement build(Object data) { List l = (List) data; // never null if (l.isEmpty()) { return null; } - return new KeyedTuple(l.get(0), l.get(1), DOUBLE.build(l.get(2))); + return new KeyedZSetElement(l.get(0), l.get(1), DOUBLE.build(l.get(2))); } @Override public String toString() { - return "KeyedTuple"; + return "KeyedZSetElement"; } - }; public static final Builder> TUPLE_ZSET = new Builder>() { diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index c6192119f81..f24d6b69912 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -13,10 +13,10 @@ import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLSocketFactory; -import redis.clients.jedis.args.ListDirection; +import redis.clients.jedis.args.*; import redis.clients.jedis.commands.*; import redis.clients.jedis.params.*; -import redis.clients.jedis.args.UnblockType; +import redis.clients.jedis.resps.*; import redis.clients.jedis.util.SafeEncoder; import redis.clients.jedis.util.Slowlog; @@ -2185,8 +2185,15 @@ public List blpop(final int timeout, final String... keys) { } @Override - public List blpop(final double timeout, final String... keys) { - return blpop(getKeysAndTimeout(timeout, keys)); + public KeyedListElement blpop(final double timeout, final String... keys) { + checkIsInMultiOrPipeline(); + client.blpop(timeout, keys); + client.setTimeoutInfinite(); + try { + return BuilderFactory.KEYED_LIST_ELEMENT.build(client.getMultiBulkReply()); + } finally { + client.rollbackTimeout(); + } } /** @@ -2257,8 +2264,15 @@ public List brpop(final int timeout, final String... keys) { } @Override - public List brpop(final double timeout, final String... keys) { - return brpop(getKeysAndTimeout(timeout, keys)); + public KeyedListElement brpop(final double timeout, final String... keys) { + checkIsInMultiOrPipeline(); + client.brpop(timeout, keys); + client.setTimeoutInfinite(); + try { + return BuilderFactory.KEYED_LIST_ELEMENT.build(client.getMultiBulkReply()); + } finally { + client.rollbackTimeout(); + } } private String[] getKeysAndTimeout(int timeout, String[] keys) { @@ -2271,16 +2285,6 @@ private String[] getKeysAndTimeout(int timeout, String[] keys) { return args; } - private String[] getKeysAndTimeout(double timeout, String[] keys) { - final int keyCount = keys.length; - final String[] args = new String[keyCount + 1]; - - System.arraycopy(keys, 0, args, 0, keyCount); - - args[keyCount] = String.valueOf(timeout); - return args; - } - @Override public List blpop(final String... args) { checkIsInMultiOrPipeline(); @@ -2306,24 +2310,24 @@ public List brpop(final String... args) { } @Override - public KeyedTuple bzpopmax(double timeout, String... keys) { + public KeyedZSetElement bzpopmax(double timeout, String... keys) { checkIsInMultiOrPipeline(); client.bzpopmax(timeout, keys); client.setTimeoutInfinite(); try { - return BuilderFactory.KEYED_TUPLE.build(client.getObjectMultiBulkReply()); + return BuilderFactory.KEYED_ZSET_ELEMENT.build(client.getObjectMultiBulkReply()); } finally { client.rollbackTimeout(); } } @Override - public KeyedTuple bzpopmin(double timeout, String... keys) { + public KeyedZSetElement bzpopmin(double timeout, String... keys) { checkIsInMultiOrPipeline(); client.bzpopmin(timeout, keys); client.setTimeoutInfinite(); try { - return BuilderFactory.KEYED_TUPLE.build(client.getObjectMultiBulkReply()); + return BuilderFactory.KEYED_ZSET_ELEMENT.build(client.getObjectMultiBulkReply()); } finally { client.rollbackTimeout(); } @@ -2335,8 +2339,8 @@ public List blpop(final int timeout, final String key) { } @Override - public List blpop(double timeout, String key) { - return blpop(key, String.valueOf(timeout)); + public KeyedListElement blpop(double timeout, String key) { + return blpop(timeout, new String[]{key}); } @Override @@ -2345,8 +2349,8 @@ public List brpop(final int timeout, final String key) { } @Override - public List brpop(double timeout, String key) { - return brpop(key, String.valueOf(timeout)); + public KeyedListElement brpop(double timeout, String key) { + return brpop(timeout, new String[]{key}); } @Override diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index ce8a732f856..afdf1346846 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -1,11 +1,12 @@ package redis.clients.jedis; -import redis.clients.jedis.args.ListDirection; +import redis.clients.jedis.args.*; import redis.clients.jedis.commands.JedisClusterCommands; import redis.clients.jedis.commands.JedisClusterScriptingCommands; import redis.clients.jedis.commands.MultiKeyJedisClusterCommands; import redis.clients.jedis.commands.ProtocolCommand; import redis.clients.jedis.params.*; +import redis.clients.jedis.resps.*; import redis.clients.jedis.util.JedisClusterHashTagUtil; import redis.clients.jedis.util.KeyMergeUtil; @@ -1843,10 +1844,10 @@ public List execute(Jedis connection) { } @Override - public List blpop(final double timeout, final String... keys) { - return new JedisClusterCommand>(connectionHandler, maxAttempts) { + public KeyedListElement blpop(final double timeout, final String... keys) { + return new JedisClusterCommand(connectionHandler, maxAttempts) { @Override - public List execute(Jedis connection) { + public KeyedListElement execute(Jedis connection) { return connection.blpop(timeout, keys); } }.run(keys.length, keys); @@ -1864,30 +1865,30 @@ public List execute(Jedis connection) { } @Override - public List brpop(final double timeout, final String... keys) { - return new JedisClusterCommand>(connectionHandler, maxAttempts) { + public KeyedListElement brpop(final double timeout, final String... keys) { + return new JedisClusterCommand(connectionHandler, maxAttempts) { @Override - public List execute(Jedis connection) { + public KeyedListElement execute(Jedis connection) { return connection.brpop(timeout, keys); } }.run(keys.length, keys); } @Override - public KeyedTuple bzpopmax(double timeout, String... keys) { - return new JedisClusterCommand(connectionHandler, maxAttempts) { + public KeyedZSetElement bzpopmax(double timeout, String... keys) { + return new JedisClusterCommand(connectionHandler, maxAttempts) { @Override - public KeyedTuple execute(Jedis connection) { + public KeyedZSetElement execute(Jedis connection) { return connection.bzpopmax(timeout, keys); } }.run(keys.length, keys); } @Override - public KeyedTuple bzpopmin(double timeout, String... keys) { - return new JedisClusterCommand(connectionHandler, maxAttempts) { + public KeyedZSetElement bzpopmin(double timeout, String... keys) { + return new JedisClusterCommand(connectionHandler, maxAttempts) { @Override - public KeyedTuple execute(Jedis connection) { + public KeyedZSetElement execute(Jedis connection) { return connection.bzpopmin(timeout, keys); } }.run(keys.length, keys); @@ -1904,10 +1905,10 @@ public List execute(Jedis connection) { } @Override - public List blpop(double timeout, String key) { - return new JedisClusterCommand>(connectionHandler, maxAttempts) { + public KeyedListElement blpop(double timeout, String key) { + return new JedisClusterCommand(connectionHandler, maxAttempts) { @Override - public List execute(Jedis connection) { + public KeyedListElement execute(Jedis connection) { return connection.blpop(timeout, key); } }.run(key); @@ -1924,10 +1925,10 @@ public List execute(Jedis connection) { } @Override - public List brpop(double timeout, String key) { - return new JedisClusterCommand>(connectionHandler, maxAttempts) { + public KeyedListElement brpop(double timeout, String key) { + return new JedisClusterCommand(connectionHandler, maxAttempts) { @Override - public List execute(Jedis connection) { + public KeyedListElement execute(Jedis connection) { return connection.brpop(timeout, key); } }.run(key); diff --git a/src/main/java/redis/clients/jedis/KeyedTuple.java b/src/main/java/redis/clients/jedis/KeyedTuple.java deleted file mode 100644 index bf0bd39e985..00000000000 --- a/src/main/java/redis/clients/jedis/KeyedTuple.java +++ /dev/null @@ -1,47 +0,0 @@ -package redis.clients.jedis; - -import redis.clients.jedis.util.SafeEncoder; - -import java.util.Arrays; - -public class KeyedTuple extends Tuple { - private byte[] key; - - public KeyedTuple(byte[] key, byte[] element, Double score) { - super(element, score); - this.key = key; - } - - public KeyedTuple(String key, String element, Double score) { - super(element, score); - this.key = SafeEncoder.encode(key); - } - - public String getKey() { - if (null != key) { - return SafeEncoder.encode(key); - } - return null; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof KeyedTuple)) return false; - if (!super.equals(o)) return false; - - KeyedTuple that = (KeyedTuple) o; - return Arrays.equals(key, that.key); - } - - @Override - public int hashCode() { - return 31 * (key != null ? Arrays.hashCode(key) : 0) + super.hashCode(); - } - - @Override - public String toString() { - return "KeyedTuple{" + "key=" + SafeEncoder.encode(key) + ", element='" + getElement() + "'" - + ", score=" + getScore() + "} "; - } -} diff --git a/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java b/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java index cf3c6190f6c..5de5ee2f051 100644 --- a/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java +++ b/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java @@ -1,9 +1,9 @@ package redis.clients.jedis; -import redis.clients.jedis.args.ListDirection; -import redis.clients.jedis.args.FlushMode; +import redis.clients.jedis.args.*; import redis.clients.jedis.commands.*; import redis.clients.jedis.params.*; +import redis.clients.jedis.resps.*; import java.util.List; import java.util.Map; @@ -97,9 +97,9 @@ public Response> blpop(int timeout, byte[]... keys) { } @Override - public Response> blpop(double timeout, String... keys) { + public Response blpop(double timeout, String... keys) { client.blpop(timeout, keys); - return getResponse(BuilderFactory.STRING_LIST); + return getResponse(BuilderFactory.KEYED_LIST_ELEMENT); } @Override @@ -138,9 +138,9 @@ public Response> brpop(int timeout, byte[]... keys) { } @Override - public Response> brpop(double timeout, String... keys) { + public Response brpop(double timeout, String... keys) { client.brpop(timeout, keys); - return getResponse(BuilderFactory.STRING_LIST); + return getResponse(BuilderFactory.KEYED_LIST_ELEMENT); } @Override @@ -150,27 +150,27 @@ public Response> brpop(double timeout, byte[]... keys) { } @Override - public Response bzpopmax(double timeout, String... keys) { + public Response bzpopmax(double timeout, String... keys) { client.bzpopmax(timeout, keys); - return getResponse(BuilderFactory.KEYED_TUPLE); + return getResponse(BuilderFactory.KEYED_ZSET_ELEMENT); } @Override - public Response bzpopmax(double timeout, byte[]... keys) { + public Response> bzpopmax(double timeout, byte[]... keys) { client.bzpopmax(timeout, keys); - return getResponse(BuilderFactory.KEYED_TUPLE); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); } @Override - public Response bzpopmin(double timeout, String... keys) { + public Response bzpopmin(double timeout, String... keys) { client.bzpopmin(timeout, keys); - return getResponse(BuilderFactory.KEYED_TUPLE); + return getResponse(BuilderFactory.KEYED_ZSET_ELEMENT); } @Override - public Response bzpopmin(double timeout, byte[]... keys) { + public Response> bzpopmin(double timeout, byte[]... keys) { client.bzpopmin(timeout, keys); - return getResponse(BuilderFactory.KEYED_TUPLE); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); } @Override diff --git a/src/main/java/redis/clients/jedis/ShardedJedis.java b/src/main/java/redis/clients/jedis/ShardedJedis.java index f3582b1e285..71187e8b694 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedis.java +++ b/src/main/java/redis/clients/jedis/ShardedJedis.java @@ -20,6 +20,7 @@ import redis.clients.jedis.params.ZAddParams; import redis.clients.jedis.params.ZIncrByParams; import redis.clients.jedis.params.LPosParams; +import redis.clients.jedis.resps.KeyedListElement; import redis.clients.jedis.util.Hashing; public class ShardedJedis extends BinaryShardedJedis implements JedisCommands, Closeable { @@ -211,7 +212,7 @@ public List blpop(final int timeout, final String key) { } @Override - public List blpop(final double timeout, final String key) { + public KeyedListElement blpop(final double timeout, final String key) { Jedis j = getShard(key); return j.blpop(timeout, key); } @@ -229,7 +230,7 @@ public List brpop(final int timeout, final String key) { } @Override - public List brpop(final double timeout, final String key) { + public KeyedListElement brpop(final double timeout, final String key) { Jedis j = getShard(key); return j.brpop(timeout, key); } diff --git a/src/main/java/redis/clients/jedis/commands/JedisClusterCommands.java b/src/main/java/redis/clients/jedis/commands/JedisClusterCommands.java index c5d4369891b..f44b924ff48 100644 --- a/src/main/java/redis/clients/jedis/commands/JedisClusterCommands.java +++ b/src/main/java/redis/clients/jedis/commands/JedisClusterCommands.java @@ -22,6 +22,7 @@ import redis.clients.jedis.params.ZAddParams; import redis.clients.jedis.params.ZIncrByParams; import redis.clients.jedis.params.LPosParams; +import redis.clients.jedis.resps.KeyedListElement; import java.util.List; import java.util.Map; @@ -318,11 +319,11 @@ default String setex(String key, int seconds, String value) { List blpop(int timeout, String key); - List blpop(double timeout, String key); + KeyedListElement blpop(double timeout, String key); List brpop(int timeout, String key); - List brpop(double timeout, String key); + KeyedListElement brpop(double timeout, String key); Long del(String key); diff --git a/src/main/java/redis/clients/jedis/commands/JedisCommands.java b/src/main/java/redis/clients/jedis/commands/JedisCommands.java index d3c3fa404b1..e81d1945fa6 100644 --- a/src/main/java/redis/clients/jedis/commands/JedisCommands.java +++ b/src/main/java/redis/clients/jedis/commands/JedisCommands.java @@ -31,6 +31,7 @@ import redis.clients.jedis.params.ZAddParams; import redis.clients.jedis.params.ZIncrByParams; import redis.clients.jedis.params.LPosParams; +import redis.clients.jedis.resps.KeyedListElement; /** * Common interface for sharded and non-sharded Jedis @@ -337,11 +338,11 @@ default String setex(String key, int seconds, String value) { List blpop(int timeout, String key); - List blpop(double timeout, String key); + KeyedListElement blpop(double timeout, String key); List brpop(int timeout, String key); - List brpop(double timeout, String key); + KeyedListElement brpop(double timeout, String key); Long del(String key); diff --git a/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryCommands.java b/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryCommands.java index 4c525a7ea41..981454c8a58 100644 --- a/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryCommands.java +++ b/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryCommands.java @@ -3,11 +3,10 @@ import redis.clients.jedis.BinaryJedisPubSub; import redis.clients.jedis.BitOP; import redis.clients.jedis.GeoUnit; -import redis.clients.jedis.KeyedTuple; import redis.clients.jedis.SortingParams; import redis.clients.jedis.Tuple; import redis.clients.jedis.ZParams; -import redis.clients.jedis.args.ListDirection; +import redis.clients.jedis.args.*; import redis.clients.jedis.params.GeoRadiusParam; import redis.clients.jedis.params.GeoRadiusStoreParam; import redis.clients.jedis.params.XReadGroupParams; @@ -45,9 +44,9 @@ public interface MultiKeyBinaryCommands { List brpop(byte[]... args); - KeyedTuple bzpopmax(double timeout, byte[]... keys); + List bzpopmax(double timeout, byte[]... keys); - KeyedTuple bzpopmin(double timeout, byte[]... keys); + List bzpopmin(double timeout, byte[]... keys); Set keys(byte[] pattern); diff --git a/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryJedisClusterCommands.java b/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryJedisClusterCommands.java index 378906c6d42..d862721bbde 100644 --- a/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryJedisClusterCommands.java +++ b/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryJedisClusterCommands.java @@ -5,11 +5,10 @@ import redis.clients.jedis.GeoUnit; import redis.clients.jedis.ScanParams; import redis.clients.jedis.ScanResult; -import redis.clients.jedis.KeyedTuple; import redis.clients.jedis.SortingParams; import redis.clients.jedis.Tuple; import redis.clients.jedis.ZParams; -import redis.clients.jedis.args.ListDirection; +import redis.clients.jedis.args.*; import redis.clients.jedis.params.GeoRadiusParam; import redis.clients.jedis.params.GeoRadiusStoreParam; import redis.clients.jedis.params.XReadGroupParams; @@ -41,9 +40,9 @@ public interface MultiKeyBinaryJedisClusterCommands { List brpop(double timeout, byte[]... keys); - KeyedTuple bzpopmax(double timeout, byte[]... keys); + List bzpopmax(double timeout, byte[]... keys); - KeyedTuple bzpopmin(double timeout, byte[]... keys); + List bzpopmin(double timeout, byte[]... keys); List mget(byte[]... keys); diff --git a/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryRedisPipeline.java b/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryRedisPipeline.java index c5a26478361..f077ead00e3 100644 --- a/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryRedisPipeline.java +++ b/src/main/java/redis/clients/jedis/commands/MultiKeyBinaryRedisPipeline.java @@ -3,11 +3,10 @@ import redis.clients.jedis.BitOP; import redis.clients.jedis.GeoUnit; import redis.clients.jedis.Response; -import redis.clients.jedis.KeyedTuple; import redis.clients.jedis.SortingParams; import redis.clients.jedis.Tuple; import redis.clients.jedis.ZParams; -import redis.clients.jedis.args.ListDirection; +import redis.clients.jedis.args.*; import redis.clients.jedis.params.*; import java.util.List; @@ -40,9 +39,9 @@ public interface MultiKeyBinaryRedisPipeline { Response> brpop(double timeout, byte[]... args); - Response bzpopmax(double timeout, byte[]... keys); + Response> bzpopmax(double timeout, byte[]... keys); - Response bzpopmin(double timeout, byte[]... keys); + Response> bzpopmin(double timeout, byte[]... keys); Response> keys(byte[] pattern); diff --git a/src/main/java/redis/clients/jedis/commands/MultiKeyCommands.java b/src/main/java/redis/clients/jedis/commands/MultiKeyCommands.java index a44410f1b27..d0a6b305177 100644 --- a/src/main/java/redis/clients/jedis/commands/MultiKeyCommands.java +++ b/src/main/java/redis/clients/jedis/commands/MultiKeyCommands.java @@ -2,7 +2,7 @@ import redis.clients.jedis.BitOP; import redis.clients.jedis.GeoUnit; -import redis.clients.jedis.KeyedTuple; +import redis.clients.jedis.resps.KeyedZSetElement; import redis.clients.jedis.StreamEntryID; import redis.clients.jedis.JedisPubSub; import redis.clients.jedis.ScanParams; @@ -11,11 +11,12 @@ import redis.clients.jedis.StreamEntry; import redis.clients.jedis.Tuple; import redis.clients.jedis.ZParams; -import redis.clients.jedis.args.ListDirection; +import redis.clients.jedis.args.*; import redis.clients.jedis.params.GeoRadiusParam; import redis.clients.jedis.params.GeoRadiusStoreParam; import redis.clients.jedis.params.XReadGroupParams; import redis.clients.jedis.params.XReadParams; +import redis.clients.jedis.resps.*; import java.util.List; import java.util.Map; @@ -39,19 +40,19 @@ public interface MultiKeyCommands { List blpop(int timeout, String... keys); - List blpop(double timeout, String... keys); + KeyedListElement blpop(double timeout, String... keys); List brpop(int timeout, String... keys); - List brpop(double timeout, String... keys); + KeyedListElement brpop(double timeout, String... keys); List blpop(String... args); List brpop(String... args); - KeyedTuple bzpopmax(double timeout, String... keys); + KeyedZSetElement bzpopmax(double timeout, String... keys); - KeyedTuple bzpopmin(double timeout, String... keys); + KeyedZSetElement bzpopmin(double timeout, String... keys); /** * Returns all the keys matching the glob-style pattern. For example if you have in the database diff --git a/src/main/java/redis/clients/jedis/commands/MultiKeyCommandsPipeline.java b/src/main/java/redis/clients/jedis/commands/MultiKeyCommandsPipeline.java index be177414883..40e698b0b9d 100644 --- a/src/main/java/redis/clients/jedis/commands/MultiKeyCommandsPipeline.java +++ b/src/main/java/redis/clients/jedis/commands/MultiKeyCommandsPipeline.java @@ -3,14 +3,14 @@ import redis.clients.jedis.BitOP; import redis.clients.jedis.GeoUnit; import redis.clients.jedis.Response; -import redis.clients.jedis.KeyedTuple; import redis.clients.jedis.SortingParams; import redis.clients.jedis.StreamEntry; import redis.clients.jedis.StreamEntryID; import redis.clients.jedis.Tuple; import redis.clients.jedis.ZParams; -import redis.clients.jedis.args.ListDirection; +import redis.clients.jedis.args.*; import redis.clients.jedis.params.*; +import redis.clients.jedis.resps.*; import java.util.List; import java.util.Map; @@ -39,17 +39,17 @@ Response blmove(String srcKey, String dstKey, ListDirection from, ListDi Response> blpop(int timeout, String... args); - Response> blpop(double timeout, String... args); + Response blpop(double timeout, String... args); Response> brpop(String... args); Response> brpop(int timeout, String... args); - Response> brpop(double timeout, String... args); + Response brpop(double timeout, String... args); - Response bzpopmax(double timeout, String... keys); + Response bzpopmax(double timeout, String... keys); - Response bzpopmin(double timeout, String... keys); + Response bzpopmin(double timeout, String... keys); Response> keys(String pattern); diff --git a/src/main/java/redis/clients/jedis/commands/MultiKeyJedisClusterCommands.java b/src/main/java/redis/clients/jedis/commands/MultiKeyJedisClusterCommands.java index 4aaf07584a3..4d60b94a401 100644 --- a/src/main/java/redis/clients/jedis/commands/MultiKeyJedisClusterCommands.java +++ b/src/main/java/redis/clients/jedis/commands/MultiKeyJedisClusterCommands.java @@ -5,17 +5,17 @@ import redis.clients.jedis.JedisPubSub; import redis.clients.jedis.ScanParams; import redis.clients.jedis.ScanResult; -import redis.clients.jedis.KeyedTuple; import redis.clients.jedis.SortingParams; import redis.clients.jedis.StreamEntry; import redis.clients.jedis.StreamEntryID; import redis.clients.jedis.Tuple; import redis.clients.jedis.ZParams; -import redis.clients.jedis.args.ListDirection; +import redis.clients.jedis.args.*; import redis.clients.jedis.params.GeoRadiusParam; import redis.clients.jedis.params.GeoRadiusStoreParam; import redis.clients.jedis.params.XReadGroupParams; import redis.clients.jedis.params.XReadParams; +import redis.clients.jedis.resps.*; import java.util.List; import java.util.Map; @@ -36,15 +36,15 @@ public interface MultiKeyJedisClusterCommands { List blpop(int timeout, String... keys); - List blpop(double timeout, String... keys); + KeyedListElement blpop(double timeout, String... keys); List brpop(int timeout, String... keys); - List brpop(double timeout, String... keys); + KeyedListElement brpop(double timeout, String... keys); - KeyedTuple bzpopmax(double timeout, String... keys); + KeyedZSetElement bzpopmax(double timeout, String... keys); - KeyedTuple bzpopmin(double timeout, String... keys); + KeyedZSetElement bzpopmin(double timeout, String... keys); List mget(String... keys); diff --git a/src/main/java/redis/clients/jedis/resps/KeyedListElement.java b/src/main/java/redis/clients/jedis/resps/KeyedListElement.java new file mode 100644 index 00000000000..222fa9fbf13 --- /dev/null +++ b/src/main/java/redis/clients/jedis/resps/KeyedListElement.java @@ -0,0 +1,45 @@ +package redis.clients.jedis.resps; + +import redis.clients.jedis.util.SafeEncoder; + +public class KeyedListElement { + + private final String key; + private final String element; + + public KeyedListElement(byte[] key, byte[] element) { + this(SafeEncoder.encode(key), SafeEncoder.encode(element)); + } + + public KeyedListElement(String key, String element) { + this.key = key; + this.element = element; + } + + public String getKey() { + return key; + } + + public String getElement() { + return element; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof KeyedZSetElement)) return false; + + KeyedListElement other = (KeyedListElement) o; + return key.equals(other.key) && element.equals(other.element); + } + + @Override + public int hashCode() { + return 31 * key.hashCode() + element.hashCode(); + } + + @Override + public String toString() { + return "KeyedListElement{" + "key=" + key + ", element='" + element + "} "; + } +} diff --git a/src/main/java/redis/clients/jedis/resps/KeyedZSetElement.java b/src/main/java/redis/clients/jedis/resps/KeyedZSetElement.java new file mode 100644 index 00000000000..bea9b8a3e3e --- /dev/null +++ b/src/main/java/redis/clients/jedis/resps/KeyedZSetElement.java @@ -0,0 +1,43 @@ +package redis.clients.jedis.resps; + +import redis.clients.jedis.Tuple; +import redis.clients.jedis.util.SafeEncoder; + +public class KeyedZSetElement extends Tuple { + + private final String key; + + public KeyedZSetElement(byte[] key, byte[] element, Double score) { + super(element, score); + this.key = SafeEncoder.encode(key); + } + + public KeyedZSetElement(String key, String element, Double score) { + super(element, score); + this.key = key; + } + + public String getKey() { + return key; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof KeyedZSetElement)) return false; + + if (!key.equals(((KeyedZSetElement) o).key)) return false; + return super.equals(o); + } + + @Override + public int hashCode() { + return 31 * key.hashCode() + super.hashCode(); + } + + @Override + public String toString() { + return "KeyedZSetElement{" + "key=" + key + ", element='" + getElement() + "'" + + ", score=" + getScore() + "} "; + } +} diff --git a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java index fbaccbc378a..8d0206f19ce 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java @@ -538,8 +538,9 @@ public void brpoplpush() { public void run() { try { Thread.sleep(100); - Jedis j = createJedis(); - j.lpush("foo", "a"); + try (Jedis j = createJedis()) { + j.lpush("foo", "a"); + } } catch (InterruptedException e) { org.apache.logging.log4j.LogManager.getLogger().error("Interruption in string lpush", e); } @@ -559,8 +560,9 @@ public void run() { public void run() { try { Thread.sleep(100); - Jedis j = createJedis(); - j.lpush(bfoo, bA); + try (Jedis j = createJedis()) { + j.lpush(bfoo, bA); + } } catch (InterruptedException e) { org.apache.logging.log4j.LogManager.getLogger().error("Interruption in binary lpush", e); } diff --git a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java index d0ecc3b5494..d0a201c34a4 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertNotNull; import static redis.clients.jedis.ScanParams.SCAN_POINTER_START; import static redis.clients.jedis.ScanParams.SCAN_POINTER_START_BINARY; +import static redis.clients.jedis.tests.utils.AssertUtil.assertByteArrayListEquals; import static redis.clients.jedis.tests.utils.AssertUtil.assertByteArraySetEquals; import static redis.clients.jedis.tests.utils.AssertUtil.assertCollectionContains; @@ -17,13 +18,14 @@ import org.junit.Test; +import redis.clients.jedis.Protocol; import redis.clients.jedis.ScanParams; import redis.clients.jedis.ScanResult; -import redis.clients.jedis.KeyedTuple; import redis.clients.jedis.Tuple; import redis.clients.jedis.ZParams; import redis.clients.jedis.params.ZAddParams; import redis.clients.jedis.params.ZIncrByParams; +import redis.clients.jedis.resps.KeyedZSetElement; import redis.clients.jedis.util.SafeEncoder; public class SortedSetCommandsTest extends JedisCommandTestBase { @@ -1487,15 +1489,14 @@ public void bzpopmax() { jedis.zadd("foo", 1d, "a", ZAddParams.zAddParams().nx()); jedis.zadd("foo", 10d, "b", ZAddParams.zAddParams().nx()); jedis.zadd("bar", 0.1d, "c", ZAddParams.zAddParams().nx()); - KeyedTuple actual = jedis.bzpopmax(0, "foo", "bar"); - assertEquals(new KeyedTuple("foo", "b", 10d), actual); + assertEquals(new KeyedZSetElement("foo", "b", 10d), jedis.bzpopmax(0, "foo", "bar")); // Binary jedis.zadd(bfoo, 1d, ba); jedis.zadd(bfoo, 10d, bb); jedis.zadd(bbar, 0.1d, bc); - actual = jedis.bzpopmax(0, bfoo, bbar); - assertEquals(new KeyedTuple(bfoo, bb, 10d), actual); + assertByteArrayListEquals(Arrays.asList(bfoo, bb, Protocol.toByteArray(10d)), + jedis.bzpopmax(0, bfoo, bbar)); } @Test @@ -1503,15 +1504,14 @@ public void bzpopmin() { jedis.zadd("foo", 1d, "a", ZAddParams.zAddParams().nx()); jedis.zadd("foo", 10d, "b", ZAddParams.zAddParams().nx()); jedis.zadd("bar", 0.1d, "c", ZAddParams.zAddParams().nx()); - KeyedTuple actual = jedis.bzpopmin(0, "bar", "foo"); - assertEquals(new KeyedTuple("bar", "c", 0.1d), actual); + assertEquals(new KeyedZSetElement("bar", "c", 0.1d), jedis.bzpopmin(0, "bar", "foo")); // Binary jedis.zadd(bfoo, 1d, ba); jedis.zadd(bfoo, 10d, bb); jedis.zadd(bbar, 0.1d, bc); - actual = jedis.bzpopmin(0, bbar, bfoo); - assertEquals(new KeyedTuple(bbar, bc, 0.1d), actual); + assertByteArrayListEquals(Arrays.asList(bbar, bc, Protocol.toByteArray(0.1d)), + jedis.bzpopmin(0, bbar, bfoo)); } @Test