Skip to content

Commit a7d2147

Browse files
denglimingsazzad16
andauthored
Add support for HRANDFIELD command (#2442)
* Add support for HRANDFIELD command * Fix review Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com>
1 parent 3a3b89a commit a7d2147

18 files changed

+338
-26
lines changed

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
import static redis.clients.jedis.Protocol.toByteArray;
44
import static redis.clients.jedis.Protocol.Command.*;
5-
import static redis.clients.jedis.Protocol.Keyword.ENCODING;
6-
import static redis.clients.jedis.Protocol.Keyword.IDLETIME;
7-
import static redis.clients.jedis.Protocol.Keyword.LEN;
8-
import static redis.clients.jedis.Protocol.Keyword.LIMIT;
9-
import static redis.clients.jedis.Protocol.Keyword.NO;
10-
import static redis.clients.jedis.Protocol.Keyword.ONE;
11-
import static redis.clients.jedis.Protocol.Keyword.REFCOUNT;
12-
import static redis.clients.jedis.Protocol.Keyword.RESET;
13-
import static redis.clients.jedis.Protocol.Keyword.STORE;
14-
import static redis.clients.jedis.Protocol.Keyword.WITHSCORES;
15-
import static redis.clients.jedis.Protocol.Keyword.FREQ;
16-
import static redis.clients.jedis.Protocol.Keyword.HELP;
5+
import static redis.clients.jedis.Protocol.Command.EXISTS;
6+
import static redis.clients.jedis.Protocol.Command.GET;
7+
import static redis.clients.jedis.Protocol.Command.INCR;
8+
import static redis.clients.jedis.Protocol.Command.KEYS;
9+
import static redis.clients.jedis.Protocol.Command.PING;
10+
import static redis.clients.jedis.Protocol.Command.PSUBSCRIBE;
11+
import static redis.clients.jedis.Protocol.Command.PUNSUBSCRIBE;
12+
import static redis.clients.jedis.Protocol.Command.SAVE;
13+
import static redis.clients.jedis.Protocol.Command.SET;
14+
import static redis.clients.jedis.Protocol.Command.SUBSCRIBE;
15+
import static redis.clients.jedis.Protocol.Command.TIME;
16+
import static redis.clients.jedis.Protocol.Command.UNSUBSCRIBE;
17+
import static redis.clients.jedis.Protocol.Keyword.*;
1718

1819
import java.util.ArrayList;
1920
import java.util.Collections;
@@ -26,17 +27,7 @@
2627
import javax.net.ssl.SSLSocketFactory;
2728

2829
import redis.clients.jedis.Protocol.Keyword;
29-
import redis.clients.jedis.params.ClientKillParams;
30-
import redis.clients.jedis.params.GeoAddParams;
31-
import redis.clients.jedis.params.GeoRadiusParam;
32-
import redis.clients.jedis.params.GeoRadiusStoreParam;
33-
import redis.clients.jedis.params.GetExParams;
34-
import redis.clients.jedis.params.MigrateParams;
35-
import redis.clients.jedis.params.SetParams;
36-
import redis.clients.jedis.params.XClaimParams;
37-
import redis.clients.jedis.params.ZAddParams;
38-
import redis.clients.jedis.params.ZIncrByParams;
39-
import redis.clients.jedis.params.LPosParams;
30+
import redis.clients.jedis.params.*;
4031
import redis.clients.jedis.util.SafeEncoder;
4132

4233
public class BinaryClient extends Connection {
@@ -415,6 +406,18 @@ public void hgetAll(final byte[] key) {
415406
sendCommand(HGETALL, key);
416407
}
417408

409+
public void hrandfield(final byte[] key) {
410+
sendCommand(HRANDFIELD, key);
411+
}
412+
413+
public void hrandfield(final byte[] key, final long count) {
414+
sendCommand(HRANDFIELD, key, toByteArray(count));
415+
}
416+
417+
public void hrandfieldWithValues(final byte[] key, final long count) {
418+
sendCommand(HRANDFIELD, key, toByteArray(count), WITHVALUES.getRaw());
419+
}
420+
418421
public void rpush(final byte[] key, final byte[]... strings) {
419422
sendCommand(RPUSH, joinParameters(key, strings));
420423
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,48 @@ public Map<byte[], byte[]> hgetAll(final byte[] key) {
12881288
return hash;
12891289
}
12901290

1291+
/**
1292+
* Get one random field from a hash.
1293+
* <p>
1294+
* <b>Time complexity:</b> O(N), where N is the number of fields returned
1295+
* @param key
1296+
* @return one random field from a hash.
1297+
*/
1298+
@Override
1299+
public byte[] hrandfield(final byte[] key) {
1300+
checkIsInMultiOrPipeline();
1301+
client.hrandfield(key);
1302+
return client.getBinaryBulkReply();
1303+
}
1304+
1305+
/**
1306+
* Get multiple random fields from a hash.
1307+
* <p>
1308+
* <b>Time complexity:</b> O(N), where N is the number of fields returned
1309+
* @param key
1310+
* @return multiple random fields from a hash.
1311+
*/
1312+
@Override
1313+
public List<byte[]> hrandfield(final byte[] key, final long count) {
1314+
checkIsInMultiOrPipeline();
1315+
client.hrandfield(key, count);
1316+
return client.getBinaryMultiBulkReply();
1317+
}
1318+
1319+
/**
1320+
* Get one or multiple random fields with values from a hash.
1321+
* <p>
1322+
* <b>Time complexity:</b> O(N), where N is the number of fields returned
1323+
* @param key
1324+
* @return one or multiple random fields with values from a hash.
1325+
*/
1326+
@Override
1327+
public Map<byte[], byte[]> hrandfieldWithValues(final byte[] key, final long count) {
1328+
checkIsInMultiOrPipeline();
1329+
client.hrandfieldWithValues(key, count);
1330+
return BuilderFactory.BYTE_ARRAY_MAP.build(client.getBinaryMultiBulkReply());
1331+
}
1332+
12911333
/**
12921334
* Add the string value to the head (LPUSH) or tail (RPUSH) of the list stored at key. If the key
12931335
* does not exist an empty list is created just before the append operation. If the key exists but

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,36 @@ public Map<byte[], byte[]> execute(Jedis connection) {
652652
}.runBinary(key);
653653
}
654654

655+
@Override
656+
public byte[] hrandfield(final byte[] key) {
657+
return new JedisClusterCommand<byte[]>(connectionHandler, maxAttempts) {
658+
@Override
659+
public byte[] execute(Jedis connection) {
660+
return connection.hrandfield(key);
661+
}
662+
}.runBinary(key);
663+
}
664+
665+
@Override
666+
public List<byte[]> hrandfield(final byte[] key, final long count) {
667+
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
668+
@Override
669+
public List<byte[]> execute(Jedis connection) {
670+
return connection.hrandfield(key, count);
671+
}
672+
}.runBinary(key);
673+
}
674+
675+
@Override
676+
public Map<byte[], byte[]> hrandfieldWithValues(final byte[] key, final long count) {
677+
return new JedisClusterCommand<Map<byte[], byte[]>>(connectionHandler, maxAttempts) {
678+
@Override
679+
public Map<byte[], byte[]> execute(Jedis connection) {
680+
return connection.hrandfieldWithValues(key, count);
681+
}
682+
}.runBinary(key);
683+
}
684+
655685
@Override
656686
public Long rpush(final byte[] key, final byte[]... args) {
657687
return new JedisClusterCommand<Long>(connectionHandler, maxAttempts) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,24 @@ public Map<byte[], byte[]> hgetAll(final byte[] key) {
334334
return j.hgetAll(key);
335335
}
336336

337+
@Override
338+
public byte[] hrandfield(final byte[] key) {
339+
Jedis j = getShard(key);
340+
return j.hrandfield(key);
341+
}
342+
343+
@Override
344+
public List<byte[]> hrandfield(final byte[] key, final long count) {
345+
Jedis j = getShard(key);
346+
return j.hrandfield(key, count);
347+
}
348+
349+
@Override
350+
public Map<byte[], byte[]> hrandfieldWithValues(final byte[] key, final long count) {
351+
Jedis j = getShard(key);
352+
return j.hrandfieldWithValues(key, count);
353+
}
354+
337355
@Override
338356
public Long rpush(final byte[] key, final byte[]... strings) {
339357
Jedis j = getShard(key);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,21 @@ public void hgetAll(final String key) {
295295
hgetAll(SafeEncoder.encode(key));
296296
}
297297

298+
@Override
299+
public void hrandfield(final String key) {
300+
hrandfield(SafeEncoder.encode(key));
301+
}
302+
303+
@Override
304+
public void hrandfield(final String key, final long count) {
305+
hrandfield(SafeEncoder.encode(key), count);
306+
}
307+
308+
@Override
309+
public void hrandfieldWithValues(final String key, final long count) {
310+
hrandfieldWithValues(SafeEncoder.encode(key), count);
311+
}
312+
298313
@Override
299314
public void rpush(final String key, final String... string) {
300315
rpush(SafeEncoder.encode(key), SafeEncoder.encodeMany(string));

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,48 @@ public Map<String, String> hgetAll(final String key) {
10121012
return BuilderFactory.STRING_MAP.build(client.getBinaryMultiBulkReply());
10131013
}
10141014

1015+
/**
1016+
* Get one random field from a hash.
1017+
* <p>
1018+
* <b>Time complexity:</b> O(N), where N is the number of fields returned
1019+
* @param key
1020+
* @return one random field from a hash.
1021+
*/
1022+
@Override
1023+
public String hrandfield(final String key) {
1024+
checkIsInMultiOrPipeline();
1025+
client.hrandfield(key);
1026+
return client.getStatusCodeReply();
1027+
}
1028+
1029+
/**
1030+
* Get multiple random fields from a hash.
1031+
* <p>
1032+
* <b>Time complexity:</b> O(N), where N is the number of fields returned
1033+
* @param key
1034+
* @return multiple random fields from a hash.
1035+
*/
1036+
@Override
1037+
public List<String> hrandfield(final String key, final long count) {
1038+
checkIsInMultiOrPipeline();
1039+
client.hrandfield(key, count);
1040+
return client.getMultiBulkReply();
1041+
}
1042+
1043+
/**
1044+
* Get one or multiple random fields with values from a hash.
1045+
* <p>
1046+
* <b>Time complexity:</b> O(N), where N is the number of fields returned
1047+
* @param key
1048+
* @return one or multiple random fields with values from a hash.
1049+
*/
1050+
@Override
1051+
public Map<String, String> hrandfieldWithValues(final String key, final long count) {
1052+
checkIsInMultiOrPipeline();
1053+
client.hrandfieldWithValues(key, count);
1054+
return BuilderFactory.STRING_MAP.build(client.getBinaryMultiBulkReply());
1055+
}
1056+
10151057
/**
10161058
* Add the string value to the head (LPUSH) or tail (RPUSH) of the list stored at key. If the key
10171059
* does not exist an empty list is created just before the append operation. If the key exists but

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,36 @@ public Map<String, String> execute(Jedis connection) {
745745
}.run(key);
746746
}
747747

748+
@Override
749+
public String hrandfield(final String key) {
750+
return new JedisClusterCommand<String>(connectionHandler, maxAttempts) {
751+
@Override
752+
public String execute(Jedis connection) {
753+
return connection.hrandfield(key);
754+
}
755+
}.run(key);
756+
}
757+
758+
@Override
759+
public List<String> hrandfield(final String key, final long count) {
760+
return new JedisClusterCommand<List<String>>(connectionHandler, maxAttempts) {
761+
@Override
762+
public List<String> execute(Jedis connection) {
763+
return connection.hrandfield(key, count);
764+
}
765+
}.run(key);
766+
}
767+
768+
@Override
769+
public Map<String, String> hrandfieldWithValues(final String key, final long count) {
770+
return new JedisClusterCommand<Map<String, String>>(connectionHandler, maxAttempts) {
771+
@Override
772+
public Map<String, String> execute(Jedis connection) {
773+
return connection.hrandfieldWithValues(key, count);
774+
}
775+
}.run(key);
776+
}
777+
748778
@Override
749779
public Long rpush(final String key, final String... string) {
750780
return new JedisClusterCommand<Long>(connectionHandler, maxAttempts) {

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,42 @@ public Response<List<byte[]>> hvals(final byte[] key) {
412412
return getResponse(BuilderFactory.BYTE_ARRAY_LIST);
413413
}
414414

415+
@Override
416+
public Response<byte[]> hrandfield(final byte[] key) {
417+
getClient(key).hrandfield(key);
418+
return getResponse(BuilderFactory.BYTE_ARRAY);
419+
}
420+
421+
@Override
422+
public Response<List<byte[]>> hrandfield(final byte[] key, final long count) {
423+
getClient(key).hrandfield(key, count);
424+
return getResponse(BuilderFactory.BYTE_ARRAY_LIST);
425+
}
426+
427+
@Override
428+
public Response<Map<byte[], byte[]>> hrandfieldWithValues(final byte[] key, final long count) {
429+
getClient(key).hrandfieldWithValues(key, count);
430+
return getResponse(BuilderFactory.BYTE_ARRAY_MAP);
431+
}
432+
433+
@Override
434+
public Response<String> hrandfield(final String key) {
435+
getClient(key).hrandfield(key);
436+
return getResponse(BuilderFactory.STRING);
437+
}
438+
439+
@Override
440+
public Response<List<String>> hrandfield(final String key, final long count) {
441+
getClient(key).hrandfield(key, count);
442+
return getResponse(BuilderFactory.STRING_LIST);
443+
}
444+
445+
@Override
446+
public Response<Map<String, String>> hrandfieldWithValues(final String key, final long count) {
447+
getClient(key).hrandfieldWithValues(key, count);
448+
return getResponse(BuilderFactory.STRING_MAP);
449+
}
450+
415451
@Override
416452
public Response<Long> incr(final String key) {
417453
getClient(key).incr(key);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public static enum Command implements ProtocolCommand {
247247
PING, SET, GET, GETDEL, GETEX, QUIT, EXISTS, DEL, UNLINK, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME,
248248
RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX,
249249
SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET,
250-
HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM,
250+
HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, HRANDFIELD, RPUSH, LPUSH, LLEN, LRANGE, LTRIM,
251251
LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER,
252252
SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM,
253253
ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, ZPOPMAX, ZPOPMIN, MULTI, DISCARD, EXEC,
@@ -283,7 +283,7 @@ public static enum Keyword implements Rawable {
283283
GETNAME, SETNAME, LIST, MATCH, COUNT, PING, PONG, UNLOAD, REPLACE, KEYS, PAUSE, DOCTOR, BLOCK,
284284
NOACK, STREAMS, KEY, CREATE, MKSTREAM, SETID, DESTROY, DELCONSUMER, MAXLEN, GROUP, ID, IDLE,
285285
TIME, RETRYCOUNT, FORCE, USAGE, SAMPLES, STREAM, GROUPS, CONSUMERS, HELP, FREQ, SETUSER,
286-
GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS, LOG, INCR, SAVE, JUSTID;
286+
GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS, LOG, INCR, SAVE, JUSTID, WITHVALUES;
287287

288288
/**
289289
* @deprecated This will be private in future. Use {@link #getRaw()}.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,24 @@ public Map<String, String> hgetAll(final String key) {
355355
return j.hgetAll(key);
356356
}
357357

358+
@Override
359+
public String hrandfield(final String key) {
360+
Jedis j = getShard(key);
361+
return j.hrandfield(key);
362+
}
363+
364+
@Override
365+
public List<String> hrandfield(final String key, final long count) {
366+
Jedis j = getShard(key);
367+
return j.hrandfield(key, count);
368+
}
369+
370+
@Override
371+
public Map<String, String> hrandfieldWithValues(final String key, final long count) {
372+
Jedis j = getShard(key);
373+
return j.hrandfieldWithValues(key, count);
374+
}
375+
358376
@Override
359377
public Long rpush(final String key, String... strings) {
360378
Jedis j = getShard(key);

0 commit comments

Comments
 (0)