Skip to content

Commit 29a8d34

Browse files
authored
Support Json commands after RESP3 update (#3460)
* JSON.GET v2 RESP3 * JSON.NUMINCRBY v2 RESP3 * JSON.TYPE v2 RESP3 * cleanup prints * edit * Use interface in RedisJsonV1Test * JSON v1 commands with RESP3 * Comment out RedisJSON v1 with RESP3 * more * pipeline test * pipeline commands * Allow RESP3 in existing methods even though there are newer methods suffixed with 'Resp3' * cleanup commented codes * remove more commented codes * breaking changes list * rephrase doc * Address json module changes * Comment unused code * CLEANUP commented implementations * merge fix
1 parent 9a71576 commit 29a8d34

File tree

12 files changed

+127
-44
lines changed

12 files changed

+127
-44
lines changed

docs/jedis5-breaking.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
- `tsMRevRange(long fromTimestamp, long toTimestamp, String... filters)`
4141
- `tsMRevRange(TSMRangeParams multiRangeParams)`
4242

43+
- `jsonNumIncrBy(String key, Path2 path, double value)` method now returns `Object` instead of `JSONArray`.
44+
- Previously when it was returning JSONArray, returned would still be JSONArray. So simple type casting should be enough to handle this change.
45+
- The returning object will be `List<Double>` when running under RESP3 protocol.
46+
4347
- `getAgeSeconds()` in `AccessControlLogEntry` now returns `Double` instead of `String`.
4448

4549
- Both `ftConfigGet(String option)` and `ftConfigGet(String indexName, String option)` methods now return `Map<String, Object>` instead of `Map<String, String>`.

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3460,7 +3460,8 @@ public final CommandObject<String> jsonMerge(String key, Path path, Object pojo)
34603460
}
34613461

34623462
public final CommandObject<Object> jsonGet(String key) {
3463-
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key), new JsonObjectBuilder<>(Object.class));
3463+
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key),
3464+
protocol != RedisProtocol.RESP3 ? JSON_GENERIC_OBJECT : JsonBuilderFactory.JSON_OBJECT);
34643465
}
34653466

34663467
public final <T> CommandObject<T> jsonGet(String key, Class<T> clazz) {
@@ -3472,7 +3473,7 @@ public final CommandObject<Object> jsonGet(String key, Path2... paths) {
34723473
}
34733474

34743475
public final CommandObject<Object> jsonGet(String key, Path... paths) {
3475-
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key).addObjects((Object[]) paths), new JsonObjectBuilder<>(Object.class));
3476+
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key).addObjects((Object[]) paths), JSON_GENERIC_OBJECT);
34763477
}
34773478

