Search in sources :

Example 16 with FeatureDiff

use of org.locationtech.geogig.api.plumbing.diff.FeatureDiff in project GeoGig by boundlessgeo.

the class ReportMergeScenarioOp method _call.

@Override
protected MergeScenarioReport _call() {
    Optional<ObjectId> ancestor = command(FindCommonAncestor.class).setLeft(toMerge).setRight(mergeInto).call();
    Preconditions.checkState(ancestor.isPresent(), "No ancestor commit could be found.");
    Map<String, DiffEntry> mergeIntoDiffs = Maps.newHashMap();
    MergeScenarioReport report = new MergeScenarioReport();
    Iterator<DiffEntry> diffs = command(DiffTree.class).setOldTree(ancestor.get()).setReportTrees(true).setNewTree(mergeInto.getId()).call();
    while (diffs.hasNext()) {
        DiffEntry diff = diffs.next();
        String path = diff.oldPath() == null ? diff.newPath() : diff.oldPath();
        mergeIntoDiffs.put(path, diff);
    }
    Iterator<DiffEntry> toMergeDiffs = command(DiffTree.class).setOldTree(ancestor.get()).setReportTrees(true).setNewTree(toMerge.getId()).call();
    while (toMergeDiffs.hasNext()) {
        DiffEntry toMergeDiff = toMergeDiffs.next();
        String path = toMergeDiff.oldPath() == null ? toMergeDiff.newPath() : toMergeDiff.oldPath();
        if (mergeIntoDiffs.containsKey(path)) {
            RevCommit ancestorCommit = command(RevObjectParse.class).setRefSpec(ancestor.get().toString()).call(RevCommit.class).get();
            RevTree ancestorTree = command(RevObjectParse.class).setObjectId(ancestorCommit.getTreeId()).call(RevTree.class).get();
            Optional<NodeRef> ancestorVersion = command(FindTreeChild.class).setChildPath(path).setParent(ancestorTree).call();
            ObjectId ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getNode().getObjectId() : ObjectId.NULL;
            ObjectId theirs = toMergeDiff.getNewObject() == null ? ObjectId.NULL : toMergeDiff.getNewObject().objectId();
            DiffEntry mergeIntoDiff = mergeIntoDiffs.get(path);
            ObjectId ours = mergeIntoDiff.getNewObject() == null ? ObjectId.NULL : mergeIntoDiff.getNewObject().objectId();
            if (!mergeIntoDiff.changeType().equals(toMergeDiff.changeType())) {
                report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
                continue;
            }
            switch(toMergeDiff.changeType()) {
                case ADDED:
                    if (toMergeDiff.getNewObject().equals(mergeIntoDiff.getNewObject())) {
                    // already added in current branch, no need to do anything
                    } else {
                        TYPE type = command(ResolveObjectType.class).setObjectId(toMergeDiff.getNewObject().objectId()).call();
                        if (TYPE.TREE.equals(type)) {
                            boolean conflict = !toMergeDiff.getNewObject().getMetadataId().equals(mergeIntoDiff.getNewObject().getMetadataId());
                            if (conflict) {
                                // In this case, we store the metadata id, not the element id
                                ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getMetadataId() : ObjectId.NULL;
                                ours = mergeIntoDiff.getNewObject().getMetadataId();
                                theirs = toMergeDiff.getNewObject().getMetadataId();
                                report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
                            }
                        // if the metadata ids match, it means both branches have added the same
                        // tree, maybe with different content, but there is no need to do
                        // anything. The correct tree is already there and the merge can be run
                        // safely, so we do not add it neither as a conflicted change nor as an
                        // unconflicted one
                        } else {
                            report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
                        }
                    }
                    break;
                case REMOVED:
                    // removed by both histories => no conflict and no need to do anything
                    break;
                case MODIFIED:
                    TYPE type = command(ResolveObjectType.class).setObjectId(toMergeDiff.getNewObject().objectId()).call();
                    if (TYPE.TREE.equals(type)) {
                        boolean conflict = !toMergeDiff.getNewObject().getMetadataId().equals(mergeIntoDiff.getNewObject().getMetadataId());
                        if (conflict) {
                            // In this case, we store the metadata id, not the element id
                            ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getMetadataId() : ObjectId.NULL;
                            ours = mergeIntoDiff.getNewObject().getMetadataId();
                            theirs = toMergeDiff.getNewObject().getMetadataId();
                            report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
                        }
                    } else {
                        FeatureDiff toMergeFeatureDiff = command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(toMergeDiff.getOldObject())).setNewVersion(Suppliers.ofInstance(toMergeDiff.getNewObject())).call();
                        FeatureDiff mergeIntoFeatureDiff = command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(mergeIntoDiff.getOldObject())).setNewVersion(Suppliers.ofInstance(mergeIntoDiff.getNewObject())).call();
                        if (toMergeFeatureDiff.conflicts(mergeIntoFeatureDiff)) {
                            report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
                        } else {
                            // try to perform automerge
                            if (!toMergeDiff.getNewObject().getMetadataId().equals(mergeIntoDiff.getNewObject().getMetadataId())) {
                                report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
                            } else if (!toMergeFeatureDiff.equals(mergeIntoFeatureDiff)) {
                                Feature mergedFeature = command(MergeFeaturesOp.class).setFirstFeature(mergeIntoDiff.getNewObject()).setSecondFeature(toMergeDiff.getNewObject()).setAncestorFeature(mergeIntoDiff.getOldObject()).call();
                                RevFeature revFeature = RevFeatureBuilder.build(mergedFeature);
                                if (revFeature.getId().equals(toMergeDiff.newObjectId())) {
                                    // the resulting merged feature equals the feature to merge from
                                    // the branch, which means that it exists in the repo and there
                                    // is no need to add it
                                    report.addUnconflicted(toMergeDiff);
                                } else {
                                    RevFeatureType featureType = command(RevObjectParse.class).setObjectId(mergeIntoDiff.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
                                    FeatureInfo merged = new FeatureInfo(mergedFeature, featureType, path);
                                    report.addMerged(merged);
                                }
                            }
                        }
                    }
                    break;
            }
        } else {
            // modified in the other branch under it.
            if (ChangeType.REMOVED.equals(toMergeDiff.changeType())) {
                TYPE type = command(ResolveObjectType.class).setObjectId(toMergeDiff.oldObjectId()).call();
                if (TYPE.TREE.equals(type)) {
                    String parentPath = toMergeDiff.oldPath();
                    Set<Entry<String, DiffEntry>> entries = mergeIntoDiffs.entrySet();
                    boolean conflict = false;
                    for (Entry<String, DiffEntry> entry : entries) {
                        if (entry.getKey().startsWith(parentPath)) {
                            if (!ChangeType.REMOVED.equals(entry.getValue().changeType())) {
                                RevCommit ancestorCommit = command(RevObjectParse.class).setRefSpec(ancestor.get().toString()).call(RevCommit.class).get();
                                RevTree ancestorTree = command(RevObjectParse.class).setObjectId(ancestorCommit.getTreeId()).call(RevTree.class).get();
                                Optional<NodeRef> ancestorVersion = command(FindTreeChild.class).setChildPath(path).setParent(ancestorTree).call();
                                ObjectId ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getNode().getObjectId() : ObjectId.NULL;
                                ObjectId theirs = toMergeDiff.getNewObject() == null ? ObjectId.NULL : toMergeDiff.getNewObject().objectId();
                                String oursRefSpec = mergeInto.getId().toString() + ":" + parentPath;
                                Optional<ObjectId> ours = command(RevParse.class).setRefSpec(oursRefSpec).call();
                                report.addConflict(new Conflict(path, ancestorVersionId, ours.get(), theirs));
                                conflict = true;
                                break;
                            }
                        }
                    }
                    if (!conflict) {
                        report.addUnconflicted(toMergeDiff);
                    }
                } else {
                    report.addUnconflicted(toMergeDiff);
                }
            } else {
                report.addUnconflicted(toMergeDiff);
            }
        }
    }
    return report;
}
Also used : FeatureInfo(org.locationtech.geogig.api.FeatureInfo) RevFeature(org.locationtech.geogig.api.RevFeature) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) Feature(org.opengis.feature.Feature) NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Entry(java.util.Map.Entry) RevFeature(org.locationtech.geogig.api.RevFeature) TYPE(org.locationtech.geogig.api.RevObject.TYPE) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) RevCommit(org.locationtech.geogig.api.RevCommit) ObjectId(org.locationtech.geogig.api.ObjectId) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) FindCommonAncestor(org.locationtech.geogig.api.plumbing.FindCommonAncestor) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevTree(org.locationtech.geogig.api.RevTree)

