Search in sources :

Example 6 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 7 with FeatureDiff

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

the class ApplyPatchOp method applyPatch.

private void applyPatch(Patch patch) {
    final WorkingTree workTree = workingTree();
    final StagingDatabase indexDb = stagingDatabase();
    if (reverse) {
        patch = patch.reversed();
    }
    List<FeatureInfo> removed = patch.getRemovedFeatures();
    for (FeatureInfo feature : removed) {
        workTree.delete(NodeRef.parentPath(feature.getPath()), NodeRef.nodeFromPath(feature.getPath()));
    }
    List<FeatureInfo> added = patch.getAddedFeatures();
    for (FeatureInfo feature : added) {
        workTree.insert(NodeRef.parentPath(feature.getPath()), feature.getFeature());
    }
    List<FeatureDiff> diffs = patch.getModifiedFeatures();
    for (FeatureDiff diff : diffs) {
        String path = diff.getPath();
        DepthSearch depthSearch = new DepthSearch(indexDb);
        Optional<NodeRef> noderef = depthSearch.find(workTree.getTree(), path);
        RevFeatureType oldRevFeatureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
        String refSpec = Ref.WORK_HEAD + ":" + path;
        RevFeature feature = command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class).get();
        RevFeatureType newRevFeatureType = getFeatureType(diff, feature, oldRevFeatureType);
        ImmutableList<Optional<Object>> values = feature.getValues();
        ImmutableList<PropertyDescriptor> oldDescriptors = oldRevFeatureType.sortedDescriptors();
        ImmutableList<PropertyDescriptor> newDescriptors = newRevFeatureType.sortedDescriptors();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) newRevFeatureType.type());
        Map<Name, Optional<?>> attrs = Maps.newHashMap();
        for (int i = 0; i < oldDescriptors.size(); i++) {
            PropertyDescriptor descriptor = oldDescriptors.get(i);
            if (newDescriptors.contains(descriptor)) {
                Optional<Object> value = values.get(i);
                attrs.put(descriptor.getName(), value);
            }
        }
        Set<Entry<PropertyDescriptor, AttributeDiff>> featureDiffs = diff.getDiffs().entrySet();
        for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = featureDiffs.iterator(); iterator.hasNext(); ) {
            Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
            if (!entry.getValue().getType().equals(TYPE.REMOVED)) {
                Optional<?> oldValue = attrs.get(entry.getKey().getName());
                attrs.put(entry.getKey().getName(), entry.getValue().applyOn(oldValue));
            }
        }
        Set<Entry<Name, Optional<?>>> entries = attrs.entrySet();
        for (Iterator<Entry<Name, Optional<?>>> iterator = entries.iterator(); iterator.hasNext(); ) {
            Entry<Name, Optional<?>> entry = iterator.next();
            featureBuilder.set(entry.getKey(), entry.getValue().orNull());
        }
        SimpleFeature featureToInsert = featureBuilder.buildFeature(NodeRef.nodeFromPath(path));
        workTree.insert(NodeRef.parentPath(path), featureToInsert);
    }
    ImmutableList<FeatureTypeDiff> alteredTrees = patch.getAlteredTrees();
    for (FeatureTypeDiff diff : alteredTrees) {
        Optional<RevFeatureType> featureType;
        if (diff.getOldFeatureType().isNull()) {
            featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
            workTree.createTypeTree(diff.getPath(), featureType.get().type());
        } else if (diff.getNewFeatureType().isNull()) {
            workTree.delete(diff.getPath());
        } else {
            featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
            workTree.updateTypeTree(diff.getPath(), featureType.get().type());
        }
    }
}
Also used : FeatureInfo(org.locationtech.geogig.api.FeatureInfo) Name(org.opengis.feature.type.Name) WorkingTree(org.locationtech.geogig.repository.WorkingTree) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) NodeRef(org.locationtech.geogig.api.NodeRef) Entry(java.util.Map.Entry) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) StagingDatabase(org.locationtech.geogig.storage.StagingDatabase) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DepthSearch(org.locationtech.geogig.repository.DepthSearch) FeatureTypeDiff(org.locationtech.geogig.api.plumbing.diff.FeatureTypeDiff) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 8 with FeatureDiff

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

