Search in sources :

Example 16 with NodeRef

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

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

the class WriteTree2 method handleDeletedTrees.

private void handleDeletedTrees(TreeDifference treeDifference, Set<String> ignoreList) {
    SortedSet<NodeRef> deletes = treeDifference.findDeletes();
    for (NodeRef ref : deletes) {
        String path = ref.path();
        if (ignoreList.contains(path)) {
            continue;
        }
        ignoreList.add(path);
        if (!filterMatchesOrIsParent(path)) {
            if (filterApplies(path, treeDifference.getRightTree())) {
                // can't optimize
                RevTree newTree = applyChanges(ref, null);
                Node newNode = Node.tree(ref.name(), newTree.getId(), ref.getMetadataId());
                MutableTree leftTree = treeDifference.getLeftTree();
                leftTree.forceChild(ref.getParentPath(), newNode);
            }
        } else {
            MutableTree leftTree = treeDifference.getLeftTree();
            leftTree.removeChild(path);
        }
    }
}
Also used : MutableTree(org.locationtech.geogig.api.plumbing.diff.MutableTree) NodeRef(org.locationtech.geogig.api.NodeRef) Node(org.locationtech.geogig.api.Node) RevTree(org.locationtech.geogig.api.RevTree)

Example 18 with NodeRef

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

the class WriteTree2 method handleNewTrees.

private void handleNewTrees(TreeDifference treeDifference, Set<String> ignoreList) {
    SortedSet<NodeRef> newTrees = treeDifference.findNewTrees();
    for (NodeRef ref : newTrees) {
        final String path = ref.path();
        if (ignoreList.contains(path)) {
            continue;
        }
        ignoreList.add(path);
        if (!filterMatchesOrIsParent(path)) {
            MutableTree rightTree = treeDifference.getRightTree();
            if (filterApplies(path, rightTree)) {
                // can't optimize
                RevTree newTree = applyChanges(null, ref);
                Node newNode = Node.tree(ref.name(), newTree.getId(), ref.getMetadataId());
                MutableTree leftTree = treeDifference.getLeftTree();
                leftTree.forceChild(ref.getParentPath(), newNode);
            }
        } else {
            LOGGER.trace("Creating new tree {}", path);
            deepMove(ref.getNode());
            MutableTree leftTree = treeDifference.getLeftTree();
            String parentPath = ref.getParentPath();
            Node node = ref.getNode();
            leftTree.setChild(parentPath, node);
        }
    }
}
Also used : MutableTree(org.locationtech.geogig.api.plumbing.diff.MutableTree) NodeRef(org.locationtech.geogig.api.NodeRef) Node(org.locationtech.geogig.api.Node) RevTree(org.locationtech.geogig.api.RevTree)

Example 19 with NodeRef

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

the class WriteTree2 method handlePureMetadataChanges.

private void handlePureMetadataChanges(TreeDifference treeDifference, Set<String> ignoreList) {
    Map<NodeRef, NodeRef> pureMetadataChanges = treeDifference.findPureMetadataChanges();
    for (Map.Entry<NodeRef, NodeRef> e : pureMetadataChanges.entrySet()) {
        NodeRef newValue = e.getValue();
        String treePath = newValue.path();
        if (ignoreList.contains(treePath)) {
            continue;
        }
        ignoreList.add(treePath);
        if (!filterMatchesOrIsParent(treePath)) {
            // filter doesn't apply to the changed tree
            continue;
        }
        deepMove(newValue.getMetadataId());
        MutableTree leftTree = treeDifference.getLeftTree();
        leftTree.setChild(newValue.getParentPath(), newValue.getNode());
    }
}
Also used : MutableTree(org.locationtech.geogig.api.plumbing.diff.MutableTree) NodeRef(org.locationtech.geogig.api.NodeRef) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 20 with NodeRef

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

the class FindOrCreateSubtree method _call.

/**
     * Executes the command.
     * 
     * @return the subtree if it was found, or a new one if it wasn't
     */
@Override
protected RevTree _call() {
    checkNotNull(parentSupplier, "parent");
    checkNotNull(childPath, "childPath");
    ObjectId subtreeId;
    if (parentSupplier.get().isPresent()) {
        RevTree parent = parentSupplier.get().get();
        Optional<NodeRef> treeChildRef = command(FindTreeChild.class).setIndex(indexDb).setParentPath(parentPath).setChildPath(childPath).setParent(Suppliers.ofInstance(parent)).call();
        if (treeChildRef.isPresent()) {
            NodeRef treeRef = treeChildRef.get();
            if (!TYPE.TREE.equals(treeRef.getType())) {
                throw new IllegalArgumentException("Object exists as child of tree " + parent.getId() + " but is not a tree: " + treeChildRef);
            }
            subtreeId = treeRef.objectId();
        } else {
            subtreeId = RevTree.EMPTY_TREE_ID;
        }
    } else {
        subtreeId = RevTree.EMPTY_TREE_ID;
    }
    if (RevTree.EMPTY_TREE_ID.equals(subtreeId)) {
        return RevTree.EMPTY;
    }
    ObjectDatabase target = indexDb ? stagingDatabase() : objectDatabase();
    RevTree tree = target.getTree(subtreeId);
    return tree;
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) ObjectId(org.locationtech.geogig.api.ObjectId) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) RevTree(org.locationtech.geogig.api.RevTree)

Aggregations

NodeRef (org.locationtech.geogig.api.NodeRef)161 ObjectId (org.locationtech.geogig.api.ObjectId)91 RevTree (org.locationtech.geogig.api.RevTree)67 Test (org.junit.Test)62 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)40 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)27 RevFeature (org.locationtech.geogig.api.RevFeature)25 Node (org.locationtech.geogig.api.Node)24 RevTreeBuilder (org.locationtech.geogig.api.RevTreeBuilder)24 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)23 FindTreeChild (org.locationtech.geogig.api.plumbing.FindTreeChild)22 RevObject (org.locationtech.geogig.api.RevObject)21 RevCommit (org.locationtech.geogig.api.RevCommit)19 Map (java.util.Map)15 SimpleFeature (org.opengis.feature.simple.SimpleFeature)15 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)14 Feature (org.opengis.feature.Feature)13 Optional (com.google.common.base.Optional)12 GeoGIG (org.locationtech.geogig.api.GeoGIG)11 LsTreeOp (org.locationtech.geogig.api.plumbing.LsTreeOp)11