Search in sources :

Example 1 with DepthTreeIterator

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

the class RevTreeBuilderTest method testPutIterate.

@Test
public void testPutIterate() throws Exception {
    final int numEntries = 1000 * 100;
    ObjectId treeId;
    Stopwatch sw;
    sw = Stopwatch.createStarted();
    treeId = createAndSaveTree(numEntries, true);
    sw.stop();
    System.err.println("Stored " + numEntries + " tree entries in " + sw + " (" + Math.round(numEntries / (sw.elapsed(TimeUnit.MILLISECONDS) / 1000D)) + "/s)");
    sw = Stopwatch.createStarted();
    treeId = createAndSaveTree(numEntries, true);
    sw.stop();
    System.err.println("Stored " + numEntries + " tree entries in " + sw + " (" + Math.round(numEntries / (sw.elapsed(TimeUnit.MILLISECONDS) / 1000D)) + "/s)");
    sw.reset().start();
    final RevTree tree = odb.getTree(treeId);
    sw.stop();
    System.err.println("Retrieved tree in " + sw);
    System.err.println("traversing with DepthTreeIterator...");
    sw.reset().start();
    int counted = 0;
    for (DepthTreeIterator it = new DepthTreeIterator("", ObjectId.NULL, tree, odb, Strategy.CHILDREN); it.hasNext(); counted++) {
        NodeRef ref = it.next();
        if ((counted + 1) % (numEntries / 10) == 0) {
            System.err.print("#" + (counted + 1));
        } else if ((counted + 1) % (numEntries / 100) == 0) {
            System.err.print('.');
        }
    }
    sw.stop();
    System.err.println("\nTraversed " + counted + " in " + sw + " (" + Math.round(counted / (sw.elapsed(TimeUnit.MILLISECONDS) / 1000D)) + "/s)\n");
    System.err.println("traversing with DepthTreeIterator...");
    sw.reset().start();
    counted = 0;
    for (DepthTreeIterator it = new DepthTreeIterator("", ObjectId.NULL, tree, odb, Strategy.CHILDREN); it.hasNext(); counted++) {
        NodeRef ref = it.next();
        if ((counted + 1) % (numEntries / 10) == 0) {
            System.err.print("#" + (counted + 1));
        } else if ((counted + 1) % (numEntries / 100) == 0) {
            System.err.print('.');
        }
    }
    sw.stop();
    System.err.println("\nTraversed " + counted + " in " + sw + " (" + Math.round(counted / (sw.elapsed(TimeUnit.MILLISECONDS) / 1000D)) + "/s)\n");
    assertEquals(numEntries, counted);
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) ObjectId(org.locationtech.geogig.api.ObjectId) Stopwatch(com.google.common.base.Stopwatch) DepthTreeIterator(org.locationtech.geogig.api.plumbing.diff.DepthTreeIterator) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Example 2 with DepthTreeIterator

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

the class RevTreeBuilderTest method testResultingTreeSize.

private void testResultingTreeSize(int numEntries) {
    RevTreeBuilder builder = createTree(numEntries, true);
    RevTree tree = builder.build();
    final long declaredSize = tree.size();
    Iterator<NodeRef> it = new DepthTreeIterator("", ObjectId.NULL, tree, odb, Strategy.RECURSIVE_FEATURES_ONLY);
    long itSize = 0;
    while (it.hasNext()) {
        it.next();
        itSize++;
    }
    assertEquals(numEntries, itSize);
    assertEquals(numEntries, declaredSize);
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) DepthTreeIterator(org.locationtech.geogig.api.plumbing.diff.DepthTreeIterator) RevTree(org.locationtech.geogig.api.RevTree)

Example 3 with DepthTreeIterator

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

the class LsTreeOp method _call.

/**
     * @see java.util.concurrent.Callable#call()
     */
