private long computeMemorySizeInBytes(AbstractNode root) { if (root.getLevel() == 0) { return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((ItemBoundable) child)).sum(); } return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((AbstractNode) child)).sum(); }
private boolean removeItem(AbstractNode node, Object item) { Boundable childToRemove = null; for (Iterator i = node.getChildBoundables().iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (childBoundable instanceof ItemBoundable) { if ( ((ItemBoundable) childBoundable).getItem() == item) childToRemove = childBoundable; } } if (childToRemove != null) { node.getChildBoundables().remove(childToRemove); return true; } return false; }
public static void printLevels(TestTree t, PrintStream out) { for (int i = 0; i <= t.getRoot().getLevel(); i++) { printBoundables(t.boundablesAtLevel(i), "Level " + i, out); } }
/** * Sorts the childBoundables then divides them into groups of size M, where * M is the node capacity. */ protected List createParentBoundables(List childBoundables, int newLevel) { Assert.isTrue(!childBoundables.isEmpty()); ArrayList parentBoundables = new ArrayList(); parentBoundables.add(createNode(newLevel)); ArrayList sortedChildBoundables = new ArrayList(childBoundables); Collections.sort(sortedChildBoundables, getComparator()); for (Iterator i = sortedChildBoundables.iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (lastNode(parentBoundables).getChildBoundables().size() == getNodeCapacity()) { parentBoundables.add(createNode(newLevel)); } lastNode(parentBoundables).addChildBoundable(childBoundable); } return parentBoundables; }
public ReferencedEnvelope getBounds() { CoordinateReferenceSystem crs = schema.getCoordinateReferenceSystem(); Envelope bounds = (Envelope) index.getRoot().getBounds(); return new ReferencedEnvelope(bounds, crs); }
/** * Gets the bounds of this node * * @return the object representing bounds in this index */ public Object getBounds() { if (bounds == null) { bounds = computeBounds(); } return bounds; }
/** * Tests whether the index contains any items. * This method does not build the index, * so items can still be inserted after it has been called. * * @return true if the index does not contain any items */ public boolean isEmpty() { if (! built) return itemBoundables.isEmpty(); return root.isEmpty(); }
protected int depth(AbstractNode node) { int maxChildDepth = 0; for (Iterator i = node.getChildBoundables().iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (childBoundable instanceof AbstractNode) { int childDepth = depth((AbstractNode) childBoundable); if (childDepth > maxChildDepth) maxChildDepth = childDepth; } } return maxChildDepth + 1; }
public void testEmptyTree() { TestTree t = new TestTree(2); t.build(); assertEquals(0, t.getRoot().getLevel()); assertEquals(1, t.boundablesAtLevel(0).size()); assertEquals(0, t.boundablesAtLevel(1).size()); assertEquals(0, t.boundablesAtLevel(-1).size()); assertEquals(0, t.query(0.5, 0.5).size()); } }
@Override protected Iterator openIterator() { List features; synchronized (CachingFeatureSource.this) { try { if (index == null || dirty || !isSubQuery(query)) { fillCache(query); } if (queryBounds != null) { features = index.query(queryBounds); } else { features = index.query((Envelope) index.getRoot().getBounds()); } } catch (Exception e) { throw new RuntimeException("Failed to get data", e); } } Iterator it = features.iterator(); if (query.getFilter() != null && Filter.INCLUDE.equals(query.getFilter())) { it = new FilteringIterator<Feature>(it, query.getFilter()); } if (targetSchema != sourceSchema) { it = new ReTypingIterator(it, sourceSchema, targetSchema); } return it; }
private long computeMemorySizeInBytes(AbstractNode root) { if (root.getLevel() == 0) { return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((ItemBoundable) child)).sum(); } return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((AbstractNode) child)).sum(); }
protected int size(AbstractNode node) { int size = 0; for (Iterator i = node.getChildBoundables().iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (childBoundable instanceof AbstractNode) { size += size((AbstractNode) childBoundable); } else if (childBoundable instanceof ItemBoundable) { size += 1; } } return size; }
public void test() { TestTree t = new TestTree(2); t.insert(2, 6, "A"); t.insert(2, 4, "B"); t.insert(2, 3, "C"); t.insert(2, 4, "D"); t.insert(0, 1, "E"); t.insert(2, 4, "F"); t.insert(5, 6, "G"); t.build(); assertEquals(2, t.getRoot().getLevel()); assertEquals(4, t.boundablesAtLevel(0).size()); assertEquals(2, t.boundablesAtLevel(1).size()); assertEquals(1, t.boundablesAtLevel(2).size()); assertEquals(1, t.query(0.5, 0.5).size()); assertEquals(0, t.query(1.5, 1.5).size()); assertEquals(2, t.query(4.5, 5.5).size()); }
/** * Removes an item from the tree. * (Builds the tree, if necessary.) */ protected boolean remove(Object searchBounds, Object item) { build(); if (getIntersectsOp().intersects(root.getBounds(), searchBounds)) { return remove(searchBounds, root, item); } return false; }
private long computeMemorySizeInBytes(AbstractNode root) { if (root.getLevel() == 0) { return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((ItemBoundable) child)).sum(); } return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((AbstractNode) child)).sum(); }
private static void addBounds(Boundable bnd, List bounds, GeometryFactory factory) { // don't include bounds of leaf nodes if (! (bnd instanceof AbstractNode)) return; Envelope env = (Envelope) bnd.getBounds(); bounds.add(factory.toGeometry(env)); if (bnd instanceof AbstractNode) { AbstractNode node = (AbstractNode) bnd; List children = node.getChildBoundables(); for (Iterator i = children.iterator(); i.hasNext(); ) { Boundable child = (Boundable) i.next(); addBounds(child, bounds, factory); } } }
/** * Also builds the tree, if necessary. */ protected List query(Object searchBounds) { build(); ArrayList matches = new ArrayList(); if (isEmpty()) { //Assert.isTrue(root.getBounds() == null); return matches; } if (getIntersectsOp().intersects(root.getBounds(), searchBounds)) { queryInternal(searchBounds, root, matches); } return matches; }
/** * @param level -1 to get items */ private void boundablesAtLevel(int level, AbstractNode top, Collection boundables) { Assert.isTrue(level > -2); if (top.getLevel() == level) { boundables.add(top); return; } for (Iterator i = top.getChildBoundables().iterator(); i.hasNext(); ) { Boundable boundable = (Boundable) i.next(); if (boundable instanceof AbstractNode) { boundablesAtLevel(level, (AbstractNode)boundable, boundables); } else { Assert.isTrue(boundable instanceof ItemBoundable); if (level == -1) { boundables.add(boundable); } } } return; }
private List itemsTree(AbstractNode node) { List valuesTreeForNode = new ArrayList(); for (Iterator i = node.getChildBoundables().iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (childBoundable instanceof AbstractNode) { List valuesTreeForChild = itemsTree((AbstractNode) childBoundable); // only add if not null (which indicates an item somewhere in this tree if (valuesTreeForChild != null) valuesTreeForNode.add(valuesTreeForChild); } else if (childBoundable instanceof ItemBoundable) { valuesTreeForNode.add(((ItemBoundable)childBoundable).getItem()); } else { Assert.shouldNeverReachHere(); } } if (valuesTreeForNode.size() <= 0) return null; return valuesTreeForNode; }
/** * Also builds the tree, if necessary. */ protected void query(Object searchBounds, ItemVisitor visitor) { build(); if (isEmpty()) { // nothing in tree, so return //Assert.isTrue(root.getBounds() == null); return; } if (getIntersectsOp().intersects(root.getBounds(), searchBounds)) { queryInternal(searchBounds, root, visitor); } }