use of org.locationtech.geogig.storage.ObjectDatabase in project GeoGig by boundlessgeo.
the class LsTreeOp method _call.
/**
* @see java.util.concurrent.Callable#call()
*/
protected Iterator<NodeRef> _call() {
String ref = this.ref;
if (ref == null) {
ref = Ref.WORK_HEAD;
}
ObjectId metadataId = ObjectId.NULL;
final String path = ref.lastIndexOf(':') != -1 ? ref.substring(ref.lastIndexOf(':') + 1) : "";
if (!path.isEmpty()) {
final String providedRefName = ref.lastIndexOf(':') != -1 ? ref.substring(0, ref.lastIndexOf(':')) : null;
if (providedRefName != null) {
Optional<ObjectId> rootTreeId = command(ResolveTreeish.class).setTreeish(providedRefName).call();
if (rootTreeId.isPresent()) {
RevTree rootTree = command(RevObjectParse.class).setObjectId(rootTreeId.get()).call(RevTree.class).get();
Optional<NodeRef> treeRef = command(FindTreeChild.class).setChildPath(path).setIndex(true).setParent(rootTree).call();
metadataId = treeRef.isPresent() ? treeRef.get().getMetadataId() : ObjectId.NULL;
}
}
}
// is it just a ref name?
Optional<Ref> reference = command(RefParse.class).setName(ref).call();
if (reference.isPresent()) {
if (reference.get().getObjectId().isNull()) {
return Iterators.emptyIterator();
}
}
Optional<RevObject> revObject = command(RevObjectParse.class).setRefSpec(ref).call(RevObject.class);
Optional<NodeRef> treeRef = Optional.absent();
if (!revObject.isPresent()) {
if (Ref.WORK_HEAD.equals(ref)) {
// tree but it is empty
return Iterators.emptyIterator();
}
// let's try to see if it is a feature type or feature in the working tree
NodeRef.checkValidPath(ref);
treeRef = command(FindTreeChild.class).setParent(workingTree().getTree()).setChildPath(ref).setIndex(true).call();
Preconditions.checkArgument(treeRef.isPresent(), "Invalid reference: %s", ref);
ObjectId treeId = treeRef.get().objectId();
metadataId = treeRef.get().getMetadataId();
revObject = command(RevObjectParse.class).setObjectId(treeId).call(RevObject.class);
}
checkArgument(revObject.isPresent(), "Invalid reference: %s", ref);
final TYPE type = revObject.get().getType();
switch(type) {
case FEATURE:
NodeRef nodeRef = treeRef.isPresent() ? treeRef.get() : null;
List<NodeRef> nodeRefs = Lists.newArrayList();
nodeRefs.add(nodeRef);
// If show trees options is passed in show all trees that contain this feature
if (this.strategy == Strategy.TREES_ONLY) {
if (nodeRef != null) {
while (!nodeRef.getParentPath().isEmpty()) {
treeRef = command(FindTreeChild.class).setParent(workingTree().getTree()).setChildPath(nodeRef.getParentPath()).setIndex(true).call();
nodeRef = treeRef.get();
nodeRefs.add(nodeRef);
}
}
}
return nodeRefs.iterator();
case COMMIT:
RevCommit revCommit = (RevCommit) revObject.get();
ObjectId treeId = revCommit.getTreeId();
revObject = command(RevObjectParse.class).setObjectId(treeId).call(RevObject.class);
case TREE:
DepthTreeIterator.Strategy iterStrategy;
switch(this.strategy) {
case CHILDREN:
iterStrategy = DepthTreeIterator.Strategy.CHILDREN;
break;
case FEATURES_ONLY:
iterStrategy = DepthTreeIterator.Strategy.FEATURES_ONLY;
break;
case TREES_ONLY:
iterStrategy = DepthTreeIterator.Strategy.TREES_ONLY;
break;
case DEPTHFIRST:
iterStrategy = DepthTreeIterator.Strategy.RECURSIVE;
break;
case DEPTHFIRST_ONLY_FEATURES:
iterStrategy = DepthTreeIterator.Strategy.RECURSIVE_FEATURES_ONLY;
break;
case DEPTHFIRST_ONLY_TREES:
iterStrategy = DepthTreeIterator.Strategy.RECURSIVE_TREES_ONLY;
break;
default:
throw new IllegalStateException("Unknown strategy: " + this.strategy);
}
RevTree tree = (RevTree) revObject.get();
ObjectDatabase database = stagingDatabase();
DepthTreeIterator iter = new DepthTreeIterator(path, metadataId, tree, database, iterStrategy);
iter.setBoundsFilter(refBoundsFilter);
return iter;
default:
throw new IllegalArgumentException(String.format("Invalid reference: %s", ref));
}
}
use of org.locationtech.geogig.storage.ObjectDatabase 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.storage.ObjectDatabase in project GeoGig by boundlessgeo.
the class FindOrCreateSubtree method _call.
/**
* Executes the command.
*
* @return the subtree if it was found, or a new one if it wasn't
*/
@Override
protected RevTree _call() {
checkNotNull(parentSupplier, "parent");
checkNotNull(childPath, "childPath");
ObjectId subtreeId;
if (parentSupplier.get().isPresent()) {
RevTree parent = parentSupplier.get().get();
Optional<NodeRef> treeChildRef = command(FindTreeChild.class).setIndex(indexDb).setParentPath(parentPath).setChildPath(childPath).setParent(Suppliers.ofInstance(parent)).call();
if (treeChildRef.isPresent()) {
NodeRef treeRef = treeChildRef.get();
if (!TYPE.TREE.equals(treeRef.getType())) {
throw new IllegalArgumentException("Object exists as child of tree " + parent.getId() + " but is not a tree: " + treeChildRef);
}
subtreeId = treeRef.objectId();
} else {
subtreeId = RevTree.EMPTY_TREE_ID;
}
} else {
subtreeId = RevTree.EMPTY_TREE_ID;
}
if (RevTree.EMPTY_TREE_ID.equals(subtreeId)) {
return RevTree.EMPTY;
}
ObjectDatabase target = indexDb ? stagingDatabase() : objectDatabase();
RevTree tree = target.getTree(subtreeId);
return tree;
}
use of org.locationtech.geogig.storage.ObjectDatabase in project GeoGig by boundlessgeo.
the class JEObjectDatabaseTest method testReadOnlyHint3.
@Test
public void testReadOnlyHint3() {
hints.set(Hints.OBJECTS_READ_ONLY, Boolean.TRUE);
db = createDb();
RevObject obj = RevTree.EMPTY;
try {
db.put(obj);
fail("Expected UOE on read only hint");
} catch (UnsupportedOperationException e) {
assertTrue(true);
}
hints.set(Hints.OBJECTS_READ_ONLY, Boolean.FALSE);
ObjectDatabase db2 = createDb();
Assert.assertTrue(db2.put(obj));
db.close();
db2.close();
}
use of org.locationtech.geogig.storage.ObjectDatabase in project GeoGig by boundlessgeo.
the class DiffTreeTest method testBoundsFilteringReprojecting.
@Test
public void testBoundsFilteringReprojecting() throws Exception {
ObjectDatabase db = geogit.getContext().objectDatabase();
RevTree tree1 = tree(1000, db);
RevTree tree2 = tree(50, db);
RevTree root = createRoot(db, tree1, tree2);
CoordinateReferenceSystem nativeCrs = revtype.type().getCoordinateReferenceSystem();
CoordinateReferenceSystem queryCrs = CRS.decode("EPSG:4326", true);
ReferencedEnvelope nativeFilter = new ReferencedEnvelope(49.9, 51.1, 49.9, 51.1, nativeCrs);
ReferencedEnvelope queryFilter = nativeFilter.transform(queryCrs, true);
List<DiffEntry> diffs;
diffTree.setOldTree(ObjectId.NULL).setNewTree(root.getId());
diffTree.setBoundsFilter(queryFilter);
diffs = ImmutableList.copyOf(diffTree.call());
assertEquals(2, diffs.size());
}
Aggregations