Skip to content

Commit 6330e6b

Browse files
author
DvirDukhan
committed
fixed according to pull request comments
1 parent 615db35 commit 6330e6b

File tree

10 files changed

+73
-61
lines changed

10 files changed

+73
-61
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.util.List;
44

5+
/**
6+
* Query response header interface. Represents the response schame (column names and types)
7+
*/
58
public interface Header {
69

710

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

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.redislabs.redisgraph;
22

33
import java.util.*;
4+
import java.util.concurrent.ConcurrentHashMap;
45
import java.util.stream.Collectors;
56

67

@@ -22,11 +23,13 @@ public class RedisGraphAPI {
2223

2324
private final Pool<Jedis> client;
2425
private final String graphId;
25-
private List<String> labels = new ArrayList<>();
26-
private List<String> relationshipTypes = new ArrayList<>();
27-
private List<String> propertyNames = new ArrayList<>();
26+
private final List<String> labels = new ArrayList<>();
27+
private final List<String> relationshipTypes = new ArrayList<>();
28+
private final List<String> propertyNames = new ArrayList<>();
29+
30+
private static final Map<String, RedisGraphAPI> apiMap = new ConcurrentHashMap<>();
31+
2832

29-
private static RedisGraphAPI redisGraphAPI;
3033

3134

3235

@@ -67,7 +70,8 @@ public RedisGraphAPI(String graphId, String host, int port) {
6770
public RedisGraphAPI(String graphId, Pool<Jedis> jedis) {
6871
this.graphId = graphId;
6972
this.client = jedis;
70-
redisGraphAPI = this;
73+
apiMap.put(graphId, this);
74+
7175

7276
}
7377

@@ -79,7 +83,6 @@ public RedisGraphAPI(String graphId, Pool<Jedis> jedis) {
7983
* @return a result set
8084
*/
8185
public ResultSet query(String query, Object ...args) {
82-
StringBuilder sb = new StringBuilder();
8386
if(args.length > 0) {
8487
for(int i=0; i<args.length; ++i) {
8588
if(args[i] instanceof String) {
@@ -90,7 +93,7 @@ public ResultSet query(String query, Object ...args) {
9093
}
9194

9295
try (Jedis conn = getConnection()) {
93-
return new ResultSetImpl(sendCompactCommand(conn, Command.QUERY, graphId, query).getObjectMultiBulkReply());
96+
return new ResultSetImpl(sendCompactCommand(conn, Command.QUERY, graphId, query).getObjectMultiBulkReply(), graphId);
9497
}
9598
}
9699

@@ -130,10 +133,10 @@ private BinaryClient sendCommand(Jedis conn, ProtocolCommand provider, String ..
130133
*/
131134
private BinaryClient sendCompactCommand(Jedis conn, ProtocolCommand provider, String ...args) {
132135
BinaryClient binaryClient = conn.getClient();
133-
List<String> largs = new ArrayList<>(Arrays.asList(args));
134-
largs.add("--COMPACT");
135-
String[] t = new String[largs.size()];
136-
binaryClient.sendCommand(provider, largs.toArray(t));
136+
String[] t = new String[args.length +1];
137+
System.arraycopy(args, 0 , t, 0, args.length);
138+
t[args.length]="--COMPACT";
139+
binaryClient.sendCommand(provider, t);
137140
return binaryClient;
138141
}
139142

@@ -174,24 +177,19 @@ public ResultSet callProcedure(String procedure, List<String> args ){
174177
*/
175178
public String getLabel(int index){
176179
if (index >= labels.size()){
177-
labels = getLabels();
180+
getLabels();
178181
}
179182
return labels.get(index);
180183
}
181184

182185
/**
183186
*
184-
* @return list of all the node labels in the graph
187+
* gets a list of all the node labels in the graph
185188
*/
186-
private List<String> getLabels() {
187-
ResultSet resultSet = callProcedure("db.labels");
188-
ArrayList<String> labels = new ArrayList<>();
189-
while (resultSet.hasNext()){
190-
Record record = resultSet.next();
191-
labels.add(record.getString(0));
189+
private void getLabels() {
190+
getProcedureInfo(labels, "db.labels");
191+
192192

193-
}
194-
return labels;
195193
}
196194

197195

@@ -202,25 +200,19 @@ private List<String> getLabels() {
202200
*/
203201
public String getRelationshipType(int index){
204202
if (index >= relationshipTypes.size()){
205-
relationshipTypes = getRelationshipTypes();
203+
getRelationshipTypes();
206204
}
207205
return relationshipTypes.get(index);
208206
}
209207

210208

211209
/**
212210
*
213-
* @return a list of the edge relationship types in the graph
211+
* gets a list of the edge relationship types in the graph
214212
*/
215-
private List<String> getRelationshipTypes() {
216-
ResultSet resultSet = callProcedure("db.relationshipTypes");
217-
ArrayList<String> relationshipTypes = new ArrayList<>();
218-
while (resultSet.hasNext()){
219-
Record record = resultSet.next();
220-
relationshipTypes.add(record.getString(0));
213+
private void getRelationshipTypes() {
214+
getProcedureInfo(relationshipTypes, "db.relationshipTypes" );
221215

222-
}
223-
return relationshipTypes;
224216
}
225217

226218

@@ -231,27 +223,39 @@ private List<String> getRelationshipTypes() {
231223
*/
232224
public String getPropertyName(int index){
233225
if (index >= propertyNames.size()){
234-
propertyNames = getPropertyNames();
226+
getPropertyNames();
235227
}
236228
return propertyNames.get(index);
237229
}
238230

239231

240232
/**
241233
*
242-
* @return a list of all the property names in the graph
234+
* gets a list of all the property names in the graph
243235
*/
244-
private List<String> getPropertyNames() {
245-
ResultSet resultSet = callProcedure("db.propertyKeys");
246-
ArrayList<String> propertyNames = new ArrayList<>();
236+
private void getPropertyNames() {
237+
getProcedureInfo(propertyNames, "db.propertyKeys");
238+
239+
240+
}
241+
242+
/**
243+
* Calls procedure in the graph and returns its data
244+
* @param list a list for storing the data
245+
* @param procedure to call in order to get the data
246+
*/
247+
private void getProcedureInfo(List<String> list, String procedure){
248+
ResultSet resultSet = callProcedure(procedure);
249+
list.clear();
247250
while (resultSet.hasNext()){
248251
Record record = resultSet.next();
249-
propertyNames.add(record.getString(0));
252+
list.add(record.getString(0));
250253

251254
}
252-
return propertyNames;
255+
253256
}
254257

258+
255259
/**
256260
* Invoke a stored procedure
257261
* @param procedure
@@ -277,7 +281,7 @@ public ResultSet callProcedure(String procedure, List<String> args , Map<String
277281
* static function to access an initialized graph api
278282
* @return an initialized instance of RedisGraphAPI
279283
*/
280-
public static RedisGraphAPI getInstance(){
281-
return redisGraphAPI;
284+
public static RedisGraphAPI getInstance(String graphId){
285+
return apiMap.get(graphId);
282286
}
283287
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ public class Utils {
1111
* @return the input string surounded with quotation marks, if needed
1212
*/
1313
public static String quoteString(String str){
14-
StringBuilder sb = new StringBuilder();
14+
if(str.startsWith("\"") && str.endsWith("\"")){
15+
return str;
16+
}
17+
18+
StringBuilder sb = new StringBuilder(str.length()+2);
1519
if(str.charAt(0)!='"'){
1620
sb.append('"');
1721
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
public class Edge extends GraphEntity {
1010

1111
//memebers
12-
String relationshipType;
13-
int source;
14-
int destination;
12+
private String relationshipType;
13+
private int source;
14+
private int destination;
1515

1616

1717
//getters & setters

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public abstract class GraphEntity {
1616

1717
//members
1818

19-
int id;
20-
Map<String, Property> propertyMap = new HashMap<>();
19+
protected int id;
20+
protected final Map<String, Property> propertyMap = new HashMap<>();
2121

2222

2323
//setters & getters
@@ -58,7 +58,7 @@ public void addProperty(String name, ResultSetScalarTypes type, Object value){
5858
public void addProperty (Property property){
5959

6060

61-
propertyMap.put(property.name, property);
61+
propertyMap.put(property.getName(), property);
6262
}
6363

6464
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private void buildSchema() {
6060
for (List<Object> tuple : this.raw) {
6161

6262
//get type
63-
ResultSetColumnTypes type = ResultSetColumnTypes.values()[(int) (long) tuple.get(0)];
63+
ResultSetColumnTypes type = ResultSetColumnTypes.values()[((Long) tuple.get(0)).intValue()];
6464
//get text
6565
String text = SafeEncoder.encode((byte[]) tuple.get(1));
6666
if (type != null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class Node extends GraphEntity {
1111

1212
//members
13-
List<String> labels = new ArrayList<>();
13+
final private List<String> labels = new ArrayList<>();
1414

1515
/**
1616
* @param label - a label to be add

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
public class Property {
1111

1212
//members
13-
String name;
14-
ResultSet.ResultSetScalarTypes type;
15-
Object value;
13+
private String name;
14+
private ResultSet.ResultSetScalarTypes type;
15+
private Object value;
1616

1717

1818
/**

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@
1111

1212
public class ResultSetImpl implements ResultSet {
1313

14-
Header header = new HeaderImpl(new ArrayList<>());
15-
Statistics statistics = new StatisticsImpl(new ArrayList<>());
14+
private Header header = new HeaderImpl(new ArrayList<>());
15+
private Statistics statistics = new StatisticsImpl(new ArrayList<>());
1616

1717
private final List<Record> results = new ArrayList<>();
1818

1919
private int position = 0;
20+
private final String graphId;
2021

2122
/**
2223
*
2324
* @param rawResponse the raw representation of response is at most 3 lists of objects.
2425
* The last list is the statistics list.
26+
* @param graphId, the graph ID
2527
*/
26-
public ResultSetImpl(List<Object> rawResponse){
27-
28+
public ResultSetImpl(List<Object> rawResponse, String graphId){
29+
this.graphId = graphId;
2830
if(rawResponse.size() != 3){
2931

3032
parseStatistics(rawResponse.get(rawResponse.size()-1));
@@ -50,7 +52,7 @@ private void parseResult(List<List<Object>> rawResultSet) {
5052
//go over each raw result
5153
for (List<Object> row : rawResultSet) {
5254

53-
List<Object> parsedRow = new ArrayList<>();
55+
List<Object> parsedRow = new ArrayList<>(row.size());
5456
//go over each object in the result
5557
for (int i = 0; i < row.size(); i++) {
5658
//get raw representation of the object
@@ -123,7 +125,7 @@ private Node deserializeNode(List<Object> rawNodeData) {
123125
deserializeGraphEntityId(node, rawNodeData.get(0));
124126
List<Long> labelsIndices = (List<Long>) rawNodeData.get(1);
125127
for (long labelIndex : labelsIndices) {
126-
String label = RedisGraphAPI.getInstance().getLabel((int) labelIndex);
128+
String label = RedisGraphAPI.getInstance(graphId).getLabel((int) labelIndex);
127129
node.addLabel(label);
128130
}
129131
deserializeGraphEntityProperties(node, (List<List<Object>>) rawNodeData.get(2));
@@ -155,7 +157,7 @@ private Edge deserializeEdge(List<Object> rawEdgeData) {
155157
Edge edge = new Edge();
156158
deserializeGraphEntityId(edge, rawEdgeData.get(0));
157159

158-
String relationshipType = RedisGraphAPI.getInstance().getRelationshipType((int) (long) rawEdgeData.get(1));
160+
String relationshipType = RedisGraphAPI.getInstance(graphId).getRelationshipType(((Long) rawEdgeData.get(1)).intValue());
159161
edge.setRelationshipType(relationshipType);
160162

161163
edge.setSource((int) (long) rawEdgeData.get(2));
@@ -179,7 +181,7 @@ void deserializeGraphEntityProperties(GraphEntity entity, List<List<Object>> raw
179181

180182
for (List<Object> rawProperty : rawProperties) {
181183
Property property = new Property();
182-
property.setName(RedisGraphAPI.getInstance().getPropertyName((int) (long) rawProperty.get(0)));
184+
property.setName(RedisGraphAPI.getInstance(graphId).getPropertyName( ((Long) rawProperty.get(0)).intValue()));
183185

184186
//trimmed for getting to value using deserializeScalar
185187
List<Object> propertyScalar = rawProperty.subList(1, rawProperty.size());

src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public void testCreateLabeledNode() {
5959
// Create a node with a label
6060
ResultSet resultSet = api.query("CREATE (:human{name:'danny',age:12})");
6161
Assert.assertFalse(resultSet.hasNext());
62-
//
6362
Assert.assertEquals("1", resultSet.getStatistics().getStringValue(Label.NODES_CREATED));
6463
Assert.assertEquals("2", resultSet.getStatistics().getStringValue(Label.PROPERTIES_SET));
6564
Assert.assertNotNull(resultSet.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME));

0 commit comments

Comments
 (0)