Skip to content

Commit d6d8924

Browse files
committed
make GeometryTree and EdgeTree writers implement Writeable (#42910)
1 parent 4883029 commit d6d8924

File tree

6 files changed

+52
-42
lines changed

6 files changed

+52
-42
lines changed

server/src/main/java/org/elasticsearch/common/geo/EdgeTreeReader.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.lucene.util.BytesRef;
2222
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
23+
import org.elasticsearch.common.io.stream.StreamInput;
2324

2425
import java.io.IOException;
2526
import java.nio.ByteBuffer;
@@ -29,8 +30,9 @@
2930
public class EdgeTreeReader {
3031
final BytesRef bytesRef;
3132

32-
public EdgeTreeReader(BytesRef bytesRef) {
33-
this.bytesRef = bytesRef;
33+
public EdgeTreeReader(StreamInput input) throws IOException {
34+
int treeBytesSize = input.readVInt();
35+
this.bytesRef = input.readBytesRef(treeBytesSize);
3436
}
3537

3638
/**

server/src/main/java/org/elasticsearch/common/geo/EdgeTreeWriter.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818
*/
1919
package org.elasticsearch.common.geo;
2020

21-
import org.apache.lucene.util.BytesRef;
22-
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2321
import org.elasticsearch.common.io.stream.StreamOutput;
22+
import org.elasticsearch.common.io.stream.Writeable;
2423

2524
import java.io.IOException;
2625
import java.util.Arrays;
2726

2827
/**
2928
* Shape edge-tree writer for use in doc-values
3029
*/
31-
public class EdgeTreeWriter {
30+
public class EdgeTreeWriter implements Writeable {
3231

3332
/**
3433
* | minY | maxY | x1 | y1 | x2 | y2 | right_offset |
@@ -68,17 +67,14 @@ public EdgeTreeWriter(int[] x, int[] y) {
6867
this.tree = createTree(edges, 0, edges.length - 1);
6968
}
7069

71-
public BytesRef toBytesRef() throws IOException {
72-
BytesStreamOutput output = new BytesStreamOutput(4 * 4 + EDGE_SIZE_IN_BYTES * tree.size);
73-
// write extent of edges
74-
output.writeInt(minX);
75-
output.writeInt(minY);
76-
output.writeInt(maxX);
77-
output.writeInt(maxY);
78-
// write edge-tree itself
79-
writeTree(tree, output);
80-
output.close();
81-
return output.bytes().toBytesRef();
70+
@Override
71+
public void writeTo(StreamOutput out) throws IOException {
72+
out.writeVInt(4 * 4 + EDGE_SIZE_IN_BYTES * tree.size);
73+
out.writeInt(minX);
74+
out.writeInt(minY);
75+
out.writeInt(maxX);
76+
out.writeInt(maxY);
77+
writeTree(tree, out);
8278
}
8379

8480
private void writeTree(Edge edge, StreamOutput output) throws IOException {

server/src/main/java/org/elasticsearch/common/geo/GeometryTreeReader.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public boolean containedInOrCrosses(int minLon, int minLat, int maxLon, int maxL
6262
for (int i = 0; i < numTrees; i++) {
6363
ShapeType shapeType = input.readEnum(ShapeType.class);
6464
if (ShapeType.POLYGON.equals(shapeType)) {
65-
BytesRef treeRef = input.readBytesRef();
66-
EdgeTreeReader reader = new EdgeTreeReader(treeRef);
65+
EdgeTreeReader reader = new EdgeTreeReader(input);
6766
if (reader.containedInOrCrosses(minLon, minLat, maxLon, maxLat)) {
6867
return true;
6968
}

server/src/main/java/org/elasticsearch/common/geo/GeometryTreeWriter.java

+13-15
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919
package org.elasticsearch.common.geo;
2020

21-
import org.apache.lucene.util.BytesRef;
22-
import org.elasticsearch.common.io.stream.BytesStreamOutput;
21+
import org.elasticsearch.common.io.stream.StreamOutput;
22+
import org.elasticsearch.common.io.stream.Writeable;
2323
import org.elasticsearch.geo.geometry.Circle;
2424
import org.elasticsearch.geo.geometry.Geometry;
2525
import org.elasticsearch.geo.geometry.GeometryCollection;
@@ -43,7 +43,7 @@
4343
* appropriate tree structure for each type of
4444
* {@link Geometry} into a byte array.
4545
*/
46-
public class GeometryTreeWriter {
46+
public class GeometryTreeWriter implements Writeable {
4747

4848
private final GeometryTreeBuilder builder;
4949

@@ -52,25 +52,23 @@ public class GeometryTreeWriter {
5252
geometry.visit(builder);
5353
}
5454

55-
public BytesRef toBytesRef() throws IOException {
56-
BytesStreamOutput output = new BytesStreamOutput();
55+
@Override
56+
public void writeTo(StreamOutput out) throws IOException {
5757
// only write a geometry extent for the tree if the tree
5858
// contains multiple sub-shapes
5959
boolean prependExtent = builder.shapeWriters.size() > 1;
60-
output.writeBoolean(prependExtent);
60+
out.writeBoolean(prependExtent);
6161
if (prependExtent) {
62-
output.writeInt(builder.minLon);
63-
output.writeInt(builder.minLat);
64-
output.writeInt(builder.maxLon);
65-
output.writeInt(builder.maxLat);
62+
out.writeInt(builder.minLon);
63+
out.writeInt(builder.minLat);
64+
out.writeInt(builder.maxLon);
65+
out.writeInt(builder.maxLat);
6666
}
67-
output.writeVInt(builder.shapeWriters.size());
67+
out.writeVInt(builder.shapeWriters.size());
6868
for (EdgeTreeWriter writer : builder.shapeWriters) {
69-
output.writeEnum(ShapeType.POLYGON);
70-
output.writeBytesRef(writer.toBytesRef());
69+
out.writeEnum(ShapeType.POLYGON);
70+
writer.writeTo(out);
7171
}
72-
output.close();
73-
return output.bytes().toBytesRef();
7472
}
7573

7674
class GeometryTreeBuilder implements GeometryVisitor<Void, RuntimeException> {

server/src/test/java/org/elasticsearch/common/geo/EdgeTreeTests.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
*/
1919
package org.elasticsearch.common.geo;
2020

21-
import org.apache.lucene.util.BytesRef;
2221
import org.elasticsearch.common.geo.builders.ShapeBuilder;
22+
import org.elasticsearch.common.io.stream.BytesStreamOutput;
23+
import org.elasticsearch.common.io.stream.StreamInput;
2324
import org.elasticsearch.geo.geometry.Polygon;
2425
import org.elasticsearch.test.ESTestCase;
2526
import org.elasticsearch.test.geo.RandomShapeGenerator;
@@ -38,8 +39,10 @@ public void testRectangleShape() throws IOException {
3839
int[] x = new int[]{minX, maxX, maxX, minX, minX};
3940
int[] y = new int[]{minY, minY, maxY, maxY, minY};
4041
EdgeTreeWriter writer = new EdgeTreeWriter(x, y);
41-
BytesRef bytes = writer.toBytesRef();
42-
EdgeTreeReader reader = new EdgeTreeReader(bytes);
42+
BytesStreamOutput output = new BytesStreamOutput();
43+
writer.writeTo(output);
44+
output.close();
45+
EdgeTreeReader reader = new EdgeTreeReader(StreamInput.wrap(output.bytes().toBytesRef().bytes));
4346

4447
// box-query touches bottom-left corner
4548
assertTrue(reader.containedInOrCrosses(minX - randomIntBetween(1, 180), minY - randomIntBetween(1, 180), minX, minY));
@@ -93,7 +96,10 @@ public void testSimplePolygon() throws IOException {
9396
int[] y = asIntArray(geo.getPolygon().getLats());
9497

9598
EdgeTreeWriter writer = new EdgeTreeWriter(x, y);
96-
EdgeTreeReader reader = new EdgeTreeReader(writer.toBytesRef());
99+
BytesStreamOutput output = new BytesStreamOutput();
100+
writer.writeTo(output);
101+
output.close();
102+
EdgeTreeReader reader = new EdgeTreeReader(StreamInput.wrap(output.bytes().toBytesRef().bytes));
97103
// polygon fully contained within box
98104
assertTrue(reader.containedInOrCrosses(minXBox, minYBox, maxXBox, maxYBox));
99105
// containedInOrCrosses
@@ -121,7 +127,10 @@ public void testPacMan() throws Exception {
121127

122128
// test cell crossing poly
123129
EdgeTreeWriter writer = new EdgeTreeWriter(px, py);
124-
EdgeTreeReader reader = new EdgeTreeReader(writer.toBytesRef());
130+
BytesStreamOutput output = new BytesStreamOutput();
131+
writer.writeTo(output);
132+
output.close();
133+
EdgeTreeReader reader = new EdgeTreeReader(StreamInput.wrap(output.bytes().toBytesRef().bytes));
125134
assertTrue(reader.containsBottomLeft(xMin, yMin, xMax, yMax));
126135
}
127136

server/src/test/java/org/elasticsearch/common/geo/GeometryTreeTests.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package org.elasticsearch.common.geo;
2020

21-
import org.apache.lucene.util.BytesRef;
21+
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2222
import org.elasticsearch.geo.geometry.LinearRing;
2323
import org.elasticsearch.geo.geometry.Polygon;
2424
import org.elasticsearch.test.ESTestCase;
@@ -37,8 +37,11 @@ public void testRectangleShape() throws IOException {
3737
double[] x = new double[]{minX, maxX, maxX, minX, minX};
3838
double[] y = new double[]{minY, minY, maxY, maxY, minY};
3939
GeometryTreeWriter writer = new GeometryTreeWriter(new Polygon(new LinearRing(y, x), Collections.emptyList()));
40-
BytesRef bytes = writer.toBytesRef();
41-
GeometryTreeReader reader = new GeometryTreeReader(bytes);
40+
41+
BytesStreamOutput output = new BytesStreamOutput();
42+
writer.writeTo(output);
43+
output.close();
44+
GeometryTreeReader reader = new GeometryTreeReader(output.bytes().toBytesRef());
4245

4346
// box-query touches bottom-left corner
4447
assertTrue(reader.containedInOrCrosses(minX - randomIntBetween(1, 180), minY - randomIntBetween(1, 180), minX, minY));
@@ -91,7 +94,10 @@ public void testPacMan() throws Exception {
9194

9295
// test cell crossing poly
9396
GeometryTreeWriter writer = new GeometryTreeWriter(new Polygon(new LinearRing(py, px), Collections.emptyList()));
94-
GeometryTreeReader reader = new GeometryTreeReader(writer.toBytesRef());
97+
BytesStreamOutput output = new BytesStreamOutput();
98+
writer.writeTo(output);
99+
output.close();
100+
GeometryTreeReader reader = new GeometryTreeReader(output.bytes().toBytesRef());
95101
assertTrue(reader.containedInOrCrosses(xMin, yMin, xMax, yMax));
96102
}
97103
}

0 commit comments

Comments
 (0)