Skip to content

Commit 225abbb

Browse files
committed
Avoid using worker thread for getting sources -- this can block data downloads
Signed-off-by: Taylor Smock <[email protected]>
1 parent e8c6e96 commit 225abbb

File tree

11 files changed

+44
-40
lines changed

11 files changed

+44
-40
lines changed

src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
import static org.openstreetmap.josm.tools.I18n.tr;
55

6-
import javax.swing.JMenuItem;
7-
86
import java.lang.reflect.InvocationTargetException;
97
import java.util.ArrayList;
108
import java.util.Arrays;
119
import java.util.LinkedHashMap;
1210
import java.util.List;
1311
import java.util.Map;
1412

13+
import javax.swing.JMenuItem;
14+
1515
import org.openstreetmap.josm.actions.JosmAction;
1616
import org.openstreetmap.josm.actions.PreferencesAction;
1717
import org.openstreetmap.josm.data.validation.OsmValidator;
@@ -29,6 +29,7 @@
2929
import org.openstreetmap.josm.plugins.PluginInformation;
3030
import org.openstreetmap.josm.plugins.mapwithai.backend.DownloadListener;
3131
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIAction;
32+
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
3233
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer;
3334
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIMoveAction;
3435
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIObject;
@@ -135,7 +136,8 @@ public MapWithAIPlugin(PluginInformation info) {
135136
MainApplication.worker.execute(() -> UpdateProd.doProd(info.mainversion));
136137
// Preload the MapWithAILayerInfo for the JOSM download window
137138
// This reduces the amount of time taken for first button click by 100ms.
138-
MainApplication.worker.execute(MapWithAILayerInfo::getInstance);
139+
// Don't use the worker thread to avoid blocking user downloads
140+
MapWithAIDataUtils.getForkJoinPool().execute(MapWithAILayerInfo::getInstance);
139141

140142
destroyables.add(new MapWithAICopyProhibit());
141143
}

src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ private static void realCleanup(DataSet dataSet, Bounds bounds, MapWithAIInfo in
275275
}
276276
(boundsToUse.isCollapsed() || boundsToUse.isOutOfTheWorld() ? dataSet.getWays()
277277
: dataSet.searchWays(boundsToUse.toBBox())).stream().filter(way -> !way.isDeleted())
278-
.forEach(GetDataRunnable::cleanupArtifacts);
278+
.forEach(GetDataRunnable::cleanupArtifacts);
279279
}
280280

