|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.search.aggregations; |
| 21 | + |
| 22 | +import org.apache.lucene.index.LeafReaderContext; |
| 23 | +import org.apache.lucene.search.ScoreMode; |
| 24 | +import org.elasticsearch.common.CheckedFunction; |
| 25 | +import org.elasticsearch.search.profile.aggregation.InternalAggregationProfileTree; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.function.BiConsumer; |
| 31 | + |
| 32 | +/** |
| 33 | + * An {@linkplain Aggregator} that delegates collection to another |
| 34 | + * {@linkplain Aggregator} and then translates its results into the results |
| 35 | + * you'd expect from another aggregation. |
| 36 | + */ |
| 37 | +public abstract class AdaptingAggregator extends Aggregator { |
| 38 | + private final Aggregator parent; |
| 39 | + private final Aggregator delegate; |
| 40 | + |
| 41 | + public AdaptingAggregator( |
| 42 | + Aggregator parent, |
| 43 | + AggregatorFactories subAggregators, |
| 44 | + CheckedFunction<AggregatorFactories, Aggregator, IOException> delegate |
| 45 | + ) throws IOException { |
| 46 | + // Its important we set parent first or else when we build the sub-aggregators they can fail because they'll call this.parent. |
| 47 | + this.parent = parent; |
| 48 | + /* |
| 49 | + * Lock the parent of the sub-aggregators to *this* instead of to |
| 50 | + * the delegate. This keeps the parent link shaped like the requested |
| 51 | + * agg tree. Thisis how it has always been and some aggs rely on it. |
| 52 | + */ |
| 53 | + this.delegate = delegate.apply(subAggregators.fixParent(this)); |
| 54 | + assert this.delegate.parent() == parent : "invalid parent set on delegate"; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Adapt the result from the collecting {@linkplain Aggregator} into the |
| 59 | + * result expected by this {@linkplain Aggregator}. |
| 60 | + */ |
| 61 | + protected abstract InternalAggregation adapt(InternalAggregation delegateResult); |
| 62 | + |
| 63 | + @Override |
| 64 | + public final void close() { |
| 65 | + delegate.close(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public final ScoreMode scoreMode() { |
| 70 | + return delegate.scoreMode(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public final String name() { |
| 75 | + return delegate.name(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public final Aggregator parent() { |
| 80 | + return parent; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public final Aggregator subAggregator(String name) { |
| 85 | + return delegate.subAggregator(name); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public final LeafBucketCollector getLeafCollector(LeafReaderContext ctx) throws IOException { |
| 90 | + return delegate.getLeafCollector(ctx); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public final void preCollection() throws IOException { |
| 95 | + delegate.preCollection(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public final InternalAggregation[] buildAggregations(long[] owningBucketOrds) throws IOException { |
| 100 | + InternalAggregation[] delegateResults = delegate.buildAggregations(owningBucketOrds); |
| 101 | + InternalAggregation[] result = new InternalAggregation[owningBucketOrds.length]; |
| 102 | + for (int ordIdx = 0; ordIdx < owningBucketOrds.length; ordIdx++) { |
| 103 | + result[ordIdx] = adapt(delegateResults[ordIdx]); |
| 104 | + } |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public final InternalAggregation buildEmptyAggregation() { |
| 110 | + return adapt(delegate.buildEmptyAggregation()); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public final Aggregator[] subAggregators() { |
| 115 | + return delegate.subAggregators(); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void collectDebugInfo(BiConsumer<String, Object> add) { |
| 120 | + super.collectDebugInfo(add); |
| 121 | + add.accept("delegate", InternalAggregationProfileTree.typeFromAggregator(delegate)); |
| 122 | + Map<String, Object> delegateDebug = new HashMap<>(); |
| 123 | + delegate.collectDebugInfo(delegateDebug::put); |
| 124 | + add.accept("delegate_debug", delegateDebug); |
| 125 | + } |
| 126 | + |
| 127 | + public Aggregator delegate() { |
| 128 | + return delegate; |
| 129 | + } |
| 130 | +} |
0 commit comments