Skip to content

Commit 12ed23f

Browse files
filipecosta90DvirDukhangkorlandsazzad16
authored
Add support for graph.RO_QUERY command (#105)
* Add support for graph.RO_QUERY command * Apply suggestions from code review Co-authored-by: DvirDukhan <[email protected]> * [fix] Fixes per PR review: removed deprecated read-only variants * [fix] Fixes per PR review: added RO to transactions. * [fix] Fixes per PR review: added RO to transactions. * [add] Added per PR review: included RO transaction test * [fix] Fixed wrong api being used on new test * Use JRedisGraphException Co-authored-by: DvirDukhan <[email protected]> Co-authored-by: Guy Korland <[email protected]> Co-authored-by: M Sazzadul Hoque <[email protected]>
1 parent d84af9a commit 12ed23f

File tree

8 files changed

+425
-1
lines changed

8 files changed

+425
-1
lines changed

src/main/java/com/redislabs/redisgraph/RedisGraph.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ public interface RedisGraph extends Closeable {
1414
*/
1515
ResultSet query(String graphId, String query);
1616

17+
/**
18+
* Execute a Cypher read-only query.
19+
* @param graphId a graph to perform the query on
20+
* @param query Cypher query
21+
* @return a result set
22+
*/
23+
ResultSet readOnlyQuery(String graphId, String query);
24+
1725
/**
1826
* Execute a Cypher query with timeout.
1927
* @param graphId a graph to perform the query on
@@ -23,6 +31,16 @@ public interface RedisGraph extends Closeable {
2331
*/
2432
ResultSet query(String graphId, String query, long timeout);
2533

34+
/**
35+
* Execute a Cypher read-only query with timeout.
36+
* @param graphId a graph to perform the query on
37+
* @param query Cypher query
38+
* @param timeout
39+
* @return a result set
40+
*/
41+
ResultSet readOnlyQuery(String graphId, String query, long timeout);
42+
43+
2644
/**
2745
* Execute a Cypher query with arguments
2846
* @param graphId a graph to perform the query on
@@ -34,6 +52,7 @@ public interface RedisGraph extends Closeable {
3452
@Deprecated
3553
ResultSet query(String graphId, String query, Object ...args);
3654

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

65+
/**
66+
* Executes a cypher read-only query with parameters.
67+
* @param graphId a graph to perform the query on.
68+
* @param query Cypher query.
69+
* @param params parameters map.
70+
* @return a result set.
71+
*/
72+
ResultSet readOnlyQuery(String graphId, String query, Map<String, Object> params);
73+
4674
/**
4775
* Executes a cypher query with parameters and timeout.
4876
* @param graphId a graph to perform the query on.
@@ -53,6 +81,16 @@ public interface RedisGraph extends Closeable {
5381
*/
5482
ResultSet query(String graphId, String query, Map<String, Object> params, long timeout);
5583

84+
/**
85+
* Executes a cypher read-only query with parameters and timeout.
86+
* @param graphId a graph to perform the query on.
87+
* @param query Cypher query.
88+
* @param params parameters map.
89+
* @param timeout
90+
* @return a result set.
91+
*/
92+
ResultSet readOnlyQuery(String graphId, String query, Map<String, Object> params, long timeout);
93+
5694
/**
5795
* Invokes stored procedures without arguments
5896
* @param graphId a graph to perform the query on

src/main/java/com/redislabs/redisgraph/RedisGraphTransaction.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public interface RedisGraphTransaction extends
3131
*/
3232
Response<ResultSet> query(String graphId, String query);
3333

34+
/**
35+
* Execute a Cypher read-only query.
36+
* @param graphId a graph to perform the query on
37+
* @param query Cypher query
38+
* @return a response which builds the result set with the query answer.
39+
*/
40+
Response<ResultSet> readOnlyQuery(String graphId, String query);
41+
3442
/**
3543
* Execute a Cypher query with timeout.
3644
* @param graphId a graph to perform the query on
@@ -40,6 +48,15 @@ public interface RedisGraphTransaction extends
4048
*/
4149
Response<ResultSet> query(String graphId, String query, long timeout);
4250

51+
/**
52+
* Execute a Cypher read-only query with timeout.
53+
* @param graphId a graph to perform the query on
54+
* @param query Cypher query
55+
* @param timeout
56+
* @return a response which builds the result set with the query answer.
57+
*/
58+
Response<ResultSet> readOnlyQuery(String graphId, String query, long timeout);
59+
4360
/**
4461
* Execute a Cypher query with arguments
4562
* @param graphId a graph to perform the query on
@@ -60,6 +77,15 @@ public interface RedisGraphTransaction extends
6077
*/
6178
Response<ResultSet> query(String graphId, String query, Map<String, Object> params);
6279

80+
/**
81+
* Executes a cypher read-only query with parameters.
82+
* @param graphId a graph to perform the query on.
83+
* @param query Cypher query.
84+
* @param params parameters map.
85+
* @return a response which builds the result set with the query answer.
86+
*/
87+
Response<ResultSet> readOnlyQuery(String graphId, String query, Map<String, Object> params);
88+
6389
/**
6490
* Executes a cypher query with parameters and timeout.
6591
* @param graphId a graph to perform the query on.
@@ -70,6 +96,16 @@ public interface RedisGraphTransaction extends
7096
*/
7197
Response<ResultSet> query(String graphId, String query, Map<String, Object> params, long timeout);
7298

99+
/**
100+
* Executes a cypher read-only query with parameters and timeout.
101+
* @param graphId a graph to perform the query on.
102+
* @param query Cypher query.
103+
* @param params parameters map.
104+
* @param timeout
105+
* @return a response which builds the result set with the query answer.
106+
*/
107+
Response<ResultSet> readOnlyQuery(String graphId, String query, Map<String, Object> params, long timeout);
108+
73109
/**
74110
* Invokes stored procedures without arguments
75111
* @param graphId a graph to perform the query on

src/main/java/com/redislabs/redisgraph/impl/api/AbstractRedisGraph.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public abstract class AbstractRedisGraph implements RedisGraph {
2727
*/
2828
protected abstract ResultSet sendQuery(String graphId, String preparedQuery);
2929

30+
/**
31+
* Sends a read-only query to the redis graph. Implementation and context dependent
32+
* @param graphId graph to be queried
33+
* @param preparedQuery prepared query
34+
* @return Result set
35+
*/
36+
protected abstract ResultSet sendReadOnlyQuery(String graphId, String preparedQuery);
37+
3038
/**
3139
* Sends a query to the redis graph.Implementation and context dependent
3240
* @param graphId graph to be queried
@@ -36,6 +44,15 @@ public abstract class AbstractRedisGraph implements RedisGraph {
3644
*/
3745
protected abstract ResultSet sendQuery(String graphId, String preparedQuery, long timeout);
3846

47+
/**
48+
* Sends a read-query to the redis graph.Implementation and context dependent
49+
* @param graphId graph to be queried
50+
* @param preparedQuery prepared query
51+
* @param timeout
52+
* @return Result set
53+
*/
54+
protected abstract ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout);
55+
3956
/**
4057
* Execute a Cypher query.
4158
* @param graphId a graph to perform the query on
@@ -46,6 +63,16 @@ public ResultSet query(String graphId, String query) {
4663
return sendQuery(graphId, query);
4764
}
4865

66+
/**
67+
* Execute a Cypher read-only query.
68+
* @param graphId a graph to perform the query on
69+
* @param query Cypher query
70+
* @return a result set
71+
*/
72+
public ResultSet readOnlyQuery(String graphId, String query) {
73+
return sendReadOnlyQuery(graphId, query);
74+
}
75+
4976
/**
5077
* Execute a Cypher query with timeout.
5178
* @param graphId a graph to perform the query on
@@ -58,6 +85,18 @@ public ResultSet query(String graphId, String query, long timeout) {
5885
return sendQuery(graphId, query, timeout);
5986
}
6087

88+
/**
89+
* Execute a Cypher read-only query with timeout.
90+
* @param graphId a graph to perform the query on
91+
* @param timeout
92+
* @param query Cypher query
93+
* @return a result set
94+
*/
95+
@Override
96+
public ResultSet readOnlyQuery(String graphId, String query, long timeout) {
97+
return sendReadOnlyQuery(graphId, query, timeout);
98+
}
99+
61100
/**
62101
* Execute a Cypher query with arguments
63102
* @param graphId a graph to perform the query on
@@ -84,6 +123,18 @@ public ResultSet query(String graphId, String query, Map<String, Object> params)
84123
return sendQuery(graphId, preparedQuery);
85124
}
86125

126+
/**
127+
* Executes a cypher read-only query with parameters.
128+
* @param graphId a graph to perform the query on.
129+
* @param query Cypher query.
130+
* @param params parameters map.
131+
* @return a result set.
132+
*/
133+
public ResultSet readOnlyQuery(String graphId, String query, Map<String, Object> params) {
134+
String preparedQuery = Utils.prepareQuery(query, params);
135+
return sendReadOnlyQuery(graphId, preparedQuery);
136+
}
137+
87138
/**
88139
* Executes a cypher query with parameters and timeout.
89140
* @param graphId a graph to perform the query on.
@@ -98,6 +149,20 @@ public ResultSet query(String graphId, String query, Map<String, Object> params,
98149
return sendQuery(graphId, preparedQuery, timeout);
99150
}
100151

152+
/**
153+
* Executes a cypher read-only query with parameters and timeout.
154+
* @param graphId a graph to perform the query on.
155+
* @param timeout
156+
* @param query Cypher query.
157+
* @param params parameters map.
158+
* @return a result set.
159+
*/
160+
@Override
161+
public ResultSet readOnlyQuery(String graphId, String query, Map<String, Object> params, long timeout) {
162+
String preparedQuery = Utils.prepareQuery(query, params);
163+
return sendReadOnlyQuery(graphId, preparedQuery, timeout);
164+
}
165+
101166
public ResultSet callProcedure(String graphId, String procedure){
102167
return callProcedure(graphId, procedure, Utils.DUMMY_LIST, Utils.DUMMY_MAP);
103168
}

src/main/java/com/redislabs/redisgraph/impl/api/ContextedRedisGraph.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ protected ResultSet sendQuery(String graphId, String preparedQuery) {
5959
}
6060
}
6161

