Skip to content

add getters/setters to all Resultset classes and dependencies - issue… #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: StatisticsImpl_jackson
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/redislabs/redisgraph/ResultSet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.redislabs.redisgraph;

import java.util.Iterator;
import java.util.List;

/**
* Hold a query result
Expand All @@ -13,4 +14,6 @@ public interface ResultSet extends Iterator<Record> {

Header getHeader();

List<Record> getResults();

}
6 changes: 6 additions & 0 deletions src/main/java/com/redislabs/redisgraph/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public static Label getEnum(String value) {
}
return null;
}

public String getText() {
return text;
}
}

/**
Expand All @@ -67,5 +71,7 @@ public static Label getEnum(String value) {

int propertiesSet();

String queryExecutionTime();

boolean cachedExecution();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public abstract class GraphEntity {

//setters & getters

public Map<String, Property<?>> getPropertyMap() {
return propertyMap;
}

/**
* @return entity id
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@

public class RecordImpl implements Record {

private final List<String> header;
private final List<Object> values;
private final List<String> header;
private final List<Object> values;

RecordImpl(List<String> header, List<Object> values){
public RecordImpl(List<String> header, List<Object> values){
this.header=header;
this.values = values;
}

public List<String> getHeader() {
return header;
}

public List<Object> getValues() {
return values;
}

@Override
public <T> T getValue(int index) {
return (T)this.values.get(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public Header getHeader() {
return header;
}

@Override
public List<Record> getResults() {
return results;
}

/**
* @param rawNodeData - raw node object in the form of list of object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public StatisticsImpl(){}
* @param raw a raw representation of the query execution statistics
*/
public StatisticsImpl(List<byte[]> raw){
this.raw = raw;
this.raw = raw;
}


