Skip to content

Commit

Permalink
Switch to iterative version of WKT format parser
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <heemin@amazon.com>
  • Loading branch information
heemin32 committed Jun 10, 2024
1 parent fbe048f commit e6fc01c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Pass parent filter to inner hit query ([#13903](https://github.com/opensearch-project/OpenSearch/pull/13903))
- Fix NPE on restore searchable snapshot ([#13911](https://github.com/opensearch-project/OpenSearch/pull/13911))
- Fix double invocation of postCollection when MultiBucketCollector is present ([#14015](https://github.com/opensearch-project/OpenSearch/pull/14015))
- Switch to iterative version of WKT format parser ([#14086](https://github.com/opensearch-project/OpenSearch/pull/14086))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Stack;

/**
* Utility class for converting to and from WKT
Expand Down Expand Up @@ -278,6 +279,16 @@ public Geometry fromWKT(String wkt) throws IOException, ParseException {
*/
private Geometry parseGeometry(StreamTokenizer stream) throws IOException, ParseException {
final String type = nextWord(stream).toLowerCase(Locale.ROOT);
switch (type) {
case "geometrycollection":
return parseGeometryCollection(stream);
default:
return parseSimpleGeometry(stream, type);
}
}

private Geometry parseSimpleGeometry(StreamTokenizer stream, String type) throws IOException, ParseException {
assert "geometrycollection".equals(type) == false;
switch (type) {
case "point":
return parsePoint(stream);
Expand All @@ -294,23 +305,80 @@ private Geometry parseGeometry(StreamTokenizer stream) throws IOException, Parse
case "bbox":
return parseBBox(stream);
case "geometrycollection":
return parseGeometryCollection(stream);
throw new IllegalStateException("Unexpected type: geometrycollection");

Check warning on line 308 in libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java

View check run for this annotation

Codecov / codecov/patch

libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java#L308

Added line #L308 was not covered by tests
case "circle": // Not part of the standard, but we need it for internal serialization
return parseCircle(stream);
}
throw new IllegalArgumentException("Unknown geometry type: " + type);
}

/**
* Iterative version of
* <!--
* ```java
* private GeometryCollection<Geometry> parseGeometryCollectionA(StreamTokenizer stream) throws IOException, ParseException {
* if (nextEmptyOrOpen(stream).equals(EMPTY)) {
* return GeometryCollection.EMPTY;
* }
* List<Geometry> shapes = new ArrayList<>();
* shapes.add(parseGeometry(stream));
* while (nextCloserOrComma(stream).equals(COMMA)) {
* shapes.add(parseGeometry(stream));
* }
* return new GeometryCollection<>(shapes);
* }
* -->
* to avoid StackOverflowError when there is a deeply nested structure of GeometryCollection.
*/
private GeometryCollection<Geometry> parseGeometryCollection(StreamTokenizer stream) throws IOException, ParseException {
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
return GeometryCollection.EMPTY;
}
List<Geometry> shapes = new ArrayList<>();
shapes.add(parseGeometry(stream));
while (nextCloserOrComma(stream).equals(COMMA)) {
shapes.add(parseGeometry(stream));

List<Geometry> topLevelShapes = new ArrayList<>();
Stack<List<Geometry>> stack = new Stack<>();
stack.push(topLevelShapes);
boolean isFirstIteration = true;
List<Geometry> currentLevelShapes = null;
while (!stack.isEmpty()) {
List<Geometry> previousShapes = stack.pop();
if (currentLevelShapes != null) {
previousShapes.add(new GeometryCollection<>(currentLevelShapes));
}
currentLevelShapes = previousShapes;

if (isFirstIteration == true) {
isFirstIteration = false;
} else {
if (!nextCloserOrComma(stream).equals(COMMA)) {
// Done with current level, continue with parent level
continue;
}
}
while (true) {
final String type = nextWord(stream).toLowerCase(Locale.ROOT);
switch (type) {
case "geometrycollection":
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
currentLevelShapes.add(GeometryCollection.EMPTY);
break;

Check warning on line 364 in libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java

View check run for this annotation

Codecov / codecov/patch

libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java#L363-L364

Added lines #L363 - L364 were not covered by tests
} else {
stack.push(currentLevelShapes);
currentLevelShapes = new ArrayList<>();
continue;
}
default:
currentLevelShapes.add(parseSimpleGeometry(stream, type));
break;
}

if (!nextCloserOrComma(stream).equals(COMMA)) {
break;
}
}
}
return new GeometryCollection<>(shapes);

return new GeometryCollection<>(topLevelShapes);
}

private Point parsePoint(StreamTokenizer stream) throws IOException, ParseException {
Expand Down

0 comments on commit e6fc01c

Please sign in to comment.