62+
/**
63+
* Sends the read-only query over the instance only connection
64+
* @param graphId graph to be queried
65+
* @param preparedQuery prepared query
66+
* @return Result set with the query answer
67+
*/
68+
@Override
69+
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery) {
70+
Jedis conn = getConnection();
71+
try {
72+
List<Object> rawResponse = (List<Object>) conn.sendCommand(RedisGraphCommand.RO_QUERY, graphId, preparedQuery, Utils.COMPACT_STRING);
73+
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
74+
}
75+
catch (JRedisGraphException ge) {
76+
throw ge;
77+
}
78+
catch (JedisDataException de) {
79+
throw new JRedisGraphException(de);
80+
}
81+
}
82+
6283
/**
6384
* Sends the query over the instance only connection
6485
* @param graphId graph to be queried
@@ -82,6 +103,29 @@ protected ResultSet sendQuery(String graphId, String preparedQuery, long timeout
82103
}
83104
}
84105

106+
/**
107+
* Sends the read-only query over the instance only connection
108+
* @param graphId graph to be queried
109+
* @param timeout
110+
* @param preparedQuery prepared query
111+
* @return Result set with the query answer
112+
*/
113+
@Override
114+
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout) {
115+
Jedis conn = getConnection();
116+
try {
117+
List<Object> rawResponse = (List<Object>) conn.sendBlockingCommand(RedisGraphCommand.RO_QUERY,
118+
graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout));
119+
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
120+
}
121+
catch (JRedisGraphException ge) {
122+
throw ge;
123+
}
124+
catch (JedisDataException de) {
125+
throw new JRedisGraphException(de);
126+
}
127+
}
128+
85129
/**
86130
* @return Returns the instance Jedis connection.
87131
*/

