public static <V> Object[] build(Collection<V> source, Comparator<V> comparator, boolean sorted, UpdateFunction<V> updateF) { return build(source, source.size(), comparator, sorted, updateF); }
private static boolean isNodeWellFormed(Comparator<?> cmp, Object[] node, Object min, Object max) { Object previous = min; int end = getKeyEnd(node); for (int i = 0; i < end; i++) { Object current = node[i]; if (compare(cmp, previous, current) >= 0) return false; previous = current; } return compare(cmp, previous, max) < 0; }
private static boolean isWellFormed(Comparator<?> cmp, Object[] node, boolean isRoot, Object min, Object max) if (cmp != null && !isNodeWellFormed(cmp, node, min, max)) return false; if (isLeaf(node)) final int keyCount = getBranchKeyEnd(node); if ((!isRoot && keyCount < FAN_FACTOR / 2) || keyCount > FAN_FACTOR + 1) return false; int[] sizeMap = getSizeMap(node); for (int i = getChildStart(node); i < getChildEnd(node) ; i++) size += size(child) + 1; if (sizeMap[i - getChildStart(node)] != size) return false; Object localmax = i < node.length - 2 ? node[i - getChildStart(node)] : max; if (!isWellFormed(cmp, child, false, min, localmax)) return false; type |= isLeaf(child) ? 1 : 2; min = localmax;
static int getKeyEnd(Object[] node) { if (isLeaf(node)) return getLeafKeyEnd(node); else return getBranchKeyEnd(node); }
public static int size(Object[] tree) { if (isLeaf(tree)) return getLeafKeyEnd(tree); int length = tree.length; // length - 1 == getChildEnd == getPositionOfSizeMap // (length / 2) - 1 == getChildCount - 1 == position of full tree size // hard code this, as will be used often; return ((int[]) tree[length - 1])[(length / 2) - 1]; }
private static Object[] rotateLeft(final Object[] node, final int i) { final int keyEnd = BTree.getBranchKeyEnd(node); final Object[] nextNode = (Object[]) node[keyEnd + i]; final Object[] leftNeighbour = (Object[]) node[keyEnd + i - 1]; final int leftNeighbourEndKey = BTree.getKeyEnd(leftNeighbour); final boolean leaves = BTree.isLeaf(nextNode); final Object[] newChild = leaves ? null : (Object[]) leftNeighbour[BTree.getChildEnd(leftNeighbour) - 1]; final Object[] newNextNode = copyWithKeyAndChildInserted(nextNode, 0, node[i - 1], 0, newChild); node[i - 1] = leftNeighbour[leftNeighbourEndKey - 1]; node[keyEnd + i - 1] = copyWithKeyAndChildRemoved(leftNeighbour, leftNeighbourEndKey - 1, leftNeighbourEndKey, true); BTree.getSizeMap(node)[i - 1] -= leaves ? 1 : 1 + BTree.getSizeMap(newNextNode)[0]; return newNextNode; }
private static boolean isWellFormed(Comparator<?> cmp, Object[] node, boolean isRoot, Object min, Object max) { if (cmp != null && !isNodeWellFormed(cmp, node, min, max)) return false; if (isLeaf(node)) { if (isRoot) return node.length <= FAN_FACTOR; return node.length >= FAN_FACTOR / 2 && node.length <= FAN_FACTOR; } int type = 0; int childOffset = getBranchKeyEnd(node); // compare each child node with the branch element at the head of this node it corresponds with for (int i = childOffset; i < node.length; i++) { Object[] child = (Object[]) node[i]; Object localmax = i < node.length - 1 ? node[i - childOffset] : max; if (!isWellFormed(cmp, child, false, min, localmax)) return false; type |= isLeaf(child) ? 1 : 2; min = localmax; } return type < 3; // either all leaves or all branches but not a mix }
public void setValue(ColumnDefinition column, CellPath path, ByteBuffer value) { ColumnData current = (ColumnData) BTree.<Object>find(btree, ColumnDefinition.asymmetricColumnDataComparator, column); if (column.isSimple()) BTree.replaceInSitu(btree, ColumnData.comparator, current, ((Cell) current).withUpdatedValue(value)); else ((ComplexColumnData) current).setValue(path, value); }
private static int findFirstComplexIdx(Object[] tree) { // have fast path for common no-complex case int size = BTree.size(tree); if (!BTree.isEmpty(tree) && BTree.<ColumnDefinition>findByIndex(tree, size - 1).isSimple()) return size; return BTree.ceilIndex(tree, Comparator.naturalOrder(), FIRST_COMPLEX); }
public static long sizeOfStructureOnHeap(Object[] tree) { long size = ObjectSizes.sizeOfArray(tree); if (isLeaf(tree)) return size; for (int i = getChildStart(tree) ; i < getChildEnd(tree) ; i++) size += sizeOfStructureOnHeap((Object[]) tree[i]); return size; }
public Row lastRow() { Object[] tree = holder().tree; if (BTree.isEmpty(tree)) return null; return BTree.findByIndex(tree, BTree.size(tree) - 1); } }
private static int copyChildren(final Object[] from, final Object[] to, final int offset, final int skipIndex) { assert !BTree.isLeaf(from); final int start = BTree.getChildStart(from); final int childCount = BTree.getChildCount(from); if (skipIndex > 0) System.arraycopy(from, start, to, offset, skipIndex); if (skipIndex + 1 <= childCount) System.arraycopy(from, start + skipIndex + 1, to, offset + skipIndex, childCount - skipIndex - 1); return offset + childCount - 1; }
/** * Whether this columns is empty. * * @return whether this columns is empty. */ public boolean isEmpty() { return BTree.isEmpty(columns); }
/** * @return first index in a branch node containing child nodes */ static int getChildStart(Object[] branchNode) { return getBranchKeyEnd(branchNode); }
private static int copyKeys(final Object[] from, final Object[] to, final int offset) { final int keysCount = BTree.getKeyEnd(from); System.arraycopy(from, 0, to, offset, keysCount); return offset + keysCount; }
NodeCursor<K> descendToFirstChild(boolean forwards) { if (isLeaf()) { position = forwards ? 0 : getLeafKeyEnd(node) - 1; return null; } inChild = true; position = forwards ? 0 : getChildCount(node) - 1; return descend(); }
private ComplexColumnData transformAndFilter(DeletionTime newDeletion, Function<? super Cell, ? extends Cell> function) { Object[] transformed = BTree.transformAndFilter(cells, function); if (cells == transformed && newDeletion == complexDeletion) return this; if (newDeletion == DeletionTime.LIVE && BTree.isEmpty(transformed)) return null; return new ComplexColumnData(column, transformed, newDeletion); }
public ComplexColumnData getComplexColumnData(ColumnDefinition c) { assert c.isComplex(); return (ComplexColumnData) BTree.<Object>find(btree, ColumnDefinition.asymmetricColumnDataComparator, c); }