11package com .redislabs .redisgraph ;
22
3+ import com .redislabs .redisgraph .impl .JRedisGraphTransaction ;
34import com .redislabs .redisgraph .impl .graph_cache .GraphCache ;
45import com .redislabs .redisgraph .impl .ResultSetImpl ;
56import org .apache .commons .text .translate .AggregateTranslator ;
67import org .apache .commons .text .translate .CharSequenceTranslator ;
78import org .apache .commons .text .translate .LookupTranslator ;
89import redis .clients .jedis .BinaryClient ;
10+ import redis .clients .jedis .Client ;
911import redis .clients .jedis .Jedis ;
1012import redis .clients .jedis .JedisPool ;
1113import 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}
0 commit comments