[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.data;
|
---|
| 3 |
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.Collection;
|
---|
[10647] | 6 | import java.util.Comparator;
|
---|
[2512] | 7 | import java.util.HashMap;
|
---|
| 8 | import java.util.HashSet;
|
---|
| 9 | import java.util.LinkedList;
|
---|
| 10 | import java.util.List;
|
---|
[6317] | 11 | import java.util.Map;
|
---|
[2512] | 12 | import java.util.Set;
|
---|
[8856] | 13 | import java.util.Stack;
|
---|
[11175] | 14 | import java.util.stream.Collectors;
|
---|
| 15 | import java.util.stream.Stream;
|
---|
[2512] | 16 |
|
---|
[2979] | 17 | import org.openstreetmap.josm.data.conflict.ConflictCollection;
|
---|
[12673] | 18 | import org.openstreetmap.josm.data.osm.CyclicUploadDependencyException;
|
---|
[2512] | 19 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[4534] | 20 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
[2512] | 21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[11177] | 23 | import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator;
|
---|
[2979] | 24 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
[2512] | 25 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 26 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
| 27 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[13161] | 28 | import org.openstreetmap.josm.tools.Logging;
|
---|
[4100] | 29 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2512] | 30 |
|
---|
| 31 | /**
|
---|
[7599] | 32 | * Represents a collection of {@link OsmPrimitive}s which should be uploaded to the API.
|
---|
[5266] | 33 | * The collection is derived from the modified primitives of an {@link DataSet} and it provides methods
|
---|
[2598] | 34 | * for sorting the objects in upload order.
|
---|
[7599] | 35 | * @since 2025
|
---|
[2512] | 36 | */
|
---|
| 37 | public class APIDataSet {
|
---|
[6317] | 38 | private List<OsmPrimitive> toAdd;
|
---|
[16913] | 39 | private final List<OsmPrimitive> toUpdate;
|
---|
[6317] | 40 | private List<OsmPrimitive> toDelete;
|
---|
[2512] | 41 |
|
---|
| 42 | /**
|
---|
[13161] | 43 | * The type of operation we can perform with OSM API on a primitive.
|
---|
| 44 | * @since 13161
|
---|
| 45 | */
|
---|
| 46 | public enum APIOperation {
|
---|
| 47 | /** Add a new primitive */
|
---|
| 48 | ADD,
|
---|
| 49 | /** Update an existing primitive */
|
---|
| 50 | UPDATE,
|
---|
| 51 | /** Delete an existing primitive */
|
---|
| 52 | DELETE;
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * Determines the API operation to perform on a primitive.
|
---|
| 56 | * @param osm OSM primitive
|
---|
| 57 | * @return the API operation to perform on {@code osm}
|
---|
| 58 | */
|
---|
| 59 | public static APIOperation of(OsmPrimitive osm) {
|
---|
| 60 | if (osm.isNewOrUndeleted() && !osm.isDeleted()) {
|
---|
| 61 | return ADD;
|
---|
| 62 | } else if (osm.isModified() && !osm.isDeleted()) {
|
---|
| 63 | return UPDATE;
|
---|
| 64 | } else if (osm.isDeleted() && !osm.isNew() && osm.isModified() && osm.isVisible()) {
|
---|
| 65 | return DELETE;
|
---|
| 66 | }
|
---|
| 67 | return null;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
[2512] | 72 | * creates a new empty data set
|
---|
| 73 | */
|
---|
| 74 | public APIDataSet() {
|
---|
[7005] | 75 | toAdd = new LinkedList<>();
|
---|
| 76 | toUpdate = new LinkedList<>();
|
---|
| 77 | toDelete = new LinkedList<>();
|
---|
[2512] | 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * initializes the API data set with the modified primitives in <code>ds</code>
|
---|
| 82 | *
|
---|
| 83 | * @param ds the data set. Ignored, if null.
|
---|
| 84 | */
|
---|
| 85 | public void init(DataSet ds) {
|
---|
| 86 | if (ds == null) return;
|
---|
[5589] | 87 | init(ds.allPrimitives());
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[11175] | 90 | /**
|
---|
| 91 | * Initializes the API data set with the modified primitives, ignores unmodified primitives.
|
---|
| 92 | *
|
---|
| 93 | * @param primitives the primitives
|
---|
| 94 | */
|
---|
[6890] | 95 | public final void init(Collection<OsmPrimitive> primitives) {
|
---|
[2512] | 96 | toAdd.clear();
|
---|
| 97 | toUpdate.clear();
|
---|
| 98 | toDelete.clear();
|
---|
| 99 |
|
---|
[5589] | 100 | for (OsmPrimitive osm :primitives) {
|
---|
[13164] | 101 | APIOperation op = APIOperation.of(osm);
|
---|
| 102 | if (op != null) {
|
---|
| 103 | switch (op) {
|
---|
| 104 | case ADD: toAdd.add(osm); break;
|
---|
| 105 | case UPDATE: toUpdate.add(osm); break;
|
---|
| 106 | case DELETE: toDelete.add(osm); break;
|
---|
| 107 | default: Logging.trace("Ignored primitive {0} -> {1}", osm, op);
|
---|
| 108 | }
|
---|
[2512] | 109 | }
|
---|
| 110 | }
|
---|
[11177] | 111 | final Comparator<OsmPrimitive> orderingNodesWaysRelations = OsmPrimitiveComparator.orderingNodesWaysRelations();
|
---|
| 112 | final Comparator<OsmPrimitive> byUniqueId = OsmPrimitiveComparator.comparingUniqueId();
|
---|
[11176] | 113 | toAdd.sort(orderingNodesWaysRelations.thenComparing(byUniqueId));
|
---|
| 114 | toUpdate.sort(orderingNodesWaysRelations.thenComparing(byUniqueId));
|
---|
| 115 | toDelete.sort(orderingNodesWaysRelations.reversed().thenComparing(byUniqueId));
|
---|
[2512] | 116 | }
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * initializes the API data set with the modified primitives in <code>ds</code>
|
---|
| 120 | *
|
---|
| 121 | * @param ds the data set. Ignored, if null.
|
---|
| 122 | */
|
---|
| 123 | public APIDataSet(DataSet ds) {
|
---|
| 124 | this();
|
---|
| 125 | init(ds);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
[2979] | 129 | * Replies true if one of the primitives to be updated or to be deleted
|
---|
| 130 | * participates in at least one conflict in <code>conflicts</code>
|
---|
[3530] | 131 | *
|
---|
[2979] | 132 | * @param conflicts the collection of conflicts
|
---|
| 133 | * @return true if one of the primitives to be updated or to be deleted
|
---|
| 134 | * participates in at least one conflict in <code>conflicts</code>
|
---|
| 135 | */
|
---|
| 136 | public boolean participatesInConflict(ConflictCollection conflicts) {
|
---|
| 137 | if (conflicts == null || conflicts.isEmpty()) return false;
|
---|
[11175] | 138 | Set<PrimitiveId> idsParticipatingInConflicts = conflicts.get().stream()
|
---|
| 139 | .flatMap(c -> Stream.of(c.getMy(), c.getTheir()))
|
---|
| 140 | .map(OsmPrimitive::getPrimitiveId)
|
---|
| 141 | .collect(Collectors.toSet());
|
---|
| 142 | return Stream.of(toUpdate, toDelete)
|
---|
| 143 | .flatMap(Collection::stream)
|
---|
| 144 | .map(OsmPrimitive::getPrimitiveId)
|
---|
| 145 | .anyMatch(idsParticipatingInConflicts::contains);
|
---|
[2979] | 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
[2512] | 149 | * initializes the API data set with the primitives in <code>primitives</code>
|
---|
| 150 | *
|
---|
| 151 | * @param primitives the collection of primitives
|
---|
| 152 | */
|
---|
| 153 | public APIDataSet(Collection<OsmPrimitive> primitives) {
|
---|
| 154 | this();
|
---|
[5589] | 155 | init(primitives);
|
---|
[2512] | 156 | }
|
---|
| 157 |
|
---|
| 158 | /**
|
---|
| 159 | * Replies true if there are no primitives to upload
|
---|
| 160 | *
|
---|
| 161 | * @return true if there are no primitives to upload
|
---|
| 162 | */
|
---|
| 163 | public boolean isEmpty() {
|
---|
| 164 | return toAdd.isEmpty() && toUpdate.isEmpty() && toDelete.isEmpty();
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /**
|
---|
| 168 | * Replies the primitives which should be added to the OSM database
|
---|
| 169 | *
|
---|
| 170 | * @return the primitives which should be added to the OSM database
|
---|
| 171 | */
|
---|
| 172 | public List<OsmPrimitive> getPrimitivesToAdd() {
|
---|
| 173 | return toAdd;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
| 177 | * Replies the primitives which should be updated in the OSM database
|
---|
| 178 | *
|
---|
| 179 | * @return the primitives which should be updated in the OSM database
|
---|
| 180 | */
|
---|
| 181 | public List<OsmPrimitive> getPrimitivesToUpdate() {
|
---|
| 182 | return toUpdate;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /**
|
---|
| 186 | * Replies the primitives which should be deleted in the OSM database
|
---|
| 187 | *
|
---|
| 188 | * @return the primitives which should be deleted in the OSM database
|
---|
| 189 | */
|
---|
| 190 | public List<OsmPrimitive> getPrimitivesToDelete() {
|
---|
| 191 | return toDelete;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /**
|
---|
| 195 | * Replies all primitives
|
---|
| 196 | *
|
---|
| 197 | * @return all primitives
|
---|
| 198 | */
|
---|
| 199 | public List<OsmPrimitive> getPrimitives() {
|
---|
[8338] | 200 | List<OsmPrimitive> ret = new LinkedList<>();
|
---|
[2512] | 201 | ret.addAll(toAdd);
|
---|
| 202 | ret.addAll(toUpdate);
|
---|
| 203 | ret.addAll(toDelete);
|
---|
| 204 | return ret;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
[2598] | 208 | * Replies the number of objects to upload
|
---|
[2711] | 209 | *
|
---|
[2598] | 210 | * @return the number of objects to upload
|
---|
| 211 | */
|
---|
| 212 | public int getSize() {
|
---|
| 213 | return toAdd.size() + toUpdate.size() + toDelete.size();
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[12374] | 216 | /**
|
---|
| 217 | * Removes the given primitives from this {@link APIDataSet}
|
---|
| 218 | * @param processed The primitives to remove
|
---|
| 219 | */
|
---|
[4534] | 220 | public void removeProcessed(Collection<IPrimitive> processed) {
|
---|
[2598] | 221 | if (processed == null) return;
|
---|
| 222 | toAdd.removeAll(processed);
|
---|
| 223 | toUpdate.removeAll(processed);
|
---|
| 224 | toDelete.removeAll(processed);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /**
|
---|
[2512] | 228 | * Adjusts the upload order for new relations. Child relations are uploaded first,
|
---|
| 229 | * parent relations second.
|
---|
| 230 | *
|
---|
| 231 | * This method detects cyclic dependencies in new relation. Relations with cyclic
|
---|
| 232 | * dependencies can't be uploaded.
|
---|
| 233 | *
|
---|
[8291] | 234 | * @throws CyclicUploadDependencyException if a cyclic dependency is detected
|
---|
[2512] | 235 | */
|
---|
[8510] | 236 | public void adjustRelationUploadOrder() throws CyclicUploadDependencyException {
|
---|
[8338] | 237 | List<OsmPrimitive> newToAdd = new LinkedList<>();
|
---|
[4100] | 238 | newToAdd.addAll(Utils.filteredCollection(toAdd, Node.class));
|
---|
| 239 | newToAdd.addAll(Utils.filteredCollection(toAdd, Way.class));
|
---|
[2512] | 240 |
|
---|
[7005] | 241 | List<Relation> relationsToAdd = new ArrayList<>(Utils.filteredCollection(toAdd, Relation.class));
|
---|
[2512] | 242 | List<Relation> noProblemRelations = filterRelationsNotReferringToNewRelations(relationsToAdd);
|
---|
| 243 | newToAdd.addAll(noProblemRelations);
|
---|
| 244 | relationsToAdd.removeAll(noProblemRelations);
|
---|
| 245 |
|
---|
[6776] | 246 | RelationUploadDependencyGraph graph = new RelationUploadDependencyGraph(relationsToAdd, true);
|
---|
[13018] | 247 | newToAdd.addAll(graph.computeUploadOrder(false));
|
---|
[2512] | 248 | toAdd = newToAdd;
|
---|
[6776] | 249 |
|
---|
[8338] | 250 | List<OsmPrimitive> newToDelete = new LinkedList<>();
|
---|
[6776] | 251 | graph = new RelationUploadDependencyGraph(Utils.filteredCollection(toDelete, Relation.class), false);
|
---|
[13018] | 252 | newToDelete.addAll(graph.computeUploadOrder(true));
|
---|
[6801] | 253 | newToDelete.addAll(Utils.filteredCollection(toDelete, Way.class));
|
---|
| 254 | newToDelete.addAll(Utils.filteredCollection(toDelete, Node.class));
|
---|
[6776] | 255 | toDelete = newToDelete;
|
---|
[2512] | 256 | }
|
---|
| 257 |
|
---|
| 258 | /**
|
---|
| 259 | * Replies the subset of relations in <code>relations</code> which are not referring to any
|
---|
| 260 | * new relation
|
---|
| 261 | *
|
---|
| 262 | * @param relations a list of relations
|
---|
| 263 | * @return the subset of relations in <code>relations</code> which are not referring to any
|
---|
| 264 | * new relation
|
---|
| 265 | */
|
---|
| 266 | protected List<Relation> filterRelationsNotReferringToNewRelations(Collection<Relation> relations) {
|
---|
[7005] | 267 | List<Relation> ret = new LinkedList<>();
|
---|
[2512] | 268 | for (Relation relation: relations) {
|
---|
[16436] | 269 | boolean refersToNewRelation = relation.getMembers().stream()
|
---|
| 270 | .anyMatch(m -> m.isRelation() && m.getMember().isNewOrUndeleted());
|
---|
[2512] | 271 | if (!refersToNewRelation) {
|
---|
| 272 | ret.add(relation);
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 | return ret;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /**
|
---|
[2915] | 279 | * Utility class to sort a collection of new relations with their dependencies
|
---|
[2512] | 280 | * topologically.
|
---|
| 281 | *
|
---|
| 282 | */
|
---|
[4874] | 283 | private static class RelationUploadDependencyGraph {
|
---|
[9067] | 284 | private final Map<Relation, Set<Relation>> children = new HashMap<>();
|
---|
[2512] | 285 | private Collection<Relation> relations;
|
---|
[7005] | 286 | private Set<Relation> visited = new HashSet<>();
|
---|
[2512] | 287 | private List<Relation> uploadOrder;
|
---|
[6776] | 288 | private final boolean newOrUndeleted;
|
---|
[2512] | 289 |
|
---|
[8836] | 290 | RelationUploadDependencyGraph(Collection<Relation> relations, boolean newOrUndeleted) {
|
---|
[6776] | 291 | this.newOrUndeleted = newOrUndeleted;
|
---|
[2512] | 292 | build(relations);
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[6890] | 295 | public final void build(Collection<Relation> relations) {
|
---|
[7005] | 296 | this.relations = new HashSet<>();
|
---|
[8510] | 297 | for (Relation relation: relations) {
|
---|
[6776] | 298 | if (newOrUndeleted ? !relation.isNewOrUndeleted() : !relation.isDeleted()) {
|
---|
[2512] | 299 | continue;
|
---|
| 300 | }
|
---|
| 301 | this.relations.add(relation);
|
---|
| 302 | for (RelationMember m: relation.getMembers()) {
|
---|
[6776] | 303 | if (m.isRelation() && (newOrUndeleted ? m.getMember().isNewOrUndeleted() : m.getMember().isDeleted())) {
|
---|
[8510] | 304 | addDependency(relation, (Relation) m.getMember());
|
---|
[2512] | 305 | }
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | public Set<Relation> getChildren(Relation relation) {
|
---|
[12865] | 311 | return children.computeIfAbsent(relation, k -> new HashSet<>());
|
---|
[2512] | 312 | }
|
---|
| 313 |
|
---|
| 314 | public void addDependency(Relation relation, Relation child) {
|
---|
| 315 | getChildren(relation).add(child);
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[8856] | 318 | protected void visit(Stack<Relation> path, Relation current) throws CyclicUploadDependencyException {
|
---|
[2512] | 319 | if (path.contains(current)) {
|
---|
| 320 | path.push(current);
|
---|
| 321 | throw new CyclicUploadDependencyException(path);
|
---|
| 322 | }
|
---|
| 323 | if (!visited.contains(current)) {
|
---|
| 324 | path.push(current);
|
---|
| 325 | visited.add(current);
|
---|
| 326 | for (Relation dependent : getChildren(current)) {
|
---|
[8510] | 327 | visit(path, dependent);
|
---|
[2512] | 328 | }
|
---|
| 329 | uploadOrder.add(current);
|
---|
| 330 | path.pop();
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[13018] | 334 | public List<Relation> computeUploadOrder(boolean reverse) throws CyclicUploadDependencyException {
|
---|
[7005] | 335 | visited = new HashSet<>();
|
---|
| 336 | uploadOrder = new LinkedList<>();
|
---|
[8856] | 337 | Stack<Relation> path = new Stack<>();
|
---|
[2512] | 338 | for (Relation relation: relations) {
|
---|
| 339 | visit(path, relation);
|
---|
| 340 | }
|
---|
[7005] | 341 | List<Relation> ret = new ArrayList<>(relations);
|
---|
[13018] | 342 | Comparator<? super Relation> cmpr = Comparator.comparingInt(uploadOrder::indexOf);
|
---|
| 343 | if (reverse) {
|
---|
| 344 | cmpr = cmpr.reversed();
|
---|
| 345 | }
|
---|
| 346 | ret.sort(cmpr);
|
---|
[2512] | 347 | return ret;
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 | }
|
---|