Skip to content

Commit

Permalink
Support JSON.DEBUG MEMORY command (#3002)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 authored May 26, 2022
1 parent 5ef56bd commit 0066b06
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -3378,6 +3378,18 @@ public final CommandObject<List<Long>> jsonArrTrim(String key, Path2 path, int s
public final CommandObject<Long> jsonArrTrim(String key, Path path, int start, int stop) {
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<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> jsonDebugMemory(String key, Path path) {
return new CommandObject<>(commandArguments(JsonCommand.DEBUG).add("MEMORY").key(key).add(path), BuilderFactory.LONG);
}
// RedisJSON commands

// RedisTimeSeries commands
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3783,6 +3783,21 @@ public List<Long> jsonArrTrim(String key, Path2 path, int start, int stop) {
public Long jsonArrTrim(String key, Path path, int start, int stop) {
return executeCommand(commandObjects.jsonArrTrim(key, path, start, stop));
}

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

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

@Override
public long jsonDebugMemory(String key, Path path) {
return executeCommand(commandObjects.jsonDebugMemory(key, path));
}
// RedisJSON commands

// RedisTimeSeries commands
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/redis/clients/jedis/json/JsonProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public enum JsonCommand implements ProtocolCommand {
ARRPOP("JSON.ARRPOP"),
ARRTRIM("JSON.ARRTRIM"),
CLEAR("JSON.CLEAR"),
TOGGLE("JSON.TOGGLE");
TOGGLE("JSON.TOGGLE"),
DEBUG("JSON.DEBUG");

private final byte[] raw;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;

public interface RedisJsonCommands {

Expand Down Expand Up @@ -141,4 +140,10 @@ default <T> List<T> jsonMGet(Class<T> clazz, String... keys) {
List<Long> jsonArrTrim(String key, Path2 path, int start, int stop);

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

long jsonDebugMemory(String key);

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

long jsonDebugMemory(String key, Path path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,18 @@ public void strLen() {
assertEquals(Long.valueOf(3), client.jsonStrLen("str"));
assertEquals(Long.valueOf(3), client.jsonStrLen("str", ROOT_PATH));
}

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

String json = "{ foo: 'bar', bar: { foo: 10 }}";
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
client.jsonSet("json", ROOT_PATH, jsonObject);
// it is okay as long as any 'long' is returned
client.jsonDebugMemory("json");
client.jsonDebugMemory("json", ROOT_PATH);
client.jsonDebugMemory("json", Path.of(".bar"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,19 @@ public void strLen() {
assertEquals(singletonList(6L), client.jsonStrLen("str", ROOT_PATH));
}

@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 }}"));
// it is okay as long as any 'long' is returned
client.jsonDebugMemory("json");
assertEquals(1, client.jsonDebugMemory("json", ROOT_PATH).size());
assertEquals(2, client.jsonDebugMemory("json", Path2.of("$..foo")).size());
assertEquals(1, client.jsonDebugMemory("json", Path2.of("$..bar")).size());
}

private void assertJsonArrayEquals(JSONArray a, Object _b) {
if (!(_b instanceof JSONArray)) {
fail("Actual value is not JSONArray.");
Expand Down

0 comments on commit 0066b06

Please sign in to comment.