Skip to content

Commit 0c1694e

Browse files
committed
Encode 2D and 3D point coordinates as doubles
1 parent 1e00d32 commit 0c1694e

File tree

6 files changed

+50
-52
lines changed

6 files changed

+50
-52
lines changed

driver/src/main/java/org/neo4j/driver/internal/async/inbound/ByteBufInput.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ public double readDouble()
7070
}
7171

7272
@Override
73-
public PackInput readBytes( byte[] into, int offset, int toRead )
73+
public void readBytes( byte[] into, int offset, int toRead )
7474
{
7575
buf.readBytes( into, offset, toRead );
76-
return this;
7776
}
7877

7978
@Override

driver/src/main/java/org/neo4j/driver/internal/messaging/PackStreamMessageFormatV2.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public class PackStreamMessageFormatV2 extends PackStreamMessageFormatV1
3838
{
3939
private static final byte POINT_2D_STRUCT_TYPE = 'X';
4040
private static final byte POINT_3D_STRUCT_TYPE = 'Y';
41-
private static final int POINT_STRUCT_SIZE = 2;
41+
42+
private static final int POINT_2D_STRUCT_SIZE = 3;
43+
private static final int POINT_3D_STRUCT_SIZE = 4;
4244

4345
@Override
4446
public MessageFormat.Writer newWriter( PackOutput output, boolean byteArraySupportEnabled )
@@ -82,16 +84,19 @@ else if ( value.typeConstructor() == POINT_3D_TyCon )
8284

8385
private void packPoint2D( Point2D point ) throws IOException
8486
{
85-
packer.packStructHeader( POINT_STRUCT_SIZE, POINT_2D_STRUCT_TYPE );
87+
packer.packStructHeader( POINT_2D_STRUCT_SIZE, POINT_2D_STRUCT_TYPE );
8688
packer.pack( point.srid() );
87-
// packer.pack( point.x(), point.y() );
89+
packer.pack( point.x() );
90+
packer.pack( point.y() );
8891
}
8992

9093
private void packPoint3D( Point3D point ) throws IOException
9194
{
92-
packer.packStructHeader( POINT_STRUCT_SIZE, POINT_3D_STRUCT_TYPE );
95+
packer.packStructHeader( POINT_3D_STRUCT_SIZE, POINT_3D_STRUCT_TYPE );
9396
packer.pack( point.srid() );
94-
// packer.pack( point.x(), point.y(), point.z() );
97+
packer.pack( point.x() );
98+
packer.pack( point.y() );
99+
packer.pack( point.z() );
95100
}
96101
}
97102

@@ -107,12 +112,12 @@ Value unpackStruct( long size, byte type ) throws IOException
107112
{
108113
if ( type == POINT_2D_STRUCT_TYPE )
109114
{
110-
ensureCorrectStructSize( POINT_2D_TyCon.typeName(), POINT_STRUCT_SIZE, size );
115+
ensureCorrectStructSize( POINT_2D_TyCon.typeName(), POINT_2D_STRUCT_SIZE, size );
111116
return unpackPoint2D();
112117
}
113118
else if ( type == POINT_3D_STRUCT_TYPE )
114119
{
115-
ensureCorrectStructSize( POINT_3D_TyCon.typeName(), POINT_STRUCT_SIZE, size );
120+
ensureCorrectStructSize( POINT_3D_TyCon.typeName(), POINT_3D_STRUCT_SIZE, size );
116121
return unpackPoint3D();
117122
}
118123
else
@@ -124,13 +129,18 @@ else if ( type == POINT_3D_STRUCT_TYPE )
124129
private Value unpackPoint2D() throws IOException
125130
{
126131
long srid = unpacker.unpackLong();
127-
return new Point2DValue( new InternalPoint2D( srid, 0.0, 0.0 ) );
132+
double x = unpacker.unpackDouble();
133+
double y = unpacker.unpackDouble();
134+
return new Point2DValue( new InternalPoint2D( srid, x, y ) );
128135
}
129136

130137
private Value unpackPoint3D() throws IOException
131138
{
132139
long srid = unpacker.unpackLong();
133-
return new Point3DValue( new InternalPoint3D( srid, 0.0, 0.0, 0.0 ) );
140+
double x = unpacker.unpackDouble();
141+
double y = unpacker.unpackDouble();
142+
double z = unpacker.unpackDouble();
143+
return new Point3DValue( new InternalPoint3D( srid, x, y, z ) );
134144
}
135145
}
136146
}

