Search in sources :

Example 1 with Bounded

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

the class LocalRemoteRepo method copyNewObjects.

private void copyNewObjects(RevTree oldTree, RevTree newTree, final ObjectDatabase fromDb, final ObjectDatabase toDb, final ProgressListener progress) {
    checkNotNull(oldTree);
    checkNotNull(newTree);
    checkNotNull(fromDb);
    checkNotNull(toDb);
    checkNotNull(progress);
    // the diff walk uses fromDb as both left and right data source since we're comparing what
    // we have in the "origin" database against trees on the same repository
    PostOrderDiffWalk diffWalk = new PostOrderDiffWalk(oldTree, newTree, fromDb, fromDb);
    // holds object ids that need to be copied to the target db. Pruned when it reaches a
    // threshold.
    final Set<ObjectId> ids = new HashSet<ObjectId>();
    // This filter further refines the post order diff walk by making it ignore trees/buckets
    // that are already present in the target db
    Predicate<Bounded> filter = new Predicate<Bounded>() {

        @Override
        public boolean apply(@Nullable Bounded b) {
            if (b == null) {
                return false;
            }
            if (progress.isCanceled()) {
                // abort traversal
                return false;
            }
            ObjectId id;
            if (b instanceof Node) {
                Node node = (Node) b;
                if (RevObject.TYPE.TREE.equals(node.getType())) {
                    // check of existence of trees only. For features the diff filtering is good
                    // enough and checking for existence on each feature would be killer
                    // performance wise
                    id = node.getObjectId();
                } else {
                    return true;
                }
            } else {
                id = ((Bucket) b).id();
            }
            boolean exists = ids.contains(id) || toDb.exists(id);
            return !exists;
        }
    };
    // receives notifications of feature/bucket/tree diffs. Only interested in the "new"/right
    // side of the comparisons
    Consumer consumer = new Consumer() {

        final int bulkSize = 10_000;

        @Override
        public void feature(@Nullable Node left, Node right) {
            add(left);
            add(right);
        }

        @Override
        public void tree(@Nullable Node left, Node right) {
            add(left);
            add(right);
        }

        private void add(@Nullable Node node) {
            if (node == null) {
                return;
            }
            ids.add(node.getObjectId());
            Optional<ObjectId> metadataId = node.getMetadataId();
            if (metadataId.isPresent()) {
                ids.add(metadataId.get());
            }
            checkLimitAndCopy();
        }

        @Override
        public void bucket(int bucketIndex, int bucketDepth, @Nullable Bucket left, Bucket right) {
            if (left != null) {
                ids.add(left.id());
            }
            if (right != null) {
                ids.add(right.id());
            }
            checkLimitAndCopy();
        }

        private void checkLimitAndCopy() {
            if (ids.size() >= bulkSize) {
                copy(ids, fromDb, toDb, progress);
                ids.clear();
            }
        }
    };
    diffWalk.walk(filter, consumer);
    // copy remaining objects
    copy(ids, fromDb, toDb, progress);
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) Node(org.locationtech.geogig.api.Node) Predicate(com.google.common.base.Predicate) Bounded(org.locationtech.geogig.api.Bounded) Consumer(org.locationtech.geogig.api.plumbing.diff.PostOrderDiffWalk.Consumer) Bucket(org.locationtech.geogig.api.Bucket) PostOrderDiffWalk(org.locationtech.geogig.api.plumbing.diff.PostOrderDiffWalk) Nullable(javax.annotation.Nullable) HashSet(java.util.HashSet)

Example 2 with Bounded

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

the class OSMExport method getFeatures.

