Skip to content

Commit

Permalink
Add/modify Keyed... response objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Mar 23, 2021
1 parent d4a798f commit 3ad03e6
Show file tree
Hide file tree
Showing 20 changed files with 236 additions and 173 deletions.
13 changes: 6 additions & 7 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 @@ -2660,24 +2659,24 @@ private byte[][] getKeysAndTimeout(double timeout, byte[][] keys) {
}

@Override
public KeyedTuple bzpopmax(final double 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 double 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
16 changes: 8 additions & 8 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 @@ -1826,20 +1826,20 @@ public List<byte[]> execute(Jedis connection) {
}

@Override
public KeyedTuple bzpopmax(double timeout, byte[]... keys) {
return new JedisClusterCommand<KeyedTuple>(connectionHandler, maxAttempts) {
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(double 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
52 changes: 28 additions & 24 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -2185,8 +2185,15 @@ public List<String> blpop(final int timeout, final String... keys) {
}

@Override
public List<String> 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();
}
}

/**
Expand Down Expand Up @@ -2257,8 +2264,15 @@ public List<String> brpop(final int timeout, final String... keys) {
}

@Override
public List<String> 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) {
Expand All @@ -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<String> blpop(final String... args) {
checkIsInMultiOrPipeline();
Expand All @@ -2306,24 +2310,24 @@ public List<String> 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();
}
Expand All @@ -2335,8 +2339,8 @@ public List<String> blpop(final int timeout, final String key) {
}

@Override
public List<String> 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
Expand All @@ -2345,8 +2349,8 @@ public List<String> brpop(final int timeout, final String key) {
}

@Override
public List<String> 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
Expand Down
39 changes: 20 additions & 19 deletions src/main/java/redis/clients/jedis/JedisCluster.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -1843,10 +1844,10 @@ public List<String> execute(Jedis connection) {
}

@Override
public List<String> blpop(final double timeout, final String... keys) {
return new JedisClusterCommand<List<String>>(connectionHandler, maxAttempts) {
public KeyedListElement blpop(final double timeout, final String... keys) {
return new JedisClusterCommand<KeyedListElement>(connectionHandler, maxAttempts) {
@Override
public List<String> execute(Jedis connection) {
public KeyedListElement execute(Jedis connection) {
return connection.blpop(timeout, keys);
}
}.run(keys.length, keys);
Expand All @@ -1864,30 +1865,30 @@ public List<String> execute(Jedis connection) {
}

@Override
public List<String> brpop(final double timeout, final String... keys) {
return new JedisClusterCommand<List<String>>(connectionHandler, maxAttempts) {
public KeyedListElement brpop(final double timeout, final String... keys) {
return new JedisClusterCommand<KeyedListElement>(connectionHandler, maxAttempts) {
@Override
public List<String> 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<KeyedTuple>(connectionHandler, maxAttempts) {
public KeyedZSetElement bzpopmax(double timeout, String... keys) {
return new JedisClusterCommand<KeyedZSetElement>(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<KeyedTuple>(connectionHandler, maxAttempts) {
public KeyedZSetElement bzpopmin(double timeout, String... keys) {
return new JedisClusterCommand<KeyedZSetElement>(connectionHandler, maxAttempts) {
@Override
public KeyedTuple execute(Jedis connection) {
public KeyedZSetElement execute(Jedis connection) {
return connection.bzpopmin(timeout, keys);
}
}.run(keys.length, keys);
Expand All @@ -1904,10 +1905,10 @@ public List<String> execute(Jedis connection) {
}

@Override
public List<String> blpop(double timeout, String key) {
return new JedisClusterCommand<List<String>>(connectionHandler, maxAttempts) {
public KeyedListElement blpop(double timeout, String key) {
return new JedisClusterCommand<KeyedListElement>(connectionHandler, maxAttempts) {
@Override
public List<String> execute(Jedis connection) {
public KeyedListElement execute(Jedis connection) {
return connection.blpop(timeout, key);
}
}.run(key);
Expand All @@ -1924,10 +1925,10 @@ public List<String> execute(Jedis connection) {
}

@Override
public List<String> brpop(double timeout, String key) {
return new JedisClusterCommand<List<String>>(connectionHandler, maxAttempts) {
public KeyedListElement brpop(double timeout, String key) {
return new JedisClusterCommand<KeyedListElement>(connectionHandler, maxAttempts) {
@Override
public List<String> execute(Jedis connection) {
public KeyedListElement execute(Jedis connection) {
return connection.brpop(timeout, key);
}
}.run(key);
Expand Down
Loading

0 comments on commit 3ad03e6

Please sign in to comment.