use of org.locationtech.geogig.api.RevObject.TYPE in project GeoGig by boundlessgeo.
the class PGExport method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
if (args.isEmpty()) {
printUsage(cli);
throw new CommandFailedException();
}
String path = args.get(0);
String tableName = args.get(1);
checkParameter(tableName != null && !tableName.isEmpty(), "No table name specified");
DataStore dataStore = getDataStore();
ObjectId featureTypeId = null;
if (!Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
SimpleFeatureType outputFeatureType;
if (sFeatureTypeId != null) {
// Check the feature type id string is a correct id
Optional<ObjectId> id = cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call();
checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId);
TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call();
checkParameter(type.equals(TYPE.FEATURETYPE), "Provided reference does not resolve to a feature type: ", sFeatureTypeId);
outputFeatureType = (SimpleFeatureType) cli.getGeogig().command(RevObjectParse.class).setObjectId(id.get()).call(RevFeatureType.class).get().type();
featureTypeId = id.get();
} else {
try {
SimpleFeatureType sft = getFeatureType(path, cli);
outputFeatureType = new SimpleFeatureTypeImpl(new NameImpl(tableName), sft.getAttributeDescriptors(), sft.getGeometryDescriptor(), sft.isAbstract(), sft.getRestrictions(), sft.getSuper(), sft.getDescription());
} catch (GeoToolsOpException e) {
throw new CommandFailedException("No features to export.", e);
}
}
try {
dataStore.createSchema(outputFeatureType);
} catch (IOException e) {
throw new CommandFailedException("Cannot create new table in database", e);
}
} else {
if (!overwrite) {
throw new InvalidParameterException("The selected table already exists. Use -o to overwrite");
}
}
SimpleFeatureSource featureSource;
try {
featureSource = dataStore.getFeatureSource(tableName);
} catch (IOException e) {
throw new CommandFailedException("Can't aquire the feature source", e);
}
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
if (overwrite) {
try {
featureStore.removeFeatures(Filter.INCLUDE);
} catch (IOException e) {
throw new CommandFailedException("Error trying to remove features", e);
}
}
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter);
if (defaultType) {
op.exportDefaultFeatureType();
}
try {
op.setProgressListener(cli.getProgressListener()).call();
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
switch(e.statusCode) {
case MIXED_FEATURE_TYPES:
throw new CommandFailedException("The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.", e);
default:
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
}
cli.getConsole().println(path + " exported successfully to " + tableName);
} else {
throw new CommandFailedException("Can't write to the selected table");
}
}
use of org.locationtech.geogig.api.RevObject.TYPE in project GeoGig by boundlessgeo.
the class SLExport method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
if (args.isEmpty()) {
printUsage(cli);
throw new CommandFailedException();
}
String path = args.get(0);
String tableName = args.get(1);
checkParameter(tableName != null && !tableName.isEmpty(), "No table name specified");
DataStore dataStore = getDataStore();
ObjectId featureTypeId = null;
if (!Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
SimpleFeatureType outputFeatureType;
if (sFeatureTypeId != null) {
// Check the feature type id string is a correct id
Optional<ObjectId> id = cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call();
checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId);
TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call();
checkParameter(type.equals(TYPE.FEATURETYPE), "Provided reference does not resolve to a feature type: ", sFeatureTypeId);
outputFeatureType = (SimpleFeatureType) cli.getGeogig().command(RevObjectParse.class).setObjectId(id.get()).call(RevFeatureType.class).get().type();
featureTypeId = id.get();
} else {
try {
SimpleFeatureType sft = getFeatureType(path, cli);
outputFeatureType = new SimpleFeatureTypeImpl(new NameImpl(tableName), sft.getAttributeDescriptors(), sft.getGeometryDescriptor(), sft.isAbstract(), sft.getRestrictions(), sft.getSuper(), sft.getDescription());
} catch (GeoToolsOpException e) {
throw new CommandFailedException("No features to export.", e);
}
}
try {
dataStore.createSchema(outputFeatureType);
} catch (IOException e) {
throw new CommandFailedException("Cannot create new table in database", e);
}
} else {
if (!overwrite) {
throw new CommandFailedException("The selected table already exists. Use -o to overwrite");
}
}
SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
if (!(featureSource instanceof SimpleFeatureStore)) {
throw new CommandFailedException("Can't write to the selected table");
}
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
if (overwrite) {
try {
featureStore.removeFeatures(Filter.INCLUDE);
} catch (IOException e) {
throw new CommandFailedException("Error truncating table: " + e.getMessage(), e);
}
}
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter);
if (defaultType) {
op.exportDefaultFeatureType();
}
try {
op.setProgressListener(cli.getProgressListener()).call();
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
switch(e.statusCode) {
case MIXED_FEATURE_TYPES:
throw new CommandFailedException("The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.", e);
default:
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
}
cli.getConsole().println(path + " exported successfully to " + tableName);
}
use of org.locationtech.geogig.api.RevObject.TYPE 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.api.RevObject.TYPE in project GeoGig by boundlessgeo.
the class ReportMergeScenarioOp method _call.
@Override
protected MergeScenarioReport _call() {
Optional<ObjectId> ancestor = command(FindCommonAncestor.class).setLeft(toMerge).setRight(mergeInto).call();
Preconditions.checkState(ancestor.isPresent(), "No ancestor commit could be found.");
Map<String, DiffEntry> mergeIntoDiffs = Maps.newHashMap();
MergeScenarioReport report = new MergeScenarioReport();
Iterator<DiffEntry> diffs = command(DiffTree.class).setOldTree(ancestor.get()).setReportTrees(true).setNewTree(mergeInto.getId()).call();
while (diffs.hasNext()) {
DiffEntry diff = diffs.next();
String path = diff.oldPath() == null ? diff.newPath() : diff.oldPath();
mergeIntoDiffs.put(path, diff);
}
Iterator<DiffEntry> toMergeDiffs = command(DiffTree.class).setOldTree(ancestor.get()).setReportTrees(true).setNewTree(toMerge.getId()).call();
while (toMergeDiffs.hasNext()) {
DiffEntry toMergeDiff = toMergeDiffs.next();
String path = toMergeDiff.oldPath() == null ? toMergeDiff.newPath() : toMergeDiff.oldPath();
if (mergeIntoDiffs.containsKey(path)) {
RevCommit ancestorCommit = command(RevObjectParse.class).setRefSpec(ancestor.get().toString()).call(RevCommit.class).get();
RevTree ancestorTree = command(RevObjectParse.class).setObjectId(ancestorCommit.getTreeId()).call(RevTree.class).get();
Optional<NodeRef> ancestorVersion = command(FindTreeChild.class).setChildPath(path).setParent(ancestorTree).call();
ObjectId ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getNode().getObjectId() : ObjectId.NULL;
ObjectId theirs = toMergeDiff.getNewObject() == null ? ObjectId.NULL : toMergeDiff.getNewObject().objectId();
DiffEntry mergeIntoDiff = mergeIntoDiffs.get(path);
ObjectId ours = mergeIntoDiff.getNewObject() == null ? ObjectId.NULL : mergeIntoDiff.getNewObject().objectId();
if (!mergeIntoDiff.changeType().equals(toMergeDiff.changeType())) {
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
continue;
}
switch(toMergeDiff.changeType()) {
case ADDED:
if (toMergeDiff.getNewObject().equals(mergeIntoDiff.getNewObject())) {
// already added in current branch, no need to do anything
} else {
TYPE type = command(ResolveObjectType.class).setObjectId(toMergeDiff.getNewObject().objectId()).call();
if (TYPE.TREE.equals(type)) {
boolean conflict = !toMergeDiff.getNewObject().getMetadataId().equals(mergeIntoDiff.getNewObject().getMetadataId());
if (conflict) {
// In this case, we store the metadata id, not the element id
ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getMetadataId() : ObjectId.NULL;
ours = mergeIntoDiff.getNewObject().getMetadataId();
theirs = toMergeDiff.getNewObject().getMetadataId();
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
}
// if the metadata ids match, it means both branches have added the same
// tree, maybe with different content, but there is no need to do
// anything. The correct tree is already there and the merge can be run
// safely, so we do not add it neither as a conflicted change nor as an
// unconflicted one
} else {
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
}
}
break;
case REMOVED:
// removed by both histories => no conflict and no need to do anything
break;
case MODIFIED:
TYPE type = command(ResolveObjectType.class).setObjectId(toMergeDiff.getNewObject().objectId()).call();
if (TYPE.TREE.equals(type)) {
boolean conflict = !toMergeDiff.getNewObject().getMetadataId().equals(mergeIntoDiff.getNewObject().getMetadataId());
if (conflict) {
// In this case, we store the metadata id, not the element id
ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getMetadataId() : ObjectId.NULL;
ours = mergeIntoDiff.getNewObject().getMetadataId();
theirs = toMergeDiff.getNewObject().getMetadataId();
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
}
} else {
FeatureDiff toMergeFeatureDiff = command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(toMergeDiff.getOldObject())).setNewVersion(Suppliers.ofInstance(toMergeDiff.getNewObject())).call();
FeatureDiff mergeIntoFeatureDiff = command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(mergeIntoDiff.getOldObject())).setNewVersion(Suppliers.ofInstance(mergeIntoDiff.getNewObject())).call();
if (toMergeFeatureDiff.conflicts(mergeIntoFeatureDiff)) {
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
} else {
// try to perform automerge
if (!toMergeDiff.getNewObject().getMetadataId().equals(mergeIntoDiff.getNewObject().getMetadataId())) {
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
} else if (!toMergeFeatureDiff.equals(mergeIntoFeatureDiff)) {
Feature mergedFeature = command(MergeFeaturesOp.class).setFirstFeature(mergeIntoDiff.getNewObject()).setSecondFeature(toMergeDiff.getNewObject()).setAncestorFeature(mergeIntoDiff.getOldObject()).call();
RevFeature revFeature = RevFeatureBuilder.build(mergedFeature);
if (revFeature.getId().equals(toMergeDiff.newObjectId())) {
// the resulting merged feature equals the feature to merge from
// the branch, which means that it exists in the repo and there
// is no need to add it
report.addUnconflicted(toMergeDiff);
} else {
RevFeatureType featureType = command(RevObjectParse.class).setObjectId(mergeIntoDiff.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
FeatureInfo merged = new FeatureInfo(mergedFeature, featureType, path);
report.addMerged(merged);
}
}
}
}
break;
}
} else {
// modified in the other branch under it.
if (ChangeType.REMOVED.equals(toMergeDiff.changeType())) {
TYPE type = command(ResolveObjectType.class).setObjectId(toMergeDiff.oldObjectId()).call();
if (TYPE.TREE.equals(type)) {
String parentPath = toMergeDiff.oldPath();
Set<Entry<String, DiffEntry>> entries = mergeIntoDiffs.entrySet();
boolean conflict = false;
for (Entry<String, DiffEntry> entry : entries) {
if (entry.getKey().startsWith(parentPath)) {
if (!ChangeType.REMOVED.equals(entry.getValue().changeType())) {
RevCommit ancestorCommit = command(RevObjectParse.class).setRefSpec(ancestor.get().toString()).call(RevCommit.class).get();
RevTree ancestorTree = command(RevObjectParse.class).setObjectId(ancestorCommit.getTreeId()).call(RevTree.class).get();
Optional<NodeRef> ancestorVersion = command(FindTreeChild.class).setChildPath(path).setParent(ancestorTree).call();
ObjectId ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getNode().getObjectId() : ObjectId.NULL;
ObjectId theirs = toMergeDiff.getNewObject() == null ? ObjectId.NULL : toMergeDiff.getNewObject().objectId();
String oursRefSpec = mergeInto.getId().toString() + ":" + parentPath;
Optional<ObjectId> ours = command(RevParse.class).setRefSpec(oursRefSpec).call();
report.addConflict(new Conflict(path, ancestorVersionId, ours.get(), theirs));
conflict = true;
break;
}
}
}
if (!conflict) {
report.addUnconflicted(toMergeDiff);
}
} else {
report.addUnconflicted(toMergeDiff);
}
} else {
report.addUnconflicted(toMergeDiff);
}
}
}
return report;
}
use of org.locationtech.geogig.api.RevObject.TYPE in project GeoGig by boundlessgeo.
the class PreOrderDiffWalk method node.
/**
* Called when found a difference between two nodes. It can be a removal ({@code right} is
* null), an added node ({@code left} is null}, or a modified feature/tree (neither is null);
* but {@code left} and {@code right} can never be equal.
* <p>
* Depending on the type of node, this method will call {@link Consumer#tree} or
* {@link Consumer#feature}, and continue the traversal down the trees in case it was a tree and
* {@link Consumer#tree} returned null.
*/
private void node(Consumer consumer, @Nullable final Node left, @Nullable final Node right) {
checkState(left != null || right != null, "both nodes can't be null");
checkArgument(!Objects.equal(left, right));
final TYPE type = left == null ? right.getType() : left.getType();
if (TYPE.FEATURE.equals(type)) {
consumer.feature(left, right);
} else {
checkState(TYPE.TREE.equals(type));
if (consumer.tree(left, right)) {
RevTree leftTree;
RevTree rightTree;
leftTree = left == null ? RevTree.EMPTY : leftSource.getTree(left.getObjectId());
rightTree = right == null ? RevTree.EMPTY : rightSource.getTree(right.getObjectId());
traverseTree(consumer, leftTree, rightTree, 0);
}
consumer.endTree(left, right);
}
}
Aggregations