source: josm/trunk/src/org/openstreetmap/josm/command/PurgeCommand.java@ 13757

Last change on this file since 13757 was 13513, checked in by Don-vip, 6 years ago

fix #16078 - avoid NPE in PurgeCommand with null relations

  • Property svn:eol-style set to native
File size: 16.8 KB
RevLine 
[3431]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.HashMap;
9import java.util.HashSet;
10import java.util.Iterator;
11import java.util.List;
12import java.util.Map;
[9371]13import java.util.Objects;
[3431]14import java.util.Set;
15
[4918]16import javax.swing.Icon;
[3431]17
[5993]18import org.openstreetmap.josm.data.conflict.Conflict;
19import org.openstreetmap.josm.data.conflict.ConflictCollection;
[3431]20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.NodeData;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.PrimitiveData;
25import org.openstreetmap.josm.data.osm.PrimitiveId;
26import org.openstreetmap.josm.data.osm.Relation;
27import org.openstreetmap.josm.data.osm.RelationData;
[12688]28import org.openstreetmap.josm.data.osm.RelationMember;
[3431]29import org.openstreetmap.josm.data.osm.Storage;
30import org.openstreetmap.josm.data.osm.Way;
31import org.openstreetmap.josm.data.osm.WayData;
[12846]32import org.openstreetmap.josm.spi.preferences.Config;
[3431]33import org.openstreetmap.josm.tools.ImageProvider;
34
35/**
36 * Command, to purge a list of primitives.
37 */
38public class PurgeCommand extends Command {
39 protected List<OsmPrimitive> toPurge;
40 protected Storage<PrimitiveData> makeIncompleteData;
41
[8346]42 protected Map<PrimitiveId, PrimitiveData> makeIncompleteDataByPrimId;
[6069]43
[5993]44 protected final ConflictCollection purgedConflicts = new ConflictCollection();
[3431]45
46 /**
[11240]47 * Constructs a new {@code PurgeCommand} (does not handle conflicts).
48 * This command relies on a number of consistency conditions:
49 * - makeIncomplete must be a subset of toPurge.
50 * - Each primitive, that is in toPurge but not in makeIncomplete, must have all its referrers in toPurge.
51 * - Each element of makeIncomplete must not be new and must have only referrers that are either a relation or included in toPurge.
52 * @param data OSM data set
53 * @param toPurge primitives to purge
54 * @param makeIncomplete primitives to make incomplete
55 * @since 11240
56 */
57 public PurgeCommand(DataSet data, Collection<OsmPrimitive> toPurge, Collection<OsmPrimitive> makeIncomplete) {
58 super(data);
59 init(toPurge, makeIncomplete);
60 }
61
62 private void init(Collection<OsmPrimitive> toPurge, Collection<OsmPrimitive> makeIncomplete) {
[3431]63 /**
64 * The topological sort is to avoid missing way nodes and missing
65 * relation members when adding primitives back to the dataset on undo.
66 *
67 * The same should hold for normal execution, but at time of writing
68 * there seem to be no such consistency checks when removing primitives.
69 * (It is done in a save manner, anyway.)
70 */
71 this.toPurge = topoSort(toPurge);
72 saveIncomplete(makeIncomplete);
73 }
74
[6890]75 protected final void saveIncomplete(Collection<OsmPrimitive> makeIncomplete) {
[7005]76 makeIncompleteData = new Storage<>(new Storage.PrimitiveIdHash());
[8346]77 makeIncompleteDataByPrimId = makeIncompleteData.foreignKey(new Storage.PrimitiveIdHash());
[3431]78
79 for (OsmPrimitive osm : makeIncomplete) {
80 makeIncompleteData.add(osm.save());
81 }
82 }
83
84 @Override
85 public boolean executeCommand() {
[11240]86 getAffectedDataSet().beginUpdate();
[3431]87 try {
[5993]88 purgedConflicts.get().clear();
[12605]89 // unselect primitives in advance to not fire a selection change for every one of them
90 getAffectedDataSet().clearSelection(toPurge);
91 // Loop from back to front to keep referential integrity.
[8510]92 for (int i = toPurge.size()-1; i >= 0; --i) {
[3431]93 OsmPrimitive osm = toPurge.get(i);
[8346]94 if (makeIncompleteDataByPrimId.containsKey(osm)) {
[3431]95 // we could simply set the incomplete flag
96 // but that would not free memory in case the
97 // user clears undo/redo buffer after purge
98 PrimitiveData empty;
99 switch(osm.getType()) {
[3479]100 case NODE: empty = new NodeData(); break;
101 case WAY: empty = new WayData(); break;
102 case RELATION: empty = new RelationData(); break;
103 default: throw new AssertionError();
[3431]104 }
105 empty.setId(osm.getUniqueId());
106 empty.setIncomplete(true);
107 osm.load(empty);
108 } else {
[11240]109 getAffectedDataSet().removePrimitive(osm);
[12672]110 Conflict<?> conflict = getAffectedDataSet().getConflicts().getConflictForMy(osm);
111 if (conflict != null) {
112 purgedConflicts.add(conflict);
113 getAffectedDataSet().getConflicts().remove(conflict);
[5993]114 }
[3431]115 }
116 }
[13420]117 getAffectedDataSet().clearMappaintCache();
[3431]118 } finally {
[11240]119 getAffectedDataSet().endUpdate();
[3431]120 }
121 return true;
122 }
123
124 @Override
125 public void undoCommand() {
[11240]126 if (getAffectedDataSet() == null)
[3431]127 return;
128
[13420]129 getAffectedDataSet().beginUpdate();
130 try {
131 for (OsmPrimitive osm : toPurge) {
132 PrimitiveData data = makeIncompleteDataByPrimId.get(osm);
133 if (data != null) {
134 if (getAffectedDataSet().getPrimitiveById(osm) != osm)
135 throw new AssertionError(
136 String.format("Primitive %s has been made incomplete when purging, but it cannot be found on undo.", osm));
137 osm.load(data);
138 } else {
139 if (getAffectedDataSet().getPrimitiveById(osm) != null)
140 throw new AssertionError(String.format("Primitive %s was removed when purging, but is still there on undo", osm));
141 getAffectedDataSet().addPrimitive(osm);
142 }
[3431]143 }
[6069]144
[13420]145 for (Conflict<?> conflict : purgedConflicts) {
146 getAffectedDataSet().getConflicts().add(conflict);
147 }
148 getAffectedDataSet().clearMappaintCache();
149 } finally {
150 getAffectedDataSet().endUpdate();
[5993]151 }
[3431]152 }
153
154 /**
155 * Sorts a collection of primitives such that for each object
156 * its referrers come later in the sorted collection.
[9243]157 * @param sel collection of primitives to sort
[8931]158 * @return sorted list
[3431]159 */
160 public static List<OsmPrimitive> topoSort(Collection<OsmPrimitive> sel) {
[7005]161 Set<OsmPrimitive> in = new HashSet<>(sel);
162 List<OsmPrimitive> out = new ArrayList<>(in.size());
[13513]163 Set<Relation> inR = new HashSet<>();
[3431]164
[5905]165 // Nodes not deleted in the first pass
[7005]166 Set<OsmPrimitive> remainingNodes = new HashSet<>(in.size());
[5905]167
[3431]168 /**
169 * First add nodes that have no way referrer.
170 */
171 outer:
[3479]172 for (Iterator<OsmPrimitive> it = in.iterator(); it.hasNext();) {
173 OsmPrimitive u = it.next();
174 if (u instanceof Node) {
175 Node n = (Node) u;
176 for (OsmPrimitive ref : n.getReferrers()) {
177 if (ref instanceof Way && in.contains(ref)) {
[5905]178 it.remove();
179 remainingNodes.add(n);
[3479]180 continue outer;
181 }
182 }
183 it.remove();
184 out.add(n);
[3431]185 }
186 }
187
188 /**
189 * Then add all ways, each preceded by its (remaining) nodes.
190 */
[5905]191 for (Iterator<OsmPrimitive> it = in.iterator(); it.hasNext();) {
192 OsmPrimitive u = it.next();
193 if (u instanceof Way) {
194 Way w = (Way) u;
195 it.remove();
196 for (Node n : w.getNodes()) {
197 if (remainingNodes.contains(n)) {
198 remainingNodes.remove(n);
199 out.add(n);
[3431]200 }
201 }
[5905]202 out.add(w);
[13513]203 } else if (u instanceof Relation) {
204 inR.add((Relation) u);
[3431]205 }
[5681]206 }
[3431]207
[5905]208 if (!remainingNodes.isEmpty())
209 throw new AssertionError("topo sort algorithm failed (nodes remaining)");
210
[13513]211 // Do topological sorting on a DAG where each arrow points from child to parent.
212 // (Because it is faster to loop over getReferrers() than getMembers().)
[3431]213
[8338]214 Map<Relation, Integer> numChilds = new HashMap<>();
[3431]215
[5681]216 // calculate initial number of childs
217 for (Relation r : inR) {
218 numChilds.put(r, 0);
219 }
220 for (Relation r : inR) {
221 for (OsmPrimitive parent : r.getReferrers()) {
222 if (!(parent instanceof Relation))
223 throw new AssertionError();
224 Integer i = numChilds.get(parent);
225 if (i != null) {
[8510]226 numChilds.put((Relation) parent, i+1);
[3431]227 }
228 }
[5681]229 }
[9062]230 Set<Relation> childlessR = new HashSet<>();
[5681]231 for (Relation r : inR) {
232 if (numChilds.get(r).equals(0)) {
233 childlessR.add(r);
[3431]234 }
[5681]235 }
[3431]236
[9062]237 List<Relation> outR = new ArrayList<>(inR.size());
[5681]238 while (!childlessR.isEmpty()) {
[10378]239 // Identify one childless Relation and let it virtually die. This makes other relations childless.
240 Iterator<Relation> it = childlessR.iterator();
[5681]241 Relation next = it.next();
242 it.remove();
243 outR.add(next);
[3431]244
[5681]245 for (OsmPrimitive parentPrim : next.getReferrers()) {
246 Relation parent = (Relation) parentPrim;
247 Integer i = numChilds.get(parent);
248 if (i != null) {
249 numChilds.put(parent, i-1);
250 if (i-1 == 0) {
251 childlessR.add(parent);
[3431]252 }
253 }
254 }
[5681]255 }
[3431]256
[5681]257 if (outR.size() != inR.size())
258 throw new AssertionError("topo sort algorithm failed");
[3431]259
[5681]260 out.addAll(outR);
[3431]261
[5681]262 return out;
[3431]263 }
[3479]264
[3431]265 @Override
[4918]266 public String getDescriptionText() {
267 return trn("Purged {0} object", "Purged {0} objects", toPurge.size(), toPurge.size());
[3431]268 }
269
270 @Override
[4918]271 public Icon getDescriptionIcon() {
272 return ImageProvider.get("data", "purge");
273 }
274
275 @Override
[3431]276 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
277 return toPurge;
278 }
279
280 @Override
281 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
[9989]282 // Do nothing
[3431]283 }
[8456]284
285 @Override
286 public int hashCode() {
[11240]287 return Objects.hash(super.hashCode(), toPurge, makeIncompleteData, makeIncompleteDataByPrimId, purgedConflicts, getAffectedDataSet());
[8456]288 }
289
290 @Override
291 public boolean equals(Object obj) {
[9371]292 if (this == obj) return true;
293 if (obj == null || getClass() != obj.getClass()) return false;
294 if (!super.equals(obj)) return false;
295 PurgeCommand that = (PurgeCommand) obj;
296 return Objects.equals(toPurge, that.toPurge) &&
297 Objects.equals(makeIncompleteData, that.makeIncompleteData) &&
298 Objects.equals(makeIncompleteDataByPrimId, that.makeIncompleteDataByPrimId) &&
[11240]299 Objects.equals(purgedConflicts, that.purgedConflicts);
[8456]300 }
[12688]301
302 /**
303 * Creates a new {@code PurgeCommand} to purge selected OSM primitives.
304 * @param sel selected OSM primitives
305 * @param toPurgeAdditionally optional list that will be filled with primitives to be purged that have not been in the selection
306 * @return command to purge selected OSM primitives
[12718]307 * @since 12718
308 */
309 public static PurgeCommand build(Collection<OsmPrimitive> sel, List<OsmPrimitive> toPurgeAdditionally) {
[12688]310 Set<OsmPrimitive> toPurge = new HashSet<>(sel);
311 // finally, contains all objects that are purged
312 Set<OsmPrimitive> toPurgeChecked = new HashSet<>();
313
314 // Add referrer, unless the object to purge is not new and the parent is a relation
315 Set<OsmPrimitive> toPurgeRecursive = new HashSet<>();
316 while (!toPurge.isEmpty()) {
317
318 for (OsmPrimitive osm: toPurge) {
319 for (OsmPrimitive parent: osm.getReferrers()) {
320 if (toPurge.contains(parent) || toPurgeChecked.contains(parent) || toPurgeRecursive.contains(parent)) {
321 continue;
322 }
323 if (parent instanceof Way || (parent instanceof Relation && osm.isNew())) {
324 if (toPurgeAdditionally != null) {
325 toPurgeAdditionally.add(parent);
326 }
327 toPurgeRecursive.add(parent);
328 }
329 }
330 toPurgeChecked.add(osm);
331 }
332 toPurge = toPurgeRecursive;
333 toPurgeRecursive = new HashSet<>();
334 }
335
336 // Subset of toPurgeChecked. Marks primitives that remain in the dataset, but incomplete.
337 Set<OsmPrimitive> makeIncomplete = new HashSet<>();
338
339 // Find the objects that will be incomplete after purging.
340 // At this point, all parents of new to-be-purged primitives are
341 // also to-be-purged and
342 // all parents of not-new to-be-purged primitives are either
343 // to-be-purged or of type relation.
344 TOP:
345 for (OsmPrimitive child : toPurgeChecked) {
346 if (child.isNew()) {
347 continue;
348 }
349 for (OsmPrimitive parent : child.getReferrers()) {
350 if (parent instanceof Relation && !toPurgeChecked.contains(parent)) {
351 makeIncomplete.add(child);
352 continue TOP;
353 }
354 }
355 }
356
357 // Add untagged way nodes. Do not add nodes that have other referrers not yet to-be-purged.
[12846]358 if (Config.getPref().getBoolean("purge.add_untagged_waynodes", true)) {
[12688]359 Set<OsmPrimitive> wayNodes = new HashSet<>();
360 for (OsmPrimitive osm : toPurgeChecked) {
361 if (osm instanceof Way) {
362 Way w = (Way) osm;
363 NODE:
364 for (Node n : w.getNodes()) {
365 if (n.isTagged() || toPurgeChecked.contains(n)) {
366 continue;
367 }
368 for (OsmPrimitive ref : n.getReferrers()) {
369 if (ref != w && !toPurgeChecked.contains(ref)) {
370 continue NODE;
371 }
372 }
373 wayNodes.add(n);
374 }
375 }
376 }
377 toPurgeChecked.addAll(wayNodes);
378 if (toPurgeAdditionally != null) {
379 toPurgeAdditionally.addAll(wayNodes);
380 }
381 }
382
[12846]383 if (Config.getPref().getBoolean("purge.add_relations_with_only_incomplete_members", true)) {
[12688]384 Set<Relation> relSet = new HashSet<>();
385 for (OsmPrimitive osm : toPurgeChecked) {
386 for (OsmPrimitive parent : osm.getReferrers()) {
387 if (parent instanceof Relation
388 && !(toPurgeChecked.contains(parent))
389 && hasOnlyIncompleteMembers((Relation) parent, toPurgeChecked, relSet)) {
390 relSet.add((Relation) parent);
391 }
392 }
393 }
394
395 // Add higher level relations (list gets extended while looping over it)
396 List<Relation> relLst = new ArrayList<>(relSet);
397 for (int i = 0; i < relLst.size(); ++i) { // foreach loop not applicable since list gets extended while looping over it
398 for (OsmPrimitive parent : relLst.get(i).getReferrers()) {
399 if (!(toPurgeChecked.contains(parent))
400 && hasOnlyIncompleteMembers((Relation) parent, toPurgeChecked, relLst)) {
401 relLst.add((Relation) parent);
402 }
403 }
404 }
405 relSet = new HashSet<>(relLst);
406 toPurgeChecked.addAll(relSet);
407 if (toPurgeAdditionally != null) {
408 toPurgeAdditionally.addAll(relSet);
409 }
410 }
411
[12718]412 return new PurgeCommand(toPurgeChecked.iterator().next().getDataSet(), toPurgeChecked, makeIncomplete);
[12688]413 }
414
415 private static boolean hasOnlyIncompleteMembers(
416 Relation r, Collection<OsmPrimitive> toPurge, Collection<? extends OsmPrimitive> moreToPurge) {
417 for (RelationMember m : r.getMembers()) {
418 if (!m.getMember().isIncomplete() && !toPurge.contains(m.getMember()) && !moreToPurge.contains(m.getMember()))
419 return false;
420 }
421 return true;
422 }
[3431]423}
Note: See TracBrowser for help on using the repository browser.