public Features(RevTree tree) { if (tree.featuresSize() > 0) { this.features = Iterators.filter(tree.features().iterator(), boundsFilter); } else if (tree.bucketsSize() > 0) { this.features = new FeatureBuckets(tree); } else { this.features = Collections.emptyIterator(); } }
public static String toString(@NonNull RevTree tree) { String stree = String.format( "%s(%s)[size:%,d, tree nodes:%,d, feature nodes:%,d, subtrees:%,d, buckets: %,d]", tree.getClass().getSimpleName(), // toShortString(tree.getId()), // tree.size(), // tree.treesSize(), // tree.featuresSize(), // tree.numTrees(), // tree.bucketsSize()); StringBuilder sb = new StringBuilder(stree); sb.append("\n[\ntrees:"); tree.forEachTree(n -> sb.append('\n').append(n)); sb.append("\nfeatures:"); tree.forEachFeature(n -> sb.append('\n').append(n)); sb.append("\nbuckets:"); tree.forEachBucket(b -> sb.append("\n").append(b)); sb.append("\n]"); return sb.toString(); }
@Override protected Iterator<Node> resolveBucketEntries(ObjectId bucketId) { RevTree bucketTree = source.getTree(bucketId); if (bucketTree.bucketsSize() > 0) { return new FeatureBuckets(bucketTree); } if (bucketTree.featuresSize() > 0) { return new Features(bucketTree); } return Collections.emptyIterator(); } }
/** * Creates and returns an iterator of the joint lists of {@link RevTree#trees() trees} and * {@link RevTree#features() features} of the given {@code RevTree} whose iteration order is * given by the provided {@code comparator}. * * @return an iterator over the <b>direct</b> {@link RevTree#trees() trees} and * {@link RevTree#features() feature} children collections, in the order mandated by the * provided {@code comparator} */ public static Iterator<Node> children(RevTree tree, Comparator<Node> comparator) { checkNotNull(comparator); if (tree.treesSize() == 0) { return tree.features().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); }
/** * Convenience method to figure out if a tree is completely empty. * <p> * A tree is completely empty not only if {@code size() == 0}, but also if it has no child tree * nodes at all, since {@code size()} could be zero but the tree might have pointers to other * empty trees. * * @apiNote an empty tree id MUST be {@link #EMPTY_TREE_ID} * * @return {@code true} if the tree is completely empty (i.e. has no feature nor child tree * nodes) */ public default boolean isEmpty() { boolean empty = treesSize() == 0 && featuresSize() == 0 && bucketsSize() == 0; if (empty) { Preconditions.checkState(size() == 0L); Preconditions.checkState(EMPTY_TREE_ID.equals(getId())); } return empty; }
if (parent.treesSize() > 0 || parent.featuresSize() > 0) { if (parent.treesSize() > 0) { final int size = parent.treesSize(); if (parent.featuresSize() > 0) { final int size = parent.featuresSize(); for (int i = 0; i < size; i++) { Node node = parent.getFeature(i);
private Map<NodeId, DAGNode> lazyNodes(final RevTree tree) { if (tree.isEmpty()) { return Collections.emptyMap(); } final TreeCache treeCache = storageProvider.getTreeCache(); final int cacheTreeId = treeCache.getTreeId(tree).intValue(); Map<NodeId, DAGNode> dagNodes = new HashMap<>(); 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); } 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; }
private static void encode(final RevTree tree, ByteArrayOutputStream buff) throws IOException { DataOutput out = ByteStreams.newDataOutput(buff); final int offsetOfTreesNodeset; final int offsetOfFeaturesNodeset; final int offsetOfBuckets; final int offsetOfStringTable; final int offsetOfTail; StringTable stringTable = StringTable.unique(); Header.encode(out, tree); offsetOfTreesNodeset = tree.treesSize() == 0 ? 0 : buff.size(); if (tree.treesSize() > 0) { NodeSet.encode(out, tree.trees(), stringTable); } offsetOfFeaturesNodeset = tree.featuresSize() == 0 ? 0 : buff.size(); if (tree.featuresSize() > 0) { NodeSet.encode(out, tree.features(), stringTable); } offsetOfBuckets = tree.bucketsSize() == 0 ? 0 : buff.size(); BucketSet.encode(out, tree, stringTable); offsetOfStringTable = buff.size(); stringTable.encode(out); offsetOfTail = buff.size(); Tail.encode(out, // offsetOfTreesNodeset, // offsetOfFeaturesNodeset, // offsetOfBuckets, // offsetOfStringTable, // offsetOfTail); }
public void writeTree(RevTree tree, DataOutput data) throws IOException { writeUnsignedVarLong(tree.size(), data); writeUnsignedVarInt(tree.numTrees(), data); Envelope envBuff = new Envelope(); final int nFeatures = tree.featuresSize(); writeUnsignedVarInt(nFeatures, data); tree.forEachFeature(n -> writeNodeQuiet(n, data, envBuff)); final int nTrees = tree.treesSize(); writeUnsignedVarInt(nTrees, data); tree.forEachTree(n -> writeNodeQuiet(n, data, envBuff)); final int nBuckets = tree.bucketsSize(); writeUnsignedVarInt(nBuckets, data); tree.forEachBucket(bucket -> { try { writeBucket(bucket, data, envBuff); } catch (IOException e) { throw new RuntimeException(e); } }); }
private RevTree testCreateLeafTree(ObjectId id, long size, List<Node> trees, List<Node> features) { RevTree actual = factory.createTree(id, size, trees, features); assertNotNull(actual); RevTree expected = DEFAULT.createTree(id, size, trees, features); assertNotNull(expected); assertEquals(id, expected.getId()); assertEquals(size, expected.size()); assertEquals(trees.size(), expected.numTrees()); assertEquals(trees.size(), expected.treesSize()); assertEquals(features.size(), expected.featuresSize()); assertEquals(0, expected.bucketsSize()); RevObjectTestUtil.deepEquals(expected, actual); return actual; }
private RevTree testCreateTree(ObjectId id, @Nonnegative long size, int childTreeCount, SortedSet<Bucket> buckets) { RevTree actual = factory.createTree(id, size, childTreeCount, buckets); assertNotNull(actual); RevTree expected = DEFAULT.createTree(id, size, childTreeCount, buckets); assertNotNull(expected); assertEquals(id, expected.getId()); assertEquals(size, expected.size()); assertEquals(0, expected.treesSize()); assertEquals(0, expected.featuresSize()); assertEquals(childTreeCount, expected.numTrees()); assertEquals(buckets.size(), expected.bucketsSize()); RevObjectTestUtil.deepEquals(expected, actual); return actual; }
public static void deepEquals(@NonNull RevTree expected, @NonNull RevTree actual) { assertEquals(TYPE.TREE, actual.getType()); assertEquals(expected.getId(), expected.getId()); assertEquals(expected.size(), expected.size()); assertEquals(expected.numTrees(), expected.numTrees()); assertEquals(expected.treesSize(), expected.treesSize()); assertEquals(expected.featuresSize(), expected.featuresSize()); assertEquals(expected.bucketsSize(), expected.bucketsSize()); deepEquals(expected.trees(), actual.trees()); deepEquals(expected.features(), actual.features()); deepEquals(expected.getBuckets(), actual.getBuckets()); for (Bucket b : expected.getBuckets()) { deepEquals(b, actual.getBucket(b.getIndex()).orElse(null)); } }