Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import static redis.clients.jedis.Protocol.toByteArray;
import static redis.clients.jedis.Protocol.Command.*;
import static redis.clients.jedis.Protocol.Keyword.ENCODING;
import static redis.clients.jedis.Protocol.Keyword.IDLETIME;
import static redis.clients.jedis.Protocol.Keyword.LEN;
import static redis.clients.jedis.Protocol.Keyword.LIMIT;
import static redis.clients.jedis.Protocol.Keyword.NO;
import static redis.clients.jedis.Protocol.Keyword.ONE;
import static redis.clients.jedis.Protocol.Keyword.REFCOUNT;
import static redis.clients.jedis.Protocol.Keyword.RESET;
import static redis.clients.jedis.Protocol.Keyword.STORE;
import static redis.clients.jedis.Protocol.Keyword.WITHSCORES;
import static redis.clients.jedis.Protocol.Keyword.FREQ;
import static redis.clients.jedis.Protocol.Keyword.HELP;
import static redis.clients.jedis.Protocol.Command.EXISTS;
import static redis.clients.jedis.Protocol.Command.GET;
import static redis.clients.jedis.Protocol.Command.INCR;
import static redis.clients.jedis.Protocol.Command.KEYS;
import static redis.clients.jedis.Protocol.Command.PING;
import static redis.clients.jedis.Protocol.Command.PSUBSCRIBE;
import static redis.clients.jedis.Protocol.Command.PUNSUBSCRIBE;
import static redis.clients.jedis.Protocol.Command.SAVE;
import static redis.clients.jedis.Protocol.Command.SET;
import static redis.clients.jedis.Protocol.Command.SUBSCRIBE;
import static redis.clients.jedis.Protocol.Command.TIME;
import static redis.clients.jedis.Protocol.Command.UNSUBSCRIBE;
import static redis.clients.jedis.Protocol.Keyword.*;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -26,17 +27,7 @@
import javax.net.ssl.SSLSocketFactory;

import redis.clients.jedis.Protocol.Keyword;
import redis.clients.jedis.params.ClientKillParams;
import redis.clients.jedis.params.GeoAddParams;
import redis.clients.jedis.params.GeoRadiusParam;
import redis.clients.jedis.params.GeoRadiusStoreParam;
import redis.clients.jedis.params.GetExParams;
import redis.clients.jedis.params.MigrateParams;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.params.XClaimParams;
import redis.clients.jedis.params.ZAddParams;
import redis.clients.jedis.params.ZIncrByParams;
import redis.clients.jedis.params.LPosParams;
import redis.clients.jedis.params.*;
import redis.clients.jedis.util.SafeEncoder;

