use of org.locationtech.geogig.api.ProgressListener in project GeoGig by boundlessgeo.
the class OSMApplyDiffOp method _call.
@Override
protected Optional<OSMReport> _call() {
checkNotNull(file);
Preconditions.checkArgument(file.exists(), "File does not exist: " + file);
ProgressListener progressListener = getProgressListener();
progressListener.setDescription("Applying OSM diff file to GeoGig repo...");
OSMReport report = parseDiffFileAndInsert();
return Optional.fromNullable(report);
}
use of org.locationtech.geogig.api.ProgressListener in project GeoGig by boundlessgeo.
the class OSMImportOp method _call.
@Override
protected Optional<OSMReport> _call() {
checkNotNull(urlOrFilepath);
ObjectId oldTreeId = workingTree().getTree().getId();
File osmDataFile = null;
final InputStream osmDataStream;
if (urlOrFilepath.startsWith("http")) {
osmDataStream = downloadFile();
} else {
osmDataFile = new File(urlOrFilepath);
Preconditions.checkArgument(osmDataFile.exists(), "File does not exist: " + urlOrFilepath);
try {
osmDataStream = new BufferedInputStream(new FileInputStream(osmDataFile), 1024 * 1024);
} catch (FileNotFoundException e) {
throw Throwables.propagate(e);
}
}
ProgressListener progressListener = getProgressListener();
progressListener.setDescription("Importing into GeoGig repo...");
EntityConverter converter = new EntityConverter();
OSMReport report;
try {
report = parseDataFileAndInsert(osmDataFile, osmDataStream, converter);
} finally {
Closeables.closeQuietly(osmDataStream);
}
if (!progressListener.isCanceled() && report != null) {
ObjectId newTreeId = workingTree().getTree().getId();
if (!noRaw) {
if (mapping != null || filter != null) {
progressListener.setDescription("Staging features...");
command(AddOp.class).setProgressListener(progressListener).call();
progressListener.setDescription("Committing features...");
command(CommitOp.class).setMessage(message).setProgressListener(progressListener).call();
OSMLogEntry entry = new OSMLogEntry(newTreeId, report.getLatestChangeset(), report.getLatestTimestamp());
command(AddOSMLogEntry.class).setEntry(entry).call();
if (filter != null) {
command(WriteOSMFilterFile.class).setEntry(entry).setFilterCode(filter).call();
}
if (mapping != null) {
command(WriteOSMMappingEntries.class).setMapping(mapping).setMappingLogEntry(new OSMMappingLogEntry(oldTreeId, newTreeId)).call();
}
}
}
}
return Optional.fromNullable(report);
}
use of org.locationtech.geogig.api.ProgressListener in project GeoGig by boundlessgeo.
the class OSMImportOp method downloadFile.
private InputStream downloadFile() {
ProgressListener listener = getProgressListener();
checkNotNull(filter);
OSMDownloader downloader = new OSMDownloader(urlOrFilepath, listener);
listener.setDescription("Connecting to " + urlOrFilepath + "...");
File destination = null;
if (keepFile) {
destination = this.downloadFile;
if (destination == null) {
try {
destination = File.createTempFile("osm-geogig", ".xml");
} catch (IOException e) {
Throwables.propagate(e);
}
} else {
destination = destination.getAbsoluteFile();
}
}
try {
InputStream dataStream = downloader.download(filter, destination);
if (keepFile) {
listener.setDescription("Downloaded data will be kept in " + destination.getAbsolutePath());
}
return dataStream;
} catch (Exception e) {
throw Throwables.propagate(Throwables.getRootCause(e));
}
}
use of org.locationtech.geogig.api.ProgressListener 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;
}
use of org.locationtech.geogig.api.ProgressListener in project GeoGig by boundlessgeo.
the class CloneOp method _call.
/**
* Executes the clone operation.
*
* @return {@code null}
* @see org.locationtech.geogig.api.AbstractGeoGigOp#call()
*/
@Override
protected Void _call() {
Preconditions.checkArgument(repositoryURL != null && !repositoryURL.isEmpty(), "No repository specified to clone from.");
Repository repository = repository();
if (repository.isSparse()) {
Preconditions.checkArgument(branch.isPresent(), "No branch specified for sparse clone.");
}
ProgressListener progressListener = getProgressListener();
progressListener.started();
// Set up origin
Remote remote = command(RemoteAddOp.class).setName("origin").setURL(repositoryURL).setMapped(repository.isSparse()).setUserName(username).setPassword(password).setBranch(repository.isSparse() ? branch.get() : null).call();
if (!depth.isPresent()) {
// See if we are cloning a shallow clone. If so, a depth must be specified.
Optional<IRemoteRepo> remoteRepo = RemoteUtils.newRemote(GlobalContextBuilder.builder.build(Hints.readOnly()), remote, repository, repository.deduplicationService());
Preconditions.checkState(remoteRepo.isPresent(), "Failed to connect to the remote.");
IRemoteRepo remoteRepoInstance = remoteRepo.get();
try {
remoteRepoInstance.open();
} catch (IOException e) {
Throwables.propagate(e);
}
try {
depth = remoteRepoInstance.getDepth();
} finally {
try {
remoteRepoInstance.close();
} catch (IOException e) {
Throwables.propagate(e);
}
}
}
if (depth.isPresent()) {
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName(Repository.DEPTH_CONFIG_KEY).setValue(depth.get().toString()).call();
}
// Fetch remote data
command(FetchOp.class).setDepth(depth.or(0)).setProgressListener(progressListener).call();
// Set up remote tracking branches
final ImmutableSet<Ref> remoteRefs = command(LsRemote.class).retrieveTags(false).setRemote(Suppliers.ofInstance(Optional.of(remote))).call();
boolean emptyRepo = true;
for (Ref remoteRef : remoteRefs) {
if (emptyRepo && !remoteRef.getObjectId().isNull()) {
emptyRepo = false;
}
String branchName = remoteRef.localName();
if (remoteRef instanceof SymRef) {
continue;
}
if (!command(RefParse.class).setName(Ref.HEADS_PREFIX + branchName).call().isPresent()) {
command(BranchCreateOp.class).setName(branchName).setSource(remoteRef.getObjectId().toString()).call();
} else {
command(UpdateRef.class).setName(Ref.HEADS_PREFIX + branchName).setNewValue(remoteRef.getObjectId()).call();
}
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branchName + ".remote").setValue(remote.getName()).call();
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branchName + ".merge").setValue(Ref.HEADS_PREFIX + remoteRef.localName()).call();
}
if (!emptyRepo) {
// checkout branch
if (branch.isPresent()) {
command(CheckoutOp.class).setForce(true).setSource(branch.get()).call();
} else {
// checkout the head
final Optional<Ref> currRemoteHead = command(RefParse.class).setName(Ref.REMOTES_PREFIX + remote.getName() + "/" + Ref.HEAD).call();
if (currRemoteHead.isPresent() && currRemoteHead.get() instanceof SymRef) {
final SymRef remoteHeadRef = (SymRef) currRemoteHead.get();
final String currentBranch = Ref.localName(remoteHeadRef.getTarget());
command(CheckoutOp.class).setForce(true).setSource(currentBranch).call();
} else {
// just leave at default; should be master since we just initialized the repo.
}
}
}
progressListener.complete();
return null;
}
Aggregations