Search in sources :

Example 1 with PostOrderDiffWalk

use of org.locationtech.geogig.api.plumbing.diff.PostOrderDiffWalk 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);
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) Node(org.locationtech.geogig.api.Node) Predicate(com.google.common.base.Predicate) Bounded(org.locationtech.geogig.api.Bounded) Consumer(org.locationtech.geogig.api.plumbing.diff.PostOrderDiffWalk.Consumer) Bucket(org.locationtech.geogig.api.Bucket) PostOrderDiffWalk(org.locationtech.geogig.api.plumbing.diff.PostOrderDiffWalk) Nullable(javax.annotation.Nullable) HashSet(java.util.HashSet)

Aggregations

Predicate (com.google.common.base.Predicate)1 HashSet (java.util.HashSet)1 Nullable (javax.annotation.Nullable)1 Bounded (org.locationtech.geogig.api.Bounded)1 Bucket (org.locationtech.geogig.api.Bucket)1 Node (org.locationtech.geogig.api.Node)1 ObjectId (org.locationtech.geogig.api.ObjectId)1 PostOrderDiffWalk (org.locationtech.geogig.api.plumbing.diff.PostOrderDiffWalk)1 Consumer (org.locationtech.geogig.api.plumbing.diff.PostOrderDiffWalk.Consumer)1