Search in sources :

Example 1 with RevFeature

use of org.locationtech.geogig.api.RevFeature 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 2 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class WorkingTree method putInDatabase.

/**
     * Adds a single feature to the staging database.
     * 
     * @param feature the feature to add
     * @param metadataId
     * @return the Node for the inserted feature
     */
private Node putInDatabase(final Feature feature, final ObjectId metadataId) {
    checkNotNull(feature);
    checkNotNull(metadataId);
    final RevFeature newFeature = RevFeatureBuilder.build(feature);
    final ObjectId objectId = newFeature.getId();
    final Envelope bounds = (ReferencedEnvelope) feature.getBounds();
    final String nodeName = feature.getIdentifier().getID();
    indexDatabase.put(newFeature);
    Node newObject = Node.create(nodeName, objectId, metadataId, TYPE.FEATURE, bounds);
    return newObject;
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) ObjectId(org.locationtech.geogig.api.ObjectId) Node(org.locationtech.geogig.api.Node) RevFeature(org.locationtech.geogig.api.RevFeature) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Envelope(com.vividsolutions.jts.geom.Envelope)

Example 3 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class MergeOpTest method testMergeConflictingBranches.

@Test
public void testMergeConflictingBranches() throws Exception {
    // Create the following revision graph
    // o
    // |
    // o - Points 1,2 added
    // |\
    // | o - TestBranch - Points 1 modified, 2 removed, 3 added
    // |
    // o - master - HEAD - Points 1 modifiedB, 2 removed
    insertAndAdd(points1, points2);
    geogig.command(CommitOp.class).call();
    geogig.command(BranchCreateOp.class).setName("TestBranch").call();
    Feature points1Modified = feature(pointsType, idP1, "StringProp1_2", new Integer(1000), "POINT(1 1)");
    insert(points1Modified);
    delete(points2);
    insert(points3);
    geogig.command(AddOp.class).call();
    RevCommit masterCommit = geogig.command(CommitOp.class).call();
    geogig.command(CheckoutOp.class).setSource("TestBranch").call();
    Feature points1ModifiedB = feature(pointsType, idP1, "StringProp1_3", new Integer(2000), "POINT(1 1)");
    insert(points1ModifiedB);
    delete(points2);
    geogig.command(AddOp.class).call();
    RevCommit branchCommit = geogig.command(CommitOp.class).call();
    // Now try to merge branch into master
    geogig.command(CheckoutOp.class).setSource("master").call();
    Ref branch = geogig.command(RefParse.class).setName("TestBranch").call().get();
    try {
        geogig.command(MergeOp.class).addCommit(Suppliers.ofInstance(branch.getObjectId())).call();
        fail();
    } catch (MergeConflictsException e) {
        assertTrue(e.getMessage().contains("conflict"));
    }
    Optional<Ref> ref = geogig.command(RefParse.class).setName(Ref.ORIG_HEAD).call();
    assertTrue(ref.isPresent());
    assertEquals(masterCommit.getId(), ref.get().getObjectId());
    ref = geogig.command(RefParse.class).setName(Ref.MERGE_HEAD).call();
    assertTrue(ref.isPresent());
    assertEquals(branch.getObjectId(), ref.get().getObjectId());
    String msg = geogig.command(ReadMergeCommitMessageOp.class).call();
    assertFalse(Strings.isNullOrEmpty(msg));
    List<Conflict> conflicts = geogig.command(ConflictsReadOp.class).call();
    assertEquals(1, conflicts.size());
    String path = NodeRef.appendChild(pointsName, idP1);
    assertEquals(conflicts.get(0).getPath(), path);
    assertEquals(conflicts.get(0).getOurs(), RevFeatureBuilder.build(points1Modified).getId());
    assertEquals(conflicts.get(0).getTheirs(), RevFeatureBuilder.build(points1ModifiedB).getId());
    // try to commit
    try {
        geogig.command(CommitOp.class).call();
        fail();
    } catch (IllegalStateException e) {
        assertEquals(e.getMessage(), "Cannot run operation while merge conflicts exist.");
    }
    // solve, and commit
    Feature points1Merged = feature(pointsType, idP1, "StringProp1_2", new Integer(2000), "POINT(1 1)");
    insert(points1Merged);
    geogig.command(AddOp.class).call();
    RevCommit commit = geogig.command(CommitOp.class).call();
    assertTrue(commit.getMessage().contains(idP1));
    List<ObjectId> parents = commit.getParentIds();
    assertEquals(2, parents.size());
    assertEquals(masterCommit.getId(), parents.get(0));
    assertEquals(branchCommit.getId(), parents.get(1));
    Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec(Ref.HEAD + ":" + path).call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    assertEquals(RevFeatureBuilder.build(points1Merged), revFeature.get());
    path = NodeRef.appendChild(pointsName, idP2);
    revFeature = geogig.command(RevObjectParse.class).setRefSpec(Ref.HEAD + ":" + path).call(RevFeature.class);
    assertFalse(revFeature.isPresent());
    path = NodeRef.appendChild(pointsName, idP3);
    revFeature = geogig.command(RevObjectParse.class).setRefSpec(Ref.HEAD + ":" + path).call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    ref = geogig.command(RefParse.class).setName(Ref.MERGE_HEAD).call();
    assertFalse(ref.isPresent());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) ConflictsReadOp(org.locationtech.geogig.api.plumbing.merge.ConflictsReadOp) ObjectId(org.locationtech.geogig.api.ObjectId) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) RevFeature(org.locationtech.geogig.api.RevFeature) Feature(org.opengis.feature.Feature) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) Ref(org.locationtech.geogig.api.Ref) NodeRef(org.locationtech.geogig.api.NodeRef) MergeConflictsException(org.locationtech.geogig.api.porcelain.MergeConflictsException) Conflict(org.locationtech.geogig.api.plumbing.merge.Conflict) RevFeature(org.locationtech.geogig.api.RevFeature) RefParse(org.locationtech.geogig.api.plumbing.RefParse) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) ReadMergeCommitMessageOp(org.locationtech.geogig.api.plumbing.merge.ReadMergeCommitMessageOp) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 4 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class MergeOpTest method testMergeWithPolygonAutoMerge.