Example 17 with FeatureDiff

use of org.locationtech.geogig.api.plumbing.diff.FeatureDiff in project GeoGig by boundlessgeo.

the class FeatureDiffWeb method run.

/**
     * Runs the command and builds the appropriate response
     * 
     * @param context - the context to use for this command
     * 
     * @throws CommandSpecException
     */
@Override
public void run(CommandContext context) {
    if (path == null || path.trim().isEmpty()) {
        throw new CommandSpecException("No path for feature name specifed");
    }
    final Context geogig = this.getCommandLocator(context);
    ObjectId newId = geogig.command(ResolveTreeish.class).setTreeish(newTreeish).call().get();
    ObjectId oldId = geogig.command(ResolveTreeish.class).setTreeish(oldTreeish).call().get();
    RevFeature newFeature = null;
    RevFeatureType newFeatureType = null;
    RevFeature oldFeature = null;
    RevFeatureType oldFeatureType = null;
    final Map<PropertyDescriptor, AttributeDiff> diffs;
    Optional<NodeRef> ref = parseID(newId, geogig);
    Optional<RevObject> object;
    // need these to determine if the feature was added or removed so I can build the diffs
    // myself until the FeatureDiff supports null values
    boolean removed = false;
    boolean added = false;
    if (ref.isPresent()) {
        object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call();
        if (object.isPresent() && object.get() instanceof RevFeatureType) {
            newFeatureType = (RevFeatureType) object.get();
        } else {
            throw new CommandSpecException("Couldn't resolve newCommit's featureType");
        }
        object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call();
        if (object.isPresent() && object.get() instanceof RevFeature) {
            newFeature = (RevFeature) object.get();
        } else {
            throw new CommandSpecException("Couldn't resolve newCommit's feature");
        }
    } else {
        removed = true;
    }
    if (!oldId.equals(ObjectId.NULL)) {
        ref = parseID(oldId, geogig);
        if (ref.isPresent()) {
            object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call();
            if (object.isPresent() && object.get() instanceof RevFeatureType) {
                oldFeatureType = (RevFeatureType) object.get();
            } else {
                throw new CommandSpecException("Couldn't resolve oldCommit's featureType");
            }
            object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call();
            if (object.isPresent() && object.get() instanceof RevFeature) {
                oldFeature = (RevFeature) object.get();
            } else {
                throw new CommandSpecException("Couldn't resolve oldCommit's feature");
            }
        } else {
            added = true;
        }
    } else {
        added = true;
    }
    if (removed) {
        Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>();
        ImmutableList<PropertyDescriptor> attributes = oldFeatureType.sortedDescriptors();
        ImmutableList<Optional<Object>> values = oldFeature.getValues();
        for (int index = 0; index < attributes.size(); index++) {
            Optional<Object> value = values.get(index);
            if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) {
                Optional<Geometry> temp = Optional.absent();
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(Optional.fromNullable((Geometry) value.orNull()), temp));
                }
            } else {
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(value, Optional.absent()));
                }
            }
        }
        diffs = tempDiffs;
    } else if (added) {
        Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>();
        ImmutableList<PropertyDescriptor> attributes = newFeatureType.sortedDescriptors();
        ImmutableList<Optional<Object>> values = newFeature.getValues();
        for (int index = 0; index < attributes.size(); index++) {
            Optional<Object> value = values.get(index);
            if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) {
                Optional<Geometry> temp = Optional.absent();
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(temp, Optional.fromNullable((Geometry) value.orNull())));
                }
            } else {
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(Optional.absent(), value));
                }
            }
        }
        diffs = tempDiffs;
    } else {
        FeatureDiff diff = new FeatureDiff(path, newFeature, oldFeature, newFeatureType, oldFeatureType, all);
        diffs = diff.getDiffs();
    }
    context.setResponseContent(new CommandResponse() {

        @Override
        public void write(ResponseWriter out) throws Exception {
            out.start();
            out.writeFeatureDiffResponse(diffs);
            out.finish();
        }
    });
}
Also used : HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) ResolveTreeish(org.locationtech.geogig.api.plumbing.ResolveTreeish) RevFeature(org.locationtech.geogig.api.RevFeature) GeometryAttributeDiff(org.locationtech.geogig.api.plumbing.diff.GeometryAttributeDiff) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) GeometryAttributeDiff(org.locationtech.geogig.api.plumbing.diff.GeometryAttributeDiff) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) Geometry(com.vividsolutions.jts.geom.Geometry) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with FeatureDiff