protected Iterator<NodeRef> _call() {
    String ref = this.ref;
    if (ref == null) {
        ref = Ref.WORK_HEAD;
    }
    ObjectId metadataId = ObjectId.NULL;
    final String path = ref.lastIndexOf(':') != -1 ? ref.substring(ref.lastIndexOf(':') + 1) : "";
    if (!path.isEmpty()) {
        final String providedRefName = ref.lastIndexOf(':') != -1 ? ref.substring(0, ref.lastIndexOf(':')) : null;
        if (providedRefName != null) {
            Optional<ObjectId> rootTreeId = command(ResolveTreeish.class).setTreeish(providedRefName).call();
            if (rootTreeId.isPresent()) {
                RevTree rootTree = command(RevObjectParse.class).setObjectId(rootTreeId.get()).call(RevTree.class).get();
                Optional<NodeRef> treeRef = command(FindTreeChild.class).setChildPath(path).setIndex(true).setParent(rootTree).call();
                metadataId = treeRef.isPresent() ? treeRef.get().getMetadataId() : ObjectId.NULL;
            }
        }
    }
    // is it just a ref name?
    Optional<Ref> reference = command(RefParse.class).setName(ref).call();
    if (reference.isPresent()) {
        if (reference.get().getObjectId().isNull()) {
            return Iterators.emptyIterator();
        }
    }
    Optional<RevObject> revObject = command(RevObjectParse.class).setRefSpec(ref).call(RevObject.class);
    Optional<NodeRef> treeRef = Optional.absent();
    if (!revObject.isPresent()) {
        if (Ref.WORK_HEAD.equals(ref)) {
            // tree but it is empty
            return Iterators.emptyIterator();
        }
        // let's try to see if it is a feature type or feature in the working tree
        NodeRef.checkValidPath(ref);
        treeRef = command(FindTreeChild.class).setParent(workingTree().getTree()).setChildPath(ref).setIndex(true).call();
        Preconditions.checkArgument(treeRef.isPresent(), "Invalid reference: %s", ref);
        ObjectId treeId = treeRef.get().objectId();
        metadataId = treeRef.get().getMetadataId();
        revObject = command(RevObjectParse.class).setObjectId(treeId).call(RevObject.class);
    }
    checkArgument(revObject.isPresent(), "Invalid reference: %s", ref);
    final TYPE type = revObject.get().getType();
    switch(type) {
        case FEATURE:
            NodeRef nodeRef = treeRef.isPresent() ? treeRef.get() : null;
            List<NodeRef> nodeRefs = Lists.newArrayList();
            nodeRefs.add(nodeRef);
            // If show trees options is passed in show all trees that contain this feature
            if (this.strategy == Strategy.TREES_ONLY) {
                if (nodeRef != null) {
                    while (!nodeRef.getParentPath().isEmpty()) {
                        treeRef = command(FindTreeChild.class).setParent(workingTree().getTree()).setChildPath(nodeRef.getParentPath()).setIndex(true).call();
                        nodeRef = treeRef.get();
                        nodeRefs.add(nodeRef);
                    }
                }
            }
            return nodeRefs.iterator();
        case COMMIT:
            RevCommit revCommit = (RevCommit) revObject.get();
            ObjectId treeId = revCommit.getTreeId();
            revObject = command(RevObjectParse.class).setObjectId(treeId).call(RevObject.class);
        case TREE:
            DepthTreeIterator.Strategy iterStrategy;
            switch(this.strategy) {
                case CHILDREN:
                    iterStrategy = DepthTreeIterator.Strategy.CHILDREN;
                    break;
                case FEATURES_ONLY:
                    iterStrategy = DepthTreeIterator.Strategy.FEATURES_ONLY;
                    break;
                case TREES_ONLY:
                    iterStrategy = DepthTreeIterator.Strategy.TREES_ONLY;
                    break;
                case DEPTHFIRST:
                    iterStrategy = DepthTreeIterator.Strategy.RECURSIVE;
                    break;
                case DEPTHFIRST_ONLY_FEATURES:
                    iterStrategy = DepthTreeIterator.Strategy.RECURSIVE_FEATURES_ONLY;
                    break;
                case DEPTHFIRST_ONLY_TREES:
                    iterStrategy = DepthTreeIterator.Strategy.RECURSIVE_TREES_ONLY;
                    break;
                default:
                    throw new IllegalStateException("Unknown strategy: " + this.strategy);
            }
            RevTree tree = (RevTree) revObject.get();
            ObjectDatabase database = stagingDatabase();
            DepthTreeIterator iter = new DepthTreeIterator(path, metadataId, tree, database, iterStrategy);
            iter.setBoundsFilter(refBoundsFilter);
            return iter;
        default:
            throw new IllegalArgumentException(String.format("Invalid reference: %s", ref));
    }
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) NodeRef(org.locationtech.geogig.api.NodeRef) Ref(org.locationtech.geogig.api.Ref) NodeRef(org.locationtech.geogig.api.NodeRef) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) DepthTreeIterator(org.locationtech.geogig.api.plumbing.diff.DepthTreeIterator) TYPE(org.locationtech.geogig.api.RevObject.TYPE) RevTree(org.locationtech.geogig.api.RevTree) RevCommit(org.locationtech.geogig.api.RevCommit)

Example 4 with DepthTreeIterator

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

the class ExportOp method getFeatures.

