- Timestamp:
- 2020-01-27T09:17:42+01:00 (5 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadReferrersTask.java
r15152 r15787 8 8 import java.text.MessageFormat; 9 9 import java.util.Collection; 10 import java.util.HashMap; 11 import java.util.Map; 12 import java.util.Map.Entry; 10 import java.util.LinkedHashSet; 13 11 import java.util.Set; 14 12 import java.util.stream.Collectors; … … 48 46 private final OsmDataLayer targetLayer; 49 47 /** the collection of child primitives */ 50 private final Map<Long, OsmPrimitiveType> children;48 private final Set<PrimitiveId> children; 51 49 /** the parents */ 52 50 private final DataSet parents; … … 57 55 * @param targetLayer the target layer for the downloaded primitives. Must not be null. 58 56 * @param children the collection of child primitives for which parents are to be downloaded 57 * @since 15787 (modified interface) 59 58 */ 60 public DownloadReferrersTask(OsmDataLayer targetLayer, Collection< OsmPrimitive> children) {59 public DownloadReferrersTask(OsmDataLayer targetLayer, Collection<? extends PrimitiveId> children) { 61 60 super("Download referrers", false /* don't ignore exception*/); 62 61 CheckParameterUtil.ensureParameterNotNull(targetLayer, "targetLayer"); … … 65 64 } 66 65 canceled = false; 67 this.children = new HashMap<>();66 this.children = new LinkedHashSet<>(); 68 67 if (children != null) { 69 for (OsmPrimitive p: children) { 70 if (!p.isNew()) { 71 this.children.put(p.getId(), OsmPrimitiveType.from(p)); 72 } 73 } 68 children.stream().filter(p -> !p.isNew()).forEach(this.children::add); 74 69 } 70 75 71 this.targetLayer = targetLayer; 76 72 parents = new DataSet(); … … 94 90 "Cannot download referrers for new primitives (ID {0})", primitiveId.getUniqueId())); 95 91 canceled = false; 96 this.children = new HashMap<>();97 this.children. put(primitiveId.getUniqueId(), primitiveId.getType());92 this.children = new LinkedHashSet<>(); 93 this.children.add(primitiveId); 98 94 this.targetLayer = targetLayer; 99 95 parents = new DataSet(); … … 141 137 142 138 protected void downloadParents(long id, OsmPrimitiveType type, ProgressMonitor progressMonitor) throws OsmTransferException { 143 reader = new OsmServerBackreferenceReader(id, type); 139 reader = new OsmServerBackreferenceReader(id, type, false).setAllowIncompleteParentWays(true); 140 144 141 DataSet ds = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false)); 145 142 synchronized (this) { // avoid race condition in cancel() 146 143 reader = null; 147 } 148 Collection<Way> ways = ds.getWays(); 149 150 if (!ways.isEmpty()) { 151 // Ensure each node is only listed once 152 Set<Node> nodes = ways.stream().flatMap(w -> w.getNodes().stream()).collect(Collectors.toSet()); 153 // Don't retrieve any nodes we've already grabbed 154 nodes.removeAll(targetLayer.data.getNodes()); 155 if (!nodes.isEmpty()) { 156 reader = MultiFetchServerObjectReader.create(); 157 ((MultiFetchServerObjectReader) reader).append(nodes); 158 DataSet wayNodes = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false)); 159 synchronized (this) { // avoid race condition in cancel() 160 reader = null; 161 } 162 new DataSetMerger(ds, wayNodes).merge(); 163 } 144 if (canceled) 145 return; 164 146 } 165 147 new DataSetMerger(parents, ds).merge(); … … 171 153 progressMonitor.setTicksCount(children.size()); 172 154 int i = 1; 173 for ( Entry<Long, OsmPrimitiveType> entry: children.entrySet()) {155 for (PrimitiveId p : children) { 174 156 if (canceled) 175 157 return; 176 158 String msg; 177 switch(entry.getValue()) { 178 case NODE: msg = tr("({0}/{1}) Loading parents of node {2}", i+1, children.size(), entry.getKey()); break; 179 case WAY: msg = tr("({0}/{1}) Loading parents of way {2}", i+1, children.size(), entry.getKey()); break; 180 case RELATION: msg = tr("({0}/{1}) Loading parents of relation {2}", i+1, children.size(), entry.getKey()); break; 159 String id = Long.toString(p.getUniqueId()); 160 switch(p.getType()) { 161 case NODE: msg = tr("({0}/{1}) Loading parents of node {2}", i, children.size(), id); break; 162 case WAY: msg = tr("({0}/{1}) Loading parents of way {2}", i, children.size(), id); break; 163 case RELATION: msg = tr("({0}/{1}) Loading parents of relation {2}", i, children.size(), id); break; 181 164 default: throw new AssertionError(); 182 165 } 183 166 progressMonitor.subTask(msg); 184 downloadParents( entry.getKey(), entry.getValue(), progressMonitor);167 downloadParents(p.getUniqueId(), p.getType(), progressMonitor); 185 168 i++; 169 } 170 Collection<Way> ways = parents.getWays(); 171 172 if (!ways.isEmpty()) { 173 // Collect incomplete nodes of parent ways 174 Set<Node> nodes = ways.stream().flatMap(w -> w.getNodes().stream().filter(OsmPrimitive::isIncomplete)) 175 .collect(Collectors.toSet()); 176 if (!nodes.isEmpty()) { 177 reader = MultiFetchServerObjectReader.create(); 178 ((MultiFetchServerObjectReader) reader).append(nodes); 179 DataSet wayNodes = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false)); 180 synchronized (this) { // avoid race condition in cancel() 181 reader = null; 182 } 183 new DataSetMerger(parents, wayNodes).merge(); 184 } 186 185 } 187 186 } catch (OsmTransferException e) { -
trunk/src/org/openstreetmap/josm/gui/io/DownloadPrimitivesWithReferrersTask.java
r15746 r15787 114 114 currentTask.run(); 115 115 // Then, download referrers for each primitive 116 if (downloadReferrers) 117 for (PrimitiveId id : ids) { 118 synchronized (this) { 119 if (canceled) { 120 currentTask = null; 121 return; 122 } 123 currentTask = new DownloadReferrersTask( 124 tmpLayer, id, getProgressMonitor().createSubTaskMonitor(1, false)); 125 } 126 currentTask.run(); 127 } 116 if (downloadReferrers) { 117 currentTask = new DownloadReferrersTask(tmpLayer, ids); 118 currentTask.run(); 119 synchronized (this) { 120 if (currentTask.getProgressMonitor().isCanceled()) 121 cancel(); 122 } 123 } 128 124 currentTask = null; 129 125 }
Note:
See TracChangeset
for help on using the changeset viewer.