From d8c75bb2d7ab0e8eabbd26f513e8e451ca627148 Mon Sep 17 00:00:00 2001 From: dengliming Date: Sun, 16 Jan 2022 00:37:13 +0800 Subject: [PATCH 1/3] Add support for JSON.OBJKEYS JSON.OBJLEN command --- redisjson/README.md | 2 +- .../redismodule/redisjson/RedisJSON.java | 42 +++++++++++++++++++ .../redisjson/protocol/RedisCommands.java | 9 ++++ .../redismodule/redisjson/RedisJSONTest.java | 32 ++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/redisjson/README.md b/redisjson/README.md index 7d615d0..0be0700 100644 --- a/redisjson/README.md +++ b/redisjson/README.md @@ -20,7 +20,7 @@ JSON.ARRLEN | RedisJSON.
arrLen()
arrLenAsync() | JSON.ARRPOP | RedisJSON.
arrPop()
arrPopAsync() | JSON.ARRTRIM | RedisJSON.
arrTrim()
arrTrimAsync() | JSON.OBJKEYS | N/A | -JSON.OBJLEN | N/A | +JSON.OBJLEN | RedisJSON.
objLen()
objLenAsync() | JSON.DEBUG | N/A | JSON.FORGET | N/A | JSON.RESP | N/A | diff --git a/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/RedisJSON.java b/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/RedisJSON.java index 0980a06..17d7dae 100644 --- a/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/RedisJSON.java +++ b/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/RedisJSON.java @@ -45,6 +45,8 @@ import static io.github.dengliming.redismodule.redisjson.protocol.RedisCommands.JSON_MGET; import static io.github.dengliming.redismodule.redisjson.protocol.RedisCommands.JSON_NUMINCRBY; import static io.github.dengliming.redismodule.redisjson.protocol.RedisCommands.JSON_NUMMULTBY; +import static io.github.dengliming.redismodule.redisjson.protocol.RedisCommands.JSON_OBJKEYS; +import static io.github.dengliming.redismodule.redisjson.protocol.RedisCommands.JSON_OBJLEN; import static io.github.dengliming.redismodule.redisjson.protocol.RedisCommands.JSON_SET; import static io.github.dengliming.redismodule.redisjson.protocol.RedisCommands.JSON_STRAPPEND; import static io.github.dengliming.redismodule.redisjson.protocol.RedisCommands.JSON_STRLEN; @@ -460,6 +462,46 @@ private RPromise transformRPromiseResult(RFuture getFuture, Class return result; } + /** + * Report the number of keys in the JSON Object at path in key. + * + * JSON.OBJLEN [path] + * + * @param key + * @param path + * @return + */ + public Long objLen(String key, String path) { + return commandExecutor.get(objLenAsync(key, path)); + } + + public RFuture objLenAsync(String key, String path) { + RAssert.notEmpty(key, "key must not be empty"); + RAssert.notNull(path, "path must not be null"); + + return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, JSON_OBJLEN, key, path); + } + + /** + * Return the keys in the object that's referenced by path. + * + * JSON.OBJKEYS [path] + * + * @param key + * @param path + * @return + */ + public List objKeys(String key, String path) { + return commandExecutor.get(objKeysAsync(key, path)); + } + + public RFuture> objKeysAsync(String key, String path) { + RAssert.notEmpty(key, "key must not be empty"); + RAssert.notNull(path, "path must not be null"); + + return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, JSON_OBJKEYS, key, path); + } + public String getName() { return ""; } diff --git a/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/protocol/RedisCommands.java b/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/protocol/RedisCommands.java index cb93a82..e8cadd1 100644 --- a/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/protocol/RedisCommands.java +++ b/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/protocol/RedisCommands.java @@ -17,12 +17,19 @@ package io.github.dengliming.redismodule.redisjson.protocol; import org.redisson.client.protocol.RedisCommand; +import org.redisson.client.protocol.convertor.BooleanReplayConvertor; import org.redisson.client.protocol.convertor.LongReplayConvertor; +import org.redisson.client.protocol.convertor.StringToListConvertor; +import org.redisson.client.protocol.decoder.ListMultiDecoder2; +import org.redisson.client.protocol.decoder.MultiDecoder; import org.redisson.client.protocol.decoder.ObjectDecoder; import org.redisson.client.protocol.decoder.ObjectListReplayDecoder; import org.redisson.client.protocol.decoder.StringDataDecoder; +import org.redisson.client.protocol.decoder.StringListReplayDecoder; import org.redisson.client.protocol.decoder.StringReplayDecoder; +import java.util.List; + /** * @author dengliming */ @@ -43,4 +50,6 @@ public interface RedisCommands { RedisCommand JSON_ARRINSERT = new RedisCommand<>("JSON.ARRINSERT", new LongReplayConvertor()); RedisCommand JSON_ARRINDEX = new RedisCommand<>("JSON.ARRINDEX", new LongReplayConvertor()); RedisCommand JSON_ARRPOP = new RedisCommand<>("JSON.ARRPOP", new ObjectDecoder(new StringDataDecoder())); + RedisCommand JSON_OBJLEN = new RedisCommand<>("JSON.OBJLEN", new LongReplayConvertor()); + RedisCommand> JSON_OBJKEYS = new RedisCommand("JSON.OBJKEYS", new ObjectListReplayDecoder<>()); } diff --git a/redisjson/src/test/java/io/github/dengliming/redismodule/redisjson/RedisJSONTest.java b/redisjson/src/test/java/io/github/dengliming/redismodule/redisjson/RedisJSONTest.java index 7a5b6ee..3e93474 100644 --- a/redisjson/src/test/java/io/github/dengliming/redismodule/redisjson/RedisJSONTest.java +++ b/redisjson/src/test/java/io/github/dengliming/redismodule/redisjson/RedisJSONTest.java @@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -144,4 +145,35 @@ public void testArr() { assertThat(redisJSON.arrPop(key, ".names", String.class, 0)).isEqualTo("wangwu"); assertThat(redisJSON.arrLen(key, ".names")).isEqualTo(1); } + + @Test + public void testObjLen() { + RedisJSON redisJSON = getRedisJSON(); + String key = "foo"; + Map m = new HashMap<>(); + m.put("id", 1); + m.put("names", new ArrayList<>()); + assertThat(redisJSON.set(key, SetArgs.Builder.create(".", GsonUtils.toJson(m)))).isEqualTo("OK"); + + assertThat(redisJSON.objLen(key, ".")).isEqualTo(2); + assertThat(redisJSON.objLen("not exist", ".")).isEqualTo(0); + } + + @Test + public void testObjKeys() { + RedisJSON redisJSON = getRedisJSON(); + String key = "foo"; + Map m = new HashMap<>(); + m.put("a", Arrays.asList(3)); + Map nestedMap = new HashMap<>(); + Map a = new HashMap<>(); + a.put("b", 2); + a.put("c", 1); + nestedMap.put("a", a); + m.put("nested", nestedMap); + assertThat(redisJSON.set(key, SetArgs.Builder.create(".", GsonUtils.toJson(m)))).isEqualTo("OK"); + + assertThat(redisJSON.objKeys(key, ".")).containsExactly("a", "nested"); + assertThat(redisJSON.objKeys(key, "$..a")).containsExactly(null, Arrays.asList("b", "c")); + } } From c3bf0f41ddaf3d8b7cfd99a9460357a97706db87 Mon Sep 17 00:00:00 2001 From: dengliming Date: Sun, 16 Jan 2022 00:39:03 +0800 Subject: [PATCH 2/3] Update README.md --- redisjson/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redisjson/README.md b/redisjson/README.md index 0be0700..011a64f 100644 --- a/redisjson/README.md +++ b/redisjson/README.md @@ -19,7 +19,7 @@ JSON.ARRINSERT | RedisJSON.
arrInsert()
arrInsertAsync() | JSON.ARRLEN | RedisJSON.
arrLen()
arrLenAsync() | JSON.ARRPOP | RedisJSON.
arrPop()
arrPopAsync() | JSON.ARRTRIM | RedisJSON.
arrTrim()
arrTrimAsync() | -JSON.OBJKEYS | N/A | +JSON.OBJKEYS | RedisJSON.
objKeys()
objKeysAsync() | JSON.OBJLEN | RedisJSON.
objLen()
objLenAsync() | JSON.DEBUG | N/A | JSON.FORGET | N/A | From cebf6edb37069498e980520cbf2c16d612807a4d Mon Sep 17 00:00:00 2001 From: dengliming Date: Sun, 16 Jan 2022 00:45:09 +0800 Subject: [PATCH 3/3] checkstyle --- .../redismodule/redisjson/protocol/RedisCommands.java | 5 ----- .../dengliming/redismodule/redisjson/RedisJSONTest.java | 5 +++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/protocol/RedisCommands.java b/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/protocol/RedisCommands.java index e8cadd1..daa782c 100644 --- a/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/protocol/RedisCommands.java +++ b/redisjson/src/main/java/io/github/dengliming/redismodule/redisjson/protocol/RedisCommands.java @@ -17,15 +17,10 @@ package io.github.dengliming.redismodule.redisjson.protocol; import org.redisson.client.protocol.RedisCommand; -import org.redisson.client.protocol.convertor.BooleanReplayConvertor; import org.redisson.client.protocol.convertor.LongReplayConvertor; -import org.redisson.client.protocol.convertor.StringToListConvertor; -import org.redisson.client.protocol.decoder.ListMultiDecoder2; -import org.redisson.client.protocol.decoder.MultiDecoder; import org.redisson.client.protocol.decoder.ObjectDecoder; import org.redisson.client.protocol.decoder.ObjectListReplayDecoder; import org.redisson.client.protocol.decoder.StringDataDecoder; -import org.redisson.client.protocol.decoder.StringListReplayDecoder; import org.redisson.client.protocol.decoder.StringReplayDecoder; import java.util.List; diff --git a/redisjson/src/test/java/io/github/dengliming/redismodule/redisjson/RedisJSONTest.java b/redisjson/src/test/java/io/github/dengliming/redismodule/redisjson/RedisJSONTest.java index 3e93474..ef6a21d 100644 --- a/redisjson/src/test/java/io/github/dengliming/redismodule/redisjson/RedisJSONTest.java +++ b/redisjson/src/test/java/io/github/dengliming/redismodule/redisjson/RedisJSONTest.java @@ -161,8 +161,6 @@ public void testObjLen() { @Test public void testObjKeys() { - RedisJSON redisJSON = getRedisJSON(); - String key = "foo"; Map m = new HashMap<>(); m.put("a", Arrays.asList(3)); Map nestedMap = new HashMap<>(); @@ -171,6 +169,9 @@ public void testObjKeys() { a.put("c", 1); nestedMap.put("a", a); m.put("nested", nestedMap); + + RedisJSON redisJSON = getRedisJSON(); + String key = "foo"; assertThat(redisJSON.set(key, SetArgs.Builder.create(".", GsonUtils.toJson(m)))).isEqualTo("OK"); assertThat(redisJSON.objKeys(key, ".")).containsExactly("a", "nested");