|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.spatial.aggregations.metrics; |
| 8 | + |
| 9 | +import org.apache.lucene.index.LeafReaderContext; |
| 10 | +import org.elasticsearch.common.lease.Releasables; |
| 11 | +import org.elasticsearch.common.util.BigArrays; |
| 12 | +import org.elasticsearch.common.util.DoubleArray; |
| 13 | +import org.elasticsearch.search.aggregations.Aggregator; |
| 14 | +import org.elasticsearch.search.aggregations.InternalAggregation; |
| 15 | +import org.elasticsearch.search.aggregations.LeafBucketCollector; |
| 16 | +import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; |
| 17 | +import org.elasticsearch.search.aggregations.metrics.InternalGeoBounds; |
| 18 | +import org.elasticsearch.search.aggregations.metrics.MetricsAggregator; |
| 19 | +import org.elasticsearch.search.internal.SearchContext; |
| 20 | +import org.elasticsearch.xpack.spatial.index.mapper.GeoShapeValuesSource; |
| 21 | +import org.elasticsearch.xpack.spatial.index.mapper.MultiGeoShapeValues; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +public final class GeoShapeBoundsAggregator extends MetricsAggregator { |
| 27 | + private final GeoShapeValuesSource valuesSource; |
| 28 | + private final boolean wrapLongitude; |
| 29 | + private DoubleArray tops; |
| 30 | + private DoubleArray bottoms; |
| 31 | + private DoubleArray posLefts; |
| 32 | + private DoubleArray posRights; |
| 33 | + private DoubleArray negLefts; |
| 34 | + private DoubleArray negRights; |
| 35 | + |
| 36 | + public GeoShapeBoundsAggregator(String name, SearchContext aggregationContext, Aggregator parent, |
| 37 | + GeoShapeValuesSource valuesSource, boolean wrapLongitude, Map<String, Object> metadata) throws IOException { |
| 38 | + super(name, aggregationContext, parent, metadata); |
| 39 | + this.valuesSource = valuesSource; |
| 40 | + this.wrapLongitude = wrapLongitude; |
| 41 | + if (valuesSource != null) { |
| 42 | + final BigArrays bigArrays = context.bigArrays(); |
| 43 | + tops = bigArrays.newDoubleArray(1, false); |
| 44 | + tops.fill(0, tops.size(), Double.NEGATIVE_INFINITY); |
| 45 | + bottoms = bigArrays.newDoubleArray(1, false); |
| 46 | + bottoms.fill(0, bottoms.size(), Double.POSITIVE_INFINITY); |
| 47 | + posLefts = bigArrays.newDoubleArray(1, false); |
| 48 | + posLefts.fill(0, posLefts.size(), Double.POSITIVE_INFINITY); |
| 49 | + posRights = bigArrays.newDoubleArray(1, false); |
| 50 | + posRights.fill(0, posRights.size(), Double.NEGATIVE_INFINITY); |
| 51 | + negLefts = bigArrays.newDoubleArray(1, false); |
| 52 | + negLefts.fill(0, negLefts.size(), Double.POSITIVE_INFINITY); |
| 53 | + negRights = bigArrays.newDoubleArray(1, false); |
| 54 | + negRights.fill(0, negRights.size(), Double.NEGATIVE_INFINITY); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, |
| 60 | + LeafBucketCollector sub) { |
| 61 | + if (valuesSource == null) { |
| 62 | + return LeafBucketCollector.NO_OP_COLLECTOR; |
| 63 | + } |
| 64 | + final BigArrays bigArrays = context.bigArrays(); |
| 65 | + final MultiGeoShapeValues values = valuesSource.geoShapeValues(ctx); |
| 66 | + return new LeafBucketCollectorBase(sub, values) { |
| 67 | + @Override |
| 68 | + public void collect(int doc, long bucket) throws IOException { |
| 69 | + if (bucket >= tops.size()) { |
| 70 | + long from = tops.size(); |
| 71 | + tops = bigArrays.grow(tops, bucket + 1); |
| 72 | + tops.fill(from, tops.size(), Double.NEGATIVE_INFINITY); |
| 73 | + bottoms = bigArrays.resize(bottoms, tops.size()); |
| 74 | + bottoms.fill(from, bottoms.size(), Double.POSITIVE_INFINITY); |
| 75 | + posLefts = bigArrays.resize(posLefts, tops.size()); |
| 76 | + posLefts.fill(from, posLefts.size(), Double.POSITIVE_INFINITY); |
| 77 | + posRights = bigArrays.resize(posRights, tops.size()); |
| 78 | + posRights.fill(from, posRights.size(), Double.NEGATIVE_INFINITY); |
| 79 | + negLefts = bigArrays.resize(negLefts, tops.size()); |
| 80 | + negLefts.fill(from, negLefts.size(), Double.POSITIVE_INFINITY); |
| 81 | + negRights = bigArrays.resize(negRights, tops.size()); |
| 82 | + negRights.fill(from, negRights.size(), Double.NEGATIVE_INFINITY); |
| 83 | + } |
| 84 | + |
| 85 | + if (values.advanceExact(doc)) { |
| 86 | + final int valuesCount = values.docValueCount(); |
| 87 | + |
| 88 | + for (int i = 0; i < valuesCount; ++i) { |
| 89 | + MultiGeoShapeValues.GeoShapeValue value = values.nextValue(); |
| 90 | + MultiGeoShapeValues.BoundingBox bounds = value.boundingBox(); |
| 91 | + double top = Math.max(tops.get(bucket), bounds.top); |
| 92 | + double bottom = Math.min(bottoms.get(bucket), bounds.bottom); |
| 93 | + double posLeft = Math.min(posLefts.get(bucket), bounds.posLeft); |
| 94 | + double posRight = Math.max(posRights.get(bucket), bounds.posRight); |
| 95 | + double negLeft = Math.min(negLefts.get(bucket), bounds.negLeft); |
| 96 | + double negRight = Math.max(negRights.get(bucket), bounds.negRight); |
| 97 | + tops.set(bucket, top); |
| 98 | + bottoms.set(bucket, bottom); |
| 99 | + posLefts.set(bucket, posLeft); |
| 100 | + posRights.set(bucket, posRight); |
| 101 | + negLefts.set(bucket, negLeft); |
| 102 | + negRights.set(bucket, negRight); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + @Override |
| 111 | + public InternalAggregation buildAggregation(long owningBucketOrdinal) { |
| 112 | + if (valuesSource == null) { |
| 113 | + return buildEmptyAggregation(); |
| 114 | + } |
| 115 | + double top = tops.get(owningBucketOrdinal); |
| 116 | + double bottom = bottoms.get(owningBucketOrdinal); |
| 117 | + double posLeft = posLefts.get(owningBucketOrdinal); |
| 118 | + double posRight = posRights.get(owningBucketOrdinal); |
| 119 | + double negLeft = negLefts.get(owningBucketOrdinal); |
| 120 | + double negRight = negRights.get(owningBucketOrdinal); |
| 121 | + return new InternalGeoBounds(name, top, bottom, posLeft, posRight, negLeft, negRight, wrapLongitude, metadata()); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public InternalAggregation buildEmptyAggregation() { |
| 126 | + return new InternalGeoBounds(name, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, |
| 127 | + Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, wrapLongitude, metadata()); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void doClose() { |
| 132 | + Releasables.close(tops, bottoms, posLefts, posRights, negLefts, negRights); |
| 133 | + } |
| 134 | +} |
0 commit comments