use of org.locationtech.geogig.api.plumbing.diff.FeatureDiff in project GeoGig by boundlessgeo.

the class ApplyPatchOpTest method testReversedPatch.

@Test
public void testReversedPatch() throws Exception {
    insert(points1, points2);
    Patch patch = new Patch();
    String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID());
    Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
    Optional<?> oldValue = Optional.fromNullable(points1.getProperty("sp").getValue());
    GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, Optional.of("new"));
    map.put(pointsType.getDescriptor("sp"), diff);
    FeatureDiff feaureDiff = new FeatureDiff(path, map, RevFeatureTypeImpl.build(pointsType), RevFeatureTypeImpl.build(pointsType));
    patch.addModifiedFeature(feaureDiff);
    String removedPath = NodeRef.appendChild(pointsName, points2.getIdentifier().getID());
    patch.addRemovedFeature(removedPath, points2, RevFeatureTypeImpl.build(pointsType));
    String addedPath = NodeRef.appendChild(pointsName, points3.getIdentifier().getID());
    patch.addAddedFeature(addedPath, points3, RevFeatureTypeImpl.build(pointsType));
    geogig.command(ApplyPatchOp.class).setPatch(patch).call();
    geogig.command(ApplyPatchOp.class).setPatch(patch.reversed()).call();
    RevTree root = repo.workingTree().getTree();
    Optional<Node> featureBlobId = findTreeChild(root, removedPath);
    assertTrue(featureBlobId.isPresent());
    featureBlobId = findTreeChild(root, addedPath);
    assertFalse(featureBlobId.isPresent());
    Optional<RevFeature> feature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + path).call(RevFeature.class);
    assertTrue(feature.isPresent());
    assertEquals(oldValue, feature.get().getValues().get(0));
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) Node(org.locationtech.geogig.api.Node) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Example 19 with FeatureDiff

