Skip to content

Commit 71dac36

Browse files
authored
Double timeout, avoid Tuple and more (#2481)
* Chnage to and/or add 'double' timeout option * Add/modify Keyed... response objects * Remove UNUSABLE methods * Rename keysAndTimeout to getKeysAndTimeout to have common name in both BinaryClient and BinaryJedis * Deprecate pipeline methods absent from infaces which now have different alternatives * JavaDoc for new Response classes * package-info for new packages
1 parent 6bcfc21 commit 71dac36

28 files changed

+560
-319
lines changed

src/main/java/redis/clients/jedis/BinaryClient.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ public void lmove(byte[] srcKey, byte[] dstKey, ListDirection from, ListDirectio
753753
sendCommand(LMOVE, srcKey, dstKey, from.getRaw(), to.getRaw());
754754
}
755755

756-
public void blmove(byte[] srcKey, byte[] dstKey, ListDirection from, ListDirection to, int timeout) {
756+
public void blmove(byte[] srcKey, byte[] dstKey, ListDirection from, ListDirection to, double timeout) {
757757
sendCommand(BLMOVE, srcKey, dstKey, from.getRaw(), to.getRaw(), toByteArray(timeout));
758758
}
759759

@@ -762,26 +762,42 @@ public void blpop(final byte[][] args) {
762762
}
763763

764764
public void blpop(final int timeout, final byte[]... keys) {
765-
blpop(keysAndTimeout(timeout, keys));
765+
blpop(getKeysAndTimeout(timeout, keys));
766+
}
767+
768+
public void blpop(final double timeout, final byte[]... keys) {
769+
blpop(getKeysAndTimeout(timeout, keys));
766770
}
767771

768772
public void brpop(final byte[][] args) {
769773
sendCommand(BRPOP, args);
770774
}
771775

772776
public void brpop(final int timeout, final byte[]... keys) {
773-
brpop(keysAndTimeout(timeout, keys));
777+
brpop(getKeysAndTimeout(timeout, keys));
778+
}
779+
780+
public void brpop(final double timeout, final byte[]... keys) {
781+
brpop(getKeysAndTimeout(timeout, keys));
782+
}
783+
784+
public void bzpopmax(final double timeout, final byte[]... keys) {
785+
sendCommand(BZPOPMAX, getKeysAndTimeout(timeout, keys));
774786
}
775787

776-
public void bzpopmax(final int timeout, final byte[]... keys) {
777-
sendCommand(BZPOPMAX, keysAndTimeout(timeout, keys));
788+
public void bzpopmin(final double timeout, final byte[]... keys) {
789+
sendCommand(BZPOPMIN, getKeysAndTimeout(timeout, keys));
778790
}
779791

780-
public void bzpopmin(final int timeout, final byte[]... keys) {
781-
sendCommand(BZPOPMIN, keysAndTimeout(timeout, keys));
792+
private static byte[][] getKeysAndTimeout(final int timeout, final byte[]... keys) {
793+
int numKeys = keys.length;
794+
byte[][] args = new byte[numKeys + 1][];
795+
System.arraycopy(keys, 0, args, 0, numKeys);
796+
args[numKeys] = toByteArray(timeout);
797+
return args;
782798
}
783799

784-
private static byte[][] keysAndTimeout(final int timeout, final byte[]... keys) {
800+
private static byte[][] getKeysAndTimeout(final double timeout, final byte[]... keys) {
785801
int numKeys = keys.length;
786802
byte[][] args = new byte[numKeys + 1][];
787803
System.arraycopy(keys, 0, args, 0, numKeys);

src/main/java/redis/clients/jedis/BinaryJedis.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import javax.net.ssl.SSLParameters;
2222
import javax.net.ssl.SSLSocketFactory;
2323

24-
import redis.clients.jedis.args.ListDirection;
25-
import redis.clients.jedis.args.FlushMode;
26-
import redis.clients.jedis.args.UnblockType;
24+
import redis.clients.jedis.args.*;
2725
import redis.clients.jedis.commands.AdvancedBinaryJedisCommands;
2826
import redis.clients.jedis.commands.BasicCommands;
2927
import redis.clients.jedis.commands.BinaryJedisCommands;
@@ -34,6 +32,7 @@
3432
import redis.clients.jedis.exceptions.JedisDataException;
3533
import redis.clients.jedis.exceptions.JedisException;
3634
import redis.clients.jedis.params.*;
35+
import redis.clients.jedis.resps.*;
3736
import redis.clients.jedis.util.JedisURIHelper;
3837

3938
public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKeyBinaryCommands,
@@ -2475,7 +2474,7 @@ public byte[] lmove(byte[] srcKey, byte[] dstKey, ListDirection from, ListDirect
24752474
* @return
24762475
*/
24772476
@Override
2478-
public byte[] blmove(byte[] srcKey, byte[] dstKey, ListDirection from, ListDirection to, int timeout) {
2477+
public byte[] blmove(byte[] srcKey, byte[] dstKey, ListDirection from, ListDirection to, double timeout) {
24792478
checkIsInMultiOrPipeline();
24802479
client.blmove(srcKey, dstKey, from, to, timeout);
24812480
client.setTimeoutInfinite();
@@ -2553,6 +2552,11 @@ public List<byte[]> blpop(final int timeout, final byte[]... keys) {
25532552
return blpop(getKeysAndTimeout(timeout, keys));
25542553
}
25552554

2555+
@Override
2556+
public List<byte[]> blpop(final double timeout, final byte[]... keys) {
2557+
return blpop(getKeysAndTimeout(timeout, keys));
2558+
}
2559+
25562560
/**
25572561
* BLPOP (and BRPOP) is a blocking list pop primitive. You can see this commands as blocking
25582562
* versions of LPOP and RPOP able to block if the specified keys don't exist or contain empty
@@ -2620,6 +2624,11 @@ public List<byte[]> brpop(final int timeout, final byte[]... keys) {
26202624
return brpop(getKeysAndTimeout(timeout, keys));
26212625
}
26222626

2627+
@Override
2628+
public List<byte[]> brpop(final double timeout, final byte[]... keys) {
2629+
return brpop(getKeysAndTimeout(timeout, keys));
2630+
}
2631+
26232632
@Override
26242633
public List<byte[]> blpop(final byte[]... args) {
26252634
checkIsInMultiOrPipeline();
@@ -2652,25 +2661,33 @@ private byte[][] getKeysAndTimeout(int timeout, byte[][] keys) {
26522661
return args;
26532662
}
26542663

2664+
private byte[][] getKeysAndTimeout(double timeout, byte[][] keys) {
2665+
int size = keys.length;
2666+
final byte[][] args = new byte[size + 1][];
2667+
System.arraycopy(keys, 0, args, 0, size);
2668+
args[size] = Protocol.toByteArray(timeout);
2669+
return args;
2670+
}
2671+
26552672
@Override
2656-
public KeyedTuple bzpopmax(final int timeout, final byte[]... keys) {
2673+
public List<byte[]> bzpopmax(final double timeout, final byte[]... keys) {
26572674
checkIsInMultiOrPipeline();
26582675
client.bzpopmax(timeout, keys);
26592676
client.setTimeoutInfinite();
26602677
try {
2661-
return BuilderFactory.KEYED_TUPLE.build(client.getBinaryMultiBulkReply());
2678+
return client.getBinaryMultiBulkReply();
26622679
} finally {
26632680
client.rollbackTimeout();
26642681
}
26652682
}
26662683

26672684
@Override
2668-
public KeyedTuple bzpopmin(final int timeout, final byte[]... keys) {
2685+
public List<byte[]> bzpopmin(final double timeout, final byte[]... keys) {
26692686
checkIsInMultiOrPipeline();
26702687
client.bzpopmin(timeout, keys);
26712688
client.setTimeoutInfinite();
26722689
try {
2673-
return BuilderFactory.KEYED_TUPLE.build(client.getBinaryMultiBulkReply());
2690+
return client.getBinaryMultiBulkReply();
26742691
} finally {
26752692
client.rollbackTimeout();
26762693
}

src/main/java/redis/clients/jedis/BinaryJedisCluster.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package redis.clients.jedis;
22

3-
import redis.clients.jedis.args.ListDirection;
4-
import redis.clients.jedis.args.FlushMode;
3+
import redis.clients.jedis.args.*;
54
import redis.clients.jedis.commands.BinaryJedisClusterCommands;
65
import redis.clients.jedis.commands.JedisClusterBinaryScriptingCommands;
76
import redis.clients.jedis.commands.MultiKeyBinaryJedisClusterCommands;
87
import redis.clients.jedis.commands.ProtocolCommand;
98
import redis.clients.jedis.params.*;
9+
import redis.clients.jedis.resps.*;
1010
import redis.clients.jedis.util.JedisClusterHashTagUtil;
1111
import redis.clients.jedis.util.KeyMergeUtil;
1212
import redis.clients.jedis.util.SafeEncoder;
@@ -1787,7 +1787,7 @@ public byte[] execute(Jedis connection) {
17871787

17881788
@Override
17891789
public byte[] blmove(final byte[] srcKey, final byte[] dstKey, final ListDirection from,
1790-
final ListDirection to, final int timeout) {
1790+
final ListDirection to, final double timeout) {
17911791
return new JedisClusterCommand<byte[]>(connectionHandler, maxAttempts) {
17921792
@Override
17931793
public byte[] execute(Jedis connection) {
@@ -1806,6 +1806,16 @@ public List<byte[]> execute(Jedis connection) {
18061806
}.runBinary(keys.length, keys);
18071807
}
18081808

1809+
@Override
1810+
public List<byte[]> blpop(final double timeout, final byte[]... keys) {
1811+
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
1812+
@Override
1813+
public List<byte[]> execute(Jedis connection) {
1814+
return connection.blpop(timeout, keys);
1815+
}
1816+
}.runBinary(keys.length, keys);
1817+
}
1818+
18091819
@Override
18101820
public List<byte[]> brpop(final int timeout, final byte[]... keys) {
18111821
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
@@ -1817,20 +1827,30 @@ public List<byte[]> execute(Jedis connection) {
18171827
}
18181828

18191829
@Override
1820-
public KeyedTuple bzpopmax(int timeout, byte[]... keys) {
1821-
return new JedisClusterCommand<KeyedTuple>(connectionHandler, maxAttempts) {
1830+
public List<byte[]> brpop(final double timeout, final byte[]... keys) {
1831+
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
1832+
@Override
1833+
public List<byte[]> execute(Jedis connection) {
1834+
return connection.brpop(timeout, keys);
1835+
}
1836+
}.runBinary(keys.length, keys);
1837+
}
1838+
1839+
@Override
1840+
public List<byte[]> bzpopmax(double timeout, byte[]... keys) {
1841+
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
18221842
@Override
1823-
public KeyedTuple execute(Jedis connection) {
1843+
public List<byte[]> execute(Jedis connection) {
18241844
return connection.bzpopmax(timeout, keys);
18251845
}
18261846
}.runBinary(keys.length, keys);
18271847
}
18281848

18291849
@Override
1830-
public KeyedTuple bzpopmin(int timeout, byte[]... keys) {
1831-
return new JedisClusterCommand<KeyedTuple>(connectionHandler, maxAttempts) {
1850+
public List<byte[]> bzpopmin(double timeout, byte[]... keys) {
1851+
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
18321852
@Override
1833-
public KeyedTuple execute(Jedis connection) {
1853+
public List<byte[]> execute(Jedis connection) {
18341854
return connection.bzpopmin(timeout, keys);
18351855
}
18361856
}.runBinary(keys.length, keys);

src/main/java/redis/clients/jedis/BuilderFactory.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.Set;
1212

13+
import redis.clients.jedis.resps.*;
1314
import redis.clients.jedis.util.JedisByteHashMap;
1415
import redis.clients.jedis.util.SafeEncoder;
1516

@@ -356,6 +357,21 @@ public String toString() {
356357

357358
};
358359

360+
public static final Builder<KeyedListElement> KEYED_LIST_ELEMENT = new Builder<KeyedListElement>() {
361+
@Override
362+
@SuppressWarnings("unchecked")
363+
public KeyedListElement build(Object data) {
364+
if (data == null) return null;
365+
List<byte[]> l = (List<byte[]>) data;
366+
return new KeyedListElement(l.get(0), l.get(1));
367+
}
368+
369+
@Override
370+
public String toString() {
371+
return "KeyedListElement";
372+
}
373+
};
374+
359375
public static final Builder<Tuple> TUPLE = new Builder<Tuple>() {
360376
@Override
361377
@SuppressWarnings("unchecked")
@@ -374,22 +390,21 @@ public String toString() {
374390

375391
};
376392

377-
public static final Builder<KeyedTuple> KEYED_TUPLE = new Builder<KeyedTuple>() {
393+
public static final Builder<KeyedZSetElement> KEYED_ZSET_ELEMENT = new Builder<KeyedZSetElement>() {
378394
@Override
379395
@SuppressWarnings("unchecked")
380-
public KeyedTuple build(Object data) {
396+
public KeyedZSetElement build(Object data) {
381397
List<byte[]> l = (List<byte[]>) data; // never null
382398
if (l.isEmpty()) {
383399
return null;
384400
}
385-
return new KeyedTuple(l.get(0), l.get(1), DOUBLE.build(l.get(2)));
401+
return new KeyedZSetElement(l.get(0), l.get(1), DOUBLE.build(l.get(2)));
386402
}
387403

388404
@Override
389405
public String toString() {
390-
return "KeyedTuple";
406+
return "KeyedZSetElement";
391407
}
392-
393408
};
394409

395410
public static final Builder<Set<Tuple>> TUPLE_ZSET = new Builder<Set<Tuple>>() {

src/main/java/redis/clients/jedis/Client.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import static redis.clients.jedis.Protocol.toByteArray;
44

5-
import java.util.ArrayList;
6-
import java.util.Collections;
75
import java.util.HashMap;
8-
import java.util.List;
96
import java.util.Map;
107
import java.util.Map.Entry;
118

@@ -637,7 +634,8 @@ public void lmove(String srcKey, String dstKey, ListDirection from, ListDirectio
637634
}
638635

639636
@Override
640-
public void blmove(String srcKey, String dstKey, ListDirection from, ListDirection to, int timeout) {
637+
public void blmove(String srcKey, String dstKey, ListDirection from, ListDirection to,
638+
double timeout) {
641639
blmove(SafeEncoder.encode(srcKey), SafeEncoder.encode(dstKey), from, to, timeout);
642640
}
643641

@@ -651,6 +649,11 @@ public void blpop(final int timeout, final String... keys) {
651649
blpop(timeout, SafeEncoder.encodeMany(keys));
652650
}
653651

652+
@Override
653+
public void blpop(final double timeout, final String... keys) {
654+
blpop(timeout, SafeEncoder.encodeMany(keys));
655+
}
656+
654657
@Override
655658
public void brpop(final String[] args) {
656659
brpop(SafeEncoder.encodeMany(args));
@@ -662,12 +665,17 @@ public void brpop(final int timeout, final String... keys) {
662665
}
663666

664667
@Override
665-
public void bzpopmax(final int timeout, final String... keys) {
668+
public void brpop(final double timeout, final String... keys) {
669+
brpop(timeout, SafeEncoder.encodeMany(keys));
670+
}
671+
672+
@Override
673+
public void bzpopmax(final double timeout, final String... keys) {
666674
bzpopmax(timeout, SafeEncoder.encodeMany(keys));
667675
}
668676

669677
@Override
670-
public void bzpopmin(final int timeout, final String... keys) {
678+
public void bzpopmin(final double timeout, final String... keys) {
671679
bzpopmin(timeout, SafeEncoder.encodeMany(keys));
672680
}
673681

0 commit comments

Comments
 (0)