Search in sources :

Example 6 with RevTreeBuilder

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

the class WriteTree method _call.

/**
     * Executes the write tree operation.
     * 
     * @return the new root tree id, the current HEAD tree id if there are no differences between
     *         the index and the HEAD, or {@code null} if the operation has been cancelled (as
     *         indicated by the {@link #getProgressListener() progress listener}.
     */
@Override
protected ObjectId _call() {
    final ProgressListener progress = getProgressListener();
    final RevTree oldRootTree = resolveRootTree();
    final ObjectDatabase repositoryDatabase = objectDatabase();
    Iterator<DiffEntry> diffs = null;
    long numChanges = 0;
    if (diffSupplier == null) {
        diffs = index().getStaged(pathFilters);
        numChanges = index().countStaged(pathFilters).count();
    } else {
        diffs = diffSupplier.get();
    }
    if (!diffs.hasNext()) {
        return oldRootTree.getId();
    }
    if (progress.isCanceled()) {
        return null;
    }
    Map<String, RevTreeBuilder> repositoryChangedTrees = Maps.newHashMap();
    Map<String, NodeRef> indexChangedTrees = Maps.newHashMap();
    Map<String, ObjectId> changedTreesMetadataId = Maps.newHashMap();
    Set<String> deletedTrees = Sets.newHashSet();
    final boolean moveObjects = this.moveObjects;
    NodeRef ref;
    int i = 0;
    RevTree stageHead = index().getTree();
    while (diffs.hasNext()) {
        if (numChanges != 0) {
            progress.setProgress((float) (++i * 100) / numChanges);
        }
        if (progress.isCanceled()) {
            return null;
        }
        DiffEntry diff = diffs.next();
        // ignore the root entry
        if (NodeRef.ROOT.equals(diff.newName()) || NodeRef.ROOT.equals(diff.oldName())) {
            continue;
        }
        ref = diff.getNewObject();
        if (ref == null) {
            ref = diff.getOldObject();
        }
        final String parentPath = ref.getParentPath();
        final boolean isDelete = ChangeType.REMOVED.equals(diff.changeType());
        final TYPE type = ref.getType();
        if (isDelete && deletedTrees.contains(parentPath)) {
            // tree delete entry was processed
            continue;
        }
        RevTreeBuilder parentTree = resolveTargetTree(oldRootTree, parentPath, repositoryChangedTrees, changedTreesMetadataId, ObjectId.NULL, repositoryDatabase);
        if (type == TYPE.TREE && !isDelete) {
            // cache the tree
            resolveTargetTree(oldRootTree, ref.name(), repositoryChangedTrees, changedTreesMetadataId, ref.getMetadataId(), repositoryDatabase);
        }
        resolveSourceTreeRef(parentPath, indexChangedTrees, changedTreesMetadataId, stageHead);
        Preconditions.checkState(parentTree != null);
        if (isDelete) {
            String oldName = diff.getOldObject().getNode().getName();
            parentTree.remove(oldName);
            if (TYPE.TREE.equals(type)) {
                deletedTrees.add(ref.path());
            }
        } else {
            if (moveObjects && ref.getType().equals(TYPE.TREE)) {
                RevTree tree = stagingDatabase().getTree(ref.objectId());
                if (!ref.getMetadataId().isNull()) {
                    repositoryDatabase.put(stagingDatabase().getFeatureType(ref.getMetadataId()));
                }
                if (tree.isEmpty()) {
                    repositoryDatabase.put(tree);
                } else {
                    continue;
                }
            } else if (moveObjects) {
                deepMove(ref.getNode());
            }
            parentTree.put(ref.getNode());
        }
    }
    if (progress.isCanceled()) {
        return null;
    }
    // now write back all changed trees
    ObjectId newTargetRootId = oldRootTree.getId();
    RevTreeBuilder directRootEntries = repositoryChangedTrees.remove(NodeRef.ROOT);
    if (directRootEntries != null) {
        RevTree newRoot = directRootEntries.build();
        repositoryDatabase.put(newRoot);
        newTargetRootId = newRoot.getId();
    }
    for (Map.Entry<String, RevTreeBuilder> e : repositoryChangedTrees.entrySet()) {
        String treePath = e.getKey();
        ObjectId metadataId = changedTreesMetadataId.get(treePath);
        RevTreeBuilder treeBuilder = e.getValue();
        RevTree newRoot = getTree(newTargetRootId);
        RevTree tree = treeBuilder.build();
        newTargetRootId = writeBack(newRoot.builder(repositoryDatabase), tree, treePath, metadataId);
    }
    progress.complete();
    return newTargetRootId;
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) NodeRef(org.locationtech.geogig.api.NodeRef) ProgressListener(org.locationtech.geogig.api.ProgressListener) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) TYPE(org.locationtech.geogig.api.RevObject.TYPE) Map(java.util.Map) RevTree(org.locationtech.geogig.api.RevTree) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 7 with RevTreeBuilder

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

the class CatObjectTest method createTree.

