Avoid using worker thread for getting sources -- this can block data downloads

Signed-off-by: Taylor Smock <tsmock@meta.com>
pull/43/head v819
Taylor Smock 2023-11-28 07:18:53 -07:00
rodzic e8c6e96217
commit 225abbb685
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 3D4E7B422350E843
11 zmienionych plików z 44 dodań i 40 usunięć

Wyświetl plik

@ -3,8 +3,6 @@ package org.openstreetmap.josm.plugins.mapwithai;
import static org.openstreetmap.josm.tools.I18n.tr; import static org.openstreetmap.josm.tools.I18n.tr;
import javax.swing.JMenuItem;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -12,6 +10,8 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.swing.JMenuItem;
import org.openstreetmap.josm.actions.JosmAction; import org.openstreetmap.josm.actions.JosmAction;
import org.openstreetmap.josm.actions.PreferencesAction; import org.openstreetmap.josm.actions.PreferencesAction;
import org.openstreetmap.josm.data.validation.OsmValidator; import org.openstreetmap.josm.data.validation.OsmValidator;
@ -29,6 +29,7 @@ import org.openstreetmap.josm.plugins.Plugin;
import org.openstreetmap.josm.plugins.PluginInformation; import org.openstreetmap.josm.plugins.PluginInformation;
import org.openstreetmap.josm.plugins.mapwithai.backend.DownloadListener; import org.openstreetmap.josm.plugins.mapwithai.backend.DownloadListener;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIAction; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIAction;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIMoveAction; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIMoveAction;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIObject; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIObject;
@ -135,7 +136,8 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
MainApplication.worker.execute(() -> UpdateProd.doProd(info.mainversion)); MainApplication.worker.execute(() -> UpdateProd.doProd(info.mainversion));
// Preload the MapWithAILayerInfo for the JOSM download window // Preload the MapWithAILayerInfo for the JOSM download window
// This reduces the amount of time taken for first button click by 100ms. // This reduces the amount of time taken for first button click by 100ms.
MainApplication.worker.execute(MapWithAILayerInfo::getInstance); // Don't use the worker thread to avoid blocking user downloads
MapWithAIDataUtils.getForkJoinPool().execute(MapWithAILayerInfo::getInstance);
destroyables.add(new MapWithAICopyProhibit()); destroyables.add(new MapWithAICopyProhibit());
} }

Wyświetl plik

