From 3025295f7eb0fcdb635fa0f82f4c74196e633595 Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Mon, 19 Mar 2018 08:54:10 -0600 Subject: [PATCH] Decouple Text and Geopoint from XContentBuilder (#29119) This removes the `Text` and `Geopoint` special handling from `XContentBuilder`. Instead, these classes now implement `ToXContentFragment` and render themselves accordingly. This allows us to further decouple XContentBuilder from Elasticsearch-specific classes so it can be factored into a standalone lib at a later time. Relates to #28504 --- .../elasticsearch/common/geo/GeoPoint.java | 11 +++++- .../org/elasticsearch/common/text/Text.java | 17 ++++++++- .../common/xcontent/XContentBuilder.java | 37 +------------------ 3 files changed, 27 insertions(+), 38 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/geo/GeoPoint.java b/server/src/main/java/org/elasticsearch/common/geo/GeoPoint.java index 125bc5aefcf..5905695fb73 100644 --- a/server/src/main/java/org/elasticsearch/common/geo/GeoPoint.java +++ b/server/src/main/java/org/elasticsearch/common/geo/GeoPoint.java @@ -25,13 +25,17 @@ import org.apache.lucene.geo.GeoEncodingUtils; import org.apache.lucene.index.IndexableField; import org.apache.lucene.util.BitUtil; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContentFragment; +import org.elasticsearch.common.xcontent.XContentBuilder; +import java.io.IOException; import java.util.Arrays; import static org.elasticsearch.common.geo.GeoHashUtils.mortonEncode; import static org.elasticsearch.common.geo.GeoHashUtils.stringEncode; -public final class GeoPoint { +public final class GeoPoint implements ToXContentFragment { private double lat; private double lon; @@ -184,4 +188,9 @@ public final class GeoPoint { public static GeoPoint fromGeohash(long geohashLong) { return new GeoPoint().resetFromGeoHash(geohashLong); } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + return builder.latlon(lat, lon); + } } diff --git a/server/src/main/java/org/elasticsearch/common/text/Text.java b/server/src/main/java/org/elasticsearch/common/text/Text.java index d895b7c11b0..45a1c2d6306 100644 --- a/server/src/main/java/org/elasticsearch/common/text/Text.java +++ b/server/src/main/java/org/elasticsearch/common/text/Text.java @@ -20,14 +20,18 @@ package org.elasticsearch.common.text; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContentFragment; +import org.elasticsearch.common.xcontent.XContentBuilder; +import java.io.IOException; import java.nio.charset.StandardCharsets; /** * Both {@link String} and {@link BytesReference} representation of the text. Starts with one of those, and if * the other is requests, caches the other one in a local reference so no additional conversion will be needed. */ -public final class Text implements Comparable { +public final class Text implements Comparable, ToXContentFragment { public static final Text[] EMPTY_ARRAY = new Text[0]; @@ -113,4 +117,15 @@ public final class Text implements Comparable { public int compareTo(Text text) { return bytes().compareTo(text.bytes()); } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + if (hasString()) { + return builder.value(this.string()); + } else { + // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a + // request to jackson to support InputStream as well? + return builder.utf8Value(this.bytes().toBytesRef()); + } + } } diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java index 9e1bb362d48..b5622a9c0d2 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java @@ -20,9 +20,7 @@ package org.elasticsearch.common.xcontent; import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.lease.Releasable; -import org.elasticsearch.common.text.Text; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.CollectionUtils; @@ -98,7 +96,6 @@ public final class XContentBuilder implements Releasable, Flushable { writers.put(double[].class, (b, v) -> b.values((double[]) v)); writers.put(Float.class, (b, v) -> b.value((Float) v)); writers.put(float[].class, (b, v) -> b.values((float[]) v)); - writers.put(GeoPoint.class, (b, v) -> b.value((GeoPoint) v)); writers.put(Integer.class, (b, v) -> b.value((Integer) v)); writers.put(int[].class, (b, v) -> b.values((int[]) v)); writers.put(Long.class, (b, v) -> b.value((Long) v)); @@ -107,7 +104,6 @@ public final class XContentBuilder implements Releasable, Flushable { writers.put(short[].class, (b, v) -> b.values((short[]) v)); writers.put(String.class, (b, v) -> b.value((String) v)); writers.put(String[].class, (b, v) -> b.values((String[]) v)); - writers.put(Text.class, (b, v) -> b.value((Text) v)); WRITERS = Collections.unmodifiableMap(writers); } @@ -630,26 +626,6 @@ public final class XContentBuilder implements Releasable, Flushable { return this; } - //////////////////////////////////////////////////////////////////////////// - // Text - ////////////////////////////////// - - public XContentBuilder field(String name, Text value) throws IOException { - return field(name).value(value); - } - - public XContentBuilder value(Text value) throws IOException { - if (value == null) { - return nullValue(); - } else if (value.hasString()) { - return value(value.string()); - } else { - // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a - // request to jackson to support InputStream as well? - return utf8Value(value.bytes().toBytesRef()); - } - } - //////////////////////////////////////////////////////////////////////////// // Date ////////////////////////////////// @@ -714,20 +690,9 @@ public final class XContentBuilder implements Releasable, Flushable { } //////////////////////////////////////////////////////////////////////////// - // GeoPoint & LatLon + // LatLon ////////////////////////////////// - public XContentBuilder field(String name, GeoPoint value) throws IOException { - return field(name).value(value); - } - - public XContentBuilder value(GeoPoint value) throws IOException { - if (value == null) { - return nullValue(); - } - return latlon(value.getLat(), value.getLon()); - } - public XContentBuilder latlon(String name, double lat, double lon) throws IOException { return field(name).latlon(lat, lon); }