34783479
public final CommandObject<String> jsonGetAsPlainString(String key, Path path) {
@@ -3528,7 +3529,8 @@ public final CommandObject<Class<?>> jsonType(String key) {
35283529
}
35293530

35303531
public final CommandObject<List<Class<?>>> jsonType(String key, Path2 path) {
3531-
return new CommandObject<>(commandArguments(JsonCommand.TYPE).key(key).add(path), JsonBuilderFactory.JSON_TYPE_LIST);
3532+
return new CommandObject<>(commandArguments(JsonCommand.TYPE).key(key).add(path),
3533+
protocol != RedisProtocol.RESP3 ? JsonBuilderFactory.JSON_TYPE_LIST : JsonBuilderFactory.JSON_TYPE_RESPONSE_RESP3_COMPATIBLE);
35323534
}
35333535

35343536
public final CommandObject<Class<?>> jsonType(String key, Path path) {
@@ -3562,8 +3564,9 @@ public final CommandObject<Long> jsonStrLen(String key, Path path) {
35623564
return new CommandObject<>(commandArguments(JsonCommand.STRLEN).key(key).add(path), BuilderFactory.LONG);
35633565
}
35643566

3565-
public final CommandObject<JSONArray> jsonNumIncrBy(String key, Path2 path, double value) {
3566-
return new CommandObject<>(commandArguments(JsonCommand.NUMINCRBY).key(key).add(path).add(value), JsonBuilderFactory.JSON_ARRAY);
3567+
public final CommandObject<Object> jsonNumIncrBy(String key, Path2 path, double value) {
3568+
return new CommandObject<>(commandArguments(JsonCommand.NUMINCRBY).key(key).add(path).add(value),
3569+
JsonBuilderFactory.JSON_ARRAY_OR_DOUBLE_LIST);
35673570
}
35683571

35693572
public final CommandObject<Double> jsonNumIncrBy(String key, Path path, double value) {
@@ -4260,6 +4263,11 @@ public T build(Object data) {
42604263
}
42614264
}
42624265

4266+
/**
4267+
* {@link JsonObjectBuilder} for {@code Object.class}.
4268+
*/
4269+
private final Builder<Object> JSON_GENERIC_OBJECT = new JsonObjectBuilder<>(Object.class);
4270+
42634271
private class JsonObjectListBuilder<T> extends Builder<List<T>> {
42644272

42654273
private final Class<T> clazz;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3607,7 +3607,7 @@ public Response<Long> jsonStrLen(String key, Path path) {
36073607
}
36083608

36093609
@Override
3610-
public Response<JSONArray> jsonNumIncrBy(String key, Path2 path, double value) {
3610+
public Response<Object> jsonNumIncrBy(String key, Path2 path, double value) {
36113611
return appendCommand(commandObjects.jsonNumIncrBy(key, path, value));
36123612
}
36133613

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3775,7 +3775,7 @@ public Response<Long> jsonStrLen(String key, Path path) {
37753775
}
37763776

37773777
@Override
3778-
public Response<JSONArray> jsonNumIncrBy(String key, Path2 path, double value) {
3778+
public Response<Object> jsonNumIncrBy(String key, Path2 path, double value) {
37793779
return appendCommand(commandObjects.jsonNumIncrBy(key, path, value));
37803780
}
37813781

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4056,7 +4056,7 @@ public Long jsonStrLen(String key, Path path) {
40564056
}
40574057

40584058
@Override
4059-
public JSONArray jsonNumIncrBy(String key, Path2 path, double value) {
4059+
public Object jsonNumIncrBy(String key, Path2 path, double value) {
40604060
return executeCommand(commandObjects.jsonNumIncrBy(key, path, value));
40614061
}
40624062

src/main/java/redis/clients/jedis/json/JsonBuilderFactory.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.json.JSONException;
1010
import org.json.JSONObject;
1111
import redis.clients.jedis.Builder;
12+
import redis.clients.jedis.BuilderFactory;
1213
import redis.clients.jedis.exceptions.JedisException;
1314

1415
public final class JsonBuilderFactory {
@@ -60,6 +61,21 @@ public List<Class<?>> build(Object data) {
6061
}
6162
};
6263

64+
public static final Builder<List<List<Class<?>>>> JSON_TYPE_RESPONSE_RESP3 = new Builder<List<List<Class<?>>>>() {
65+
@Override
66+
public List<List<Class<?>>> build(Object data) {
67+
return ((List<Object>) data).stream().map(JSON_TYPE_LIST::build).collect(Collectors.toList());
68+
}
69+
};
70+
71+
public static final Builder<List<Class<?>>> JSON_TYPE_RESPONSE_RESP3_COMPATIBLE = new Builder<List<Class<?>>>() {
72+
@Override
73+
public List<Class<?>> build(Object data) {
74+
List<List<Class<?>>> fullReply = JSON_TYPE_RESPONSE_RESP3.build(data);
75+
return fullReply == null ? null : fullReply.get(0);
76+
}
77+
};
78+
6379
public static final Builder<Object> JSON_OBJECT = new Builder<Object>() {
6480
@Override
6581
public Object build(Object data) {
@@ -70,7 +86,6 @@ public Object build(Object data) {
7086
if (!(data instanceof byte[])) {
7187
return data;
7288
}
73-
7489
String str = STRING.build(data);
7590
if (str.charAt(0) == '{') {
7691
try {
@@ -103,6 +118,15 @@ public JSONArray build(Object data) {
103118
}
104119
};
105120

121+
public static final Builder<Object> JSON_ARRAY_OR_DOUBLE_LIST = new Builder<Object>() {
122+
@Override
123+
public Object build(Object data) {
124+
if (data == null) return null;
125+
if (data instanceof List) return BuilderFactory.DOUBLE_LIST.build(data);
126+
return JSON_ARRAY.build(data);
127+
}
128+
};
129+
106130
public static final Builder<List<JSONArray>> JSON_ARRAY_LIST = new Builder<List<JSONArray>>() {
107131
@Override
108132
public List<JSONArray> build(Object data) {

src/main/java/redis/clients/jedis/json/commands/RedisJsonV2Commands.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ default List<JSONArray> jsonMGet(String... keys) {
5959

6060
List<Long> jsonStrLen(String key, Path2 path);
6161

62-
JSONArray jsonNumIncrBy(String key, Path2 path, double value);
62+
Object jsonNumIncrBy(String key, Path2 path, double value);
6363

6464
List<Long> jsonArrAppend(String key, Path2 path, Object... objects);
6565

src/main/java/redis/clients/jedis/json/commands/RedisJsonV2PipelineCommands.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ default Response<List<JSONArray>> jsonMGet(String... keys) {
6060

6161
Response<List<Long>> jsonStrLen(String key, Path2 path);
6262

63-
Response<JSONArray> jsonNumIncrBy(String key, Path2 path, double value);
63+
Response<Object> jsonNumIncrBy(String key, Path2 path, double value);
6464

6565
Response<List<Long>> jsonArrAppend(String key, Path2 path, Object... objects);
6666

src/test/java/redis/clients/jedis/modules/RedisModulesPipelineTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@
1414
import java.util.Collections;
1515
import org.json.JSONArray;
1616
import org.json.JSONObject;
17+
import org.junit.Assume;
1718
import org.junit.BeforeClass;
1819
import org.junit.Test;
1920

2021
import redis.clients.jedis.Pipeline;
22+
import redis.clients.jedis.RedisProtocol;
2123
import redis.clients.jedis.Response;
2224
import redis.clients.jedis.json.JsonSetParams;
2325
import redis.clients.jedis.json.Path;
2426
import redis.clients.jedis.json.Path2;
2527
import redis.clients.jedis.search.*;
2628
import redis.clients.jedis.search.aggr.*;
29+
import redis.clients.jedis.util.RedisProtocolUtil;
2730

2831
public class RedisModulesPipelineTest extends RedisModuleCommandsTestBase {
2932

@@ -88,6 +91,8 @@ public void search() {
8891

8992
@Test
9093
public void jsonV1() {
94+
Assume.assumeFalse(RedisProtocolUtil.getRedisProtocol() == RedisProtocol.RESP3);
95+
9196
Map<String, String> hm1 = new HashMap<>();
9297
hm1.put("hello", "world");
9398
hm1.put("oh", "snap");

src/test/java/redis/clients/jedis/modules/json/JsonObjects.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public IRLObject() {
1717
this.str = "string";
1818
this.bool = true;
1919
}
20+
21+
@Override
22+
public boolean equals(Object obj) {
23+
if (this == obj) return true;
24+
if (obj == null) return false;
25+
if (getClass() != obj.getClass()) return false;
26+
final IRLObject other = (IRLObject) obj;
27+
return Objects.equals(str, other.str)
28+
&& Objects.equals(bool, other.bool);
29+
}
2030
}
2131

2232
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)