Class TrieNode
The serialization methods take as argument a generic SerializationNode and provide a method typeFor
for choosing a suitable type to represent it, which can then be used to calculate size and write the node.
To read a file containing trie nodes, one would use at to identify the node type and then the various
read methods to retrieve the data. They all take a buffer (usually memory-mapped) containing the data, and a position
in it that identifies the node.
These node types do not specify any treatment of payloads. They are only concerned with providing 4 bits of
space for payloadFlags, and a way of calculating the position after the node. Users of this class by convention
use non-zero payloadFlags to indicate a payload exists, write it (possibly in flag-dependent format) at serialization
time after the node itself is written, and read it using the payloadPosition value.
To improve efficiency, multiple node types depending on the number of transitions are provided: -- payload only, which has no outgoing transitions -- single outgoing transition -- sparse, which provides a list of transition bytes with corresponding targets -- dense, where the transitions span a range of values and having the list (and the search in it) can be avoided
For each of the transition-carrying types we also have "in-page" versions where transition targets are the 4, 8 or 12 lowest bits of the position within the same page. To save one further byte, the single in-page versions using 4 or 12 bits cannot carry a payload.
This class is effectively an enumeration; abstract class permits instances to extend each other and reuse code.
See org/apache/cassandra/io/sstable/format/bti/BtiFormat.md for a description of the mechanisms of writing
and reading an on-disk trie.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic TrieNodeat(ByteBuffer src, int position) Returns the type of node stored at this position.abstract longgreaterTransition(ByteBuffer src, int position, long positionLong, int searchIndex, long defaultValue) Returns a transition that is higher than the index returned bysearch.longlastTransition(ByteBuffer src, int position, long positionLong) Returns the highest transition for this node, or NONE if none exist (PAYLOAD_ONLY nodes).abstract longlesserTransition(ByteBuffer src, int position, long positionLong, int searchIndex, long defaultValue) Returns a transition that is lower than the index returned bysearch.intpayloadFlags(ByteBuffer src, int position) Returns the 4 payload flag bits.abstract intpayloadPosition(ByteBuffer src, int position) Return the position just after the node, where the payload is usually stored.abstract intsearch(ByteBuffer src, int position, int transitionByte) Returns search index for the given byte in the node.abstract voidserialize(DataOutputPlus out, SerializationNode<?> node, int payloadBits, long nodePosition) Serializes the node.abstract intsizeofNode(SerializationNode<?> node) Returns the size needed to serialize this node.toString()longtransition(ByteBuffer src, int position, long positionLong, int childIndex) Returns position of node to transition to for the given search index.abstract inttransitionByte(ByteBuffer src, int position, int childIndex) Returns the byte value for this child index, or Integer.MAX_VALUE if there are no transitions with this index or higher to permit listing the children without needing to call transitionRange.abstract inttransitionRange(ByteBuffer src, int position) Returns the upper childIndex limit.static TrieNodetypeFor(SerializationNode<?> node, long nodePosition) Returns a node type that is suitable to store the node.
-
Field Details
-
NONE
public static final int NONEValue used to indicate a branch (e.g. for transition and lastTransition) does not exist.- See Also:
-
-
Method Details
-
at
Returns the type of node stored at this position. It can then be used to call the methods below. -
payloadFlags
Returns the 4 payload flag bits. Node types that cannot carry a payload return 0. -
payloadPosition
Return the position just after the node, where the payload is usually stored. -
search
Returns search index for the given byte in the node. If exact match is present, this is >= 0, otherwise as in binary search. -
transitionRange
Returns the upper childIndex limit. Calling transition with values 0...transitionRange - 1 is valid. -
transitionByte
Returns the byte value for this child index, or Integer.MAX_VALUE if there are no transitions with this index or higher to permit listing the children without needing to call transitionRange.- Parameters:
childIndex- must be >= 0, though it is allowed to pass a value greater thantransitionRange - 1
-
transition
Returns position of node to transition to for the given search index. Argument must be positive. May return NONE if a transition with that index does not exist (DENSE nodes). Position is the offset of the node within the ByteBuffer. positionLong is its global placement, which is the base for any offset calculations.- Parameters:
positionLong- although it seems to be obvious, this argument must be "real", that is, each child must have the calculated absolute position >= 0, otherwise the behaviour of this method is undefinedchildIndex- must be >= 0 and <transitionRange(ByteBuffer, int)- note that this is not validated and behaviour of this method is undefined for values outside of that range
-
lastTransition
Returns the highest transition for this node, or NONE if none exist (PAYLOAD_ONLY nodes). -
greaterTransition
public abstract long greaterTransition(ByteBuffer src, int position, long positionLong, int searchIndex, long defaultValue) Returns a transition that is higher than the index returned bysearch. This may not exist (if the argument was higher than the last transition byte), in which case this returns the givendefaultValue. -
lesserTransition
public abstract long lesserTransition(ByteBuffer src, int position, long positionLong, int searchIndex, long defaultValue) Returns a transition that is lower than the index returned bysearch. ReturnsdefaultValueforsearchIndexequals 0 or -1 as lesser transition for those indexes does not exist. -
typeFor
Returns a node type that is suitable to store the node. -
sizeofNode
Returns the size needed to serialize this node. -
serialize
public abstract void serialize(DataOutputPlus out, SerializationNode<?> node, int payloadBits, long nodePosition) throws IOException Serializes the node. All transition target positions must already have been defined.payloadBitsmust be four bits.- Throws:
IOException
-
toString
-