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
34 changes: 12 additions & 22 deletions src/main/java/com/redislabs/modules/rejson/JReJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ private enum Command implements ProtocolCommand {
ARRLEN("JSON.ARRLEN"),
ARRPOP("JSON.ARRPOP"),
ARRTRIM("JSON.ARRTRIM");

private final byte[] raw;

Command(String alt) {
raw = SafeEncoder.encode(alt);
}

@Override
public byte[] getRaw() {
return raw;
}
Expand All @@ -82,12 +84,14 @@ public enum ExistenceModifier implements ProtocolCommand {
DEFAULT(""),
NOT_EXISTS("NX"),
MUST_EXIST("XX");

private final byte[] raw;

ExistenceModifier(String alt) {
raw = SafeEncoder.encode(alt);
}

@Override
public byte[] getRaw() {
return raw;
}
Expand Down Expand Up @@ -121,23 +125,14 @@ public JReJSON(Pool<Jedis> jedis) {
this.client = jedis;
}

/**
* Helper to check for errors and throw them as an exception
* @param str the reply string to "analyze"
* @throws RuntimeException
*/
private static void assertReplyNotError(final String str) {
if (str.startsWith("-ERR"))
throw new RuntimeException(str.substring(5));
}

/**
* Helper to check for an OK reply
* @param str the reply string to "scrutinize"
*/
private static void assertReplyOK(final String str) {
if (!str.equals("OK"))
throw new RuntimeException(str);
if (str == null) {
throw new NullPointerException("Null response received.");
}
}

/**
Expand Down Expand Up @@ -227,7 +222,7 @@ public <T> T get(String key, Class<T> clazz, Path... paths) {
conn.getClient().sendCommand(Command.GET, args);
rep = conn.getClient().getBulkReply();
}
assertReplyNotError(rep);

return gson.fromJson(rep, clazz);
}

Expand Down Expand Up @@ -356,8 +351,6 @@ public Class<?> type(String key, Path path) {
rep = conn.getClient().getBulkReply();
}

assertReplyNotError(rep);

switch (rep) {
case "null":
return null;
Expand All @@ -374,7 +367,7 @@ public Class<?> type(String key, Path path) {
case "array":
return List.class;
default:
throw new java.lang.RuntimeException(rep);
throw new RuntimeException(rep);
}
}

Expand Down Expand Up @@ -425,7 +418,6 @@ public static Object get(Jedis conn, String key, Path... paths) {
String rep = conn.getClient().getBulkReply();
conn.close();

assertReplyNotError(rep);
return gson.fromJson(rep, Object.class);
}

Expand Down Expand Up @@ -492,8 +484,6 @@ public static Class<?> type(Jedis conn, String key, Path... path) {
String rep = conn.getClient().getBulkReply();
conn.close();

assertReplyNotError(rep);

switch (rep) {
case "null":
return null;
Expand All @@ -510,7 +500,7 @@ public static Class<?> type(Jedis conn, String key, Path... path) {
case "array":
return List.class;
default:
throw new java.lang.RuntimeException(rep);
throw new RuntimeException(rep);
}
}

Expand All @@ -533,7 +523,7 @@ private Jedis getConnection() {
public Long strAppend(String key, Path path, Object... objects) {
List<byte[]> args = new ArrayList<>();
args.add(SafeEncoder.encode(key));
args.add(SafeEncoder.encode(getSingleOptionalPath(path).toString()));
args.add(SafeEncoder.encode(path.toString()));

args.addAll(Arrays.stream(objects) //
.map(object -> SafeEncoder.encode(gson.toJson(object))) //
Expand Down Expand Up @@ -698,7 +688,7 @@ public <T> T arrPop(String key, Class<T> clazz, Path path, Long index) {
conn.getClient().sendCommand(Command.ARRPOP, args.toArray(new byte[args.size()][]));
rep = conn.getClient().getBulkReply();
}
assertReplyNotError(rep);

return gson.fromJson(rep, clazz);
}

Expand Down
Loading