You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by kw...@apache.org on 2017/08/25 14:07:38 UTC

lucene-solr:master: LUCENE-7936: Complete the serialization/deserialization implementation of Geo3d.

Repository: lucene-solr
Updated Branches:
  refs/heads/master 26b7644d0 -> 554f0d5f2


LUCENE-7936: Complete the serialization/deserialization implementation of Geo3d.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/554f0d5f
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/554f0d5f
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/554f0d5f

Branch: refs/heads/master
Commit: 554f0d5f2009a69389d4dd5d5d3907fa3a4727f5
Parents: 26b7644
Author: Karl Wright <Da...@gmail.com>
Authored: Fri Aug 25 10:07:25 2017 -0400
Committer: Karl Wright <Da...@gmail.com>
Committed: Fri Aug 25 10:07:25 2017 -0400

----------------------------------------------------------------------
 .../spatial3d/geom/GeoComplexPolygon.java       | 17 ++++--
 .../lucene/spatial3d/geom/PlanetModel.java      | 19 ++++++-
 .../spatial3d/geom/SerializableObject.java      | 58 ++++++++++++++------
 .../spatial3d/geom/RandomBinaryCodecTest.java   |  4 --
 4 files changed, 70 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/554f0d5f/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java
----------------------------------------------------------------------
diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java
index 107815d..fe7ccf6 100644
--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java
+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java
@@ -1270,17 +1270,24 @@ class GeoComplexPolygon extends GeoBasePolygon {
       }
     }
   }
