Search in sources :

Example 26 with Repository

use of org.locationtech.geogig.repository.Repository in project GeoGig by boundlessgeo.

the class LocalRemoteRepo method walkHead.

protected void walkHead(final ObjectId newHeadId, final boolean fetch, final ProgressListener progress) {
    Repository from = localRepository;
    Repository to = remoteGeoGig.getRepository();
    if (fetch) {
        Repository tmp = to;
        to = from;
        from = tmp;
    }
    final ObjectDatabase fromDb = from.objectDatabase();
    final ObjectDatabase toDb = to.objectDatabase();
    final RevObject object = fromDb.get(newHeadId);
    RevCommit commit = null;
    RevTag tag = null;
    if (object.getType().equals(TYPE.COMMIT)) {
        commit = (RevCommit) object;
    } else if (object.getType().equals(TYPE.TAG)) {
        tag = (RevTag) object;
        commit = fromDb.getCommit(tag.getCommitId());
    }
    if (commit != null) {
        final RevTree newTree = fromDb.getTree(commit.getTreeId());
        List<ObjectId> parentIds = new ArrayList<>(commit.getParentIds());
        if (parentIds.isEmpty()) {
            parentIds.add(ObjectId.NULL);
        }
        RevTree oldTree = RevTree.EMPTY;
        // the diff against each parent is not working. For some reason some buckets that are
        // equal between the two ends of the comparison never get transferred (at some point
        // they shouldn't be equal and so the Consumer notified of it/them). Yet with the target
        // databse exists check for each tree the performance is good enough.
        // for (ObjectId parentId : parentIds) {
        // if (!parentId.isNull()) {
        // RevCommit parent = fromDb.getCommit(parentId);
        // oldTree = fromDb.getTree(parent.getTreeId());
        // }
        copyNewObjects(oldTree, newTree, fromDb, toDb, progress);
        // }
        Preconditions.checkState(toDb.exists(newTree.getId()));
        toDb.put(commit);
    }
    if (tag != null) {
        toDb.put(tag);
    }
}
Also used : Repository(org.locationtech.geogig.repository.Repository) RevTag(org.locationtech.geogig.api.RevTag) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) ArrayList(java.util.ArrayList) RevTree(org.locationtech.geogig.api.RevTree) RevCommit(org.locationtech.geogig.api.RevCommit)

Example 27 with Repository

use of org.locationtech.geogig.repository.Repository in project GeoGig by boundlessgeo.

the class LocalMappedRemoteRepo method pushSparseCommit.

/**
     * This function takes all of the changes introduced by a commit on the sparse repository and
     * creates a new commit on the full repository with those changes.
     * 
     * @param commitId the commit id of commit from the sparse repository
     * @param from the sparse repository
     * @param to the full repository
     */
