Skip to content

Commit

Permalink
Use the new RevTree methods where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Roldan committed Nov 24, 2018
1 parent d4d0453 commit d0f18ae
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 218 deletions.
Expand Up @@ -225,10 +225,14 @@ private static final class TreeFunnel implements Funnel<RevTree> {

@Override
public void funnel(RevTree from, PrimitiveSink into) {
ImmutableList<Node> trees = from.trees();
ImmutableList<Node> features = from.features();
ImmutableSortedMap<Integer, Bucket> buckets = from.buckets();
funnel(into, trees, features, buckets);
RevObjectTypeFunnel.funnel(TYPE.TREE, into);
from.forEachTree((n) -> NodeFunnel.funnel(n, into));
from.forEachFeature((n) -> NodeFunnel.funnel(n, into));

from.forEachBucket((index, bucket) -> {
Funnels.integerFunnel().funnel(index, into);
ObjectIdFunnel.funnel(bucket.getObjectId(), into);
});
}

public void funnel(PrimitiveSink into, List<Node> trees, List<Node> features,
Expand Down
Expand Up @@ -70,14 +70,14 @@ public static StringBuilder toString(final ObjectId id, final int numBytes,
*/
public static Iterator<Node> children(RevTree tree, Comparator<Node> comparator) {
checkNotNull(comparator);
ImmutableList<Node> trees = tree.trees();
ImmutableList<Node> features = tree.features();
if (trees.isEmpty()) {
return features.iterator();
if (tree.treesSize() == 0) {
return tree.features().iterator();
}
if (features.isEmpty()) {
return trees.iterator();
if (tree.featuresSize() == 0) {
return tree.trees().iterator();
}
ImmutableList<Node> trees = tree.trees();
ImmutableList<Node> features = tree.features();
return Iterators.mergeSorted(ImmutableList.of(trees.iterator(), features.iterator()),
comparator);
}
Expand Down
13 changes: 13 additions & 0 deletions src/api/src/main/java/org/locationtech/geogig/model/RevTree.java
Expand Up @@ -10,6 +10,7 @@
package org.locationtech.geogig.model;

import java.util.Comparator;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand Down Expand Up @@ -194,6 +195,10 @@ public default int treesSize() {
return trees().size();
}

public default Node getTree(int index) {
return trees().get(index);
}

/**
* Performs the given action for each element of the {@link #trees} collection respecting its
* iteration order
Expand Down Expand Up @@ -233,6 +238,10 @@ public default int featuresSize() {
return features().size();
}

public default Node getFeature(int index) {
return features().get(index);
}

/**
* Performs the given action for each element of the {@link #features} collection respecting its
* iteration order
Expand Down Expand Up @@ -274,4 +283,8 @@ public default int bucketsSize() {
public default void forEachBucket(BiConsumer<Integer, Bucket> consumer) {
buckets().forEach(consumer);
}

public default Optional<Bucket> getBucket(int bucketIndex) {
return Optional.ofNullable(buckets().get(Integer.valueOf(bucketIndex)));
}
}
Expand Up @@ -126,9 +126,9 @@ public static RevTree create(final ObjectId id, final long size, final int child

@Override
public String toString() {
final int nSubtrees = trees().size();
final int nBuckets = buckets().size();
final int nFeatures = features().size();
final int nSubtrees = treesSize();
final int nBuckets = bucketsSize();
final int nFeatures = featuresSize();

StringBuilder builder = new StringBuilder();
builder.append("Tree[");
Expand Down
Expand Up @@ -18,7 +18,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
Expand All @@ -28,7 +27,6 @@
import java.util.concurrent.locks.ReentrantLock;

import org.eclipse.jdt.annotation.Nullable;
import org.locationtech.geogig.model.Bucket;
import org.locationtech.geogig.model.CanonicalNodeNameOrder;
import org.locationtech.geogig.model.Node;
import org.locationtech.geogig.model.ObjectId;
Expand All @@ -39,12 +37,8 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.ListMultimap;

/**
Expand Down Expand Up @@ -380,7 +374,7 @@ protected void mergeRoot(DAG root) {

root.setTotalChildCount(original.size() + original.numTrees());

final boolean originalIsLeaf = original.buckets().isEmpty();
final boolean originalIsLeaf = 0 == original.bucketsSize();

if (originalIsLeaf) {
final Map<NodeId, DAGNode> origNodes = lazyNodes(original);
Expand All @@ -391,32 +385,32 @@ protected void mergeRoot(DAG root) {
}

} else {
final ImmutableSortedMap<Integer, Bucket> buckets = original.buckets();

if (root.getState() == STATE.INITIALIZED) {
// make DAG a bucket tree
checkState(root.numChildren() == 0);

// initialize buckets
preload(buckets.values());
for (Entry<Integer, Bucket> e : buckets.entrySet()) {
Integer bucketIndex = e.getKey();
preloadBuckets(original);
original.forEachBucket((bucketIndex, bucket) -> {
TreeId dagBucketId = root.getId().newChild(bucketIndex.intValue());
ObjectId bucketId = e.getValue().getObjectId();
ObjectId bucketId = bucket.getObjectId();
// make sure the DAG exists and is initialized
DAG dag = getOrCreateDAG(dagBucketId, bucketId);
root.addBucket(dagBucketId);
}
});
}
}
root.setMirrored();
}

}

private void preload(ImmutableCollection<Bucket> values) {
this.storageProvider.getTreeCache()
.preload(Iterables.transform(values, (b) -> b.getObjectId()));
private void preloadBuckets(RevTree tree) {
if (tree.bucketsSize() > 0) {
List<ObjectId> ids = new ArrayList<>(tree.bucketsSize());
tree.forEachBucket((i, b) -> ids.add(b.getObjectId()));
this.storageProvider.getTreeCache().preload(ids);
}
}

protected RevTree getOriginalTree(@Nullable ObjectId originalId) {
Expand Down Expand Up @@ -472,19 +466,20 @@ private Map<NodeId, DAGNode> lazyNodes(final RevTree tree) {

Map<NodeId, DAGNode> dagNodes = new HashMap<>();

List<Node> treeNodes = tree.trees();
for (int i = 0; i < treeNodes.size(); i++) {
NodeId nodeId = computeId(treeNodes.get(i));
final int treesSize = tree.treesSize();
for (int i = 0; i < treesSize; i++) {
NodeId nodeId = computeId(tree.getTree(i));
DAGNode dagNode = DAGNode.treeNode(cacheTreeId, i);
dagNodes.put(nodeId, dagNode);
}

ImmutableList<Node> featureNodes = tree.features();
for (int i = 0; i < featureNodes.size(); i++) {
NodeId nodeId = computeId(featureNodes.get(i));
final int featuresSize = tree.featuresSize();
for (int i = 0; i < featuresSize; i++) {
NodeId nodeId = computeId(tree.getFeature(i));
DAGNode dagNode = DAGNode.featureNode(cacheTreeId, i);
dagNodes.put(nodeId, dagNode);
}

return dagNodes;
}

Expand Down
Expand Up @@ -9,8 +9,6 @@
*/
package org.locationtech.geogig.model.internal;

import static com.google.common.base.Preconditions.checkState;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
Expand All @@ -20,9 +18,6 @@
import org.locationtech.geogig.storage.datastream.FormatCommonV2_2;
import org.locationtech.geogig.storage.datastream.Varint;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

abstract class DAGNode {

public abstract Node resolve(TreeCache cache);
Expand Down Expand Up @@ -126,18 +121,10 @@ public LazyDAGNode(final int leafRevTreeId, final int nodeIndex) {
@Override
public final Node resolve(TreeCache cache) {
RevTree tree = cache.resolve(leafRevTreeId);
ImmutableList<Node> collection = collection(tree);
Node node;
try {
node = collection.get(nodeIndex);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
throw e;
}
return node;
return resolve(tree);
}

protected abstract ImmutableList<Node> collection(RevTree tree);
protected abstract Node resolve(RevTree tree);

@Override
public boolean isNull() {
Expand Down Expand Up @@ -167,10 +154,8 @@ static final class TreeDAGNode extends LazyDAGNode {
}

@Override
protected ImmutableList<Node> collection(RevTree tree) {
Preconditions.checkState(!tree.trees().isEmpty());
ImmutableList<Node> trees = tree.trees();
return trees;
protected Node resolve(RevTree tree) {
return tree.getTree(this.nodeIndex);
}

}
Expand All @@ -182,10 +167,8 @@ static final class FeatureDAGNode extends LazyDAGNode {
}

@Override
protected ImmutableList<Node> collection(RevTree tree) {
checkState(!tree.features().isEmpty());
ImmutableList<Node> features = tree.features();
return features;
protected Node resolve(RevTree tree) {
return tree.getFeature(this.nodeIndex);
}
}

Expand Down
Expand Up @@ -32,7 +32,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSortedMap;

/**
* This class determines how the quadtree clustering strategy puts features in the tree.
Expand Down Expand Up @@ -222,12 +221,11 @@ private TreeId computeExpandedChildId(RevTree originalTree, TreeId rootId) {
} else {
normalizedSizeLimit = normalizedSizeLimit();
// it may be a quad wit sub-quads instead of an unpromotables tree
ImmutableSortedMap<Integer, Bucket> buckets = originalTree.buckets();
boolean isValidQuad = !buckets.isEmpty();
boolean isValidQuad = originalTree.bucketsSize() > 0;
for (Quadrant q : Quadrant.VALUES) {
Bucket treeBucket = buckets.get(Integer.valueOf(q.getBucketNumber()));
if (treeBucket != null) {
Envelope bucketBounds = treeBucket.bounds().orNull();
java.util.Optional<Bucket> treeBucket = originalTree.getBucket(q.getBucketNumber());
if (treeBucket.isPresent()) {
Envelope bucketBounds = treeBucket.get().bounds().orNull();
Quadrant bucketQuad = computeQuadrant(bucketBounds, childDepthIndex);
if (bucketQuad == null) {
isValidQuad = false;
Expand Down
Expand Up @@ -9,7 +9,9 @@
*/
package org.locationtech.geogig.model.internal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -25,7 +27,6 @@
import com.google.common.cache.LoadingCache;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Iterables;

class TreeCache {

Expand Down Expand Up @@ -59,8 +60,10 @@ public RevTree getTree(final ObjectId treeId) {
if (internalId == null) {
tree = store.getTree(treeId);
getTreeId(tree);
if (!tree.buckets().isEmpty()) {
preload(Iterables.transform(tree.buckets().values(), (b) -> b.getObjectId()));
if (tree.bucketsSize() > 0) {
List<ObjectId> bucketIds = new ArrayList<>(tree.bucketsSize());
tree.forEachBucket((i, b) -> bucketIds.add(b.getObjectId()));
preload(bucketIds);
}
} else {
tree = resolve(internalId.intValue());
Expand Down
Expand Up @@ -222,7 +222,7 @@ private class Children extends AbstractIterator<Node> {
private Iterator<Node> children;

public Children(RevTree tree) {
if (!tree.buckets().isEmpty()) {
if (tree.bucketsSize() > 0) {
this.children = new Buckets(tree);
} else {
this.children = Iterators.filter(
Expand All @@ -244,9 +244,9 @@ private class Features extends AbstractIterator<Node> {
private Iterator<Node> features;

public Features(RevTree tree) {
if (!tree.features().isEmpty()) {
if (tree.featuresSize() > 0) {
this.features = Iterators.filter(tree.features().iterator(), boundsFilter);
} else if (!tree.buckets().isEmpty()) {
} else if (tree.bucketsSize() > 0) {
this.features = new FeatureBuckets(tree);
} else {
this.features = Collections.emptyIterator();
Expand All @@ -269,9 +269,9 @@ private class Trees extends AbstractIterator<Node> {
public Trees(RevTree tree) {
if (tree.numTrees() == 0) {
this.trees = Collections.emptyIterator();
} else if (!tree.trees().isEmpty()) {
} else if (tree.treesSize() > 0) {
this.trees = Iterators.filter(tree.trees().iterator(), boundsFilter);
} else if (!tree.buckets().isEmpty()) {
} else if (tree.bucketsSize() > 0) {
this.trees = new TreeBuckets(tree);
} else {
this.trees = Collections.emptyIterator();
Expand All @@ -297,7 +297,7 @@ private class Buckets extends AbstractIterator<Node> {
private Iterator<Node> bucketEntries;

public Buckets(RevTree tree) {
Preconditions.checkArgument(!tree.buckets().isEmpty());
Preconditions.checkArgument(tree.bucketsSize() > 0);
buckets = Iterators.filter(tree.buckets().values().iterator(), boundsFilter);
bucketEntries = Collections.emptyIterator();
// may it be a mixed tree (having both direct children and buckets)
Expand All @@ -323,7 +323,7 @@ protected Node computeNext() {
*/
protected Iterator<Node> resolveBucketEntries(ObjectId bucketId) {
RevTree bucketTree = source.getTree(bucketId);
if (!bucketTree.buckets().isEmpty()) {
if (bucketTree.bucketsSize() > 0) {
return new Buckets(bucketTree);
}
return new Children(bucketTree);
Expand All @@ -345,10 +345,10 @@ protected Iterator<Node> resolveBucketEntries(ObjectId bucketId) {
if (bucketTree.numTrees() == 0) {
return Collections.emptyIterator();
}
if (!bucketTree.trees().isEmpty()) {
if (bucketTree.treesSize() > 0) {
return new Trees(bucketTree);
}
if (!bucketTree.buckets().isEmpty()) {
if (bucketTree.bucketsSize() > 0) {
return new TreeBuckets(bucketTree);
}
return Collections.emptyIterator();
Expand All @@ -367,10 +367,10 @@ public FeatureBuckets(RevTree tree) {
@Override
protected Iterator<Node> resolveBucketEntries(ObjectId bucketId) {
RevTree bucketTree = source.getTree(bucketId);
if (!bucketTree.buckets().isEmpty()) {
if (bucketTree.bucketsSize() > 0) {
return new FeatureBuckets(bucketTree);
}
if (!bucketTree.features().isEmpty()) {
if (bucketTree.featuresSize() > 0) {
return new Features(bucketTree);
}
return Collections.emptyIterator();
Expand Down

0 comments on commit d0f18ae

Please sign in to comment.