From f095e6482583ad909f1c1858539d0fb0d9fda6b5 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 13 Apr 2016 10:37:31 -0400 Subject: [PATCH] Remove PROTOTYPE from histogram aggregations --- .../elasticsearch/search/SearchModule.java | 7 +- .../histogram/AbstractHistogramBuilder.java | 89 +++++++++---------- .../DateHistogramAggregatorBuilder.java | 48 +++++----- .../bucket/histogram/DateHistogramParser.java | 10 --- .../histogram/HistogramAggregatorBuilder.java | 16 ++-- .../bucket/histogram/HistogramParser.java | 10 --- 6 files changed, 84 insertions(+), 96 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/search/SearchModule.java b/core/src/main/java/org/elasticsearch/search/SearchModule.java index f625f9f7766..2a8ffc12f50 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchModule.java +++ b/core/src/main/java/org/elasticsearch/search/SearchModule.java @@ -111,7 +111,9 @@ import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGridParser; import org.elasticsearch.search.aggregations.bucket.geogrid.InternalGeoHashGrid; import org.elasticsearch.search.aggregations.bucket.global.GlobalParser; import org.elasticsearch.search.aggregations.bucket.global.InternalGlobal; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregatorBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramParser; +import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregatorBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.HistogramParser; import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram; import org.elasticsearch.search.aggregations.bucket.missing.InternalMissing; @@ -458,8 +460,9 @@ public class SearchModule extends AbstractModule { registerAggregatorParser(new RangeParser()); registerAggregatorParser(new DateRangeParser()); registerAggregatorParser(new IpRangeParser()); - registerAggregatorParser(new HistogramParser()); - registerAggregatorParser(new DateHistogramParser()); + registerAggregation(HistogramAggregatorBuilder::new, new HistogramParser(), HistogramAggregatorBuilder.AGGREGATION_NAME_FIELD); + registerAggregation(DateHistogramAggregatorBuilder::new, new DateHistogramParser(), + DateHistogramAggregatorBuilder.AGGREGATION_NAME_FIELD); registerAggregatorParser(new GeoDistanceParser()); registerAggregatorParser(new GeoHashGridParser()); registerAggregatorParser(new NestedParser()); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AbstractHistogramBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AbstractHistogramBuilder.java index 501a0082d56..8e8e5ca73e0 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AbstractHistogramBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AbstractHistogramBuilder.java @@ -23,10 +23,10 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.rounding.Rounding; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceType; + import java.io.IOException; import java.util.Objects; @@ -40,10 +40,50 @@ public abstract class AbstractHistogramBuilder histogramFactory) { + protected AbstractHistogramBuilder(String name, InternalHistogram.Factory histogramFactory) { super(name, histogramFactory.type(), ValuesSourceType.NUMERIC, histogramFactory.valueType()); } + /** + * Read from a stream. + */ + protected AbstractHistogramBuilder(StreamInput in, InternalHistogram.Factory histogramFactory) throws IOException { + super(in, histogramFactory.type(), ValuesSourceType.NUMERIC, histogramFactory.valueType()); + interval = in.readVLong(); + offset = in.readLong(); + if (in.readBoolean()) { + order = InternalOrder.Streams.readOrder(in); + } + keyed = in.readBoolean(); + minDocCount = in.readVLong(); + if (in.readBoolean()) { + extendedBounds = ExtendedBounds.readFrom(in); + } + } + + @Override + protected void innerWriteTo(StreamOutput out) throws IOException { + out.writeVLong(interval); + out.writeLong(offset); + boolean hasOrder = order != null; + out.writeBoolean(hasOrder); + if (hasOrder) { + InternalOrder.Streams.writeOrder(order, out); + } + out.writeBoolean(keyed); + out.writeVLong(minDocCount); + boolean hasExtendedBounds = extendedBounds != null; + out.writeBoolean(hasExtendedBounds); + if (hasExtendedBounds) { + extendedBounds.writeTo(out); + } + } + + @Override + protected boolean usesNewStyleSerialization() { + return true; + } + public long interval() { return interval; } @@ -150,53 +190,10 @@ public abstract class AbstractHistogramBuilder factory = createFactoryFromStream(name, in); - factory.interval = in.readVLong(); - factory.offset = in.readLong(); - if (in.readBoolean()) { - factory.order = InternalOrder.Streams.readOrder(in); - } - factory.keyed = in.readBoolean(); - factory.minDocCount = in.readVLong(); - if (in.readBoolean()) { - factory.extendedBounds = ExtendedBounds.readFrom(in); - } - return (AB) factory; - } - - protected abstract AB createFactoryFromStream(String name, StreamInput in) throws IOException; - - @Override - protected void innerWriteTo(StreamOutput out) throws IOException { - writeFactoryToStream(out); - out.writeVLong(interval); - out.writeLong(offset); - boolean hasOrder = order != null; - out.writeBoolean(hasOrder); - if (hasOrder) { - InternalOrder.Streams.writeOrder(order, out); - } - out.writeBoolean(keyed); - out.writeVLong(minDocCount); - boolean hasExtendedBounds = extendedBounds != null; - out.writeBoolean(hasExtendedBounds); - if (hasExtendedBounds) { - extendedBounds.writeTo(out); - } - } - - protected void writeFactoryToStream(StreamOutput out) throws IOException { - // Default impl does nothing -} - @Override protected int innerHashCode() { return Objects.hash(interval, offset, order, keyed, minDocCount, extendedBounds); -} + } @Override protected boolean innerEquals(Object obj) { diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregatorBuilder.java index 7f08f7ba01c..bf8db6449fb 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregatorBuilder.java @@ -19,22 +19,24 @@ package org.elasticsearch.search.aggregations.bucket.histogram; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; +import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.aggregations.support.AggregationContext; -import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; +import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import java.io.IOException; import java.util.Objects; public class DateHistogramAggregatorBuilder extends AbstractHistogramBuilder { - public static final DateHistogramAggregatorBuilder PROTOTYPE = new DateHistogramAggregatorBuilder(""); + public static final String NAME = InternalDateHistogram.TYPE.name(); + public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME); private DateHistogramInterval dateHistogramInterval; @@ -42,6 +44,26 @@ public class DateHistogramAggregatorBuilder extends AbstractHistogramBuilder { - public static final HistogramAggregatorBuilder PROTOTYPE = new HistogramAggregatorBuilder(""); + public static final String NAME = InternalHistogram.TYPE.name(); + public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME); public HistogramAggregatorBuilder(String name) { super(name, InternalHistogram.HISTOGRAM_FACTORY); } - @Override - protected HistogramAggregatorBuilder createFactoryFromStream(String name, StreamInput in) throws IOException { - return new HistogramAggregatorBuilder(name); + /** + * Read from a stream. + */ + public HistogramAggregatorBuilder(StreamInput in) throws IOException { + super(in, InternalHistogram.HISTOGRAM_FACTORY); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramParser.java index c30cc5919e1..a03ed992a38 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramParser.java @@ -44,11 +44,6 @@ public class HistogramParser extends NumericValuesSourceParser { super(true, true, timezoneAware); } - @Override - public String type() { - return InternalHistogram.TYPE.name(); - } - @Override protected AbstractHistogramBuilder createFactory(String aggregationName, ValuesSourceType valuesSourceType, ValueType targetValueType, Map otherOptions) { @@ -160,9 +155,4 @@ public class HistogramParser extends NumericValuesSourceParser { } return new InternalOrder.Aggregation(key, asc); } - - @Override - public AbstractHistogramBuilder getFactoryPrototypes() { - return HistogramAggregatorBuilder.PROTOTYPE; - } }