Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions redisjson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ JSON.ARRINSERT | RedisJSON.<br/>arrInsert()<br/>arrInsertAsync() |
JSON.ARRLEN | RedisJSON.<br/>arrLen()<br/>arrLenAsync() |
JSON.ARRPOP | RedisJSON.<br/>arrPop()<br/>arrPopAsync() |
JSON.ARRTRIM | RedisJSON.<br/>arrTrim()<br/>arrTrimAsync() |
JSON.OBJKEYS | N/A |
JSON.OBJLEN | N/A |
JSON.OBJKEYS | RedisJSON.<br/>objKeys()<br/>objKeysAsync() |
JSON.OBJLEN | RedisJSON.<br/>objLen()<br/>objLenAsync() |
JSON.DEBUG | N/A |
JSON.FORGET | N/A |
JSON.RESP | N/A |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -460,6 +462,46 @@ private <T> RPromise transformRPromiseResult(RFuture<String> getFuture, Class<T>
return result;
}

/**
* Report the number of keys in the JSON Object at path in key.
*
* JSON.OBJLEN <key> [path]
*
* @param key
* @param path
* @return
*/
public Long objLen(String key, String path) {
return commandExecutor.get(objLenAsync(key, path));
}

public RFuture<Long> 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 <key> [path]
*
* @param key
* @param path
* @return
*/
public List<Object> objKeys(String key, String path) {
return commandExecutor.get(objKeysAsync(key, path));
}

public RFuture<List<Object>> 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 "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.redisson.client.protocol.decoder.StringDataDecoder;
import org.redisson.client.protocol.decoder.StringReplayDecoder;

import java.util.List;

/**
* @author dengliming
*/
Expand All @@ -43,4 +45,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<List<Object>> JSON_OBJKEYS = new RedisCommand("JSON.OBJKEYS", new ObjectListReplayDecoder<>());
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -144,4 +145,36 @@ 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<String, Object> 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() {
Map<String, Object> m = new HashMap<>();
m.put("a", Arrays.asList(3));
Map<String, Object> nestedMap = new HashMap<>();
Map<String, Object> a = new HashMap<>();
a.put("b", 2);
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");
assertThat(redisJSON.objKeys(key, "$..a")).containsExactly(null, Arrays.asList("b", "c"));
}
}