Skip to content

Commit d7cc19c

Browse files
authored
Make the visitor of the spatial triangle tree a public interface (#92349)
1 parent 1e1ba68 commit d7cc19c

File tree

10 files changed

+195
-189
lines changed

10 files changed

+195
-189
lines changed

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/Component2DRelationVisitor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* This class supports checking {@link Component2D} relations against a serialized triangle tree.
1818
* It does not support bounding boxes crossing the dateline.
1919
*/
20-
class Component2DRelationVisitor extends TriangleTreeReader.DecodedVisitor {
20+
class Component2DRelationVisitor extends TriangleTreeVisitor.TriangleTreeDecodedVisitor {
2121

2222
private GeoRelation relation;
2323
private Component2D component2D;
@@ -39,7 +39,7 @@ public GeoRelation relation() {
3939
}
4040

4141
@Override
42-
void visitDecodedPoint(double x, double y) {
42+
protected void visitDecodedPoint(double x, double y) {
4343
if (component2D.contains(x, y)) {
4444
if (canBeContained()) {
4545
relation = GeoRelation.QUERY_CONTAINS;
@@ -54,7 +54,7 @@ void visitDecodedPoint(double x, double y) {
5454
}
5555

5656
@Override
57-
void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
57+
protected void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
5858
if (component2D.intersectsLine(aX, aY, bX, bY)) {
5959
final boolean ab = (metadata & 1 << 4) == 1 << 4;
6060
if (canBeContained() && component2D.containsLine(aX, aY, bX, bY)) {
@@ -70,7 +70,7 @@ void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata)
7070
}
7171

7272
@Override
73-
void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
73+
protected void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
7474
if (component2D.intersectsTriangle(aX, aY, bX, bY, cX, cY)) {
7575
final boolean ab = (metadata & 1 << 4) == 1 << 4;
7676
final boolean bc = (metadata & 1 << 5) == 1 << 5;

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/Component2DVisitor.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
import org.apache.lucene.index.PointValues;
1313

1414
/**
15-
* A {@link TriangleTreeReader.Visitor} implementation for {@link Component2D} geometries.
15+
* A {@link TriangleTreeVisitor.TriangleTreeDecodedVisitor} implementation for {@link Component2D} geometries.
1616
* It can solve spatial relationships against a serialize triangle tree.
1717
*/
18-
public abstract class Component2DVisitor extends TriangleTreeReader.DecodedVisitor {
18+
public abstract class Component2DVisitor extends TriangleTreeVisitor.TriangleTreeDecodedVisitor {
1919

2020
protected final Component2D component2D;
2121

@@ -31,23 +31,23 @@ private Component2DVisitor(Component2D component2D, CoordinateEncoder encoder) {
3131
public abstract void reset();
3232

3333
@Override
34-
public boolean pushDecodedX(double minX) {
34+
protected boolean pushDecodedX(double minX) {
3535
return component2D.getMaxX() >= minX;
3636
}
3737

3838
@Override
39-
public boolean pushDecodedY(double minY) {
39+
protected boolean pushDecodedY(double minY) {
4040
return component2D.getMaxY() >= minY;
4141
}
4242

4343
@Override
44-
public boolean pushDecoded(double maxX, double maxY) {
44+
protected boolean pushDecoded(double maxX, double maxY) {
4545
return component2D.getMinX() <= maxX && component2D.getMinY() <= maxY;
4646

4747
}
4848

4949
@Override
50-
public boolean pushDecoded(double minX, double minY, double maxX, double maxY) {
50+
protected boolean pushDecoded(double minX, double minY, double maxX, double maxY) {
5151
return pushDecoded(component2D.relate(minX, maxX, minY, maxY));
5252
}
5353

@@ -56,7 +56,7 @@ public boolean pushDecoded(double minX, double minY, double maxX, double maxY) {
5656
*
5757
* @return if true, the visitor keeps traversing the tree, else it stops.
5858
* */
59-
abstract boolean pushDecoded(PointValues.Relation relation);
59+
protected abstract boolean pushDecoded(PointValues.Relation relation);
6060

6161
/**
6262
* Creates a visitor from the provided Component2D and spatial relationship. Visitors are re-usable by
@@ -95,17 +95,17 @@ public void reset() {
9595
}
9696

9797
@Override
98-
void visitDecodedPoint(double x, double y) {
98+
protected void visitDecodedPoint(double x, double y) {
9999
intersects = component2D.contains(x, y);
100100
}
101101

102102
@Override
103-
void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
103+
protected void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
104104
intersects = component2D.intersectsLine(aX, aY, bX, bY);
105105
}
106106

107107
@Override
108-
void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
108+
protected void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
109109
intersects = component2D.intersectsTriangle(aX, aY, bX, bY, cX, cY);
110110
}
111111

@@ -116,7 +116,7 @@ public boolean push() {
116116
}
117117

118118
@Override
119-
boolean pushDecoded(PointValues.Relation relation) {
119+
protected boolean pushDecoded(PointValues.Relation relation) {
120120
if (relation == PointValues.Relation.CELL_OUTSIDE_QUERY) {
121121
// shapes are disjoint, stop traversing the tree.
122122
return false;
@@ -156,17 +156,17 @@ public void reset() {
156156
}
157157

158158
@Override
159-
void visitDecodedPoint(double x, double y) {
159+
protected void visitDecodedPoint(double x, double y) {
160160
disjoint = component2D.contains(x, y) == false;
161161
}
162162

163163
@Override
164-
void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
164+
protected void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
165165
disjoint = component2D.intersectsLine(aX, aY, bX, bY) == false;
166166
}
167167

168168
@Override
169-
void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
169+
protected void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
170170
disjoint = component2D.intersectsTriangle(aX, aY, bX, bY, cX, cY) == false;
171171
}
172172

@@ -177,7 +177,7 @@ public boolean push() {
177177
}
178178

179179
@Override
180-
boolean pushDecoded(PointValues.Relation relation) {
180+
protected boolean pushDecoded(PointValues.Relation relation) {
181181
if (relation == PointValues.Relation.CELL_OUTSIDE_QUERY) {
182182
// shapes are disjoint, stop traversing the tree.
183183
return false;
@@ -217,17 +217,17 @@ public void reset() {
217217
}
218218

219219
@Override
220-
void visitDecodedPoint(double x, double y) {
220+
protected void visitDecodedPoint(double x, double y) {
221221
within = component2D.contains(x, y);
222222
}
223223

224224
@Override
225-
void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
225+
protected void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
226226
within = component2D.containsLine(aX, aY, bX, bY);
227227
}
228228

229229
@Override
230-
void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
230+
protected void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
231231
within = component2D.containsTriangle(aX, aY, bX, bY, cX, cY);
232232
}
233233

@@ -238,31 +238,31 @@ public boolean push() {
238238
}
239239

240240
@Override
241-
public boolean pushX(int minX) {
241+
protected boolean pushDecodedX(double minX) {
242242
// if any part of the tree is skipped, then the doc value is not within the shape,
243243
// stop traversing the tree
244-
within = super.pushX(minX);
244+
within = super.pushDecodedX(minX);
245245
return within;
246246
}
247247

248248
@Override
249-
public boolean pushY(int minY) {
249+
protected boolean pushDecodedY(double minY) {
250250
// if any part of the tree is skipped, then the doc value is not within the shape,
251251
// stop traversing the tree
252-
within = super.pushY(minY);
252+
within = super.pushDecodedY(minY);
253253
return within;
254254
}
255255

256256
@Override
257-
public boolean push(int maxX, int maxY) {
257+
protected boolean pushDecoded(double maxX, double maxY) {
258258
// if any part of the tree is skipped, then the doc value is not within the shape,
259259
// stop traversing the tree
260-
within = super.push(maxX, maxY);
260+
within = super.pushDecoded(maxX, maxY);
261261
return within;
262262
}
263263

264264
@Override
265-
boolean pushDecoded(PointValues.Relation relation) {
265+
protected boolean pushDecoded(PointValues.Relation relation) {
266266
if (relation == PointValues.Relation.CELL_OUTSIDE_QUERY) {
267267
// shapes are disjoint, stop traversing the tree.
268268
within = false;
@@ -297,7 +297,7 @@ public void reset() {
297297
}
298298

299299
@Override
300-
void visitDecodedPoint(double x, double y) {
300+
protected void visitDecodedPoint(double x, double y) {
301301
final Component2D.WithinRelation rel = component2D.withinPoint(x, y);
302302
if (rel != Component2D.WithinRelation.DISJOINT) {
303303
// Only override relationship if different to DISJOINT
@@ -306,7 +306,7 @@ void visitDecodedPoint(double x, double y) {
306306
}
307307

308308
@Override
309-
void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
309+
protected void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
310310
final boolean ab = (metadata & 1 << 4) == 1 << 4;
311311
final Component2D.WithinRelation rel = component2D.withinLine(aX, aY, ab, bX, bY);
312312
if (rel != Component2D.WithinRelation.DISJOINT) {
@@ -316,7 +316,7 @@ void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata)
316316
}
317317

318318
@Override
319-
void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
319+
protected void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
320320
final boolean ab = (metadata & 1 << 4) == 1 << 4;
321321
final boolean bc = (metadata & 1 << 5) == 1 << 5;
322322
final boolean ca = (metadata & 1 << 6) == 1 << 6;
@@ -334,7 +334,7 @@ public boolean push() {
334334
}
335335

336336
@Override
337-
boolean pushDecoded(PointValues.Relation relation) {
337+
protected boolean pushDecoded(PointValues.Relation relation) {
338338
// Only traverse the tree if the shapes intersects.
339339
return relation == PointValues.Relation.CELL_CROSSES_QUERY;
340340
}

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeoShapeValues.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected Component2D centroidAsComponent2D() throws IOException {
8080
*/
8181
public GeoRelation relate(int minX, int maxX, int minY, int maxY) throws IOException {
8282
tile2DVisitor.reset(minX, minY, maxX, maxY);
83-
reader.visit(tile2DVisitor);
83+
visit(tile2DVisitor);
8484
return tile2DVisitor.relation();
8585
}
8686

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected double getSumCentroidWeight() throws IOException {
9898
/**
9999
* Visit the triangle tree with the provided visitor
100100
*/
101-
public void visit(TriangleTreeReader.Visitor visitor) throws IOException {
101+
public void visit(TriangleTreeVisitor visitor) throws IOException {
102102
Extent geometryExtent = getExtent();
103103
int thisMaxX = geometryExtent.maxX();
104104
int thisMinX = geometryExtent.minX();

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/LabelPositionVisitor.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,41 @@
1717
*
1818
* TODO: We could instead choose the point closer to the centroid which improves unbalanced trees
1919
*/
20-
public class LabelPositionVisitor extends TriangleTreeReader.DecodedVisitor {
20+
class LabelPositionVisitor extends TriangleTreeVisitor.TriangleTreeDecodedVisitor {
2121

2222
private SpatialPoint labelPosition;
2323
private final BiFunction<Double, Double, SpatialPoint> pointMaker;
2424

25-
public LabelPositionVisitor(CoordinateEncoder encoder, BiFunction<Double, Double, SpatialPoint> pointMaker) {
25+
LabelPositionVisitor(CoordinateEncoder encoder, BiFunction<Double, Double, SpatialPoint> pointMaker) {
2626
super(encoder);
2727
this.pointMaker = pointMaker;
2828
}
2929

3030
@Override
31-
void visitDecodedPoint(double x, double y) {
31+
protected void visitDecodedPoint(double x, double y) {
3232
assert labelPosition == null;
3333
labelPosition = pointMaker.apply(x, y);
3434
}
3535

3636
@Override
37-
public void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
37+
protected void visitDecodedLine(double aX, double aY, double bX, double bY, byte metadata) {
3838
assert labelPosition == null;
3939
labelPosition = pointMaker.apply((aX + bX) / 2.0, (aY + bY) / 2.0);
4040
}
4141

4242
@Override
43-
public void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
43+
protected void visitDecodedTriangle(double aX, double aY, double bX, double bY, double cX, double cY, byte metadata) {
4444
assert labelPosition == null;
4545
labelPosition = pointMaker.apply((aX + bX + cX) / 3.0, (aY + bY + cY) / 3.0);
4646
}
4747

4848
@Override
49-
boolean pushDecodedX(double minX) {
49+
protected boolean pushDecodedX(double minX) {
5050
return labelPosition == null;
5151
}
5252

5353
@Override
54-
boolean pushDecodedY(double minX) {
54+
protected boolean pushDecodedY(double minX) {
5555
return labelPosition == null;
5656
}
5757

@@ -62,18 +62,12 @@ public boolean push() {
6262
}
6363

6464
@Override
65-
boolean pushDecoded(double maxX, double maxY) {
65+
protected boolean pushDecoded(double maxX, double maxY) {
6666
return labelPosition == null;
6767
}
6868

6969
@Override
70-
public boolean push(int minX, int minY, int maxX, int maxY) {
71-
// Always start the traversal
72-
return labelPosition == null;
73-
}
74-
75-
@Override
76-
boolean pushDecoded(double minX, double minY, double maxX, double maxY) {
70+
protected boolean pushDecoded(double minX, double minY, double maxX, double maxY) {
7771
return labelPosition == null;
7872
}
7973

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/ShapeValues.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ public T missing(String missing) {
9191
* the provided decoder (could be geo or cartesian)
9292
*/
9393
protected abstract static class ShapeValue implements ToXContentFragment {
94-
protected final GeometryDocValueReader reader;
94+
private final GeometryDocValueReader reader;
9595
private final BoundingBox boundingBox;
96-
protected final CoordinateEncoder encoder;
96+
private final CoordinateEncoder encoder;
9797
private final BiFunction<Double, Double, SpatialPoint> pointMaker;
9898
private final Component2DRelationVisitor component2DRelationVisitor;
9999

@@ -117,6 +117,13 @@ public BoundingBox boundingBox() {
117117
return boundingBox;
118118
}
119119

120+
/**
121+
* Visit the underlying tree structure using the provided {@link TriangleTreeVisitor}
122+
*/
123+
public void visit(TriangleTreeVisitor visitor) throws IOException {
124+
reader.visit(visitor);
125+
}
126+
120127
protected abstract Component2D centroidAsComponent2D() throws IOException;
121128

122129
private boolean centroidWithinShape() throws IOException {
@@ -139,7 +146,7 @@ public SpatialPoint labelPosition() throws IOException {
139146
}
140147
// For all other cases, use the first triangle (or line or point) in the tree which will always intersect the shape
141148
LabelPositionVisitor visitor = new LabelPositionVisitor(this.encoder, pointMaker);
142-
reader.visit(visitor);
149+
visit(visitor);
143150
return visitor.labelPosition();
144151
}
145152

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/Tile2DVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* This class supports checking bounding box relations against a serialized triangle tree.
1717
*
1818
*/
19-
class Tile2DVisitor implements TriangleTreeReader.Visitor {
19+
class Tile2DVisitor implements TriangleTreeVisitor {
2020

2121
private GeoRelation relation;
2222
private int minX;

0 commit comments

Comments
 (0)