Search in sources :

Example 1 with WriteTree

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

the class AbstractMappedRemoteRepo method fetchSparseCommit.

/**
     * This function takes all of the changes introduced by the specified commit and filters them
     * based on the repository filter. It then uses the filtered results to construct a new commit
     * that is the descendant of commits that the original's parents are mapped to.
     * 
     * @param commitId the commit id of the original, non-sparse commit
     * @param allowEmpty allow the function to create an empty sparse commit
     */
private void fetchSparseCommit(ObjectId commitId, boolean allowEmpty) {
    Optional<RevObject> object = getObject(commitId);
    if (object.isPresent() && object.get().getType().equals(TYPE.COMMIT)) {
        RevCommit commit = (RevCommit) object.get();
        FilteredDiffIterator changes = getFilteredChanges(commit);
        GraphDatabase graphDatabase = localRepository.graphDatabase();
        ObjectDatabase objectDatabase = localRepository.objectDatabase();
        graphDatabase.put(commit.getId(), commit.getParentIds());
        RevTree rootTree = RevTree.EMPTY;
        if (commit.getParentIds().size() > 0) {
            // Map this commit to the last "sparse" commit in my ancestry
            ObjectId mappedCommit = graphDatabase.getMapping(commit.getParentIds().get(0));
            graphDatabase.map(commit.getId(), mappedCommit);
            Optional<ObjectId> treeId = localRepository.command(ResolveTreeish.class).setTreeish(mappedCommit).call();
            if (treeId.isPresent()) {
                rootTree = localRepository.getTree(treeId.get());
            }
        } else {
            graphDatabase.map(commit.getId(), ObjectId.NULL);
        }
        Iterator<DiffEntry> it = Iterators.filter(changes, new Predicate<DiffEntry>() {

            @Override
            public boolean apply(DiffEntry e) {
                return true;
            }
        });
        if (it.hasNext()) {
            // Create new commit
            WriteTree writeTree = localRepository.command(WriteTree.class).setOldRoot(Suppliers.ofInstance(rootTree)).setDiffSupplier(Suppliers.ofInstance((Iterator<DiffEntry>) it));
            if (changes.isAutoIngesting()) {
                // the iterator already ingests objects into the ObjectDatabase
                writeTree.dontMoveObjects();
            }
            ObjectId newTreeId = writeTree.call();
            CommitBuilder builder = new CommitBuilder(commit);
            List<ObjectId> newParents = new LinkedList<ObjectId>();
            for (ObjectId parentCommitId : commit.getParentIds()) {
                newParents.add(graphDatabase.getMapping(parentCommitId));
            }
            builder.setParentIds(newParents);
            builder.setTreeId(newTreeId);
            RevCommit mapped = builder.build();
            objectDatabase.put(mapped);
            if (changes.wasFiltered()) {
                graphDatabase.setProperty(mapped.getId(), GraphDatabase.SPARSE_FLAG, "true");
            }
            graphDatabase.map(mapped.getId(), commit.getId());
            // Replace the old mapping with the new commit Id.
            graphDatabase.map(commit.getId(), mapped.getId());
        } else if (allowEmpty) {
            CommitBuilder builder = new CommitBuilder(commit);
            List<ObjectId> newParents = new LinkedList<ObjectId>();
            for (ObjectId parentCommitId : commit.getParentIds()) {
                newParents.add(graphDatabase.getMapping(parentCommitId));
            }
            builder.setParentIds(newParents);
            builder.setTreeId(rootTree.getId());
            builder.setMessage(PLACEHOLDER_COMMIT_MESSAGE);
            RevCommit mapped = builder.build();
            objectDatabase.put(mapped);
            graphDatabase.setProperty(mapped.getId(), GraphDatabase.SPARSE_FLAG, "true");
            graphDatabase.map(mapped.getId(), commit.getId());
            // Replace the old mapping with the new commit Id.
            graphDatabase.map(commit.getId(), mapped.getId());
        } else {
            // Mark the mapped commit as sparse, since it wont have these changes
            graphDatabase.setProperty(graphDatabase.getMapping(commit.getId()), GraphDatabase.SPARSE_FLAG, "true");
        }
    }
}
Also used : WriteTree(org.locationtech.geogig.api.plumbing.WriteTree) RevObject(org.locationtech.geogig.api.RevObject) GraphDatabase(org.locationtech.geogig.storage.GraphDatabase) ObjectId(org.locationtech.geogig.api.ObjectId) CommitBuilder(org.locationtech.geogig.api.CommitBuilder) LinkedList(java.util.LinkedList) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) Iterator(java.util.Iterator) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) List(java.util.List) RevTree(org.locationtech.geogig.api.RevTree) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 CommitBuilder (org.locationtech.geogig.api.CommitBuilder)1 ObjectId (org.locationtech.geogig.api.ObjectId)1 RevCommit (org.locationtech.geogig.api.RevCommit)1 RevObject (org.locationtech.geogig.api.RevObject)1 RevTree (org.locationtech.geogig.api.RevTree)1 WriteTree (org.locationtech.geogig.api.plumbing.WriteTree)1 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)1 GraphDatabase (org.locationtech.geogig.storage.GraphDatabase)1 ObjectDatabase (org.locationtech.geogig.storage.ObjectDatabase)1