use of org.locationtech.geogig.api.plumbing.diff.FeatureDiff in project GeoGig by boundlessgeo.

the class ApplyPatchOpTest method testRemoveFeatureAttributePatch.

@Test
public void testRemoveFeatureAttributePatch() throws Exception {
    insert(points1B);
    Patch patch = new Patch();
    String path = NodeRef.appendChild(pointsName, points1B.getIdentifier().getID());
    Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
    Optional<?> oldValue = Optional.fromNullable(points1B.getProperty("extra").getValue());
    GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, null);
    map.put(modifiedPointsType.getDescriptor("extra"), diff);
    FeatureDiff featureDiff = new FeatureDiff(path, map, RevFeatureTypeImpl.build(modifiedPointsType), RevFeatureTypeImpl.build(pointsType));
    patch.addModifiedFeature(featureDiff);
    geogig.command(ApplyPatchOp.class).setPatch(patch).call();
    Optional<RevFeature> feature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + path).call(RevFeature.class);
    assertTrue(feature.isPresent());
    ImmutableList<Optional<Object>> values = feature.get().getValues();
    assertEquals(points1.getProperties().size(), values.size());
    assertFalse(values.contains("ExtraString"));
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) Test(org.junit.Test)

Aggregations

FeatureDiff (org.locationtech.geogig.api.plumbing.diff.FeatureDiff)19 AttributeDiff (org.locationtech.geogig.api.plumbing.diff.AttributeDiff)15 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)15 Test (org.junit.Test)11 GenericAttributeDiffImpl (org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl)11 Patch (org.locationtech.geogig.api.plumbing.diff.Patch)11 RevFeature (org.locationtech.geogig.api.RevFeature)10 NodeRef (org.locationtech.geogig.api.NodeRef)8 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)7 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)7 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)6 Optional (com.google.common.base.Optional)5 ObjectId (org.locationtech.geogig.api.ObjectId)5 RevTree (org.locationtech.geogig.api.RevTree)5 DiffFeature (org.locationtech.geogig.api.plumbing.DiffFeature)5 RevObject (org.locationtech.geogig.api.RevObject)4 CannotApplyPatchException (org.locationtech.geogig.api.porcelain.CannotApplyPatchException)4 Entry (java.util.Map.Entry)3 Node (org.locationtech.geogig.api.Node)3 TYPE (org.locationtech.geogig.api.RevObject.TYPE)3