Skip to content

Commit 62e9c43

Browse files
author
DvirDukhan
committed
added multi exec support
1 parent 8aa70bb commit 62e9c43

File tree

5 files changed

+289
-35
lines changed

5 files changed

+289
-35
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
<dependency>
7070
<groupId>redis.clients</groupId>
7171
<artifactId>jedis</artifactId>
72-
<version>3.1.0-SNAPSHOT</version>
72+
<version>3.1.0-m4</version>
7373
</dependency>
7474
<dependency>
7575
<groupId>org.apache.commons</groupId>

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

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.redislabs.redisgraph;
22

3+
import com.redislabs.redisgraph.impl.JRedisGraphTransaction;
34
import com.redislabs.redisgraph.impl.graph_cache.GraphCache;
45
import com.redislabs.redisgraph.impl.ResultSetImpl;
56
import org.apache.commons.text.translate.AggregateTranslator;
67
import org.apache.commons.text.translate.CharSequenceTranslator;
78
import org.apache.commons.text.translate.LookupTranslator;
89
import redis.clients.jedis.BinaryClient;
10+
import redis.clients.jedis.Client;
911
import redis.clients.jedis.Jedis;
1012
import redis.clients.jedis.JedisPool;
1113
import redis.clients.jedis.commands.ProtocolCommand;
@@ -28,16 +30,6 @@ public class RedisGraph implements Closeable {
2830
private final Pool<Jedis> client;
2931
private final Map<String, GraphCache> graphCaches = new ConcurrentHashMap<>();
3032

31-
32-
33-
public static final CharSequenceTranslator ESCAPE_CHYPER;
34-
static {
35-
final Map<CharSequence, CharSequence> escapeJavaMap = new HashMap<>();
36-
escapeJavaMap.put("\'", "\\'");
37-
escapeJavaMap.put("\"", "\\\"");
38-
ESCAPE_CHYPER = new AggregateTranslator(new LookupTranslator(Collections.unmodifiableMap(escapeJavaMap)));
39-
}
40-
4133
/**
4234
* Creates a client running on the local machine
4335
@@ -81,21 +73,13 @@ public void close(){
8173
* @return a result set
8274
*/
8375
public ResultSet query(String graphId, String query, Object ...args) {
84-
if(args.length > 0) {
85-
for(int i=0; i<args.length; ++i) {
86-
if(args[i] instanceof String) {
87-
args[i] = "\'" + ESCAPE_CHYPER.translate((String)args[i]) + "\'";
88-
}
89-
}
90-
query = String.format(query, args);
91-
}
76+
String preparedQuery = Utils.prepareQuery(query, args);
9277
graphCaches.putIfAbsent(graphId, new GraphCache(graphId, this));
9378
List<Object> rawResponse = null;
9479
try(Jedis conn = getConnection()){
95-
rawResponse = (List<Object>) conn.sendCommand(Command.QUERY, graphId, query, "--COMPACT");
80+
rawResponse = (List<Object>) conn.sendCommand(Command.QUERY, graphId, preparedQuery, "--COMPACT");
9681
}
9782
return new ResultSetImpl(rawResponse, graphCaches.get(graphId));
98-
9983
}
10084

10185
/**
@@ -104,7 +88,7 @@ public ResultSet query(String graphId, String query, Object ...args) {
10488
* @param procedure procedure name to invoke
10589
* @return result set with the procedure data
10690
*/
107-
public ResultSet callProcedure(String graphId, String procedure ){
91+
public ResultSet callProcedure(String graphId, String procedure){
10892
return callProcedure(graphId, procedure, new ArrayList<>(), new HashMap<>());
10993
}
11094

@@ -120,10 +104,9 @@ public ResultSet callProcedure(String graphId, String procedure, List<String> ar
120104
return callProcedure(graphId, procedure, args, new HashMap<>());
121105
}
122106

123-
124107
/**
125108
* Deletes the entire graph
126-
*
109+
* @param graphId graph to delete
127110
* @return delete running time statistics
128111
*/
129112
public String deleteGraph(String graphId) {
@@ -146,17 +129,23 @@ private Jedis getConnection() {
146129
* @param procedure - procedure to execute
147130
* @param args - procedure arguments
148131
* @param kwargs - procedure output arguments
149-
* @return
132+
* @return result set with the procedure data
150133
*/
151134
public ResultSet callProcedure(String graphId, String procedure, List<String> args , Map<String, List<String>> kwargs ){
152135

153-
args = args.stream().map( s -> Utils.quoteString(s)).collect(Collectors.toList());
154-
StringBuilder queryString = new StringBuilder();
155-
queryString.append(String.format("CALL %s(%s)", procedure, String.join(",", args)));
156-
List<String> kwargsList = kwargs.getOrDefault("y", null);
157-
if(kwargsList != null){
158-
queryString.append(String.join(",", kwargsList));
159-
}
160-
return query(graphId, queryString.toString());
136+
String preparedProcedure = Utils.prepareProcedure(procedure, args, kwargs);
137+
return query(graphId, preparedProcedure);
138+
}
139+
140+
public JRedisGraphTransaction multi(){
141+
Jedis jedis = getConnection();
142+
Client client = jedis.getClient();
143+
client.multi();
144+
client.getOne();
145+
return new JRedisGraphTransaction(client,this, this.graphCaches);
146+
}
147+
148+
public Map<String, GraphCache> getGraphCaches() {
149+
return graphCaches;
161150
}
162151
}

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
package com.redislabs.redisgraph;
22

3+
import org.apache.commons.text.translate.AggregateTranslator;
4+
import org.apache.commons.text.translate.CharSequenceTranslator;
5+
import org.apache.commons.text.translate.LookupTranslator;
6+
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.stream.Collectors;
12+
313
/**
414
* Utilities class
515
*/
616
public class Utils {
717

18+
public static final CharSequenceTranslator ESCAPE_CHYPER;
19+
static {
20+
final Map<CharSequence, CharSequence> escapeJavaMap = new HashMap<>();
21+
escapeJavaMap.put("\'", "\\'");
22+
escapeJavaMap.put("\"", "\\\"");
23+
ESCAPE_CHYPER = new AggregateTranslator(new LookupTranslator(Collections.unmodifiableMap(escapeJavaMap)));
24+
}
25+
826
/**
927
*
1028
* @param str - a string
11-
* @return the input string surounded with quotation marks, if needed
29+
* @return the input string surrounded with quotation marks, if needed
1230
*/
1331
public static String quoteString(String str){
1432
if(str.startsWith("\"") && str.endsWith("\"")){
@@ -25,4 +43,40 @@ public static String quoteString(String str){
2543
}
2644
return sb.toString();
2745
}
46+
47+
/**
48+
* Prepare and formats a query and query arguments
49+
* @param query - query
50+
* @param args - query arguments
51+
* @return formatted query
52+
*/
53+
public static String prepareQuery(String query, Object ...args){
54+
if(args.length > 0) {
55+
for(int i=0; i<args.length; ++i) {
56+
if(args[i] instanceof String) {
57+
args[i] = "\'" + ESCAPE_CHYPER.translate((String)args[i]) + "\'";
58+
}
59+
}
60+
query = String.format(query, args);
61+
}
62+
return query;
63+
}
64+
65+
/**
66+
* Prepare and format a procedure call and its arguments
67+
* @param procedure - procedure to invoke
68+
* @param args - procedure arguments
69+
* @param kwargs - procedure output arguments
70+
* @return formatter procedure call
71+
*/
72+
public static String prepareProcedure(String procedure, List<String> args , Map<String, List<String>> kwargs){
73+
args = args.stream().map( s -> Utils.quoteString(s)).collect(Collectors.toList());
74+
StringBuilder queryStringBuilder = new StringBuilder();
75+
queryStringBuilder.append(String.format("CALL %s(%s)", procedure, String.join(",", args)));
76+
List<String> kwargsList = kwargs.getOrDefault("y", null);
77+
if(kwargsList != null){
78+
queryStringBuilder.append(String.join(",", kwargsList));
79+
}
80+
return queryStringBuilder.toString();
81+
}
2882
}
Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,106 @@
11
package com.redislabs.redisgraph.impl;
22

3-
public class JRedisGraphTransaction {
3+
4+
import com.redislabs.redisgraph.Command;
5+
import com.redislabs.redisgraph.RedisGraph;
6+
import com.redislabs.redisgraph.ResultSet;
7+
import com.redislabs.redisgraph.Utils;
8+
import com.redislabs.redisgraph.impl.graph_cache.GraphCache;
9+
import redis.clients.jedis.Builder;
10+
import redis.clients.jedis.BuilderFactory;
11+
import redis.clients.jedis.Client;
12+
import redis.clients.jedis.Jedis;
13+
import redis.clients.jedis.Response;
14+
import redis.clients.jedis.Transaction;
15+
16+
import java.util.ArrayList;
17+
import java.util.HashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
/**
22+
* This class is extending Jedis Transaction
23+
*/
24+
public class JRedisGraphTransaction extends Transaction {
25+
26+
private final RedisGraph redisGraph;
27+
private final Map<String, GraphCache> graphCaches;
28+
29+
30+
public JRedisGraphTransaction(Client client, RedisGraph redisGraph, Map<String, GraphCache> graphCaches){
31+
// init as in Jedis
32+
super(client);
33+
34+
this.redisGraph = redisGraph;
35+
this.graphCaches = graphCaches;
36+
}
37+
38+
/**
39+
* Execute a Cypher query with arguments
40+
*
41+
* @param graphId a graph to perform the query on
42+
* @param query Cypher query
43+
* @param args
44+
* @return response with a result set
45+
*/
46+
public Response<ResultSet> graphQuery(String graphId, String query, Object ...args){
47+
String preparedQuery = Utils.prepareQuery(query, args);
48+
graphCaches.putIfAbsent(graphId, new GraphCache(graphId, redisGraph));
49+
client.sendCommand(Command.QUERY, graphId, preparedQuery, "--COMPACT");
50+
return getResponse(new Builder<ResultSet>() {
51+
@Override
52+
public ResultSet build(Object o) {
53+
return new ResultSetImpl((List<Object>)o, graphCaches.get(graphId));
54+
}
55+
});
56+
}
57+
58+
59+
/**
60+
* Invokes stored procedures without arguments, in multi/exec context
61+
* @param graphId a graph to perform the query on
62+
* @param procedure procedure name to invoke
63+
* @return response with result set with the procedure data
64+
*/
65+
public Response<ResultSet> graphCallProcedure(String graphId, String procedure){
66+
return graphCallProcedure(graphId, procedure, new ArrayList<>(), new HashMap<>());
67+
}
68+
69+
/**
70+
* Invokes stored procedure with arguments, in multi/exec context
71+
* @param graphId a graph to perform the query on
72+
* @param procedure procedure name to invoke
73+
* @param args procedure arguments
74+
* @return response with result set with the procedure data
75+
*/
76+
public Response<ResultSet> graphCallProcedure(String graphId, String procedure, List<String> args ){
77+
return graphCallProcedure(graphId, procedure, args, new HashMap<>());
78+
}
79+
80+
81+
/**
82+
* Invoke a stored procedure, in multi/exec context
83+
* @param graphId a graph to perform the query on
84+
* @param procedure - procedure to execute
85+
* @param args - procedure arguments
86+
* @param kwargs - procedure output arguments
87+
* @return response with result set with the procedure data
88+
*/
89+
public Response<ResultSet> graphCallProcedure(String graphId, String procedure, List<String> args,
90+
HashMap<String, List<String>> kwargs) {
91+
String preparedProcedure = Utils.prepareProcedure(procedure, args, kwargs);
92+
return graphQuery(graphId, preparedProcedure);
93+
}
94+
95+
96+
/**
97+
* Deletes the entire graph, in multi/exec context
98+
* @param graphId graph to delete
99+
* @return response with the deletion running time statistics
100+
*/
101+
public Response<String> graphDeleteGraph(String graphId){
102+
graphCaches.remove(graphId);
103+
client.sendCommand(Command.DELETE, graphId);
104+
return getResponse(BuilderFactory.STRING);
105+
}
4106
}

0 commit comments

Comments
 (0)