protected void pushSparseCommit(ObjectId commitId) {
    Repository from = localRepository;
    Repository to = remoteGeoGig.getRepository();
    Optional<RevObject> object = from.command(RevObjectParse.class).setObjectId(commitId).call();
    if (object.isPresent() && object.get().getType().equals(TYPE.COMMIT)) {
        RevCommit commit = (RevCommit) object.get();
        ObjectId parent = ObjectId.NULL;
        List<ObjectId> newParents = new LinkedList<ObjectId>();
        for (int i = 0; i < commit.getParentIds().size(); i++) {
            ObjectId parentId = commit.getParentIds().get(i);
            if (i != 0) {
                Optional<ObjectId> commonAncestor = from.command(FindCommonAncestor.class).setLeftId(commit.getParentIds().get(0)).setRightId(parentId).call();
                if (commonAncestor.isPresent()) {
                    if (from.command(CheckSparsePath.class).setStart(parentId).setEnd(commonAncestor.get()).call()) {
                        // This should be the base commit to preserve the sparse changes that
                        // were filtered
                        // out.
                        newParents.add(0, from.graphDatabase().getMapping(parentId));
                        continue;
                    }
                }
            }
            newParents.add(from.graphDatabase().getMapping(parentId));
        }
        if (newParents.size() > 0) {
            parent = from.graphDatabase().getMapping(newParents.get(0));
        }
        Iterator<DiffEntry> diffIter = from.command(DiffOp.class).setNewVersion(commitId).setOldVersion(parent).setReportTrees(true).call();
        LocalCopyingDiffIterator changes = new LocalCopyingDiffIterator(diffIter, from, to);
        RevTree rootTree = RevTree.EMPTY;
        if (newParents.size() > 0) {
            ObjectId mappedCommit = newParents.get(0);
            Optional<ObjectId> treeId = to.command(ResolveTreeish.class).setTreeish(mappedCommit).call();
            if (treeId.isPresent()) {
                rootTree = to.getTree(treeId.get());
            }
        }
        // Create new commit
        ObjectId newTreeId = to.command(WriteTree.class).setOldRoot(Suppliers.ofInstance(rootTree)).setDiffSupplier(Suppliers.ofInstance((Iterator<DiffEntry>) changes)).call();
        CommitBuilder builder = new CommitBuilder(commit);
        builder.setParentIds(newParents);
        builder.setTreeId(newTreeId);
        RevCommit mapped = builder.build();
        to.objectDatabase().put(mapped);
        from.graphDatabase().map(commit.getId(), mapped.getId());
        from.graphDatabase().map(mapped.getId(), commit.getId());
    }
}
Also used : CheckSparsePath(org.locationtech.geogig.api.plumbing.CheckSparsePath) WriteTree(org.locationtech.geogig.api.plumbing.WriteTree) RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) CommitBuilder(org.locationtech.geogig.api.CommitBuilder) LinkedList(java.util.LinkedList) Repository(org.locationtech.geogig.repository.Repository) FindCommonAncestor(org.locationtech.geogig.api.plumbing.FindCommonAncestor) RevTree(org.locationtech.geogig.api.RevTree) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 28 with Repository

use of org.locationtech.geogig.repository.Repository in project GeoGig by boundlessgeo.

the class ReportCommitConflictsOp method _call.

