Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for graph.RO_QUERY command #105

Merged
merged 13 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 11 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
38 changes: 38 additions & 0 deletions src/main/java/com/redislabs/redisgraph/RedisGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public interface RedisGraph extends Closeable {
*/
ResultSet query(String graphId, String query);

/**
* Execute a Cypher read-only query.
* @param graphId a graph to perform the query on
* @param query Cypher query
* @return a result set
*/
ResultSet readOnlyQuery(String graphId, String query);

/**
* Execute a Cypher query with timeout.
* @param graphId a graph to perform the query on
Expand All @@ -23,6 +31,16 @@ public interface RedisGraph extends Closeable {
*/
ResultSet query(String graphId, String query, long timeout);

/**
* Execute a Cypher read-only query with timeout.
* @param graphId a graph to perform the query on
* @param query Cypher query
* @param timeout
* @return a result set
*/
ResultSet readOnlyQuery(String graphId, String query, long timeout);


/**
* Execute a Cypher query with arguments
* @param graphId a graph to perform the query on
Expand All @@ -34,6 +52,7 @@ public interface RedisGraph extends Closeable {
@Deprecated
ResultSet query(String graphId, String query, Object ...args);


/**
* Executes a cypher query with parameters.
* @param graphId a graph to perform the query on.
Expand All @@ -43,6 +62,15 @@ public interface RedisGraph extends Closeable {
*/
ResultSet query(String graphId, String query, Map<String, Object> params);

/**
* Executes a cypher read-only query with parameters.
* @param graphId a graph to perform the query on.
* @param query Cypher query.
* @param params parameters map.
* @return a result set.
*/
ResultSet readOnlyQuery(String graphId, String query, Map<String, Object> params);

/**
* Executes a cypher query with parameters and timeout.
* @param graphId a graph to perform the query on.
Expand All @@ -53,6 +81,16 @@ public interface RedisGraph extends Closeable {
*/
ResultSet query(String graphId, String query, Map<String, Object> params, long timeout);

/**
* Executes a cypher read-only query with parameters and timeout.
* @param graphId a graph to perform the query on.
* @param query Cypher query.
* @param params parameters map.
* @param timeout
* @return a result set.
*/
ResultSet readOnlyQuery(String graphId, String query, Map<String, Object> params, long timeout);

/**
* Invokes stored procedures without arguments
* @param graphId a graph to perform the query on
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/redislabs/redisgraph/RedisGraphTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public interface RedisGraphTransaction extends
*/
Response<ResultSet> query(String graphId, String query);

/**
* Execute a Cypher read-only query.
* @param graphId a graph to perform the query on
* @param query Cypher query
* @return a response which builds the result set with the query answer.
*/
Response<ResultSet> readOnlyQuery(String graphId, String query);

/**
* Execute a Cypher query with timeout.
* @param graphId a graph to perform the query on
Expand All @@ -40,6 +48,15 @@ public interface RedisGraphTransaction extends
*/
Response<ResultSet> query(String graphId, String query, long timeout);

/**
* Execute a Cypher read-only query with timeout.
* @param graphId a graph to perform the query on
* @param query Cypher query
* @param timeout
* @return a response which builds the result set with the query answer.
*/
Response<ResultSet> readOnlyQuery(String graphId, String query, long timeout);

/**
* Execute a Cypher query with arguments
* @param graphId a graph to perform the query on
Expand All @@ -60,6 +77,15 @@ public interface RedisGraphTransaction extends
*/
Response<ResultSet> query(String graphId, String query, Map<String, Object> params);

/**
* Executes a cypher read-only query with parameters.
* @param graphId a graph to perform the query on.
* @param query Cypher query.
* @param params parameters map.
* @return a response which builds the result set with the query answer.
*/
Response<ResultSet> readOnlyQuery(String graphId, String query, Map<String, Object> params);

/**
* Executes a cypher query with parameters and timeout.
* @param graphId a graph to perform the query on.
Expand All @@ -70,6 +96,16 @@ public interface RedisGraphTransaction extends
*/
Response<ResultSet> query(String graphId, String query, Map<String, Object> params, long timeout);

/**
* Executes a cypher read-only query with parameters and timeout.
* @param graphId a graph to perform the query on.
* @param query Cypher query.
* @param params parameters map.
* @param timeout
* @return a response which builds the result set with the query answer.
*/
Response<ResultSet> readOnlyQuery(String graphId, String query, Map<String, Object> params, long timeout);

/**
* Invokes stored procedures without arguments
* @param graphId a graph to perform the query on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public abstract class AbstractRedisGraph implements RedisGraph {
*/
protected abstract ResultSet sendQuery(String graphId, String preparedQuery);

/**
* Sends a read-only query to the redis graph. Implementation and context dependent
* @param graphId graph to be queried
* @param preparedQuery prepared query
* @return Result set
*/
protected abstract ResultSet sendReadOnlyQuery(String graphId, String preparedQuery);

/**
* Sends a query to the redis graph.Implementation and context dependent
* @param graphId graph to be queried
Expand All @@ -36,6 +44,15 @@ public abstract class AbstractRedisGraph implements RedisGraph {
*/
protected abstract ResultSet sendQuery(String graphId, String preparedQuery, long timeout);

/**
* Sends a read-query to the redis graph.Implementation and context dependent
* @param graphId graph to be queried
* @param preparedQuery prepared query
* @param timeout
* @return Result set
*/
protected abstract ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout);

