Skip to content

Commit

Permalink
Support JSON.OBJLEN and JSON.OBJKEYS commands (#3051)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 authored Jun 28, 2022
1 parent ba63b5d commit bf0d815
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 38 deletions.
46 changes: 17 additions & 29 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,59 +312,34 @@ public String build(Object data) {
public String toString() {
return "String";
}

};

public static final Builder<List<String>> STRING_LIST = new Builder<List<String>>() {
@Override
@SuppressWarnings("unchecked")
public List<String> build(Object data) {
if (null == data) {
return null;
}
List<byte[]> l = (List<byte[]>) data;
final ArrayList<String> result = new ArrayList<>(l.size());
for (final byte[] barray : l) {
if (barray == null) {
result.add(null);
} else {
result.add(SafeEncoder.encode(barray));
}
}
return result;
if (null == data) return null;
return ((List<Object>) data).stream().map(STRING::build).collect(Collectors.toList());
}

@Override
public String toString() {
return "List<String>";
}

};

public static final Builder<Set<String>> STRING_SET = new Builder<Set<String>>() {
@Override
@SuppressWarnings("unchecked")
public Set<String> build(Object data) {
if (null == data) {
return null;
}
List<byte[]> l = (List<byte[]>) data;
final Set<String> result = new HashSet<>(l.size(), 1);
for (final byte[] barray : l) {
if (barray == null) {
result.add(null);
} else {
result.add(SafeEncoder.encode(barray));
}
}
return result;
if (null == data) return null;
return ((List<Object>) data).stream().map(STRING::build).collect(Collectors.toSet());
}

@Override
public String toString() {
return "Set<String>";
}

};

public static final Builder<Map<String, String>> STRING_MAP = new Builder<Map<String, String>>() {
Expand All @@ -385,7 +360,20 @@ public Map<String, String> build(Object data) {
public String toString() {
return "Map<String, String>";
}
};

public static final Builder<List<List<String>>> STRING_LIST_LIST = new Builder<List<List<String>>>() {
@Override
@SuppressWarnings("unchecked")
public List<List<String>> build(Object data) {
if (null == data) return null;
return ((List<Object>) data).stream().map(STRING_LIST::build).collect(Collectors.toList());
}

@Override
public String toString() {
return "List<List<String>>";
}
};

public static final Builder<KeyedListElement> KEYED_LIST_ELEMENT = new Builder<KeyedListElement>() {
Expand Down
32 changes: 28 additions & 4 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -3462,17 +3462,41 @@ public final CommandObject<Long> jsonArrTrim(String key, Path path, int start, i
return new CommandObject<>(commandArguments(JsonCommand.ARRTRIM).key(key).add(path).add(start).add(stop), BuilderFactory.LONG);
}

public final CommandObject<Long> jsonDebugMemory(String key) {
return new CommandObject<>(commandArguments(JsonCommand.DEBUG).add("MEMORY").key(key), BuilderFactory.LONG);
public final CommandObject<Long> jsonObjLen(String key) {
return new CommandObject<>(commandArguments(JsonCommand.OBJLEN).key(key), BuilderFactory.LONG);
}

public final CommandObject<List<Long>> jsonDebugMemory(String key, Path2 path) {
return new CommandObject<>(commandArguments(JsonCommand.DEBUG).add("MEMORY").key(key).add(path), BuilderFactory.LONG_LIST);
public final CommandObject<Long> jsonObjLen(String key, Path path) {
return new CommandObject<>(commandArguments(JsonCommand.OBJLEN).key(key).add(path), BuilderFactory.LONG);
}

public final CommandObject<List<Long>> jsonObjLen(String key, Path2 path) {
return new CommandObject<>(commandArguments(JsonCommand.OBJLEN).key(key).add(path), BuilderFactory.LONG_LIST);
}

public final CommandObject<List<String>> jsonObjKeys(String key) {
return new CommandObject<>(commandArguments(JsonCommand.OBJKEYS).key(key), BuilderFactory.STRING_LIST);
}

public final CommandObject<List<String>> jsonObjKeys(String key, Path path) {
return new CommandObject<>(commandArguments(JsonCommand.OBJKEYS).key(key).add(path), BuilderFactory.STRING_LIST);
}

public final CommandObject<List<List<String>>> jsonObjKeys(String key, Path2 path) {
return new CommandObject<>(commandArguments(JsonCommand.OBJKEYS).key(key).add(path), BuilderFactory.STRING_LIST_LIST);
}

public final CommandObject<Long> jsonDebugMemory(String key) {
return new CommandObject<>(commandArguments(JsonCommand.DEBUG).add("MEMORY").key(key), BuilderFactory.LONG);
}

public final CommandObject<Long> jsonDebugMemory(String key, Path path) {
return new CommandObject<>(commandArguments(JsonCommand.DEBUG).add("MEMORY").key(key).add(path), BuilderFactory.LONG);
}

public final CommandObject<List<Long>> jsonDebugMemory(String key, Path2 path) {
return new CommandObject<>(commandArguments(JsonCommand.DEBUG).add("MEMORY").key(key).add(path), BuilderFactory.LONG_LIST);
}
// RedisJSON commands

// RedisTimeSeries commands
Expand Down
34 changes: 32 additions & 2 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3879,18 +3879,48 @@ public Long jsonArrTrim(String key, Path path, int start, int stop) {
return executeCommand(commandObjects.jsonArrTrim(key, path, start, stop));
}

@Override
public Long jsonObjLen(String key) {
return executeCommand(commandObjects.jsonObjLen(key));
}

@Override
public Long jsonObjLen(String key, Path path) {
return executeCommand(commandObjects.jsonObjLen(key, path));
}

@Override
public List<Long> jsonObjLen(String key, Path2 path) {
return executeCommand(commandObjects.jsonObjLen(key, path));
}

@Override
public List<String> jsonObjKeys(String key) {
return executeCommand(commandObjects.jsonObjKeys(key));
}

@Override
public List<String> jsonObjKeys(String key, Path path) {
return executeCommand(commandObjects.jsonObjKeys(key, path));
}

@Override
public List<List<String>> jsonObjKeys(String key, Path2 path) {
return executeCommand(commandObjects.jsonObjKeys(key, path));
}

@Override
public long jsonDebugMemory(String key) {
return executeCommand(commandObjects.jsonDebugMemory(key));
}

@Override
public List<Long> jsonDebugMemory(String key, Path2 path) {
public long jsonDebugMemory(String key, Path path) {
return executeCommand(commandObjects.jsonDebugMemory(key, path));
}

@Override
public long jsonDebugMemory(String key, Path path) {
public List<Long> jsonDebugMemory(String key, Path2 path) {
return executeCommand(commandObjects.jsonDebugMemory(key, path));
}
// RedisJSON commands
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/redis/clients/jedis/json/JsonProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public enum JsonCommand implements ProtocolCommand {
ARRTRIM("JSON.ARRTRIM"),
CLEAR("JSON.CLEAR"),
TOGGLE("JSON.TOGGLE"),
OBJKEYS("JSON.OBJKEYS"),
OBJLEN("JSON.OBJLEN"),
DEBUG("JSON.DEBUG");

private final byte[] raw;
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/redis/clients/jedis/json/RedisJsonCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,21 @@ default <T> List<T> jsonMGet(Class<T> clazz, String... keys) {

Long jsonArrTrim(String key, Path path, int start, int stop);

long jsonDebugMemory(String key);
Long jsonObjLen(String key);

List<Long> jsonDebugMemory(String key, Path2 path);
Long jsonObjLen(String key, Path path);

List<Long> jsonObjLen(String key, Path2 path);

List<String> jsonObjKeys(String key);

List<String> jsonObjKeys(String key, Path path);

List<List<String>> jsonObjKeys(String key, Path2 path);

long jsonDebugMemory(String key);

long jsonDebugMemory(String key, Path path);

List<Long> jsonDebugMemory(String key, Path2 path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -424,6 +425,21 @@ public void numIncrBy() {
assertEquals(5d, client.jsonNumIncrBy("doc", Path.of(".a"), 2), 0d);
}

@Test
public void obj() {
assertNull(client.jsonObjLen("doc"));
assertNull(client.jsonObjKeys("doc"));
assertNull(client.jsonObjLen("doc", ROOT_PATH));
assertNull(client.jsonObjKeys("doc", ROOT_PATH));

String json = "{\"a\":[3], \"nested\": {\"a\": {\"b\":2, \"c\": 1}}}";
client.jsonSetWithPlainString("doc", ROOT_PATH, json);
assertEquals(Long.valueOf(2), client.jsonObjLen("doc"));
assertEquals(Arrays.asList("a", "nested"), client.jsonObjKeys("doc"));
assertEquals(Long.valueOf(2), client.jsonObjLen("doc", Path.of(".nested.a")));
assertEquals(Arrays.asList("b", "c"), client.jsonObjKeys("doc", Path.of(".nested.a")));
}

@Test
public void debugMemory() {
assertEquals(0L, client.jsonDebugMemory("json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static redis.clients.jedis.modules.json.JsonObjects.*;

import com.google.gson.Gson;
import java.util.Arrays;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
Expand Down Expand Up @@ -386,9 +387,18 @@ public void numIncrBy() {
assertJsonArrayEquals(jsonArray(), client.jsonNumIncrBy("doc", Path2.of("..c"), 0d));
}

@Test
public void obj() {
String json = "{\"a\":[3], \"nested\": {\"a\": {\"b\":2, \"c\": 1}}}";
client.jsonSet("doc", ROOT_PATH, json);
assertEquals(Arrays.asList(2L), client.jsonObjLen("doc", ROOT_PATH));
assertEquals(Arrays.asList(Arrays.asList("a", "nested")), client.jsonObjKeys("doc", ROOT_PATH));
assertEquals(Arrays.asList(null, 2L), client.jsonObjLen("doc", Path2.of("..a")));
assertEquals(Arrays.asList(null, Arrays.asList("b", "c")), client.jsonObjKeys("doc", Path2.of("..a")));
}

@Test
public void debugMemory() {
assertEquals(0L, client.jsonDebugMemory("json"));
assertEquals(emptyList(), client.jsonDebugMemory("json", ROOT_PATH));

client.jsonSet("json", new JSONObject("{ foo: 'bar', bar: { foo: 10 }}"));
Expand Down

0 comments on commit bf0d815

Please sign in to comment.