@Override
protected MergeScenarioReport _call() {
    MergeScenarioReport report = new MergeScenarioReport();
    ObjectId parentCommitId = ObjectId.NULL;
    if (commit.getParentIds().size() > 0) {
        parentCommitId = commit.getParentIds().get(0);
    }
    ObjectId parentTreeId = ObjectId.NULL;
    Repository repository = repository();
    if (repository.commitExists(parentCommitId)) {
        parentTreeId = repository.getCommit(parentCommitId).getTreeId();
    }
    // get changes
    Iterator<DiffEntry> diffs = command(DiffTree.class).setOldTree(parentTreeId).setNewTree(commit.getTreeId()).setReportTrees(true).call();
    while (diffs.hasNext()) {
        DiffEntry diff = diffs.next();
        String path = diff.oldPath() == null ? diff.newPath() : diff.oldPath();
        Optional<RevObject> obj = command(RevObjectParse.class).setRefSpec(Ref.HEAD + ":" + path).call();
        switch(diff.changeType()) {
            case ADDED:
                if (obj.isPresent()) {
                    TYPE type = command(ResolveObjectType.class).setObjectId(diff.getNewObject().objectId()).call();
                    if (TYPE.TREE.equals(type)) {
                        NodeRef headVersion = command(FindTreeChild.class).setChildPath(path).setParent(repository.getOrCreateHeadTree()).call().get();
                        if (!headVersion.getMetadataId().equals(diff.getNewObject().getMetadataId())) {
                            report.addConflict(new Conflict(path, ObjectId.NULL, diff.getNewObject().getMetadataId(), headVersion.getMetadataId()));
                        }
                    } else {
                        if (!obj.get().getId().equals(diff.newObjectId())) {
                            report.addConflict(new Conflict(path, ObjectId.NULL, diff.newObjectId(), obj.get().getId()));
                        }
                    }
                } else {
                    report.addUnconflicted(diff);
                }
                break;
            case REMOVED:
                if (obj.isPresent()) {
                    if (obj.get().getId().equals(diff.oldObjectId())) {
                        report.addUnconflicted(diff);
                    } else {
                        report.addConflict(new Conflict(path, diff.oldObjectId(), ObjectId.NULL, obj.get().getId()));
                    }
                }
                break;
            case MODIFIED:
                TYPE type = command(ResolveObjectType.class).setObjectId(diff.getNewObject().objectId()).call();
                if (TYPE.TREE.equals(type)) {
                    // one
                    if (!diff.isChange()) {
                        report.addUnconflicted(diff);
                    }
                } else {
                    String refSpec = Ref.HEAD + ":" + path;
                    obj = command(RevObjectParse.class).setRefSpec(refSpec).call();
                    if (!obj.isPresent()) {
                        // git reports this as a conflict but does not mark as conflicted, just adds
                        // the missing file.
                        // We add it and consider it unconflicted
                        report.addUnconflicted(diff);
                        break;
                    }
                    RevFeature feature = (RevFeature) obj.get();
                    DepthSearch depthSearch = new DepthSearch(repository.objectDatabase());
                    Optional<NodeRef> noderef = depthSearch.find(this.workingTree().getTree(), path);
                    RevFeatureType featureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
                    ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
                    FeatureDiff featureDiff = command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(diff.getOldObject())).setNewVersion(Suppliers.ofInstance(diff.getNewObject())).call();
                    Set<Entry<PropertyDescriptor, AttributeDiff>> attrDiffs = featureDiff.getDiffs().entrySet();
                    RevFeature newFeature = command(RevObjectParse.class).setObjectId(diff.newObjectId()).call(RevFeature.class).get();
                    boolean ok = true;
                    for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = attrDiffs.iterator(); iterator.hasNext() && ok; ) {
                        Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
                        AttributeDiff attrDiff = entry.getValue();
                        PropertyDescriptor descriptor = entry.getKey();
                        switch(attrDiff.getType()) {
                            case ADDED:
                                if (descriptors.contains(descriptor)) {
                                    ok = false;
                                }
                                break;
                            case REMOVED:
                            case MODIFIED:
                                if (!descriptors.contains(descriptor)) {
                                    ok = false;
                                    break;
                                }
                                for (int i = 0; i < descriptors.size(); i++) {
                                    if (descriptors.get(i).equals(descriptor)) {
                                        Optional<Object> value = feature.getValues().get(i);
                                        Optional<Object> newValue = newFeature.getValues().get(i);
                                        if (!newValue.equals(value)) {
                                            // check
                                            if (!attrDiff.canBeAppliedOn(value)) {
                                                ok = false;
                                            }
                                            break;
                                        }
                                    }
                                }
                        }
                    }
                    if (ok) {
                        report.addUnconflicted(diff);
                    } else {
                        report.addConflict(new Conflict(path, diff.oldObjectId(), diff.newObjectId(), obj.get().getId()));
                    }
                }
                break;
        }
    }
    return report;
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Entry(java.util.Map.Entry) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) TYPE(org.locationtech.geogig.api.RevObject.TYPE) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) Repository(org.locationtech.geogig.repository.Repository) DepthSearch(org.locationtech.geogig.repository.DepthSearch) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject)

Example 29 with Repository

use of org.locationtech.geogig.repository.Repository in project GeoGig by boundlessgeo.

the class LsRemote method getRemoteRepo.

/**
     * @param remote the remote to get
     * @return an interface for the remote repository
     */
public Optional<IRemoteRepo> getRemoteRepo(Remote remote) {
    Repository localRepository = repository();
    DeduplicationService deduplicationService = context.deduplicationService();
    return RemoteUtils.newRemote(GlobalContextBuilder.builder.build(Hints.readOnly()), remote, localRepository, deduplicationService);
}
Also used : Repository(org.locationtech.geogig.repository.Repository) DeduplicationService(org.locationtech.geogig.storage.DeduplicationService)

Example 30 with Repository

use of org.locationtech.geogig.repository.Repository in project GeoGig by boundlessgeo.

the class OSMHistoryImport method insertChanges.

/**
     * @param cli
     * @param changes
     * @param featureFilter
     * @throws IOException
     */