driver/src/main/java/org/neo4j/driver/internal/packstream/PackInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public interface PackInput
4242
double readDouble() throws IOException;
4343

4444
/** Consume a specified number of bytes */
45-
PackInput readBytes( byte[] into, int offset, int toRead ) throws IOException;
45+
void readBytes( byte[] into, int offset, int toRead ) throws IOException;
4646

4747
/** Get the next byte without forwarding the internal pointer */
4848
byte peekByte() throws IOException;

driver/src/main/java/org/neo4j/driver/v1/Values.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ public static Value value( final Map<String,Object> val )
263263
return new MapValue( asValues );
264264
}
265265

266-
public static Value point2D( int srid, double x, double y )
266+
public static Value point2D( long srid, double x, double y )
267267
{
268268
return new Point2DValue( new InternalPoint2D( srid, x, y ) );
269269
}
270270

271-
public static Value point3D( int srid, double x, double y, double z )
271+
public static Value point3D( long srid, double x, double y, double z )
272272
{
273273
return new Point3DValue( new InternalPoint3D( srid, x, y, z ) );
274274
}

driver/src/test/java/org/neo4j/driver/internal/packstream/BufferedChannelInput.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public double readDouble() throws IOException
8080
}
8181

8282
@Override
83-
public PackInput readBytes( byte[] into, int index, int toRead ) throws IOException
83+
public void readBytes( byte[] into, int index, int toRead ) throws IOException
8484
{
8585
int endIndex = index + toRead;
8686
while ( index < endIndex)
@@ -98,7 +98,6 @@ public PackInput readBytes( byte[] into, int index, int toRead ) throws IOExcept
9898
}
9999
}
100100
}
101-
return this;
102101
}
103102

104103
@Override

driver/src/test/java/org/neo4j/driver/v1/integration/PointTypeIT.java renamed to driver/src/test/java/org/neo4j/driver/v1/integration/PointsIT.java

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,20 @@
2727

2828
import org.neo4j.driver.v1.Record;
2929
import org.neo4j.driver.v1.Value;
30-
import org.neo4j.driver.v1.types.Point;
30+
import org.neo4j.driver.v1.types.Point2D;
3131
import org.neo4j.driver.v1.util.TestNeo4jSession;
3232

33-
import static java.util.Arrays.asList;
3433
import static java.util.Collections.singletonMap;
3534
import static org.junit.Assert.assertEquals;
3635
import static org.junit.Assume.assumeTrue;
3736
import static org.neo4j.driver.internal.util.ServerVersion.v3_4_0;
38-
import static org.neo4j.driver.v1.Values.point;
37+
import static org.neo4j.driver.v1.Values.point2D;
3938

