Skip to content

Commit

Permalink
Simplify generics (by Ignacio)
Browse files Browse the repository at this point in the history
Co-authored-by: Ignacio Vera <[email protected]>
  • Loading branch information
craigtaverner and iverase committed Sep 5, 2022
1 parent cd5a740 commit 050df95
Show file tree
Hide file tree
Showing 21 changed files with 64 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.DoubleValues;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.LeafPointFieldData;
import org.elasticsearch.index.fielddata.MultiPointValues;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.plain.LeafGeoPointFieldData;

import java.io.IOException;

Expand All @@ -27,8 +27,8 @@ final class GeoEmptyValueSource extends FieldDataBasedDoubleValuesSource {

@Override
public DoubleValues getValues(LeafReaderContext leaf, DoubleValues scores) {
LeafPointFieldData<?> leafData = (LeafPointFieldData<?>) fieldData.load(leaf);
final MultiPointValues<?> values = leafData.getPointValues();
LeafGeoPointFieldData leafData = (LeafGeoPointFieldData) fieldData.load(leaf);
final MultiGeoPointValues values = leafData.getPointValues(); // shade
return new DoubleValues() {
@Override
public double doubleValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.DoubleValues;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.MultiPointValues;
import org.elasticsearch.index.fielddata.plain.AbstractLeafGeoPointFieldData;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.plain.LeafGeoPointFieldData;

import java.io.IOException;

Expand All @@ -28,8 +27,8 @@ final class GeoLatitudeValueSource extends FieldDataBasedDoubleValuesSource {

@Override
public DoubleValues getValues(LeafReaderContext leaf, DoubleValues scores) {
AbstractLeafGeoPointFieldData leafData = (AbstractLeafGeoPointFieldData) fieldData.load(leaf);
final MultiPointValues<GeoPoint> values = leafData.getPointValues();
LeafGeoPointFieldData leafData = (LeafGeoPointFieldData) fieldData.load(leaf);
final MultiGeoPointValues values = leafData.getPointValues();
return new DoubleValues() {
@Override
public double doubleValue() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.DoubleValues;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.MultiPointValues;
import org.elasticsearch.index.fielddata.plain.AbstractLeafGeoPointFieldData;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.plain.LeafGeoPointFieldData;

import java.io.IOException;

Expand All @@ -28,8 +27,8 @@ final class GeoLongitudeValueSource extends FieldDataBasedDoubleValuesSource {

@Override
public DoubleValues getValues(LeafReaderContext leaf, DoubleValues scores) {
AbstractLeafGeoPointFieldData leafData = (AbstractLeafGeoPointFieldData) fieldData.load(leaf);
final MultiPointValues<GeoPoint> values = leafData.getPointValues();
LeafGeoPointFieldData leafData = (LeafGeoPointFieldData) fieldData.load(leaf);
final MultiGeoPointValues values = leafData.getPointValues();
return new DoubleValues() {
@Override
public double doubleValue() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.GeoPointValues;
import org.elasticsearch.index.fielddata.MultiPointValues;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.NumericDoubleValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.index.fielddata.SortingNumericDoubleValues;
Expand Down Expand Up @@ -522,10 +522,10 @@ public static double planeDistance(double lat1, double lon1, double lat2, double
public static SortedNumericDoubleValues distanceValues(
final GeoDistance distance,
final DistanceUnit unit,
final MultiPointValues<GeoPoint> geoPointValues,
final MultiGeoPointValues geoPointValues,
final GeoPoint... fromPoints
) {
final GeoPointValues singleValues = (GeoPointValues) FieldData.unwrapSingleton(geoPointValues);
final GeoPointValues singleValues = FieldData.unwrapSingleton(geoPointValues);
if (singleValues != null && fromPoints.length == 1) {
return FieldData.singleton(new NumericDoubleValues() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public static NumericDoubleValues unwrapSingleton(SortedNumericDoubleValues valu
* Returns a single-valued view of the {@link MultiGeoPointValues},
* if the wrapped {@link SortedNumericDocValues} is a singleton.
*/
public static PointValues<?> unwrapSingleton(MultiPointValues<? extends ElasticPoint> values) {
public static GeoPointValues unwrapSingleton(MultiGeoPointValues values) {
return values.getPointValues();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.search.SortField;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.index.fielddata.plain.AbstractLeafGeoPointFieldData;
import org.elasticsearch.index.fielddata.plain.LeafGeoPointFieldData;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.script.GeoPointFieldScript;
import org.elasticsearch.script.field.ToScriptFieldFactory;
Expand All @@ -24,16 +23,16 @@
import org.elasticsearch.search.sort.BucketedSort;
import org.elasticsearch.search.sort.SortOrder;

public class GeoPointScriptFieldData implements IndexGeoPointFieldData {
public final class GeoPointScriptFieldData implements IndexGeoPointFieldData {
public static class Builder implements IndexFieldData.Builder {
private final String name;
private final GeoPointFieldScript.LeafFactory leafFactory;
private final ToScriptFieldFactory<MultiPointValues<GeoPoint>> toScriptFieldFactory;
private final ToScriptFieldFactory<MultiGeoPointValues> toScriptFieldFactory;

public Builder(
String name,
GeoPointFieldScript.LeafFactory leafFactory,
ToScriptFieldFactory<MultiPointValues<GeoPoint>> toScriptFieldFactory
ToScriptFieldFactory<MultiGeoPointValues> toScriptFieldFactory
) {
this.name = name;
this.leafFactory = leafFactory;
Expand All @@ -48,12 +47,12 @@ public GeoPointScriptFieldData build(IndexFieldDataCache cache, CircuitBreakerSe

private final GeoPointFieldScript.LeafFactory leafFactory;
private final String name;
private final ToScriptFieldFactory<MultiPointValues<GeoPoint>> toScriptFieldFactory;
private final ToScriptFieldFactory<MultiGeoPointValues> toScriptFieldFactory;

private GeoPointScriptFieldData(
String fieldName,
GeoPointFieldScript.LeafFactory leafFactory,
ToScriptFieldFactory<MultiPointValues<GeoPoint>> toScriptFieldFactory
ToScriptFieldFactory<MultiGeoPointValues> toScriptFieldFactory
) {
this.name = fieldName;
this.leafFactory = leafFactory;
Expand Down Expand Up @@ -90,9 +89,9 @@ public ValuesSourceType getValuesSourceType() {
}

@Override
public LeafPointFieldData<GeoPoint> load(LeafReaderContext context) {
public LeafPointFieldData<MultiGeoPointValues> load(LeafReaderContext context) {
GeoPointFieldScript script = leafFactory.newInstance(context);
return new AbstractLeafGeoPointFieldData(toScriptFieldFactory) {
return new LeafGeoPointFieldData(toScriptFieldFactory) {
@Override
public SortedNumericDocValues getSortedNumericDocValues() {
return new GeoPointScriptDocValues(script);
Expand All @@ -111,7 +110,7 @@ public void close() {
}

@Override
public LeafPointFieldData<GeoPoint> loadDirect(LeafReaderContext context) {
public LeafPointFieldData<MultiGeoPointValues> loadDirect(LeafReaderContext context) {
return load(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

package org.elasticsearch.index.fielddata;

import org.elasticsearch.common.geo.GeoPoint;

/**
* Specialization of {@link IndexFieldData} for geo points.
*/
public interface IndexGeoPointFieldData extends IndexPointFieldData<GeoPoint> {}
public interface IndexGeoPointFieldData extends IndexPointFieldData<MultiGeoPointValues> {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

package org.elasticsearch.index.fielddata;

import org.elasticsearch.common.geo.ElasticPoint;

/**
* Specialization of {@link IndexFieldData} for geo points and points.
*/
public interface IndexPointFieldData<T extends ElasticPoint> extends IndexFieldData<LeafPointFieldData<T>> {}
public interface IndexPointFieldData<T extends MultiPointValues<?>> extends IndexFieldData<LeafPointFieldData<T>> {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
package org.elasticsearch.index.fielddata;

import org.apache.lucene.index.SortedNumericDocValues;
import org.elasticsearch.common.geo.ElasticPoint;

/**
* {@link LeafFieldData} specialization for geo points and points.
*/
public abstract class LeafPointFieldData<T extends ElasticPoint> implements LeafFieldData {
public abstract class LeafPointFieldData<T extends MultiPointValues<?>> implements LeafFieldData {

/**
* Return geo-point or point values.
*/
public abstract MultiPointValues<T> getPointValues();
public abstract T getPointValues();

/**
* Return the internal representation of geo_point or point doc values as a {@link SortedNumericDocValues}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package org.elasticsearch.index.fielddata.plain;

import org.apache.lucene.search.SortField;
import org.elasticsearch.common.geo.ElasticPoint;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested;
Expand All @@ -21,16 +20,16 @@
import org.elasticsearch.search.sort.BucketedSort;
import org.elasticsearch.search.sort.SortOrder;

public abstract class AbstractPointIndexFieldData<T extends ElasticPoint> implements IndexPointFieldData<T> {
abstract class AbstractPointIndexFieldData<T extends MultiPointValues<?>> implements IndexPointFieldData<T> {

protected final String fieldName;
protected final ValuesSourceType valuesSourceType;
protected final ToScriptFieldFactory<MultiPointValues<T>> toScriptFieldFactory;
protected final ToScriptFieldFactory<T> toScriptFieldFactory;

protected AbstractPointIndexFieldData(
String fieldName,
ValuesSourceType valuesSourceType,
ToScriptFieldFactory<MultiPointValues<T>> toScriptFieldFactory
ToScriptFieldFactory<T> toScriptFieldFactory
) {
this.fieldName = fieldName;
this.valuesSourceType = valuesSourceType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.util.Accountable;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.fielddata.MultiPointValues;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.script.field.ToScriptFieldFactory;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

final class LatLonPointDVLeafFieldData extends AbstractLeafGeoPointFieldData {
final class LatLonPointDVLeafFieldData extends LeafGeoPointFieldData {
private final LeafReader reader;
private final String fieldName;

LatLonPointDVLeafFieldData(LeafReader reader, String fieldName, ToScriptFieldFactory<MultiPointValues<GeoPoint>> toScriptFieldFactory) {
LatLonPointDVLeafFieldData(LeafReader reader, String fieldName, ToScriptFieldFactory<MultiGeoPointValues> toScriptFieldFactory) {
super(toScriptFieldFactory);
this.reader = reader;
this.fieldName = fieldName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,26 @@
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexFieldDataCache;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.fielddata.LeafPointFieldData;
import org.elasticsearch.index.fielddata.MultiPointValues;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.script.field.ToScriptFieldFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;

public class LatLonPointIndexFieldData extends AbstractPointIndexFieldData<GeoPoint> implements IndexGeoPointFieldData {
public final class LatLonPointIndexFieldData extends AbstractPointIndexFieldData<MultiGeoPointValues> implements IndexGeoPointFieldData {
public LatLonPointIndexFieldData(
String fieldName,
ValuesSourceType valuesSourceType,
ToScriptFieldFactory<MultiPointValues<GeoPoint>> toScriptFieldFactory
ToScriptFieldFactory<MultiGeoPointValues> toScriptFieldFactory
) {
super(fieldName, valuesSourceType, toScriptFieldFactory);
}

@Override
public LeafPointFieldData<GeoPoint> load(LeafReaderContext context) {
public LeafPointFieldData<MultiGeoPointValues> load(LeafReaderContext context) {
LeafReader reader = context.reader();
FieldInfo info = reader.getFieldInfos().fieldInfo(fieldName);
if (info != null) {
Expand All @@ -43,7 +42,7 @@ public LeafPointFieldData<GeoPoint> load(LeafReaderContext context) {
}

@Override
public LeafPointFieldData<GeoPoint> loadDirect(LeafReaderContext context) throws Exception {
public LeafPointFieldData<MultiGeoPointValues> loadDirect(LeafReaderContext context) throws Exception {
return load(context);
}

Expand All @@ -67,13 +66,9 @@ static void checkCompatible(FieldInfo fieldInfo) {
public static class Builder implements IndexFieldData.Builder {
private final String name;
private final ValuesSourceType valuesSourceType;
private final ToScriptFieldFactory<MultiPointValues<GeoPoint>> toScriptFieldFactory;
private final ToScriptFieldFactory<MultiGeoPointValues> toScriptFieldFactory;

public Builder(
String name,
ValuesSourceType valuesSourceType,
ToScriptFieldFactory<MultiPointValues<GeoPoint>> toScriptFieldFactory
) {
public Builder(String name, ValuesSourceType valuesSourceType, ToScriptFieldFactory<MultiGeoPointValues> toScriptFieldFactory) {
this.name = name;
this.valuesSourceType = valuesSourceType;
this.toScriptFieldFactory = toScriptFieldFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
*/
package org.elasticsearch.index.fielddata.plain;

import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.LeafPointFieldData;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.MultiPointValues;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
import org.elasticsearch.script.field.ToScriptFieldFactory;

public abstract class AbstractLeafGeoPointFieldData extends LeafPointFieldData<GeoPoint> {
public abstract class LeafGeoPointFieldData extends LeafPointFieldData<MultiGeoPointValues> {

protected final ToScriptFieldFactory<MultiPointValues<GeoPoint>> toScriptFieldFactory;
protected final ToScriptFieldFactory<MultiGeoPointValues> toScriptFieldFactory;

public AbstractLeafGeoPointFieldData(ToScriptFieldFactory<MultiPointValues<GeoPoint>> toScriptFieldFactory) {
public LeafGeoPointFieldData(ToScriptFieldFactory<MultiGeoPointValues> toScriptFieldFactory) {
this.toScriptFieldFactory = toScriptFieldFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.fielddata.MultiPointValues;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.NumericDoubleValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.index.fielddata.SortingNumericDoubleValues;
Expand Down Expand Up @@ -382,7 +382,7 @@ public boolean needsScores() {

@Override
protected NumericDoubleValues distance(LeafReaderContext context) {
final MultiPointValues<GeoPoint> geoPointValues = fieldData.load(context).getPointValues();
final MultiGeoPointValues geoPointValues = fieldData.load(context).getPointValues();
return FieldData.replaceMissing(mode.select(new SortingNumericDoubleValues() {
@Override
public boolean advanceExact(int docId) throws IOException {
Expand Down Expand Up @@ -413,7 +413,7 @@ public boolean advanceExact(int docId) throws IOException {
protected String getDistanceString(LeafReaderContext ctx, int docId) throws IOException {
StringBuilder values = new StringBuilder(mode.name());
values.append(" of: [");
final MultiPointValues<GeoPoint> geoPointValues = fieldData.load(ctx).getPointValues();
final MultiGeoPointValues geoPointValues = fieldData.load(ctx).getPointValues();
if (geoPointValues.advanceExact(docId)) {
final int num = geoPointValues.docValueCount();
for (int i = 0; i < num; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import org.elasticsearch.common.geo.GeoBoundingBox;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.index.fielddata.MultiPointValues;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.ScriptDocValues;

public class GeoPointDocValuesField extends PointDocValuesField<GeoPoint> {
// maintain bwc by making centroid and bounding box available to ScriptDocValues.GeoPoints
private ScriptDocValues.GeoPoints geoPoints = null;

public GeoPointDocValuesField(MultiPointValues<GeoPoint> input, String name) {
public GeoPointDocValuesField(MultiGeoPointValues input, String name) {
super(input, name, GeoPoint::new, new GeoBoundingBox(new GeoPoint(), new GeoPoint()), new GeoPoint[0]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final boolean isFloatingPoint() {
@Override
public final SortedNumericDocValues longValues(LeafReaderContext ctx) {
final MultiGeoPointValues multiGeoPointValues = valuesSource.geoPointValues(ctx);
final GeoPointValues values = (GeoPointValues) org.elasticsearch.index.fielddata.FieldData.unwrapSingleton(multiGeoPointValues);
final GeoPointValues values = org.elasticsearch.index.fielddata.FieldData.unwrapSingleton(multiGeoPointValues);
if (geoBoundingBox.isUnbounded()) {
return values == null ? unboundedCellMultiValues(multiGeoPointValues) : DocValues.singleton(unboundedCellSingleValue(values));
} else {
Expand Down
Loading

0 comments on commit 050df95

Please sign in to comment.