private static Iterator<SimpleFeature> getFeatures(final RevTree typeTree, final ObjectDatabase database, final ObjectId defaultMetadataId, final ProgressListener progressListener) {
    Iterator<NodeRef> nodes = new DepthTreeIterator("", defaultMetadataId, typeTree, database, Strategy.FEATURES_ONLY);
    // progress reporting
    nodes = Iterators.transform(nodes, new Function<NodeRef, NodeRef>() {

        private AtomicInteger count = new AtomicInteger();

        @Override
        public NodeRef apply(NodeRef input) {
            progressListener.setProgress((count.incrementAndGet() * 100.f) / typeTree.size());
            return input;
        }
    });
    Function<NodeRef, SimpleFeature> asFeature = new Function<NodeRef, SimpleFeature>() {

        private Map<ObjectId, FeatureBuilder> ftCache = Maps.newHashMap();

        @Override
        @Nullable
        public SimpleFeature apply(final NodeRef input) {
            final ObjectId metadataId = input.getMetadataId();
            final RevFeature revFeature = database.getFeature(input.objectId());
            FeatureBuilder featureBuilder = getBuilderFor(metadataId);
            Feature feature = featureBuilder.build(input.name(), revFeature);
            feature.getUserData().put(Hints.USE_PROVIDED_FID, true);
            feature.getUserData().put(RevFeature.class, revFeature);
            feature.getUserData().put(RevFeatureType.class, featureBuilder.getType());
            if (feature instanceof SimpleFeature) {
                return (SimpleFeature) feature;
            }
            return null;
        }

        private FeatureBuilder getBuilderFor(final ObjectId metadataId) {
            FeatureBuilder featureBuilder = ftCache.get(metadataId);
            if (featureBuilder == null) {
                RevFeatureType revFtype = database.getFeatureType(metadataId);
                featureBuilder = new FeatureBuilder(revFtype);
                ftCache.put(metadataId, featureBuilder);
            }
            return featureBuilder;
        }
    };
    Iterator<SimpleFeature> asFeatures = Iterators.transform(nodes, asFeature);
    UnmodifiableIterator<SimpleFeature> filterNulls = Iterators.filter(asFeatures, Predicates.notNull());
    return filterNulls;
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RevFeature(org.locationtech.geogig.api.RevFeature) DepthTreeIterator(org.locationtech.geogig.api.plumbing.diff.DepthTreeIterator) Map(java.util.Map) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 5 with DepthTreeIterator

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

the class RevTreeBuilderTest method testRemoveSplittedTree.

@Test
public void testRemoveSplittedTree() throws Exception {
    final int numEntries = (int) (1.5 * RevTree.NORMALIZED_SIZE_LIMIT);
    final ObjectId treeId = createAndSaveTree(numEntries, true);
    final RevTree tree = odb.getTree(treeId);
    // collect some keys to remove
    final Set<String> removedKeys = new HashSet<String>();
    {
        int i = 0;
        DepthTreeIterator it = new DepthTreeIterator("", ObjectId.NULL, tree, odb, Strategy.CHILDREN);
        for (; it.hasNext(); i++) {
            NodeRef entry = it.next();
            if (i % 10 == 0) {
                removedKeys.add(entry.path());
            }
        }
        assertTrue(removedKeys.size() > 0);
    }
    RevTreeBuilder builder = tree.builder(odb);
    for (String key : removedKeys) {
        assertTrue(key, builder.get(key).isPresent());
        builder.remove(key);
        assertFalse(key, builder.get(key).isPresent());
    }
    for (String key : removedKeys) {
        assertFalse(builder.get(key).isPresent());
    }
    final RevTree tree2 = builder.build();
    for (String key : removedKeys) {
        assertFalse(key, repo.getTreeChild(tree2, key).isPresent());
    }
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) ObjectId(org.locationtech.geogig.api.ObjectId) DepthTreeIterator(org.locationtech.geogig.api.plumbing.diff.DepthTreeIterator) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) RevTree(org.locationtech.geogig.api.RevTree) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

NodeRef (org.locationtech.geogig.api.NodeRef)6 DepthTreeIterator (org.locationtech.geogig.api.plumbing.diff.DepthTreeIterator)6 ObjectId (org.locationtech.geogig.api.ObjectId)5 RevTree (org.locationtech.geogig.api.RevTree)5 Test (org.junit.Test)3 RevTreeBuilder (org.locationtech.geogig.api.RevTreeBuilder)3 HashSet (java.util.HashSet)2 Function (com.google.common.base.Function)1 Stopwatch (com.google.common.base.Stopwatch)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)1 Ref (org.locationtech.geogig.api.Ref)1 RevCommit (org.locationtech.geogig.api.RevCommit)1 RevFeature (org.locationtech.geogig.api.RevFeature)1 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)1 RevObject (org.locationtech.geogig.api.RevObject)1 TYPE (org.locationtech.geogig.api.RevObject.TYPE)1 ObjectDatabase (org.locationtech.geogig.storage.ObjectDatabase)1 Feature (org.opengis.feature.Feature)1