private Iterator<EntityContainer> getFeatures(String ref) {
    Optional<ObjectId> id = geogig.command(RevParse.class).setRefSpec(ref).call();
    if (!id.isPresent()) {
        return Iterators.emptyIterator();
    }
    LsTreeOp op = geogig.command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
    if (bbox != null) {
        final Envelope env;
        try {
            env = new Envelope(Double.parseDouble(bbox.get(0)), Double.parseDouble(bbox.get(2)), Double.parseDouble(bbox.get(1)), Double.parseDouble(bbox.get(3)));
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Wrong bbox definition");
        }
        Predicate<Bounded> filter = new Predicate<Bounded>() {

            @Override
            public boolean apply(final Bounded bounded) {
                boolean intersects = bounded.intersects(env);
                return intersects;
            }
        };
        op.setBoundsFilter(filter);
    }
    Iterator<NodeRef> iterator = op.call();
    final EntityConverter converter = new EntityConverter();
    Function<NodeRef, EntityContainer> function = new Function<NodeRef, EntityContainer>() {

        @Override
        @Nullable
        public EntityContainer apply(@Nullable NodeRef ref) {
            RevFeature revFeature = geogig.command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
            SimpleFeatureType featureType;
            if (ref.path().startsWith(OSMUtils.NODE_TYPE_NAME)) {
                featureType = OSMUtils.nodeType();
            } else {
                featureType = OSMUtils.wayType();
            }
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
            RevFeatureType revFeatureType = RevFeatureTypeImpl.build(featureType);
            List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
            ImmutableList<Optional<Object>> values = revFeature.getValues();
            for (int i = 0; i < descriptors.size(); i++) {
                PropertyDescriptor descriptor = descriptors.get(i);
                Optional<Object> value = values.get(i);
                featureBuilder.set(descriptor.getName(), value.orNull());
            }
            SimpleFeature feature = featureBuilder.buildFeature(ref.name());
            Entity entity = converter.toEntity(feature, null);
            EntityContainer container;
            if (entity instanceof Node) {
                container = new NodeContainer((Node) entity);
            } else {
                container = new WayContainer((Way) entity);
            }
            return container;
        }
    };
    return Iterators.transform(iterator, function);
}
Also used : EntityConverter(org.locationtech.geogig.osm.internal.EntityConverter) Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) WayContainer(org.openstreetmap.osmosis.core.container.v0_6.WayContainer) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) NodeContainer(org.openstreetmap.osmosis.core.container.v0_6.NodeContainer) Envelope(com.vividsolutions.jts.geom.Envelope) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) Predicate(com.google.common.base.Predicate) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) Bounded(org.locationtech.geogig.api.Bounded) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 3 with Bounded

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

the class RevTreeSerializationTest method assertTreesAreEqual.

private void assertTreesAreEqual(RevTree a, RevTree b) {
    assertTrue(a.getId().equals(b.getId()));
    assertTrue(a.buckets().equals(b.buckets()));
    assertTrue(a.features().equals(b.features()));
    assertTrue(a.trees().equals(b.trees()));
    assertTrue(a.numTrees() == b.numTrees());
    assertTrue(a.size() == b.size());
    Iterator<? extends Bounded> ia;
    Iterator<? extends Bounded> ib;
    if (a.buckets().isPresent()) {
        ia = a.buckets().get().values().iterator();
        ib = b.buckets().get().values().iterator();
    } else {
        ia = a.children();
        ib = b.children();
    }
    // bounds are not part of the Bounded.equals(Object) contract as its auxiliary information
    while (ia.hasNext()) {
        Bounded ba = ia.next();
        Bounded bb = ib.next();
        Envelope ea = new Envelope();
        Envelope eb = new Envelope();
        ba.expand(ea);
        bb.expand(eb);
        assertEquals(ea.getMinX(), eb.getMinX(), 1e-7D);
        assertEquals(ea.getMinY(), eb.getMinY(), 1e-7D);
        assertEquals(ea.getMaxX(), eb.getMaxX(), 1e-7D);
        assertEquals(ea.getMaxY(), eb.getMaxY(), 1e-7D);
    }
}
Also used : Bounded(org.locationtech.geogig.api.Bounded) Envelope(com.vividsolutions.jts.geom.Envelope)

Example 4 with Bounded

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

the class PostOrderDiffWalkTest method testLeafLeafWithSubStrees.

