-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathContextedRedisGraph.java
170 lines (153 loc) · 5.14 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
package com.redislabs.redisgraph.impl.api;
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.util.SafeEncoder;
import redis.clients.jedis.exceptions.JedisDataException;
import java.util.List;
/**
* 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 Jedis connectionContext;
private RedisGraphCaches caches;
/**
* Generates a new instance with a specific Jedis connection
* @param connectionContext
*/
public ContextedRedisGraph(Jedis connectionContext){
this.connectionContext = connectionContext;
}
/**
* Overrides the abstract method. Return the instance only connection
* @return
*/
@Override
protected Jedis 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) {
long startTime = System.nanoTime();
Jedis conn = getConnection();
try {
List<Object> rawResponse = (List<Object>) conn.sendCommand(RedisGraphCommand.QUERY, graphId, preparedQuery, Utils.COMPACT_STRING);
long stopTime = System.nanoTime();
System.out.println("Server response time " + preparedQuery + " " + (stopTime - startTime)/1000000);
startTime = System.nanoTime();
ResultSet rs = new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
stopTime = System.nanoTime();
System.out.println("result set parsing " + preparedQuery + " " + (stopTime - startTime)/1000000);
return rs;
}
catch (JRedisGraphException rt) {
throw rt;
}
catch (JedisDataException j) {
throw new JRedisGraphException(j);
}
}
/**
* 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) {
Jedis conn = getConnection();
try {
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);
}
}
/**
* @return Returns the instance Jedis connection.
*/
@Override
public Jedis getConnectionContext() {
return this.connectionContext;
}
/**
* Creates a new RedisGraphTransaction transactional object
* @return new RedisGraphTransaction
*/
@Override
public RedisGraphTransaction multi() {
Jedis jedis = getConnection();
Client client = jedis.getClient();
client.multi();
client.getOne();
RedisGraphTransaction transaction = new RedisGraphTransaction(client,this);
transaction.setRedisGraphCaches(caches);
return transaction;
}
/**
* 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) {
Jedis 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;
}
}