Skip to content

Commit

Permalink
Encode and decode points in th eproperty store.
Browse files Browse the repository at this point in the history
  • Loading branch information
sherfert authored and MishaDemianenko committed Nov 27, 2017
1 parent 52e441c commit f89d97b
Show file tree
Hide file tree
Showing 30 changed files with 470 additions and 136 deletions.
Expand Up @@ -31,7 +31,7 @@
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.values.AnyValue;
import org.neo4j.values.utils.PrettyPrinter;
import org.neo4j.values.virtual.CoordinateReferenceSystem;
import org.neo4j.values.storable.CoordinateReferenceSystem;
import org.neo4j.values.virtual.MapValue;

import static org.neo4j.bolt.v1.messaging.BoltResponseMessage.FAILURE;
Expand Down
Expand Up @@ -42,7 +42,7 @@
import org.neo4j.values.storable.TextArray;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.Values;
import org.neo4j.values.virtual.CoordinateReferenceSystem;
import org.neo4j.values.storable.CoordinateReferenceSystem;
import org.neo4j.values.virtual.EdgeValue;
import org.neo4j.values.virtual.ListValue;
import org.neo4j.values.virtual.MapValue;
Expand Down
Expand Up @@ -59,7 +59,7 @@
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.impl.util.HexPrinter;
import org.neo4j.values.AnyValue;
import org.neo4j.values.virtual.CoordinateReferenceSystem;
import org.neo4j.values.storable.CoordinateReferenceSystem;
import org.neo4j.values.virtual.MapValue;

import static org.hamcrest.CoreMatchers.containsString;
Expand Down
Expand Up @@ -28,6 +28,7 @@
import java.util.Iterator;
import java.util.List;

import org.neo4j.cypher.internal.runtime.CRS;
import org.neo4j.cypher.internal.runtime.CartesianPoint;
import org.neo4j.cypher.internal.runtime.GeographicPoint;
import org.neo4j.graphdb.Node;
Expand All @@ -39,12 +40,14 @@
import org.neo4j.values.AnyValueWriter;
import org.neo4j.values.storable.TextArray;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.virtual.CoordinateReferenceSystem;
import org.neo4j.values.storable.CoordinateReferenceSystem;
import org.neo4j.values.virtual.EdgeValue;
import org.neo4j.values.virtual.MapValue;
import org.neo4j.values.virtual.NodeValue;

import static org.neo4j.helpers.collection.Iterators.iteratorsEqual;
import static org.neo4j.values.storable.CoordinateReferenceSystem.Cartesian;
import static org.neo4j.values.storable.CoordinateReferenceSystem.WGS84;

