Search in sources :

Example 16 with RevTree

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

the class WriteBack method writeBack.

private ObjectId writeBack(RevTreeBuilder ancestor, final String ancestorPath, final RevTree childTree, final String childPath, final ObjectDatabase targetDatabase, final ObjectId metadataId) {
    final ObjectId treeId = childTree.getId();
    targetDatabase.put(childTree);
    final boolean isDirectChild = NodeRef.isDirectChild(ancestorPath, childPath);
    if (isDirectChild) {
        Envelope treeBounds = null;
        if (!metadataId.isNull()) {
            // only include bounds for trees with a default feature type
            treeBounds = SpatialOps.boundsOf(childTree);
        }
        String childName = childPath;
        Node treeNode = Node.create(childName, treeId, metadataId, TYPE.TREE, treeBounds);
        ancestor.put(treeNode);
        RevTree newAncestor = ancestor.build();
        targetDatabase.put(newAncestor);
        return newAncestor.getId();
    }
    final String parentPath = NodeRef.parentPath(childPath);
    Optional<NodeRef> parentRef = getTreeChild(ancestor, parentPath);
    RevTreeBuilder parentBuilder;
    ObjectId parentMetadataId = ObjectId.NULL;
    if (parentRef.isPresent()) {
        ObjectId parentId = parentRef.get().objectId();
        parentMetadataId = parentRef.get().getMetadataId();
        parentBuilder = getTree(parentId, targetDatabase).builder(targetDatabase);
    } else {
        parentBuilder = RevTree.EMPTY.builder(targetDatabase);
    }
    String childName = NodeRef.nodeFromPath(childPath);
    Envelope treeBounds = null;
    if (!metadataId.isNull()) {
        // only include bounds for trees with a default feature type
        treeBounds = SpatialOps.boundsOf(childTree);
    }
    Node treeNode = Node.create(childName, treeId, metadataId, TYPE.TREE, treeBounds);
    parentBuilder.put(treeNode);
    RevTree parent = parentBuilder.build();
    return writeBack(ancestor, ancestorPath, parent, parentPath, targetDatabase, parentMetadataId);
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) ObjectId(org.locationtech.geogig.api.ObjectId) Node(org.locationtech.geogig.api.Node) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) Envelope(com.vividsolutions.jts.geom.Envelope) RevTree(org.locationtech.geogig.api.RevTree)

Example 17 with RevTree

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

the class WriteTree method resolveTargetTree.

private RevTreeBuilder resolveTargetTree(final RevTree root, String treePath, Map<String, RevTreeBuilder> treeCache, Map<String, ObjectId> metadataCache, ObjectId fallbackMetadataId, ObjectDatabase repositoryDatabase) {
    RevTreeBuilder treeBuilder = treeCache.get(treePath);
    if (treeBuilder == null) {
        if (NodeRef.ROOT.equals(treePath)) {
            treeBuilder = root.builder(repositoryDatabase);
        } else {
            Optional<NodeRef> treeRef = command(FindTreeChild.class).setIndex(false).setParent(root).setChildPath(treePath).call();
            if (treeRef.isPresent()) {
                metadataCache.put(treePath, treeRef.get().getMetadataId());
                treeBuilder = command(RevObjectParse.class).setObjectId(treeRef.get().objectId()).call(RevTree.class).get().builder(repositoryDatabase);
            } else {
                metadataCache.put(treePath, fallbackMetadataId);
                treeBuilder = new RevTreeBuilder(repositoryDatabase);
            }
        }
        treeCache.put(treePath, treeBuilder);
    }
    return treeBuilder;
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) RevTree(org.locationtech.geogig.api.RevTree)

Example 18 with RevTree

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

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

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

Aggregations

RevTree (org.locationtech.geogig.api.RevTree)214 Test (org.junit.Test)120 ObjectId (org.locationtech.geogig.api.ObjectId)91 NodeRef (org.locationtech.geogig.api.NodeRef)73 Node (org.locationtech.geogig.api.Node)56 RevTreeBuilder (org.locationtech.geogig.api.RevTreeBuilder)47 RevCommit (org.locationtech.geogig.api.RevCommit)36 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)29 FindTreeChild (org.locationtech.geogig.api.plumbing.FindTreeChild)27 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)24 RevObject (org.locationtech.geogig.api.RevObject)21 RevFeature (org.locationtech.geogig.api.RevFeature)18 ObjectDatabase (org.locationtech.geogig.storage.ObjectDatabase)18 Bucket (org.locationtech.geogig.api.Bucket)17 TreeTestSupport.featureNode (org.locationtech.geogig.api.plumbing.diff.TreeTestSupport.featureNode)17 File (java.io.File)16 Ref (org.locationtech.geogig.api.Ref)16 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)15 GeoGIG (org.locationtech.geogig.api.GeoGIG)14 OSMImportOp (org.locationtech.geogig.osm.internal.OSMImportOp)14