-  
+
   @Override
   public boolean equals(Object o) {
-    // Way too expensive to do this the hard way, so each complex polygon will be considered unique.
-    return this == o;
+    if (!(o instanceof GeoComplexPolygon))
+      return false;
+    final GeoComplexPolygon other = (GeoComplexPolygon) o;
+    return super.equals(other) && testPointInSet == other.testPointInSet
+        && testPoint.equals(testPoint)
+        && pointsList.equals(other.pointsList);
   }
 
   @Override
   public int hashCode() {
-    // Each complex polygon is considered unique.
-    return System.identityHashCode(this);
+    int result = super.hashCode();
+    result = 31 * result + Boolean.hashCode(testPointInSet);
+    result = 31 * result + testPoint.hashCode();
+    result = 31 * result + pointsList.hashCode();
+    return result;
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/554f0d5f/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/PlanetModel.java
----------------------------------------------------------------------
diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/PlanetModel.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/PlanetModel.java
index e720b21..5ef7889 100644
--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/PlanetModel.java
+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/PlanetModel.java
@@ -16,11 +16,15 @@
  */
 package org.apache.lucene.spatial3d.geom;
 
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+
 /**
  * Holds mathematical constants associated with the model of a planet.
  * @lucene.experimental
  */
-public class PlanetModel {
+public class PlanetModel implements SerializableObject {
   
   /** Planet model corresponding to sphere. */
   public static final PlanetModel SPHERE = new PlanetModel(1.0,1.0);
@@ -94,6 +98,19 @@ public class PlanetModel {
     this.MIN_Y_POLE = new GeoPoint(ab, 0.0, -1.0, 0.0, 0.0, -Math.PI * 0.5);
     this.MAX_Y_POLE = new GeoPoint(ab, 0.0, 1.0, 0.0, 0.0, Math.PI * 0.5);
   }
+
+  /** Deserialization constructor.
+   * @param inputStream is the input stream.
+   */
+  public PlanetModel(final InputStream inputStream) throws IOException {
+    this(SerializableObject.readDouble(inputStream), SerializableObject.readDouble(inputStream));
+  }
+  
+  @Override
+  public void write(final OutputStream outputStream) throws IOException {
+    SerializableObject.writeDouble(outputStream, ab);
+    SerializableObject.writeDouble(outputStream, c);
+  }
   
   /** Find the minimum magnitude of all points on the ellipsoid.
    * @return the minimum magnitude for the planet.

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/554f0d5f/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SerializableObject.java
----------------------------------------------------------------------
diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SerializableObject.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SerializableObject.java
index 7ca64e3..ee108a4 100644
--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SerializableObject.java
+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SerializableObject.java
@@ -39,6 +39,28 @@ public interface SerializableObject {
    */
   void write(OutputStream outputStream) throws IOException;
 
+  /** Write a PlanetObject to a stream.
+   * @param outputStream is the output stream.
+   * @param object is the object to write.
+   */
+  public static void writePlanetObject(final OutputStream outputStream, final PlanetObject object) throws IOException {
+    object.getPlanetModel().write(outputStream);
+    writeObject(outputStream, object);
+  }
+  
+  /** Read a PlanetObject from a stream.
+   * @param inputStream is the input stream.
+   * @return the PlanetObject.
+   */
+  public static PlanetObject readPlanetObject(final InputStream inputStream) throws IOException {
+    final PlanetModel pm = new PlanetModel(inputStream);
+    final SerializableObject so = readObject(pm, inputStream);
+    if (!(so instanceof PlanetObject)) {
+      throw new IOException("Type of object is not expected PlanetObject: "+so.getClass().getName());
+    }
+    return (PlanetObject)so;
+  }
+  
   /** Write an object to a stream.
    * @param outputStream is the output stream.
    * @param object is the object to write.
@@ -65,6 +87,22 @@ public interface SerializableObject {
     }
   }
 
+  /** Read an object from a stream (for objects that do not need a PlanetModel).
+   * @param inputStream is the input stream.
+   * @return the deserialized object.
+   */
+  public static SerializableObject readObject(final InputStream inputStream) throws IOException {
+    // Read the class name
+    final String className = readString(inputStream);
+    try {
+      // Look for the class
+      final Class<?> clazz = Class.forName(className);
+      return readObject(inputStream, clazz);
+    } catch (ClassNotFoundException e) {
+      throw new IOException("Can't find class "+className+" for deserialization: "+e.getMessage(), e);
+    }
+  }
+  
   /** Instantiate a serializable object from a stream.
    * @param planetModel is the planet model.
    * @param inputStream is the input stream.
@@ -93,22 +131,6 @@ public interface SerializableObject {
 
   }
   
-  /** Read an object from a stream (for objects that do not need a PlanetModel).
-   * @param inputStream is the input stream.
-   * @return the deserialized object.
-   */
-  public static SerializableObject readObject(final InputStream inputStream) throws IOException {
-    // Read the class name
-    final String className = readString(inputStream);
-    try {
-      // Look for the class
-      final Class<?> clazz = Class.forName(className);
-      return readObject(inputStream, clazz);
-    } catch (ClassNotFoundException e) {
-      throw new IOException("Can't find class "+className+" for deserialization: "+e.getMessage(), e);
-    }
-  }
-  
   /** Instantiate a serializable object from a stream without a planet model.
    * @param inputStream is the input stream.
    * @param clazz is the class to instantiate.
@@ -238,9 +260,9 @@ public interface SerializableObject {
    * @param clazz is the class of the objects to read.
    * @return the array.
    */
-  @SuppressWarnings("unchecked")
   static <T extends SerializableObject> T[] readHomogeneousArray(final PlanetModel planetModel, final InputStream inputStream, final Class<T> clazz) throws IOException {
     final int count = readInt(inputStream);
+    @SuppressWarnings("unchecked")
     final T[] rval = (T[])Array.newInstance(clazz, count);
     for (int i = 0; i < count; i++) {
       rval[i] = clazz.cast(readObject(planetModel, inputStream, clazz));
@@ -283,9 +305,9 @@ public interface SerializableObject {
    * @param inputStream is the input stream.
    * @return the array.
    */
-  @SuppressWarnings("unchecked")
   static <T extends SerializableObject> T[] readHeterogeneousArray(final PlanetModel planetModel, final InputStream inputStream, final Class<T> clazz) throws IOException {
     final int count = readInt(inputStream);
+    @SuppressWarnings("unchecked")
     final T[] rval = (T[])Array.newInstance(clazz, count);
     for (int i = 0; i < count; i++) {
       rval[i] = clazz.cast(readObject(planetModel, inputStream));

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/554f0d5f/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomBinaryCodecTest.java
----------------------------------------------------------------------
diff --git a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomBinaryCodecTest.java b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomBinaryCodecTest.java
index 5828d52..4b1f98d 100644
--- a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomBinaryCodecTest.java
+++ b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomBinaryCodecTest.java
@@ -34,10 +34,6 @@ public class RandomBinaryCodecTest extends RandomGeoShapeGenerator{
   public void testRandomShapeCodec() throws IOException{
     PlanetModel planetModel = randomPlanetModel();
     int type = randomShapeType();
-    while (type == COMPLEX_POLYGON){
-      //We need to implement equals method
-      type = randomShapeType();
-    }
 
     GeoShape shape = randomGeoShape(type, planetModel);
     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();