Skip to content

Commit a736bc3

Browse files
author
DvirDukhan
authored
Merge pull request #42 from RedisGraph/multi-exec
Multi exec
2 parents 76a3f13 + d8bc765 commit a736bc3

26 files changed

+1073
-250
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<distribution>repo</distribution>
4747
</license>
4848
</licenses>
49-
49+
5050
<dependencies>
5151
<dependency>
5252
<groupId>org.apache.commons</groupId>
@@ -62,7 +62,7 @@
6262
<dependency>
6363
<groupId>redis.clients</groupId>
6464
<artifactId>jedis</artifactId>
65-
<version>3.0.1</version>
65+
<version>3.1.0</version>
6666
</dependency>
6767
<dependency>
6868
<groupId>org.apache.commons</groupId>
@@ -179,7 +179,7 @@
179179
<plugin>
180180
<groupId>org.apache.maven.plugins</groupId>
181181
<artifactId>maven-pmd-plugin</artifactId>
182-
<version>3.11.0</version>
182+
<version>3.12.0</version>
183183
</plugin>
184184
</plugins>
185185
</build>

src/main/java/META-INF/MANIFEST.MF

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Manifest-Version: 1.0
2-
Main-Class: com.redislabs.redisgraph.RedisGraphAPI
3-
1+
Manifest-Version: 1.0
2+
Main-Class: com.redislabs.redisgraph.RedisGraph
3+

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import java.util.List;
44

55
/**
6-
* Query response header interface. Represents the response schame (column names and types)
6+
* Query response header interface. Represents the response schema (column names and types)
77
*/
88
public interface Header {
99

1010

11-
public enum ResultSetColumnTypes {
11+
enum ResultSetColumnTypes {
1212
COLUMN_UNKNOWN,
1313
COLUMN_SCALAR,
1414
COLUMN_NODE,
15-
COLUMN_RELATION;
15+
COLUMN_RELATION
1616

1717
}
1818

Lines changed: 15 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,27 @@
11
package 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-
143
import 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
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.redislabs.redisgraph;
2+
3+
import redis.clients.jedis.Jedis;
4+
5+
public interface RedisGraphContexted extends RedisGraph {
6+
7+
8+
/**
9+
* Returns implementing class connection context
10+
* @return Jedis connection
11+
*/
12+
Jedis getConnectionContext();
13+
14+
/**
15+
* Returns a Redis transactional object, over the connection context, with graph API capabilities
16+
* @return Redis transactional object, over the connection context, with graph API capabilities
17+
*/
18+
RedisGraphTransaction multi();
19+
20+
/**
21+
* Perform watch over given Redis keys
22+
* @param keys
23+
* @return "OK"
24+
*/
25+
String watch(String... keys);
26+
27+
/**
28+
* Removes watch from all keys
29+
* @return
30+
*/
31+
String unwatch();
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.redislabs.redisgraph;
2+
3+
public interface RedisGraphGeneralContext extends RedisGraph {
4+
5+
/**
6+
* Generate a connection bounded api
7+
* @return a connection bounded api
8+
*/
9+
RedisGraphContexted getContextedAPI();
10+
11+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.redislabs.redisgraph;
2+
3+
import redis.clients.jedis.Response;
4+
import redis.clients.jedis.commands.BasicRedisPipeline;
5+
import redis.clients.jedis.commands.BinaryRedisPipeline;
6+
import redis.clients.jedis.commands.BinaryScriptingCommandsPipeline;
7+
import redis.clients.jedis.commands.ClusterPipeline;
8+
import redis.clients.jedis.commands.MultiKeyBinaryRedisPipeline;
9+
import redis.clients.jedis.commands.MultiKeyCommandsPipeline;
10+
import redis.clients.jedis.commands.RedisPipeline;
11+
import redis.clients.jedis.commands.ScriptingCommandsPipeline;
12+
13+
import java.io.Closeable;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
/**
18+
* An interface which aligned to Jedis transactional interface
19+
*/
20+
public interface RedisGraphTransaction extends
21+
MultiKeyBinaryRedisPipeline,
22+
MultiKeyCommandsPipeline, ClusterPipeline,
23+
BinaryScriptingCommandsPipeline, ScriptingCommandsPipeline,
24+
BasicRedisPipeline, BinaryRedisPipeline, RedisPipeline, Closeable {
25+
/**
26+
* Execute a Cypher query with arguments
27+
* @param graphId a graph to perform the query on
28+
* @param query Cypher query
29+
* @param args
30+
* @return a response which builds the result set with the query answer
31+
*/
32+
Response<ResultSet> query(String graphId, String query, Object ...args);
33+
34+
/**
35+
* Invokes stored procedures without arguments
36+
* @param graphId a graph to perform the query on
37+
* @param procedure procedure name to invoke
38+
* @return a response which builds result set with the procedure data
39+
*/
40+
Response<ResultSet> callProcedure(String graphId, String procedure);
41+
42+
/**
43+
* Invokes stored procedure with arguments
44+
* @param graphId a graph to perform the query on
45+
* @param procedure procedure name to invoke
46+
* @param args procedure arguments
47+
* @return a response which builds result set with the procedure data
48+
*/
49+
Response<ResultSet> callProcedure(String graphId, String procedure, List<String> args);
50+
51+
/**
52+
* Invoke a stored procedure
53+
* @param graphId a graph to perform the query on
54+
* @param procedure - procedure to execute
55+
* @param args - procedure arguments
56+
* @param kwargs - procedure output arguments
57+
* @return a response which builds result set with the procedure data
58+
*/
59+
Response<ResultSet> callProcedure(String graphId, String procedure, List<String> args , Map<String, List<String>> kwargs);
60+
61+
/**
62+
* Deletes the entire graph
63+
* @param graphId graph to delete
64+
* @return a response which builds the delete running time statistics
65+
*/
66+
Response<String> deleteGraph(String graphId);
67+
68+
69+
/**
70+
* executes the transaction
71+
* @return a list of the executed transaction commands answers, in case of successful transaction, null otherwise
72+
*/
73+
List<Object> exec();
74+
75+
/**
76+
* If object is in transaction mode,
77+
* flushes all previously queued commands in a transaction and restores the connection state to normal
78+
*/
79+
void clear();
80+
81+
/**
82+
*
83+
* @return
84+
*/
85+
List<Response<?>> execGetResponse();
86+
87+
/**
88+
* Flushes all previously queued commands in a transaction and restores the connection state to normal
89+
*/
90+
String discard();
91+
}

0 commit comments

Comments
 (0)