-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathRedisGraphTransaction.java
147 lines (127 loc) · 5.19 KB
/
RedisGraphTransaction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.redislabs.redisgraph.impl.api;
import com.redislabs.redisgraph.RedisGraph;
import com.redislabs.redisgraph.ResultSet;
import com.redislabs.redisgraph.impl.Utils;
import com.redislabs.redisgraph.impl.graph_cache.RedisGraphCaches;
import com.redislabs.redisgraph.impl.resultset.ResultSetImpl;
import redis.clients.jedis.Builder;
import redis.clients.jedis.BuilderFactory;
import redis.clients.jedis.Client;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;
import java.util.List;
import java.util.Map;
/**
* This class is extending Jedis Transaction
*/
public class RedisGraphTransaction extends Transaction implements com.redislabs.redisgraph.RedisGraphTransaction, RedisGraphCacheHolder {
private final RedisGraph redisGraph;
private RedisGraphCaches caches;
public RedisGraphTransaction(Client client, RedisGraph redisGraph){
// init as in Jedis
super(client);
this.redisGraph = redisGraph;
}
/**
* Execute a Cypher query.
* @param graphId a graph to perform the query on
* @param query Cypher query
* @return a response which builds the result set with the query answer.
*/
@Override
public Response<ResultSet> query(String graphId, String query) {
client.sendCommand(RedisGraphCommand.QUERY, graphId, query, "--COMPACT");
return getResponse(new Builder<ResultSet>() {
@Override
public ResultSet build(Object o) {
return new ResultSetImpl((List<Object>)o, redisGraph, caches.getGraphCache(graphId));
}
});
}
/**
* Execute a Cypher query with arguments
*
* @param graphId a graph to perform the query on
* @param query Cypher query
* @param args
* @return response with a result set
* @deprecated use {@link #query(String, String, Map)} instead.
*/
@Deprecated
@Override
public Response<ResultSet> query(String graphId, String query, Object ...args){
String preparedQuery = Utils.prepareQuery(query, args);
client.sendCommand(RedisGraphCommand.QUERY, graphId, preparedQuery, "--COMPACT");
return getResponse(new Builder<ResultSet>() {
@Override
public ResultSet build(Object o) {
return new ResultSetImpl((List<Object>)o, redisGraph, caches.getGraphCache(graphId));
}
});
}
/**
* Executes a cypher query with parameters.
* @param graphId a graph to perform the query on.
* @param query Cypher query.
* @param params parameters map.
* @return a response which builds the result set with the query answer.
*/
@Override
public Response<ResultSet> query(String graphId, String query, Map<String, Object> params) {
String preparedQuery = Utils.prepareQuery(query, params);
client.sendCommand(RedisGraphCommand.QUERY, graphId, preparedQuery, "--COMPACT");
return getResponse(new Builder<ResultSet>() {
@Override
public ResultSet build(Object o) {
return new ResultSetImpl((List<Object>)o, redisGraph, caches.getGraphCache(graphId));
}
});
}
/**
* Invokes stored procedures without arguments, in multi/exec context
* @param graphId a graph to perform the query on
* @param procedure procedure name to invoke
* @return response with result set with the procedure data
*/
public Response<ResultSet> callProcedure(String graphId, String procedure){
return callProcedure(graphId, procedure, Utils.DUMMY_LIST);
}
/**
* Invokes stored procedure with arguments, in multi/exec context
* @param graphId a graph to perform the query on
* @param procedure procedure name to invoke
* @param args procedure arguments
* @return response with result set with the procedure data
*/
public Response<ResultSet> callProcedure(String graphId, String procedure, List<String> args){
return callProcedure(graphId, procedure, args, Utils.DUMMY_MAP);
}
/**
* Invoke a stored procedure, in multi/exec context
* @param graphId a graph to perform the query on
* @param procedure - procedure to execute
* @param args - procedure arguments
* @param kwargs - procedure output arguments
* @return response with result set with the procedure data
*/
public Response<ResultSet> callProcedure(String graphId, String procedure, List<String> args,
Map<String, List<String>> kwargs) {
String preparedProcedure = Utils.prepareProcedure(procedure, args, kwargs);
return query(graphId, preparedProcedure);
}
/**
* Deletes the entire graph, in multi/exec context
* @param graphId graph to delete
* @return response with the deletion running time statistics
*/
public Response<String> deleteGraph(String graphId){
client.sendCommand(RedisGraphCommand.DELETE, graphId);
Response<String> response = getResponse(BuilderFactory.STRING);
caches.removeGraphCache(graphId);
return response;
}
@Override
public void setRedisGraphCaches(RedisGraphCaches caches) {
this.caches = caches;
}
}