use of org.locationtech.geogig.api.RevTreeBuilder 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.RevTreeBuilder in project GeoGig by boundlessgeo.
the class CatObjectTest method createTree.
private RevTree createTree(int numChildren) {
RevTreeBuilder rtb = new RevTreeBuilder(odb);
for (int i = 0; i < numChildren; i++) {
String key = FEATURE_PREFIX + i;
Node ref = Node.create(key, FAKE_ID, FAKE_ID, TYPE.FEATURE, null);
rtb.put(ref);
}
return rtb.build();
}
use of org.locationtech.geogig.api.RevTreeBuilder in project GeoGig by boundlessgeo.
the class RevTreeBuilderTest method createTree.
private RevTreeBuilder createTree(final int numEntries, final boolean insertInAscendingKeyOrder) {
RevTreeBuilder tree = new RevTreeBuilder(odb);
final int increment = insertInAscendingKeyOrder ? 1 : -1;
final int from = insertInAscendingKeyOrder ? 0 : numEntries - 1;
final int breakAt = insertInAscendingKeyOrder ? numEntries : -1;
int c = 0;
for (int i = from; i != breakAt; i += increment, c++) {
addNode(tree, i);
if (numEntries > 100) {
if ((c + 1) % (numEntries / 10) == 0) {
System.err.print("#" + (c + 1));
} else if ((c + 1) % (numEntries / 100) == 0) {
System.err.print('.');
}
}
}
System.err.print('\n');
return tree;
}
use of org.locationtech.geogig.api.RevTreeBuilder in project GeoGig by boundlessgeo.
the class RevTreeBuilderTest method testPutRandomGet.
@Test
public void testPutRandomGet() throws Exception {
final int numEntries = 2 * RevTree.NORMALIZED_SIZE_LIMIT + 1500;
final ObjectId treeId;
Stopwatch sw;
sw = Stopwatch.createStarted();
treeId = createAndSaveTree(numEntries, true);
sw.stop();
System.err.println("Stored " + numEntries + " tree entries in " + sw + " (" + Math.round(numEntries / (sw.elapsed(TimeUnit.MILLISECONDS) / 1000D)) + "/s)");
sw.reset().start();
final RevTree tree = odb.getTree(treeId);
sw.stop();
System.err.println("Retrieved tree in " + sw);
{
Map<Integer, Node> randomEdits = Maps.newHashMap();
Random randGen = new Random();
for (int i = 0; i < tree.size() / 2; i++) {
int random;
while (randomEdits.containsKey(random = randGen.nextInt(numEntries))) {
// $codepro.audit.disable extraSemicolon
;
}
String name = "Feature." + random;
ObjectId newid = ObjectId.forString(name + "changed");
Node ref = Node.create(name, newid, ObjectId.NULL, TYPE.FEATURE, null);
randomEdits.put(random, ref);
}
RevTreeBuilder mutable = tree.builder(odb);
sw.reset().start();
for (Node ref : randomEdits.values()) {
mutable.put(ref);
}
mutable.build();
sw.stop();
System.err.println(randomEdits.size() + " random modifications in " + sw);
}
// CharSequence treeStr =
// repo.command(CatObject.class).setObject(Suppliers.ofInstance(tree))
// .call();
// System.out.println(treeStr);
final FindTreeChild childFinder = repo.command(FindTreeChild.class).setParent(tree);
sw.reset().start();
System.err.println("Reading " + numEntries + " entries....");
for (int i = 0; i < numEntries; i++) {
if ((i + 1) % (numEntries / 10) == 0) {
System.err.print("#" + (i + 1));
} else if ((i + 1) % (numEntries / 100) == 0) {
System.err.print('.');
}
String key = "Feature." + i;
// ObjectId oid = ObjectId.forString(key);
Optional<NodeRef> ref = childFinder.setChildPath(key).call();
assertTrue(key, ref.isPresent());
// assertEquals(key, ref.get().getPath());
// assertEquals(key, oid, ref.get().getObjectId());
}
sw.stop();
System.err.println("\nGot " + numEntries + " in " + sw.elapsed(TimeUnit.MILLISECONDS) + "ms (" + Math.round(numEntries / (sw.elapsed(TimeUnit.MILLISECONDS) / 1000D)) + "/s)\n");
}
use of org.locationtech.geogig.api.RevTreeBuilder in project GeoGig by boundlessgeo.
the class RevTreeBuilderTest method testResultingTreeSize.
private void testResultingTreeSize(int numEntries) {
RevTreeBuilder builder = createTree(numEntries, true);
RevTree tree = builder.build();
final long declaredSize = tree.size();
Iterator<NodeRef> it = new DepthTreeIterator("", ObjectId.NULL, tree, odb, Strategy.RECURSIVE_FEATURES_ONLY);
long itSize = 0;
while (it.hasNext()) {
it.next();
itSize++;
}
assertEquals(numEntries, itSize);
assertEquals(numEntries, declaredSize);
}
Aggregations