-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathRedisGraph.java
170 lines (153 loc) · 5.55 KB
/
RedisGraph.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.Connection;
import com.redislabs.redisgraph.RedisGraphContext;
import com.redislabs.redisgraph.RedisGraphContextGenerator;
import com.redislabs.redisgraph.ResultSet;
import com.redislabs.redisgraph.impl.graph_cache.RedisGraphCaches;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.util.Pool;
import redis.clients.jedis.util.SafeEncoder;
/**
*
*/
public class RedisGraph extends AbstractRedisGraph implements RedisGraphContextGenerator {
private final Pool<Jedis> pool;
private final Connection connection;
private final RedisGraphCaches caches = new RedisGraphCaches();
/**
* Creates a client running on the local machine
*/
public RedisGraph() {
this("localhost", 6379);
}
/**
* Creates a client running on the specific host/post
*
* @param host Redis host
* @param port Redis port
*/
public RedisGraph(String host, int port) {
this(new JedisPool(host, port));
}
/**
* Creates a client using provided Jedis pool
*
* @param pool bring your own Jedis pool
*/
public RedisGraph(Pool<Jedis> pool) {
this.pool = pool;
this.connection = null;
}
public RedisGraph(Jedis jedis) {
this.connection = new SingleConnection(jedis);
this.pool = null;
}
public RedisGraph(JedisCluster jedis) {
this.connection = new ClusterConnection(jedis);
this.pool = null;
}
/**
* Overrides the abstract function. Gets and returns a Jedis connection from the Jedis pool
* @return a Jedis connection
*/
@Override
protected Connection getConnection() {
return pool == null ? connection : new PooledConnection(pool.getResource());
}
/**
* Overrides the abstract function.
* Sends the query from any Jedis connection received from the Jedis pool and closes it once done
* @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){
try (ContextedRedisGraph contextedRedisGraph = new ContextedRedisGraph(getConnection())) {
contextedRedisGraph.setRedisGraphCaches(caches);
return contextedRedisGraph.sendQuery(graphId, preparedQuery);
}
}
/**
* Overrides the abstract function.
* Sends the read-only query from any Jedis connection received from the Jedis pool and closes it once done
* @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){
try (ContextedRedisGraph contextedRedisGraph = new ContextedRedisGraph(getConnection())) {
contextedRedisGraph.setRedisGraphCaches(caches);
return contextedRedisGraph.sendReadOnlyQuery(graphId, preparedQuery);
}
}
/**
* Overrides the abstract function.
* Sends the query from any Jedis connection received from the Jedis pool and closes it once done
* @param graphId graph to be queried
* @param preparedQuery prepared query
* @param timeout
* @return Result set with the query answer
*/
@Override
protected ResultSet sendQuery(String graphId, String preparedQuery, long timeout){
try (ContextedRedisGraph contextedRedisGraph = new ContextedRedisGraph(getConnection())) {
contextedRedisGraph.setRedisGraphCaches(caches);
return contextedRedisGraph.sendQuery(graphId, preparedQuery, timeout);
}
}
/**
* Overrides the abstract function.
* Sends the read-only query from any Jedis connection received from the Jedis pool and closes it once done
* @param graphId graph to be queried
* @param preparedQuery prepared query
* @param timeout
* @return Result set with the query answer
*/
@Override
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout){
try (ContextedRedisGraph contextedRedisGraph = new ContextedRedisGraph(getConnection())) {
contextedRedisGraph.setRedisGraphCaches(caches);
return contextedRedisGraph.sendReadOnlyQuery(graphId, preparedQuery, timeout);
}
}
/**
* Closes all the connections
*/
@Override
public void close() {
if (pool != null) {
pool.close();
}
if (connection != null) {
connection.disconnect();
}
}
/**
* Deletes the entire graph
* @param graphId graph to delete
* @return delete running time statistics
*/
@Override
public String deleteGraph(String graphId) {
try (Connection conn = getConnection()) {
Object response = conn.sendCommand(RedisGraphCommand.DELETE, graphId);
//clear local state
caches.removeGraphCache(graphId);
return SafeEncoder.encode((byte[]) response);
}
}
/**
* Returns a new ContextedRedisGraph bounded to a Jedis connection from the Jedis pool
* @return ContextedRedisGraph
*/
@Override
public RedisGraphContext getContext() {
ContextedRedisGraph contextedRedisGraph = new ContextedRedisGraph(getConnection());
contextedRedisGraph.setRedisGraphCaches(this.caches);
return contextedRedisGraph;
}
}