Remove PROTOTYPE from histogram aggregations

This commit is contained in:
Nik Everett 2016-04-13 10:37:31 -04:00
parent b87fd54ba9
commit f095e64825
6 changed files with 84 additions and 96 deletions

View File

@ -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.geogrid.InternalGeoHashGrid;
import org.elasticsearch.search.aggregations.bucket.global.GlobalParser; import org.elasticsearch.search.aggregations.bucket.global.GlobalParser;
import org.elasticsearch.search.aggregations.bucket.global.InternalGlobal; 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.DateHistogramParser;
import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregatorBuilder;
import org.elasticsearch.search.aggregations.bucket.histogram.HistogramParser; import org.elasticsearch.search.aggregations.bucket.histogram.HistogramParser;
import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram; import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram;
import org.elasticsearch.search.aggregations.bucket.missing.InternalMissing; import org.elasticsearch.search.aggregations.bucket.missing.InternalMissing;
@ -458,8 +460,9 @@ public class SearchModule extends AbstractModule {
registerAggregatorParser(new RangeParser()); registerAggregatorParser(new RangeParser());
registerAggregatorParser(new DateRangeParser()); registerAggregatorParser(new DateRangeParser());
registerAggregatorParser(new IpRangeParser()); registerAggregatorParser(new IpRangeParser());
registerAggregatorParser(new HistogramParser()); registerAggregation(HistogramAggregatorBuilder::new, new HistogramParser(), HistogramAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregatorParser(new DateHistogramParser()); registerAggregation(DateHistogramAggregatorBuilder::new, new DateHistogramParser(),
DateHistogramAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregatorParser(new GeoDistanceParser()); registerAggregatorParser(new GeoDistanceParser());
registerAggregatorParser(new GeoHashGridParser()); registerAggregatorParser(new GeoHashGridParser());
registerAggregatorParser(new NestedParser()); registerAggregatorParser(new NestedParser());

View File

@ -23,10 +23,10 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.rounding.Rounding; import org.elasticsearch.common.rounding.Rounding;
import org.elasticsearch.common.xcontent.XContentBuilder; 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.ValuesSource;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorBuilder;
import org.elasticsearch.search.aggregations.support.ValuesSourceType; import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
@ -40,10 +40,50 @@ public abstract class AbstractHistogramBuilder<AB extends AbstractHistogramBuild
protected long minDocCount = 0; protected long minDocCount = 0;
protected ExtendedBounds extendedBounds; protected ExtendedBounds extendedBounds;
AbstractHistogramBuilder(String name, InternalHistogram.Factory<?> histogramFactory) { protected AbstractHistogramBuilder(String name, InternalHistogram.Factory<?> histogramFactory) {
super(name, histogramFactory.type(), ValuesSourceType.NUMERIC, histogramFactory.valueType()); 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() { public long interval() {
return interval; return interval;
} }
@ -150,49 +190,6 @@ public abstract class AbstractHistogramBuilder<AB extends AbstractHistogramBuild
return InternalHistogram.TYPE.name(); return InternalHistogram.TYPE.name();
} }
@SuppressWarnings("unchecked")
@Override
protected AB innerReadFrom(String name, ValuesSourceType valuesSourceType, ValueType targetValueType, StreamInput in)
throws IOException {
AbstractHistogramBuilder<AB> 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 @Override
protected int innerHashCode() { protected int innerHashCode() {
return Objects.hash(interval, offset, order, keyed, minDocCount, extendedBounds); return Objects.hash(interval, offset, order, keyed, minDocCount, extendedBounds);

View File

@ -19,22 +19,24 @@
package org.elasticsearch.search.aggregations.bucket.histogram; 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.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; 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.AggregationContext;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
public class DateHistogramAggregatorBuilder extends AbstractHistogramBuilder<DateHistogramAggregatorBuilder> { public class DateHistogramAggregatorBuilder extends AbstractHistogramBuilder<DateHistogramAggregatorBuilder> {
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; private DateHistogramInterval dateHistogramInterval;
@ -42,6 +44,26 @@ public class DateHistogramAggregatorBuilder extends AbstractHistogramBuilder<Dat
super(name, InternalDateHistogram.HISTOGRAM_FACTORY); super(name, InternalDateHistogram.HISTOGRAM_FACTORY);
} }
/**
* Read from a stream.
*/
public DateHistogramAggregatorBuilder(StreamInput in) throws IOException {
super(in, InternalDateHistogram.HISTOGRAM_FACTORY);
if (in.readBoolean()) {
dateHistogramInterval = DateHistogramInterval.readFromStream(in);
}
}
@Override
protected void innerWriteTo(StreamOutput out) throws IOException {
super.innerWriteTo(out);
boolean hasDateInterval = dateHistogramInterval != null;
out.writeBoolean(hasDateInterval);
if (hasDateInterval) {
dateHistogramInterval.writeTo(out);
}
}
/** /**
* Set the interval. * Set the interval.
*/ */
@ -85,7 +107,7 @@ public class DateHistogramAggregatorBuilder extends AbstractHistogramBuilder<Dat
@Override @Override
public String getWriteableName() { public String getWriteableName() {
return InternalDateHistogram.TYPE.name(); return NAME;
} }
@Override @Override
@ -98,24 +120,6 @@ public class DateHistogramAggregatorBuilder extends AbstractHistogramBuilder<Dat
return builder; return builder;
} }
@Override
protected DateHistogramAggregatorBuilder createFactoryFromStream(String name, StreamInput in) throws IOException {
DateHistogramAggregatorBuilder factory = new DateHistogramAggregatorBuilder(name);
if (in.readBoolean()) {
factory.dateHistogramInterval = DateHistogramInterval.readFromStream(in);
}
return factory;
}
@Override
protected void writeFactoryToStream(StreamOutput out) throws IOException {
boolean hasDateInterval = dateHistogramInterval != null;
out.writeBoolean(hasDateInterval);
if (hasDateInterval) {
dateHistogramInterval.writeTo(out);
}
}
@Override @Override
protected int innerHashCode() { protected int innerHashCode() {
return Objects.hash(super.innerHashCode(), dateHistogramInterval); return Objects.hash(super.innerHashCode(), dateHistogramInterval);

View File

@ -36,11 +36,6 @@ public class DateHistogramParser extends HistogramParser {
super(true); super(true);
} }
@Override
public String type() {
return InternalDateHistogram.TYPE.name();
}
@Override @Override
protected Object parseStringInterval(String text) { protected Object parseStringInterval(String text) {
return new DateHistogramInterval(text); return new DateHistogramInterval(text);
@ -96,9 +91,4 @@ public class DateHistogramParser extends HistogramParser {
protected long parseStringOffset(String offset) throws IOException { protected long parseStringOffset(String offset) throws IOException {
return DateHistogramAggregatorBuilder.parseStringOffset(offset); return DateHistogramAggregatorBuilder.parseStringOffset(offset);
} }
@Override
public DateHistogramAggregatorBuilder getFactoryPrototypes() {
return DateHistogramAggregatorBuilder.PROTOTYPE;
}
} }

View File

@ -19,25 +19,29 @@
package org.elasticsearch.search.aggregations.bucket.histogram; 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.StreamInput;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; 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.AggregationContext;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import java.io.IOException; import java.io.IOException;
public class HistogramAggregatorBuilder extends AbstractHistogramBuilder<HistogramAggregatorBuilder> { public class HistogramAggregatorBuilder extends AbstractHistogramBuilder<HistogramAggregatorBuilder> {
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) { public HistogramAggregatorBuilder(String name) {
super(name, InternalHistogram.HISTOGRAM_FACTORY); super(name, InternalHistogram.HISTOGRAM_FACTORY);
} }
@Override /**
protected HistogramAggregatorBuilder createFactoryFromStream(String name, StreamInput in) throws IOException { * Read from a stream.
return new HistogramAggregatorBuilder(name); */
public HistogramAggregatorBuilder(StreamInput in) throws IOException {
super(in, InternalHistogram.HISTOGRAM_FACTORY);
} }
@Override @Override

View File

@ -44,11 +44,6 @@ public class HistogramParser extends NumericValuesSourceParser {
super(true, true, timezoneAware); super(true, true, timezoneAware);
} }
@Override
public String type() {
return InternalHistogram.TYPE.name();
}
@Override @Override
protected AbstractHistogramBuilder<?> createFactory(String aggregationName, ValuesSourceType valuesSourceType, protected AbstractHistogramBuilder<?> createFactory(String aggregationName, ValuesSourceType valuesSourceType,
ValueType targetValueType, Map<ParseField, Object> otherOptions) { ValueType targetValueType, Map<ParseField, Object> otherOptions) {
@ -160,9 +155,4 @@ public class HistogramParser extends NumericValuesSourceParser {
} }
return new InternalOrder.Aggregation(key, asc); return new InternalOrder.Aggregation(key, asc);
} }
@Override
public AbstractHistogramBuilder<?> getFactoryPrototypes() {
return HistogramAggregatorBuilder.PROTOTYPE;
}
} }