private RevTree createTree(int numChildren) {
    RevTreeBuilder rtb = new RevTreeBuilder(odb);
    for (int i = 0; i < numChildren; i++) {
        String key = FEATURE_PREFIX + i;
        Node ref = Node.create(key, FAKE_ID, FAKE_ID, TYPE.FEATURE, null);
        rtb.put(ref);
    }
    return rtb.build();
}
Also used : Node(org.locationtech.geogig.api.Node) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder)

Example 8 with RevTreeBuilder

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

the class RevTreeBuilderTest method createTree.

private RevTreeBuilder createTree(final int numEntries, final boolean insertInAscendingKeyOrder) {
    RevTreeBuilder tree = new RevTreeBuilder(odb);
    final int increment = insertInAscendingKeyOrder ? 1 : -1;
    final int from = insertInAscendingKeyOrder ? 0 : numEntries - 1;
    final int breakAt = insertInAscendingKeyOrder ? numEntries : -1;
    int c = 0;
    for (int i = from; i != breakAt; i += increment, c++) {
        addNode(tree, i);
        if (numEntries > 100) {
            if ((c + 1) % (numEntries / 10) == 0) {
                System.err.print("#" + (c + 1));
            } else if ((c + 1) % (numEntries / 100) == 0) {
                System.err.print('.');
            }
        }
    }
    System.err.print('\n');
    return tree;
}
Also used : RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder)

Example 9 with RevTreeBuilder

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

the class RevTreeBuilderTest method testPutRandomGet.

@Test
public void testPutRandomGet() throws Exception {
    final int numEntries = 2 * RevTree.NORMALIZED_SIZE_LIMIT + 1500;
    final 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.reset().start();
    final RevTree tree = odb.getTree(treeId);
    sw.stop();
    System.err.println("Retrieved tree in " + sw);
    {
        Map<Integer, Node> randomEdits = Maps.newHashMap();
        Random randGen = new Random();
        for (int i = 0; i < tree.size() / 2; i++) {
            int random;
            while (randomEdits.containsKey(random = randGen.nextInt(numEntries))) {
                // $codepro.audit.disable extraSemicolon
                ;
            }
            String name = "Feature." + random;
            ObjectId newid = ObjectId.forString(name + "changed");
            Node ref = Node.create(name, newid, ObjectId.NULL, TYPE.FEATURE, null);
            randomEdits.put(random, ref);
        }
        RevTreeBuilder mutable = tree.builder(odb);
        sw.reset().start();
        for (Node ref : randomEdits.values()) {
            mutable.put(ref);
        }
        mutable.build();
        sw.stop();
        System.err.println(randomEdits.size() + " random modifications in " + sw);
    }
    // CharSequence treeStr =
    // repo.command(CatObject.class).setObject(Suppliers.ofInstance(tree))
    // .call();
    // System.out.println(treeStr);
    final FindTreeChild childFinder = repo.command(FindTreeChild.class).setParent(tree);
    sw.reset().start();
    System.err.println("Reading " + numEntries + " entries....");
    for (int i = 0; i < numEntries; i++) {
        if ((i + 1) % (numEntries / 10) == 0) {
            System.err.print("#" + (i + 1));
        } else if ((i + 1) % (numEntries / 100) == 0) {
            System.err.print('.');
        }
        String key = "Feature." + i;
        // ObjectId oid = ObjectId.forString(key);
        Optional<NodeRef> ref = childFinder.setChildPath(key).call();
        assertTrue(key, ref.isPresent());
    // assertEquals(key, ref.get().getPath());
    // assertEquals(key, oid, ref.get().getObjectId());
    }
    sw.stop();
    System.err.println("\nGot " + numEntries + " in " + sw.elapsed(TimeUnit.MILLISECONDS) + "ms (" + Math.round(numEntries / (sw.elapsed(TimeUnit.MILLISECONDS) / 1000D)) + "/s)\n");
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) Random(java.util.Random) ObjectId(org.locationtech.geogig.api.ObjectId) Node(org.locationtech.geogig.api.Node) Stopwatch(com.google.common.base.Stopwatch) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) Map(java.util.Map) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Example 10 with RevTreeBuilder

use of org.locationtech.geogig.api.RevTreeBuilder 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)

Aggregations

RevTreeBuilder (org.locationtech.geogig.api.RevTreeBuilder)58 RevTree (org.locationtech.geogig.api.RevTree)47 ObjectId (org.locationtech.geogig.api.ObjectId)27 Test (org.junit.Test)25 NodeRef (org.locationtech.geogig.api.NodeRef)24 Node (org.locationtech.geogig.api.Node)14 DepthSearch (org.locationtech.geogig.repository.DepthSearch)6 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)5 Stopwatch (com.google.common.base.Stopwatch)4 Envelope (com.vividsolutions.jts.geom.Envelope)4 Map (java.util.Map)4 Context (org.locationtech.geogig.api.Context)4 FindTreeChild (org.locationtech.geogig.api.plumbing.FindTreeChild)4 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)4 ObjectDatabase (org.locationtech.geogig.storage.ObjectDatabase)4 Before (org.junit.Before)3 FindOrCreateSubtree (org.locationtech.geogig.api.plumbing.FindOrCreateSubtree)3 WriteBack (org.locationtech.geogig.api.plumbing.WriteBack)3 DepthTreeIterator (org.locationtech.geogig.api.plumbing.diff.DepthTreeIterator)3 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)3