/**
* Used for turning parameters into appropriate types in the compiled runtime
Expand Down Expand Up @@ -343,21 +346,16 @@ public void writeByteArray( byte[] value ) throws RuntimeException
@Override
public void writePoint( CoordinateReferenceSystem crs, double[] coordinate ) throws RuntimeException
{
switch ( crs )
if ( crs.equals( WGS84 ) )
{
writeValue( new GeographicPoint( coordinate[0], coordinate[1], new CRS( crs.name, crs.code, crs.href ) ) );
}
else if ( crs.equals( Cartesian ) )
{
writeValue( new CartesianPoint( coordinate[0], coordinate[1], new CRS( crs.name, crs.code, crs.href ) ) );
}
else
{
case WGS84:
writeValue( new GeographicPoint( coordinate[0], coordinate[1],
new org.neo4j.cypher.internal.compatibility.v3_4.runtime.CRS( crs.name, crs.code,
crs.href ) ) );
break;

case Cartesian:
writeValue( new CartesianPoint( coordinate[0], coordinate[1],
new org.neo4j.cypher.internal.compatibility.v3_4.runtime.CRS( crs.name, crs.code,
crs.href ) ) );
break;

default:
throw new IllegalArgumentException( crs + " is not a supported coordinate reference system" );
}
}
Expand Down Expand Up @@ -494,42 +492,4 @@ public Object value()
return list;
}
}

private class PointWriter implements Writer
{
//TODO it is quite silly that the point writer doesn't give me the whole thing at once
private final double[] coordinates = new double[2];
private int index;
private final CoordinateReferenceSystem crs;

PointWriter( CoordinateReferenceSystem crs )
{
this.crs = crs;
}

@Override
public void write( Object value )
{
coordinates[index++] = (double) value;
}

@Override
public Object value()
{
if ( crs.code() == CoordinateReferenceSystem.WGS84.code() )
{
return new GeographicPoint( coordinates[0], coordinates[1],
new org.neo4j.cypher.internal.runtime.CRS( crs.name, crs.code, crs.href ) );
}
else if ( crs.code() == CoordinateReferenceSystem.Cartesian.code() )
{
return new CartesianPoint( coordinates[0], coordinates[1],
new org.neo4j.cypher.internal.runtime.CRS( crs.name, crs.code, crs.href ) );
}
else
{
throw new IllegalArgumentException( crs + " is not a supported coordinate reference system" );
}
}
}
}
Expand Up @@ -74,7 +74,7 @@ trait DistanceCalculator {

object CartesianCalculator extends DistanceCalculator {
override def isDefinedAt(p1: PointValue, p2: PointValue): Boolean =
p1.getCoordinateReferenceSystem.code() == CRS.Cartesian.code && p2.getCoordinateReferenceSystem.code() == CRS.Cartesian.code
p1.getCoordinateReferenceSystem.code == CRS.Cartesian.code && p2.getCoordinateReferenceSystem.code == CRS.Cartesian.code

override def calculateDistance(p1: PointValue, p2: PointValue): Double = {
val p1Coordinates = p1.coordinate()
Expand All @@ -90,7 +90,7 @@ object HaversinCalculator extends DistanceCalculator {
private val EARTH_RADIUS_METERS = 6378140.0

override def isDefinedAt(p1: PointValue, p2: PointValue): Boolean =
p1.getCoordinateReferenceSystem.code() == CRS.WGS84.code && p2.getCoordinateReferenceSystem.code == CRS.WGS84.code
p1.getCoordinateReferenceSystem.code == CRS.WGS84.code && p2.getCoordinateReferenceSystem.code == CRS.WGS84.code

override def calculateDistance(p1: PointValue, p2: PointValue): Double = {
val c1Coord = p1.coordinate()
Expand Down
Expand Up @@ -34,8 +34,9 @@ import org.neo4j.kernel.impl.query.clientconnection.ClientConnectionInfo
import org.neo4j.kernel.impl.util.BaseToObjectValueWriter
import org.neo4j.kernel.monitoring.{Monitors => KernelMonitors}
import org.neo4j.values.AnyValue
import org.neo4j.values.storable.CoordinateReferenceSystem
import org.neo4j.values.virtual.MapValue
import org.neo4j.values.virtual.VirtualValues.EMPTY_MAP
import org.neo4j.values.virtual.{CoordinateReferenceSystem, MapValue}

import scala.collection.mutable

Expand Down
Expand Up @@ -25,7 +25,7 @@
import org.neo4j.graphdb.spatial.Point;
import org.neo4j.kernel.impl.core.NodeManager;
import org.neo4j.kernel.impl.util.BaseToObjectValueWriter;
import org.neo4j.values.virtual.CoordinateReferenceSystem;
import org.neo4j.values.storable.CoordinateReferenceSystem;

public class ValueToObjectSerializer extends BaseToObjectValueWriter<RuntimeException>
{
Expand All @@ -51,7 +51,6 @@ protected Relationship newRelationshipProxyById( long id )
@Override
protected Point newPoint( CoordinateReferenceSystem crs, double[] coordinate )
{
// TODO: Is this necessary, perhaps PointValue is sufficient for use in Cypher-land
return Points.fromValue( crs, coordinate );
}
}
Expand Up @@ -24,12 +24,10 @@ import java.util.Collections
import org.neo4j.cypher.internal.util.v3_4.{CypherTypeException, InvalidArgumentException}
import org.neo4j.graphdb.spatial
import org.neo4j.graphdb.spatial.{Coordinate, Point}
import org.neo4j.values.virtual.CoordinateReferenceSystem
import org.neo4j.values.storable.CoordinateReferenceSystem

import scala.beans.BeanProperty


// FIXME Does not handle more than 2 dimensions
abstract class ScalaPoint extends Point {
def x: Double
def y: Double
Expand All @@ -54,6 +52,7 @@ case class GeographicPoint(longitude: Double, latitude: Double,
def y: Double = latitude
}

//TODO: These classes could be removed since CoordinateReferenceSystem implements the public API too
case class CRS(@BeanProperty name: String, @BeanProperty code: Int, @BeanProperty href: String) extends spatial.CRS {
override def getType: String = name
}
Expand Down
Expand Up @@ -35,5 +35,11 @@ default Coordinate getCoordinate()
{
return getCoordinates().get( 0 );
}

@Override
default String getGeometryType()
{
return "Point";
}
}

Expand Up @@ -25,7 +25,7 @@
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueWriter;
import org.neo4j.values.storable.Values;
import org.neo4j.values.virtual.CoordinateReferenceSystem;
import org.neo4j.values.storable.CoordinateReferenceSystem;

public final class ArrayEncoder
{
Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import org.neo4j.kernel.impl.store.GeometryType;
import org.neo4j.kernel.impl.store.LongerShortString;
import org.neo4j.kernel.impl.store.PropertyType;
import org.neo4j.kernel.impl.store.RecordCursor;
Expand Down Expand Up @@ -49,6 +50,7 @@
import static org.neo4j.kernel.impl.store.PropertyType.CHAR;
import static org.neo4j.kernel.impl.store.PropertyType.DOUBLE;
import static org.neo4j.kernel.impl.store.PropertyType.FLOAT;
import static org.neo4j.kernel.impl.store.PropertyType.GEOMETRY;
import static org.neo4j.kernel.impl.store.PropertyType.INT;
import static org.neo4j.kernel.impl.store.PropertyType.LONG;
import static org.neo4j.kernel.impl.store.PropertyType.SHORT;
Expand Down Expand Up @@ -226,6 +228,12 @@ Value arrayValue()
return PropertyUtil.readArrayFromBuffer( buffer );
}

Value geometryValue()
{
assertOfType( GEOMETRY );
return GeometryType.decode( data, position );
}

Value value()
{
switch ( type() )
Expand Down Expand Up @@ -254,6 +262,8 @@ Value value()
return shortArrayValue();
case ARRAY:
return arrayValue();
case GEOMETRY:
return geometryValue();
default:
throw new IllegalStateException( "No such type:" + type() );
}
Expand Down

0 comments on commit f89d97b

Please sign in to comment.