/**
* Execute a Cypher query.
* @param graphId a graph to perform the query on
Expand All @@ -46,6 +63,16 @@ public ResultSet query(String graphId, String query) {
return sendQuery(graphId, query);
}

/**
* Execute a Cypher read-only query.
* @param graphId a graph to perform the query on
* @param query Cypher query
* @return a result set
*/
public ResultSet readOnlyQuery(String graphId, String query) {
return sendReadOnlyQuery(graphId, query);
}

/**
* Execute a Cypher query with timeout.
* @param graphId a graph to perform the query on
Expand All @@ -58,6 +85,18 @@ public ResultSet query(String graphId, String query, long timeout) {
return sendQuery(graphId, query, timeout);
}

/**
* Execute a Cypher read-only query with timeout.
* @param graphId a graph to perform the query on
* @param timeout
* @param query Cypher query
* @return a result set
*/
@Override
public ResultSet readOnlyQuery(String graphId, String query, long timeout) {
return sendReadOnlyQuery(graphId, query, timeout);
}

/**
* Execute a Cypher query with arguments
* @param graphId a graph to perform the query on
Expand All @@ -84,6 +123,18 @@ public ResultSet query(String graphId, String query, Map<String, Object> params)
return sendQuery(graphId, preparedQuery);
}

/**
* Executes a cypher read-only query with parameters.
* @param graphId a graph to perform the query on.
* @param query Cypher query.
* @param params parameters map.
* @return a result set.
*/
public ResultSet readOnlyQuery(String graphId, String query, Map<String, Object> params) {
String preparedQuery = Utils.prepareQuery(query, params);
return sendReadOnlyQuery(graphId, preparedQuery);
}

/**
* Executes a cypher query with parameters and timeout.
* @param graphId a graph to perform the query on.
Expand All @@ -98,6 +149,20 @@ public ResultSet query(String graphId, String query, Map<String, Object> params,
return sendQuery(graphId, preparedQuery, timeout);
}

/**
* Executes a cypher read-only query with parameters and timeout.
* @param graphId a graph to perform the query on.
* @param timeout
* @param query Cypher query.
* @param params parameters map.
* @return a result set.
*/
@Override
public ResultSet readOnlyQuery(String graphId, String query, Map<String, Object> params, long timeout) {
String preparedQuery = Utils.prepareQuery(query, params);
return sendReadOnlyQuery(graphId, preparedQuery, timeout);
}

public ResultSet callProcedure(String graphId, String procedure){
return callProcedure(graphId, procedure, Utils.DUMMY_LIST, Utils.DUMMY_MAP);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ protected ResultSet sendQuery(String graphId, String preparedQuery) {
}
}

/**
* Sends the read-only query over the instance only connection
* @param graphId graph to be queried
* @param preparedQuery prepared query
* @return Result set with the query answer
*/
@Override
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery) {
Jedis conn = getConnection();
try {
List<Object> rawResponse = (List<Object>) conn.sendCommand(RedisGraphCommand.RO_QUERY, graphId, preparedQuery, Utils.COMPACT_STRING);
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
}
catch (JRedisGraphRunTimeException rt) {
throw rt;
}
catch (JedisDataException j) {
throw new JRedisGraphCompileTimeException(j);
}
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Sends the query over the instance only connection
* @param graphId graph to be queried
Expand All @@ -82,6 +103,29 @@ protected ResultSet sendQuery(String graphId, String preparedQuery, long timeout
}
}

/**
* Sends the read-only query over the instance only connection
* @param graphId graph to be queried
* @param timeout
* @param preparedQuery prepared query
* @return Result set with the query answer
*/
@Override
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout) {
Jedis conn = getConnection();
try {
List<Object> rawResponse = (List<Object>) conn.sendBlockingCommand(RedisGraphCommand.RO_QUERY,
graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout));
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
}
catch (JRedisGraphRunTimeException rt) {
throw rt;
}
catch (JedisDataException j) {
throw new JRedisGraphCompileTimeException(j);
}
}

/**
* @return Returns the instance Jedis connection.
*/
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/redislabs/redisgraph/impl/api/RedisGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ protected ResultSet sendQuery(String graphId, String preparedQuery){
}
}

/**
* Overrides the abstract function.
* Sends the read-only query from any Jedis connection received from the Jedis pool and closes it once done
* @param graphId graph to be queried
* @param preparedQuery prepared query
* @return Result set with the query answer
*/
@Override
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery){
try (ContextedRedisGraph contextedRedisGraph = new ContextedRedisGraph(getConnection())) {
contextedRedisGraph.setRedisGraphCaches(caches);
return contextedRedisGraph.sendReadOnlyQuery(graphId, preparedQuery);
}
}

/**
* Overrides the abstract function.
* Sends the query from any Jedis connection received from the Jedis pool and closes it once done
Expand All @@ -85,6 +100,22 @@ protected ResultSet sendQuery(String graphId, String preparedQuery, long timeout
}
}

/**
* Overrides the abstract function.
* Sends the read-only query from any Jedis connection received from the Jedis pool and closes it once done
* @param graphId graph to be queried
* @param preparedQuery prepared query
* @param timeout
* @return Result set with the query answer
*/
@Override
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout){
try (ContextedRedisGraph contextedRedisGraph = new ContextedRedisGraph(getConnection())) {
contextedRedisGraph.setRedisGraphCaches(caches);
return contextedRedisGraph.sendReadOnlyQuery(graphId, preparedQuery, timeout);
}
}

/**
* Closes the Jedis pool
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
public enum RedisGraphCommand implements ProtocolCommand {
QUERY("graph.QUERY"),
RO_QUERY("graph.RO_QUERY"),
DELETE("graph.DELETE");

private final byte[] raw;
Expand Down
Loading