-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support serialization of GeoShapeValue and CartesianShapeValue (#99299)
* Support serialization of GeoShapeValue and CartesianShapeValue * Improve test assert message * Code review suggestions * Use BytesReference in order to benefit from future plans for improved serialization * Removed copyBytes by moving the writing closer to the actual byte[] * Fixed unused import * Simplfy read/write BytesReference by maintaining passed in BytesRef * Added unit tests for serialization/deserialization * Fixed license header, reduced duplicate code, reduced flaky tests
- Loading branch information
1 parent
db53786
commit 2a90ff4
Showing
11 changed files
with
258 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
48 changes: 48 additions & 0 deletions
48
...lasticsearch/xpack/spatial/index/fielddata/CartesianShapeValuesGenericWriteableTests.java
This file contains 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,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.spatial.index.fielddata; | ||
|
||
import org.elasticsearch.geometry.Rectangle; | ||
import org.elasticsearch.xpack.spatial.util.GeoTestUtils; | ||
|
||
import java.io.IOException; | ||
|
||
public class CartesianShapeValuesGenericWriteableTests extends ShapeValuesGenericWriteableTests<CartesianShapeValues.CartesianShapeValue> { | ||
|
||
@Override | ||
protected String shapeValueName() { | ||
return "CartesianShapeValue"; | ||
} | ||
|
||
@Override | ||
protected GenericWriteableWrapper createTestInstance() { | ||
try { | ||
double minX = randomDoubleBetween(-Float.MAX_VALUE, 0, false); | ||
double minY = randomDoubleBetween(-Float.MAX_VALUE, 0, false); | ||
double maxX = randomDoubleBetween(minX + 10, Float.MAX_VALUE, false); | ||
double maxY = randomDoubleBetween(minY + 10, Float.MAX_VALUE, false); | ||
Rectangle rectangle = new Rectangle(minX, maxX, maxY, minY); | ||
CartesianShapeValues.CartesianShapeValue shapeValue = GeoTestUtils.cartesianShapeValue(rectangle); | ||
return new GenericWriteableWrapper(shapeValue); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
protected GenericWriteableWrapper mutateInstance(GenericWriteableWrapper instance) throws IOException { | ||
ShapeValues.ShapeValue shapeValue = instance.shapeValue(); | ||
ShapeValues.BoundingBox bbox = shapeValue.boundingBox(); | ||
double height = bbox.maxY() - bbox.minY(); | ||
double width = bbox.maxX() - bbox.minX(); | ||
double xs = width * 0.001; | ||
double ys = height * 0.001; | ||
Rectangle rectangle = new Rectangle(bbox.minX() + xs, bbox.maxX() - xs, bbox.maxY() - ys, bbox.minY() + ys); | ||
return new GenericWriteableWrapper(GeoTestUtils.cartesianShapeValue(rectangle)); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../org/elasticsearch/xpack/spatial/index/fielddata/GeoShapeValuesGenericWriteableTests.java
This file contains 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,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.spatial.index.fielddata; | ||
|
||
import org.elasticsearch.common.geo.GeoBoundingBox; | ||
import org.elasticsearch.geometry.Rectangle; | ||
import org.elasticsearch.xpack.spatial.util.GeoTestUtils; | ||
|
||
import java.io.IOException; | ||
|
||
public class GeoShapeValuesGenericWriteableTests extends ShapeValuesGenericWriteableTests<GeoShapeValues.GeoShapeValue> { | ||
|
||
@Override | ||
protected String shapeValueName() { | ||
return "GeoShapeValue"; | ||
} | ||
|
||
@Override | ||
protected GenericWriteableWrapper createTestInstance() { | ||
try { | ||
GeoBoundingBox bbox = GeoTestUtils.randomBBox(); | ||
Rectangle rectangle = new Rectangle(bbox.left(), bbox.right(), bbox.top(), bbox.bottom()); | ||
GeoShapeValues.GeoShapeValue shapeValue = GeoTestUtils.geoShapeValue(rectangle); | ||
return new GenericWriteableWrapper(shapeValue); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
protected GenericWriteableWrapper mutateInstance(GenericWriteableWrapper instance) throws IOException { | ||
ShapeValues.ShapeValue shapeValue = instance.shapeValue(); | ||
ShapeValues.BoundingBox bbox = shapeValue.boundingBox(); | ||
double height = bbox.maxY() - bbox.minY(); | ||
double width = bbox.maxX() - bbox.minX(); | ||
double xs = width * 0.001; | ||
double ys = height * 0.001; | ||
Rectangle rectangle = new Rectangle(bbox.minX() + xs, bbox.maxX() - xs, bbox.maxY() - ys, bbox.minY() + ys); | ||
return new GenericWriteableWrapper(GeoTestUtils.geoShapeValue(rectangle)); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...ava/org/elasticsearch/xpack/spatial/index/fielddata/ShapeValuesGenericWriteableTests.java
This file contains 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,81 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.spatial.index.fielddata; | ||
|
||
import org.elasticsearch.TransportVersion; | ||
import org.elasticsearch.TransportVersions; | ||
import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
import org.elasticsearch.common.io.stream.GenericNamedWriteable; | ||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.test.AbstractWireTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
|
||
public abstract class ShapeValuesGenericWriteableTests<T extends ShapeValues.ShapeValue> extends AbstractWireTestCase< | ||
ShapeValuesGenericWriteableTests.GenericWriteableWrapper> { | ||
|
||
/** | ||
* Wrapper around a GeoShapeValue to verify that it round-trips via {@code writeGenericValue} and {@code readGenericValue} | ||
*/ | ||
public record GenericWriteableWrapper(ShapeValues.ShapeValue shapeValue) implements Writeable { | ||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeGenericValue(shapeValue); | ||
} | ||
|
||
public static GenericWriteableWrapper readFrom(StreamInput in) throws IOException { | ||
return new GenericWriteableWrapper((ShapeValues.ShapeValue) in.readGenericValue()); | ||
} | ||
} | ||
|
||
private static final NamedWriteableRegistry NAMED_WRITEABLE_REGISTRY = new NamedWriteableRegistry( | ||
List.of( | ||
new NamedWriteableRegistry.Entry( | ||
GenericNamedWriteable.class, | ||
GeoShapeValues.GeoShapeValue.class.getSimpleName(), | ||
GeoShapeValues.GeoShapeValue::new | ||
), | ||
new NamedWriteableRegistry.Entry( | ||
GenericNamedWriteable.class, | ||
CartesianShapeValues.CartesianShapeValue.class.getSimpleName(), | ||
CartesianShapeValues.CartesianShapeValue::new | ||
) | ||
) | ||
); | ||
|
||
@Override | ||
protected NamedWriteableRegistry writableRegistry() { | ||
return NAMED_WRITEABLE_REGISTRY; | ||
} | ||
|
||
@Override | ||
protected GenericWriteableWrapper copyInstance(GenericWriteableWrapper instance, TransportVersion version) throws IOException { | ||
return copyInstance(instance, writableRegistry(), StreamOutput::writeWriteable, GenericWriteableWrapper::readFrom, version); | ||
} | ||
|
||
protected abstract String shapeValueName(); | ||
|
||
public void testSerializationFailsWithOlderVersion() { | ||
TransportVersion older = TransportVersions.KNN_AS_QUERY_ADDED; | ||
assert older.before(TransportVersions.SHAPE_VALUE_SERIALIZATION_ADDED); | ||
final var testInstance = createTestInstance().shapeValue(); | ||
try (var output = new BytesStreamOutput()) { | ||
output.setTransportVersion(older); | ||
assertThat( | ||
expectThrows(Throwable.class, () -> output.writeGenericValue(testInstance)).getMessage(), | ||
containsString("[" + shapeValueName() + "] requires minimal transport version") | ||
); | ||
} | ||
} | ||
} |
This file contains 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