-
Notifications
You must be signed in to change notification settings - Fork 25.3k
refactor inner geogrid classes to own class files #37596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/CellIdSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* 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.bucket.geogrid; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.index.SortedNumericDocValues; | ||
import org.elasticsearch.common.geo.GeoHashUtils; | ||
import org.elasticsearch.index.fielddata.AbstractSortingNumericDocValues; | ||
import org.elasticsearch.index.fielddata.MultiGeoPointValues; | ||
import org.elasticsearch.index.fielddata.SortedBinaryDocValues; | ||
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; | ||
import org.elasticsearch.search.aggregations.support.ValuesSource; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Wrapper class to help convert {@link MultiGeoPointValues} | ||
* to numeric long values for bucketing. | ||
*/ | ||
class CellIdSource extends ValuesSource.Numeric { | ||
private final ValuesSource.GeoPoint valuesSource; | ||
private final int precision; | ||
|
||
CellIdSource(GeoPoint valuesSource, int precision) { | ||
this.valuesSource = valuesSource; | ||
//different GeoPoints could map to the same or different geohash cells. | ||
this.precision = precision; | ||
} | ||
|
||
public int precision() { | ||
return precision; | ||
} | ||
|
||
@Override | ||
public boolean isFloatingPoint() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public SortedNumericDocValues longValues(LeafReaderContext ctx) { | ||
return new CellValues(valuesSource.geoPointValues(ctx), precision); | ||
} | ||
|
||
@Override | ||
public SortedNumericDoubleValues doubleValues(LeafReaderContext ctx) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public SortedBinaryDocValues bytesValues(LeafReaderContext ctx) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
private static class CellValues extends AbstractSortingNumericDocValues { | ||
private MultiGeoPointValues geoValues; | ||
private int precision; | ||
|
||
protected CellValues(MultiGeoPointValues geoValues, int precision) { | ||
this.geoValues = geoValues; | ||
this.precision = precision; | ||
} | ||
|
||
@Override | ||
public boolean advanceExact(int docId) throws IOException { | ||
if (geoValues.advanceExact(docId)) { | ||
resize(geoValues.docValueCount()); | ||
for (int i = 0; i < docValueCount(); ++i) { | ||
org.elasticsearch.common.geo.GeoPoint target = geoValues.nextValue(); | ||
values[i] = GeoHashUtils.longEncode(target.getLon(), target.getLat(), | ||
precision); | ||
} | ||
sort(); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridBucket.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* 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.bucket.geogrid; | ||
|
||
import org.elasticsearch.common.geo.GeoHashUtils; | ||
import org.elasticsearch.common.geo.GeoPoint; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.search.aggregations.Aggregation; | ||
import org.elasticsearch.search.aggregations.Aggregations; | ||
import org.elasticsearch.search.aggregations.InternalAggregation; | ||
import org.elasticsearch.search.aggregations.InternalAggregations; | ||
import org.elasticsearch.search.aggregations.InternalMultiBucketAggregation; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
class GeoGridBucket extends InternalMultiBucketAggregation.InternalBucket implements GeoHashGrid.Bucket, Comparable<GeoGridBucket> { | ||
|
||
protected long geohashAsLong; | ||
protected long docCount; | ||
protected InternalAggregations aggregations; | ||
|
||
GeoGridBucket(long geohashAsLong, long docCount, InternalAggregations aggregations) { | ||
this.docCount = docCount; | ||
this.aggregations = aggregations; | ||
this.geohashAsLong = geohashAsLong; | ||
} | ||
|
||
/** | ||
* Read from a stream. | ||
*/ | ||
GeoGridBucket(StreamInput in) throws IOException { | ||
geohashAsLong = in.readLong(); | ||
docCount = in.readVLong(); | ||
aggregations = InternalAggregations.readAggregations(in); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeLong(geohashAsLong); | ||
out.writeVLong(docCount); | ||
aggregations.writeTo(out); | ||
} | ||
|
||
@Override | ||
public String getKeyAsString() { | ||
return GeoHashUtils.stringEncode(geohashAsLong); | ||
} | ||
|
||
@Override | ||
public GeoPoint getKey() { | ||
return GeoPoint.fromGeohash(geohashAsLong); | ||
} | ||
|
||
@Override | ||
public long getDocCount() { | ||
return docCount; | ||
} | ||
|
||
@Override | ||
public Aggregations getAggregations() { | ||
return aggregations; | ||
} | ||
|
||
@Override | ||
public int compareTo(GeoGridBucket other) { | ||
if (this.geohashAsLong > other.geohashAsLong) { | ||
return 1; | ||
} | ||
if (this.geohashAsLong < other.geohashAsLong) { | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
public GeoGridBucket reduce(List<? extends GeoGridBucket> buckets, InternalAggregation.ReduceContext context) { | ||
List<InternalAggregations> aggregationsList = new ArrayList<>(buckets.size()); | ||
long docCount = 0; | ||
for (GeoGridBucket bucket : buckets) { | ||
docCount += bucket.docCount; | ||
aggregationsList.add(bucket.aggregations); | ||
} | ||
final InternalAggregations aggs = InternalAggregations.reduce(aggregationsList, context); | ||
return new GeoGridBucket(geohashAsLong, docCount, aggs); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(Aggregation.CommonFields.KEY.getPreferredName(), getKeyAsString()); | ||
builder.field(Aggregation.CommonFields.DOC_COUNT.getPreferredName(), docCount); | ||
aggregations.toXContentInternal(builder, params); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
GeoGridBucket bucket = (GeoGridBucket) o; | ||
return geohashAsLong == bucket.geohashAsLong && | ||
docCount == bucket.docCount && | ||
Objects.equals(aggregations, bucket.aggregations); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(geohashAsLong, docCount, aggregations); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.