Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JSON.MERGE Command #3429

Merged
merged 28 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b1188fc
Support JSON.MERGE Command
shacharPash May 18, 2023
10d860f
add comment
shacharPash May 21, 2023
c747d54
add Merge test to JsonV1Test
shacharPash May 21, 2023
f7a7a77
add test to V2 + fixes
shacharPash May 21, 2023
791893e
fix tests
shacharPash May 21, 2023
2e6fd25
change to new Path("$")
shacharPash May 21, 2023
6ed1c7c
wip
shacharPash May 21, 2023
b612b32
Merge branch 'master' into SP/Jedis/SupportJSON.MERGE
shacharPash May 21, 2023
bf2013b
Support JSON.MERGE Command
shacharPash May 18, 2023
8cf2166
add comment
shacharPash May 21, 2023
584d2b1
add Merge test to JsonV1Test
shacharPash May 21, 2023
cebfb50
add test to V2 + fixes
shacharPash May 21, 2023
8abf0eb
fix tests
shacharPash May 21, 2023
71e96e9
change to new Path("$")
shacharPash May 21, 2023
451191d
wip
shacharPash May 21, 2023
c4cf743
uncommend V2 test and delete tostrings
shacharPash May 21, 2023
6f862fe
Merge branch 'master' into SP/Jedis/SupportJSON.MERGE
shacharPash May 21, 2023
5a3a03a
Merge branch 'SP/Jedis/SupportJSON.MERGE' of https://github.com/shach…
shacharPash May 21, 2023
a4d8431
complete merge
shacharPash May 22, 2023
9a78369
merge after set
shacharPash May 22, 2023
58d227c
fix V1 tests
shacharPash May 22, 2023
3d01e35
tring to fix test
shacharPash May 24, 2023
6d88699
review
shacharPash May 28, 2023
6226297
Merge branch 'master' into SP/Jedis/SupportJSON.MERGE
shacharPash May 28, 2023
155a033
add jsonMerge to PipelineBase
shacharPash May 28, 2023
6eb7f63
undo formatting white spaces
sazzad16 May 28, 2023
916749f
empty line
sazzad16 May 28, 2023
6b56d90
empty line
sazzad16 May 28, 2023
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
9 changes: 9 additions & 0 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -3425,6 +3425,15 @@ public final <T> CommandObject<T> jsonGet(String key, Class<T> clazz, Path... pa
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key).addObjects((Object[]) paths), new JsonObjectBuilder<>(clazz));
}

public final CommandObject<String> jsonMerge(String key, Path2 path, Object object) {
return new CommandObject<>(commandArguments(JsonCommand.MERGE).key(key).add(path).add(object), BuilderFactory.STRING);
}

public final CommandObject<String> jsonMerge(String key, Path path, Object pojo) {
return new CommandObject<>(commandArguments(JsonCommand.MERGE).key(key).add(path).add(
getJsonObjectMapper().toJson(pojo)), BuilderFactory.STRING);
}