private long insertChanges(GeogigCLI cli, final Iterator<Change> changes, @Nullable Envelope featureFilter) throws IOException {
    final GeoGIG geogig = cli.getGeogig();
    final Repository repository = geogig.getRepository();
    final WorkingTree workTree = repository.workingTree();
    Map<Long, Coordinate> thisChangePointCache = new LinkedHashMap<Long, Coordinate>() {

        /** serialVersionUID */
        private static final long serialVersionUID = 1277795218777240552L;

        @Override
        protected boolean removeEldestEntry(Map.Entry<Long, Coordinate> eldest) {
            return size() == 10000;
        }
    };
    long cnt = 0;
    Set<String> deletes = Sets.newHashSet();
    Multimap<String, SimpleFeature> insertsByParent = HashMultimap.create();
    while (changes.hasNext()) {
        Change change = changes.next();
        final String featurePath = featurePath(change);
        if (featurePath == null) {
            // ignores relations
            continue;
        }
        final String parentPath = NodeRef.parentPath(featurePath);
        if (Change.Type.delete.equals(change.getType())) {
            cnt++;
            deletes.add(featurePath);
        } else {
            final Primitive primitive = change.getNode().isPresent() ? change.getNode().get() : change.getWay().get();
            final Geometry geom = parseGeometry(geogig, primitive, thisChangePointCache);
            if (geom instanceof Point) {
                thisChangePointCache.put(Long.valueOf(primitive.getId()), ((Point) geom).getCoordinate());
            }
            SimpleFeature feature = toFeature(primitive, geom);
            if (featureFilter == null || featureFilter.intersects((Envelope) feature.getBounds())) {
                insertsByParent.put(parentPath, feature);
                cnt++;
            }
        }
    }
    for (String parentPath : insertsByParent.keySet()) {
        Collection<SimpleFeature> features = insertsByParent.get(parentPath);
        if (features.isEmpty()) {
            continue;
        }
        Iterator<? extends Feature> iterator = features.iterator();
        ProgressListener listener = new DefaultProgressListener();
        List<org.locationtech.geogig.api.Node> insertedTarget = null;
        Integer collectionSize = Integer.valueOf(features.size());
        workTree.insert(parentPath, iterator, listener, insertedTarget, collectionSize);
    }
    if (!deletes.isEmpty()) {
        workTree.delete(deletes.iterator());
    }
    return cnt;
}
Also used : Node(org.locationtech.geogig.osm.internal.history.Node) Envelope(com.vividsolutions.jts.geom.Envelope) LinkedHashMap(java.util.LinkedHashMap) WorkingTree(org.locationtech.geogig.repository.WorkingTree) Entry(java.util.Map.Entry) Primitive(org.locationtech.geogig.osm.internal.history.Primitive) DefaultProgressListener(org.locationtech.geogig.api.DefaultProgressListener) Change(org.locationtech.geogig.osm.internal.history.Change) Point(com.vividsolutions.jts.geom.Point) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(com.vividsolutions.jts.geom.Geometry) Repository(org.locationtech.geogig.repository.Repository) ProgressListener(org.locationtech.geogig.api.ProgressListener) DefaultProgressListener(org.locationtech.geogig.api.DefaultProgressListener) Coordinate(com.vividsolutions.jts.geom.Coordinate) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Aggregations

Repository (org.locationtech.geogig.repository.Repository)30 ObjectId (org.locationtech.geogig.api.ObjectId)13 RevCommit (org.locationtech.geogig.api.RevCommit)11 File (java.io.File)10 IOException (java.io.IOException)9 Ref (org.locationtech.geogig.api.Ref)9 UpdateRef (org.locationtech.geogig.api.plumbing.UpdateRef)9 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)8 GeoGIG (org.locationtech.geogig.api.GeoGIG)7 UpdateSymRef (org.locationtech.geogig.api.plumbing.UpdateSymRef)7 SymRef (org.locationtech.geogig.api.SymRef)6 ArrayList (java.util.ArrayList)5 NodeRef (org.locationtech.geogig.api.NodeRef)5 Conflict (org.locationtech.geogig.api.plumbing.merge.Conflict)5 CommitBuilder (org.locationtech.geogig.api.CommitBuilder)4 Platform (org.locationtech.geogig.api.Platform)4 RevObject (org.locationtech.geogig.api.RevObject)4 CanRunDuringConflict (org.locationtech.geogig.di.CanRunDuringConflict)4 LinkedList (java.util.LinkedList)3 RevTree (org.locationtech.geogig.api.RevTree)3