@Test
public void testLeafLeafWithSubStrees() {
    // two leaf trees
    ObjectId metadataId = ObjectId.forString("fake");
    RevTree left = createTreesTree(leftSource, 2, 2, metadataId).build();
    RevTree right = createTreesTree(rightSource, 3, 2, metadataId).build();
    PostOrderDiffWalk visitor = new PostOrderDiffWalk(left, right, leftSource, rightSource);
    visitor.walk(testConsumer);
    List<Bounded> leftCalls = testConsumer.orderedLeft;
    List<Bounded> rightCalls = testConsumer.orderedRight;
    System.err.println(leftCalls);
    System.err.println(rightCalls);
    Node lroot = nodeFor(left);
    Node rroot = nodeFor(right);
    assertEquals(4, leftCalls.size());
    assertEquals(4, rightCalls.size());
    assertNull(leftCalls.get(0));
    assertNull(leftCalls.get(1));
    assertNull(leftCalls.get(2));
    assertEquals(lroot, leftCalls.get(3));
    assertEquals(rroot, rightCalls.get(3));
    assertNotNull(rightCalls.get(2));
    assertEquals(RevObject.TYPE.TREE, ((Node) rightCalls.get(2)).getType());
    assertEquals(RevObject.TYPE.FEATURE, ((Node) rightCalls.get(1)).getType());
    assertEquals(RevObject.TYPE.FEATURE, ((Node) rightCalls.get(0)).getType());
}
Also used : Bounded(org.locationtech.geogig.api.Bounded) ObjectId(org.locationtech.geogig.api.ObjectId) Node(org.locationtech.geogig.api.Node) TreeTestSupport.featureNode(org.locationtech.geogig.api.plumbing.diff.TreeTestSupport.featureNode) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Example 5 with Bounded

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

the class PostOrderDiffWalkTest method testBucketBucketFlat.

@Test
public void testBucketBucketFlat() {
    RevTree left = createFeaturesTree(leftSource, "f", RevTree.NORMALIZED_SIZE_LIMIT + 1).build();
    RevTree right = createFeaturesTree(rightSource, "f", RevTree.NORMALIZED_SIZE_LIMIT + 2).build();
    PostOrderDiffWalk visitor = new PostOrderDiffWalk(left, right, leftSource, rightSource);
    visitor.walk(testConsumer);
    List<Bounded> leftCalls = testConsumer.orderedLeft;
    List<Bounded> rightCalls = testConsumer.orderedRight;
    // System.err.println(leftCalls);
    // System.err.println(rightCalls);
    Node lroot = nodeFor(left);
    Node rroot = nodeFor(right);
    assertEquals(3, leftCalls.size());
    assertEquals(3, rightCalls.size());
    assertNull(leftCalls.get(0));
    assertTrue(leftCalls.get(1) instanceof Bucket);
    assertEquals(lroot, leftCalls.get(2));
    assertEquals(rroot, rightCalls.get(2));
    assertTrue(rightCalls.get(1) instanceof Bucket);
    assertTrue(rightCalls.get(0) instanceof Node);
    assertEquals(RevObject.TYPE.FEATURE, ((Node) rightCalls.get(0)).getType());
}
Also used : Bounded(org.locationtech.geogig.api.Bounded) Bucket(org.locationtech.geogig.api.Bucket) Node(org.locationtech.geogig.api.Node) TreeTestSupport.featureNode(org.locationtech.geogig.api.plumbing.diff.TreeTestSupport.featureNode) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Aggregations

Bounded (org.locationtech.geogig.api.Bounded)6 Node (org.locationtech.geogig.api.Node)4 ObjectId (org.locationtech.geogig.api.ObjectId)4 Predicate (com.google.common.base.Predicate)3 Bucket (org.locationtech.geogig.api.Bucket)3 RevTree (org.locationtech.geogig.api.RevTree)3 Envelope (com.vividsolutions.jts.geom.Envelope)2 Nullable (javax.annotation.Nullable)2 Test (org.junit.Test)2 NodeRef (org.locationtech.geogig.api.NodeRef)2 TreeTestSupport.featureNode (org.locationtech.geogig.api.plumbing.diff.TreeTestSupport.featureNode)2 Function (com.google.common.base.Function)1 Optional (com.google.common.base.Optional)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)1 RevFeature (org.locationtech.geogig.api.RevFeature)1 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)1 RevTreeBuilder (org.locationtech.geogig.api.RevTreeBuilder)1 LsTreeOp (org.locationtech.geogig.api.plumbing.LsTreeOp)1