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);
}
}
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());
}
}
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;
}
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);
}
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;
}
Aggregations