Remove unnecessary Writeable implementation from GeoPoint

GeoPoint now has native support in StreamOutput/StreamInput
impementing Writable is not necessary. This also adds tests
for XContentBuilder rendering GeoPoint
This commit is contained in:
Simon Willnauer 2015-10-08 12:33:14 +02:00
parent a46cf97388
commit cc40f20544
6 changed files with 24 additions and 37 deletions

View File

@ -19,12 +19,6 @@
package org.elasticsearch.common.geo; package org.elasticsearch.common.geo;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import java.io.IOException;
import org.apache.lucene.util.BitUtil; import org.apache.lucene.util.BitUtil;
import org.apache.lucene.util.XGeoHashUtils; import org.apache.lucene.util.XGeoHashUtils;
import org.apache.lucene.util.XGeoUtils; import org.apache.lucene.util.XGeoUtils;
@ -32,15 +26,12 @@ import org.apache.lucene.util.XGeoUtils;
/** /**
* *
*/ */
public final class GeoPoint implements Writeable<GeoPoint> { public final class GeoPoint {
private double lat; private double lat;
private double lon; private double lon;
private final static double TOLERANCE = XGeoUtils.TOLERANCE; private final static double TOLERANCE = XGeoUtils.TOLERANCE;
// for serialization purposes
private static final GeoPoint PROTOTYPE = new GeoPoint(Double.NaN, Double.NaN);
public GeoPoint() { public GeoPoint() {
} }
@ -179,21 +170,4 @@ public final class GeoPoint implements Writeable<GeoPoint> {
public static GeoPoint fromIndexLong(long indexLong) { public static GeoPoint fromIndexLong(long indexLong) {
return new GeoPoint().resetFromIndexHash(indexLong); return new GeoPoint().resetFromIndexHash(indexLong);
} }
@Override
public GeoPoint readFrom(StreamInput in) throws IOException {
double lat = in.readDouble();
double lon = in.readDouble();
return new GeoPoint(lat, lon);
}
public static GeoPoint readGeoPointFrom(StreamInput in) throws IOException {
return PROTOTYPE.readFrom(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeDouble(lat);
out.writeDouble(lon);
}
} }

View File

@ -314,8 +314,8 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder<GeoBounding
public GeoBoundingBoxQueryBuilder doReadFrom(StreamInput in) throws IOException { public GeoBoundingBoxQueryBuilder doReadFrom(StreamInput in) throws IOException {
String fieldName = in.readString(); String fieldName = in.readString();
GeoBoundingBoxQueryBuilder geo = new GeoBoundingBoxQueryBuilder(fieldName); GeoBoundingBoxQueryBuilder geo = new GeoBoundingBoxQueryBuilder(fieldName);
geo.topLeft = geo.topLeft.readFrom(in); geo.topLeft = in.readGeoPoint();
geo.bottomRight = geo.bottomRight.readFrom(in); geo.bottomRight = in.readGeoPoint();
geo.type = GeoExecType.readTypeFrom(in); geo.type = GeoExecType.readTypeFrom(in);
geo.validationMethod = GeoValidationMethod.readGeoValidationMethodFrom(in); geo.validationMethod = GeoValidationMethod.readGeoValidationMethodFrom(in);
return geo; return geo;
@ -324,8 +324,8 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder<GeoBounding
@Override @Override
public void doWriteTo(StreamOutput out) throws IOException { public void doWriteTo(StreamOutput out) throws IOException {
out.writeString(fieldName); out.writeString(fieldName);
topLeft.writeTo(out); out.writeGeoPoint(topLeft);
bottomRight.writeTo(out); out.writeGeoPoint(bottomRight);
type.writeTo(out); type.writeTo(out);
validationMethod.writeTo(out); validationMethod.writeTo(out);
} }

View File

@ -261,7 +261,7 @@ public class GeoDistanceQueryBuilder extends AbstractQueryBuilder<GeoDistanceQue
GeoDistanceQueryBuilder result = new GeoDistanceQueryBuilder(fieldName); GeoDistanceQueryBuilder result = new GeoDistanceQueryBuilder(fieldName);
result.distance = in.readDouble(); result.distance = in.readDouble();
result.validationMethod = GeoValidationMethod.readGeoValidationMethodFrom(in); result.validationMethod = GeoValidationMethod.readGeoValidationMethodFrom(in);
result.center = GeoPoint.readGeoPointFrom(in); result.center = in.readGeoPoint();
result.optimizeBbox = in.readString(); result.optimizeBbox = in.readString();
result.geoDistance = GeoDistance.readGeoDistanceFrom(in); result.geoDistance = GeoDistance.readGeoDistanceFrom(in);
return result; return result;
@ -272,7 +272,7 @@ public class GeoDistanceQueryBuilder extends AbstractQueryBuilder<GeoDistanceQue
out.writeString(fieldName); out.writeString(fieldName);
out.writeDouble(distance); out.writeDouble(distance);
validationMethod.writeTo(out); validationMethod.writeTo(out);
center.writeTo(out); out.writeGeoPoint(center);
out.writeString(optimizeBbox); out.writeString(optimizeBbox);
geoDistance.writeTo(out); geoDistance.writeTo(out);
} }

View File

@ -277,7 +277,7 @@ public class GeoDistanceRangeQueryBuilder extends AbstractQueryBuilder<GeoDistan
@Override @Override
protected GeoDistanceRangeQueryBuilder doReadFrom(StreamInput in) throws IOException { protected GeoDistanceRangeQueryBuilder doReadFrom(StreamInput in) throws IOException {
GeoDistanceRangeQueryBuilder queryBuilder = new GeoDistanceRangeQueryBuilder(in.readString(), GeoPoint.readGeoPointFrom(in)); GeoDistanceRangeQueryBuilder queryBuilder = new GeoDistanceRangeQueryBuilder(in.readString(), in.readGeoPoint());
queryBuilder.from = in.readGenericValue(); queryBuilder.from = in.readGenericValue();
queryBuilder.to = in.readGenericValue(); queryBuilder.to = in.readGenericValue();
queryBuilder.includeLower = in.readBoolean(); queryBuilder.includeLower = in.readBoolean();
@ -292,7 +292,7 @@ public class GeoDistanceRangeQueryBuilder extends AbstractQueryBuilder<GeoDistan
@Override @Override
protected void doWriteTo(StreamOutput out) throws IOException { protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(fieldName); out.writeString(fieldName);
point.writeTo(out); out.writeGeoPoint(point);
out.writeGenericValue(from); out.writeGenericValue(from);
out.writeGenericValue(to); out.writeGenericValue(to);
out.writeBoolean(includeLower); out.writeBoolean(includeLower);

View File

@ -164,7 +164,7 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQuery
List<GeoPoint> shell = new ArrayList<>(); List<GeoPoint> shell = new ArrayList<>();
int size = in.readVInt(); int size = in.readVInt();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
shell.add(GeoPoint.readGeoPointFrom(in)); shell.add(in.readGeoPoint());
} }
GeoPolygonQueryBuilder builder = new GeoPolygonQueryBuilder(fieldName, shell); GeoPolygonQueryBuilder builder = new GeoPolygonQueryBuilder(fieldName, shell);
builder.validationMethod = GeoValidationMethod.readGeoValidationMethodFrom(in); builder.validationMethod = GeoValidationMethod.readGeoValidationMethodFrom(in);
@ -176,7 +176,7 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQuery
out.writeString(fieldName); out.writeString(fieldName);
out.writeVInt(shell.size()); out.writeVInt(shell.size());
for (GeoPoint point : shell) { for (GeoPoint point : shell) {
point.writeTo(out); out.writeGeoPoint(point);
} }
validationMethod.writeTo(out); validationMethod.writeTo(out);
} }

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.xcontent.builder; package org.elasticsearch.common.xcontent.builder;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.io.FastCharArrayWriter; import org.elasticsearch.common.io.FastCharArrayWriter;
import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.BytesStreamOutput;
@ -351,4 +352,16 @@ public class XContentBuilderTests extends ESTestCase {
" foobar: \"boom\"\n", string); " foobar: \"boom\"\n", string);
} }
public void testRenderGeoPoint() throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
builder.startObject().field("foo").value(new GeoPoint(1,2)).endObject();
String string = builder.string();
assertEquals("{\n" +
" \"foo\" : {\n" +
" \"lat\" : 1.0,\n" +
" \"lon\" : 2.0\n" +
" }\n" +
"}", string.trim());
}
} }