Skip to content
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

Remove ShapeValues from Geo specific classes in favour of GeoShapeValues #90100

Merged
merged 2 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {

private double scriptHeight(Map<String, Object> vars) {
Map<?, ?> doc = (Map<?, ?>) vars.get("doc");
LeafShapeFieldData.ShapeScriptValues<GeoPoint> geometry = assertGeometry(doc);
LeafShapeFieldData.ShapeScriptValues<GeoPoint, GeoShapeValues.GeoShapeValue> geometry = assertGeometry(doc);
if (geometry.size() == 0) {
return Double.NaN;
} else {
Expand All @@ -92,7 +92,7 @@ private double scriptHeight(Map<String, Object> vars) {

private double scriptWidth(Map<String, Object> vars) {
Map<?, ?> doc = (Map<?, ?>) vars.get("doc");
LeafShapeFieldData.ShapeScriptValues<GeoPoint> geometry = assertGeometry(doc);
LeafShapeFieldData.ShapeScriptValues<GeoPoint, GeoShapeValues.GeoShapeValue> geometry = assertGeometry(doc);
if (geometry.size() == 0) {
return Double.NaN;
} else {
Expand All @@ -103,29 +103,29 @@ private double scriptWidth(Map<String, Object> vars) {

private double scriptLat(Map<String, Object> vars) {
Map<?, ?> doc = (Map<?, ?>) vars.get("doc");
LeafShapeFieldData.ShapeScriptValues<GeoPoint> geometry = assertGeometry(doc);
LeafShapeFieldData.ShapeScriptValues<GeoPoint, GeoShapeValues.GeoShapeValue> geometry = assertGeometry(doc);
return geometry.size() == 0 ? Double.NaN : geometry.getCentroid().lat();
}

private double scriptLon(Map<String, Object> vars) {
Map<?, ?> doc = (Map<?, ?>) vars.get("doc");
LeafShapeFieldData.ShapeScriptValues<GeoPoint> geometry = assertGeometry(doc);
LeafShapeFieldData.ShapeScriptValues<GeoPoint, GeoShapeValues.GeoShapeValue> geometry = assertGeometry(doc);
return geometry.size() == 0 ? Double.NaN : geometry.getCentroid().lon();
}

private double scriptLabelLat(Map<String, Object> vars) {
Map<?, ?> doc = (Map<?, ?>) vars.get("doc");
LeafShapeFieldData.ShapeScriptValues<GeoPoint> geometry = assertGeometry(doc);
LeafShapeFieldData.ShapeScriptValues<GeoPoint, GeoShapeValues.GeoShapeValue> geometry = assertGeometry(doc);
return geometry.size() == 0 ? Double.NaN : geometry.getLabelPosition().lat();
}

private double scriptLabelLon(Map<String, Object> vars) {
Map<?, ?> doc = (Map<?, ?>) vars.get("doc");
LeafShapeFieldData.ShapeScriptValues<GeoPoint> geometry = assertGeometry(doc);
LeafShapeFieldData.ShapeScriptValues<GeoPoint, GeoShapeValues.GeoShapeValue> geometry = assertGeometry(doc);
return geometry.size() == 0 ? Double.NaN : geometry.getLabelPosition().lon();
}

private LeafShapeFieldData.ShapeScriptValues<GeoPoint> assertGeometry(Map<?, ?> doc) {
private LeafShapeFieldData.ShapeScriptValues<GeoPoint, GeoShapeValues.GeoShapeValue> assertGeometry(Map<?, ?> doc) {
AbstractAtomicGeoShapeShapeFieldData.GeoShapeScriptValues geometry =
(AbstractAtomicGeoShapeShapeFieldData.GeoShapeScriptValues) doc.get("location");
if (geometry.size() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*
* There is just one value for one document.
*/
public abstract class GeoShapeValues extends ShapeValues {
public abstract class GeoShapeValues extends ShapeValues<GeoShapeValues.GeoShapeValue> {

public static GeoShapeValues EMPTY = new GeoShapeValues() {
private final GeoShapeValuesSourceType DEFAULT_VALUES_SOURCE_TYPE = GeoShapeValuesSourceType.instance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
/**
* Specialization of {@link IndexFieldData} for geo shapes and shapes.
*/
public interface IndexShapeFieldData extends IndexFieldData<LeafShapeFieldData> {}
public interface IndexShapeFieldData<T extends ShapeValues<?>> extends IndexFieldData<LeafShapeFieldData<T>> {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package org.elasticsearch.xpack.spatial.index.fielddata;

import org.apache.lucene.util.Accountable;
import org.elasticsearch.common.geo.BoundingBox;
import org.elasticsearch.common.geo.SpatialPoint;
import org.elasticsearch.index.fielddata.LeafFieldData;
Expand All @@ -16,50 +15,20 @@
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
import org.elasticsearch.script.field.ToScriptFieldFactory;

import java.util.Collection;
import java.util.Collections;

/**
* {@link LeafFieldData} specialization for geo-shapes and shapes.
*/
public abstract class LeafShapeFieldData implements LeafFieldData {
protected final ToScriptFieldFactory<ShapeValues> toScriptFieldFactory;
public abstract class LeafShapeFieldData<T extends ShapeValues<?>> implements LeafFieldData {
protected final ToScriptFieldFactory<T> toScriptFieldFactory;

public LeafShapeFieldData(ToScriptFieldFactory<ShapeValues> toScriptFieldFactory) {
public LeafShapeFieldData(ToScriptFieldFactory<T> toScriptFieldFactory) {
this.toScriptFieldFactory = toScriptFieldFactory;
}

public static class Empty<T extends SpatialPoint> extends LeafShapeFieldData {
private final ShapeValues emptyValues;

public Empty(ToScriptFieldFactory<ShapeValues> toScriptFieldFactory, ShapeValues emptyValues) {
super(toScriptFieldFactory);
this.emptyValues = emptyValues;
}

@Override
public long ramBytesUsed() {
return 0;
}

@Override
public Collection<Accountable> getChildResources() {
return Collections.emptyList();
}

@Override
public void close() {}

@Override
public ShapeValues getShapeValues() {
return emptyValues;
}
}

/**
* Return geo-shape or shape values.
*/
public abstract ShapeValues getShapeValues();
public abstract T getShapeValues();

@Override
public final SortedBinaryDocValues getBytesValues() {
Expand All @@ -71,11 +40,13 @@ public final DocValuesScriptFieldFactory getScriptFieldFactory(String name) {
return toScriptFieldFactory.getScriptFieldFactory(getShapeValues(), name);
}

public static class ShapeScriptValues<T extends SpatialPoint> extends ScriptDocValues.BaseGeometry<T, ShapeValues.ShapeValue> {
public static class ShapeScriptValues<T extends SpatialPoint, V extends ShapeValues.ShapeValue> extends ScriptDocValues.BaseGeometry<
T,
V> {

private final GeometrySupplier<T, ShapeValues.ShapeValue> gsSupplier;
private final GeometrySupplier<T, V> gsSupplier;

protected ShapeScriptValues(GeometrySupplier<T, ShapeValues.ShapeValue> supplier) {
protected ShapeScriptValues(GeometrySupplier<T, V> supplier) {
super(supplier);
this.gsSupplier = supplier;
}
Expand All @@ -101,11 +72,11 @@ public T getLabelPosition() {
}

@Override
public ShapeValues.ShapeValue get(int index) {
public V get(int index) {
return gsSupplier.getInternal(0);
}

public ShapeValues.ShapeValue getValue() {
public V getValue() {
return gsSupplier.getInternal(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
*
* There is just one value for one document.
*/
public abstract class ShapeValues {
public abstract class ShapeValues<T extends ShapeValues.ShapeValue> {
protected final CoordinateEncoder encoder;
protected final Supplier<ShapeValue> supplier;
protected final Supplier<T> supplier;
protected final ShapeIndexer missingShapeIndexer;

/**
* Creates a new {@link ShapeValues} instance
*/
protected ShapeValues(CoordinateEncoder encoder, Supplier<ShapeValue> supplier, ShapeIndexer missingShapeIndexer) {
protected ShapeValues(CoordinateEncoder encoder, Supplier<T> supplier, ShapeIndexer missingShapeIndexer) {
this.encoder = encoder;
this.supplier = supplier;
this.missingShapeIndexer = missingShapeIndexer;
Expand All @@ -70,14 +70,14 @@ protected ShapeValues(CoordinateEncoder encoder, Supplier<ShapeValue> supplier,
*
* @return the value for the current docID set to {@link #advanceExact(int)}.
*/
public abstract ShapeValue value() throws IOException;
public abstract T value() throws IOException;

public ShapeValue missing(String missing) {
public T missing(String missing) {
try {
final Geometry geometry = WellKnownText.fromWKT(GeographyValidator.instance(true), true, missing);
final BinaryShapeDocValuesField field = new BinaryShapeDocValuesField("missing", encoder);
field.add(missingShapeIndexer.indexShape(geometry), geometry);
final ShapeValue value = supplier.get();
final T value = supplier.get();
value.reset(field.binaryValue());
return value;
} catch (IOException | ParseException e) {
Expand All @@ -87,7 +87,7 @@ public ShapeValue missing(String missing) {

/** thin wrapper around a {@link GeometryDocValueReader} which encodes / decodes values using
* the Geo decoder */
public abstract static class ShapeValue implements ToXContentFragment {
protected abstract static class ShapeValue implements ToXContentFragment {
protected final GeometryDocValueReader reader;
private final BoundingBox boundingBox;
private final Tile2DVisitor tile2DVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,73 @@

package org.elasticsearch.xpack.spatial.index.fielddata.plain;

import org.apache.lucene.util.Accountable;
import org.elasticsearch.common.geo.GeoBoundingBox;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.script.field.ToScriptFieldFactory;
import org.elasticsearch.xpack.spatial.index.fielddata.GeoShapeValues;
import org.elasticsearch.xpack.spatial.index.fielddata.LeafShapeFieldData;
import org.elasticsearch.xpack.spatial.index.fielddata.ShapeValues;

import java.util.Collection;
import java.util.Collections;

import static org.elasticsearch.common.geo.SphericalMercatorUtils.latToSphericalMercator;
import static org.elasticsearch.common.geo.SphericalMercatorUtils.lonToSphericalMercator;

public abstract class AbstractAtomicGeoShapeShapeFieldData extends LeafShapeFieldData {
public abstract class AbstractAtomicGeoShapeShapeFieldData extends LeafShapeFieldData<GeoShapeValues> {

private static class Empty extends AbstractAtomicGeoShapeShapeFieldData {
private final GeoShapeValues emptyValues;

Empty(ToScriptFieldFactory<GeoShapeValues> toScriptFieldFactory, GeoShapeValues emptyValues) {
super(toScriptFieldFactory);
this.emptyValues = emptyValues;
}

@Override
public long ramBytesUsed() {
return 0;
}

@Override
public Collection<Accountable> getChildResources() {
return Collections.emptyList();
}

@Override
public void close() {}

@Override
public GeoShapeValues getShapeValues() {
return emptyValues;
}
}

public AbstractAtomicGeoShapeShapeFieldData(ToScriptFieldFactory<ShapeValues> toScriptFieldFactory) {
public AbstractAtomicGeoShapeShapeFieldData(ToScriptFieldFactory<GeoShapeValues> toScriptFieldFactory) {
super(toScriptFieldFactory);
}

public static LeafShapeFieldData empty(final int maxDoc, ToScriptFieldFactory<ShapeValues> toScriptFieldFactory) {
return new LeafShapeFieldData.Empty<>(toScriptFieldFactory, GeoShapeValues.EMPTY);
public static AbstractAtomicGeoShapeShapeFieldData empty(final int maxDoc, ToScriptFieldFactory<GeoShapeValues> toScriptFieldFactory) {
return new Empty(toScriptFieldFactory, GeoShapeValues.EMPTY);
}

public static final class GeoShapeScriptValues extends LeafShapeFieldData.ShapeScriptValues<GeoPoint>
public static final class GeoShapeScriptValues extends LeafShapeFieldData.ShapeScriptValues<GeoPoint, GeoShapeValues.GeoShapeValue>
implements
ScriptDocValues.Geometry {

public GeoShapeScriptValues(GeometrySupplier<GeoPoint, ShapeValues.ShapeValue> supplier) {
public GeoShapeScriptValues(GeometrySupplier<GeoPoint, GeoShapeValues.GeoShapeValue> supplier) {
super(supplier);
}

@Override
public GeoShapeValues.GeoShapeValue get(int index) {
return (GeoShapeValues.GeoShapeValue) super.get(index);
return super.get(index);
}

@Override
public GeoShapeValues.GeoShapeValue getValue() {
return (GeoShapeValues.GeoShapeValue) super.getValue();
return super.getValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@
* `ValueSourceType` that this is constructed with. For example, the `LatLonShapeIndexFieldData.load(context)` method will
* create an instance of `LatLonShapeDVAtomicShapeFieldData`.
*/
public abstract class AbstractShapeIndexFieldData implements IndexShapeFieldData {
public abstract class AbstractShapeIndexFieldData<T extends ShapeValues<?>> implements IndexShapeFieldData<T> {
protected final String fieldName;
protected final ValuesSourceType valuesSourceType;
protected final ToScriptFieldFactory<ShapeValues> toScriptFieldFactory;
protected final ToScriptFieldFactory<T> toScriptFieldFactory;

AbstractShapeIndexFieldData(
String fieldName,
ValuesSourceType valuesSourceType,
ToScriptFieldFactory<ShapeValues> toScriptFieldFactory
) {
AbstractShapeIndexFieldData(String fieldName, ValuesSourceType valuesSourceType, ToScriptFieldFactory<T> toScriptFieldFactory) {
this.fieldName = fieldName;
this.valuesSourceType = valuesSourceType;
this.toScriptFieldFactory = toScriptFieldFactory;
Expand All @@ -55,7 +51,7 @@ public ValuesSourceType getValuesSourceType() {
}

@Override
public LeafShapeFieldData loadDirect(LeafReaderContext context) {
public LeafShapeFieldData<T> loadDirect(LeafReaderContext context) {
return load(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import org.elasticsearch.xpack.spatial.index.fielddata.GeoShapeValues;
import org.elasticsearch.xpack.spatial.index.fielddata.LeafShapeFieldData;
import org.elasticsearch.xpack.spatial.index.fielddata.ShapeValues;
import org.elasticsearch.xpack.spatial.search.aggregations.support.GeoShapeValuesSourceType;

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

final class LatLonShapeDVAtomicShapeFieldData extends LeafShapeFieldData {
final class LatLonShapeDVAtomicShapeFieldData extends LeafShapeFieldData<GeoShapeValues> {
private final LeafReader reader;
private final String fieldName;

LatLonShapeDVAtomicShapeFieldData(LeafReader reader, String fieldName, ToScriptFieldFactory<ShapeValues> toScriptFieldFactory) {
LatLonShapeDVAtomicShapeFieldData(LeafReader reader, String fieldName, ToScriptFieldFactory<GeoShapeValues> toScriptFieldFactory) {
super(toScriptFieldFactory);
this.reader = reader;
this.fieldName = fieldName;
Expand All @@ -48,7 +47,7 @@ public void close() {
}

@Override
public ShapeValues getShapeValues() {
public GeoShapeValues getShapeValues() {
try {
final BinaryDocValues binaryValues = DocValues.getBinary(reader, fieldName);
final GeoShapeValues.GeoShapeValue geoShapeValue = new GeoShapeValues.GeoShapeValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.script.field.ToScriptFieldFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import org.elasticsearch.xpack.spatial.index.fielddata.GeoShapeValues;
import org.elasticsearch.xpack.spatial.index.fielddata.LeafShapeFieldData;
import org.elasticsearch.xpack.spatial.index.fielddata.ShapeValues;

public class LatLonShapeIndexFieldData extends AbstractShapeIndexFieldData {
public class LatLonShapeIndexFieldData extends AbstractShapeIndexFieldData<GeoShapeValues> {
public LatLonShapeIndexFieldData(
String fieldName,
ValuesSourceType valuesSourceType,
ToScriptFieldFactory<ShapeValues> toScriptFieldFactory
ToScriptFieldFactory<GeoShapeValues> toScriptFieldFactory
) {
super(fieldName, valuesSourceType, toScriptFieldFactory);
}

@Override
public LeafShapeFieldData load(LeafReaderContext context) {
public LeafShapeFieldData<GeoShapeValues> load(LeafReaderContext context) {
LeafReader reader = context.reader();
FieldInfo info = reader.getFieldInfos().fieldInfo(fieldName);
if (info != null) {
Expand Down
Loading