281281
/**

src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAILayer.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -320,21 +320,21 @@ public static void createBadDataNotification() {
320320
private record OsmComparator(
321321
Collection<OsmPrimitive> previousSelection) implements Comparator<OsmPrimitive>, Serializable {
322322

323-
@Override
324-
public int compare(OsmPrimitive o1, OsmPrimitive o2) {
325-
if (previousSelection.contains(o1) == previousSelection.contains(o2)) {
326-
if (o1.isTagged() == o2.isTagged()) {
327-
return o1.compareTo(o2);
328-
} else if (o1.isTagged()) {
329-
return -1;
330-
}
331-
return 1;
332-
}
333-
if (previousSelection.contains(o1)) {
323+
@Override
324+
public int compare(OsmPrimitive o1, OsmPrimitive o2) {
325+
if (previousSelection.contains(o1) == previousSelection.contains(o2)) {
326+
if (o1.isTagged() == o2.isTagged()) {
327+
return o1.compareTo(o2);
328+
} else if (o1.isTagged()) {
334329
return -1;
335330
}
336331
return 1;
337332
}
333+
if (previousSelection.contains(o1)) {
334+
return -1;
335+
}
336+
return 1;
337+
}
338338

339339
}
340340

src/main/java/org/openstreetmap/josm/plugins/mapwithai/commands/MergeDuplicateWays.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ public static void filterDataSet(@Nonnull DataSet dataSet, @Nonnull List<Command
165165
.collect(Collectors.toCollection(ArrayList::new));
166166
for (var i = 0; i < ways.size(); i++) {
167167
final var way1 = ways.get(i);
168-
final var nearbyWays = dataSet.searchWays(way1.getBBox()).stream()
169-
.filter(MergeDuplicateWays::nonDeletedWay).filter(w -> !Objects.equals(w, way1)).toList();
168+
final var nearbyWays = dataSet.searchWays(way1.getBBox()).stream().filter(MergeDuplicateWays::nonDeletedWay)
169+
.filter(w -> !Objects.equals(w, way1)).toList();
170170
for (final Way way2 : nearbyWays) {
171171
final var command = checkForDuplicateWays(way1, way2);
172172
final var deletedWays = new ArrayList<OsmPrimitive>();

src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAICategory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
*/
2929
public enum MapWithAICategory implements ISourceCategory<MapWithAICategory> {
3030

31-
BUILDING("data/closedway", "buildings", marktr("Buildings")),
32-
HIGHWAY("presets/transport/way/way_road", "highways", marktr("Roads")),
33-
ADDRESS("presets/misc/housenumber_small", "addresses", marktr("Addresses")),
34-
POWER("presets/power/pole", "pole", marktr("Power")), PRESET("dialogs/search", "presets", marktr("Presets")),
35-
FEATURED("presets/service/network-wireless", "featured", marktr("Featured")),
36-
PREVIEW("presets/misc/fixme", "preview", marktr("Preview")), OTHER(null, "other", marktr("Other"));
31+
BUILDING("data/closedway", "buildings", marktr("Buildings")), HIGHWAY("presets/transport/way/way_road", "highways",
32+
marktr("Roads")), ADDRESS("presets/misc/housenumber_small", "addresses",
33+
marktr("Addresses")), POWER("presets/power/pole", "pole", marktr("Power")), PRESET("dialogs/search",
34+
"presets", marktr("Presets")), FEATURED("presets/service/network-wireless", "featured",
35+
marktr("Featured")), PREVIEW("presets/misc/fixme", "preview",
36+
marktr("Preview")), OTHER(null, "other", marktr("Other"));
3737

3838
private static final Map<ImageSizes, Map<MapWithAICategory, ImageIcon>> iconCache = Collections
3939
.synchronizedMap(new EnumMap<>(ImageSizes.class));

src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAILayerInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public List<MapWithAIInfo> compute() {
283283
}
284284
// This is literally to avoid allocations on startup
285285
final Preferences preferences;
286-
if (Config.getPref() instanceof Preferences pref) {
286+
if (Config.getPref()instanceof Preferences pref) {
287287
preferences = pref;
288288
} else {
289289
preferences = null;

src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* @author Taylor Smock
1212
*/
1313
public enum MapWithAIType implements ISourceType<MapWithAIType> {
14-
FACEBOOK("facebook"), THIRD_PARTY("thirdParty"), ESRI("esri"), ESRI_FEATURE_SERVER("esriFeatureServer"),
15-
MAPBOX_VECTOR_TILE("mvt"), PMTILES("pmtiles");
14+
FACEBOOK("facebook"), THIRD_PARTY("thirdParty"), ESRI("esri"), ESRI_FEATURE_SERVER(
15+
"esriFeatureServer"), MAPBOX_VECTOR_TILE("mvt"), PMTILES("pmtiles");
1616

1717
private final String typeString;
1818

src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/validation/tests/RoutingIslandsTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,11 @@ private void runGenericTest(String currentTransportMode, Collection<Way> potenti
179179
.filter(way -> !incomingWays.contains(way) || !outgoingWays.contains(way))
180180
.filter(way -> Access.getPositiveAccessValues().contains(
181181
getDefaultAccessTags(way).getOrDefault(currentTransportMode, Access.AccessTags.NO.getKey())))
182-
.collect(Collectors.toSet())).stream()
183-
.map(way -> new Pair<>((incomingWays.containsAll(way) ? marktr("outgoing") : marktr("incoming")), way))
184-
.toList();
182+
.collect(Collectors.toSet()))
183+
.stream()
184+
.map(way -> new Pair<>(
185+
(incomingWays.containsAll(way) ? marktr("outgoing") : marktr("incoming")), way))
186+
.toList();
185187
createErrors(problematic, currentTransportMode);
186188
}
187189

@@ -328,8 +330,7 @@ public static boolean checkAccessibility(Way from, Way to, String currentTranspo
328330
var isAccessible = true;
329331

330332
List<Relation> relations = from.getReferrers().stream().distinct().filter(Relation.class::isInstance)
331-
.map(Relation.class::cast).filter(relation -> "restriction".equals(relation.get("type")))
332-
.toList();
333+
.map(Relation.class::cast).filter(relation -> "restriction".equals(relation.get("type"))).toList();
333334
for (Relation relation : relations) {
334335
if (((relation.hasKey("except") && relation.get("except").contains(currentTransportMode))
335336
|| (currentTransportMode == null || currentTransportMode.trim().isEmpty()))

src/main/java/org/openstreetmap/josm/plugins/mapwithai/gui/preferences/mapwithai/MapWithAIProvidersPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ public void mouseClicked(MouseEvent e) {
503503
* @param e The MouseEvent (used to get the appropriate JTable)
504504
*/
505505
private static void clickListener(MouseEvent e) {
506-
if (e.getSource() instanceof JTable table && table.getSelectedRow() >= 0 && table.getSelectedColumn() >= 0) {
506+
if (e.getSource()instanceof JTable table && table.getSelectedRow() >= 0 && table.getSelectedColumn() >= 0) {
507507
final int realCol = table.convertColumnIndexToModel(table.getSelectedColumn());
508508
final int realRow = table.convertRowIndexToModel(table.getSelectedRow());
509509
final var tableName = table.getModel().getColumnName(realCol);

src/main/java/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/ESRISourceReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ public List<ForkJoinTask<MapWithAIInfo>> parse() throws IOException {
119119
/* Do nothing */
120120
if (parser.hasNext() && parser.next() == JsonParser.Event.START_OBJECT) {
121121
parser.getObjectStream().forEach(entry -> {
122-
if ("nextStart".equals(entry.getKey()) && entry.getValue() instanceof JsonNumber number) {
122+
if ("nextStart".equals(entry.getKey()) && entry.getValue()instanceof JsonNumber number) {
123123
next.set(number.intValue());
124124
searchUrl.set(startReplace.matcher(search).replaceAll(Integer.toString(next.get())));
125-
} else if ("results".equals(entry.getKey()) && entry.getValue() instanceof JsonArray features) {
125+
} else if ("results".equals(entry.getKey()) && entry.getValue()instanceof JsonArray features) {
126126
for (var feature : features.getValuesAs(JsonObject.class)) {
127127
information.add(parse(feature));
128128
}

0 commit comments

Comments
 (0)