Skip to content

Commit 50ec110

Browse files
authored
Fix return value of HRANDFIELD With Values when count is negative (#3425)
* #3017 - Fix return value of HRANDFIELD With Values when count is negative * #3017 - Fix return value of HRANDFIELD With Values when count is negative * Fix testcase failures
1 parent 9b94d07 commit 50ec110

File tree

14 files changed

+131
-38
lines changed

14 files changed

+131
-38
lines changed

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

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,23 +241,43 @@ public String toString() {
241241
}
242242
};
243243

244-
public static final Builder<Map<byte[], byte[]>> BINARY_MAP_FROM_PAIRS = new Builder<Map<byte[], byte[]>>() {
244+
public static final Builder<List<Map.Entry<byte[], byte[]>>> BINARY_PAIR_LIST = new Builder<List<Map.Entry<byte[], byte[]>>>() {
245245
@Override
246246
@SuppressWarnings("unchecked")
247-
public Map<byte[], byte[]> build(Object data) {
247+
public List<Map.Entry<byte[], byte[]>> build(Object data) {
248+
final List<byte[]> flatHash = (List<byte[]>) data;
249+
final List<Map.Entry<byte[], byte[]>> pairList = new ArrayList<>();
250+
final Iterator<byte[]> iterator = flatHash.iterator();
251+
while (iterator.hasNext()) {
252+
pairList.add(new AbstractMap.SimpleEntry<>(iterator.next(), iterator.next()));
253+
}
254+
255+
return pairList;
256+
}
257+
258+
@Override
259+
public String toString() {
260+
return "List<Map.Entry<byte[], byte[]>>";
261+
}
262+
};
263+
264+
public static final Builder<List<Map.Entry<byte[], byte[]>>> BINARY_PAIR_LIST_FROM_PAIRS = new Builder<List<Map.Entry<byte[], byte[]>>>() {
265+
@Override
266+
@SuppressWarnings("unchecked")
267+
public List<Map.Entry<byte[], byte[]>> build(Object data) {
248268
final List<Object> list = (List<Object>) data;
249-
final Map<byte[], byte[]> map = new JedisByteHashMap();
269+
final List<Map.Entry<byte[], byte[]>> pairList = new ArrayList<>();
250270
for (Object object : list) {
251271
final List<byte[]> flat = (List<byte[]>) object;
252-
map.put(flat.get(0), flat.get(1));
272+
pairList.add(new AbstractMap.SimpleEntry<>(flat.get(0), flat.get(1)));
253273
}
254274

255-
return map;
275+
return pairList;
256276
}
257277

258278
@Override
259279
public String toString() {
260-
return "Map<byte[], byte[]>";
280+
return "List<Map.Entry<byte[], byte[]>>";
261281
}
262282
};
263283

@@ -335,6 +355,48 @@ public String toString() {
335355
}
336356
};
337357

358+
public static final Builder<List<Map.Entry<String, String>>> STRING_PAIR_LIST = new Builder<List<Map.Entry<String, String>>>() {
359+
@Override
360+
@SuppressWarnings("unchecked")
361+
public List<Map.Entry<String, String>> build(Object data) {
362+
final List<byte[]> flatHash = (List<byte[]>) data;
363+
final List<Map.Entry<String, String>> pairList = new ArrayList<>();
364+
final Iterator<byte[]> iterator = flatHash.iterator();
365+
while (iterator.hasNext()) {
366+
pairList.add(new AbstractMap.SimpleEntry<>(STRING.build(iterator.next()), STRING.build(iterator.next())));
367+
}
368+
369+
return pairList;
370+
}
371+
372+
@Override
373+
public String toString() {
374+
return "List<Map.Entry<String, String>>";
375+
}
376+
377+
};
378+
379+
public static final Builder<List<Map.Entry<String, String>>> STRING_PAIR_LIST_FROM_PAIRS = new Builder<List<Map.Entry<String, String>>>() {
380+
@Override
381+
@SuppressWarnings("unchecked")
382+
public List<Map.Entry<String, String>> build(Object data) {
383+
final List<Object> list = (List<Object>) data;
384+
final List<Map.Entry<String, String>> pairList = new ArrayList<>();
385+
for (Object object : list) {
386+
final List<byte[]> flat = (List<byte[]>) object;
387+
pairList.add(new AbstractMap.SimpleEntry<>(SafeEncoder.encode(flat.get(0)), SafeEncoder.encode(flat.get(1))));
388+
}
389+
390+
return pairList;
391+
}
392+
393+
@Override
394+
public String toString() {
395+
return "List<Map.Entry<String, String>>";
396+
}
397+
398+
};
399+
338400
public static final Builder<KeyedListElement> KEYED_LIST_ELEMENT = new Builder<KeyedListElement>() {
339401
@Override
340402
@SuppressWarnings("unchecked")

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,9 +1090,9 @@ public final CommandObject<List<String>> hrandfield(String key, long count) {
10901090
return new CommandObject<>(commandArguments(HRANDFIELD).key(key).add(count), BuilderFactory.STRING_LIST);
10911091
}
10921092

1093-
public final CommandObject<Map<String, String>> hrandfieldWithValues(String key, long count) {
1093+
public final CommandObject<List<Map.Entry<String, String>>> hrandfieldWithValues(String key, long count) {
10941094
return new CommandObject<>(commandArguments(HRANDFIELD).key(key).add(count).add(WITHVALUES),
1095-
proto != RedisProtocol.RESP3 ? BuilderFactory.STRING_MAP : BuilderFactory.STRING_MAP_FROM_PAIRS);
1095+
proto != RedisProtocol.RESP3 ? BuilderFactory.STRING_PAIR_LIST : BuilderFactory.STRING_PAIR_LIST_FROM_PAIRS);
10961096
}
10971097

10981098
public final CommandObject<Map<byte[], byte[]>> hgetAll(byte[] key) {
@@ -1107,9 +1107,9 @@ public final CommandObject<List<byte[]>> hrandfield(byte[] key, long count) {
11071107
return new CommandObject<>(commandArguments(HRANDFIELD).key(key).add(count), BuilderFactory.BINARY_LIST);
11081108
}
11091109

1110-
public final CommandObject<Map<byte[], byte[]>> hrandfieldWithValues(byte[] key, long count) {
1110+
public final CommandObject<List<Map.Entry<byte[], byte[]>>> hrandfieldWithValues(byte[] key, long count) {
11111111
return new CommandObject<>(commandArguments(HRANDFIELD).key(key).add(count).add(WITHVALUES),
1112-
proto != RedisProtocol.RESP3 ? BuilderFactory.BINARY_MAP : BuilderFactory.BINARY_MAP_FROM_PAIRS);
1112+
proto != RedisProtocol.RESP3 ? BuilderFactory.BINARY_PAIR_LIST : BuilderFactory.BINARY_PAIR_LIST_FROM_PAIRS);
11131113
}
11141114

11151115
public final CommandObject<ScanResult<Map.Entry<String, String>>> hscan(String key, String cursor, ScanParams params) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ public List<byte[]> hrandfield(final byte[] key, final long count) {
13721372
* @return One or multiple random fields with values from a hash.
13731373
*/
13741374
@Override
1375-
public Map<byte[], byte[]> hrandfieldWithValues(final byte[] key, final long count) {
1375+
public List<Map.Entry<byte[], byte[]>> hrandfieldWithValues(final byte[] key, final long count) {
13761376
checkIsInMultiOrPipeline();
13771377
return connection.executeCommand(commandObjects.hrandfieldWithValues(key, count));
13781378
}
@@ -5799,7 +5799,7 @@ public List<String> hrandfield(final String key, final long count) {
57995799
* @return one or multiple random fields with values from a hash.
58005800
*/
58015801
@Override
5802-
public Map<String, String> hrandfieldWithValues(final String key, final long count) {
5802+
public List<Map.Entry<String, String>> hrandfieldWithValues(final String key, final long count) {
58035803
checkIsInMultiOrPipeline();
58045804
return connection.executeCommand(commandObjects.hrandfieldWithValues(key, count));
58055805
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ public Response<List<String>> hrandfield(String key, long count) {
807807
}
808808

809809
@Override
810-
public Response<Map<String, String>> hrandfieldWithValues(String key, long count) {
810+
public Response<List<Map.Entry<String, String>>> hrandfieldWithValues(String key, long count) {
811811
return appendCommand(commandObjects.hrandfieldWithValues(key, count));
812812
}
813813

@@ -2067,7 +2067,7 @@ public Response<List<byte[]>> hrandfield(byte[] key, long count) {
20672067
}
20682068

20692069
@Override
2070-
public Response<Map<byte[], byte[]>> hrandfieldWithValues(byte[] key, long count) {
2070+
public Response<List<Map.Entry<byte[], byte[]>>> hrandfieldWithValues(byte[] key, long count) {
20712071
return appendCommand(commandObjects.hrandfieldWithValues(key, count));
20722072
}
20732073

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ public Response<List<String>> hrandfield(String key, long count) {
761761
}
762762

763763
@Override
764-
public Response<Map<String, String>> hrandfieldWithValues(String key, long count) {
764+
public Response<List<Map.Entry<String, String>>> hrandfieldWithValues(String key, long count) {
765765
return appendCommand(commandObjects.hrandfieldWithValues(key, count));
766766
}
767767

@@ -2020,7 +2020,7 @@ public Response<List<byte[]>> hrandfield(byte[] key, long count) {
20202020
}
20212021

20222022
@Override
2023-
public Response<Map<byte[], byte[]>> hrandfieldWithValues(byte[] key, long count) {
2023+
public Response<List<Map.Entry<byte[], byte[]>>> hrandfieldWithValues(byte[] key, long count) {
20242024
return appendCommand(commandObjects.hrandfieldWithValues(key, count));
20252025
}
20262026

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ public Response<List<String>> hrandfield(String key, long count) {
856856
}
857857

858858
@Override
859-
public Response<Map<String, String>> hrandfieldWithValues(String key, long count) {
859+
public Response<List<Map.Entry<String, String>>> hrandfieldWithValues(String key, long count) {
860860
return appendCommand(commandObjects.hrandfieldWithValues(key, count));
861861
}
862862

@@ -2117,7 +2117,7 @@ public Response<List<byte[]>> hrandfield(byte[] key, long count) {
21172117
}
21182118

21192119
@Override
2120-
public Response<Map<byte[], byte[]>> hrandfieldWithValues(byte[] key, long count) {
2120+
public Response<List<Map.Entry<byte[], byte[]>>> hrandfieldWithValues(byte[] key, long count) {
21212121
return appendCommand(commandObjects.hrandfieldWithValues(key, count));
21222122
}
21232123

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ public List<String> hrandfield(String key, long count) {
15171517
}
15181518

15191519
@Override
1520-
public Map<String, String> hrandfieldWithValues(String key, long count) {
1520+
public List<Map.Entry<String, String>> hrandfieldWithValues(String key, long count) {
15211521
return executeCommand(commandObjects.hrandfieldWithValues(key, count));
15221522
}
15231523

@@ -1542,7 +1542,7 @@ public List<byte[]> hrandfield(byte[] key, long count) {
15421542
}
15431543

15441544
@Override
1545-
public Map<byte[], byte[]> hrandfieldWithValues(byte[] key, long count) {
1545+
public List<Map.Entry<byte[], byte[]>> hrandfieldWithValues(byte[] key, long count) {
15461546
return executeCommand(commandObjects.hrandfieldWithValues(key, count));
15471547
}
15481548

src/main/java/redis/clients/jedis/commands/HashBinaryCommands.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public interface HashBinaryCommands {
4141

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

44-
Map<byte[], byte[]> hrandfieldWithValues(byte[] key, long count);
44+
List<Map.Entry<byte[], byte[]>> hrandfieldWithValues(byte[] key, long count);
4545

4646
default ScanResult<Map.Entry<byte[], byte[]>> hscan(byte[] key, byte[] cursor) {
4747
return hscan(key, cursor, new ScanParams());

src/main/java/redis/clients/jedis/commands/HashCommands.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public interface HashCommands {
4141

4242
List<String> hrandfield(String key, long count);
4343

44-
Map<String, String> hrandfieldWithValues(String key, long count);
44+
List<Map.Entry<String, String>> hrandfieldWithValues(String key, long count);
4545

4646
default ScanResult<Map.Entry<String, String>> hscan(String key, String cursor) {
4747
return hscan(key, cursor, new ScanParams());

src/main/java/redis/clients/jedis/commands/HashPipelineBinaryCommands.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public interface HashPipelineBinaryCommands {
4242

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

45-
Response<Map<byte[], byte[]>> hrandfieldWithValues(byte[] key, long count);
45+
Response<List<Map.Entry<byte[], byte[]>>> hrandfieldWithValues(byte[] key, long count);
4646

4747
default Response<ScanResult<Map.Entry<byte[], byte[]>>> hscan(byte[] key, byte[] cursor) {
4848
return hscan(key, cursor, new ScanParams());

0 commit comments

Comments
 (0)