Skip to content

Commit 24f633b

Browse files
authored
Cleanups (#77)
* Code cleanups * replace range check with catching IndexOutOfBoundsException
1 parent 2283cb8 commit 24f633b

File tree

7 files changed

+45
-57
lines changed

7 files changed

+45
-57
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public abstract class GraphEntity {
1111
//members
1212
protected long id;
13-
protected final Map<String, Property> propertyMap = new HashMap<>();
13+
protected final Map<String, Property<?>> propertyMap = new HashMap<>();
1414

1515

1616
//setters & getters

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.redislabs.redisgraph.graph_entities;
22

3-
import java.util.ArrayList;
43
import java.util.List;
54
import java.util.Objects;
65

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.redislabs.redisgraph.graph_entities;
22

3-
import com.redislabs.redisgraph.ResultSet;
4-
53
import java.util.Objects;
64

75
/**

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ public class ResultSetImpl implements ResultSet {
2929
/**
3030
* @param rawResponse the raw representation of response is at most 3 lists of objects.
3131
* The last list is the statistics list.
32-
* @param redisGraph, the graph local cache
32+
* @param redisGraph the graph connection
33+
* @param cache the graph local cache
3334
*/
3435
public ResultSetImpl(List<Object> rawResponse, RedisGraph redisGraph, GraphCache cache) {
3536
this.redisGraph = redisGraph;
3637
this.cache = cache;
3738

38-
// If a run-time error occured, the last member of the rawResponse will be a JedisDataException.
39+
// If a run-time error occurred, the last member of the rawResponse will be a JedisDataException.
3940
if (rawResponse.get(rawResponse.size()-1) instanceof JedisDataException) {
4041

4142
throw new JRedisGraphRunTimeException((Throwable) rawResponse.get(rawResponse.size() - 1));
@@ -45,8 +46,8 @@ public ResultSetImpl(List<Object> rawResponse, RedisGraph redisGraph, GraphCache
4546

4647
header = parseHeader(new ArrayList<>());
4748
results = new ArrayList<>();
48-
statistics = rawResponse.size()> 0 ? parseStatistics(rawResponse.get(rawResponse.size() - 1)) :
49-
parseStatistics(new ArrayList<Objects>());
49+
statistics = rawResponse.isEmpty() ? parseStatistics(new ArrayList<Objects>()) :
50+
parseStatistics(rawResponse.get(rawResponse.size() - 1)) ;
5051

5152
} else {
5253

@@ -85,15 +86,12 @@ private List<Record> parseResult(List<List<Object>> rawResultSet) {
8586
case COLUMN_RELATION:
8687
parsedRow.add(deserializeEdge(obj));
8788
break;
88-
case COLUMN_SCALAR: {
89+
case COLUMN_SCALAR:
8990
parsedRow.add(deserializeScalar(obj));
9091
break;
91-
}
92-
default: {
92+
default:
9393
parsedRow.add(null);
9494
break;
95-
}
96-
9795
}
9896

9997
}
@@ -203,7 +201,7 @@ private void deserializeGraphEntityProperties(GraphEntity entity, List<List<Obje
203201

204202

205203
for (List<Object> rawProperty : rawProperties) {
206-
Property property = new Property();
204+
Property<Object> property = new Property<>();
207205
property.setName(cache.getPropertyName(((Long) rawProperty.get(0)).intValue(),
208206
redisGraph));
209207

src/main/java/com/redislabs/redisgraph/impl/resultset/ResultSetScalarTypes.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ enum ResultSetScalarTypes {
1414
VALUE_NODE,
1515
VALUE_PATH;
1616

17-
18-
static ResultSetScalarTypes[] values = values();
17+
private static final ResultSetScalarTypes[] values = values();
1918

2019
public static ResultSetScalarTypes getValue(int index) {
21-
if (index < 0 || index > values.length) throw new JedisDataException("Unrecognized response type");
20+
try {
2221
return values[index];
22+
} catch(IndexOutOfBoundsException e) {
23+
throw new JedisDataException("Unrecognized response type");
24+
}
2325
}
2426

2527
}

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

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,15 @@ public void testRecord(){
203203
String place = "TLV";
204204
int since = 2000;
205205

206+
Property<String> nameProperty = new Property<>("name", name);
207+
Property<Integer> ageProperty = new Property<>("age", age);
208+
Property<Double> doubleProperty = new Property<>("doubleValue", doubleValue);
209+
Property<Boolean> trueBooleanProperty = new Property<>("boolValue", true);
210+
Property<Boolean> falseBooleanProperty = new Property<>("boolValue", false);
211+
Property<?> nullProperty = new Property<>("nullValue", null);
206212

207-
208-
Property nameProperty = new Property("name", name);
209-
Property ageProperty = new Property("age", age);
210-
Property doubleProperty = new Property("doubleValue", doubleValue);
211-
Property trueBooleanProperty = new Property("boolValue", true);
212-
Property falseBooleanProperty = new Property("boolValue", false);
213-
Property nullProperty = new Property("nullValue", null);
214-
215-
Property placeProperty = new Property("place", place);
216-
Property sinceProperty = new Property("since", since);
213+
Property<String> placeProperty = new Property<>("place", place);
214+
Property<Integer> sinceProperty = new Property<>("since", since);
217215

218216
Node expectedNode = new Node();
219217
expectedNode.setId(0);
@@ -340,9 +338,9 @@ public void testMultiThread(){
340338
mapToObj(i-> api.query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, a.age")).
341339
collect(Collectors.toList());
342340

343-
Property nameProperty = new Property("name", "roi");
344-
Property ageProperty = new Property("age", 32);
345-
Property lastNameProperty =new Property("lastName", "a");
341+
Property<String> nameProperty = new Property<>("name", "roi");
342+
Property<Integer> ageProperty = new Property<>("age", 32);
343+
Property<String> lastNameProperty =new Property<>("lastName", "a");
346344

347345
Node expectedNode = new Node();
348346
expectedNode.setId(0);
@@ -423,9 +421,9 @@ public void testAdditionToProcedures(){
423421
Assert.assertNotNull(api.query("social", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)"));
424422

425423
//expected objects init
426-
Property nameProperty = new Property("name", "roi");
427-
Property ageProperty = new Property("age", 32);
428-
Property lastNameProperty =new Property("lastName", "a");
424+
Property<String> nameProperty = new Property<>("name", "roi");
425+
Property<Integer> ageProperty = new Property<>("age", 32);
426+
Property<String> lastNameProperty =new Property<>("lastName", "a");
429427

430428
Node expectedNode = new Node();
431429
expectedNode.setId(0);
@@ -549,7 +547,7 @@ public void testMultiExec(){
549547
Assert.assertEquals(1, schemaNames.size());
550548
Assert.assertEquals("n", schemaNames.get(0));
551549

552-
Property nameProperty = new Property("name", "a");
550+
Property<String> nameProperty = new Property<>("name", "a");
553551

554552
Node expectedNode = new Node();
555553
expectedNode.setId(0);
@@ -596,15 +594,15 @@ public void testContextedAPI() {
596594
int since = 2000;
597595

598596

599-
Property nameProperty = new Property("name", name);
600-
Property ageProperty = new Property("age", age);
601-
Property doubleProperty = new Property("doubleValue", doubleValue);
602-
Property trueBooleanProperty = new Property("boolValue", true);
603-
Property falseBooleanProperty = new Property("boolValue", false);
604-
Property nullProperty = new Property("nullValue", null);
597+
Property<String> nameProperty = new Property<>("name", name);
598+
Property<Integer> ageProperty = new Property<>("age", age);
599+
Property<Double> doubleProperty = new Property<>("doubleValue", doubleValue);
600+
Property<Boolean> trueBooleanProperty = new Property<>("boolValue", true);
601+
Property<Boolean> falseBooleanProperty = new Property<>("boolValue", false);
602+
Property<?> nullProperty = new Property<>("nullValue", null);
605603

606-
Property placeProperty = new Property("place", place);
607-
Property sinceProperty = new Property("since", since);
604+
Property<String> placeProperty = new Property<>("place", place);
605+
Property<Integer> sinceProperty = new Property<>("since", since);
608606

609607
Node expectedNode = new Node();
610608
expectedNode.setId(0);
@@ -737,9 +735,9 @@ public void testArraySupport() {
737735
Node expectedANode = new Node();
738736
expectedANode.setId(0);
739737
expectedANode.addLabel("person");
740-
Property aNameProperty = new Property("name", "a");
741-
Property aAgeProperty = new Property("age", 32);
742-
Property aListProperty = new Property("array", Arrays.asList(0L, 1L, 2L));
738+
Property<String> aNameProperty = new Property<>("name", "a");
739+
Property<Integer> aAgeProperty = new Property<>("age", 32);
740+
Property<List<Long>> aListProperty = new Property<>("array", Arrays.asList(0L, 1L, 2L));
743741
expectedANode.addProperty(aNameProperty);
744742
expectedANode.addProperty(aAgeProperty);
745743
expectedANode.addProperty(aListProperty);
@@ -748,9 +746,9 @@ public void testArraySupport() {
748746
Node expectedBNode = new Node();
749747
expectedBNode.setId(1);
750748
expectedBNode.addLabel("person");
751-
Property bNameProperty = new Property("name", "b");
752-
Property bAgeProperty = new Property("age", 30);
753-
Property bListProperty = new Property("array", Arrays.asList(3L, 4L, 5L));
749+
Property<String> bNameProperty = new Property<>("name", "b");
750+
Property<Integer> bAgeProperty = new Property<>("age", 30);
751+
Property<List<Long>> bListProperty = new Property<>("array", Arrays.asList(3L, 4L, 5L));
754752
expectedBNode.addProperty(bNameProperty);
755753
expectedBNode.addProperty(bAgeProperty);
756754
expectedBNode.addProperty(bListProperty);
@@ -783,7 +781,7 @@ public void testArraySupport() {
783781
Assert.assertEquals(Arrays.asList("x"), record.keys());
784782

785783

786-
List x = record.getValue("x");
784+
List<Long> x = record.getValue("x");
787785
Assert.assertEquals(Arrays.asList(0L, 1L, 2L), x);
788786

789787
// test collect
@@ -928,18 +926,14 @@ record = resultSet.next();
928926
// Test a query that produces 2 records, the first containing a path and the second containing a null value.
929927
resultSet = api.query("social", "MATCH (a) OPTIONAL MATCH p = (a)-[e]->(b) RETURN p");
930928
Assert.assertEquals(2, resultSet.size());
929+
931930
record = resultSet.next();
932931
Assert.assertEquals(1, record.size());
933-
934-
Object path = record.getValue(0);
935932
Assert.assertNotNull(record.getValue(0));
936933

937934
record = resultSet.next();
938935
Assert.assertEquals(1, record.size());
939-
940-
path = record.getValue(0);
941936
Assert.assertNull(record.getValue(0));
942-
943937
}
944938

945939
@Test

src/test/java/com/redislabs/redisgraph/graph_entities/PathTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import nl.jqno.equalsverifier.EqualsVerifier;
44
import org.junit.Test;
55

6-
import java.util.ArrayList;
76
import java.util.List;
87
import java.util.concurrent.ThreadLocalRandom;
98
import java.util.stream.Collectors;
@@ -28,12 +27,10 @@ private Edge buildEdge(int id, int src, int dst){
2827
}
2928

3029
private List<Node> buildNodeArray(int size) {
31-
List<Node> nodes = new ArrayList<>();
3230
return IntStream.range(0, size).mapToObj(i -> buildNode(i)).collect(Collectors.toList());
3331
}
3432

3533
private List<Edge> buildEdgeArray(int size){
36-
List<Node> nodes = new ArrayList<>();
3734
return IntStream.range(0, size).mapToObj(i -> buildEdge(i, i, i+1)).collect(Collectors.toList());
3835
}
3936

0 commit comments

Comments
 (0)