src/main/java/com/redislabs/redisgraph/impl/api/RedisGraph.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ protected ResultSet sendQuery(String graphId, String preparedQuery){
6969
}
7070
}
7171

72+
/**
73+
* Overrides the abstract function.
74+
* Sends the read-only query from any Jedis connection received from the Jedis pool and closes it once done
75+
* @param graphId graph to be queried
76+
* @param preparedQuery prepared query
77+
* @return Result set with the query answer
78+
*/
79+
@Override
80+
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery){
81+
try (ContextedRedisGraph contextedRedisGraph = new ContextedRedisGraph(getConnection())) {
82+
contextedRedisGraph.setRedisGraphCaches(caches);
83+
return contextedRedisGraph.sendReadOnlyQuery(graphId, preparedQuery);
84+
}
85+
}
86+
7287
/**
7388
* Overrides the abstract function.
7489
* Sends the query from any Jedis connection received from the Jedis pool and closes it once done
@@ -85,6 +100,22 @@ protected ResultSet sendQuery(String graphId, String preparedQuery, long timeout
85100
}
86101
}
87102

103+
/**
104+
* Overrides the abstract function.
105+
* Sends the read-only query from any Jedis connection received from the Jedis pool and closes it once done
106+
* @param graphId graph to be queried
107+
* @param preparedQuery prepared query
108+
* @param timeout
109+
* @return Result set with the query answer
110+
*/
111+
@Override
112+
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout){
113+
try (ContextedRedisGraph contextedRedisGraph = new ContextedRedisGraph(getConnection())) {
114+
contextedRedisGraph.setRedisGraphCaches(caches);
115+
return contextedRedisGraph.sendReadOnlyQuery(graphId, preparedQuery, timeout);
116+
}
117+
}
118+
88119
/**
89120
* Closes the Jedis pool
90121
*/

src/main/java/com/redislabs/redisgraph/impl/api/RedisGraphCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
public enum RedisGraphCommand implements ProtocolCommand {
1111
QUERY("graph.QUERY"),
12+
RO_QUERY("graph.RO_QUERY"),
1213
DELETE("graph.DELETE");
1314

1415
private final byte[] raw;

0 commit comments

Comments
 (0)