11package com .redislabs .redisgraph ;
22
3- import com .redislabs .redisgraph .impl .graph_cache .GraphCache ;
4- import com .redislabs .redisgraph .impl .ResultSetImpl ;
5- import org .apache .commons .text .translate .AggregateTranslator ;
6- import org .apache .commons .text .translate .CharSequenceTranslator ;
7- import org .apache .commons .text .translate .LookupTranslator ;
8- import redis .clients .jedis .BinaryClient ;
9- import redis .clients .jedis .Jedis ;
10- import redis .clients .jedis .JedisPool ;
11- import redis .clients .jedis .commands .ProtocolCommand ;
12- import redis .clients .jedis .util .Pool ;
13-
143import java .io .Closeable ;
15- import java .util .*;
16- import java .util .concurrent .ConcurrentHashMap ;
17- import java .util .stream .Collectors ;
18-
19-
20- /**
21- *
22- */
23- public class RedisGraph implements Closeable {
24-
25-
26-
27- private final Pool <Jedis > client ;
28- private final Map <String , GraphCache > graphCaches = new ConcurrentHashMap <>();
29-
30-
31-
32- private static final CharSequenceTranslator ESCAPE_CHYPER ;
33- static {
34- final Map <CharSequence , CharSequence > escapeJavaMap = new HashMap <>();
35- escapeJavaMap .put ("\' " , "\\ '" );
36- escapeJavaMap .put ("\" " , "\\ \" " );
37- ESCAPE_CHYPER = new AggregateTranslator (new LookupTranslator (Collections .unmodifiableMap (escapeJavaMap )));
38- }
39-
40- /**
41- * Creates a client running on the local machine
42-
43- */
44- public RedisGraph () {
45- this ("localhost" , 6379 );
46- }
47-
48- /**
49- * Creates a client running on the specific host/post
50- *
51- * @param host Redis host
52- * @param port Redis port
53- */
54- public RedisGraph (String host , int port ) {
55- this ( new JedisPool (host , port ));
56- }
57-
58- /**
59- * Creates a client using provided Jedis pool
60- *
61- * @param jedis bring your own Jedis pool
62- */
63- public RedisGraph ( Pool <Jedis > jedis ) {
64-
65- this .client = jedis ;
66- }
67-
68- @ Override
69- public void close (){
70- this .client .close ();
71- }
4+ import java .util .List ;
5+ import java .util .Map ;
726
7+ public interface RedisGraph extends Closeable {
738
749 /**
7510 * Execute a Cypher query with arguments
76- *
7711 * @param graphId a graph to perform the query on
7812 * @param query Cypher query
7913 * @param args
8014 * @return a result set
8115 */
82- public ResultSet query (String graphId , String query , Object ...args ) {
83- if (args .length > 0 ) {
84- for (int i =0 ; i <args .length ; ++i ) {
85- if (args [i ] instanceof String ) {
86- args [i ] = "\' " + ESCAPE_CHYPER .translate ((String )args [i ]) + "\' " ;
87- }
88- }
89- query = String .format (query , args );
90- }
91- graphCaches .putIfAbsent (graphId , new GraphCache (graphId , this ));
92- List <Object > rawResponse = null ;
93- try (Jedis conn = getConnection ()){
94- rawResponse = sendCompactCommand (conn , Command .QUERY , graphId , query ).getObjectMultiBulkReply ();
95- }
96- return new ResultSetImpl (rawResponse , graphCaches .get (graphId ));
97-
98- }
16+ ResultSet query (String graphId , String query , Object ...args );
9917
10018 /**
10119 * Invokes stored procedures without arguments
10220 * @param graphId a graph to perform the query on
10321 * @param procedure procedure name to invoke
10422 * @return result set with the procedure data
10523 */
106- public ResultSet callProcedure (String graphId , String procedure ){
107- return callProcedure (graphId , procedure , new ArrayList <>(), new HashMap <>());
108- }
109-
24+ ResultSet callProcedure (String graphId , String procedure );
11025
11126 /**
11227 * Invokes stored procedure with arguments
@@ -115,76 +30,23 @@ public ResultSet callProcedure(String graphId, String procedure ){
11530 * @param args procedure arguments
11631 * @return result set with the procedure data
11732 */
118- public ResultSet callProcedure (String graphId , String procedure , List <String > args ){
119- return callProcedure (graphId , procedure , args , new HashMap <>());
120- }
121-
122-
123- /**
124- * Deletes the entire graph
125- *
126- * @return delete running time statistics
127- */
128- public String deleteGraph (String graphId ) {
129- //clear local state
130- graphCaches .remove (graphId );
131- try (Jedis conn = getConnection ()) {
132- return sendCommand (conn , Command .DELETE , graphId ).getBulkReply ();
133- }
134-
135- }
136-
137-
138- /**
139- * Sends command - will be replaced with sendCompactCommand once graph.delete support --compact flag
140- * @param conn - connection
141- * @param provider - command type
142- * @param args - command arguments
143- * @return
144- */
145- private BinaryClient sendCommand (Jedis conn , ProtocolCommand provider , String ...args ) {
146- BinaryClient binaryClient = conn .getClient ();
147- binaryClient .sendCommand (provider , args );
148- return binaryClient ;
149- }
150-
151-
152- /**
153- * Sends the command with --COMPACT flag
154- * @param conn - connection
155- * @param provider - command type
156- * @param args - command arguments
157- * @return
158- */
159- private BinaryClient sendCompactCommand (Jedis conn , ProtocolCommand provider , String ...args ) {
160- String [] t = new String [args .length +1 ];
161- System .arraycopy (args , 0 , t , 0 , args .length );
162- t [args .length ]="--COMPACT" ;
163- return sendCommand (conn , provider , t );
164- }
165-
166- private Jedis getConnection () {
167- return this .client .getResource ();
168- }
169-
33+ ResultSet callProcedure (String graphId , String procedure , List <String > args );
17034
17135 /**
17236 * Invoke a stored procedure
17337 * @param graphId a graph to perform the query on
17438 * @param procedure - procedure to execute
17539 * @param args - procedure arguments
17640 * @param kwargs - procedure output arguments
177- * @return
41+ * @return result set with the procedure data
42+ */
43+ ResultSet callProcedure (String graphId , String procedure , List <String > args , Map <String , List <String >> kwargs );
44+
45+ /**
46+ * Deletes the entire graph
47+ * @param graphId graph to delete
48+ * @return delete running time statistics
17849 */
179- public ResultSet callProcedure (String graphId , String procedure , List < String > args , Map < String , List < String >> kwargs ){
50+ String deleteGraph (String graphId );
18051
181- args = args .stream ().map ( s -> Utils .quoteString (s )).collect (Collectors .toList ());
182- StringBuilder queryString = new StringBuilder ();
183- queryString .append (String .format ("CALL %s(%s)" , procedure , String .join ("," , args )));
184- List <String > kwargsList = kwargs .getOrDefault ("y" , null );
185- if (kwargsList != null ){
186- queryString .append (String .join ("," , kwargsList ));
187- }
188- return query (graphId , queryString .toString ());
189- }
19052}
0 commit comments