Skip to content

Commit

Permalink
Fix empty LUA table reply (#3924)
Browse files Browse the repository at this point in the history
* Fix empty LUA table reply

* processMapKeyValueReply returns List<KeyValue>

* make PROTOCOL_EMPTY_MAP unmodifiable
  • Loading branch information
sazzad16 authored Aug 18, 2024
1 parent cd9a1ab commit 4a1f35b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ public Object build(Object data) {

if (data instanceof List) {
final List list = (List) data;
if (list.isEmpty()) return Collections.emptyMap();
if (list.isEmpty()) {
return list == Protocol.PROTOCOL_EMPTY_MAP ? Collections.emptyMap() : Collections.emptyList();
}

if (list.get(0) instanceof KeyValue) {
return ((List<KeyValue>) data).stream()
Expand All @@ -397,8 +399,9 @@ public Object build(Object data) {
}
} else if (data instanceof byte[]) {
return STRING.build(data);
} else {
return data;
}
return data;
}
};

Expand Down
22 changes: 14 additions & 8 deletions src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -49,6 +50,8 @@ public final class Protocol {
public static final byte[] POSITIVE_INFINITY_BYTES = "+inf".getBytes();
public static final byte[] NEGATIVE_INFINITY_BYTES = "-inf".getBytes();

static final List<KeyValue> PROTOCOL_EMPTY_MAP = Collections.unmodifiableList(new ArrayList<>(0));

private static final String ASK_PREFIX = "ASK ";
private static final String MOVED_PREFIX = "MOVED ";
private static final String CLUSTERDOWN_PREFIX = "CLUSTERDOWN ";
Expand Down Expand Up @@ -192,7 +195,6 @@ private static byte[] processBulkReply(final RedisInputStream is) {
}

private static List<Object> processMultiBulkReply(final RedisInputStream is) {
// private static List<Object> processMultiBulkReply(final int num, final RedisInputStream is) {
final int num = is.readIntCrLf();
if (num == -1) return null;
final List<Object> ret = new ArrayList<>(num);
Expand All @@ -206,16 +208,20 @@ private static List<Object> processMultiBulkReply(final RedisInputStream is) {
return ret;
}

// private static List<Object> processMultiBulkReply(final RedisInputStream is) {
// private static List<Object> processMultiBulkReply(final int num, final RedisInputStream is) {
private static List<KeyValue> processMapKeyValueReply(final RedisInputStream is) {
final int num = is.readIntCrLf();
if (num == -1) return null;
final List<KeyValue> ret = new ArrayList<>(num);
for (int i = 0; i < num; i++) {
ret.add(new KeyValue(process(is), process(is)));
switch (num) {
case -1:
return null;
case 0:
return PROTOCOL_EMPTY_MAP;
default:
final List<KeyValue> ret = new ArrayList<>(num);
for (int i = 0; i < num; i++) {
ret.add(new KeyValue(process(is), process(is)));
}
return ret;
}
return ret;
}

public static Object read(final RedisInputStream is) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ public void scriptExistsWithBrokenConnection() {
deadClient.close();
}

@Test
public void emptyLuaTableReply() {
Object reply = jedis.eval("return {}");
assertEquals(Collections.emptyList(), reply);
}

@Test
public void functionLoadAndDelete() {
String engine = "Lua";
Expand Down

0 comments on commit 4a1f35b

Please sign in to comment.