Skip to content

Commit 494ccf8

Browse files
TairDoc support JSON.MERGE command
1 parent dabc83f commit 494ccf8

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed

src/main/java/com/aliyun/tair/ModuleCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public enum ModuleCommand implements ProtocolCommand {
1818
JSONARRINSERT("JSON.ARRINSERT"),
1919
JSONARRLEN("JSON.ARRLEN"),
2020
JSONARRTRIM("JSON.ARRTRIM"),
21+
JSONMERGE("JSON.MERGE"),
2122

2223
// TairHash command
2324
EXHSET("EXHSET"),

src/main/java/com/aliyun/tair/tairdoc/TairDoc.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,42 @@ public Long jsonarrTrim(final byte[] key, final byte[] path, final int start, fi
610610
releaseJedis(jedis);
611611
}
612612
}
613+
614+
/**
615+
* JSON.MERGE <key> <path> <value>
616+
* Merges the given value into the JSON value at the specified path.
617+
*
618+
* @param key the key of the JSON document
619+
* @param path the path within the JSON document
620+
* @param value the value to merge
621+
* @return OK if successful, throw error otherwise
622+
*/
623+
public String jsonMerge(final String key, final String path, final String value) {
624+
Jedis jedis = getJedis();
625+
try {
626+
Object obj = jedis.sendCommand(ModuleCommand.JSONMERGE, key, path, value);
627+
return BuilderFactory.STRING.build(obj);
628+
} finally {
629+
releaseJedis(jedis);
630+
}
631+
}
632+
633+
/**
634+
* JSON.MERGE <key> <path> <value>
635+
* Merges the given value into the JSON value at the specified path.
636+
*
637+
* @param key the key of the JSON document
638+
* @param path the path within the JSON document
639+
* @param value the value to merge
640+
* @return OK if successful, throw error otherwise
641+
*/
642+
public String jsonMerge(final byte[] key, final byte[] path, final byte[] value) {
643+
Jedis jedis = getJedis();
644+
try {
645+
Object obj = jedis.sendCommand(ModuleCommand.JSONMERGE, key, path, value);
646+
return BuilderFactory.STRING.build(obj);
647+
} finally {
648+
releaseJedis(jedis);
649+
}
650+
}
613651
}

src/main/java/com/aliyun/tair/tairdoc/TairDocCluster.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,14 @@ public Long jsonarrTrim(final byte[] key, final byte[] path, final int start, fi
260260
toByteArray(stop));
261261
return BuilderFactory.LONG.build(obj);
262262
}
263+
264+
public String jsonMerge(final String key, final String path, final String value) {
265+
Object obj = jc.sendCommand(key, ModuleCommand.JSONMERGE, key, path, value);
266+
return BuilderFactory.STRING.build(obj);
267+
}
268+
269+
public String jsonMerge(final byte[] key, final byte[] path, final byte[] value) {
270+
Object obj = jc.sendCommand(key, ModuleCommand.JSONMERGE, key, path, value);
271+
return BuilderFactory.STRING.build(obj);
272+
}
263273
}

src/main/java/com/aliyun/tair/tairdoc/TairDocPipeline.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,14 @@ public Response<Long> jsonarrTrim(final byte[] key, final byte[] path, final int
287287
return appendCommand(new CommandObject<>(new CommandArguments(ModuleCommand.JSONARRTRIM).key(key).add(path)
288288
.add(start).add(stop), BuilderFactory.LONG));
289289
}
290+
291+
public Response<String> jsonMerge(final String key, final String path, final String value) {
292+
return appendCommand(new CommandObject<>(new CommandArguments(ModuleCommand.JSONMERGE).key(key).add(path)
293+
.add(value), BuilderFactory.STRING));
294+
}
295+
296+
public Response<String> jsonMerge(final byte[] key, final byte[] path, final byte[] value) {
297+
return appendCommand(new CommandObject<>(new CommandArguments(ModuleCommand.JSONMERGE).key(key).add(path)
298+
.add(value), BuilderFactory.STRING));
299+
}
290300
}