public final CommandObject<List<JSONArray>> jsonMGet(Path2 path, String... keys) {
return new CommandObject<>(commandArguments(JsonCommand.MGET).keys((Object[]) keys).add(path), JsonBuilderFactory.JSON_ARRAY_LIST);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/MultiNodePipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3593,6 +3593,16 @@ public <T> Response<T> jsonGet(String key, Class<T> clazz, Path... paths) {
return appendCommand(commandObjects.jsonGet(key, clazz, paths));
}

@Override
public Response<String> jsonMerge(String key, Path2 path, Object object) {
return appendCommand(commandObjects.jsonMerge(key, path, object));
}

@Override
public Response<String> jsonMerge(String key, Path path, Object object) {
return appendCommand(commandObjects.jsonMerge(key, path, object));
}

@Override
public Response<List<JSONArray>> jsonMGet(Path2 path, String... keys) {
return appendCommand(commandObjects.jsonMGet(path, keys));
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3546,6 +3546,16 @@ public <T> Response<T> jsonGet(String key, Class<T> clazz, Path... paths) {
return appendCommand(commandObjects.jsonGet(key, clazz, paths));
}

@Override
public Response<String> jsonMerge(String key, Path path, Object object) {
return appendCommand(commandObjects.jsonMerge(key, path, object));
}

@Override
public Response<String> jsonMerge(String key, Path2 path, Object object) {
return appendCommand(commandObjects.jsonMerge(key, path, object));
}

@Override
public Response<List<JSONArray>> jsonMGet(Path2 path, String... keys) {
return appendCommand(commandObjects.jsonMGet(path, keys));
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/redis/clients/jedis/TransactionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public abstract class TransactionBase extends Queable implements PipelineCommand

/**
* Creates a new transaction.
*
*
* A MULTI command will be added to be sent to server. WATCH/UNWATCH/MULTI commands must not be
* called with this object.
* @param connection connection
Expand Down Expand Up @@ -3644,6 +3644,16 @@ public <T> Response<T> jsonGet(String key, Class<T> clazz, Path... paths) {
return appendCommand(commandObjects.jsonGet(key, clazz, paths));
}

@Override
public Response<String> jsonMerge(String key, Path2 path, Object object) {
return appendCommand(commandObjects.jsonMerge(key, path, object));
}

@Override
public Response<String> jsonMerge(String key, Path path, Object object) {
return appendCommand(commandObjects.jsonMerge(key, path, object));
}

@Override
public Response<List<JSONArray>> jsonMGet(Path2 path, String... keys) {
return appendCommand(commandObjects.jsonMGet(path, keys));
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3917,6 +3917,16 @@ public <T> T jsonGet(String key, Class<T> clazz, Path... paths) {
return executeCommand(commandObjects.jsonGet(key, clazz, paths));
}

@Override
public String jsonMerge(String key, Path2 path, Object object) {
return executeCommand(commandObjects.jsonMerge(key, path, object));
}

@Override
public String jsonMerge(String key, Path path, Object pojo) {
return executeCommand(commandObjects.jsonMerge(key, path, pojo));
}

@Override
public List<JSONArray> jsonMGet(Path2 path, String... keys) {
return executeCommand(commandObjects.jsonMGet(path, keys));
Expand Down
1 change: 1 addition & 0 deletions src/main/java/redis/clients/jedis/json/JsonProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum JsonCommand implements ProtocolCommand {
DEL("JSON.DEL"),
GET("JSON.GET"),
MGET("JSON.MGET"),
MERGE("JSON.MERGE"),
SET("JSON.SET"),
TYPE("JSON.TYPE"),
STRAPPEND("JSON.STRAPPEND"),
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/json/RedisJsonCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ default String jsonSetLegacy(String key, Object pojo, JsonSetParams params) {

<T> T jsonGet(String key, Class<T> clazz, Path... paths);

String jsonMerge(String key, Path2 path, Object object);

String jsonMerge(String key, Path path, Object pojo);

shacharPash marked this conversation as resolved.
Show resolved Hide resolved
default List<JSONArray> jsonMGet(String... keys) {
return jsonMGet(Path2.ROOT_PATH, keys);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ default Response<String> jsonSetLegacy(String key, Object pojo, JsonSetParams pa

<T> Response<T> jsonGet(String key, Class<T> clazz, Path... paths);

Response<String> jsonMerge(String key, Path2 path, Object object);

Response<String> jsonMerge(String key, Path path, Object pojo);

default Response<List<JSONArray>> jsonMGet(String... keys) {
return jsonMGet(Path2.ROOT_PATH, keys);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@ public void typeChecksShouldSucceed() {
assertNull(client.jsonType("foobar", Path.of(".fooErr")));
}

// @Test
// public void testJsonMerge() {
// // Test with root path
// assertEquals("OK", client.jsonSet("test_merge", "{\"person\":{\"name\":\"John Doe\",\"age\":25,\"address\":{\"home\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}"));
// assertEquals("OK", client.jsonMerge("test_merge", new Path("$"), "{\"person\":{\"age\":30}}"));
// assertEquals("{[person={name=John Doe, age=30.0, address={home=123 Main Street}, phone=123-456-7890]}}", client.jsonGet("test_merge").toString());

// // Test with root path path $.a.b
// assertEquals("OK", client.jsonMerge("test_merge", new Path("$.person.address"), "{\"work\":\"Redis office\"}"));
// assertEquals("{[[person={name=John Doe, age=30.0, address={home=123 Main Street, work=Redis office}, phone=123-456-7890]]}}", client.jsonGet("test_merge").toString());

// // Test with null value to delete a value
// assertEquals("OK", client.jsonMerge("test_merge", new Path("$.person"), "{\"age\":null}"));
// assertEquals("{[person={name=John Doe, phone=123-456-7890, address={home=123 Main Street, work=Redis office}]}}", client.jsonGet("test_merge").toString());
// }

shacharPash marked this conversation as resolved.
Show resolved Hide resolved
@Test
public void mgetWithPathWithAllKeysExist() {
Baz baz1 = new Baz("quuz1", "grault1", "waldo1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ public void typeChecksShouldSucceed() {
assertEquals(Collections.emptyList(), client.jsonType("foobar", Path2.of(".fooErr")));
}

@Test
public void testJsonMerge() {
shacharPash marked this conversation as resolved.
Show resolved Hide resolved
// Test with root path
assertEquals("OK", client.jsonSet("test_merge", "{\"person\":{\"name\":\"John Doe\",\"age\":25,\"address\":{\"home\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}"));
assertEquals("OK", client.jsonMerge("test_merge", new Path2("$"), "{\"person\":{\"age\":30}}"));
assertEquals("{person={name=John Doe, age=30.0, address={home=123 Main Street}, phone=123-456-7890}}", client.jsonGet("test_merge").toString());
shacharPash marked this conversation as resolved.
Show resolved Hide resolved
shacharPash marked this conversation as resolved.
Show resolved Hide resolved

// Test with root path path $.a.b
assertEquals("OK", client.jsonMerge("test_merge", new Path2("$.person.address"), "{\"work\":\"Redis office\"}"));
assertEquals("{person={name=John Doe, age=30.0, address={home=123 Main Street, work=Redis office}, phone=123-456-7890}}", client.jsonGet("test_merge").toString());

// Test with null value to delete a value
assertEquals("OK", client.jsonMerge("test_merge", new Path2("$.person"), "{\"age\":null}"));
assertEquals("{person={name=John Doe, phone=123-456-7890, address={home=123 Main Street, work=Redis office}}}", client.jsonGet("test_merge").toString());

// cleanup
assertEquals(1L, client.del("test_merge"));
}

@Test
public void mgetWithPathWithAllKeysExist() {
Baz baz1 = new Baz("quuz1", "grault1", "waldo1");
Expand Down