Class BTree
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic enumRepresents the direction of iteration.static classA pooled builder for constructing a tree in-order, and without needing any reconciliation. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intTheBRANCH_FACTORis defined as the maximum number of children of each branch, with between BRANCH_FACTOR/2-1 and BRANCH_FACTOR-1 keys being stored in every node.static final intstatic final intstatic final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <V,A> long accumulate(Object[] btree, BiLongAccumulator<A, V> accumulator, A arg, long initialValue) static <V,A> long accumulate(Object[] btree, BiLongAccumulator<A, V> accumulator, A arg, Comparator<V> comparator, V from, long initialValue) Walk the btree and accumulate a long value using the supplied accumulator function.static <V> longaccumulate(Object[] btree, LongAccumulator<V> accumulator, long initialValue) static <V> longaccumulate(Object[] btree, LongAccumulator<V> accumulator, Comparator<V> comparator, V from, long initialValue) static <V,A> void apply(Object[] btree, BiConsumer<A, V> function, A argument) Simple method to walk the btree forwards and apply a function till a stop condition is reachedstatic <V> voidSimple method to walk the btree forwards and apply a function till a stop condition is reachedstatic <V,A> void applyLeaf(Object[] btree, BiConsumer<A, V> function, A argument) static <C,K extends C, V extends C>
Object[]build(Collection<K> source) Deprecated.See CASSANDRA-15510static <C,K extends C, V extends C>
Object[]build(Collection<K> source, UpdateFunction<K, V> updateF) Deprecated.See CASSANDRA-15510static <C,I extends C, O extends C>
Object[]build(BulkIterator<I> source, int size, UpdateFunction<I, O> updateF) static <V> BTree.Builder<V>builder(Comparator<? super V> comparator) static <V> BTree.Builder<V>builder(Comparator<? super V> comparator, int initialCapacity) static <V> Vceil(Object[] btree, Comparator<? super V> comparator, V find) static <V> intceilIndex(Object[] btree, Comparator<? super V> comparator, V find) static intstatic Object[]empty()Returns an empty BTreestatic booleanstatic <V> BTree.FastBuilder<V>Build a tree of unknown size, in order.static <V> Vfind(Object[] node, Comparator<? super V> comparator, V find) static <V> VfindByIndex(Object[] tree, int index) static <V> intfindIndex(Object[] node, Comparator<? super V> comparator, V find) Honours result semantics ofArrays.binarySearch(long[], long), as though it were performed on the tree flattened into an arraystatic <V> Vfloor(Object[] btree, Comparator<? super V> comparator, V find) static <V> intfloorIndex(Object[] btree, Comparator<? super V> comparator, V find) static intstatic intstatic <V> Vhigher(Object[] btree, Comparator<? super V> comparator, V find) static <V> inthigherIndex(Object[] btree, Comparator<? super V> comparator, V find) static booleanstatic booleanChecks is the node is a leaf.static booleanisWellFormed(Object[] btree, Comparator<?> cmp) static <V> Iterable<V>static <V> Iterable<V>static <V> Iterable<V>static <V> Iterator<V>static <V> Iterator<V>static <V> Iterator<V>static <V> Vlower(Object[] btree, Comparator<? super V> comparator, V find) static <V> intlowerIndex(Object[] btree, Comparator<? super V> comparator, V find) static <V> voidreplaceInSitu(Object[] tree, int index, V replace) Modifies the provided btree directly.static <V> voidreplaceInSitu(Object[] node, Comparator<? super V> comparator, V find, V replace) Modifies the provided btree directly.static voidreverseInSitu(Object[] tree) static Object[]Create a BTree containing only the specified objectstatic intstatic longsizeOfStructureOnHeap(Object[] tree) static longsizeOnHeapOf(Object[] tree) static <K,V extends K>
BTreeSearchIterator<K,V> slice(Object[] btree, Comparator<? super K> comparator, int startIndex, int endIndex, BTree.Dir dir) static <K,V extends K>
BTreeSearchIterator<K,V> slice(Object[] btree, Comparator<? super K> comparator, K start, boolean startInclusive, K end, boolean endInclusive, BTree.Dir dir) static <K,V extends K>
BTreeSearchIterator<K,V> slice(Object[] btree, Comparator<? super K> comparator, K start, K end, BTree.Dir dir) static <K,V> BTreeSearchIterator<K, V> slice(Object[] btree, Comparator<? super K> comparator, BTree.Dir dir) Returns an Iterator over the entire treestatic intstatic intFill the target array with the contents of the provided subtree, in ascending order, starting at targetOffsetstatic Stringstatic <I,O> Object[] Takes a tree and transforms it using the provided function.static <I,I2, O> Object[] transformAndFilter(Object[] tree, BiFunction<? super I, ? super I2, ? extends O> apply, I2 param) Takes a tree and transforms it using the provided function, filtering out any null results.static <I,O> Object[] transformAndFilter(Object[] tree, Function<? super I, ? extends O> apply) Takes a tree and transforms it using the provided function, filtering out any null results.static inttreeIndexOfBranchKey(Object[] root, int keyIndex) static inttreeIndexOffsetOfChild(Object[] root, int childIndex) static inttreeIndexOfKey(Object[] root, int keyIndex) tree index => index of key wrt all items in the tree laid out seriallystatic inttreeIndexOfLeafKey(int keyIndex) static <Compare> Object[]update(Object[] toUpdate, Object[] insert, Comparator<? super Compare> comparator) static <Compare,Existing extends Compare, Insert extends Compare>
Object[]update(Object[] toUpdate, Object[] insert, Comparator<? super Compare> comparator, UpdateFunction<Insert, Existing> updateF) Insertsinsertintoupdate, applyingupdateFto each new item ininsert, as well as any matched items inupdate.static <Compare,Existing extends Compare, Insert extends Compare>
Object[]updateLeaves(Object[] unode, Object[] inode, Comparator<? super Compare> comparator, UpdateFunction<Insert, Existing> updateF) A fast tight-loop variant of updating one btree with another, when both are leaves.
-
Field Details
-
BRANCH_SHIFT
public static final int BRANCH_SHIFTTheBRANCH_FACTORis defined as the maximum number of children of each branch, with between BRANCH_FACTOR/2-1 and BRANCH_FACTOR-1 keys being stored in every node. This yields a minimum tree size of(BRANCH_FACTOR/2)^height - 1and a maximum tree size ofBRANCH_FACTOR^height - 1.Branches differ from leaves only in that they contain a suffix region containing the child nodes that occur either side of the keys, and a sizeMap in the last position, permitting seeking by index within the tree. Nodes are disambiguated by the length of the array that represents them: an even number is a branch, odd a leaf.
Leaf Nodes are represented by an odd-length array of keys, with the final element possibly null, i.e. Object[V1, V2, ...,null?]
Branch nodes: Object[V1, V2, ..., child[<V1.key], child[<V2.key], ..., child[< Inf], sizeMap] Each child is either a branch or leaf, i.e., always an Object[]. The key elements in a branch node occupy the first half of the array (minus one)
BTrees are immutable; updating one returns a new tree that reuses unmodified nodes.
There are no references back to a parent node from its children (this would make it impossible to re-use subtrees when modifying the tree, since the modified tree would need new parent references). Instead, we store these references in a Path as needed when navigating the tree.
-
MIN_KEYS
public static final int MIN_KEYS -
MAX_KEYS
public static final int MAX_KEYS -
STOP_SENTINEL_VALUE
public static final long STOP_SENTINEL_VALUE- See Also:
-
-
Constructor Details
-
BTree
public BTree()
-
-
Method Details
-
empty
Returns an empty BTree- Returns:
- an empty BTree
-
singleton
Create a BTree containing only the specified object- Returns:
- an new BTree containing only the specified object
-
build
@Deprecated(since="4.0") public static <C,K extends C, Object[] buildV extends C> (Collection<K> source) Deprecated.See CASSANDRA-15510 -
build
@Deprecated(since="4.0") public static <C,K extends C, Object[] buildV extends C> (Collection<K> source, UpdateFunction<K, V> updateF) Deprecated.See CASSANDRA-15510 -
build
public static <C,I extends C, Object[] buildO extends C> (BulkIterator<I> source, int size, UpdateFunction<I, O> updateF) -
update
public static <Compare> Object[] update(Object[] toUpdate, Object[] insert, Comparator<? super Compare> comparator) -
update
public static <Compare,Existing extends Compare, Object[] updateInsert extends Compare> (Object[] toUpdate, Object[] insert, Comparator<? super Compare> comparator, UpdateFunction<Insert, Existing> updateF) Insertsinsertintoupdate, applyingupdateFto each new item ininsert, as well as any matched items inupdate.Note that
UpdateFunction.noOpis assumed to indicate a lack of interest in which value survives. -
updateLeaves
public static <Compare,Existing extends Compare, Object[] updateLeavesInsert extends Compare> (Object[] unode, Object[] inode, Comparator<? super Compare> comparator, UpdateFunction<Insert, Existing> updateF) A fast tight-loop variant of updating one btree with another, when both are leaves. -
reverseInSitu
-
iterator
-
iterator
-
iterator
-
iterable
-
iterable
-
iterable
-
slice
public static <K,V> BTreeSearchIterator<K,V> slice(Object[] btree, Comparator<? super K> comparator, BTree.Dir dir) Returns an Iterator over the entire tree- Type Parameters:
V-- Parameters:
btree- the tree to iterate overdir- direction of iteration- Returns:
-
slice
public static <K,V extends K> BTreeSearchIterator<K,V> slice(Object[] btree, Comparator<? super K> comparator, K start, K end, BTree.Dir dir) - Parameters:
btree- the tree to iterate overcomparator- the comparator that defines the ordering over the items in the treestart- the beginning of the range to return, inclusive (in ascending order)end- the end of the range to return, exclusive (in ascending order)dir- if false, the iterator will start at the last item and move backwards- Returns:
- an Iterator over the defined sub-range of the tree
-
slice
public static <K,V extends K> BTreeSearchIterator<K,V> slice(Object[] btree, Comparator<? super K> comparator, int startIndex, int endIndex, BTree.Dir dir) - Parameters:
btree- the tree to iterate overcomparator- the comparator that defines the ordering over the items in the treestartIndex- the start index of the range to return, inclusiveendIndex- the end index of the range to return, inclusivedir- if false, the iterator will start at the last item and move backwards- Returns:
- an Iterator over the defined sub-range of the tree
-
slice
public static <K,V extends K> BTreeSearchIterator<K,V> slice(Object[] btree, Comparator<? super K> comparator, K start, boolean startInclusive, K end, boolean endInclusive, BTree.Dir dir) - Parameters:
btree- the tree to iterate overcomparator- the comparator that defines the ordering over the items in the treestart- low bound of the rangestartInclusive- inclusivity of lower boundend- high bound of the rangeendInclusive- inclusivity of higher bounddir- direction of iteration- Returns:
- an Iterator over the defined sub-range of the tree
-
find
- Returns:
- the item in the tree that sorts as equal to the search argument, or null if no such item
-
replaceInSitu
Modifies the provided btree directly. THIS SHOULD NOT BE USED WITHOUT EXTREME CARE as BTrees are meant to be immutable. Finds and replaces the item provided by index in the tree. -
replaceInSitu
public static <V> void replaceInSitu(Object[] node, Comparator<? super V> comparator, V find, V replace) Modifies the provided btree directly. THIS SHOULD NOT BE USED WITHOUT EXTREME CARE as BTrees are meant to be immutable. Finds and replaces the provided item in the tree. Both should sort as equal to each other (although this is not enforced) -
findIndex
Honours result semantics ofArrays.binarySearch(long[], long), as though it were performed on the tree flattened into an array- Returns:
- index of item in tree, or (-(insertion point) - 1) if not present
-
findByIndex
- Returns:
- the value at the index'th position in the tree, in tree order
-
lowerIndex
-
lower
-
floorIndex
-
floor
-
higherIndex
-
higher
-
ceilIndex
-
ceil
-
size
-
sizeOfStructureOnHeap
-
isLeaf
Checks is the node is a leaf.- Returns:
trueif the provided node is a leaf,falseif it is a branch.
-
isEmpty
-
depth
-
toArray
Fill the target array with the contents of the provided subtree, in ascending order, starting at targetOffset- Parameters:
tree- sourcetarget- arraytargetOffset- offset in target array- Returns:
- number of items copied (size of tree)
-
toArray
-
transformAndFilter
public static <I,I2, Object[] transformAndFilterO> (Object[] tree, BiFunction<? super I, ? super I2, ? extends O> apply, I2 param) Takes a tree and transforms it using the provided function, filtering out any null results. The result of any transformation must sort identically as their originals, wrt other results.If no modifications are made, the original is returned. NOTE: codewise *identical* to
transformAndFilter(Object[], Function) -
transformAndFilter
public static <I,O> Object[] transformAndFilter(Object[] tree, Function<? super I, ? extends O> apply) Takes a tree and transforms it using the provided function, filtering out any null results. The result of any transformation must sort identically as their originals, wrt other results.If no modifications are made, the original is returned.
An efficient transformAndFilter implementation suitable for a tree consisting of a single leaf root NOTE: codewise *identical* to
transformAndFilter(Object[], BiFunction, Object) -
transform
Takes a tree and transforms it using the provided function. The result of any transformation must sort identically as their originals, wrt other results.If no modifications are made, the original is returned.
-
equals
-
hashCode
-
toString
-
treeIndexOfKey
tree index => index of key wrt all items in the tree laid out seriallyThis version of the method permits requesting out-of-bounds indexes, -1 and size
- Parameters:
root- to calculate tree index withinkeyIndex- root-local index of key to calculate tree-index- Returns:
- the number of items preceding the key in the whole tree of root
-
treeIndexOfLeafKey
public static int treeIndexOfLeafKey(int keyIndex) - Parameters:
keyIndex- node-local index of the key to calculate index of- Returns:
- keyIndex; this method is here only for symmetry and clarity
-
treeIndexOfBranchKey
- Parameters:
root- to calculate tree-index withinkeyIndex- root-local index of key to calculate tree-index of- Returns:
- the number of items preceding the key in the whole tree of root
-
treeIndexOffsetOfChild
- Parameters:
root- to calculate tree-index withinchildIndex- root-local index of *child* to calculate tree-index of- Returns:
- the number of items preceding the child in the whole tree of root
-
builder
-
builder
-
applyLeaf
-
apply
Simple method to walk the btree forwards and apply a function till a stop condition is reachedPrivate method
- Parameters:
btree-function-
-
apply
Simple method to walk the btree forwards and apply a function till a stop condition is reachedPrivate method
- Parameters:
btree-function-
-
accumulate
public static <V,A> long accumulate(Object[] btree, BiLongAccumulator<A, V> accumulator, A arg, Comparator<V> comparator, V from, long initialValue) Walk the btree and accumulate a long value using the supplied accumulator function. Iteration will stop if the accumulator function returns the sentinel valueSTOP_SENTINEL_VALUEIf the optional from argument is not null, iteration will start from that value (or the one after it's insertion point if an exact match isn't found)
-
accumulate
public static <V> long accumulate(Object[] btree, LongAccumulator<V> accumulator, Comparator<V> comparator, V from, long initialValue) -
accumulate
public static <V> long accumulate(Object[] btree, LongAccumulator<V> accumulator, long initialValue) -
accumulate
public static <V,A> long accumulate(Object[] btree, BiLongAccumulator<A, V> accumulator, A arg, long initialValue) -
height
- Returns:
- the actual height of
tree
-
sizeOnHeapOf
-
isWellFormed
-
fastBuilder
Build a tree of unknown size, in order.Can be used with
reverseInSitu(java.lang.Object[])to build a tree in reverse.
-