40-
public class PointTypeIT
39+
public class PointsIT
4140
{
42-
private static final int EPSG_TABLE_ID = 1;
43-
private static final int WGS_84_CRS_CODE = 4326;
44-
45-
private static final int SR_ORG_TABLE_ID = 2;
46-
private static final int CARTESIAN_CRS_CODE = 7203;
41+
private static final long WGS_84_CRS_CODE = 4326;
42+
private static final long CARTESIAN_CRS_CODE = 7203;
43+
private static final double DELTA = 0.00001;
4744

4845
@Rule
4946
public final TestNeo4jSession session = new TestNeo4jSession();
@@ -59,33 +56,33 @@ public void shouldReceivePoint()
5956
{
6057
Record record = session.run( "RETURN point({x: 39.111748, y:-76.775635})" ).single();
6158

62-
Point point = record.get( 0 ).asPoint();
59+
Point2D point = record.get( 0 ).asPoint2D();
6360

64-
assertEquals( SR_ORG_TABLE_ID, point.crsTableId() );
65-
assertEquals( CARTESIAN_CRS_CODE, point.crsCode() );
66-
assertEquals( asList( 39.111748, -76.775635 ), point.coordinate().values() );
61+
assertEquals( CARTESIAN_CRS_CODE, point.srid() );
62+
assertEquals( 39.111748, point.x(), DELTA );
63+
assertEquals( -76.775635, point.y(), DELTA );
6764
}
6865

6966
@Test
7067
public void shouldSendPoint()
7168
{
72-
Value pointValue = point( EPSG_TABLE_ID, WGS_84_CRS_CODE, 38.8719, 77.0563 );
69+
Value pointValue = point2D( WGS_84_CRS_CODE, 38.8719, 77.0563 );
7370
Record record1 = session.run( "CREATE (n:Node {location: $point}) RETURN 42", singletonMap( "point", pointValue ) ).single();
7471

7572
assertEquals( 42, record1.get( 0 ).asInt() );
7673

7774
Record record2 = session.run( "MATCH (n:Node) RETURN n.location" ).single();
78-
Point point = record2.get( 0 ).asPoint();
75+
Point2D point = record2.get( 0 ).asPoint2D();
7976

80-
assertEquals( EPSG_TABLE_ID, point.crsTableId() );
81-
assertEquals( WGS_84_CRS_CODE, point.crsCode() );
82-
assertEquals( asList( 38.8719, 77.0563 ), point.coordinate().values() );
77+
assertEquals( WGS_84_CRS_CODE, point.srid() );
78+
assertEquals( 38.8719, point.x(), DELTA );
79+
assertEquals( 77.0563, point.y(), DELTA );
8380
}
8481

8582
@Test
8683
public void shouldSendAndReceivePoint()
8784
{
88-
testPointSendAndReceive( SR_ORG_TABLE_ID, CARTESIAN_CRS_CODE, 40.7624, 73.9738 );
85+
testPointSendAndReceive( point2D( CARTESIAN_CRS_CODE, 40.7624, 73.9738 ) );
8986
}
9087

9188
@Test
@@ -94,34 +91,27 @@ public void shouldSendAndReceiveRandomPoints()
9491
Stream<Value> randomPoints = ThreadLocalRandom.current()
9592
.ints( 1_000, 0, 2 )
9693
.mapToObj( idx -> idx % 2 == 0
97-
? point( EPSG_TABLE_ID, WGS_84_CRS_CODE, randomCoordinate() )
98-
: point( SR_ORG_TABLE_ID, CARTESIAN_CRS_CODE, randomCoordinate() ) );
94+
? point2D( WGS_84_CRS_CODE, randomDouble(), randomDouble() )
95+
: point2D( CARTESIAN_CRS_CODE, randomDouble(), randomDouble() ) );
9996

10097
randomPoints.forEach( this::testPointSendAndReceive );
10198
}
10299

103-
private void testPointSendAndReceive( long crsTableId, long crsCode, double... coordinate )
104-
{
105-
testPointSendAndReceive( point( crsTableId, crsCode, coordinate ) );
106-
}
107-
108100
private void testPointSendAndReceive( Value pointValue )
109101
{
110-
Point originalPoint = pointValue.asPoint();
102+
Point2D originalPoint = pointValue.asPoint2D();
111103

112104
Record record = session.run( "CREATE (n {p:$point}) return n.p", singletonMap( "point", pointValue ) ).single();
113-
Point receivedPoint = record.get( 0 ).asPoint();
105+
Point2D receivedPoint = record.get( 0 ).asPoint2D();
114106

115107
String message = "Failed for " + originalPoint;
116-
assertEquals( message, originalPoint.crsTableId(), receivedPoint.crsTableId() );
117-
assertEquals( message, originalPoint.crsCode(), receivedPoint.crsCode() );
118-
assertEquals( message, originalPoint.coordinate().values(), receivedPoint.coordinate().values() );
108+
assertEquals( message, originalPoint.srid(), receivedPoint.srid() );
109+
assertEquals( message, originalPoint.x(), receivedPoint.x(), DELTA );
110+
assertEquals( message, originalPoint.y(), receivedPoint.y(), DELTA );
119111
}
120112

121-
private static double[] randomCoordinate()
113+
private static double randomDouble()
122114
{
123-
ThreadLocalRandom random = ThreadLocalRandom.current();
124-
int count = random.nextInt( 2, 4 ); // either 2D or 3D point
125-
return random.doubles( count, -180.0, 180 ).toArray();
115+
return ThreadLocalRandom.current().nextDouble( -180.0, 180 );
126116
}
127-
}
117+
}

0 commit comments

Comments
 (0)