Skip to content

Commit

Permalink
Cleanups (#77)
Browse files Browse the repository at this point in the history
* Code cleanups

* replace range check with catching IndexOutOfBoundsException
  • Loading branch information
gkorland authored Jul 23, 2020
1 parent 2283cb8 commit 24f633b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public abstract class GraphEntity {
//members
protected long id;
protected final Map<String, Property> propertyMap = new HashMap<>();
protected final Map<String, Property<?>> propertyMap = new HashMap<>();


//setters & getters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.redislabs.redisgraph.graph_entities;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.redislabs.redisgraph.graph_entities;

import com.redislabs.redisgraph.ResultSet;

import java.util.Objects;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ public class ResultSetImpl implements ResultSet {
/**
* @param rawResponse the raw representation of response is at most 3 lists of objects.
* The last list is the statistics list.
* @param redisGraph, the graph local cache
* @param redisGraph the graph connection
* @param cache the graph local cache
*/
public ResultSetImpl(List<Object> rawResponse, RedisGraph redisGraph, GraphCache cache) {
this.redisGraph = redisGraph;
this.cache = cache;

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

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

header = parseHeader(new ArrayList<>());
results = new ArrayList<>();
statistics = rawResponse.size()> 0 ? parseStatistics(rawResponse.get(rawResponse.size() - 1)) :
parseStatistics(new ArrayList<Objects>());
statistics = rawResponse.isEmpty() ? parseStatistics(new ArrayList<Objects>()) :
parseStatistics(rawResponse.get(rawResponse.size() - 1)) ;

} else {

Expand Down Expand Up @@ -85,15 +86,12 @@ private List<Record> parseResult(List<List<Object>> rawResultSet) {
case COLUMN_RELATION:
parsedRow.add(deserializeEdge(obj));
break;
case COLUMN_SCALAR: {
case COLUMN_SCALAR:
parsedRow.add(deserializeScalar(obj));
break;
}
default: {
default:
parsedRow.add(null);
break;
}

}

}
Expand Down Expand Up @@ -203,7 +201,7 @@ private void deserializeGraphEntityProperties(GraphEntity entity, List<List<Obje


for (List<Object> rawProperty : rawProperties) {
Property property = new Property();
Property<Object> property = new Property<>();
property.setName(cache.getPropertyName(((Long) rawProperty.get(0)).intValue(),
redisGraph));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ enum ResultSetScalarTypes {
VALUE_NODE,
VALUE_PATH;


static ResultSetScalarTypes[] values = values();
private static final ResultSetScalarTypes[] values = values();

public static ResultSetScalarTypes getValue(int index) {
if (index < 0 || index > values.length) throw new JedisDataException("Unrecognized response type");
try {
return values[index];
} catch(IndexOutOfBoundsException e) {
throw new JedisDataException("Unrecognized response type");
}
}

}
68 changes: 31 additions & 37 deletions src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,15 @@ public void testRecord(){
String place = "TLV";
int since = 2000;

Property<String> nameProperty = new Property<>("name", name);
Property<Integer> ageProperty = new Property<>("age", age);
Property<Double> doubleProperty = new Property<>("doubleValue", doubleValue);
Property<Boolean> trueBooleanProperty = new Property<>("boolValue", true);
Property<Boolean> falseBooleanProperty = new Property<>("boolValue", false);
Property<?> nullProperty = new Property<>("nullValue", null);


Property nameProperty = new Property("name", name);
Property ageProperty = new Property("age", age);
Property doubleProperty = new Property("doubleValue", doubleValue);
Property trueBooleanProperty = new Property("boolValue", true);
Property falseBooleanProperty = new Property("boolValue", false);
Property nullProperty = new Property("nullValue", null);

Property placeProperty = new Property("place", place);
Property sinceProperty = new Property("since", since);
Property<String> placeProperty = new Property<>("place", place);
Property<Integer> sinceProperty = new Property<>("since", since);

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

Property nameProperty = new Property("name", "roi");
Property ageProperty = new Property("age", 32);
Property lastNameProperty =new Property("lastName", "a");
Property<String> nameProperty = new Property<>("name", "roi");
Property<Integer> ageProperty = new Property<>("age", 32);
Property<String> lastNameProperty =new Property<>("lastName", "a");

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

//expected objects init
Property nameProperty = new Property("name", "roi");
Property ageProperty = new Property("age", 32);
Property lastNameProperty =new Property("lastName", "a");
Property<String> nameProperty = new Property<>("name", "roi");
Property<Integer> ageProperty = new Property<>("age", 32);
Property<String> lastNameProperty =new Property<>("lastName", "a");

Node expectedNode = new Node();
expectedNode.setId(0);
Expand Down Expand Up @@ -549,7 +547,7 @@ public void testMultiExec(){
Assert.assertEquals(1, schemaNames.size());
Assert.assertEquals("n", schemaNames.get(0));

Property nameProperty = new Property("name", "a");
Property<String> nameProperty = new Property<>("name", "a");

Node expectedNode = new Node();
expectedNode.setId(0);
Expand Down Expand Up @@ -596,15 +594,15 @@ public void testContextedAPI() {
int since = 2000;


Property nameProperty = new Property("name", name);
Property ageProperty = new Property("age", age);
Property doubleProperty = new Property("doubleValue", doubleValue);
Property trueBooleanProperty = new Property("boolValue", true);
Property falseBooleanProperty = new Property("boolValue", false);
Property nullProperty = new Property("nullValue", null);
Property<String> nameProperty = new Property<>("name", name);
Property<Integer> ageProperty = new Property<>("age", age);
Property<Double> doubleProperty = new Property<>("doubleValue", doubleValue);
Property<Boolean> trueBooleanProperty = new Property<>("boolValue", true);
Property<Boolean> falseBooleanProperty = new Property<>("boolValue", false);
Property<?> nullProperty = new Property<>("nullValue", null);

Property placeProperty = new Property("place", place);
Property sinceProperty = new Property("since", since);
Property<String> placeProperty = new Property<>("place", place);
Property<Integer> sinceProperty = new Property<>("since", since);

Node expectedNode = new Node();
expectedNode.setId(0);
Expand Down Expand Up @@ -737,9 +735,9 @@ public void testArraySupport() {
Node expectedANode = new Node();
expectedANode.setId(0);
expectedANode.addLabel("person");
Property aNameProperty = new Property("name", "a");
Property aAgeProperty = new Property("age", 32);
Property aListProperty = new Property("array", Arrays.asList(0L, 1L, 2L));
Property<String> aNameProperty = new Property<>("name", "a");
Property<Integer> aAgeProperty = new Property<>("age", 32);
Property<List<Long>> aListProperty = new Property<>("array", Arrays.asList(0L, 1L, 2L));
expectedANode.addProperty(aNameProperty);
expectedANode.addProperty(aAgeProperty);
expectedANode.addProperty(aListProperty);
Expand All @@ -748,9 +746,9 @@ public void testArraySupport() {
Node expectedBNode = new Node();
expectedBNode.setId(1);
expectedBNode.addLabel("person");
Property bNameProperty = new Property("name", "b");
Property bAgeProperty = new Property("age", 30);
Property bListProperty = new Property("array", Arrays.asList(3L, 4L, 5L));
Property<String> bNameProperty = new Property<>("name", "b");
Property<Integer> bAgeProperty = new Property<>("age", 30);
Property<List<Long>> bListProperty = new Property<>("array", Arrays.asList(3L, 4L, 5L));
expectedBNode.addProperty(bNameProperty);
expectedBNode.addProperty(bAgeProperty);
expectedBNode.addProperty(bListProperty);
Expand Down Expand Up @@ -783,7 +781,7 @@ public void testArraySupport() {
Assert.assertEquals(Arrays.asList("x"), record.keys());


List x = record.getValue("x");
List<Long> x = record.getValue("x");
Assert.assertEquals(Arrays.asList(0L, 1L, 2L), x);

// test collect
Expand Down Expand Up @@ -928,18 +926,14 @@ record = resultSet.next();
// Test a query that produces 2 records, the first containing a path and the second containing a null value.
resultSet = api.query("social", "MATCH (a) OPTIONAL MATCH p = (a)-[e]->(b) RETURN p");
Assert.assertEquals(2, resultSet.size());

record = resultSet.next();
Assert.assertEquals(1, record.size());

Object path = record.getValue(0);
Assert.assertNotNull(record.getValue(0));

record = resultSet.next();
Assert.assertEquals(1, record.size());

path = record.getValue(0);
Assert.assertNull(record.getValue(0));

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
Expand All @@ -28,12 +27,10 @@ private Edge buildEdge(int id, int src, int dst){
}

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

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

Expand Down

0 comments on commit 24f633b

Please sign in to comment.