Fixed infinite timeout handling. Increased code reuse so that there will be fewer copy and paste errors.

This commit is contained in:
rdifalco
2014-10-18 12:22:07 -07:00
parent 7845dc350a
commit 8f4dbaf89f
5 changed files with 190 additions and 229 deletions

View File

@@ -249,9 +249,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
public Set<byte[]> keys(final byte[] pattern) { public Set<byte[]> keys(final byte[] pattern) {
checkIsInMulti(); checkIsInMulti();
client.keys(pattern); client.keys(pattern);
final HashSet<byte[]> keySet = new HashSet<byte[]>( return new HashSet<byte[]>(client.getBinaryMultiBulkReply());
client.getBinaryMultiBulkReply());
return keySet;
} }
/** /**
@@ -515,7 +513,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
/** /**
* Set the the respective keys to the respective values. MSET will replace * Set the the respective keys to the respective values. MSET will replace
* old values with new values, while {@link #msetnx(String...) MSETNX} will * old values with new values, while {@link #msetnx(byte[]...) MSETNX} will
* not perform any operation at all even if just a single key already * not perform any operation at all even if just a single key already
* exists. * exists.
* <p> * <p>
@@ -528,7 +526,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* either see the changes to both A and B at once, or no modification at * either see the changes to both A and B at once, or no modification at
* all. * all.
* *
* @see #msetnx(String...) * @see #msetnx(byte[]...)
* *
* @param keysvalues * @param keysvalues
* @return Status code reply Basically +OK as MSET can't fail * @return Status code reply Basically +OK as MSET can't fail
@@ -541,7 +539,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
/** /**
* Set the the respective keys to the respective values. * Set the the respective keys to the respective values.
* {@link #mset(String...) MSET} will replace old values with new values, * {@link #mset(byte[]...) MSET} will replace old values with new values,
* while MSETNX will not perform any operation at all even if just a single * while MSETNX will not perform any operation at all even if just a single
* key already exists. * key already exists.
* <p> * <p>
@@ -554,8 +552,8 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* either see the changes to both A and B at once, or no modification at * either see the changes to both A and B at once, or no modification at
* all. * all.
* *
* @see #mset(String...) * @see #mset(byte[]...)
* *
* @param keysvalues * @param keysvalues
* @return Integer reply, specifically: 1 if the all the keys were set 0 if * @return Integer reply, specifically: 1 if the all the keys were set 0 if
* no key was set (at least one key already existed) * no key was set (at least one key already existed)
@@ -567,7 +565,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
} }
/** /**
* IDECRBY work just like {@link #decr(String) INCR} but instead to * DECRRBY work just like {@link #decr(byte[]) INCR} but instead to
* decrement by 1 the decrement is integer. * decrement by 1 the decrement is integer.
* <p> * <p>
* INCR commands are limited to 64 bit signed integers. * INCR commands are limited to 64 bit signed integers.
@@ -651,7 +649,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
} }
/** /**
* INCRBYFLOAT work just like {@link #incrBy(byte[]) INCRBY} but increments * INCRBYFLOAT work just like {@link #incrBy(byte[], long)} INCRBY} but increments
* by floats instead of integers. * by floats instead of integers.
* <p> * <p>
* INCRBYFLOAT commands are limited to double precision floating point * INCRBYFLOAT commands are limited to double precision floating point
@@ -669,8 +667,8 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* @see #decr(byte[]) * @see #decr(byte[])
* @see #decrBy(byte[], long) * @see #decrBy(byte[], long)
* *
* @param key * @param key the key to increment
* @param integer * @param integer the value to increment by
* @return Integer reply, this commands will reply with the new value of key * @return Integer reply, this commands will reply with the new value of key
* after the increment. * after the increment.
*/ */
@@ -974,8 +972,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
public List<byte[]> hvals(final byte[] key) { public List<byte[]> hvals(final byte[] key) {
checkIsInMulti(); checkIsInMulti();
client.hvals(key); client.hvals(key);
final List<byte[]> lresult = client.getBinaryMultiBulkReply(); return client.getBinaryMultiBulkReply();
return lresult;
} }
/** /**
@@ -1180,7 +1177,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* O(N) (with N being the length of the list), setting the first or last * O(N) (with N being the length of the list), setting the first or last
* elements of the list is O(1). * elements of the list is O(1).
* *
* @see #lindex(byte[], int) * @see #lindex(byte[], long)
* *
* @param key * @param key
* @param index * @param index
@@ -1301,11 +1298,11 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
/** /**
* Return all the members (elements) of the set value stored at key. This is * Return all the members (elements) of the set value stored at key. This is
* just syntax glue for {@link #sinter(String...) SINTER}. * just syntax glue for {@link #sinter(byte[]...)} SINTER}.
* <p> * <p>
* Time complexity O(N) * Time complexity O(N)
* *
* @param key * @param key the key of the set
* @return Multi bulk reply * @return Multi bulk reply
*/ */
public Set<byte[]> smembers(final byte[] key) { public Set<byte[]> smembers(final byte[] key) {
@@ -1322,8 +1319,8 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* <p> * <p>
* Time complexity O(1) * Time complexity O(1)
* *
* @param key * @param key the key of the set
* @param member * @param member the set member to remove
* @return Integer reply, specifically: 1 if the new element was removed 0 * @return Integer reply, specifically: 1 if the new element was removed 0
* if the new element was not a member of the set * if the new element was not a member of the set
*/ */
@@ -1416,7 +1413,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
/** /**
* Return the members of a set resulting from the intersection of all the * Return the members of a set resulting from the intersection of all the
* sets hold at the specified keys. Like in * sets hold at the specified keys. Like in
* {@link #lrange(byte[], int, int) LRANGE} the result is sent to the client * {@link #lrange(byte[], long, long)} LRANGE} the result is sent to the client
* as a multi-bulk reply (see the protocol specification for more * as a multi-bulk reply (see the protocol specification for more
* information). If just a single key is specified, then this command * information). If just a single key is specified, then this command
* produces the same result as {@link #smembers(byte[]) SMEMBERS}. Actually * produces the same result as {@link #smembers(byte[]) SMEMBERS}. Actually
@@ -1440,7 +1437,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
} }
/** /**
* This commnad works exactly like {@link #sinter(String...) SINTER} but * This commnad works exactly like {@link #sinter(byte[]...) SINTER} but
* instead of being returned the resulting set is sotred as dstkey. * instead of being returned the resulting set is sotred as dstkey.
* <p> * <p>
* Time complexity O(N*M) worst case where N is the cardinality of the * Time complexity O(N*M) worst case where N is the cardinality of the
@@ -1458,7 +1455,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
/** /**
* Return the members of a set resulting from the union of all the sets hold * Return the members of a set resulting from the union of all the sets hold
* at the specified keys. Like in {@link #lrange(byte[], int, int) LRANGE} * at the specified keys. Like in {@link #lrange(byte[], long, long)} LRANGE}
* the result is sent to the client as a multi-bulk reply (see the protocol * the result is sent to the client as a multi-bulk reply (see the protocol
* specification for more information). If just a single key is specified, * specification for more information). If just a single key is specified,
* then this command produces the same result as {@link #smembers(byte[]) * then this command produces the same result as {@link #smembers(byte[])
@@ -1480,7 +1477,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
} }
/** /**
* This command works exactly like {@link #sunion(String...) SUNION} but * This command works exactly like {@link #sunion(byte[]...) SUNION} but
* instead of being returned the resulting set is stored as dstkey. Any * instead of being returned the resulting set is stored as dstkey. Any
* existing value in dstkey will be over-written. * existing value in dstkey will be over-written.
* <p> * <p>
@@ -1528,7 +1525,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
} }
/** /**
* This command works exactly like {@link #sdiff(String...) SDIFF} but * This command works exactly like {@link #sdiff(byte[]...) SDIFF} but
* instead of being returned the resulting set is stored in dstkey. * instead of being returned the resulting set is stored in dstkey.
* *
* @param dstkey * @param dstkey
@@ -1722,16 +1719,14 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
final long end) { final long end) {
checkIsInMulti(); checkIsInMulti();
client.zrangeWithScores(key, start, end); client.zrangeWithScores(key, start, end);
Set<Tuple> set = getBinaryTupledSet(); return getBinaryTupledSet();
return set;
} }
public Set<Tuple> zrevrangeWithScores(final byte[] key, final long start, public Set<Tuple> zrevrangeWithScores(final byte[] key, final long start,
final long end) { final long end) {
checkIsInMulti(); checkIsInMulti();
client.zrevrangeWithScores(key, start, end); client.zrevrangeWithScores(key, start, end);
Set<Tuple> set = getBinaryTupledSet(); return getBinaryTupledSet();
return set;
} }
/** /**
@@ -1782,13 +1777,11 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* @see https://github.com/xetorthio/jedis/pull/498 * @see https://github.com/xetorthio/jedis/pull/498
*/ */
public List<Object> multi(final TransactionBlock jedisTransaction) { public List<Object> multi(final TransactionBlock jedisTransaction) {
List<Object> results = null;
jedisTransaction.setClient(client); jedisTransaction.setClient(client);
client.multi(); client.multi();
client.getOne(); // expected OK client.getOne(); // expected OK
jedisTransaction.execute(); jedisTransaction.execute();
results = jedisTransaction.exec(); return jedisTransaction.exec();
return results;
} }
protected void checkIsInMulti() { protected void checkIsInMulti() {
@@ -2008,7 +2001,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* <p> * <p>
* Time complexity: O(1) * Time complexity: O(1)
* *
* @see #brpop(int, String...) * @see #brpop(int, byte[]...)
* *
* @param timeout * @param timeout
* @param keys * @param keys
@@ -2021,18 +2014,17 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* programming language used. * programming language used.
*/ */
public List<byte[]> blpop(final int timeout, final byte[]... keys) { public List<byte[]> blpop(final int timeout, final byte[]... keys) {
checkIsInMulti(); return blpop(getArgsAddTimeout(timeout, keys));
final List<byte[]> args = new ArrayList<byte[]>(); }
for (final byte[] arg : keys) {
args.add(arg);
}
args.add(Protocol.toByteArray(timeout));
client.blpop(args.toArray(new byte[args.size()][])); private byte[][] getArgsAddTimeout (int timeout, byte[][] keys) {
client.setTimeoutInfinite(); int size = keys.length;
final List<byte[]> multiBulkReply = client.getBinaryMultiBulkReply(); final byte[][] args = new byte[size + 1][];
client.rollbackTimeout(); for (int at = 0; at != size; ++at) {
return multiBulkReply; args[at] = keys[at];
}
args[size] = Protocol.toByteArray(timeout);
return args;
} }
/** /**
@@ -2137,7 +2129,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* <p> * <p>
* Time complexity: O(1) * Time complexity: O(1)
* *
* @see #blpop(int, String...) * @see #blpop(int, byte[]...)
* *
* @param timeout * @param timeout
* @param keys * @param keys
@@ -2150,59 +2142,39 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* programming language used. * programming language used.
*/ */
public List<byte[]> brpop(final int timeout, final byte[]... keys) { public List<byte[]> brpop(final int timeout, final byte[]... keys) {
checkIsInMulti(); return brpop(getArgsAddTimeout(timeout, keys));
final List<byte[]> args = new ArrayList<byte[]>();
for (final byte[] arg : keys) {
args.add(arg);
}
args.add(Protocol.toByteArray(timeout));
client.brpop(args.toArray(new byte[args.size()][]));
client.setTimeoutInfinite();
final List<byte[]> multiBulkReply = client.getBinaryMultiBulkReply();
client.rollbackTimeout();
return multiBulkReply;
} }
@Deprecated
public List<byte[]> blpop(byte[] arg) { public List<byte[]> blpop(byte[] arg) {
checkIsInMulti(); return blpop(new byte[][]{arg});
byte[][] args = new byte[1][];
args[0] = arg;
client.blpop(args);
client.setTimeoutInfinite();
final List<byte[]> multiBulkReply = client.getBinaryMultiBulkReply();
client.rollbackTimeout();
return multiBulkReply;
} }
@Deprecated
public List<byte[]> brpop(byte[] arg) { public List<byte[]> brpop(byte[] arg) {
checkIsInMulti(); return brpop(new byte[][]{arg});
byte[][] args = new byte[1][];
args[0] = arg;
client.brpop(args);
client.setTimeoutInfinite();
final List<byte[]> multiBulkReply = client.getBinaryMultiBulkReply();
client.rollbackTimeout();
return multiBulkReply;
} }
public List<byte[]> blpop(byte[]... args) { public List<byte[]> blpop(byte[]... args) {
checkIsInMulti(); checkIsInMulti();
client.blpop(args); client.blpop(args);
client.setTimeoutInfinite(); client.setTimeoutInfinite();
final List<byte[]> multiBulkReply = client.getBinaryMultiBulkReply(); try {
client.rollbackTimeout(); return client.getBinaryMultiBulkReply();
return multiBulkReply; } finally {
client.rollbackTimeout();
}
} }
public List<byte[]> brpop(byte[]... args) { public List<byte[]> brpop(byte[]... args) {
checkIsInMulti(); checkIsInMulti();
client.brpop(args); client.brpop(args);
client.setTimeoutInfinite(); client.setTimeoutInfinite();
final List<byte[]> multiBulkReply = client.getBinaryMultiBulkReply(); try {
client.rollbackTimeout(); return client.getBinaryMultiBulkReply();
return multiBulkReply; } finally {
client.rollbackTimeout();
}
} }
/** /**
@@ -2457,8 +2429,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
final byte[] min, final byte[] max) { final byte[] min, final byte[] max) {
checkIsInMulti(); checkIsInMulti();
client.zrangeByScoreWithScores(key, min, max); client.zrangeByScoreWithScores(key, min, max);
Set<Tuple> set = getBinaryTupledSet(); return getBinaryTupledSet();
return set;
} }
/** /**
@@ -2529,8 +2500,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
final int count) { final int count) {
checkIsInMulti(); checkIsInMulti();
client.zrangeByScoreWithScores(key, min, max, offset, count); client.zrangeByScoreWithScores(key, min, max, offset, count);
Set<Tuple> set = getBinaryTupledSet(); return getBinaryTupledSet();
return set;
} }
private Set<Tuple> getBinaryTupledSet() { private Set<Tuple> getBinaryTupledSet() {
@@ -2587,8 +2557,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
final byte[] max, final byte[] min) { final byte[] max, final byte[] min) {
checkIsInMulti(); checkIsInMulti();
client.zrevrangeByScoreWithScores(key, max, min); client.zrevrangeByScoreWithScores(key, max, min);
Set<Tuple> set = getBinaryTupledSet(); return getBinaryTupledSet();
return set;
} }
public Set<Tuple> zrevrangeByScoreWithScores(final byte[] key, public Set<Tuple> zrevrangeByScoreWithScores(final byte[] key,
@@ -2596,8 +2565,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
final int count) { final int count) {
checkIsInMulti(); checkIsInMulti();
client.zrevrangeByScoreWithScores(key, max, min, offset, count); client.zrevrangeByScoreWithScores(key, max, min, offset, count);
Set<Tuple> set = getBinaryTupledSet(); return getBinaryTupledSet();
return set;
} }
/** /**
@@ -2652,10 +2620,10 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* input keys N, before passing the input keys and the other (optional) * input keys N, before passing the input keys and the other (optional)
* arguments. * arguments.
* <p> * <p>
* As the terms imply, the {@link #zinterstore(String, String...) * As the terms imply, the {@link #zinterstore(byte[], byte[]...)}
* ZINTERSTORE} command requires an element to be present in each of the * ZINTERSTORE} command requires an element to be present in each of the
* given inputs to be inserted in the result. The * given inputs to be inserted in the result. The
* {@link #zunionstore(String, String...) ZUNIONSTORE} command inserts all * {@link #zunionstore(byte[], byte[]...)}} command inserts all
* elements across all inputs. * elements across all inputs.
* <p> * <p>
* Using the WEIGHTS option, it is possible to add weight to each input * Using the WEIGHTS option, it is possible to add weight to each input
@@ -2674,10 +2642,10 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* sizes of the input sorted sets, and M being the number of elements in the * sizes of the input sorted sets, and M being the number of elements in the
* resulting sorted set * resulting sorted set
* *
* @see #zunionstore(String, String...) * @see #zunionstore(byte[], byte[]...)
* @see #zunionstore(String, ZParams, String...) * @see #zunionstore(byte[], ZParams, byte[]...)
* @see #zinterstore(String, String...) * @see #zinterstore(byte[], byte[]...)
* @see #zinterstore(String, ZParams, String...) * @see #zinterstore(byte[], ZParams, byte[]...)
* *
* @param dstkey * @param dstkey
* @param sets * @param sets
@@ -2696,10 +2664,10 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* input keys N, before passing the input keys and the other (optional) * input keys N, before passing the input keys and the other (optional)
* arguments. * arguments.
* <p> * <p>
* As the terms imply, the {@link #zinterstore(String, String...) * As the terms imply, the {@link #zinterstore(byte[], byte[]...)
* ZINTERSTORE} command requires an element to be present in each of the * ZINTERSTORE} command requires an element to be present in each of the
* given inputs to be inserted in the result. The * given inputs to be inserted in the result. The
* {@link #zunionstore(String, String...) ZUNIONSTORE} command inserts all * {@link #zunionstore(byte[], byte[]...) ZUNIONSTORE} command inserts all
* elements across all inputs. * elements across all inputs.
* <p> * <p>
* Using the WEIGHTS option, it is possible to add weight to each input * Using the WEIGHTS option, it is possible to add weight to each input
@@ -2718,10 +2686,10 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* sizes of the input sorted sets, and M being the number of elements in the * sizes of the input sorted sets, and M being the number of elements in the
* resulting sorted set * resulting sorted set
* *
* @see #zunionstore(String, String...) * @see #zunionstore(byte[], byte[]...)
* @see #zunionstore(String, ZParams, String...) * @see #zunionstore(byte[], ZParams, byte[]...)
* @see #zinterstore(String, String...) * @see #zinterstore(byte[], byte[]...)
* @see #zinterstore(String, ZParams, String...) * @see #zinterstore(byte[], ZParams, byte[]...)
* *
* @param dstkey * @param dstkey
* @param sets * @param sets
@@ -2742,10 +2710,10 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* input keys N, before passing the input keys and the other (optional) * input keys N, before passing the input keys and the other (optional)
* arguments. * arguments.
* <p> * <p>
* As the terms imply, the {@link #zinterstore(String, String...) * As the terms imply, the {@link #zinterstore(byte[], byte[]...)
* ZINTERSTORE} command requires an element to be present in each of the * ZINTERSTORE} command requires an element to be present in each of the
* given inputs to be inserted in the result. The * given inputs to be inserted in the result. The
* {@link #zunionstore(String, String...) ZUNIONSTORE} command inserts all * {@link #zunionstore(byte[], byte[]...) ZUNIONSTORE} command inserts all
* elements across all inputs. * elements across all inputs.
* <p> * <p>
* Using the WEIGHTS option, it is possible to add weight to each input * Using the WEIGHTS option, it is possible to add weight to each input
@@ -2764,10 +2732,10 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* sizes of the input sorted sets, and M being the number of elements in the * sizes of the input sorted sets, and M being the number of elements in the
* resulting sorted set * resulting sorted set
* *
* @see #zunionstore(String, String...) * @see #zunionstore(byte[], byte[]...)
* @see #zunionstore(String, ZParams, String...) * @see #zunionstore(byte[], ZParams, byte[]...)
* @see #zinterstore(String, String...) * @see #zinterstore(byte[], byte[]...)
* @see #zinterstore(String, ZParams, String...) * @see #zinterstore(byte[], ZParams, byte[]...)
* *
* @param dstkey * @param dstkey
* @param sets * @param sets
@@ -2786,10 +2754,10 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* input keys N, before passing the input keys and the other (optional) * input keys N, before passing the input keys and the other (optional)
* arguments. * arguments.
* <p> * <p>
* As the terms imply, the {@link #zinterstore(String, String...) * As the terms imply, the {@link #zinterstore(byte[], byte[]...)
* ZINTERSTORE} command requires an element to be present in each of the * ZINTERSTORE} command requires an element to be present in each of the
* given inputs to be inserted in the result. The * given inputs to be inserted in the result. The
* {@link #zunionstore(String, String...) ZUNIONSTORE} command inserts all * {@link #zunionstore(byte[], byte[]...) ZUNIONSTORE} command inserts all
* elements across all inputs. * elements across all inputs.
* <p> * <p>
* Using the WEIGHTS option, it is possible to add weight to each input * Using the WEIGHTS option, it is possible to add weight to each input
@@ -2808,10 +2776,10 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* sizes of the input sorted sets, and M being the number of elements in the * sizes of the input sorted sets, and M being the number of elements in the
* resulting sorted set * resulting sorted set
* *
* @see #zunionstore(String, String...) * @see #zunionstore(byte[], byte[]...)
* @see #zunionstore(String, ZParams, String...) * @see #zunionstore(byte[], ZParams, byte[]...)
* @see #zinterstore(String, String...) * @see #zinterstore(byte[], byte[]...)
* @see #zinterstore(String, ZParams, String...) * @see #zinterstore(byte[], ZParams, byte[]...)
* *
* @param dstkey * @param dstkey
* @param sets * @param sets
@@ -2959,7 +2927,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
*/ */
public String shutdown() { public String shutdown() {
client.shutdown(); client.shutdown();
String status = null; String status;
try { try {
status = client.getStatusCodeReply(); status = client.getStatusCodeReply();
} catch (JedisException ex) { } catch (JedisException ex) {
@@ -3125,7 +3093,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* configuration parameters are supported. * configuration parameters are supported.
* <p> * <p>
* The list of configuration parameters supported by CONFIG SET can be * The list of configuration parameters supported by CONFIG SET can be
* obtained issuing a {@link #configGet(String) CONFIG GET *} command. * obtained issuing a {@link #configGet(byte[]) CONFIG GET *} command.
* <p> * <p>
* The configuration set using CONFIG SET is immediately loaded by the Redis * The configuration set using CONFIG SET is immediately loaded by the Redis
* server that will start acting as specified starting from the next * server that will start acting as specified starting from the next
@@ -3229,9 +3197,11 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
public byte[] brpoplpush(byte[] source, byte[] destination, int timeout) { public byte[] brpoplpush(byte[] source, byte[] destination, int timeout) {
client.brpoplpush(source, destination, timeout); client.brpoplpush(source, destination, timeout);
client.setTimeoutInfinite(); client.setTimeoutInfinite();
byte[] reply = client.getBinaryBulkReply(); try {
client.rollbackTimeout(); return client.getBinaryBulkReply();
return reply; } finally {
client.rollbackTimeout();
}
} }
/** /**
@@ -3291,14 +3261,20 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
public void subscribe(BinaryJedisPubSub jedisPubSub, byte[]... channels) { public void subscribe(BinaryJedisPubSub jedisPubSub, byte[]... channels) {
client.setTimeoutInfinite(); client.setTimeoutInfinite();
jedisPubSub.proceed(client, channels); try {
client.rollbackTimeout(); jedisPubSub.proceed(client, channels);
} finally {
client.rollbackTimeout();
}
} }
public void psubscribe(BinaryJedisPubSub jedisPubSub, byte[]... patterns) { public void psubscribe(BinaryJedisPubSub jedisPubSub, byte[]... patterns) {
client.setTimeoutInfinite(); client.setTimeoutInfinite();
jedisPubSub.proceedWithPatterns(client, patterns); try {
client.rollbackTimeout(); jedisPubSub.proceedWithPatterns(client, patterns);
} finally {
client.rollbackTimeout();
}
} }
public Long getDB() { public Long getDB() {
@@ -3313,15 +3289,13 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
* @return Script result * @return Script result
*/ */
public Object eval(byte[] script, List<byte[]> keys, List<byte[]> args) { public Object eval(byte[] script, List<byte[]> keys, List<byte[]> args) {
client.setTimeoutInfinite(); return eval(script, toByteArray(keys.size()), getParams(keys, args));
client.eval(script, toByteArray(keys.size()), getParams(keys, args));
return client.getOne();
} }
private byte[][] getParams(List<byte[]> keys, List<byte[]> args) { private byte[][] getParams(List<byte[]> keys, List<byte[]> args) {
int keyCount = keys.size(); final int keyCount = keys.size();
int argCount = args.size(); final int argCount = args.size();
byte[][] params = new byte[keyCount + args.size()][]; byte[][] params = new byte[keyCount + argCount][];
for (int i = 0; i < keyCount; i++) for (int i = 0; i < keyCount; i++)
params[i] = keys.get(i); params[i] = keys.get(i);
@@ -3334,49 +3308,38 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
public Object eval(byte[] script, byte[] keyCount, byte[]... params) { public Object eval(byte[] script, byte[] keyCount, byte[]... params) {
client.setTimeoutInfinite(); client.setTimeoutInfinite();
client.eval(script, keyCount, params); try {
return client.getOne(); client.eval(script, keyCount, params);
return client.getOne();
} finally {
client.rollbackTimeout();
}
} }
public Object eval(byte[] script, int keyCount, byte[]... params) { public Object eval(byte[] script, int keyCount, byte[]... params) {
client.setTimeoutInfinite(); return eval(script, toByteArray(keyCount), params);
client.eval(script, SafeEncoder.encode(Integer.toString(keyCount)),
params);
return client.getOne();
} }
public Object eval(byte[] script) { public Object eval(byte[] script) {
client.setTimeoutInfinite(); return eval(script, 0);
client.eval(script, 0);
return client.getOne();
} }
public Object evalsha(byte[] sha1) { public Object evalsha(byte[] sha1) {
client.setTimeoutInfinite(); return evalsha(sha1, 1);
client.evalsha(sha1, 0);
return client.getOne();
} }
public Object evalsha(byte[] sha1, List<byte[]> keys, List<byte[]> args) { public Object evalsha(byte[] sha1, List<byte[]> keys, List<byte[]> args) {
return evalsha(sha1, keys.size(), getParams(keys, args));
int keyCount = keys == null ? 0 : keys.size();
int argCount = args == null ? 0 : args.size();
byte[][] params = new byte[keyCount + argCount][];
for (int i = 0; i < keyCount; i++)
params[i] = keys.get(i);
for (int i = 0; i < argCount; i++)
params[keyCount + i] = args.get(i);
return evalsha(sha1, keyCount, params);
} }
public Object evalsha(byte[] sha1, int keyCount, byte[]... params) { public Object evalsha(byte[] sha1, int keyCount, byte[]... params) {
client.setTimeoutInfinite(); client.setTimeoutInfinite();
client.evalsha(sha1, keyCount, params); try {
return client.getOne(); client.evalsha(sha1, keyCount, params);
return client.getOne();
} finally {
client.rollbackTimeout();
}
} }
public String scriptFlush() { public String scriptFlush() {

View File

@@ -218,8 +218,17 @@ public interface BinaryJedisCommands {
Long rpushx(byte[] key, byte[]... arg); Long rpushx(byte[] key, byte[]... arg);
/**
* @deprecated This command has no meaning.
*/
@Deprecated
List<byte[]> blpop(byte[] arg); List<byte[]> blpop(byte[] arg);
/**
* @deprecated This command has no meaning.
*/
@Deprecated
List<byte[]> brpop(byte[] arg); List<byte[]> brpop(byte[] arg);
Long del(byte[] key); Long del(byte[] key);

View File

@@ -42,7 +42,6 @@ public class Connection implements Closeable {
if (!isConnected()) { if (!isConnected()) {
connect(); connect();
} }
socket.setKeepAlive(true);
socket.setSoTimeout(0); socket.setSoTimeout(0);
} catch (SocketException ex) { } catch (SocketException ex) {
broken = true; broken = true;
@@ -53,7 +52,6 @@ public class Connection implements Closeable {
public void rollbackTimeout() { public void rollbackTimeout() {
try { try {
socket.setSoTimeout(timeout); socket.setSoTimeout(timeout);
socket.setKeepAlive(false);
} catch (SocketException ex) { } catch (SocketException ex) {
broken = true; broken = true;
throw new JedisConnectionException(ex); throw new JedisConnectionException(ex);

View File

@@ -1845,54 +1845,56 @@ public class Jedis extends BinaryJedis implements JedisCommands,
* programming language used. * programming language used.
*/ */
public List<String> blpop(final int timeout, final String... keys) { public List<String> blpop(final int timeout, final String... keys) {
checkIsInMulti(); return blpop(getArgsAddTimeout(timeout, keys));
List<String> args = new ArrayList<String>(); }
for (String arg : keys) {
args.add(arg);
}
args.add(String.valueOf(timeout));
client.blpop(args.toArray(new String[args.size()])); private String[] getArgsAddTimeout (int timeout, String[] keys) {
client.setTimeoutInfinite(); final int keyCount = keys.length;
final List<String> multiBulkReply = client.getMultiBulkReply(); final String[] args = new String[keyCount + 1];
client.rollbackTimeout(); for (int at = 0; at != keyCount; ++at) {
return multiBulkReply; args[at] = keys[at];
}
args[keyCount] = String.valueOf(timeout);
return args;
} }
public List<String> blpop(String... args) { public List<String> blpop(String... args) {
checkIsInMulti();
client.blpop(args); client.blpop(args);
client.setTimeoutInfinite(); client.setTimeoutInfinite();
final List<String> multiBulkReply = client.getMultiBulkReply(); try {
client.rollbackTimeout(); return client.getMultiBulkReply();
return multiBulkReply; } finally {
client.rollbackTimeout();
}
} }
public List<String> brpop(String... args) { public List<String> brpop(String... args) {
checkIsInMulti();
client.brpop(args); client.brpop(args);
client.setTimeoutInfinite(); client.setTimeoutInfinite();
final List<String> multiBulkReply = client.getMultiBulkReply(); try {
client.rollbackTimeout(); return client.getMultiBulkReply();
return multiBulkReply; } finally {
client.rollbackTimeout();
}
} }
/**
* @deprecated unusable command
*/
@Deprecated
public List<String> blpop(String arg) { public List<String> blpop(String arg) {
String[] args = new String[1]; return blpop(new String[]{arg});
args[0] = arg;
client.blpop(args);
client.setTimeoutInfinite();
final List<String> multiBulkReply = client.getMultiBulkReply();
client.rollbackTimeout();
return multiBulkReply;
} }
/**
* @deprecated unusable command
*/
@Deprecated
public List<String> brpop(String arg) { public List<String> brpop(String arg) {
String[] args = new String[1]; return brpop(new String[]{arg});
args[0] = arg;
client.brpop(args);
client.setTimeoutInfinite();
final List<String> multiBulkReply = client.getMultiBulkReply();
client.rollbackTimeout();
return multiBulkReply;
} }
/** /**
@@ -2010,19 +2012,7 @@ public class Jedis extends BinaryJedis implements JedisCommands,
* programming language used. * programming language used.
*/ */
public List<String> brpop(final int timeout, final String... keys) { public List<String> brpop(final int timeout, final String... keys) {
checkIsInMulti(); return brpop(getArgsAddTimeout(timeout, keys));
List<String> args = new ArrayList<String>();
for (String arg : keys) {
args.add(arg);
}
args.add(String.valueOf(timeout));
client.brpop(args.toArray(new String[args.size()]));
client.setTimeoutInfinite();
List<String> multiBulkReply = client.getMultiBulkReply();
client.rollbackTimeout();
return multiBulkReply;
} }
public Long zcount(final String key, final double min, final double max) { public Long zcount(final String key, final double min, final double max) {
@@ -2724,9 +2714,11 @@ public class Jedis extends BinaryJedis implements JedisCommands,
public String brpoplpush(String source, String destination, int timeout) { public String brpoplpush(String source, String destination, int timeout) {
client.brpoplpush(source, destination, timeout); client.brpoplpush(source, destination, timeout);
client.setTimeoutInfinite(); client.setTimeoutInfinite();
String reply = client.getBulkReply(); try {
client.rollbackTimeout(); return client.getBulkReply();
return reply; } finally {
client.rollbackTimeout();
}
} }
/** /**
@@ -2861,16 +2853,22 @@ public class Jedis extends BinaryJedis implements JedisCommands,
public Object eval(String script, int keyCount, String... params) { public Object eval(String script, int keyCount, String... params) {
client.setTimeoutInfinite(); client.setTimeoutInfinite();
client.eval(script, keyCount, params); try {
client.eval(script, keyCount, params);
return getEvalResult(); return getEvalResult();
} finally {
client.rollbackTimeout();
}
} }
public void subscribe(final JedisPubSub jedisPubSub, public void subscribe(final JedisPubSub jedisPubSub,
final String... channels) { final String... channels) {
client.setTimeoutInfinite(); client.setTimeoutInfinite();
jedisPubSub.proceed(client, channels); try {
client.rollbackTimeout(); jedisPubSub.proceed(client, channels);
} finally {
client.rollbackTimeout();
}
} }
public Long publish(final String channel, final String message) { public Long publish(final String channel, final String message) {
@@ -2883,10 +2881,12 @@ public class Jedis extends BinaryJedis implements JedisCommands,
public void psubscribe(final JedisPubSub jedisPubSub, public void psubscribe(final JedisPubSub jedisPubSub,
final String... patterns) { final String... patterns) {
checkIsInMulti(); checkIsInMulti();
connect();
client.setTimeoutInfinite(); client.setTimeoutInfinite();
jedisPubSub.proceedWithPatterns(client, patterns); try {
client.rollbackTimeout(); jedisPubSub.proceedWithPatterns(client, patterns);
} finally {
client.rollbackTimeout();
}
} }
protected static String[] getParams(List<String> keys, List<String> args) { protected static String[] getParams(List<String> keys, List<String> args) {
@@ -2944,7 +2944,6 @@ public class Jedis extends BinaryJedis implements JedisCommands,
public Object evalsha(String sha1, int keyCount, String... params) { public Object evalsha(String sha1, int keyCount, String... params) {
checkIsInMulti(); checkIsInMulti();
client.evalsha(sha1, keyCount, params); client.evalsha(sha1, keyCount, params);
return getEvalResult(); return getEvalResult();
} }
@@ -3503,28 +3502,12 @@ public class Jedis extends BinaryJedis implements JedisCommands,
@Override @Override
public List<String> blpop(int timeout, String key) { public List<String> blpop(int timeout, String key) {
checkIsInMulti(); return blpop(key, String.valueOf(timeout));
List<String> args = new ArrayList<String>();
args.add(key);
args.add(String.valueOf(timeout));
client.blpop(args.toArray(new String[args.size()]));
client.setTimeoutInfinite();
final List<String> multiBulkReply = client.getMultiBulkReply();
client.rollbackTimeout();
return multiBulkReply;
} }
@Override @Override
public List<String> brpop(int timeout, String key) { public List<String> brpop(int timeout, String key) {
checkIsInMulti(); return brpop(key, String.valueOf(timeout));
List<String> args = new ArrayList<String>();
args.add(key);
args.add(String.valueOf(timeout));
client.brpop(args.toArray(new String[args.size()]));
client.setTimeoutInfinite();
final List<String> multiBulkReply = client.getMultiBulkReply();
client.rollbackTimeout();
return multiBulkReply;
} }
} }

View File

@@ -216,10 +216,18 @@ public interface JedisCommands {
Long rpushx(String key, String... string); Long rpushx(String key, String... string);
/**
* @deprecated unusable command
*/
@Deprecated
List<String> blpop(String arg); List<String> blpop(String arg);
List<String> blpop(int timeout, String key); List<String> blpop(int timeout, String key);
/**
* @deprecated unusable command
*/
@Deprecated
List<String> brpop(String arg); List<String> brpop(String arg);
List<String> brpop(int timeout, String key); List<String> brpop(int timeout, String key);