Skip to content

Commit bce4555

Browse files
bsboddensazzad16
andauthored
Adds Jedis constructor, general cleanup (#135)
* feat: allow client to be instantiated with a Jedis instance * refactor: close standalone Jedis instance if needed * build: upgrade JUnit to 4.13.2 * refactor: fix deprecations and compiler warnings * refactor: rename api to client * test: add tests for instantiation Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com>
1 parent a611183 commit bce4555

File tree

19 files changed

+616
-552
lines changed

19 files changed

+616
-552
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<dependency>
6262
<groupId>junit</groupId>
6363
<artifactId>junit</artifactId>
64-
<version>4.13.1</version>
64+
<version>4.13.2</version>
6565
<scope>test</scope>
6666
</dependency>
6767
<dependency>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44
import java.util.List;
55
import java.util.Map;
66

7+
import redis.clients.jedis.Jedis;
8+
79
public interface RedisGraph extends Closeable {
810

11+
public static RedisGraph with(Jedis jedis) {
12+
return new com.redislabs.redisgraph.impl.api.RedisGraph(jedis);
13+
}
14+
915
/**
1016
* Execute a Cypher query.
1117
* @param graphId a graph to perform the query on

src/main/java/com/redislabs/redisgraph/exceptions/JRedisGraphException.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
import redis.clients.jedis.exceptions.JedisDataException;
44

55
/**
6-
* RedisGraph query evaluation exception. An instance of JRedisGraphException is thrown when RedisGraph
7-
* encounters an error during query evaluation.
6+
* RedisGraph query evaluation exception. An instance of JRedisGraphException is
7+
* thrown when RedisGraph encounters an error during query evaluation.
88
*/
99
public class JRedisGraphException extends JedisDataException {
10-
public JRedisGraphException(String message) {
11-
super(message);
12-
}
10+
private static final long serialVersionUID = -476099681322055468L;
1311

14-
public JRedisGraphException(Throwable cause) {
15-
super(cause);
16-
}
12+
public JRedisGraphException(String message) {
13+
super(message);
14+
}
1715

18-
public JRedisGraphException(String message, Throwable cause) {
19-
super(message, cause);
20-
}
16+
public JRedisGraphException(Throwable cause) {
17+
super(cause);
18+
}
19+
20+
public JRedisGraphException(String message, Throwable cause) {
21+
super(message, cause);
22+
}
2123
}

src/main/java/com/redislabs/redisgraph/graph_entities/Property.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private boolean valueEquals(Object value1, Object value2) {
7373
public boolean equals(Object o) {
7474
if (this == o) return true;
7575
if (!(o instanceof Property)) return false;
76-
Property property = (Property) o;
76+
Property<?> property = (Property<?>) o;
7777
return Objects.equals(name, property.name) &&
7878
valueEquals(value, property.value);
7979
}

src/main/java/com/redislabs/redisgraph/impl/Utils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Utils {
2323
escapeJavaMap.put("\"", "\\\"");
2424
ESCAPE_CHYPER = new AggregateTranslator(new LookupTranslator(Collections.unmodifiableMap(escapeJavaMap)));
2525
}
26-
26+
2727
private Utils() {}
2828

2929
/**
@@ -87,9 +87,8 @@ private static String arrayToString(Object[] arr) {
8787
}
8888

8989
private static String valueToString(Object value) {
90-
if(value == null)
91-
return "null";
92-
90+
if(value == null) return "null";
91+
9392
if(value instanceof String){
9493
return quoteString((String) value);
9594
}
@@ -102,7 +101,8 @@ private static String valueToString(Object value) {
102101

103102
}
104103
if(value instanceof List){
105-
List<Object> list = (List<Object>)value;
104+
@SuppressWarnings("unchecked")
105+
List<Object> list = (List<Object>) value;
106106
return arrayToString(list.toArray());
107107
}
108108
return value.toString();

src/main/java/com/redislabs/redisgraph/impl/api/ContextedRedisGraph.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package com.redislabs.redisgraph.impl.api;
22

3+
import java.util.List;
4+
35
import com.redislabs.redisgraph.RedisGraphContext;
46
import com.redislabs.redisgraph.ResultSet;
57
import com.redislabs.redisgraph.exceptions.JRedisGraphException;
68
import com.redislabs.redisgraph.impl.Utils;
79
import com.redislabs.redisgraph.impl.graph_cache.RedisGraphCaches;
810
import com.redislabs.redisgraph.impl.resultset.ResultSetImpl;
11+
912
import redis.clients.jedis.Client;
1013
import redis.clients.jedis.Jedis;
11-
import redis.clients.jedis.Pipeline;
12-
import redis.clients.jedis.util.SafeEncoder;
1314
import redis.clients.jedis.exceptions.JedisDataException;
14-
import java.util.List;
15+
import redis.clients.jedis.util.SafeEncoder;
1516

1617
/**
1718
* An implementation of RedisGraphContext. Allows sending RedisGraph and some Redis commands,
@@ -26,7 +27,7 @@ public class ContextedRedisGraph extends AbstractRedisGraph implements RedisGrap
2627
* Generates a new instance with a specific Jedis connection
2728
* @param connectionContext
2829
*/
29-
public ContextedRedisGraph(Jedis connectionContext){
30+
public ContextedRedisGraph(Jedis connectionContext) {
3031
this.connectionContext = connectionContext;
3132
}
3233

@@ -49,13 +50,12 @@ protected Jedis getConnection() {
4950
protected ResultSet sendQuery(String graphId, String preparedQuery) {
5051
Jedis conn = getConnection();
5152
try {
53+
@SuppressWarnings("unchecked")
5254
List<Object> rawResponse = (List<Object>) conn.sendCommand(RedisGraphCommand.QUERY, graphId, preparedQuery, Utils.COMPACT_STRING);
5355
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
54-
}
55-
catch (JRedisGraphException rt) {
56+
} catch (JRedisGraphException rt) {
5657
throw rt;
57-
}
58-
catch (JedisDataException j) {
58+
} catch (JedisDataException j) {
5959
throw new JRedisGraphException(j);
6060
}
6161
}
@@ -70,13 +70,12 @@ protected ResultSet sendQuery(String graphId, String preparedQuery) {
7070
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery) {
7171
Jedis conn = getConnection();
7272
try {
73+
@SuppressWarnings("unchecked")
7374
List<Object> rawResponse = (List<Object>) conn.sendCommand(RedisGraphCommand.RO_QUERY, graphId, preparedQuery, Utils.COMPACT_STRING);
7475
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
75-
}
76-
catch (JRedisGraphException ge) {
76+
} catch (JRedisGraphException ge) {
7777
throw ge;
78-
}
79-
catch (JedisDataException de) {
78+
} catch (JedisDataException de) {
8079
throw new JRedisGraphException(de);
8180
}
8281
}
@@ -92,14 +91,13 @@ protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery) {
9291
protected ResultSet sendQuery(String graphId, String preparedQuery, long timeout) {
9392
Jedis conn = getConnection();
9493
try {
94+
@SuppressWarnings("unchecked")
9595
List<Object> rawResponse = (List<Object>) conn.sendBlockingCommand(RedisGraphCommand.QUERY,
96-
graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout));
96+
graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout));
9797
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
98-
}
99-
catch (JRedisGraphException rt) {
98+
} catch (JRedisGraphException rt) {
10099
throw rt;
101-
}
102-
catch (JedisDataException j) {
100+
} catch (JedisDataException j) {
103101
throw new JRedisGraphException(j);
104102
}
105103
}
@@ -115,14 +113,13 @@ protected ResultSet sendQuery(String graphId, String preparedQuery, long timeout
115113
protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout) {
116114
Jedis conn = getConnection();
117115
try {
116+
@SuppressWarnings("unchecked")
118117
List<Object> rawResponse = (List<Object>) conn.sendBlockingCommand(RedisGraphCommand.RO_QUERY,
119118
graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout));
120119
return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId));
121-
}
122-
catch (JRedisGraphException ge) {
120+
} catch (JRedisGraphException ge) {
123121
throw ge;
124-
}
125-
catch (JedisDataException de) {
122+
} catch (JedisDataException de) {
126123
throw new JRedisGraphException(de);
127124
}
128125
}
@@ -145,11 +142,11 @@ public RedisGraphTransaction multi() {
145142
Client client = jedis.getClient();
146143
client.multi();
147144
client.getOne();
148-
RedisGraphTransaction transaction = new RedisGraphTransaction(client,this);
145+
RedisGraphTransaction transaction = new RedisGraphTransaction(client, this);
149146
transaction.setRedisGraphCaches(caches);
150147
return transaction;
151148
}
152-
149+
153150
/**
154151
* Creates a new RedisGraphPipeline pipeline object
155152
* @return new RedisGraphPipeline
@@ -158,7 +155,7 @@ public RedisGraphTransaction multi() {
158155
public RedisGraphPipeline pipelined() {
159156
Jedis jedis = getConnection();
160157
Client client = jedis.getClient();
161-
RedisGraphPipeline pipeline = new RedisGraphPipeline(client, this);
158+
RedisGraphPipeline pipeline = new RedisGraphPipeline(client, this);
162159
pipeline.setRedisGraphCaches(caches);
163160
return pipeline;
164161
}
@@ -193,8 +190,7 @@ public String deleteGraph(String graphId) {
193190
Object response;
194191
try {
195192
response = conn.sendCommand(RedisGraphCommand.DELETE, graphId);
196-
}
197-
catch (Exception e) {
193+
} catch (Exception e) {
198194
conn.close();
199195
throw e;
200196
}

src/main/java/com/redislabs/redisgraph/impl/api/RedisGraph.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
*/
1515
public class RedisGraph extends AbstractRedisGraph implements RedisGraphContextGenerator {
1616

17-
private final Pool<Jedis> client;
17+
private final Pool<Jedis> pool;
18+
private final Jedis jedis;
1819
private final RedisGraphCaches caches = new RedisGraphCaches();
1920

2021
/**
@@ -32,26 +33,31 @@ public RedisGraph() {
3233
* @param port Redis port
3334
*/
3435
public RedisGraph(String host, int port) {
35-
this( new JedisPool(host, port));
36+
this(new JedisPool(host, port));
3637
}
3738

3839
/**
3940
* Creates a client using provided Jedis pool
4041
*
41-
* @param jedis bring your own Jedis pool
42+
* @param pool bring your own Jedis pool
4243
*/
43-
public RedisGraph( Pool<Jedis> jedis) {
44-
this.client = jedis;
44+
public RedisGraph(Pool<Jedis> pool) {
45+
this.pool = pool;
46+
this.jedis = null;
4547
}
4648

49+
public RedisGraph(Jedis jedis) {
50+
this.jedis = jedis;
51+
this.pool = null;
52+
}
4753

4854
/**
4955
* Overrides the abstract function. Gets and returns a Jedis connection from the Jedis pool
5056
* @return a Jedis connection
5157
*/
5258
@Override
5359
protected Jedis getConnection() {
54-
return client.getResource();
60+
return jedis != null ? jedis : pool.getResource();
5561
}
5662

5763
/**
@@ -120,11 +126,15 @@ protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long
120126
* Closes the Jedis pool
121127
*/
122128
@Override
123-
public void close(){
124-
this.client.close();
129+
public void close() {
130+
if (pool != null) {
131+
pool.close();
132+
}
133+
if (jedis != null) {
134+
jedis.close();
135+
}
125136
}
126137

127-
128138
/**
129139
* Deletes the entire graph
130140
* @param graphId graph to delete

0 commit comments

Comments
 (0)