Skip to content

Commit 5e1de05

Browse files
authored
Support (Geo) Point datatype (#112)
* Support (Geo) Point datatype * address equals review * remove hashCode * address toString review * assert CREATE response * Revert "remove hashCode" This reverts commit 629500d. * reorder * Use IllegalArgumentException
1 parent ac66172 commit 5e1de05

File tree

4 files changed

+111
-10
lines changed

4 files changed

+111
-10
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.redislabs.redisgraph.graph_entities;
2+
3+
import java.util.List;
4+
import java.util.Objects;
5+
6+
/**
7+
* This class represents a (geographical) point in the graph.
8+
*/
9+
public final class Point {
10+
11+
private static final double EPSILON = 1e-5;
12+
13+
private final double latitude;
14+
private final double longitude;
15+
16+
/**
17+
* @param latitude
18+
* @param longitude
19+
*/
20+
public Point(double latitude, double longitude) {
21+
this.latitude = latitude;
22+
this.longitude = longitude;
23+
}
24+
25+
/**
26+
* @param values {@code [latitude, longitude]}
27+
*/
28+
public Point(List<Double> values) {
29+
if (values == null || values.size() != 2) {
30+
throw new IllegalArgumentException("Point requires two doubles.");
31+
}
32+
this.latitude = values.get(0);
33+
this.longitude = values.get(1);
34+
}
35+
36+
public double getLatitude() {
37+
return latitude;
38+
}
39+
40+
public double getLongitude() {
41+
return longitude;
42+
}
43+
44+
@Override
45+
public boolean equals(Object other) {
46+
if (this == other) return true;
47+
if (!(other instanceof Point)) return false;
48+
Point o = (Point) other;
49+
return Math.abs(latitude - o.latitude) < EPSILON &&
50+
Math.abs(longitude - o.longitude) < EPSILON;
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Objects.hash(latitude, longitude);
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return "Point{latitude=" + latitude + ", longitude=" + longitude + "}";
61+
}
62+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.redislabs.redisgraph.exceptions.JRedisGraphException;
99
import com.redislabs.redisgraph.graph_entities.*;
1010
import com.redislabs.redisgraph.impl.graph_cache.GraphCache;
11+
import redis.clients.jedis.BuilderFactory;
1112
import redis.clients.jedis.util.SafeEncoder;
1213
import redis.clients.jedis.exceptions.JedisDataException;
1314

@@ -239,12 +240,18 @@ private Object deserializeScalar(List<Object> rawScalarData) {
239240
return deserializePath(obj);
240241
case VALUE_MAP:
241242
return deserializeMap(obj);
243+
case VALUE_POINT:
244+
return deserializePoint(obj);
242245
case VALUE_UNKNOWN:
243246
default:
244247
return obj;
245248
}
246249
}
247250

251+
private Object deserializePoint(Object rawScalarData) {
252+
return new Point(BuilderFactory.DOUBLE_LIST.build(rawScalarData));
253+
}
254+
248255
private Map<String, Object> deserializeMap(Object rawScalarData) {
249256
List<Object> keyTypeValueEntries = (List<Object>) rawScalarData;
250257
Map<String, Object> map = new HashMap<>();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ enum ResultSetScalarTypes {
1313
VALUE_EDGE,
1414
VALUE_NODE,
1515
VALUE_PATH,
16-
VALUE_MAP;
16+
VALUE_MAP,
17+
VALUE_POINT;
1718

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

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

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import com.redislabs.redisgraph.graph_entities.Edge;
55
import com.redislabs.redisgraph.graph_entities.Node;
66
import com.redislabs.redisgraph.graph_entities.Path;
7+
import com.redislabs.redisgraph.graph_entities.Point;
78
import com.redislabs.redisgraph.graph_entities.Property;
89
import com.redislabs.redisgraph.impl.api.RedisGraph;
9-
import com.redislabs.redisgraph.impl.resultset.RecordImpl;
1010
import com.redislabs.redisgraph.impl.resultset.ResultSetImpl;
1111
import com.redislabs.redisgraph.test.utils.PathBuilder;
1212

@@ -874,6 +874,45 @@ public void testMapDataType() {
874874
Assert.assertEquals(expected, actual);
875875
}
876876

877+
@Test
878+
public void testGeoPointLatLon() {
879+
ResultSet rs = api.query("social", "CREATE (:restaurant"
880+
+ " {location: point({latitude:30.27822306, longitude:-97.75134723})})");
881+
Assert.assertEquals(1, rs.getStatistics().nodesCreated());
882+
Assert.assertEquals(1, rs.getStatistics().propertiesSet());
883+
884+
assertTestGeoPoint();
885+
}
886+
887+
@Test
888+
public void testGeoPointLonLat() {
889+
ResultSet rs = api.query("social", "CREATE (:restaurant"
890+
+ " {location: point({longitude:-97.75134723, latitude:30.27822306})})");
891+
Assert.assertEquals(1, rs.getStatistics().nodesCreated());
892+
Assert.assertEquals(1, rs.getStatistics().propertiesSet());
893+
894+
assertTestGeoPoint();
895+
}
896+
897+
private void assertTestGeoPoint() {
898+
ResultSet results = api.query("social", "MATCH (restaurant) RETURN restaurant");
899+
Assert.assertEquals(1, results.size());
900+
Record record = results.next();
901+
Assert.assertEquals(1, record.size());
902+
Assert.assertEquals(Collections.singletonList("restaurant"), record.keys());
903+
Node node = record.getValue(0);
904+
Property property = node.getProperty("location");
905+
Assert.assertEquals(new Point(30.27822306, -97.75134723), property.getValue());
906+
}
907+
908+
@Test
909+
public void timeoutArgument() {
910+
ResultSet rs = api.query("social", "UNWIND range(0,100) AS x WITH x AS x WHERE x = 100 RETURN x", 1L);
911+
Assert.assertEquals(1, rs.size());
912+
Record r = rs.next();
913+
Assert.assertEquals(Long.valueOf(100), r.getValue(0));
914+
}
915+
877916
@Test
878917
public void testCachedExecutionReadOnly() {
879918
api.query("social", "CREATE (:N {val:1}), (:N {val:2})");
@@ -898,14 +937,6 @@ public void testCachedExecutionReadOnly() {
898937
Assert.assertTrue(resultSet.getStatistics().cachedExecution());
899938
}
900939

901-
@Test
902-
public void timeoutArgument() {
903-
ResultSet rs = api.query("social", "UNWIND range(0,100) AS x WITH x AS x WHERE x = 100 RETURN x", 1L);
904-
Assert.assertEquals(1, rs.size());
905-
Record r = rs.next();
906-
Assert.assertEquals(Long.valueOf(100), r.getValue(0));
907-
}
908-
909940
@Test
910941
public void testSimpleReadOnly() {
911942
api.query("social","CREATE (:person{name:'filipe',age:30})");

0 commit comments

Comments
 (0)