Expand All @@ -43,10 +43,10 @@ public String getStringValue(Statistics.Label label) {
/**
* Lazy parse statistics on first call
*/
private Map<Statistics.Label, String> getStatistics(){
public Map<Statistics.Label, String> getStatistics(){
if(statistics.size() == 0 && this.raw != null) {
for(byte[] tuple : this.raw) {
String text = SafeEncoder.encode(tuple);
String text = SafeEncoder.encode(tuple);
String[] rowTuple = text.split(":");
if(rowTuple.length == 2) {
Statistics.Label label = Statistics.Label.getEnum(rowTuple[0]);
Expand Down Expand Up @@ -145,6 +145,15 @@ public boolean cachedExecution() {
return getIntValue(Label.CACHED_EXECUTION) == 1;
}

/**
*
* @return The execution time for the Query.
*/
@Override
public String queryExecutionTime() {
return getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
155 changes: 147 additions & 8 deletions src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.redislabs.redisgraph.graph_entities.Edge;
import com.redislabs.redisgraph.graph_entities.Node;
import com.redislabs.redisgraph.graph_entities.Path;
import com.redislabs.redisgraph.graph_entities.Property;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.redislabs.redisgraph.graph_entities.*;
import com.redislabs.redisgraph.impl.api.RedisGraph;
import com.redislabs.redisgraph.impl.resultset.RecordImpl;
import com.redislabs.redisgraph.impl.resultset.ResultSetImpl;
import com.redislabs.redisgraph.impl.resultset.StatisticsImpl;
import com.redislabs.redisgraph.test.utils.PathBuilder;
import org.junit.*;

Expand Down Expand Up @@ -223,13 +226,6 @@ public void testRecord(){
expectedNode.addProperty(doubleProperty);
expectedNode.addProperty(trueBooleanProperty);
expectedNode.addProperty(nullProperty);
Assert.assertEquals(
"Node{labels=[person], id=0, "
+ "propertyMap={name=Property{name='name', value=roi}, "
+ "boolValue=Property{name='boolValue', value=true}, "
+ "doubleValue=Property{name='doubleValue', value=3.14}, "
+ "nullValue=Property{name='nullValue', value=null}, "
+ "age=Property{name='age', value=32}}}", expectedNode.toString());

Edge expectedEdge = new Edge();
expectedEdge.setId(0);
Expand All @@ -241,6 +237,11 @@ public void testRecord(){
expectedEdge.addProperty(doubleProperty);
expectedEdge.addProperty(falseBooleanProperty);
expectedEdge.addProperty(nullProperty);
Assert.assertEquals("{boolValue=Property{name='boolValue', value=false}, "
+ "place=Property{name='place', value=TLV}, "
+ "doubleValue=Property{name='doubleValue', value=3.14}, "
+ "nullValue=Property{name='nullValue', value=null}, "
+ "since=Property{name='since', value=2000}}",expectedEdge.getPropertyMap().toString());
Assert.assertEquals("Edge{relationshipType='knows', source=0, destination=1, id=0, "
+ "propertyMap={boolValue=Property{name='boolValue', value=false}, "
+ "place=Property{name='place', value=TLV}, "
Expand Down Expand Up @@ -272,6 +273,8 @@ public void testRecord(){
Assert.assertEquals(0, resultSet.getStatistics().relationshipsCreated());
Assert.assertEquals(0, resultSet.getStatistics().relationshipsDeleted());
Assert.assertNotNull(resultSet.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME));
//Added the getQueryExecutionTime method
Assert.assertNotNull(resultSet.getStatistics().queryExecutionTime());


Assert.assertEquals(1, resultSet.size());
Expand Down Expand Up @@ -615,6 +618,13 @@ public void testContextedAPI() {
expectedNode.addProperty(doubleProperty);
expectedNode.addProperty(trueBooleanProperty);
expectedNode.addProperty(nullProperty);
Assert.assertEquals(
"Node{labels=[person], id=0, "
+ "propertyMap={name=Property{name='name', value=roi}, "
+ "boolValue=Property{name='boolValue', value=true}, "
+ "doubleValue=Property{name='doubleValue', value=3.14}, "
+ "nullValue=Property{name='nullValue', value=null}, "
+ "age=Property{name='age', value=32}}}", expectedNode.toString());

Edge expectedEdge = new Edge();
expectedEdge.setId(0);
Expand All @@ -626,6 +636,12 @@ public void testContextedAPI() {
expectedEdge.addProperty(doubleProperty);
expectedEdge.addProperty(falseBooleanProperty);
expectedEdge.addProperty(nullProperty);
Assert.assertEquals("Edge{relationshipType='knows', source=0, destination=1, id=0, "
+ "propertyMap={boolValue=Property{name='boolValue', value=false}, "
+ "place=Property{name='place', value=TLV}, "
+ "doubleValue=Property{name='doubleValue', value=3.14}, "
+ "nullValue=Property{name='nullValue', value=null}, "
+ "since=Property{name='since', value=2000}}}", expectedEdge.toString());

Map<String, Object> params = new HashMap<>();
params.put("name", name);
Expand Down Expand Up @@ -973,4 +989,127 @@ public void testCachedExecution() {
Assert.assertEquals(params.get("val"), r.getValue(0));
Assert.assertTrue(resultSet.getStatistics().cachedExecution());
}

@Test
public void testResultSetImplSerializable(){


String name = "roi";
int age = 32;
double doubleValue = 3.14;
boolean boolValue = true;

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<String> placeProperty = new Property<>("place", place);
Property<Integer> sinceProperty = new Property<>("since", since);

Node expectedNode = new Node();
expectedNode.setId(0);
expectedNode.addLabel("person");
expectedNode.addProperty(nameProperty);
expectedNode.addProperty(ageProperty);
expectedNode.addProperty(doubleProperty);
expectedNode.addProperty(trueBooleanProperty);
expectedNode.addProperty(nullProperty);

Edge expectedEdge = new Edge();
expectedEdge.setId(0);
expectedEdge.setSource(0);
expectedEdge.setDestination(1);
expectedEdge.setRelationshipType("knows");
expectedEdge.addProperty(placeProperty);
expectedEdge.addProperty(sinceProperty);
expectedEdge.addProperty(doubleProperty);
expectedEdge.addProperty(falseBooleanProperty);
expectedEdge.addProperty(nullProperty);
// PropertyMap getter tests
Assert.assertEquals("{name=Property{name='name', value=roi}, "
+ "boolValue=Property{name='boolValue', value=true}, "
+ "doubleValue=Property{name='doubleValue', value=3.14}, "
+ "nullValue=Property{name='nullValue', value=null}, "
+ "age=Property{name='age', value=32}}",expectedNode.getPropertyMap().toString());

Assert.assertEquals(
"Node{labels=[person], id=0, "
+ "propertyMap={name=Property{name='name', value=roi}, "
+ "boolValue=Property{name='boolValue', value=true}, "
+ "doubleValue=Property{name='doubleValue', value=3.14}, "
+ "nullValue=Property{name='nullValue', value=null}, "
+ "age=Property{name='age', value=32}}}", expectedNode.toString());

Map<String, Object> params = new HashMap<>();
params.put("name", name);
params.put("age", age);
params.put("boolValue", boolValue);
params.put("doubleValue", doubleValue);

Assert.assertNotNull(api.query("social", "CREATE (:person{name:$name,age:$age, doubleValue:$doubleValue, boolValue:$boolValue, nullValue:null})", params));
Assert.assertNotNull(api.query("social", "CREATE (:person{name:'amit',age:30})"));
Assert.assertNotNull(api.query("social", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') " +
"CREATE (a)-[:knows{place:'TLV', since:2000,doubleValue:3.14, boolValue:false, nullValue:null}]->(b)"));



ResultSet resultSet = api.query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, " +
"a.name, a.age, a.doubleValue, a.boolValue, a.nullValue, " +
"r.place, r.since, r.doubleValue, r.boolValue, r.nullValue");
Assert.assertNotNull(resultSet);
//New ResultSet.getResults() Method test
Assert.assertNotNull(resultSet.getResults());
Assert.assertEquals(1,resultSet.getResults().size());

Assert.assertFalse(resultSet.getResults().isEmpty());
Record record = resultSet.next();
Assert.assertFalse(resultSet.hasNext());



Node node = record.getValue(0);
Node node2 = resultSet.getResults().get(0).getValue(0);
Assert.assertNotNull(node);
Assert.assertNotNull(node2);
Assert.assertEquals(node,node2);

Assert.assertEquals(expectedNode, node2);

node = resultSet.getResults().get(0).getValue("a");
Assert.assertEquals(expectedNode, node);

Edge edge = resultSet.getResults().get(0).getValue(1);
Assert.assertNotNull(edge);
Assert.assertEquals(expectedEdge, edge);

edge = resultSet.getResults().get(0).getValue("r");
Assert.assertEquals(expectedEdge, edge);

Assert.assertEquals(Arrays.asList("a", "r", "a.name", "a.age", "a.doubleValue", "a.boolValue", "a.nullValue",
"r.place", "r.since", "r.doubleValue", "r.boolValue", "r.nullValue"), resultSet.getResults().get(0).keys());
//Serialization tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a scenario of serialization and deserialization?
resultsSet->JSON->resultSet
compare the original resultSet to the generated resultSet

ObjectMapper mapper = new ObjectMapper();


Assert.assertTrue((mapper.canSerialize(StatisticsImpl.class)));
Assert.assertTrue((mapper.canSerialize(RecordImpl.class)));
Assert.assertTrue((mapper.canSerialize(GraphEntity.class)));
Assert.assertTrue((mapper.canSerialize(ResultSetImpl.class)));
Assert.assertTrue((mapper.canSerialize(Property.class)));
Assert.assertTrue((mapper.canSerialize(Node.class)));
Assert.assertTrue((mapper.canSerialize(Edge.class)));
Assert.assertTrue((mapper.canSerialize(Path.class)));
Assert.assertTrue((mapper.canSerialize(ResultSet.class)));
Comment on lines +1100 to +1108
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

align indentations, please

// Assert.assertTrue((mapper.canSerialize(Statistics.class)));
// Assert.assertTrue((mapper.canSerialize(Record.class)));
Comment on lines +1109 to +1110
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove those




}
}