-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathContextedRedisGraph.java
217 lines (196 loc) · 6.98 KB
/
ContextedRedisGraph.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.redislabs.redisgraph.impl.api;
import java.util.List;
import com.redislabs.redisgraph.Connection;
import com.redislabs.redisgraph.RedisGraphContext;
import com.redislabs.redisgraph.ResultSet;
import com.redislabs.redisgraph.exceptions.JRedisGraphException;
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.Client;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.util.SafeEncoder;
/**
* An implementation of RedisGraphContext. Allows sending RedisGraph and some Redis commands,
* within a specific connection context
*/
public class ContextedRedisGraph extends AbstractRedisGraph implements RedisGraphContext, RedisGraphCacheHolder {
private final Connection connectionContext;
private RedisGraphCaches caches;
/**
* Generates a new instance with a specific Jedis connection
* @param connectionContext
*/
public ContextedRedisGraph(Connection connectionContext) {
this.connectionContext = connectionContext;
}
/**
* Overrides the abstract method. Return the instance only connection
* @return
*/
@Override
protected Connection getConnection() {
return this.connectionContext;
}
/**
* Sends the query over the instance only connection
* @param graphId graph to be queried
* @param preparedQuery prepared query
* @return Result set with the query answer
*/
@Override
protected ResultSet sendQuery(String graphId, String preparedQuery) {
Connection conn = getConnection();
try {
@SuppressWarnings("unchecked")
List<Object> rawResponse = (List<Object>) conn.sendCommand(RedisGraphCommand.QUERY, graphId, preparedQuery, Utils.COMPACT_STRING);
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
} catch (JRedisGraphException rt) {
throw rt;
} catch (JedisDataException j) {
throw new JRedisGraphException(j);
}
}
/**
* Sends the read-only query over the instance only connection
* @param graphId graph to be queried
* @param preparedQuery prepared query
* @return Result set with the query answer
*/
@Override
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery) {
Connection conn = getConnection();
try {
@SuppressWarnings("unchecked")
List<Object> rawResponse = (List<Object>) conn.sendCommand(RedisGraphCommand.RO_QUERY, graphId, preparedQuery, Utils.COMPACT_STRING);
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
} catch (JRedisGraphException ge) {
throw ge;
} catch (JedisDataException de) {
throw new JRedisGraphException(de);
}
}
/**
* Sends the query over the instance only connection
* @param graphId graph to be queried
* @param timeout
* @param preparedQuery prepared query
* @return Result set with the query answer
*/
@Override
protected ResultSet sendQuery(String graphId, String preparedQuery, long timeout) {
Connection conn = getConnection();
try {
@SuppressWarnings("unchecked")
List<Object> rawResponse = (List<Object>) conn.sendBlockingCommand(RedisGraphCommand.QUERY,
graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout));
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
} catch (JRedisGraphException rt) {
throw rt;
} catch (JedisDataException j) {
throw new JRedisGraphException(j);
}
}
/**
* Sends the read-only query over the instance only connection
* @param graphId graph to be queried
* @param timeout
* @param preparedQuery prepared query
* @return Result set with the query answer
*/
@Override
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout) {
Connection conn = getConnection();
try {
@SuppressWarnings("unchecked")
List<Object> rawResponse = (List<Object>) conn.sendBlockingCommand(RedisGraphCommand.RO_QUERY,
graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout));
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
} catch (JRedisGraphException ge) {
throw ge;
} catch (JedisDataException de) {
throw new JRedisGraphException(de);
}
}
/**
* @return Returns the instance Jedis connection.
*/
@Override
public Connection getConnectionContext() {
return this.connectionContext;
}
/**
* Creates a new RedisGraphTransaction transactional object
* @return new RedisGraphTransaction
*/
@Override
public RedisGraphTransaction multi() {
Connection connection = getConnection();
Client client = connection.getClient();
client.multi();
client.getOne();
RedisGraphTransaction transaction = new RedisGraphTransaction(client, this);
transaction.setRedisGraphCaches(caches);
return transaction;
}
/**
* Creates a new RedisGraphPipeline pipeline object
* @return new RedisGraphPipeline
*/
@Override
public RedisGraphPipeline pipelined() {
Connection connection = getConnection();
Client client = connection.getClient();
RedisGraphPipeline pipeline = new RedisGraphPipeline(client, this);
pipeline.setRedisGraphCaches(caches);
return pipeline;
}
/**
* Perfrom watch over given Redis keys
* @param keys
* @return "OK"
*/
@Override
public String watch(String... keys) {
return this.getConnection().watch(keys);
}
/**
* Removes watch from all keys
* @return
*/
@Override
public String unwatch() {
return this.getConnection().unwatch();
}
/**
* Deletes the entire graph
* @param graphId graph to delete
* @return delete running time statistics
*/
@Override
public String deleteGraph(String graphId) {
Connection conn = getConnection();
Object response;
try {
response = conn.sendCommand(RedisGraphCommand.DELETE, graphId);
} catch (Exception e) {
conn.close();
throw e;
}
//clear local state
caches.removeGraphCache(graphId);
return SafeEncoder.encode((byte[]) response);
}
/**
* closes the Jedis connection
*/
@Override
public void close() {
this.connectionContext.close();
}
@Override
public void setRedisGraphCaches(RedisGraphCaches caches) {
this.caches = caches;
}
}