Skip to content

Commit

Permalink
Remove ParsedPoint interface from point field mappers (#73710)
Browse files Browse the repository at this point in the history
  • Loading branch information
iverase authored Jun 4, 2021
1 parent 30cf4dc commit 094f193
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ private void fetchFromSource(Object sourceMap, Consumer<Object> consumer, String
}
}

public abstract static class AbstractGeometryFieldType extends MappedFieldType {
public abstract static class AbstractGeometryFieldType<T> extends MappedFieldType {

protected final Parser<?> geometryParser;
protected final Parser<T> geometryParser;
protected final boolean parsesArrayValue;

protected AbstractGeometryFieldType(String name, boolean indexed, boolean stored, boolean hasDocValues,
boolean parsesArrayValue, Parser<?> geometryParser, Map<String, String> meta) {
boolean parsesArrayValue, Parser<T> geometryParser, Map<String, String> meta) {
super(name, indexed, stored, hasDocValues, TextSearchInfo.NONE, meta);
this.parsesArrayValue = parsesArrayValue;
this.geometryParser = geometryParser;
Expand Down Expand Up @@ -116,13 +116,13 @@ protected Object parseSourceValue(Object value) {

private final Explicit<Boolean> ignoreMalformed;
private final Explicit<Boolean> ignoreZValue;
private final Parser<? extends T> parser;
private final Parser<T> parser;

protected AbstractGeometryFieldMapper(String simpleName, MappedFieldType mappedFieldType,
Map<String, NamedAnalyzer> indexAnalyzers,
Explicit<Boolean> ignoreMalformed, Explicit<Boolean> ignoreZValue,
MultiFields multiFields, CopyTo copyTo,
Parser<? extends T> parser) {
Parser<T> parser) {
super(simpleName, mappedFieldType, indexAnalyzers, multiFields, copyTo, false, null);
this.ignoreMalformed = ignoreMalformed;
this.ignoreZValue = ignoreZValue;
Expand All @@ -132,7 +132,7 @@ protected AbstractGeometryFieldMapper(String simpleName, MappedFieldType mappedF
protected AbstractGeometryFieldMapper(String simpleName, MappedFieldType mappedFieldType,
Explicit<Boolean> ignoreMalformed, Explicit<Boolean> ignoreZValue,
MultiFields multiFields, CopyTo copyTo,
Parser<? extends T> parser) {
Parser<T> parser) {
this(simpleName, mappedFieldType, Collections.emptyMap(), ignoreMalformed, ignoreZValue, multiFields, copyTo, parser);
}

Expand All @@ -141,7 +141,7 @@ protected AbstractGeometryFieldMapper(
MappedFieldType mappedFieldType,
MultiFields multiFields,
CopyTo copyTo,
Parser<? extends T> parser,
Parser<T> parser,
String onScriptError
) {
super(simpleName, mappedFieldType, Collections.emptyMap(), multiFields, copyTo, true, onScriptError);
Expand All @@ -151,8 +151,8 @@ protected AbstractGeometryFieldMapper(
}

@Override
public AbstractGeometryFieldType fieldType() {
return (AbstractGeometryFieldType) mappedFieldType;
public AbstractGeometryFieldType<T> fieldType() {
return (AbstractGeometryFieldType<T>) mappedFieldType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.TriFunction;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeometryFormat;
import org.elasticsearch.common.geo.GeometryParser;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.geometry.Geometry;
import org.elasticsearch.geometry.Point;
import org.elasticsearch.index.mapper.Mapper.TypeParser.ParserContext;

import java.io.IOException;
Expand All @@ -28,25 +23,25 @@
/** Base class for for spatial fields that only support indexing points */
public abstract class AbstractPointGeometryFieldMapper<T> extends AbstractGeometryFieldMapper<T> {

public static Parameter<ParsedPoint> nullValueParam(Function<FieldMapper, ParsedPoint> initializer,
TriFunction<String, ParserContext, Object, ParsedPoint> parser,
Supplier<ParsedPoint> def) {
return new Parameter<>("null_value", false, def, parser, initializer);
public static <T> Parameter<T> nullValueParam(Function<FieldMapper, T> initializer,
TriFunction<String, ParserContext, Object, T> parser,
Supplier<T> def) {
return new Parameter<T>("null_value", false, def, parser, initializer);
}

protected final ParsedPoint nullValue;
protected final T nullValue;

protected AbstractPointGeometryFieldMapper(String simpleName, MappedFieldType mappedFieldType,
MultiFields multiFields, Explicit<Boolean> ignoreMalformed,
Explicit<Boolean> ignoreZValue, ParsedPoint nullValue, CopyTo copyTo,
Parser<? extends T> parser) {
Explicit<Boolean> ignoreZValue, T nullValue, CopyTo copyTo,
Parser<T> parser) {
super(simpleName, mappedFieldType, ignoreMalformed, ignoreZValue, multiFields, copyTo, parser);
this.nullValue = nullValue;
}

protected AbstractPointGeometryFieldMapper(String simpleName, MappedFieldType mappedFieldType,
MultiFields multiFields, CopyTo copyTo,
Parser<? extends T> parser, String onScriptError) {
Parser<T> parser, String onScriptError) {
super(simpleName, mappedFieldType, multiFields, copyTo, parser, onScriptError);
this.nullValue = null;
}
Expand All @@ -56,80 +51,62 @@ public final boolean parsesArrayValue() {
return true;
}

public ParsedPoint getNullValue() {
public T getNullValue() {
return nullValue;
}

/** represents a Point that has been parsed by {@link PointParser} */
public interface ParsedPoint {
void validate(String fieldName);
void normalize(String fieldName);
void resetCoords(double x, double y);
Point asGeometry();
default boolean isNormalizable(double coord) {
return Double.isNaN(coord) == false && Double.isInfinite(coord) == false;
}
}

/** A parser implementation that can parse the various point formats */
public static class PointParser<P extends ParsedPoint> extends Parser<P> {
/**
* Note that this parser is only used for formatting values.
*/
private final GeometryParser geometryParser;
private final String field;
private final Supplier<P> pointSupplier;
private final CheckedBiFunction<XContentParser, P, P, IOException> objectParser;
private final P nullValue;
/** A base parser implementation for point formats */
protected abstract static class PointParser<T> extends Parser<T> {
protected final String field;
private final Supplier<T> pointSupplier;
private final CheckedBiFunction<XContentParser, T, T, IOException> objectParser;
private final T nullValue;
private final boolean ignoreZValue;
private final boolean ignoreMalformed;

public PointParser(String field,
Supplier<P> pointSupplier,
CheckedBiFunction<XContentParser, P, P, IOException> objectParser,
P nullValue,
boolean ignoreZValue,
boolean ignoreMalformed) {
protected final boolean ignoreMalformed;

protected PointParser(String field,
Supplier<T> pointSupplier,
CheckedBiFunction<XContentParser, T, T, IOException> objectParser,
T nullValue,
boolean ignoreZValue,
boolean ignoreMalformed) {
this.field = field;
this.pointSupplier = pointSupplier;
this.objectParser = objectParser;
this.nullValue = nullValue == null ? null : process(nullValue);
this.nullValue = nullValue == null ? null : validate(nullValue);
this.ignoreZValue = ignoreZValue;
this.ignoreMalformed = ignoreMalformed;
this.geometryParser = new GeometryParser(true, true, true);
}

private P process(P in) {
if (ignoreMalformed == false) {
in.validate(field);
} else {
in.normalize(field);
}
return in;
}
protected abstract T validate(T in);

protected abstract void reset(T in, double x, double y);

@Override
public void parse(
XContentParser parser,
CheckedConsumer<P, IOException> consumer,
CheckedConsumer<T, IOException> consumer,
Consumer<Exception> onMalformed
) throws IOException {
if (parser.currentToken() == XContentParser.Token.START_ARRAY) {
XContentParser.Token token = parser.nextToken();
P point = pointSupplier.get();
T point = pointSupplier.get();
if (token == XContentParser.Token.VALUE_NUMBER) {
double x = parser.doubleValue();
parser.nextToken();
double y = parser.doubleValue();
token = parser.nextToken();
if (token == XContentParser.Token.VALUE_NUMBER) {
GeoPoint.assertZValue(ignoreZValue, parser.doubleValue());
if (ignoreZValue == false) {
throw new ElasticsearchParseException("Exception parsing coordinates: found Z value [{}] but [ignore_z_value] "
+ "parameter is [{}]", parser.doubleValue(), ignoreZValue);
}
} else if (token != XContentParser.Token.END_ARRAY) {
throw new ElasticsearchParseException("field type does not accept > 3 dimensions");
}

point.resetCoords(x, y);
consumer.accept(process(point));
reset(point, x, y);
consumer.accept(validate(point));
} else {
while (token != XContentParser.Token.END_ARRAY) {
parseAndConsumeFromObject(parser, point, consumer, onMalformed);
Expand All @@ -148,22 +125,16 @@ public void parse(

private void parseAndConsumeFromObject(
XContentParser parser,
P point,
CheckedConsumer<P, IOException> consumer,
T point,
CheckedConsumer<T, IOException> consumer,
Consumer<Exception> onMalformed
) {
try {
point = objectParser.apply(parser, point);
consumer.accept(process(point));
consumer.accept(validate(point));
} catch (Exception e) {
onMalformed.accept(e);
}
}

@Override
public Object format(P point, String format) {
GeometryFormat<Geometry> geometryFormat = geometryParser.geometryFormat(format);
return geometryFormat.toXContentAsObject(point.asGeometry());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public static Parameter<Explicit<Orientation>> orientationParam(Function<FieldMa
.setSerializer((b, f, v) -> b.field(f, v.value()), v -> v.value().toString());
}

public abstract static class AbstractShapeGeometryFieldType extends AbstractGeometryFieldType {
public abstract static class AbstractShapeGeometryFieldType<T> extends AbstractGeometryFieldType<T> {

private final Orientation orientation;

protected AbstractShapeGeometryFieldType(String name, boolean isSearchable, boolean isStored, boolean hasDocValues,
boolean parsesArrayValue, Parser<?> parser,
boolean parsesArrayValue, Parser<T> parser,
Orientation orientation, Map<String, String> meta) {
super(name, isSearchable, isStored, hasDocValues, parsesArrayValue, parser, meta);
this.orientation = orientation;
Expand Down
Loading

0 comments on commit 094f193

Please sign in to comment.