Package org.apache.cassandra.db.tries
Class Trie<T>
- java.lang.Object
-
- org.apache.cassandra.db.tries.Trie<T>
-
- Type Parameters:
T
- The content type of the trie.
- Direct Known Subclasses:
InMemoryReadTrie
,SlicedTrie
public abstract class Trie<T> extends java.lang.Object
Base class for tries. Normal users of tries will only use the public methods, which provide various transformations of the trie, conversion of its content to other formats (e.g. iterable of values), and several forms of processing. For any unimplemented data extraction operations one can build on theTrieEntriesWalker
(for-each processing) andTrieEntriesIterator
(to iterator) base classes, which provide the necessary mechanisms to handle walking the trie. The internal representation of tries using this interface is defined in theTrie.Cursor
interface. Cursors are a method of presenting the internal structure of a trie without representing nodes as objects, which is still useful for performing the basic operations on tries (iteration, slicing/intersection and merging). A cursor will list the nodes of a trie in order, together with information about the path that was taken to reach them. To begin traversal over a trie, one must retrieve a cursor by callingcursor()
. Because cursors are stateful, the traversal must always proceed from one thread. Should concurrent reads be required, separate calls tocursor()
must be made. Any modification that has completed before the construction of a cursor must be visible, but any later concurrent modifications may be presented fully, partially or not at all; this also means that if multiple are made, the cursor may see any part of any subset of them. Note: This model only supports depth-first traversals. We do not currently have a need for breadth-first walks. See Trie.md for further description of the trie representation model.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Trie.CollectionMergeResolver<T>
Resolver of content of merged nodes.protected static interface
Trie.Cursor<T>
A trie cursor.static interface
Trie.MergeResolver<T>
Resolver of content of merged nodes, used for two-source merges (i.e.protected static interface
Trie.ResettingTransitionsReceiver
Used byTrie.Cursor.advanceToContent(org.apache.cassandra.db.tries.Trie.ResettingTransitionsReceiver)
to track the transitions and backtracking taken.protected static interface
Trie.TransitionsReceiver
Used byTrie.Cursor.advanceMultiple(org.apache.cassandra.db.tries.Trie.TransitionsReceiver)
to feed the transitions taken.static interface
Trie.ValueConsumer<T>
Adapter interface providing the methods aTrie.Walker
to aConsumer
, so that the latter can be used withprocess(org.apache.cassandra.db.tries.Trie.Walker<T, R>)
.protected static interface
Trie.Walker<T,R>
A push interface for walking over the trie.
-
Field Summary
Fields Modifier and Type Field Description protected static ByteComparable.Version
BYTE_COMPARABLE_VERSION
-
Constructor Summary
Constructors Constructor Description Trie()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract Trie.Cursor<T>
cursor()
java.lang.String
dump()
Constuct a textual representation of the trie.java.lang.String
dump(java.util.function.Function<T,java.lang.String> contentToString)
Constuct a textual representation of the trie using the given content-to-string mapper.static <T> Trie<T>
empty()
java.util.Iterator<java.util.Map.Entry<ByteComparable,T>>
entryIterator()
Returns the ordered entry set of this trie's content in an iterator.java.lang.Iterable<java.util.Map.Entry<ByteComparable,T>>
entrySet()
Returns the ordered entry set of this trie's content as an iterable.void
forEachEntry(java.util.function.BiConsumer<ByteComparable,T> consumer)
Call the given consumer on all (path, content) pairs with non-null content in the trie in order.void
forEachValue(Trie.ValueConsumer<T> consumer)
Call the given consumer on all content values in the trie in order.static <T> Trie<T>
merge(java.util.Collection<? extends Trie<T>> sources, Trie.CollectionMergeResolver<T> resolver)
Constructs a view of the merge of multiple tries.static <T> Trie<T>
mergeDistinct(java.util.Collection<? extends Trie<T>> sources)
Constructs a view of the merge of multiple tries, where each source must have distinct keys.Trie<T>
mergeWith(Trie<T> other, Trie.MergeResolver<T> resolver)
Constructs a view of the merge of this trie with the given one.<R> R
process(Trie.Walker<T,R> walker)
Process the trie using the given Walker.static <T> Trie<T>
singleton(ByteComparable b, T v)
Returns a singleton trie mapping the given byte path to content.Trie<T>
subtrie(ByteComparable left, boolean includeLeft, ByteComparable right, boolean includeRight)
Returns a view of the subtrie containing everything in this trie whose keys fall between the given boundaries.Trie<T>
subtrie(ByteComparable left, ByteComparable right)
Returns a view of the subtrie containing everything in this trie whose keys fall between the given boundaries, left-inclusive and right-exclusive.static <T> Trie.CollectionMergeResolver<T>
throwingResolver()
Returns a resolver that throws whenever more than one of the merged nodes contains content.java.util.Iterator<T>
valueIterator()
Returns the ordered set of values of this trie in an iterator.java.lang.Iterable<T>
values()
Returns the ordered set of values of this trie as an iterable.java.lang.Iterable<T>
valuesUnordered()
Returns the values in any order.
-
-
-
Field Detail
-
BYTE_COMPARABLE_VERSION
protected static final ByteComparable.Version BYTE_COMPARABLE_VERSION
-
-
Method Detail
-
cursor
protected abstract Trie.Cursor<T> cursor()
-
forEachValue
public void forEachValue(Trie.ValueConsumer<T> consumer)
Call the given consumer on all content values in the trie in order.
-
forEachEntry
public void forEachEntry(java.util.function.BiConsumer<ByteComparable,T> consumer)
Call the given consumer on all (path, content) pairs with non-null content in the trie in order.
-
process
public <R> R process(Trie.Walker<T,R> walker)
Process the trie using the given Walker.
-
dump
public java.lang.String dump()
Constuct a textual representation of the trie.
-
dump
public java.lang.String dump(java.util.function.Function<T,java.lang.String> contentToString)
Constuct a textual representation of the trie using the given content-to-string mapper.
-
singleton
public static <T> Trie<T> singleton(ByteComparable b, T v)
Returns a singleton trie mapping the given byte path to content.
-
subtrie
public Trie<T> subtrie(ByteComparable left, boolean includeLeft, ByteComparable right, boolean includeRight)
Returns a view of the subtrie containing everything in this trie whose keys fall between the given boundaries. The view is live, i.e. any write to the source will be reflected in the subtrie. This method will not check its arguments for correctness. The resulting trie may be empty or throw an exception if the right bound is smaller than the left.- Parameters:
left
- the left bound for the returned subtrie. Ifnull
, the resulting subtrie is not left-bounded.includeLeft
- whetherleft
is an inclusive bound of not.right
- the right bound for the returned subtrie. Ifnull
, the resulting subtrie is not right-bounded.includeRight
- whetherright
is an inclusive bound of not.- Returns:
- a view of the subtrie containing all the keys of this trie falling between
left
(inclusively ifincludeLeft
) andright
(inclusively ifincludeRight
).
-
subtrie
public Trie<T> subtrie(ByteComparable left, ByteComparable right)
Returns a view of the subtrie containing everything in this trie whose keys fall between the given boundaries, left-inclusive and right-exclusive. The view is live, i.e. any write to the source will be reflected in the subtrie. This method will not check its arguments for correctness. The resulting trie may be empty or throw an exception if the right bound is smaller than the left. Equivalent to calling subtrie(left, true, right, false).- Parameters:
left
- the left bound for the returned subtrie. Ifnull
, the resulting subtrie is not left-bounded.right
- the right bound for the returned subtrie. Ifnull
, the resulting subtrie is not right-bounded.- Returns:
- a view of the subtrie containing all the keys of this trie falling between
left
(inclusively ifincludeLeft
) andright
(inclusively ifincludeRight
).
-
entrySet
public java.lang.Iterable<java.util.Map.Entry<ByteComparable,T>> entrySet()
Returns the ordered entry set of this trie's content as an iterable.
-
entryIterator
public java.util.Iterator<java.util.Map.Entry<ByteComparable,T>> entryIterator()
Returns the ordered entry set of this trie's content in an iterator.
-
values
public java.lang.Iterable<T> values()
Returns the ordered set of values of this trie as an iterable.
-
valueIterator
public java.util.Iterator<T> valueIterator()
Returns the ordered set of values of this trie in an iterator.
-
valuesUnordered
public java.lang.Iterable<T> valuesUnordered()
Returns the values in any order. For some tries this is much faster than the ordered iterable.
-
mergeWith
public Trie<T> mergeWith(Trie<T> other, Trie.MergeResolver<T> resolver)
Constructs a view of the merge of this trie with the given one. The view is live, i.e. any write to any of the sources will be reflected in the merged view. If there is content for a given key in both sources, the resolver will be called to obtain the combination. (The resolver will not be called if there's content from only one source.)
-
throwingResolver
public static <T> Trie.CollectionMergeResolver<T> throwingResolver()
Returns a resolver that throws whenever more than one of the merged nodes contains content. Can be used to merge tries that are known to have distinct content paths.
-
merge
public static <T> Trie<T> merge(java.util.Collection<? extends Trie<T>> sources, Trie.CollectionMergeResolver<T> resolver)
Constructs a view of the merge of multiple tries. The view is live, i.e. any write to any of the sources will be reflected in the merged view. If there is content for a given key in more than one sources, the resolver will be called to obtain the combination. (The resolver will not be called if there's content from only one source.)
-
mergeDistinct
public static <T> Trie<T> mergeDistinct(java.util.Collection<? extends Trie<T>> sources)
Constructs a view of the merge of multiple tries, where each source must have distinct keys. The view is live, i.e. any write to any of the sources will be reflected in the merged view. If there is content for a given key in more than one sources, the merge will throw an assertion error.
-
empty
public static <T> Trie<T> empty()
-
-