@Test
public void testMergeWithPolygonAutoMerge() throws Exception {
    String polyId = "polyId";
    String polygonTypeSpec = "poly:Polygon:srid=4326";
    SimpleFeatureType polygonType = DataUtilities.createType("http://geogig.polygon", "polygons", polygonTypeSpec);
    Feature polygonOriginal = feature(polygonType, polyId, "POLYGON((0 0,1 0,2 0,3 0,4 0,5 0,5 1,4 1,3 1,2 1,1 1,1 0,0 0))");
    insertAndAdd(polygonOriginal);
    geogig.command(CommitOp.class).call();
    geogig.command(BranchCreateOp.class).setName("TestBranch").call();
    Feature polygonMaster = feature(polygonType, polyId, "POLYGON((0 0,1 0,2 0.2,3 0.2,4 0,5 0,5 1,4 1,3 1,2 1,1 1,1 0,0 0))");
    insertAndAdd(polygonMaster);
    geogig.command(CommitOp.class).call();
    geogig.command(CheckoutOp.class).setSource("TestBranch").call();
    Feature polygonBranch = feature(polygonType, polyId, "POLYGON((0 0,1 0,2 0,3 0,4 0,5 0,5 1,4 1,3 0.8,2 0.8,1 1,1 0,0 0))");
    insertAndAdd(polygonBranch);
    geogig.command(CommitOp.class).call();
    geogig.command(CheckoutOp.class).setSource("master").call();
    Ref branch = geogig.command(RefParse.class).setName("TestBranch").call().get();
    geogig.command(MergeOp.class).addCommit(Suppliers.ofInstance(branch.getObjectId())).call();
    Optional<RevFeature> feature = repo.command(RevObjectParse.class).setRefSpec("WORK_HEAD:polygons/polyId").call(RevFeature.class);
    assertTrue(feature.isPresent());
    RevFeature merged = feature.get();
    Feature expected = feature(polygonType, polyId, "POLYGON((0 0,1 0,2 0.2,3 0.2,4 0,5 0,5 1,4 1,3 0.8,2 0.8,1 1,1 0,0 0))");
    assertEquals(expected.getProperty("poly").getValue(), merged.getValues().get(0).get());
}
Also used : UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) Ref(org.locationtech.geogig.api.Ref) NodeRef(org.locationtech.geogig.api.NodeRef) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevFeature(org.locationtech.geogig.api.RevFeature) RefParse(org.locationtech.geogig.api.plumbing.RefParse) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) RevFeature(org.locationtech.geogig.api.RevFeature) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 5 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class MergeOpTest method testMergeWithFeatureMerge.

