Skip to content

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Smock <[email protected]>
  • Loading branch information
tsmock committed Oct 31, 2024
1 parent 87274ba commit 551e26e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import org.openstreetmap.josm.tools.Utils;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.json.JsonStructure;
import jakarta.json.JsonValue;
import jakarta.json.stream.JsonParser;
import jakarta.json.stream.JsonParsingException;

/**
Expand Down Expand Up @@ -44,12 +41,12 @@ public Optional<T> parse() throws IOException {
this.cachedFile = new CachedFile(this.source);
}
this.cachedFile.setFastFail(this.fastFail);
try (JsonReader reader = Json.createReader(cachedFile.setMaxAge(CachedFile.DAYS)
try (JsonParser reader = Json.createParser(cachedFile.setMaxAge(CachedFile.DAYS)
.setCachingStrategy(CachedFile.CachingStrategy.IfModifiedSince).getContentReader())) {
JsonStructure struct = reader.read();
if (JsonValue.ValueType.OBJECT == struct.getValueType()) {
final var jsonObject = struct.asJsonObject();
return Optional.ofNullable(this.parseJson(jsonObject));
while (reader.hasNext()) {
if (reader.hasNext() && reader.next() == JsonParser.Event.START_OBJECT) {
return Optional.ofNullable(this.parseJson(reader));
}
}
} catch (JsonParsingException jsonParsingException) {
Logging.error(jsonParsingException);
Expand All @@ -60,10 +57,10 @@ public Optional<T> parse() throws IOException {
/**
* Parses MapWithAI entry sources
*
* @param jsonObject The json of the data sources
* @param parser The json of the data sources. This will be in the {@link JsonParser.Event#START_OBJECT} state.
* @return The parsed entries
*/
public abstract T parseJson(JsonObject jsonObject);
public abstract T parseJson(JsonParser parser);

/**
* Sets whether opening HTTP connections should fail fast, i.e., whether a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAICategory;
import org.openstreetmap.josm.tools.Pair;

import jakarta.json.JsonObject;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import jakarta.json.stream.JsonParser;

/**
* Read conflation entries from JSON
Expand Down Expand Up @@ -44,12 +44,12 @@ public ConflationSourceReader(String source) {
/**
* Parses MapWithAI entry sources
*
* @param jsonObject The json of the data sources
* @param parser The json of the data sources
* @return The parsed entries
*/
@Override
public Map<MapWithAICategory, List<String>> parseJson(JsonObject jsonObject) {
return jsonObject.entrySet().stream().flatMap(i -> parse(i).stream())
public Map<MapWithAICategory, List<String>> parseJson(JsonParser parser) {
return parser.getObjectStream().flatMap(i -> parse(i).stream())
.collect(Collectors.groupingBy(p -> p.a, Collectors.mapping(p -> p.b, Collectors.toList())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private ForkJoinTask<MapWithAIInfo> parse(JsonObject feature) {
newInfo.setName(feature.getString("title", feature.getString("name")));
final var extent = feature.getJsonArray("extent").getValuesAs(JsonArray.class).stream()
.flatMap(array -> array.getValuesAs(JsonNumber.class).stream()).map(JsonNumber::doubleValue)
.map(Object::toString).toArray(String[]::new);
.map(d -> Double.toString(d)).toArray(String[]::new);
final var imageryBounds = new ImageryBounds(String.join(",", extent[1], extent[0], extent[3], extent[2]), ",");
newInfo.setBounds(imageryBounds);
newInfo.setSourceType(MapWithAIType.ESRI_FEATURE_SERVER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIType;
import org.openstreetmap.josm.tools.Territories;

import jakarta.json.JsonObject;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import jakarta.json.stream.JsonParser;

/**
* Reader to parse the list of available MapWithAI servers from an JSON
Expand Down Expand Up @@ -54,12 +54,12 @@ public MapWithAISourceReader(String source) {
/**
* Parses MapWithAI entry sources
*
* @param jsonObject The json of the data sources
* @param parser The json of the data sources
* @return The parsed entries
*/
@Override
public List<MapWithAIInfo> parseJson(JsonObject jsonObject) {
return jsonObject.entrySet().stream().map(MapWithAISourceReader::parse).collect(Collectors.toList());
public List<MapWithAIInfo> parseJson(JsonParser parser) {
return parser.getObjectStream().map(MapWithAISourceReader::parse).collect(Collectors.toList());
}

private static MapWithAIInfo parse(Map.Entry<String, JsonValue> entry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.imagery.ImageryInfo;
Expand All @@ -23,6 +25,7 @@
import jakarta.json.JsonObject;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import jakarta.json.stream.JsonParser;

/**
* Read data from overture sources
Expand All @@ -36,35 +39,38 @@ public OvertureSourceReader(MapWithAIInfo source) {
}

@Override
public List<MapWithAIInfo> parseJson(JsonObject jsonObject) {
public List<MapWithAIInfo> parseJson(JsonParser jsonParser) {
final var jsonObject = jsonParser.getObject();
if (jsonObject.containsKey("releases")) {
final var info = new ArrayList<MapWithAIInfo>(6 * 4);
final var releases = jsonObject.get("releases");
final var baseUri = URI.create(this.source.getUrl()).resolve("./"); // safe since we created an URI from the source to get to this point
if (releases instanceof JsonArray rArray) {
for (JsonValue value : rArray) {
if (value instanceof JsonObject release && release.containsKey("release_id")
&& release.containsKey("files")) {
final var id = release.get("release_id");
final var files = release.get("files");
if (id instanceof JsonString sId && files instanceof JsonArray fArray) {
final String releaseId = sId.getString();
for (JsonValue file : fArray) {
final var newInfo = parseFile(baseUri, releaseId, file);
if (newInfo != null) {
info.add(newInfo);
}
}
}
}
}
}
info.trimToSize();
return info;
return parseRoot(jsonObject);
}
return Collections.emptyList();
}

private List<MapWithAIInfo> parseRoot(JsonObject jsonObject) {
final var info = new ArrayList<MapWithAIInfo>(6 * 4);
final var releases = jsonObject.get("releases");
final var baseUri = URI.create(this.source.getUrl()).resolve("./"); // safe since we created an URI from the source to get to this point
if (releases instanceof JsonArray rArray) {
rArray.parallelStream().flatMap(value -> parseReleases(baseUri, value)).filter(Objects::nonNull)
.forEachOrdered(info::add);
}
info.trimToSize();
return info;
}

private Stream<MapWithAIInfo> parseReleases(URI baseUri, JsonValue value) {
if (value instanceof JsonObject release && release.containsKey("release_id") && release.containsKey("files")) {
final var id = release.get("release_id");
final var files = release.get("files");
if (id instanceof JsonString sId && files instanceof JsonArray fArray) {
final String releaseId = sId.getString();
return fArray.parallelStream().map(file -> parseFile(baseUri, releaseId, file));
}
}
return Stream.empty();
}

/**
* Parse the individual file from the files array
* @param baseUri The base URI (if the href is relative)
Expand Down

0 comments on commit 551e26e

Please sign in to comment.