use of org.locationtech.geogig.api.Bounded in project GeoGig by boundlessgeo.
the class LocalRemoteRepo method copyNewObjects.
private void copyNewObjects(RevTree oldTree, RevTree newTree, final ObjectDatabase fromDb, final ObjectDatabase toDb, final ProgressListener progress) {
checkNotNull(oldTree);
checkNotNull(newTree);
checkNotNull(fromDb);
checkNotNull(toDb);
checkNotNull(progress);
// the diff walk uses fromDb as both left and right data source since we're comparing what
// we have in the "origin" database against trees on the same repository
PostOrderDiffWalk diffWalk = new PostOrderDiffWalk(oldTree, newTree, fromDb, fromDb);
// holds object ids that need to be copied to the target db. Pruned when it reaches a
// threshold.
final Set<ObjectId> ids = new HashSet<ObjectId>();
// This filter further refines the post order diff walk by making it ignore trees/buckets
// that are already present in the target db
Predicate<Bounded> filter = new Predicate<Bounded>() {
@Override
public boolean apply(@Nullable Bounded b) {
if (b == null) {
return false;
}
if (progress.isCanceled()) {
// abort traversal
return false;
}
ObjectId id;
if (b instanceof Node) {
Node node = (Node) b;
if (RevObject.TYPE.TREE.equals(node.getType())) {
// check of existence of trees only. For features the diff filtering is good
// enough and checking for existence on each feature would be killer
// performance wise
id = node.getObjectId();
} else {
return true;
}
} else {
id = ((Bucket) b).id();
}
boolean exists = ids.contains(id) || toDb.exists(id);
return !exists;
}
};
// receives notifications of feature/bucket/tree diffs. Only interested in the "new"/right
// side of the comparisons
Consumer consumer = new Consumer() {
final int bulkSize = 10_000;
@Override
public void feature(@Nullable Node left, Node right) {
add(left);
add(right);
}
@Override
public void tree(@Nullable Node left, Node right) {
add(left);
add(right);
}
private void add(@Nullable Node node) {
if (node == null) {
return;
}
ids.add(node.getObjectId());
Optional<ObjectId> metadataId = node.getMetadataId();
if (metadataId.isPresent()) {
ids.add(metadataId.get());
}
checkLimitAndCopy();
}
@Override
public void bucket(int bucketIndex, int bucketDepth, @Nullable Bucket left, Bucket right) {
if (left != null) {
ids.add(left.id());
}
if (right != null) {
ids.add(right.id());
}
checkLimitAndCopy();
}
private void checkLimitAndCopy() {
if (ids.size() >= bulkSize) {
copy(ids, fromDb, toDb, progress);
ids.clear();
}
}
};
diffWalk.walk(filter, consumer);
// copy remaining objects
copy(ids, fromDb, toDb, progress);
}
use of org.locationtech.geogig.api.Bounded in project GeoGig by boundlessgeo.
the class OSMExport method getFeatures.
private Iterator<EntityContainer> getFeatures(String ref) {
Optional<ObjectId> id = geogig.command(RevParse.class).setRefSpec(ref).call();
if (!id.isPresent()) {
return Iterators.emptyIterator();
}
LsTreeOp op = geogig.command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
if (bbox != null) {
final Envelope env;
try {
env = new Envelope(Double.parseDouble(bbox.get(0)), Double.parseDouble(bbox.get(2)), Double.parseDouble(bbox.get(1)), Double.parseDouble(bbox.get(3)));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Wrong bbox definition");
}
Predicate<Bounded> filter = new Predicate<Bounded>() {
@Override
public boolean apply(final Bounded bounded) {
boolean intersects = bounded.intersects(env);
return intersects;
}
};
op.setBoundsFilter(filter);
}
Iterator<NodeRef> iterator = op.call();
final EntityConverter converter = new EntityConverter();
Function<NodeRef, EntityContainer> function = new Function<NodeRef, EntityContainer>() {
@Override
@Nullable
public EntityContainer apply(@Nullable NodeRef ref) {
RevFeature revFeature = geogig.command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
SimpleFeatureType featureType;
if (ref.path().startsWith(OSMUtils.NODE_TYPE_NAME)) {
featureType = OSMUtils.nodeType();
} else {
featureType = OSMUtils.wayType();
}
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
RevFeatureType revFeatureType = RevFeatureTypeImpl.build(featureType);
List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = revFeature.getValues();
for (int i = 0; i < descriptors.size(); i++) {
PropertyDescriptor descriptor = descriptors.get(i);
Optional<Object> value = values.get(i);
featureBuilder.set(descriptor.getName(), value.orNull());
}
SimpleFeature feature = featureBuilder.buildFeature(ref.name());
Entity entity = converter.toEntity(feature, null);
EntityContainer container;
if (entity instanceof Node) {
container = new NodeContainer((Node) entity);
} else {
container = new WayContainer((Way) entity);
}
return container;
}
};
return Iterators.transform(iterator, function);
}
use of org.locationtech.geogig.api.Bounded in project GeoGig by boundlessgeo.
the class RevTreeSerializationTest method assertTreesAreEqual.
private void assertTreesAreEqual(RevTree a, RevTree b) {
assertTrue(a.getId().equals(b.getId()));
assertTrue(a.buckets().equals(b.buckets()));
assertTrue(a.features().equals(b.features()));
assertTrue(a.trees().equals(b.trees()));
assertTrue(a.numTrees() == b.numTrees());
assertTrue(a.size() == b.size());
Iterator<? extends Bounded> ia;
Iterator<? extends Bounded> ib;
if (a.buckets().isPresent()) {
ia = a.buckets().get().values().iterator();
ib = b.buckets().get().values().iterator();
} else {
ia = a.children();
ib = b.children();
}
// bounds are not part of the Bounded.equals(Object) contract as its auxiliary information
while (ia.hasNext()) {
Bounded ba = ia.next();
Bounded bb = ib.next();
Envelope ea = new Envelope();
Envelope eb = new Envelope();
ba.expand(ea);
bb.expand(eb);
assertEquals(ea.getMinX(), eb.getMinX(), 1e-7D);
assertEquals(ea.getMinY(), eb.getMinY(), 1e-7D);
assertEquals(ea.getMaxX(), eb.getMaxX(), 1e-7D);
assertEquals(ea.getMaxY(), eb.getMaxY(), 1e-7D);
}
}
use of org.locationtech.geogig.api.Bounded in project GeoGig by boundlessgeo.
the class PostOrderDiffWalkTest method testLeafLeafWithSubStrees.
@Test
public void testLeafLeafWithSubStrees() {
// two leaf trees
ObjectId metadataId = ObjectId.forString("fake");
RevTree left = createTreesTree(leftSource, 2, 2, metadataId).build();
RevTree right = createTreesTree(rightSource, 3, 2, metadataId).build();
PostOrderDiffWalk visitor = new PostOrderDiffWalk(left, right, leftSource, rightSource);
visitor.walk(testConsumer);
List<Bounded> leftCalls = testConsumer.orderedLeft;
List<Bounded> rightCalls = testConsumer.orderedRight;
System.err.println(leftCalls);
System.err.println(rightCalls);
Node lroot = nodeFor(left);
Node rroot = nodeFor(right);
assertEquals(4, leftCalls.size());
assertEquals(4, rightCalls.size());
assertNull(leftCalls.get(0));
assertNull(leftCalls.get(1));
assertNull(leftCalls.get(2));
assertEquals(lroot, leftCalls.get(3));
assertEquals(rroot, rightCalls.get(3));
assertNotNull(rightCalls.get(2));
assertEquals(RevObject.TYPE.TREE, ((Node) rightCalls.get(2)).getType());
assertEquals(RevObject.TYPE.FEATURE, ((Node) rightCalls.get(1)).getType());
assertEquals(RevObject.TYPE.FEATURE, ((Node) rightCalls.get(0)).getType());
}
use of org.locationtech.geogig.api.Bounded in project GeoGig by boundlessgeo.
the class PostOrderDiffWalkTest method testBucketBucketFlat.
@Test
public void testBucketBucketFlat() {
RevTree left = createFeaturesTree(leftSource, "f", RevTree.NORMALIZED_SIZE_LIMIT + 1).build();
RevTree right = createFeaturesTree(rightSource, "f", RevTree.NORMALIZED_SIZE_LIMIT + 2).build();
PostOrderDiffWalk visitor = new PostOrderDiffWalk(left, right, leftSource, rightSource);
visitor.walk(testConsumer);
List<Bounded> leftCalls = testConsumer.orderedLeft;
List<Bounded> rightCalls = testConsumer.orderedRight;
// System.err.println(leftCalls);
// System.err.println(rightCalls);
Node lroot = nodeFor(left);
Node rroot = nodeFor(right);
assertEquals(3, leftCalls.size());
assertEquals(3, rightCalls.size());
assertNull(leftCalls.get(0));
assertTrue(leftCalls.get(1) instanceof Bucket);
assertEquals(lroot, leftCalls.get(2));
assertEquals(rroot, rightCalls.get(2));
assertTrue(rightCalls.get(1) instanceof Bucket);
assertTrue(rightCalls.get(0) instanceof Node);
assertEquals(RevObject.TYPE.FEATURE, ((Node) rightCalls.get(0)).getType());
}
Aggregations