AggregatorFactories now stores reducers as well as aggregators

These reducers will be passed through from the AggregatorParser
This commit is contained in:
Colin Goodheart-Smithe 2015-02-12 08:41:21 +00:00
parent c60bb4d73b
commit ae76239b0a
2 changed files with 18 additions and 10 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.search.aggregations;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.search.aggregations.reducers.Reducer;
import org.elasticsearch.search.aggregations.support.AggregationContext;
import java.io.IOException;
@ -35,13 +36,19 @@ public class AggregatorFactories {
public static final AggregatorFactories EMPTY = new Empty();
private AggregatorFactory[] factories;
private List<Reducer> reducers;
public static Builder builder() {
return new Builder();
}
private AggregatorFactories(AggregatorFactory[] factories) {
private AggregatorFactories(AggregatorFactory[] factories, List<Reducer> reducers) {
this.factories = factories;
this.reducers = reducers;
}
public List<Reducer> reducers() {
return reducers;
}
private static Aggregator createAndRegisterContextAware(AggregationContext context, AggregatorFactory factory, Aggregator parent, boolean collectsFromSingleBucket) throws IOException {
@ -100,9 +107,10 @@ public class AggregatorFactories {
private static final AggregatorFactory[] EMPTY_FACTORIES = new AggregatorFactory[0];
private static final Aggregator[] EMPTY_AGGREGATORS = new Aggregator[0];
private static final List<Reducer> EMPTY_REDUCERS = new ArrayList<>();
private Empty() {
super(EMPTY_FACTORIES);
super(EMPTY_FACTORIES, EMPTY_REDUCERS);
}
@Override
@ -121,6 +129,7 @@ public class AggregatorFactories {
private final Set<String> names = new HashSet<>();
private final List<AggregatorFactory> factories = new ArrayList<>();
private List<Reducer> reducers = new ArrayList<>();
public Builder add(AggregatorFactory factory) {
if (!names.add(factory.name)) {
@ -130,11 +139,16 @@ public class AggregatorFactories {
return this;
}
public Builder setReducers(List<Reducer> reducers) {
this.reducers = reducers;
return this;
}
public AggregatorFactories build() {
if (factories.isEmpty()) {
return EMPTY;
}
return new AggregatorFactories(factories.toArray(new AggregatorFactory[factories.size()]));
return new AggregatorFactories(factories.toArray(new AggregatorFactory[factories.size()]), this.reducers);
}
}
}

View File

@ -28,7 +28,6 @@ import org.elasticsearch.search.aggregations.support.AggregationContext;
import org.elasticsearch.search.internal.SearchContext.Lifetime;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -41,7 +40,6 @@ public abstract class AggregatorFactory {
protected String type;
protected AggregatorFactory parent;
protected AggregatorFactories factories = AggregatorFactories.EMPTY;
protected List<Reducer> reducers = Collections.emptyList();
protected Map<String, Object> metaData;
/**
@ -97,7 +95,7 @@ public abstract class AggregatorFactory {
* @return The created aggregator
*/
public final Aggregator create(AggregationContext context, Aggregator parent, boolean collectsFromSingleBucket) throws IOException {
return createInternal(context, parent, collectsFromSingleBucket, this.reducers, this.metaData);
return createInternal(context, parent, collectsFromSingleBucket, this.factories.reducers(), this.metaData);
}
public void doValidate() {
@ -108,10 +106,6 @@ public abstract class AggregatorFactory {
}
public void setReducers(List<Reducer> reducers) {
this.reducers = reducers;
}
/**
* Utility method. Given an {@link AggregatorFactory} that creates {@link Aggregator}s that only know how