From 5da1898f11b66bf1a9158ab011cfaeac5147da62 Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Wed, 11 Nov 2015 14:30:12 +0000 Subject: [PATCH] Aggregations Refactor: Refactor Max, Avg, and Sum Aggregations --- .../metrics/avg/AvgAggregator.java | 37 ++++++++++++++++-- .../aggregations/metrics/avg/AvgParser.java | 37 +++++++++++++----- .../metrics/max/MaxAggregator.java | 38 +++++++++++++++++-- .../aggregations/metrics/max/MaxParser.java | 37 +++++++++++++----- .../metrics/sum/SumAggregator.java | 37 ++++++++++++++++-- .../aggregations/metrics/sum/SumParser.java | 37 +++++++++++++----- .../search/aggregations/metrics/AvgTests.java | 31 +++++++++++++++ .../search/aggregations/metrics/MaxTests.java | 31 +++++++++++++++ .../search/aggregations/metrics/SumTests.java | 31 +++++++++++++++ 9 files changed, 280 insertions(+), 36 deletions(-) create mode 100644 core/src/test/java/org/elasticsearch/search/aggregations/metrics/AvgTests.java create mode 100644 core/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxTests.java create mode 100644 core/src/test/java/org/elasticsearch/search/aggregations/metrics/SumTests.java diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregator.java index c370397c008..d87137a7891 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregator.java @@ -19,10 +19,13 @@ package org.elasticsearch.search.aggregations.metrics.avg; import org.apache.lucene.index.LeafReaderContext; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.DoubleArray; import org.elasticsearch.common.util.LongArray; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; @@ -31,9 +34,11 @@ import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; +import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; -import org.elasticsearch.search.aggregations.support.ValuesSourceParser; +import org.elasticsearch.search.aggregations.support.ValuesSourceType; import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; @@ -115,8 +120,8 @@ public class AvgAggregator extends NumericMetricsAggregator.SingleValue { public static class Factory extends ValuesSourceAggregatorFactory.LeafOnly { - public Factory(String name, String type, ValuesSourceParser.Input valuesSourceInput) { - super(name, type, valuesSourceInput); + public Factory(String name) { + super(name, InternalAvg.TYPE.name(), ValuesSourceType.NUMERIC, ValueType.NUMERIC); } @Override @@ -132,6 +137,32 @@ public class AvgAggregator extends NumericMetricsAggregator.SingleValue { throws IOException { return new AvgAggregator(name, valuesSource, config.formatter(), aggregationContext, parent, pipelineAggregators, metaData); } + + @Override + protected ValuesSourceAggregatorFactory innerReadFrom(String name, ValuesSourceType valuesSourceType, + ValueType targetValueType, StreamInput in) { + return new AvgAggregator.Factory(name); + } + + @Override + protected void innerWriteTo(StreamOutput out) { + // Do nothing, no extra state to write to stream + } + + @Override + public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { + return builder; + } + + @Override + protected int innerHashCode() { + return 0; + } + + @Override + protected boolean innerEquals(Object obj) { + return true; + } } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgParser.java index 08ee0170282..2db4bd56e15 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgParser.java @@ -18,29 +18,48 @@ */ package org.elasticsearch.search.aggregations.metrics.avg; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.search.aggregations.AggregatorFactory; -import org.elasticsearch.search.aggregations.metrics.NumericValuesSourceMetricsAggregatorParser; -import org.elasticsearch.search.aggregations.support.ValuesSource; -import org.elasticsearch.search.aggregations.support.ValuesSourceParser; +import org.elasticsearch.search.aggregations.support.AbstractValuesSourceParser.NumericValuesSourceParser; +import org.elasticsearch.search.aggregations.support.ValueType; +import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; +import org.elasticsearch.search.aggregations.support.ValuesSourceType; + +import java.io.IOException; +import java.util.Map; /** * */ -public class AvgParser extends NumericValuesSourceMetricsAggregatorParser { +public class AvgParser extends NumericValuesSourceParser { public AvgParser() { - super(InternalAvg.TYPE); + super(true, true, false); } @Override - protected AggregatorFactory createFactory(String aggregationName, ValuesSourceParser.Input input) { - return new AvgAggregator.Factory(aggregationName, type(), input); + public String type() { + return InternalAvg.TYPE.name(); + } + + @Override + protected boolean token(String aggregationName, String currentFieldName, XContentParser.Token token, XContentParser parser, + ParseFieldMatcher parseFieldMatcher, Map otherOptions) throws IOException { + return false; + } + + @Override + protected ValuesSourceAggregatorFactory createFactory(String aggregationName, ValuesSourceType valuesSourceType, + ValueType targetValueType, Map otherOptions) { + return new AvgAggregator.Factory(aggregationName); } - // NORELEASE implement this method when refactoring this aggregation @Override public AggregatorFactory getFactoryPrototype() { - return null; + return new AvgAggregator.Factory(null); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregator.java index 9868546a6c1..0edb5d48046 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregator.java @@ -19,9 +19,12 @@ package org.elasticsearch.search.aggregations.metrics.max; import org.apache.lucene.index.LeafReaderContext; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.DoubleArray; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.NumericDoubleValues; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; import org.elasticsearch.search.MultiValueMode; @@ -32,9 +35,11 @@ import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; +import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; -import org.elasticsearch.search.aggregations.support.ValuesSourceParser; +import org.elasticsearch.search.aggregations.support.ValuesSourceType; import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; @@ -116,8 +121,8 @@ public class MaxAggregator extends NumericMetricsAggregator.SingleValue { public static class Factory extends ValuesSourceAggregatorFactory.LeafOnly { - public Factory(String name, ValuesSourceParser.Input valuesSourceInput) { - super(name, InternalMax.TYPE.name(), valuesSourceInput); + public Factory(String name) { + super(name, InternalMax.TYPE.name(), ValuesSourceType.NUMERIC, ValueType.NUMERIC); } @Override @@ -132,6 +137,33 @@ public class MaxAggregator extends NumericMetricsAggregator.SingleValue { throws IOException { return new MaxAggregator(name, valuesSource, config.formatter(), aggregationContext, parent, pipelineAggregators, metaData); } + + @Override + protected ValuesSourceAggregatorFactory innerReadFrom(String name, ValuesSourceType valuesSourceType, + ValueType targetValueType, StreamInput in) { + return new MaxAggregator.Factory(name); + } + + @Override + protected void innerWriteTo(StreamOutput out) { + // Do nothing, no extra state to write to stream + } + + @Override + public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { + return builder; + } + + @Override + protected int innerHashCode() { + return 0; + } + + @Override + protected boolean innerEquals(Object obj) { + return true; + } + } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxParser.java index 5a46975c5f6..9acb6a04cc1 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxParser.java @@ -18,29 +18,48 @@ */ package org.elasticsearch.search.aggregations.metrics.max; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.search.aggregations.AggregatorFactory; -import org.elasticsearch.search.aggregations.metrics.NumericValuesSourceMetricsAggregatorParser; -import org.elasticsearch.search.aggregations.support.ValuesSource; -import org.elasticsearch.search.aggregations.support.ValuesSourceParser; +import org.elasticsearch.search.aggregations.support.AbstractValuesSourceParser.NumericValuesSourceParser; +import org.elasticsearch.search.aggregations.support.ValueType; +import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; +import org.elasticsearch.search.aggregations.support.ValuesSourceType; + +import java.io.IOException; +import java.util.Map; /** * */ -public class MaxParser extends NumericValuesSourceMetricsAggregatorParser { +public class MaxParser extends NumericValuesSourceParser { public MaxParser() { - super(InternalMax.TYPE); + super(true, true, false); } @Override - protected AggregatorFactory createFactory(String aggregationName, ValuesSourceParser.Input input) { - return new MaxAggregator.Factory(aggregationName, input); + public String type() { + return InternalMax.TYPE.name(); + } + + @Override + protected boolean token(String aggregationName, String currentFieldName, XContentParser.Token token, XContentParser parser, + ParseFieldMatcher parseFieldMatcher, Map otherOptions) throws IOException { + return false; + } + + @Override + protected ValuesSourceAggregatorFactory createFactory(String aggregationName, ValuesSourceType valuesSourceType, + ValueType targetValueType, Map otherOptions) { + return new MaxAggregator.Factory(aggregationName); } - // NORELEASE implement this method when refactoring this aggregation @Override public AggregatorFactory getFactoryPrototype() { - return null; + return new MaxAggregator.Factory(null); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregator.java index 00c31e08b8c..f13cb15ff0a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregator.java @@ -19,9 +19,12 @@ package org.elasticsearch.search.aggregations.metrics.sum; import org.apache.lucene.index.LeafReaderContext; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.DoubleArray; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; @@ -30,9 +33,11 @@ import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; +import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; -import org.elasticsearch.search.aggregations.support.ValuesSourceParser; +import org.elasticsearch.search.aggregations.support.ValuesSourceType; import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; @@ -107,8 +112,8 @@ public class SumAggregator extends NumericMetricsAggregator.SingleValue { public static class Factory extends ValuesSourceAggregatorFactory.LeafOnly { - public Factory(String name, ValuesSourceParser.Input valuesSourceInput) { - super(name, InternalSum.TYPE.name(), valuesSourceInput); + public Factory(String name) { + super(name, InternalSum.TYPE.name(), ValuesSourceType.NUMERIC, ValueType.NUMERIC); } @Override @@ -123,6 +128,32 @@ public class SumAggregator extends NumericMetricsAggregator.SingleValue { throws IOException { return new SumAggregator(name, valuesSource, config.formatter(), aggregationContext, parent, pipelineAggregators, metaData); } + + @Override + protected ValuesSourceAggregatorFactory innerReadFrom(String name, ValuesSourceType valuesSourceType, + ValueType targetValueType, StreamInput in) { + return new SumAggregator.Factory(name); + } + + @Override + protected void innerWriteTo(StreamOutput out) { + // Do nothing, no extra state to write to stream + } + + @Override + public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { + return builder; + } + + @Override + protected int innerHashCode() { + return 0; + } + + @Override + protected boolean innerEquals(Object obj) { + return true; + } } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumParser.java index 849fa2022d5..e91589e4f8a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumParser.java @@ -18,28 +18,47 @@ */ package org.elasticsearch.search.aggregations.metrics.sum; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.search.aggregations.AggregatorFactory; -import org.elasticsearch.search.aggregations.metrics.NumericValuesSourceMetricsAggregatorParser; -import org.elasticsearch.search.aggregations.support.ValuesSource; -import org.elasticsearch.search.aggregations.support.ValuesSourceParser; +import org.elasticsearch.search.aggregations.support.AbstractValuesSourceParser.NumericValuesSourceParser; +import org.elasticsearch.search.aggregations.support.ValueType; +import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; +import org.elasticsearch.search.aggregations.support.ValuesSourceType; + +import java.io.IOException; +import java.util.Map; /** * */ -public class SumParser extends NumericValuesSourceMetricsAggregatorParser { +public class SumParser extends NumericValuesSourceParser { public SumParser() { - super(InternalSum.TYPE); + super(true, true, false); } @Override - protected AggregatorFactory createFactory(String aggregationName, ValuesSourceParser.Input input) { - return new SumAggregator.Factory(aggregationName, input); + public String type() { + return InternalSum.TYPE.name(); + } + + @Override + protected boolean token(String aggregationName, String currentFieldName, XContentParser.Token token, XContentParser parser, + ParseFieldMatcher parseFieldMatcher, Map otherOptions) throws IOException { + return false; + } + + @Override + protected ValuesSourceAggregatorFactory createFactory(String aggregationName, ValuesSourceType valuesSourceType, + ValueType targetValueType, Map otherOptions) { + return new SumAggregator.Factory(aggregationName); } - // NORELEASE implement this method when refactoring this aggregation @Override public AggregatorFactory getFactoryPrototype() { - return null; + return new SumAggregator.Factory(null); } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/AvgTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/AvgTests.java new file mode 100644 index 00000000000..fa51fb20c3f --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/AvgTests.java @@ -0,0 +1,31 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.metrics; + +import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregator; + +public class AvgTests extends AbstractNumericMetricTestCase { + + @Override + protected AvgAggregator.Factory doCreateTestAggregatorFactory() { + return new AvgAggregator.Factory("foo"); + } + +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxTests.java new file mode 100644 index 00000000000..cff48889257 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxTests.java @@ -0,0 +1,31 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.metrics; + +import org.elasticsearch.search.aggregations.metrics.max.MaxAggregator; + +public class MaxTests extends AbstractNumericMetricTestCase { + + @Override + protected MaxAggregator.Factory doCreateTestAggregatorFactory() { + return new MaxAggregator.Factory("foo"); + } + +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/SumTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/SumTests.java new file mode 100644 index 00000000000..0d8d61e2c0d --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/SumTests.java @@ -0,0 +1,31 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.metrics; + +import org.elasticsearch.search.aggregations.metrics.sum.SumAggregator; + +public class SumTests extends AbstractNumericMetricTestCase { + + @Override + protected SumAggregator.Factory doCreateTestAggregatorFactory() { + return new SumAggregator.Factory("foo"); + } + +}