@ -275,7 +275,7 @@ public class GetDataRunnable extends RecursiveTask<DataSet> {
} }
(boundsToUse.isCollapsed() || boundsToUse.isOutOfTheWorld() ? dataSet.getWays() (boundsToUse.isCollapsed() || boundsToUse.isOutOfTheWorld() ? dataSet.getWays()
: dataSet.searchWays(boundsToUse.toBBox())).stream().filter(way -> !way.isDeleted()) : dataSet.searchWays(boundsToUse.toBBox())).stream().filter(way -> !way.isDeleted())
.forEach(GetDataRunnable::cleanupArtifacts); .forEach(GetDataRunnable::cleanupArtifacts);
} }
/** /**

Wyświetl plik

@ -320,21 +320,21 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
private record OsmComparator( private record OsmComparator(
Collection<OsmPrimitive> previousSelection) implements Comparator<OsmPrimitive>, Serializable { Collection<OsmPrimitive> previousSelection) implements Comparator<OsmPrimitive>, Serializable {
@Override @Override
public int compare(OsmPrimitive o1, OsmPrimitive o2) { public int compare(OsmPrimitive o1, OsmPrimitive o2) {
if (previousSelection.contains(o1) == previousSelection.contains(o2)) { if (previousSelection.contains(o1) == previousSelection.contains(o2)) {
if (o1.isTagged() == o2.isTagged()) { if (o1.isTagged() == o2.isTagged()) {
return o1.compareTo(o2); return o1.compareTo(o2);
} else if (o1.isTagged()) { } else if (o1.isTagged()) {
return -1;
}
return 1;
}
if (previousSelection.contains(o1)) {
return -1; return -1;
} }
return 1; return 1;
} }
if (previousSelection.contains(o1)) {
return -1;
}
return 1;
}
} }

Wyświetl plik

@ -165,8 +165,8 @@ public class MergeDuplicateWays extends Command {
.collect(Collectors.toCollection(ArrayList::new)); .collect(Collectors.toCollection(ArrayList::new));
for (var i = 0; i < ways.size(); i++) { for (var i = 0; i < ways.size(); i++) {
final var way1 = ways.get(i); final var way1 = ways.get(i);
final var nearbyWays = dataSet.searchWays(way1.getBBox()).stream() final var nearbyWays = dataSet.searchWays(way1.getBBox()).stream().filter(MergeDuplicateWays::nonDeletedWay)
.filter(MergeDuplicateWays::nonDeletedWay).filter(w -> !Objects.equals(w, way1)).toList(); .filter(w -> !Objects.equals(w, way1)).toList();
for (final Way way2 : nearbyWays) { for (final Way way2 : nearbyWays) {
final var command = checkForDuplicateWays(way1, way2); final var command = checkForDuplicateWays(way1, way2);
final var deletedWays = new ArrayList<OsmPrimitive>(); final var deletedWays = new ArrayList<OsmPrimitive>();

Wyświetl plik

@ -28,12 +28,12 @@ import jakarta.annotation.Nonnull;
*/ */
public enum MapWithAICategory implements ISourceCategory<MapWithAICategory> { public enum MapWithAICategory implements ISourceCategory<MapWithAICategory> {
BUILDING("data/closedway", "buildings", marktr("Buildings")), BUILDING("data/closedway", "buildings", marktr("Buildings")), HIGHWAY("presets/transport/way/way_road", "highways",
HIGHWAY("presets/transport/way/way_road", "highways", marktr("Roads")), marktr("Roads")), ADDRESS("presets/misc/housenumber_small", "addresses",
ADDRESS("presets/misc/housenumber_small", "addresses", marktr("Addresses")), marktr("Addresses")), POWER("presets/power/pole", "pole", marktr("Power")), PRESET("dialogs/search",
POWER("presets/power/pole", "pole", marktr("Power")), PRESET("dialogs/search", "presets", marktr("Presets")), "presets", marktr("Presets")), FEATURED("presets/service/network-wireless", "featured",
FEATURED("presets/service/network-wireless", "featured", marktr("Featured")), marktr("Featured")), PREVIEW("presets/misc/fixme", "preview",
PREVIEW("presets/misc/fixme", "preview", marktr("Preview")), OTHER(null, "other", marktr("Other")); marktr("Preview")), OTHER(null, "other", marktr("Other"));
private static final Map<ImageSizes, Map<MapWithAICategory, ImageIcon>> iconCache = Collections private static final Map<ImageSizes, Map<MapWithAICategory, ImageIcon>> iconCache = Collections
.synchronizedMap(new EnumMap<>(ImageSizes.class)); .synchronizedMap(new EnumMap<>(ImageSizes.class));

Wyświetl plik

@ -283,7 +283,7 @@ public class MapWithAILayerInfo {
} }
// This is literally to avoid allocations on startup // This is literally to avoid allocations on startup
final Preferences preferences; final Preferences preferences;
if (Config.getPref() instanceof Preferences pref) { if (Config.getPref()instanceof Preferences pref) {
preferences = pref; preferences = pref;
} else { } else {
preferences = null; preferences = null;

Wyświetl plik

@ -11,8 +11,8 @@ import jakarta.annotation.Nonnull;
* @author Taylor Smock * @author Taylor Smock
*/ */
public enum MapWithAIType implements ISourceType<MapWithAIType> { public enum MapWithAIType implements ISourceType<MapWithAIType> {
FACEBOOK("facebook"), THIRD_PARTY("thirdParty"), ESRI("esri"), ESRI_FEATURE_SERVER("esriFeatureServer"), FACEBOOK("facebook"), THIRD_PARTY("thirdParty"), ESRI("esri"), ESRI_FEATURE_SERVER(
MAPBOX_VECTOR_TILE("mvt"), PMTILES("pmtiles"); "esriFeatureServer"), MAPBOX_VECTOR_TILE("mvt"), PMTILES("pmtiles");
private final String typeString; private final String typeString;

Wyświetl plik

@ -179,9 +179,11 @@ public class RoutingIslandsTest extends Test {
.filter(way -> !incomingWays.contains(way) || !outgoingWays.contains(way)) .filter(way -> !incomingWays.contains(way) || !outgoingWays.contains(way))
.filter(way -> Access.getPositiveAccessValues().contains( .filter(way -> Access.getPositiveAccessValues().contains(
getDefaultAccessTags(way).getOrDefault(currentTransportMode, Access.AccessTags.NO.getKey()))) getDefaultAccessTags(way).getOrDefault(currentTransportMode, Access.AccessTags.NO.getKey())))
.collect(Collectors.toSet())).stream() .collect(Collectors.toSet()))
.map(way -> new Pair<>((incomingWays.containsAll(way) ? marktr("outgoing") : marktr("incoming")), way)) .stream()
.toList(); .map(way -> new Pair<>(
(incomingWays.containsAll(way) ? marktr("outgoing") : marktr("incoming")), way))
.toList();
createErrors(problematic, currentTransportMode); createErrors(problematic, currentTransportMode);
} }
@ -328,8 +330,7 @@ public class RoutingIslandsTest extends Test {
var isAccessible = true; var isAccessible = true;
List<Relation> relations = from.getReferrers().stream().distinct().filter(Relation.class::isInstance) List<Relation> relations = from.getReferrers().stream().distinct().filter(Relation.class::isInstance)
.map(Relation.class::cast).filter(relation -> "restriction".equals(relation.get("type"))) .map(Relation.class::cast).filter(relation -> "restriction".equals(relation.get("type"))).toList();
.toList();
for (Relation relation : relations) { for (Relation relation : relations) {
if (((relation.hasKey("except") && relation.get("except").contains(currentTransportMode)) if (((relation.hasKey("except") && relation.get("except").contains(currentTransportMode))
|| (currentTransportMode == null || currentTransportMode.trim().isEmpty())) || (currentTransportMode == null || currentTransportMode.trim().isEmpty()))

Wyświetl plik

@ -503,7 +503,7 @@ public class MapWithAIProvidersPanel extends JPanel {
* @param e The MouseEvent (used to get the appropriate JTable) * @param e The MouseEvent (used to get the appropriate JTable)
*/ */
private static void clickListener(MouseEvent e) { private static void clickListener(MouseEvent e) {
if (e.getSource() instanceof JTable table && table.getSelectedRow() >= 0 && table.getSelectedColumn() >= 0) { if (e.getSource()instanceof JTable table && table.getSelectedRow() >= 0 && table.getSelectedColumn() >= 0) {
final int realCol = table.convertColumnIndexToModel(table.getSelectedColumn()); final int realCol = table.convertColumnIndexToModel(table.getSelectedColumn());
final int realRow = table.convertRowIndexToModel(table.getSelectedRow()); final int realRow = table.convertRowIndexToModel(table.getSelectedRow());
final var tableName = table.getModel().getColumnName(realCol); final var tableName = table.getModel().getColumnName(realCol);

Wyświetl plik

@ -119,10 +119,10 @@ public class ESRISourceReader {
/* Do nothing */ /* Do nothing */
if (parser.hasNext() && parser.next() == JsonParser.Event.START_OBJECT) { if (parser.hasNext() && parser.next() == JsonParser.Event.START_OBJECT) {
parser.getObjectStream().forEach(entry -> { parser.getObjectStream().forEach(entry -> {
if ("nextStart".equals(entry.getKey()) && entry.getValue() instanceof JsonNumber number) { if ("nextStart".equals(entry.getKey()) && entry.getValue()instanceof JsonNumber number) {
next.set(number.intValue()); next.set(number.intValue());
searchUrl.set(startReplace.matcher(search).replaceAll(Integer.toString(next.get()))); searchUrl.set(startReplace.matcher(search).replaceAll(Integer.toString(next.get())));
} else if ("results".equals(entry.getKey()) && entry.getValue() instanceof JsonArray features) { } else if ("results".equals(entry.getKey()) && entry.getValue()instanceof JsonArray features) {
for (var feature : features.getValuesAs(JsonObject.class)) { for (var feature : features.getValuesAs(JsonObject.class)) {
information.add(parse(feature)); information.add(parse(feature));
} }

Wyświetl plik

@ -60,12 +60,13 @@ public final class MapPaintUtils {
* Safe colors * Safe colors
*/ */
public enum SafeColors { public enum SafeColors {
RED(Color.RED), ORANGE(Color.ORANGE), GOLD(Objects.requireNonNull(ColorHelper.html2color("#ffd700"))), RED(Color.RED), ORANGE(Color.ORANGE), GOLD(Objects.requireNonNull(ColorHelper.html2color("#ffd700"))), LIME(
LIME(Objects.requireNonNull(ColorHelper.html2color("#00ff00"))), CYAN(Color.CYAN), Objects.requireNonNull(ColorHelper.html2color("#00ff00"))), CYAN(Color.CYAN), DODGER_BLUE(
DODGER_BLUE(Objects.requireNonNull(ColorHelper.html2color("#1e90ff"))), Objects.requireNonNull(ColorHelper.html2color("#1e90ff"))), AI_MAGENTA(
AI_MAGENTA(Objects.requireNonNull(ColorHelper.html2color("#ff26d4"))), PINK(Color.PINK), Objects.requireNonNull(ColorHelper.html2color("#ff26d4"))), PINK(
LIGHT_GREY(Objects.requireNonNull(ColorHelper.html2color("#d3d3d3"))), Color.PINK), LIGHT_GREY(
LINEN(Objects.requireNonNull(ColorHelper.html2color("#faf0e6"))); Objects.requireNonNull(ColorHelper.html2color("#d3d3d3"))), LINEN(
Objects.requireNonNull(ColorHelper.html2color("#faf0e6")));
private final Color color; private final Color color;