001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005import static org.openstreetmap.josm.tools.I18n.trn;
006
007import java.io.IOException;
008import java.io.InputStream;
009import java.net.HttpURLConnection;
010import java.util.ArrayList;
011import java.util.Collection;
012import java.util.Collections;
013import java.util.HashSet;
014import java.util.Iterator;
015import java.util.LinkedHashSet;
016import java.util.List;
017import java.util.Set;
018import java.util.concurrent.Callable;
019import java.util.concurrent.CompletionService;
020import java.util.concurrent.ExecutionException;
021import java.util.concurrent.ExecutorCompletionService;
022import java.util.concurrent.ExecutorService;
023import java.util.concurrent.Executors;
024import java.util.concurrent.Future;
025
026import org.openstreetmap.josm.Main;
027import org.openstreetmap.josm.data.osm.DataSet;
028import org.openstreetmap.josm.data.osm.DataSetMerger;
029import org.openstreetmap.josm.data.osm.Node;
030import org.openstreetmap.josm.data.osm.OsmPrimitive;
031import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
032import org.openstreetmap.josm.data.osm.PrimitiveId;
033import org.openstreetmap.josm.data.osm.Relation;
034import org.openstreetmap.josm.data.osm.RelationMember;
035import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
036import org.openstreetmap.josm.data.osm.Way;
037import org.openstreetmap.josm.gui.preferences.server.OverpassServerPreference;
038import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
039import org.openstreetmap.josm.gui.progress.ProgressMonitor;
040import org.openstreetmap.josm.tools.Utils;
041
042/**
043 * Retrieves a set of {@link OsmPrimitive}s from an OSM server using the so called
044 * Multi Fetch API.
045 *
046 * Usage:
047 * <pre>
048 *    MultiFetchServerObjectReader reader = MultiFetchServerObjectReader()
049 *         .append(2345,2334,4444)
050 *         .append(new Node(72343));
051 *    reader.parseOsm();
052 *    if (!reader.getMissingPrimitives().isEmpty()) {
053 *        Main.info("There are missing primitives: " + reader.getMissingPrimitives());
054 *    }
055 *    if (!reader.getSkippedWays().isEmpty()) {
056 *       Main.info("There are skipped ways: " + reader.getMissingPrimitives());
057 *    }
058 * </pre>
059 */
060public class MultiFetchServerObjectReader extends OsmServerReader {
061    /**
062     * the max. number of primitives retrieved in one step. Assuming IDs with 7 digits,
063     * this leads to a max. request URL of ~ 1600 Bytes ((7 digits +  1 Separator) * 200),
064     * which should be safe according to the
065     * <a href="http://www.boutell.com/newfaq/misc/urllength.html">WWW FAQ</a>.
066     */
067    private static final int MAX_IDS_PER_REQUEST = 200;
068
069    private final Set<Long> nodes;
070    private final Set<Long> ways;
071    private final Set<Long> relations;
072    private Set<PrimitiveId> missingPrimitives;
073    private final DataSet outputDataSet;
074
075    /**
076     * Constructs a {@code MultiFetchServerObjectReader}.
077     */
078    protected MultiFetchServerObjectReader() {
079        nodes = new LinkedHashSet<>();
080        ways = new LinkedHashSet<>();
081        relations = new LinkedHashSet<>();
082        this.outputDataSet = new DataSet();
083        this.missingPrimitives = new LinkedHashSet<>();
084    }
085
086    /**
087     * Creates a new instance of {@link MultiFetchServerObjectReader} or {@link MultiFetchOverpassObjectReader}
088     * depending on the {@link OverpassServerPreference#useForMultiFetch preference}.
089     *
090     * @return a new instance
091     * @since 9241
092     */
093    public static MultiFetchServerObjectReader create() {
094        return create(OverpassServerPreference.useForMultiFetch());
095    }
096
097    /**
098     * Creates a new instance of {@link MultiFetchServerObjectReader} or {@link MultiFetchOverpassObjectReader}
099     * depending on the {@code fromMirror} parameter.
100     *
101     * @param fromMirror {@code false} for {@link MultiFetchServerObjectReader}, {@code true} for {@link MultiFetchOverpassObjectReader}
102     * @return a new instance
103     * @since 9241
104     */
105    static MultiFetchServerObjectReader create(final boolean fromMirror) {
106        if (fromMirror) {
107            return new MultiFetchOverpassObjectReader();
108        } else {
109            return new MultiFetchServerObjectReader();
110        }
111    }
112
113    /**
114     * Remembers an {@link OsmPrimitive}'s id. The id will
115     * later be fetched as part of a Multi Get request.
116     *
117     * Ignore the id if it represents a new primitives.
118     *
119     * @param id  the id
120     */
121    protected void remember(PrimitiveId id) {
122        if (id.isNew()) return;
123        switch(id.getType()) {
124        case NODE: nodes.add(id.getUniqueId()); break;
125        case WAY: ways.add(id.getUniqueId()); break;
126        case RELATION: relations.add(id.getUniqueId()); break;
127        default: throw new AssertionError();
128        }
129    }
130
131    /**
132     * appends a {@link OsmPrimitive} id to the list of ids which will be fetched from the server.
133     *
134     * @param ds the {@link DataSet} to which the primitive belongs
135     * @param id the primitive id
136     * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
137     * {@link OsmPrimitiveType#RELATION RELATION}
138     * @return this
139     */
140    public MultiFetchServerObjectReader append(DataSet ds, long id, OsmPrimitiveType type) {
141        OsmPrimitive p = ds.getPrimitiveById(id, type);
142        switch(type) {
143        case NODE:
144            return appendNode((Node) p);
145        case WAY:
146            return appendWay((Way) p);
147        case RELATION:
148            return appendRelation((Relation) p);
149        default:
150            return this;
151        }
152    }
153
154    /**
155     * appends a {@link Node} id to the list of ids which will be fetched from the server.
156     *
157     * @param node  the node (ignored, if null)
158     * @return this
159     */
160    public MultiFetchServerObjectReader appendNode(Node node) {
161        if (node == null) return this;
162        remember(node.getPrimitiveId());
163        return this;
164    }
165
166    /**
167     * appends a {@link Way} id and the list of ids of nodes the way refers to the list of ids which will be fetched from the server.
168     *
169     * @param way the way (ignored, if null)
170     * @return this
171     */
172    public MultiFetchServerObjectReader appendWay(Way way) {
173        if (way == null) return this;
174        if (way.isNew()) return this;
175        for (Node node: !recursesDown() ? way.getNodes() : Collections.<Node>emptyList()) {
176            if (!node.isNew()) {
177                remember(node.getPrimitiveId());
178            }
179        }
180        remember(way.getPrimitiveId());
181        return this;
182    }
183
184    /**
185     * appends a {@link Relation} id to the list of ids which will be fetched from the server.
186     *
187     * @param relation  the relation (ignored, if null)
188     * @return this
189     */
190    protected MultiFetchServerObjectReader appendRelation(Relation relation) {
191        if (relation == null) return this;
192        if (relation.isNew()) return this;
193        remember(relation.getPrimitiveId());
194        for (RelationMember member : !recursesDown() ? relation.getMembers() : Collections.<RelationMember>emptyList()) {
195            if (OsmPrimitiveType.from(member.getMember()).equals(OsmPrimitiveType.RELATION)) {
196                // avoid infinite recursion in case of cyclic dependencies in relations
197                //
198                if (relations.contains(member.getMember().getId())) {
199                    continue;
200                }
201            }
202            if (!member.getMember().isIncomplete()) {
203                append(member.getMember());
204            }
205        }
206        return this;
207    }
208
209    /**
210     * appends an {@link OsmPrimitive} to the list of ids which will be fetched from the server.
211     * @param primitive the primitive
212     * @return this
213     */
214    public MultiFetchServerObjectReader append(OsmPrimitive primitive) {
215        if (primitive != null) {
216            switch (OsmPrimitiveType.from(primitive)) {
217                case NODE: return appendNode((Node) primitive);
218                case WAY: return appendWay((Way) primitive);
219                case RELATION: return appendRelation((Relation) primitive);
220                default: break;
221            }
222        }
223        return this;
224    }
225
226    /**
227     * appends a list of {@link OsmPrimitive} to the list of ids which will be fetched from the server.
228     *
229     * @param primitives  the list of primitives (ignored, if null)
230     * @return this
231     *
232     * @see #append(OsmPrimitive)
233     */
234    public MultiFetchServerObjectReader append(Collection<? extends OsmPrimitive> primitives) {
235        if (primitives == null) return this;
236        for (OsmPrimitive primitive : primitives) {
237            append(primitive);
238        }
239        return this;
240    }
241
242    /**
243     * extracts a subset of max {@link #MAX_IDS_PER_REQUEST} ids from <code>ids</code> and
244     * replies the subset. The extracted subset is removed from <code>ids</code>.
245     *
246     * @param ids a set of ids
247     * @return the subset of ids
248     */
249    protected Set<Long> extractIdPackage(Set<Long> ids) {
250        Set<Long> pkg = new HashSet<>();
251        if (ids.isEmpty())
252            return pkg;
253        if (ids.size() > MAX_IDS_PER_REQUEST) {
254            Iterator<Long> it = ids.iterator();
255            for (int i = 0; i < MAX_IDS_PER_REQUEST; i++) {
256                pkg.add(it.next());
257            }
258            ids.removeAll(pkg);
259        } else {
260            pkg.addAll(ids);
261            ids.clear();
262        }
263        return pkg;
264    }
265
266    /**
267     * builds the Multi Get request string for a set of ids and a given {@link OsmPrimitiveType}.
268     *
269     * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
270     * {@link OsmPrimitiveType#RELATION RELATION}
271     * @param idPackage  the package of ids
272     * @return the request string
273     */
274    protected String buildRequestString(final OsmPrimitiveType type, Set<Long> idPackage) {
275        return type.getAPIName() + "s?" + type.getAPIName() + "s=" + Utils.join(",", idPackage);
276    }
277
278    protected void rememberNodesOfIncompleteWaysToLoad(DataSet from) {
279        for (Way w: from.getWays()) {
280            if (w.hasIncompleteNodes()) {
281                for (Node n: w.getNodes()) {
282                    if (n.isIncomplete()) {
283                        nodes.add(n.getId());
284                    }
285                }
286            }
287        }
288    }
289
290    /**
291     * merges the dataset <code>from</code> to {@link #outputDataSet}.
292     *
293     * @param from the other dataset
294     */
295    protected void merge(DataSet from) {
296        final DataSetMerger visitor = new DataSetMerger(outputDataSet, from);
297        visitor.merge();
298    }
299
300    /**
301     * fetches a set of ids of a given {@link OsmPrimitiveType} from the server
302     *
303     * @param ids the set of ids
304     * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
305     * {@link OsmPrimitiveType#RELATION RELATION}
306     * @param progressMonitor progress monitor
307     * @throws OsmTransferException if an error occurs while communicating with the API server
308     */
309    protected void fetchPrimitives(Set<Long> ids, OsmPrimitiveType type, ProgressMonitor progressMonitor) throws OsmTransferException {
310        String msg;
311        final String baseUrl = getBaseUrl();
312        switch (type) {
313            // CHECKSTYLE.OFF: SingleSpaceSeparator
314            case NODE:     msg = tr("Fetching a package of nodes from ''{0}''",     baseUrl); break;
315            case WAY:      msg = tr("Fetching a package of ways from ''{0}''",      baseUrl); break;
316            case RELATION: msg = tr("Fetching a package of relations from ''{0}''", baseUrl); break;
317            // CHECKSTYLE.ON: SingleSpaceSeparator
318            default: throw new AssertionError();
319        }
320        progressMonitor.setTicksCount(ids.size());
321        progressMonitor.setTicks(0);
322        // The complete set containing all primitives to fetch
323        Set<Long> toFetch = new HashSet<>(ids);
324        // Build a list of fetchers that will  download smaller sets containing only MAX_IDS_PER_REQUEST (200) primitives each.
325        // we will run up to MAX_DOWNLOAD_THREADS concurrent fetchers.
326        int threadsNumber = Main.pref.getInteger("osm.download.threads", OsmApi.MAX_DOWNLOAD_THREADS);
327        threadsNumber = Math.min(Math.max(threadsNumber, 1), OsmApi.MAX_DOWNLOAD_THREADS);
328        final ExecutorService exec = Executors.newFixedThreadPool(
329                threadsNumber, Utils.newThreadFactory(getClass() + "-%d", Thread.NORM_PRIORITY));
330        CompletionService<FetchResult> ecs = new ExecutorCompletionService<>(exec);
331        List<Future<FetchResult>> jobs = new ArrayList<>();
332        while (!toFetch.isEmpty()) {
333            jobs.add(ecs.submit(new Fetcher(type, extractIdPackage(toFetch), progressMonitor)));
334        }
335        // Run the fetchers
336        for (int i = 0; i < jobs.size() && !isCanceled(); i++) {
337            progressMonitor.subTask(msg + "... " + progressMonitor.getTicks() + '/' + progressMonitor.getTicksCount());
338            try {
339                FetchResult result = ecs.take().get();
340                if (result.missingPrimitives != null) {
341                    missingPrimitives.addAll(result.missingPrimitives);
342                }
343                if (result.dataSet != null && !isCanceled()) {
344                    rememberNodesOfIncompleteWaysToLoad(result.dataSet);
345                    merge(result.dataSet);
346                }
347            } catch (InterruptedException | ExecutionException e) {
348                Main.error(e);
349            }
350        }
351        exec.shutdown();
352        // Cancel requests if the user chose to
353        if (isCanceled()) {
354            for (Future<FetchResult> job : jobs) {
355                job.cancel(true);
356            }
357        }
358    }
359
360    /**
361     * invokes one or more Multi Gets to fetch the {@link OsmPrimitive}s and replies
362     * the dataset of retrieved primitives. Note that the dataset includes non visible primitives too!
363     * In contrast to a simple Get for a node, a way, or a relation, a Multi Get always replies
364     * the latest version of the primitive (if any), even if the primitive is not visible (i.e. if
365     * visible==false).
366     *
367     * Invoke {@link #getMissingPrimitives()} to get a list of primitives which have not been
368     * found on  the server (the server response code was 404)
369     *
370     * @return the parsed data
371     * @throws OsmTransferException if an error occurs while communicating with the API server
372     * @see #getMissingPrimitives()
373     *
374     */
375    @Override
376    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
377        int n = nodes.size() + ways.size() + relations.size();
378        progressMonitor.beginTask(trn("Downloading {0} object from ''{1}''",
379                "Downloading {0} objects from ''{1}''", n, n, OsmApi.getOsmApi().getBaseUrl()));
380        try {
381            missingPrimitives = new HashSet<>();
382            if (isCanceled()) return null;
383            fetchPrimitives(ways, OsmPrimitiveType.WAY, progressMonitor);
384            if (isCanceled()) return null;
385            fetchPrimitives(nodes, OsmPrimitiveType.NODE, progressMonitor);
386            if (isCanceled()) return null;
387            fetchPrimitives(relations, OsmPrimitiveType.RELATION, progressMonitor);
388            if (outputDataSet != null) {
389                outputDataSet.deleteInvisible();
390            }
391            return outputDataSet;
392        } finally {
393            progressMonitor.finishTask();
394        }
395    }
396
397    /**
398     * replies the set of ids of all primitives for which a fetch request to the
399     * server was submitted but which are not available from the server (the server
400     * replied a return code of 404)
401     *
402     * @return the set of ids of missing primitives
403     */
404    public Set<PrimitiveId> getMissingPrimitives() {
405        return missingPrimitives;
406    }
407
408    /**
409     * Whether this reader fetches nodes when loading ways, or members when loading relations.
410     *
411     * @return {@code true} if the reader recurses down
412     */
413    protected boolean recursesDown() {
414        return false;
415    }
416
417    /**
418     * The class holding the results given by {@link Fetcher}.
419     * It is only a wrapper of the resulting {@link DataSet} and the collection of {@link PrimitiveId} that could not have been loaded.
420     */
421    protected static class FetchResult {
422
423        /**
424         * The resulting data set
425         */
426        public final DataSet dataSet;
427
428        /**
429         * The collection of primitive ids that could not have been loaded
430         */
431        public final Set<PrimitiveId> missingPrimitives;
432
433        /**
434         * Constructs a {@code FetchResult}
435         * @param dataSet The resulting data set
436         * @param missingPrimitives The collection of primitive ids that could not have been loaded
437         */
438        public FetchResult(DataSet dataSet, Set<PrimitiveId> missingPrimitives) {
439            this.dataSet = dataSet;
440            this.missingPrimitives = missingPrimitives;
441        }
442    }
443
444    /**
445     * The class that actually download data from OSM API.
446     * Several instances of this class are used by {@link MultiFetchServerObjectReader} (one per set of primitives to fetch).
447     * The inheritance of {@link OsmServerReader} is only explained by the need to have a distinct OSM connection by {@code Fetcher} instance.
448     * @see FetchResult
449     */
450    protected class Fetcher extends OsmServerReader implements Callable<FetchResult> {
451
452        private final Set<Long> pkg;
453        private final OsmPrimitiveType type;
454        private final ProgressMonitor progressMonitor;
455
456        /**
457         * Constructs a {@code Fetcher}
458         * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
459         * {@link OsmPrimitiveType#RELATION RELATION}
460         * @param idsPackage The set of primitives ids to fetch
461         * @param progressMonitor The progress monitor
462         */
463        public Fetcher(OsmPrimitiveType type, Set<Long> idsPackage, ProgressMonitor progressMonitor) {
464            this.pkg = idsPackage;
465            this.type = type;
466            this.progressMonitor = progressMonitor;
467        }
468
469        @Override
470        public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
471            // This method is implemented because of the OsmServerReader inheritance, but not used,
472            // as the main target of this class is the call() method.
473            return fetch(progressMonitor).dataSet;
474        }
475
476        @Override
477        public FetchResult call() throws Exception {
478            return fetch(progressMonitor);
479        }
480
481        /**
482         * fetches the requested primitives and updates the specified progress monitor.
483         * @param progressMonitor the progress monitor
484         * @return the {@link FetchResult} of this operation
485         * @throws OsmTransferException if an error occurs while communicating with the API server
486         */
487        protected FetchResult fetch(ProgressMonitor progressMonitor) throws OsmTransferException {
488            try {
489                return multiGetIdPackage(type, pkg, progressMonitor);
490            } catch (OsmApiException e) {
491                if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
492                    Main.info(tr("Server replied with response code 404, retrying with an individual request for each object."));
493                    return singleGetIdPackage(type, pkg, progressMonitor);
494                } else {
495                    throw e;
496                }
497            }
498        }
499
500        @Override
501        protected String getBaseUrl() {
502            return MultiFetchServerObjectReader.this.getBaseUrl();
503        }
504
505        /**
506         * invokes a Multi Get for a set of ids and a given {@link OsmPrimitiveType}.
507         * The retrieved primitives are merged to {@link #outputDataSet}.
508         *
509         * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
510         * {@link OsmPrimitiveType#RELATION RELATION}
511         * @param pkg the package of ids
512         * @param progressMonitor progress monitor
513         * @return the {@link FetchResult} of this operation
514         * @throws OsmTransferException if an error occurs while communicating with the API server
515         */
516        protected FetchResult multiGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor)
517                throws OsmTransferException {
518            String request = buildRequestString(type, pkg);
519            FetchResult result = null;
520            try (InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE)) {
521                if (in == null) return null;
522                progressMonitor.subTask(tr("Downloading OSM data..."));
523                try {
524                    result = new FetchResult(OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(pkg.size(), false)), null);
525                } catch (IllegalDataException e) {
526                    throw new OsmTransferException(e);
527                }
528            } catch (IOException ex) {
529                Main.warn(ex);
530            }
531            return result;
532        }
533
534        /**
535         * invokes a Multi Get for a single id and a given {@link OsmPrimitiveType}.
536         * The retrieved primitive is merged to {@link #outputDataSet}.
537         *
538         * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
539         * {@link OsmPrimitiveType#RELATION RELATION}
540         * @param id the id
541         * @param progressMonitor progress monitor
542         * @return the {@link DataSet} resulting of this operation
543         * @throws OsmTransferException if an error occurs while communicating with the API server
544         */
545        protected DataSet singleGetId(OsmPrimitiveType type, long id, ProgressMonitor progressMonitor) throws OsmTransferException {
546            String request = buildRequestString(type, Collections.singleton(id));
547            DataSet result = null;
548            try (InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE)) {
549                if (in == null) return null;
550                progressMonitor.subTask(tr("Downloading OSM data..."));
551                try {
552                    result = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
553                } catch (IllegalDataException e) {
554                    throw new OsmTransferException(e);
555                }
556            } catch (IOException ex) {
557                Main.warn(ex);
558            }
559            return result;
560        }
561
562        /**
563         * invokes a sequence of Multi Gets for individual ids in a set of ids and a given {@link OsmPrimitiveType}.
564         * The retrieved primitives are merged to {@link #outputDataSet}.
565         *
566         * This method is used if one of the ids in pkg doesn't exist (the server replies with return code 404).
567         * If the set is fetched with this method it is possible to find out which of the ids doesn't exist.
568         * Unfortunately, the server does not provide an error header or an error body for a 404 reply.
569         *
570         * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
571         * {@link OsmPrimitiveType#RELATION RELATION}
572         * @param pkg the set of ids
573         * @param progressMonitor progress monitor
574         * @return the {@link FetchResult} of this operation
575         * @throws OsmTransferException if an error occurs while communicating with the API server
576         */
577        protected FetchResult singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor)
578                throws OsmTransferException {
579            FetchResult result = new FetchResult(new DataSet(), new HashSet<PrimitiveId>());
580            String baseUrl = OsmApi.getOsmApi().getBaseUrl();
581            for (long id : pkg) {
582                try {
583                    String msg;
584                    switch (type) {
585                        // CHECKSTYLE.OFF: SingleSpaceSeparator
586                        case NODE:     msg = tr("Fetching node with id {0} from ''{1}''",     id, baseUrl); break;
587                        case WAY:      msg = tr("Fetching way with id {0} from ''{1}''",      id, baseUrl); break;
588                        case RELATION: msg = tr("Fetching relation with id {0} from ''{1}''", id, baseUrl); break;
589                        // CHECKSTYLE.ON: SingleSpaceSeparator
590                        default: throw new AssertionError();
591                    }
592                    progressMonitor.setCustomText(msg);
593                    result.dataSet.mergeFrom(singleGetId(type, id, progressMonitor));
594                } catch (OsmApiException e) {
595                    if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
596                        Main.info(tr("Server replied with response code 404 for id {0}. Skipping.", Long.toString(id)));
597                        result.missingPrimitives.add(new SimplePrimitiveId(id, type));
598                    } else {
599                        throw e;
600                    }
601                }
602            }
603            return result;
604        }
605    }
606}