Search in sources :

Example 1 with FeatureDiff

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

the class DefaultStepDefinitions method I_have_a_patch_file.

@Given("^I have a patch file$")
public void I_have_a_patch_file() throws Throwable {
    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);
    File file = new File(platform.pwd(), "test.patch");
    BufferedWriter writer = Files.newWriter(file, Charsets.UTF_8);
    PatchSerializer.write(writer, patch);
    writer.flush();
    writer.close();
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) File(java.io.File) BufferedWriter(java.io.BufferedWriter) Given(cucumber.annotation.en.Given)

Example 2 with FeatureDiff

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

the class DiffTree method runInternal.

/**
     * Executes the diff-tree command with the specified options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    if (refSpec.size() > 2) {
        throw new CommandFailedException("Tree refspecs list is too long :" + refSpec);
    }
    if (treeStats && describe) {
        throw new CommandFailedException("Cannot use --describe and --tree-stats simultaneously");
    }
    GeoGIG geogig = cli.getGeogig();
    org.locationtech.geogig.api.plumbing.DiffTree diff = geogig.command(org.locationtech.geogig.api.plumbing.DiffTree.class);
    String oldVersion = resolveOldVersion();
    String newVersion = resolveNewVersion();
    diff.setOldVersion(oldVersion).setNewVersion(newVersion);
    Iterator<DiffEntry> diffEntries;
    if (paths.isEmpty()) {
        diffEntries = diff.setProgressListener(cli.getProgressListener()).call();
    } else {
        diffEntries = Iterators.emptyIterator();
        for (String path : paths) {
            Iterator<DiffEntry> moreEntries = diff.setPathFilter(path).setProgressListener(cli.getProgressListener()).call();
            diffEntries = Iterators.concat(diffEntries, moreEntries);
        }
    }
    DiffEntry diffEntry;
    HashMap<String, Long[]> stats = Maps.newHashMap();
    while (diffEntries.hasNext()) {
        diffEntry = diffEntries.next();
        StringBuilder sb = new StringBuilder();
        String path = diffEntry.newPath() != null ? diffEntry.newPath() : diffEntry.oldPath();
        if (describe) {
            sb.append(diffEntry.changeType().toString().charAt(0)).append(' ').append(path).append(LINE_BREAK);
            if (diffEntry.changeType() == ChangeType.MODIFIED) {
                FeatureDiff featureDiff = geogig.command(DiffFeature.class).setNewVersion(Suppliers.ofInstance(diffEntry.getNewObject())).setOldVersion(Suppliers.ofInstance(diffEntry.getOldObject())).call();
                Map<PropertyDescriptor, AttributeDiff> diffs = featureDiff.getDiffs();
                HashSet<PropertyDescriptor> diffDescriptors = Sets.newHashSet(diffs.keySet());
                NodeRef noderef = diffEntry.changeType() != ChangeType.REMOVED ? diffEntry.getNewObject() : diffEntry.getOldObject();
                RevFeatureType featureType = geogig.command(RevObjectParse.class).setObjectId(noderef.getMetadataId()).call(RevFeatureType.class).get();
                Optional<RevObject> obj = geogig.command(RevObjectParse.class).setObjectId(noderef.objectId()).call();
                RevFeature feature = (RevFeature) obj.get();
                ImmutableList<Optional<Object>> values = feature.getValues();
                ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
                int idx = 0;
                for (PropertyDescriptor descriptor : descriptors) {
                    if (diffs.containsKey(descriptor)) {
                        AttributeDiff ad = diffs.get(descriptor);
                        sb.append(ad.getType().toString().charAt(0) + " " + descriptor.getName().toString() + LINE_BREAK);
                        if (!ad.getType().equals(TYPE.ADDED)) {
                            Object value = ad.getOldValue().orNull();
                            sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                            sb.append(LINE_BREAK);
                        }
                        if (!ad.getType().equals(TYPE.REMOVED)) {
                            Object value = ad.getNewValue().orNull();
                            sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                            sb.append(LINE_BREAK);
                        }
                        diffDescriptors.remove(descriptor);
                    } else {
                        sb.append("U ").append(descriptor.getName().toString()).append(LINE_BREAK);
                        sb.append(TextValueSerializer.asString(values.get(idx))).append(LINE_BREAK);
                    }
                    idx++;
                }
                for (PropertyDescriptor descriptor : diffDescriptors) {
                    AttributeDiff ad = diffs.get(descriptor);
                    sb.append(ad.getType().toString().charAt(0) + " " + descriptor.getName().toString() + LINE_BREAK);
                    if (!ad.getType().equals(TYPE.ADDED)) {
                        Object value = ad.getOldValue().orNull();
                        sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                        sb.append(LINE_BREAK);
                    }
                    if (!ad.getType().equals(TYPE.REMOVED)) {
                        Object value = ad.getNewValue().orNull();
                        sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                        sb.append(LINE_BREAK);
                    }
                }
            } else {
                NodeRef noderef = diffEntry.changeType() == ChangeType.ADDED ? diffEntry.getNewObject() : diffEntry.getOldObject();
                RevFeatureType featureType = geogig.command(RevObjectParse.class).setObjectId(noderef.getMetadataId()).call(RevFeatureType.class).get();
                Optional<RevObject> obj = geogig.command(RevObjectParse.class).setObjectId(noderef.objectId()).call();
                RevFeature feature = (RevFeature) obj.get();
                ImmutableList<Optional<Object>> values = feature.getValues();
                int i = 0;
                for (Optional<Object> value : values) {
                    sb.append(diffEntry.changeType().toString().charAt(0));
                    sb.append(' ');
                    sb.append(featureType.sortedDescriptors().get(i).getName().toString());
                    sb.append(LINE_BREAK);
                    sb.append(TextValueSerializer.asString(value));
                    sb.append(LINE_BREAK);
                    i++;
                }
                sb.append(LINE_BREAK);
            }
            sb.append(LINE_BREAK);
            cli.getConsole().println(sb.toString());
        } else if (treeStats) {
            String parent = NodeRef.parentPath(path);
            if (!stats.containsKey(parent)) {
                stats.put(parent, new Long[] { 0l, 0l, 0l });
            }
            Long[] counts = stats.get(parent);
            if (diffEntry.changeType() == ChangeType.ADDED) {
                counts[0]++;
            } else if (diffEntry.changeType() == ChangeType.REMOVED) {
                counts[1]++;
            } else if (diffEntry.changeType() == ChangeType.MODIFIED) {
                counts[2]++;
            }
        } else {
            sb.append(path).append(' ');
            sb.append(diffEntry.oldObjectId().toString());
            sb.append(' ');
            sb.append(diffEntry.newObjectId().toString());
            cli.getConsole().println(sb.toString());
        }
    }
    if (treeStats) {
        for (String path : stats.keySet()) {
            StringBuffer sb = new StringBuffer();
            sb.append(path);
            Long[] counts = stats.get(path);
            for (int i = 0; i < counts.length; i++) {
                sb.append(" " + counts[i].toString());
            }
            cli.getConsole().println(sb.toString());
        }
    }
}
Also used : CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) NodeRef(org.locationtech.geogig.api.NodeRef) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) RevObject(org.locationtech.geogig.api.RevObject) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 3 with FeatureDiff

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

the class ApplyPatchOpTest method testPartialApplication.

@Test
public void testPartialApplication() throws Exception {
    insert(points1, points2);
    Patch patch = new Patch();
    String pathRemove = NodeRef.appendChild(pointsName, points2.getIdentifier().getID());
    patch.addRemovedFeature(pathRemove, points2, RevFeatureTypeImpl.build(pointsType));
    String pathModify = 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(pathModify, map, RevFeatureTypeImpl.build(modifiedPointsType), RevFeatureTypeImpl.build(pointsType));
    patch.addModifiedFeature(featureDiff);
    Patch rejected = geogig.command(ApplyPatchOp.class).setPatch(patch).setApplyPartial(true).call();
    assertFalse(rejected.isEmpty());
    RevTree root = repo.workingTree().getTree();
    assertNotNull(root);
    Optional<Node> featureBlobId = findTreeChild(root, pathRemove);
    assertFalse(featureBlobId.isPresent());
    // now we take the rejected patch and apply it, and the new rejected should be identical to
    // it
    Patch newRejected = geogig.command(ApplyPatchOp.class).setPatch(rejected).setApplyPartial(true).call();
    assertEquals(rejected, newRejected);
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) Node(org.locationtech.geogig.api.Node) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) ApplyPatchOp(org.locationtech.geogig.api.porcelain.ApplyPatchOp) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Example 4 with FeatureDiff

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

the class ApplyPatchOpTest method testModifiedFeatureDoesNotExists.

@Test
public void testModifiedFeatureDoesNotExists() throws Exception {
    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 featureDiff = new FeatureDiff(path, map, RevFeatureTypeImpl.build(pointsType), RevFeatureTypeImpl.build(pointsType));
    patch.addModifiedFeature(featureDiff);
    try {
        geogig.command(ApplyPatchOp.class).setPatch(patch).call();
        fail();
    } catch (CannotApplyPatchException e) {
        assertTrue(true);
    }
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) CannotApplyPatchException(org.locationtech.geogig.api.porcelain.CannotApplyPatchException) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) Test(org.junit.Test)

Example 5 with FeatureDiff

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

the class ApplyPatchOpTest method testModifyFeatureAttributeOutdatedPatch.

@Test
public void testModifyFeatureAttributeOutdatedPatch() throws Exception {
    insert(points1_modified);
    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);
    try {
        geogig.command(ApplyPatchOp.class).setPatch(patch).call();
        fail();
    } catch (CannotApplyPatchException e) {
        assertTrue(true);
    }
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) CannotApplyPatchException(org.locationtech.geogig.api.porcelain.CannotApplyPatchException) 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