use of org.locationtech.geogig.api.RevTreeBuilder in project GeoGig by boundlessgeo.
the class WorkingTree method createTypeTree.
public synchronized NodeRef createTypeTree(final String treePath, final FeatureType featureType) {
final RevTree workHead = getTree();
Optional<NodeRef> typeTreeRef = context.command(FindTreeChild.class).setIndex(true).setParent(workHead).setChildPath(treePath).call();
final RevFeatureType revType = RevFeatureTypeImpl.build(featureType);
if (typeTreeRef.isPresent()) {
throw new IllegalArgumentException("Tree already exists at " + treePath);
}
indexDatabase.put(revType);
final ObjectId metadataId = revType.getId();
final RevTree newTree = new RevTreeBuilder(indexDatabase).build();
ObjectId newWorkHeadId = context.command(WriteBack.class).setToIndex(true).setAncestor(workHead.builder(indexDatabase)).setChildPath(treePath).setTree(newTree).setMetadataId(metadataId).call();
updateWorkHead(newWorkHeadId);
return context.command(FindTreeChild.class).setIndex(true).setParent(getTree()).setChildPath(treePath).call().get();
}
use of org.locationtech.geogig.api.RevTreeBuilder in project GeoGig by boundlessgeo.
the class WorkingTree method insert.
/**
* Insert a single feature into the working tree and updates the WORK_HEAD ref.
*
* @param parentTreePath path of the parent tree to insert the feature into
* @param feature the feature to insert
*/
public Node insert(final String parentTreePath, final Feature feature) {
final FeatureType featureType = feature.getType();
NodeRef treeRef;
Optional<NodeRef> typeTreeRef = context.command(FindTreeChild.class).setIndex(true).setParent(getTree()).setChildPath(parentTreePath).call();
ObjectId metadataId;
if (typeTreeRef.isPresent()) {
treeRef = typeTreeRef.get();
RevFeatureType newFeatureType = RevFeatureTypeImpl.build(featureType);
metadataId = newFeatureType.getId().equals(treeRef.getMetadataId()) ? ObjectId.NULL : newFeatureType.getId();
if (!newFeatureType.getId().equals(treeRef.getMetadataId())) {
indexDatabase.put(newFeatureType);
}
} else {
treeRef = createTypeTree(parentTreePath, featureType);
// treeRef.getMetadataId();
metadataId = ObjectId.NULL;
}
// ObjectId metadataId = treeRef.getMetadataId();
final Node node = putInDatabase(feature, metadataId);
RevTreeBuilder parentTree = context.command(FindOrCreateSubtree.class).setIndex(true).setParent(Suppliers.ofInstance(Optional.of(getTree()))).setChildPath(parentTreePath).call().builder(indexDatabase);
parentTree.put(node);
final ObjectId treeMetadataId = treeRef.getMetadataId();
ObjectId newTree = context.command(WriteBack.class).setAncestor(getTreeSupplier()).setChildPath(parentTreePath).setToIndex(true).setTree(parentTree.build()).setMetadataId(treeMetadataId).call();
updateWorkHead(newTree);
final String featurePath = NodeRef.appendChild(parentTreePath, node.getName());
Optional<NodeRef> featureRef = context.command(FindTreeChild.class).setIndex(true).setParent(getTree()).setChildPath(featurePath).call();
return featureRef.get().getNode();
}
use of org.locationtech.geogig.api.RevTreeBuilder in project GeoGig by boundlessgeo.
the class RevTreeBuilder2 method build.
/**
* Traverses the nodes in the {@link NodeIndex}, deletes the ones with {@link ObjectId#NULL
* NULL} ObjectIds, and adds the ones with non "NULL" ids.
*
* @return the new tree, not saved to the object database. Any bucket tree though is saved when
* this method returns.
*/
public RevTree build() {
if (nodeIndex == null) {
return original.builder(db).build();
}
Stopwatch sw = Stopwatch.createStarted();
RevTreeBuilder builder;
try {
builder = new RevTreeBuilder(db, original);
Iterator<Node> nodes = nodeIndex.nodes();
while (nodes.hasNext()) {
Node node = nodes.next();
if (node.getObjectId().isNull()) {
builder.remove(node.getName());
} else {
builder.put(node);
}
}
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
} finally {
nodeIndex.close();
}
LOGGER.debug("Index traversed in {}", sw.stop());
sw.reset().start();
RevTree namedTree = builder.build();
saveExtraFeatureTypes();
LOGGER.debug("RevTreeBuilder.build() in {}", sw.stop());
return namedTree;
}
use of org.locationtech.geogig.api.RevTreeBuilder in project GeoGig by boundlessgeo.
the class WriteBack method writeBack.
private ObjectId writeBack(RevTreeBuilder ancestor, final String ancestorPath, final RevTree childTree, final String childPath, final ObjectDatabase targetDatabase, final ObjectId metadataId) {
final ObjectId treeId = childTree.getId();
targetDatabase.put(childTree);
final boolean isDirectChild = NodeRef.isDirectChild(ancestorPath, childPath);
if (isDirectChild) {
Envelope treeBounds = null;
if (!metadataId.isNull()) {
// only include bounds for trees with a default feature type
treeBounds = SpatialOps.boundsOf(childTree);
}
String childName = childPath;
Node treeNode = Node.create(childName, treeId, metadataId, TYPE.TREE, treeBounds);
ancestor.put(treeNode);
RevTree newAncestor = ancestor.build();
targetDatabase.put(newAncestor);
return newAncestor.getId();
}
final String parentPath = NodeRef.parentPath(childPath);
Optional<NodeRef> parentRef = getTreeChild(ancestor, parentPath);
RevTreeBuilder parentBuilder;
ObjectId parentMetadataId = ObjectId.NULL;
if (parentRef.isPresent()) {
ObjectId parentId = parentRef.get().objectId();
parentMetadataId = parentRef.get().getMetadataId();
parentBuilder = getTree(parentId, targetDatabase).builder(targetDatabase);
} else {
parentBuilder = RevTree.EMPTY.builder(targetDatabase);
}
String childName = NodeRef.nodeFromPath(childPath);
Envelope treeBounds = null;
if (!metadataId.isNull()) {
// only include bounds for trees with a default feature type
treeBounds = SpatialOps.boundsOf(childTree);
}
Node treeNode = Node.create(childName, treeId, metadataId, TYPE.TREE, treeBounds);
parentBuilder.put(treeNode);
RevTree parent = parentBuilder.build();
return writeBack(ancestor, ancestorPath, parent, parentPath, targetDatabase, parentMetadataId);
}
use of org.locationtech.geogig.api.RevTreeBuilder in project GeoGig by boundlessgeo.
the class WriteTree method resolveTargetTree.
private RevTreeBuilder resolveTargetTree(final RevTree root, String treePath, Map<String, RevTreeBuilder> treeCache, Map<String, ObjectId> metadataCache, ObjectId fallbackMetadataId, ObjectDatabase repositoryDatabase) {
RevTreeBuilder treeBuilder = treeCache.get(treePath);
if (treeBuilder == null) {
if (NodeRef.ROOT.equals(treePath)) {
treeBuilder = root.builder(repositoryDatabase);
} else {
Optional<NodeRef> treeRef = command(FindTreeChild.class).setIndex(false).setParent(root).setChildPath(treePath).call();
if (treeRef.isPresent()) {
metadataCache.put(treePath, treeRef.get().getMetadataId());
treeBuilder = command(RevObjectParse.class).setObjectId(treeRef.get().objectId()).call(RevTree.class).get().builder(repositoryDatabase);
} else {
metadataCache.put(treePath, fallbackMetadataId);
treeBuilder = new RevTreeBuilder(repositoryDatabase);
}
}
treeCache.put(treePath, treeBuilder);
}
return treeBuilder;
}
Aggregations