Package org.apache.cassandra.io.tries
Class Walker<CONCRETE extends Walker<CONCRETE>>
- java.lang.Object
-
- org.apache.cassandra.io.tries.Walker<CONCRETE>
-
- All Implemented Interfaces:
java.lang.AutoCloseable
- Direct Known Subclasses:
PartitionIndex.Reader
,ReverseValueIterator
,RowIndexReader
,TrieTermsDictionaryReader
,ValueIterator
@NotThreadSafe public class Walker<CONCRETE extends Walker<CONCRETE>> extends java.lang.Object implements java.lang.AutoCloseable
Thread-unsafe trie walking helper. This is analogous toRandomAccessReader
for tries -- takes an on-disk trie accessible via a supplied Rebufferer and lets user seek to nodes and work with them.Assumes data was written using page-aware builder and thus no node crosses a page and thus a buffer boundary.
See
org/apache/cassandra/io/sstable/format/bti/BtiFormat.md
for a description of the mechanisms of writing and reading an on-disk trie.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Walker.Extractor<RESULT,VALUE>
static interface
Walker.PayloadToString
static class
Walker.TransitionBytesCollector
-
Field Summary
Fields Modifier and Type Field Description protected java.nio.ByteBuffer
buf
static ByteComparable.Version
BYTE_COMPARABLE_VERSION
protected long
greaterBranch
protected long
lesserBranch
protected TrieNode
nodeType
static int
NONE
Value used to indicate a branch (e.g.protected long
position
protected long
root
-
Constructor Summary
Constructors Constructor Description Walker(Rebufferer source, long root)
Creates a walker.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
dumpTrie(java.io.PrintStream out, Walker.PayloadToString payloadReader, Version version)
int
follow(ByteComparable key)
Follows the given key while there are transitions in the trie for it.int
followWithGreater(ByteComparable key)
Follows the trie for a given key, remembering the closest greater branch.int
followWithLesser(ByteComparable key)
Follows the trie for a given key, remembering the closest lesser branch.ByteComparable
getMaxTerm()
ByteComparable
getMinTerm()
protected void
go(long position)
protected void
goMax(long pos)
protected void
goMin(long pos)
protected long
greaterTransition(int searchIndex, long defaultValue)
protected boolean
hasChildren()
protected boolean
hasPayload()
protected long
lastTransition()
protected long
lesserTransition(int searchIndex, long defaultValue)
protected int
nodeSize()
To be used only in analysis.protected int
nodeTypeOrdinal()
To be used only in analysis.protected int
payloadFlags()
protected int
payloadPosition()
<RESULT> RESULT
prefix(ByteComparable key, Walker.Extractor<RESULT,CONCRETE> extractor)
Takes a prefix of the given key.<RESULT> RESULT
prefixAndNeighbours(ByteComparable key, Walker.Extractor<RESULT,CONCRETE> extractor)
Follows the trie for a given key, taking a prefix (in the sense above) and searching for neighboring values.protected int
search(int transitionByte)
java.lang.String
toString()
protected long
transition(int childIndex)
protected int
transitionByte(int childIndex)
protected int
transitionRange()
-
-
-
Field Detail
-
NONE
public static int NONE
Value used to indicate a branch (e.g. lesser/greaterBranch) does not exist.
-
root
protected final long root
-
nodeType
protected TrieNode nodeType
-
buf
protected java.nio.ByteBuffer buf
-
position
protected long position
-
greaterBranch
protected long greaterBranch
-
lesserBranch
protected long lesserBranch
-
BYTE_COMPARABLE_VERSION
public static final ByteComparable.Version BYTE_COMPARABLE_VERSION
-
-
Constructor Detail
-
Walker
public Walker(Rebufferer source, long root)
Creates a walker. Rebufferer must be aligned and with a buffer size that is at least 4k.
-
-
Method Detail
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
-
go
protected final void go(long position)
-
payloadFlags
protected final int payloadFlags()
-
hasPayload
protected final boolean hasPayload()
-
payloadPosition
protected final int payloadPosition()
-
search
protected final int search(int transitionByte)
-
transition
protected final long transition(int childIndex)
-
lastTransition
protected final long lastTransition()
-
greaterTransition
protected final long greaterTransition(int searchIndex, long defaultValue)
-
lesserTransition
protected final long lesserTransition(int searchIndex, long defaultValue)
-
transitionByte
protected final int transitionByte(int childIndex)
-
transitionRange
protected final int transitionRange()
-
hasChildren
protected final boolean hasChildren()
-
goMax
protected final void goMax(long pos)
-
goMin
protected final void goMin(long pos)
-
follow
public int follow(ByteComparable key)
Follows the given key while there are transitions in the trie for it.- Returns:
- the first unmatched byte of the key, may be
ByteSource.END_OF_STREAM
-
followWithGreater
public int followWithGreater(ByteComparable key)
Follows the trie for a given key, remembering the closest greater branch. On return the walker is positioned at the longest prefix that matches the input (with or without payload), and min(greaterBranch) is the immediate greater neighbour.- Returns:
- the first unmatched byte of the key, may be
ByteSource.END_OF_STREAM
-
followWithLesser
public int followWithLesser(ByteComparable key)
Follows the trie for a given key, remembering the closest lesser branch. On return the walker is positioned at the longest prefix that matches the input (with or without payload), and max(lesserBranch) is the immediate lesser neighbour.- Returns:
- the first unmatched byte of the key, may be
ByteSource.END_OF_STREAM
-
prefix
public <RESULT> RESULT prefix(ByteComparable key, Walker.Extractor<RESULT,CONCRETE> extractor) throws java.io.IOException
Takes a prefix of the given key. The prefix is in the sense of a separator key match, i.e. it is only understood as valid if there are no greater entries in the trie (e.g. data at 'a' is ignored if 'ab' or 'abba' is in the trie when looking for 'abc' or 'ac', but accepted when looking for 'aa'). In order to not have to go back to data that may have exited cache, payloads are extracted when the node is visited (instead of saving the node's position), which requires an extractor to be passed as parameter.- Throws:
java.io.IOException
-
prefixAndNeighbours
public <RESULT> RESULT prefixAndNeighbours(ByteComparable key, Walker.Extractor<RESULT,CONCRETE> extractor) throws java.io.IOException
Follows the trie for a given key, taking a prefix (in the sense above) and searching for neighboring values. On return min(greaterBranch) and max(lesserBranch) are the immediate non-prefix neighbours for the sought value.Note: in a separator trie the closest smaller neighbour can be another prefix of the given key. This method does not take that into account. E.g. if trie contains "abba", "as" and "ask", looking for "asking" will find "ask" as the match, but max(lesserBranch) will point to "abba" instead of the correct "as". This problem can only occur if there is a valid prefix match.
- Throws:
java.io.IOException
-
getMaxTerm
public ByteComparable getMaxTerm()
-
getMinTerm
public ByteComparable getMinTerm()
-
nodeTypeOrdinal
protected int nodeTypeOrdinal()
To be used only in analysis.
-
nodeSize
protected int nodeSize()
To be used only in analysis.
-
dumpTrie
public void dumpTrie(java.io.PrintStream out, Walker.PayloadToString payloadReader, Version version) throws java.io.IOException
- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-