public class BinaryClient extends Connection {
Expand Down Expand Up @@ -415,6 +406,18 @@ public void hgetAll(final byte[] key) {
sendCommand(HGETALL, key);
}

public void hrandfield(final byte[] key) {
sendCommand(HRANDFIELD, key);
}

public void hrandfield(final byte[] key, final long count) {
sendCommand(HRANDFIELD, key, toByteArray(count));
}

public void hrandfieldWithValues(final byte[] key, final long count) {
sendCommand(HRANDFIELD, key, toByteArray(count), WITHVALUES.getRaw());
}

public void rpush(final byte[] key, final byte[]... strings) {
sendCommand(RPUSH, joinParameters(key, strings));
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,48 @@ public Map<byte[], byte[]> hgetAll(final byte[] key) {
return hash;
}

/**
* Get one random field from a hash.
* <p>
* <b>Time complexity:</b> O(N), where N is the number of fields returned
* @param key
* @return one random field from a hash.
*/
@Override
public byte[] hrandfield(final byte[] key) {
checkIsInMultiOrPipeline();
client.hrandfield(key);
return client.getBinaryBulkReply();
}

/**
* Get multiple random fields from a hash.
* <p>
* <b>Time complexity:</b> O(N), where N is the number of fields returned
* @param key
* @return multiple random fields from a hash.
*/
@Override
public List<byte[]> hrandfield(final byte[] key, final long count) {
checkIsInMultiOrPipeline();
client.hrandfield(key, count);
return client.getBinaryMultiBulkReply();
}

/**
* Get one or multiple random fields with values from a hash.
* <p>
* <b>Time complexity:</b> O(N), where N is the number of fields returned
* @param key
* @return one or multiple random fields with values from a hash.
*/
@Override
public Map<byte[], byte[]> hrandfieldWithValues(final byte[] key, final long count) {
checkIsInMultiOrPipeline();
client.hrandfieldWithValues(key, count);
return BuilderFactory.BYTE_ARRAY_MAP.build(client.getBinaryMultiBulkReply());
}

/**
* Add the string value to the head (LPUSH) or tail (RPUSH) of the list stored at key. If the key
* does not exist an empty list is created just before the append operation. If the key exists but
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,36 @@ public Map<byte[], byte[]> execute(Jedis connection) {
}.runBinary(key);
}

@Override
public byte[] hrandfield(final byte[] key) {
return new JedisClusterCommand<byte[]>(connectionHandler, maxAttempts) {
@Override
public byte[] execute(Jedis connection) {
return connection.hrandfield(key);
}
}.runBinary(key);
}

@Override
public List<byte[]> hrandfield(final byte[] key, final long count) {
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
@Override
public List<byte[]> execute(Jedis connection) {
return connection.hrandfield(key, count);
}
}.runBinary(key);
}

@Override
public Map<byte[], byte[]> hrandfieldWithValues(final byte[] key, final long count) {
return new JedisClusterCommand<Map<byte[], byte[]>>(connectionHandler, maxAttempts) {
@Override
public Map<byte[], byte[]> execute(Jedis connection) {
return connection.hrandfieldWithValues(key, count);
}
}.runBinary(key);
}

@Override
public Long rpush(final byte[] key, final byte[]... args) {
return new JedisClusterCommand<Long>(connectionHandler, maxAttempts) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,24 @@ public Map<byte[], byte[]> hgetAll(final byte[] key) {
return j.hgetAll(key);
}

@Override
public byte[] hrandfield(final byte[] key) {
Jedis j = getShard(key);
return j.hrandfield(key);
}

@Override
public List<byte[]> hrandfield(final byte[] key, final long count) {
Jedis j = getShard(key);
return j.hrandfield(key, count);
}

@Override
public Map<byte[], byte[]> hrandfieldWithValues(final byte[] key, final long count) {
Jedis j = getShard(key);
return j.hrandfieldWithValues(key, count);
}

@Override
public Long rpush(final byte[] key, final byte[]... strings) {
Jedis j = getShard(key);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/redis/clients/jedis/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,21 @@ public void hgetAll(final String key) {
hgetAll(SafeEncoder.encode(key));
}

@Override
public void hrandfield(final String key) {
hrandfield(SafeEncoder.encode(key));
}

@Override
public void hrandfield(final String key, final long count) {
hrandfield(SafeEncoder.encode(key), count);
}

@Override
public void hrandfieldWithValues(final String key, final long count) {
hrandfieldWithValues(SafeEncoder.encode(key), count);
}

@Override
public void rpush(final String key, final String... string) {
rpush(SafeEncoder.encode(key), SafeEncoder.encodeMany(string));
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,48 @@ public Map<String, String> hgetAll(final String key) {
return BuilderFactory.STRING_MAP.build(client.getBinaryMultiBulkReply());
}

/**
* Get one random field from a hash.
* <p>
* <b>Time complexity:</b> O(N), where N is the number of fields returned
* @param key
* @return one random field from a hash.
*/
@Override
public String hrandfield(final String key) {
checkIsInMultiOrPipeline();
client.hrandfield(key);
return client.getStatusCodeReply();
}

/**
* Get multiple random fields from a hash.
* <p>
* <b>Time complexity:</b> O(N), where N is the number of fields returned
* @param key
* @return multiple random fields from a hash.
*/
@Override
public List<String> hrandfield(final String key, final long count) {
checkIsInMultiOrPipeline();
client.hrandfield(key, count);
return client.getMultiBulkReply();
}

/**
* Get one or multiple random fields with values from a hash.
* <p>
* <b>Time complexity:</b> O(N), where N is the number of fields returned
* @param key
* @return one or multiple random fields with values from a hash.
*/
@Override
public Map<String, String> hrandfieldWithValues(final String key, final long count) {
checkIsInMultiOrPipeline();
client.hrandfieldWithValues(key, count);
return BuilderFactory.STRING_MAP.build(client.getBinaryMultiBulkReply());
}

/**
* Add the string value to the head (LPUSH) or tail (RPUSH) of the list stored at key. If the key
* does not exist an empty list is created just before the append operation. If the key exists but
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/redis/clients/jedis/JedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,36 @@ public Map<String, String> execute(Jedis connection) {
}.run(key);
}

@Override
public String hrandfield(final String key) {
return new JedisClusterCommand<String>(connectionHandler, maxAttempts) {
@Override
public String execute(Jedis connection) {
return connection.hrandfield(key);
}
}.run(key);
}

@Override
public List<String> hrandfield(final String key, final long count) {
return new JedisClusterCommand<List<String>>(connectionHandler, maxAttempts) {
@Override
public List<String> execute(Jedis connection) {
return connection.hrandfield(key, count);
}
}.run(key);
}

@Override
public Map<String, String> hrandfieldWithValues(final String key, final long count) {
return new JedisClusterCommand<Map<String, String>>(connectionHandler, maxAttempts) {
@Override
public Map<String, String> execute(Jedis connection) {
return connection.hrandfieldWithValues(key, count);
}
}.run(key);
}

@Override
public Long rpush(final String key, final String... string) {
return new JedisClusterCommand<Long>(connectionHandler, maxAttempts) {
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/redis/clients/jedis/PipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,42 @@ public Response<List<byte[]>> hvals(final byte[] key) {
return getResponse(BuilderFactory.BYTE_ARRAY_LIST);
}

@Override
public Response<byte[]> hrandfield(final byte[] key) {
getClient(key).hrandfield(key);
return getResponse(BuilderFactory.BYTE_ARRAY);
}

@Override
public Response<List<byte[]>> hrandfield(final byte[] key, final long count) {
getClient(key).hrandfield(key, count);
return getResponse(BuilderFactory.BYTE_ARRAY_LIST);
}

@Override
public Response<Map<byte[], byte[]>> hrandfieldWithValues(final byte[] key, final long count) {
getClient(key).hrandfieldWithValues(key, count);
return getResponse(BuilderFactory.BYTE_ARRAY_MAP);
}

@Override
public Response<String> hrandfield(final String key) {
getClient(key).hrandfield(key);
return getResponse(BuilderFactory.STRING);
}

@Override
public Response<List<String>> hrandfield(final String key, final long count) {
getClient(key).hrandfield(key, count);
return getResponse(BuilderFactory.STRING_LIST);
}

@Override
public Response<Map<String, String>> hrandfieldWithValues(final String key, final long count) {
getClient(key).hrandfieldWithValues(key, count);
return getResponse(BuilderFactory.STRING_MAP);
}

@Override
public Response<Long> incr(final String key) {
getClient(key).incr(key);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public static enum Command implements ProtocolCommand {
PING, SET, GET, GETDEL, GETEX, QUIT, EXISTS, DEL, UNLINK, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME,
RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX,
SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET,
HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM,
HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, HRANDFIELD, RPUSH, LPUSH, LLEN, LRANGE, LTRIM,
LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER,
SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM,
ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, ZPOPMAX, ZPOPMIN, MULTI, DISCARD, EXEC,
Expand Down Expand Up @@ -282,7 +282,7 @@ public static enum Keyword {
GETNAME, SETNAME, LIST, MATCH, COUNT, PING, PONG, UNLOAD, REPLACE, KEYS, PAUSE, DOCTOR, BLOCK,
NOACK, STREAMS, KEY, CREATE, MKSTREAM, SETID, DESTROY, DELCONSUMER, MAXLEN, GROUP, ID, IDLE,
TIME, RETRYCOUNT, FORCE, USAGE, SAMPLES, STREAM, GROUPS, CONSUMERS, HELP, FREQ, SETUSER,
GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS, LOG, INCR, SAVE, JUSTID;
GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS, LOG, INCR, SAVE, JUSTID, WITHVALUES;

/**
* @deprecated This will be private in future. Use {@link #getRaw()}.
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/redis/clients/jedis/ShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,24 @@ public Map<String, String> hgetAll(final String key) {
return j.hgetAll(key);
}

@Override
public String hrandfield(final String key) {
Jedis j = getShard(key);
return j.hrandfield(key);
}

@Override
public List<String> hrandfield(final String key, final long count) {
Jedis j = getShard(key);
return j.hrandfield(key, count);
}

@Override
public Map<String, String> hrandfieldWithValues(final String key, final long count) {
Jedis j = getShard(key);
return j.hrandfieldWithValues(key, count);
}

@Override
public Long rpush(final String key, String... strings) {
Jedis j = getShard(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ default String setex(byte[] key, int seconds, byte[] value) {

Map<byte[], byte[]> hgetAll(byte[] key);

byte[] hrandfield(byte[] key);

List<byte[]> hrandfield(byte[] key, long count);

Map<byte[], byte[]> hrandfieldWithValues(byte[] key, long count);

Long rpush(byte[] key, byte[]... args);

Long lpush(byte[] key, byte[]... args);
Expand Down
Loading