src/test/java/com/aliyun/tair/tests/tairdoc/TairDocTest.java

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,124 @@ public void jsonarrtrimException() {
579579
assertTrue(e.getMessage().contains("WRONGTYPE"));
580580
}
581581
}
582+
583+
@Test
584+
public void jsonMergeCreatePathTest() {
585+
String ret = tairDoc.jsonset(jsonKey, ".", "{\"a\":2}");
586+
assertEquals("OK", ret);
587+
588+
ret = tairDoc.jsonMerge(jsonKey, ".b", "8");
589+
assertEquals("OK", ret);
590+
591+
ret = tairDoc.jsonget(jsonKey, ".");
592+
assertEquals("{\"a\":2,\"b\":8}", ret);
593+
}
594+
595+
@Test
596+
public void jsonMergeReplaceValueTest() {
597+
String ret = tairDoc.jsonset(jsonKey, ".", "{\"a\":2}");
598+
assertEquals("OK", ret);
599+
600+
ret = tairDoc.jsonMerge(jsonKey, ".a", "3");
601+
assertEquals("OK", ret);
602+
603+
ret = tairDoc.jsonget(jsonKey, ".");
604+
assertEquals("{\"a\":3}", ret);
605+
}
606+
607+
@Test
608+
public void jsonMergeDeleteValueTest() {
609+
String ret = tairDoc.jsonset(jsonKey, ".", "{\"a\":2}");
610+
assertEquals("OK", ret);
611+
612+
ret = tairDoc.jsonMerge(jsonKey, ".", "{\"a\":null}");
613+
assertEquals("OK", ret);
614+
615+
ret = tairDoc.jsonget(jsonKey, ".");
616+
assertEquals("{}", ret);
617+
}
618+
619+
@Test
620+
public void jsonMergeReplaceArrayTest() {
621+
String ret = tairDoc.jsonset(jsonKey, ".", "{\"a\":[2,4,6,8]}");
622+
assertEquals("OK", ret);
623+
624+
ret = tairDoc.jsonMerge(jsonKey, ".a", "[10,12]");
625+
assertEquals("OK", ret);
626+
627+
ret = tairDoc.jsonget(jsonKey, ".");
628+
assertEquals("{\"a\":[10,12]}", ret);
629+
}
630+
631+
@Test
632+
public void jsonMergeMultiPathsTest() {
633+
String ret = tairDoc.jsonset(jsonKey, ".", "{\"f1\":{\"a\":1},\"f2\":{\"a\":2}}");
634+
assertEquals("OK", ret);
635+
636+
ret = tairDoc.jsonMerge(jsonKey, ".", "{\"f1\":null,\"f2\":{\"a\":3,\"b\":4},\"f3\":[2,4,6]}");
637+
assertEquals("OK", ret);
638+
639+
ret = tairDoc.jsonget(jsonKey, ".");
640+
assertEquals("{\"f2\":{\"a\":3,\"b\":4},\"f3\":[2,4,6]}", ret);
641+
}
642+
643+
@Test
644+
public void jsonMergeCreatePathTestBinary() {
645+
String ret = tairDoc.jsonset(jsonKey.getBytes(), ".".getBytes(), "{\"a\":2}".getBytes());
646+
assertEquals("OK", ret);
647+
648+
ret = tairDoc.jsonMerge(jsonKey.getBytes(), ".b".getBytes(), "8".getBytes());
649+
assertEquals("OK", ret);
650+
651+
byte[] bret = tairDoc.jsonget(jsonKey.getBytes(), ".".getBytes());
652+
assertEquals("{\"a\":2,\"b\":8}", new String(bret));
653+
}
654+
655+
@Test
656+
public void jsonMergeReplaceValueTestBinary() {
657+
String ret = tairDoc.jsonset(jsonKey.getBytes(), ".".getBytes(), "{\"a\":2}".getBytes());
658+
assertEquals("OK", ret);
659+
660+
ret = tairDoc.jsonMerge(jsonKey.getBytes(), ".a".getBytes(), "3".getBytes());
661+
assertEquals("OK", ret);
662+
663+
byte[] bret = tairDoc.jsonget(jsonKey.getBytes(), ".".getBytes());
664+
assertEquals("{\"a\":3}", new String(bret));
665+
}
666+
667+
@Test
668+
public void jsonMergeDeleteValueTestBinary() {
669+
String ret = tairDoc.jsonset(jsonKey.getBytes(), ".".getBytes(), "{\"a\":2}".getBytes());
670+
assertEquals("OK", ret);
671+
672+
ret = tairDoc.jsonMerge(jsonKey.getBytes(), ".".getBytes(), "{\"a\":null}".getBytes());
673+
assertEquals("OK", ret);
674+
675+
byte[] bret = tairDoc.jsonget(jsonKey.getBytes(), ".".getBytes());
676+
assertEquals("{}", new String(bret));
677+
}
678+
679+
@Test
680+
public void jsonMergeReplaceArrayTestBinary() {
681+
String ret = tairDoc.jsonset(jsonKey.getBytes(), ".".getBytes(), "{\"a\":[2,4,6,8]}".getBytes());
682+
assertEquals("OK", ret);
683+
684+
ret = tairDoc.jsonMerge(jsonKey.getBytes(), ".a".getBytes(), "[10,12]".getBytes());
685+
assertEquals("OK", ret);
686+
687+
byte[] bret = tairDoc.jsonget(jsonKey.getBytes(), ".".getBytes());
688+
assertEquals("{\"a\":[10,12]}", new String(bret));
689+
}
690+
691+
@Test
692+
public void jsonMergeMultiPathsTestBinary() {
693+
String ret = tairDoc.jsonset(jsonKey.getBytes(), ".".getBytes(), "{\"f1\":{\"a\":1},\"f2\":{\"a\":2}}".getBytes());
694+
assertEquals("OK", ret);
695+
696+
ret = tairDoc.jsonMerge(jsonKey.getBytes(), ".".getBytes(), "{\"f1\":null,\"f2\":{\"a\":3,\"b\":4},\"f3\":[2,4,6]}".getBytes());
697+
assertEquals("OK", ret);
698+
699+
byte[] bret = tairDoc.jsonget(jsonKey.getBytes(), ".".getBytes());
700+
assertEquals("{\"f2\":{\"a\":3,\"b\":4},\"f3\":[2,4,6]}", new String(bret));
701+
}
582702
}

0 commit comments

Comments
 (0)