@Test
public void testMergeWithFeatureMerge() throws Exception {
    // Create the following revision graph
    // o
    // |
    // o - Points 1 added
    // |\
    // | o - TestBranch - Points 1 modified and points 2 added
    // |
    // o - master - HEAD - Points 1 modifiedB
    insertAndAdd(points1);
    geogig.command(CommitOp.class).call();
    geogig.command(BranchCreateOp.class).setName("TestBranch").call();
    Feature points1Modified = feature(pointsType, idP1, "StringProp1_2", new Integer(1000), "POINT(1 1)");
    insertAndAdd(points1Modified);
    geogig.command(CommitOp.class).call();
    geogig.command(CheckoutOp.class).setSource("TestBranch").call();
    Feature points1ModifiedB = feature(pointsType, idP1, "StringProp1_1", new Integer(2000), "POINT(1 1)");
    insertAndAdd(points1ModifiedB);
    insertAndAdd(points2);
    geogig.command(CommitOp.class).call();
    geogig.command(CheckoutOp.class).setSource("master").call();
    Ref branch = geogig.command(RefParse.class).setName("TestBranch").call().get();
    geogig.command(MergeOp.class).addCommit(Suppliers.ofInstance(branch.getObjectId())).call();
    String path = appendChild(pointsName, points1.getIdentifier().getID());
    Optional<RevFeature> feature = repo.command(RevObjectParse.class).setRefSpec(/*
                             * mergeCommit. getId ().toString () + ":" +
                             */
    "WORK_HEAD" + ":" + path).call(RevFeature.class);
    assertTrue(feature.isPresent());
    Feature mergedFeature = feature(pointsType, idP1, "StringProp1_2", new Integer(2000), "POINT(1 1)");
    RevFeature expected = RevFeatureBuilder.build(mergedFeature);
    assertEquals(expected, feature.get());
}
Also used : UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) Ref(org.locationtech.geogig.api.Ref) NodeRef(org.locationtech.geogig.api.NodeRef) RevFeature(org.locationtech.geogig.api.RevFeature) RefParse(org.locationtech.geogig.api.plumbing.RefParse) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) RevFeature(org.locationtech.geogig.api.RevFeature) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Aggregations

RevFeature (org.locationtech.geogig.api.RevFeature)89 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)46 Test (org.junit.Test)45 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)43 Optional (com.google.common.base.Optional)40 NodeRef (org.locationtech.geogig.api.NodeRef)32 File (java.io.File)24 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)24 ObjectId (org.locationtech.geogig.api.ObjectId)21 AddOp (org.locationtech.geogig.api.porcelain.AddOp)21 ImmutableList (com.google.common.collect.ImmutableList)20 Feature (org.opengis.feature.Feature)20 List (java.util.List)19 RevObject (org.locationtech.geogig.api.RevObject)18 RevTree (org.locationtech.geogig.api.RevTree)16 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)16 SimpleFeature (org.opengis.feature.simple.SimpleFeature)16 ArrayList (java.util.ArrayList)15 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)15 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)15