-
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.
Add visitor pattern to the triangle tree (#63333)
In order to add more functionality to this data structure, this commit adds a visitor pattern to the tree.
- Loading branch information
Showing
18 changed files
with
1,116 additions
and
893 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
108 changes: 108 additions & 0 deletions
108
...src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueReader.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,108 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.spatial.index.fielddata; | ||
|
||
import org.apache.lucene.store.ByteArrayDataInput; | ||
import org.apache.lucene.util.BytesRef; | ||
|
||
/** | ||
* A reusable Geometry doc value reader for a previous serialized {@link org.elasticsearch.geometry.Geometry} using | ||
* {@link GeometryDocValueWriter}. | ||
* | ||
* | ||
* ----------------------------------------- | ||
* | The binary format of the tree | | ||
* ----------------------------------------- | ||
* ----------------------------------------- -- | ||
* | centroid-x-coord (4 bytes) | | | ||
* ----------------------------------------- | | ||
* | centroid-y-coord (4 bytes) | | | ||
* ----------------------------------------- | | ||
* | DimensionalShapeType (1 byte) | | Centroid-related header | ||
* ----------------------------------------- | | ||
* | Sum of weights (VLong 1-8 bytes) | | | ||
* ----------------------------------------- -- | ||
* | Extent (var-encoding) | | ||
* ----------------------------------------- | ||
* | Triangle Tree | | ||
* ----------------------------------------- | ||
* ----------------------------------------- | ||
*/ | ||
public class GeometryDocValueReader { | ||
private final ByteArrayDataInput input; | ||
private final Extent extent; | ||
private int treeOffset; | ||
private int docValueOffset; | ||
|
||
public GeometryDocValueReader() { | ||
this.extent = new Extent(); | ||
this.input = new ByteArrayDataInput(); | ||
} | ||
|
||
/** | ||
* reset the geometry. | ||
*/ | ||
public void reset(BytesRef bytesRef) { | ||
this.input.reset(bytesRef.bytes, bytesRef.offset, bytesRef.length); | ||
docValueOffset = bytesRef.offset; | ||
treeOffset = 0; | ||
} | ||
|
||
/** | ||
* returns the {@link Extent} of this geometry. | ||
*/ | ||
protected Extent getExtent() { | ||
if (treeOffset == 0) { | ||
getSumCentroidWeight(); // skip CENTROID_HEADER + var-long sum-weight | ||
Extent.readFromCompressed(input, extent); | ||
treeOffset = input.getPosition(); | ||
} else { | ||
input.setPosition(treeOffset); | ||
} | ||
return extent; | ||
} | ||
|
||
/** | ||
* returns the encoded X coordinate of the centroid. | ||
*/ | ||
protected int getCentroidX() { | ||
input.setPosition(docValueOffset + 0); | ||
return input.readInt(); | ||
} | ||
|
||
/** | ||
* returns the encoded Y coordinate of the centroid. | ||
*/ | ||
protected int getCentroidY() { | ||
input.setPosition(docValueOffset + 4); | ||
return input.readInt(); | ||
} | ||
|
||
protected DimensionalShapeType getDimensionalShapeType() { | ||
input.setPosition(docValueOffset + 8); | ||
return DimensionalShapeType.readFrom(input); | ||
} | ||
|
||
protected double getSumCentroidWeight() { | ||
input.setPosition(docValueOffset + 9); | ||
return Double.longBitsToDouble(input.readVLong()); | ||
} | ||
|
||
/** | ||
* Visit the triangle tree with the provided visitor | ||
*/ | ||
protected void visit(TriangleTreeReader.Visitor visitor) { | ||
Extent extent = getExtent(); | ||
int thisMaxX = extent.maxX(); | ||
int thisMinX = extent.minX(); | ||
int thisMaxY = extent.maxY(); | ||
int thisMinY = extent.minY(); | ||
if(visitor.push(thisMinX, thisMinY, thisMaxX, thisMaxY)) { | ||
TriangleTreeReader.visit(input, visitor, thisMaxX, thisMaxY); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueWriter.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,40 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.spatial.index.fielddata; | ||
|
||
import org.apache.lucene.document.ShapeField; | ||
import org.apache.lucene.store.ByteBuffersDataOutput; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* This is a tree-writer that serializes a list of {@link ShapeField.DecodedTriangle} as an interval tree | ||
* into a byte array. | ||
*/ | ||
public class GeometryDocValueWriter { | ||
|
||
private final TriangleTreeWriter treeWriter; | ||
private final CoordinateEncoder coordinateEncoder; | ||
private final CentroidCalculator centroidCalculator; | ||
|
||
public GeometryDocValueWriter(List<ShapeField.DecodedTriangle> triangles, CoordinateEncoder coordinateEncoder, | ||
CentroidCalculator centroidCalculator) { | ||
this.coordinateEncoder = coordinateEncoder; | ||
this.centroidCalculator = centroidCalculator; | ||
this.treeWriter = new TriangleTreeWriter(triangles); | ||
} | ||
|
||
/*** Serialize the interval tree in the provided data output */ | ||
public void writeTo(ByteBuffersDataOutput out) throws IOException { | ||
out.writeInt(coordinateEncoder.encodeX(centroidCalculator.getX())); | ||
out.writeInt(coordinateEncoder.encodeY(centroidCalculator.getY())); | ||
centroidCalculator.getDimensionalShapeType().writeTo(out); | ||
out.writeVLong(Double.doubleToLongBits(centroidCalculator.sumWeight())); | ||
treeWriter.writeTo(out); | ||
} | ||
} |
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
Oops, something went wrong.