Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double timeout, avoid Tuple and more #2481

Merged
merged 11 commits into from
Mar 30, 2021
32 changes: 24 additions & 8 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ public void lmove(byte[] srcKey, byte[] dstKey, ListDirection from, ListDirectio
sendCommand(LMOVE, srcKey, dstKey, from.getRaw(), to.getRaw());
}

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

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

public void blpop(final int timeout, final byte[]... keys) {
blpop(keysAndTimeout(timeout, keys));
blpop(getKeysAndTimeout(timeout, keys));
}

public void blpop(final double timeout, final byte[]... keys) {
blpop(getKeysAndTimeout(timeout, keys));
}

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

public void brpop(final int timeout, final byte[]... keys) {
brpop(keysAndTimeout(timeout, keys));
brpop(getKeysAndTimeout(timeout, keys));
}

public void brpop(final double timeout, final byte[]... keys) {
brpop(getKeysAndTimeout(timeout, keys));
}

public void bzpopmax(final double timeout, final byte[]... keys) {
sendCommand(BZPOPMAX, getKeysAndTimeout(timeout, keys));
}

public void bzpopmax(final int timeout, final byte[]... keys) {
sendCommand(BZPOPMAX, keysAndTimeout(timeout, keys));
public void bzpopmin(final double timeout, final byte[]... keys) {
sendCommand(BZPOPMIN, getKeysAndTimeout(timeout, keys));
}

public void bzpopmin(final int timeout, final byte[]... keys) {
sendCommand(BZPOPMIN, keysAndTimeout(timeout, keys));
private static byte[][] getKeysAndTimeout(final int timeout, final byte[]... keys) {
int numKeys = keys.length;
byte[][] args = new byte[numKeys + 1][];
System.arraycopy(keys, 0, args, 0, numKeys);
args[numKeys] = toByteArray(timeout);
return args;
}

private static byte[][] keysAndTimeout(final int timeout, final byte[]... keys) {
private static byte[][] getKeysAndTimeout(final double timeout, final byte[]... keys) {
int numKeys = keys.length;
byte[][] args = new byte[numKeys + 1][];
System.arraycopy(keys, 0, args, 0, numKeys);
Expand Down
33 changes: 25 additions & 8 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -2475,7 +2474,7 @@ public byte[] lmove(byte[] srcKey, byte[] dstKey, ListDirection from, ListDirect
* @return
*/
@Override
public byte[] blmove(byte[] srcKey, byte[] dstKey, ListDirection from, ListDirection to, int timeout) {
public byte[] blmove(byte[] srcKey, byte[] dstKey, ListDirection from, ListDirection to, double timeout) {
checkIsInMultiOrPipeline();
client.blmove(srcKey, dstKey, from, to, timeout);
client.setTimeoutInfinite();
Expand Down Expand Up @@ -2553,6 +2552,11 @@ public List<byte[]> blpop(final int timeout, final byte[]... keys) {
return blpop(getKeysAndTimeout(timeout, keys));
}

@Override
public List<byte[]> blpop(final double timeout, final byte[]... keys) {
return blpop(getKeysAndTimeout(timeout, keys));
}

/**
* BLPOP (and BRPOP) is a blocking list pop primitive. You can see this commands as blocking
* versions of LPOP and RPOP able to block if the specified keys don't exist or contain empty
Expand Down Expand Up @@ -2620,6 +2624,11 @@ public List<byte[]> brpop(final int timeout, final byte[]... keys) {
return brpop(getKeysAndTimeout(timeout, keys));
}

@Override
public List<byte[]> brpop(final double timeout, final byte[]... keys) {
return brpop(getKeysAndTimeout(timeout, keys));
}

@Override
public List<byte[]> blpop(final byte[]... args) {
checkIsInMultiOrPipeline();
Expand Down Expand Up @@ -2652,25 +2661,33 @@ private byte[][] getKeysAndTimeout(int timeout, byte[][] keys) {
return args;
}

private byte[][] getKeysAndTimeout(double timeout, byte[][] keys) {
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
int size = keys.length;
final byte[][] args = new byte[size + 1][];
System.arraycopy(keys, 0, args, 0, size);
args[size] = Protocol.toByteArray(timeout);
return args;
}

@Override
public KeyedTuple bzpopmax(final int timeout, final byte[]... keys) {
public List<byte[]> 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 int timeout, final byte[]... keys) {
public List<byte[]> 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();
}
Expand Down
38 changes: 29 additions & 9 deletions src/main/java/redis/clients/jedis/BinaryJedisCluster.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -1787,7 +1787,7 @@ public byte[] execute(Jedis connection) {

@Override
public byte[] blmove(final byte[] srcKey, final byte[] dstKey, final ListDirection from,
final ListDirection to, final int timeout) {
final ListDirection to, final double timeout) {
return new JedisClusterCommand<byte[]>(connectionHandler, maxAttempts) {
@Override
public byte[] execute(Jedis connection) {
Expand All @@ -1806,6 +1806,16 @@ public List<byte[]> execute(Jedis connection) {
}.runBinary(keys.length, keys);
}

@Override
public List<byte[]> blpop(final double timeout, final byte[]... keys) {
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
@Override
public List<byte[]> execute(Jedis connection) {
return connection.blpop(timeout, keys);
}
}.runBinary(keys.length, keys);
}

@Override
public List<byte[]> brpop(final int timeout, final byte[]... keys) {
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
Expand All @@ -1817,20 +1827,30 @@ public List<byte[]> execute(Jedis connection) {
}

@Override
public KeyedTuple bzpopmax(int timeout, byte[]... keys) {
return new JedisClusterCommand<KeyedTuple>(connectionHandler, maxAttempts) {
public List<byte[]> brpop(final double timeout, final byte[]... keys) {
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
@Override
public List<byte[]> execute(Jedis connection) {
return connection.brpop(timeout, keys);
}
}.runBinary(keys.length, keys);
}

@Override
public List<byte[]> bzpopmax(double timeout, byte[]... keys) {
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
@Override
public KeyedTuple execute(Jedis connection) {
public List<byte[]> execute(Jedis connection) {
return connection.bzpopmax(timeout, keys);
}
}.runBinary(keys.length, keys);
}

@Override
public KeyedTuple bzpopmin(int timeout, byte[]... keys) {
return new JedisClusterCommand<KeyedTuple>(connectionHandler, maxAttempts) {
public List<byte[]> bzpopmin(double timeout, byte[]... keys) {
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
@Override
public KeyedTuple execute(Jedis connection) {
public List<byte[]> execute(Jedis connection) {
return connection.bzpopmin(timeout, keys);
}
}.runBinary(keys.length, keys);
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -356,6 +357,21 @@ public String toString() {

};

public static final Builder<KeyedListElement> KEYED_LIST_ELEMENT = new Builder<KeyedListElement>() {
@Override
@SuppressWarnings("unchecked")
public KeyedListElement build(Object data) {
if (data == null) return null;
List<byte[]> l = (List<byte[]>) data;
return new KeyedListElement(l.get(0), l.get(1));
}

@Override
public String toString() {
return "KeyedListElement";
}
};

public static final Builder<Tuple> TUPLE = new Builder<Tuple>() {
@Override
@SuppressWarnings("unchecked")
Expand All @@ -374,22 +390,21 @@ public String toString() {

};

public static final Builder<KeyedTuple> KEYED_TUPLE = new Builder<KeyedTuple>() {
public static final Builder<KeyedZSetElement> KEYED_ZSET_ELEMENT = new Builder<KeyedZSetElement>() {
@Override
@SuppressWarnings("unchecked")
public KeyedTuple build(Object data) {
public KeyedZSetElement build(Object data) {
List<byte[]> l = (List<byte[]>) 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<Set<Tuple>> TUPLE_ZSET = new Builder<Set<Tuple>>() {
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/redis/clients/jedis/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

Expand Down Expand Up @@ -637,7 +634,8 @@ public void lmove(String srcKey, String dstKey, ListDirection from, ListDirectio
}

@Override
public void blmove(String srcKey, String dstKey, ListDirection from, ListDirection to, int timeout) {
public void blmove(String srcKey, String dstKey, ListDirection from, ListDirection to,
double timeout) {
blmove(SafeEncoder.encode(srcKey), SafeEncoder.encode(dstKey), from, to, timeout);
}

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

@Override
public void blpop(final double timeout, final String... keys) {
blpop(timeout, SafeEncoder.encodeMany(keys));
}

@Override
public void brpop(final String[] args) {
brpop(SafeEncoder.encodeMany(args));
Expand All @@ -662,12 +665,17 @@ public void brpop(final int timeout, final String... keys) {
}

@Override
public void bzpopmax(final int timeout, final String... keys) {
public void brpop(final double timeout, final String... keys) {
brpop(timeout, SafeEncoder.encodeMany(keys));
}

@Override
public void bzpopmax(final double timeout, final String... keys) {
bzpopmax(timeout, SafeEncoder.encodeMany(keys));
}

@Override
public void bzpopmin(final int timeout, final String... keys) {
public void bzpopmin(final double timeout, final String... keys) {
bzpopmin(timeout, SafeEncoder.encodeMany(keys));
}

Expand Down
Loading