Move CumulativeSumPipelineAgg to use ConstructingObjectParser parsing (#55990) (#56380)

As part of #52776, this refactors the aggregation to use
the context parser to parse its parameters.
This commit is contained in:
Tal Levy 2020-05-07 12:34:54 -07:00 committed by GitHub
parent b84d1e2577
commit 6e0178fb68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 55 deletions

View File

@ -600,7 +600,7 @@ public class SearchModule {
CumulativeSumPipelineAggregationBuilder.NAME,
CumulativeSumPipelineAggregationBuilder::new,
CumulativeSumPipelineAggregator::new,
CumulativeSumPipelineAggregationBuilder::parse));
CumulativeSumPipelineAggregationBuilder.PARSER));
registerPipelineAggregation(new PipelineAggregationSpec(
BucketScriptPipelineAggregationBuilder.NAME,
BucketScriptPipelineAggregationBuilder::new,

View File

@ -19,20 +19,18 @@
package org.elasticsearch.search.aggregations.pipeline;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.DocValueFormat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static org.elasticsearch.search.aggregations.pipeline.PipelineAggregator.Parser.BUCKETS_PATH;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.search.aggregations.pipeline.PipelineAggregator.Parser.FORMAT;
public class CumulativeSumPipelineAggregationBuilder extends AbstractPipelineAggregationBuilder<CumulativeSumPipelineAggregationBuilder> {
@ -40,10 +38,23 @@ public class CumulativeSumPipelineAggregationBuilder extends AbstractPipelineAgg
private String format;
@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<CumulativeSumPipelineAggregationBuilder, String> PARSER = new ConstructingObjectParser<>(
NAME, false, (args, name) -> new CumulativeSumPipelineAggregationBuilder(name, (List<String>) args[0]));
static {
PARSER.declareStringArray(constructorArg(), BUCKETS_PATH_FIELD);
PARSER.declareString(CumulativeSumPipelineAggregationBuilder::format, FORMAT);
};
public CumulativeSumPipelineAggregationBuilder(String name, String bucketsPath) {
super(name, NAME, new String[] { bucketsPath });
}
CumulativeSumPipelineAggregationBuilder(String name, List<String> bucketsPath) {
super(name, NAME, bucketsPath.toArray(new String[0]));
}
/**
* Read from a stream.
*/
@ -104,56 +115,6 @@ public class CumulativeSumPipelineAggregationBuilder extends AbstractPipelineAgg
return builder;
}
public static CumulativeSumPipelineAggregationBuilder parse(String pipelineAggregatorName, XContentParser parser)
throws IOException {
XContentParser.Token token;
String currentFieldName = null;
String[] bucketsPaths = null;
String format = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_STRING) {
if (FORMAT.match(currentFieldName, parser.getDeprecationHandler())) {
format = parser.text();
} else if (BUCKETS_PATH.match(currentFieldName, parser.getDeprecationHandler())) {
bucketsPaths = new String[] { parser.text() };
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + pipelineAggregatorName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.START_ARRAY) {
if (BUCKETS_PATH.match(currentFieldName, parser.getDeprecationHandler())) {
List<String> paths = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
String path = parser.text();
paths.add(path);
}
bucketsPaths = paths.toArray(new String[paths.size()]);
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + pipelineAggregatorName + "]: [" + currentFieldName + "].");
}
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unexpected token " + token + " in [" + pipelineAggregatorName + "].");
}
}
if (bucketsPaths == null) {
throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + BUCKETS_PATH.getPreferredName()
+ "] for derivative aggregation [" + pipelineAggregatorName + "]");
}
CumulativeSumPipelineAggregationBuilder factory =
new CumulativeSumPipelineAggregationBuilder(pipelineAggregatorName, bucketsPaths[0]);
if (format != null) {
factory.format(format);
}
return factory;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), format);