the class BlameOp method _call.

@Override
protected BlameReport _call() {
    String fullPath = (commit != null ? commit.toString() : Ref.HEAD) + ":" + path;
    Optional<ObjectId> id = command(RevParse.class).setRefSpec(fullPath).call();
    if (!id.isPresent()) {
        throw new BlameException(StatusCode.FEATURE_NOT_FOUND);
    }
    TYPE type = command(ResolveObjectType.class).setObjectId(id.get()).call();
    if (!type.equals(TYPE.FEATURE)) {
        throw new BlameException(StatusCode.PATH_NOT_FEATURE);
    }
    Optional<RevFeatureType> featureType = command(ResolveFeatureType.class).setRefSpec(path).call();
    BlameReport report = new BlameReport(featureType.get());
    Iterator<RevCommit> log = command(LogOp.class).addPath(path).setUntil(commit).call();
    RevCommit commit = log.next();
    RevObjectParse revObjectParse = command(RevObjectParse.class);
    DiffOp diffOp = command(DiffOp.class);
    DiffFeature diffFeature = command(DiffFeature.class);
    while (!report.isComplete()) {
        if (!log.hasNext()) {
            String refSpec = commit.getId().toString() + ":" + path;
            RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
            report.setFirstVersion(feature, commit);
            break;
        }
        RevCommit commitB = log.next();
        Iterator<DiffEntry> diffs = diffOp.setNewVersion(commit.getId()).setOldVersion(commitB.getId()).setReportTrees(false).call();
        while (diffs.hasNext()) {
            DiffEntry diff = diffs.next();
            if (path.equals(diff.newPath())) {
                if (diff.isAdd()) {
                    String refSpec = commit.getId().toString() + ":" + path;
                    RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
                    report.setFirstVersion(feature, commit);
                    break;
                }
                FeatureDiff featureDiff = diffFeature.setNewVersion(Suppliers.ofInstance(diff.getNewObject())).setOldVersion(Suppliers.ofInstance(diff.getOldObject())).call();
                Map<PropertyDescriptor, AttributeDiff> attribDiffs = featureDiff.getDiffs();
                Iterator<PropertyDescriptor> iter = attribDiffs.keySet().iterator();
                while (iter.hasNext()) {
                    PropertyDescriptor key = iter.next();
                    Optional<?> value = attribDiffs.get(key).getNewValue();
                    String attribute = key.getName().toString();
                    report.addDiff(attribute, value, commit);
                }
            }
        }
        commit = commitB;
    }
    return report;
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) ObjectId(org.locationtech.geogig.api.ObjectId) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) TYPE(org.locationtech.geogig.api.RevObject.TYPE) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 9 with FeatureDiff

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

the class ApplyPatchOpTest method testAddFeatureAttributeOutdatedPatch.

@Test
public void testAddFeatureAttributeOutdatedPatch() throws Exception {
    insert(points1B);
    Patch patch = new Patch();
    String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID());
    Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
    Optional<?> newValue = Optional.fromNullable(points1B.getProperty("extra").getValue());
    GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(null, newValue);
    map.put(modifiedPointsType.getDescriptor("extra"), diff);
    FeatureDiff featureDiff = new FeatureDiff(path, map, RevFeatureTypeImpl.build(modifiedPointsType), RevFeatureTypeImpl.build(modifiedPointsType));
    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 10 with FeatureDiff

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

the class ApplyPatchOpTest method testModifyFeatureAttributePatch.

@Test
public void testModifyFeatureAttributePatch() throws Exception {
    insert(points1);
    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);
    geogig.command(ApplyPatchOp.class).setPatch(patch).call();
    RevTree root = repo.workingTree().getTree();
    Optional<Node> featureBlobId = findTreeChild(root, path);
    assertTrue(featureBlobId.isPresent());
    Iterator<DiffEntry> unstaged = repo.workingTree().getUnstaged(pointsName);
    ArrayList<DiffEntry> diffs = Lists.newArrayList(unstaged);
    assertEquals(2, diffs.size());
    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("new", values.get(0).get());
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) 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) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) 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