Class ConcurrentRadixTree<O>
java.lang.Object
com.googlecode.concurrenttrees.radix.ConcurrentRadixTree<O>
- All Implemented Interfaces:
PrettyPrintable
,RadixTree<O>
,Serializable
- Direct Known Subclasses:
ConcurrentInvertedRadixTree.ConcurrentInvertedRadixTreeImpl
,ConcurrentReversedRadixTree.ConcurrentReverseRadixTreeImpl
,ConcurrentSuffixTree.ConcurrentSuffixTreeImpl
,LCSubstringSolver.ConcurrentSuffixTreeImpl
public class ConcurrentRadixTree<O>
extends Object
implements RadixTree<O>, PrettyPrintable, Serializable
An implementation of
RadixTree
which supports lock-free concurrent reads, and allows items to be added to and
to be removed from the tree atomically by background thread(s), without blocking reads.
Unlike reads, writes require locking of the tree (locking out other writing threads only; reading threads are never
blocked). Currently write locks are coarse-grained; in fact they are tree-level. In future branch-level write locks
might be added, but the current implementation is targeted at high concurrency read-mostly use cases.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Implementation of theKeyValuePair
interface.protected static class
Encapsulates a node and its associated key.(package private) static class
Encapsulates results of searching the tree for a node for which a given key is a prefix. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConcurrentRadixTree
(NodeFactory nodeFactory) Creates a newConcurrentRadixTree
which will use the givenNodeFactory
to create nodes. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
getClosestKeys
(CharSequence candidate) Returns a lazy iterable which returns the set of keys in the tree which are the closest match for the given candidate key.(package private) Iterable
<CharSequence> getDescendantKeys
(CharSequence startKey, Node startNode) Returns a lazy iterable which will returnCharSequence
keys for which the given key is a prefix.(package private) <O> Iterable
<KeyValuePair<O>> getDescendantKeyValuePairs
(CharSequence startKey, Node startNode) Returns a lazy iterable which will returnKeyValuePair
objects each containing a key and a value, for which the given key is a prefix of the key in theKeyValuePair
.(package private) <O> Iterable
<O> getDescendantValues
(CharSequence startKey, Node startNode) Returns a lazy iterable which will return values which are associated with keys in the tree for which the given key is a prefix.getKeysStartingWith
(CharSequence prefix) Returns a lazy iterable which returns the set of keys in the tree which start with the given prefix.getKeyValuePairsForClosestKeys
(CharSequence candidate) Returns a lazy iterable which returns the set ofKeyValuePair
s for keys and their associated values in the tree which are the closest match for the given candidate key.Returns a lazy iterable which returns the set ofKeyValuePair
s for keys and their associated values in the tree, where the keys start with the given prefix.getNode()
Returns the value associated with the given key (exact match), or returns null if no such value is associated with the key.getValuesForClosestKeys
(CharSequence candidate) Returns a lazy iterable which returns the set of values associated with keys in the tree which are the closest match for the given candidate key.Returns a lazy iterable which returns the set of values associated with keys in the tree which start with the given prefix.protected Iterable
<ConcurrentRadixTree.NodeKeyPair> lazyTraverseDescendants
(CharSequence startKey, Node startNode) Traverses the tree using depth-first, preordered traversal, starting at the given node, using lazy evaluation such that the next node is only determined when next() is called on the iterator returned.put
(CharSequence key, O value) Associates the given value with the given key; replacing any previous value associated with the key.putIfAbsent
(CharSequence key, O value) If a value is not already associated with the given key in the tree, associates the given value with the key; otherwise if an existing value is already associated, returns the existing value and does not overwrite it.(package private) Object
putInternal
(CharSequence key, Object value, boolean overwrite) Atomically adds the given value to the tree, creating a node for the value as necessary.protected void
boolean
remove
(CharSequence key) Removes the value associated with the given key (exact match).(package private) ConcurrentRadixTree.SearchResult
searchTree
(CharSequence key) Traverses the tree and finds the node which matches the longest prefix of the given key.int
size()
Counts the number of keys/values stored in the tree.protected CharSequence
transformKeyForResult
(CharSequence rawKey) A hook method which may be overridden by subclasses, to transform a key just before it is returned to the application, for example by thegetKeysStartingWith(CharSequence)
or thegetKeyValuePairsForKeysStartingWith(CharSequence)
methods.
-
Field Details
-
nodeFactory
-
root
-
writeLock
-
-
Constructor Details
-
ConcurrentRadixTree
Creates a newConcurrentRadixTree
which will use the givenNodeFactory
to create nodes.- Parameters:
nodeFactory
- An object which createsNode
objects on-demand, and which might return node implementations optimized for storing the values supplied to it for the creation of each node
-
-
Method Details
-
acquireWriteLock
protected void acquireWriteLock() -
releaseWriteLock
protected void releaseWriteLock() -
put
Associates the given value with the given key; replacing any previous value associated with the key. Returns the previous value associated with the key, if any. This operation is performed atomically. -
putIfAbsent
If a value is not already associated with the given key in the tree, associates the given value with the key; otherwise if an existing value is already associated, returns the existing value and does not overwrite it. This operation is performed atomically.- Specified by:
putIfAbsent
in interfaceRadixTree<O>
- Parameters:
key
- The key with which the specified value should be associatedvalue
- The value to associate with the key, which cannot be null- Returns:
- The existing value associated with the key, if there was one; otherwise null in which case the new value was successfully associated
-
getValueForExactKey
Returns the value associated with the given key (exact match), or returns null if no such value is associated with the key.- Specified by:
getValueForExactKey
in interfaceRadixTree<O>
- Parameters:
key
- The key with which a sought value might be associated- Returns:
- The value associated with the given key (exact match), or null if no value was associated with the key
-
getKeysStartingWith
Returns a lazy iterable which returns the set of keys in the tree which start with the given prefix. This is inclusive - if the given prefix is an exact match for a key in the tree, that key is also returned.- Specified by:
getKeysStartingWith
in interfaceRadixTree<O>
- Parameters:
prefix
- A prefix of sought keys in the tree- Returns:
- The set of keys in the tree which start with the given prefix, inclusive
-
getValuesForKeysStartingWith
Returns a lazy iterable which returns the set of values associated with keys in the tree which start with the given prefix. This is inclusive - if the given prefix is an exact match for a key in the tree, the value associated with that key is also returned. Note that although the same value might originally have been associated with multiple keys, the set returned does not contain duplicates (as determined by the value objects' implementation ofObject.equals(Object)
).- Specified by:
getValuesForKeysStartingWith
in interfaceRadixTree<O>
- Parameters:
prefix
- A prefix of keys in the tree for which associated values are sought- Returns:
- The set of values associated with keys in the tree which start with the given prefix, inclusive
-
getKeyValuePairsForKeysStartingWith
Returns a lazy iterable which returns the set ofKeyValuePair
s for keys and their associated values in the tree, where the keys start with the given prefix. This is inclusive - if the given prefix is an exact match for a key in the tree, theKeyValuePair
for that key is also returned.- Specified by:
getKeyValuePairsForKeysStartingWith
in interfaceRadixTree<O>
- Parameters:
prefix
- A prefix of keys in the tree for which associatedKeyValuePair
s are sought- Returns:
- The set of
KeyValuePair
s for keys in the tree which start with the given prefix, inclusive
-
remove
Removes the value associated with the given key (exact match). If no value is associated with the key, does nothing. -
getClosestKeys
Returns a lazy iterable which returns the set of keys in the tree which are the closest match for the given candidate key. Example:
Tree contains:Ford Focus
,Ford Mondeo
,BMW M3
getClosestKeys("Ford F150")
-> returnsFord Focus
,Ford Mondeo
This is inclusive - if the given candidate is an exact match for a key in the tree, that key is also returned.- Specified by:
getClosestKeys
in interfaceRadixTree<O>
- Parameters:
candidate
- A candidate key- Returns:
- The set of keys in the tree which most closely match the candidate key, inclusive
-
getValuesForClosestKeys
Returns a lazy iterable which returns the set of values associated with keys in the tree which are the closest match for the given candidate key. See {#getClosestKeys} for more details.- Specified by:
getValuesForClosestKeys
in interfaceRadixTree<O>
- Parameters:
candidate
- A candidate key- Returns:
- The set of values associated with keys in the tree which most closely match the candidate key, inclusive
-
getKeyValuePairsForClosestKeys
Returns a lazy iterable which returns the set ofKeyValuePair
s for keys and their associated values in the tree which are the closest match for the given candidate key. See {#getClosestKeys} for more details.- Specified by:
getKeyValuePairsForClosestKeys
in interfaceRadixTree<O>
- Parameters:
candidate
- A candidate key- Returns:
- The set of
KeyValuePair
s for keys and their associated values in the tree which most closely match the candidate key, inclusive
-
size
public int size()Counts the number of keys/values stored in the tree. In the current implementation, this is an expensive operation, having O(n) time complexity. -
putInternal
Atomically adds the given value to the tree, creating a node for the value as necessary. If the value is already stored for the same key, either overwrites the existing value, or simply returns the existing value, depending on the given value of theoverwrite
flag.- Parameters:
key
- The key against which the value should be storedvalue
- The value to store against the keyoverwrite
- If true, should replace any existing value, if false should not replace any existing value- Returns:
- The existing value for this key, if there was one, otherwise null
-
getDescendantKeys
Returns a lazy iterable which will returnCharSequence
keys for which the given key is a prefix. The results inherently will not contain duplicates (duplicate keys cannot exist in the tree). Note that this method internally convertsCharSequence
s toString
s, to avoid set equality issues, because equals() and hashCode() are not specified by the CharSequence API contract. -
getDescendantValues
Returns a lazy iterable which will return values which are associated with keys in the tree for which the given key is a prefix. -
getDescendantKeyValuePairs
Returns a lazy iterable which will returnKeyValuePair
objects each containing a key and a value, for which the given key is a prefix of the key in theKeyValuePair
. These results inherently will not contain duplicates (duplicate keys cannot exist in the tree). Note that this method internally convertsCharSequence
s toString
s, to avoid set equality issues, because equals() and hashCode() are not specified by the CharSequence API contract. -
lazyTraverseDescendants
protected Iterable<ConcurrentRadixTree.NodeKeyPair> lazyTraverseDescendants(CharSequence startKey, Node startNode) Traverses the tree using depth-first, preordered traversal, starting at the given node, using lazy evaluation such that the next node is only determined when next() is called on the iterator returned. The traversal algorithm uses iteration instead of recursion to allow deep trees to be traversed without requiring large JVM stack sizes. Each node that is encountered is returned from the iterator along with a key associated with that node, in a NodeKeyPair object. The key will be prefixed by the given start key, and will be generated by appending to the start key the edges traversed along the path to that node from the start node.- Parameters:
startKey
- The key which matches the given start nodestartNode
- The start node- Returns:
- An iterator which when iterated traverses the tree using depth-first, preordered traversal, starting at the given start node
-
transformKeyForResult
A hook method which may be overridden by subclasses, to transform a key just before it is returned to the application, for example by thegetKeysStartingWith(CharSequence)
or thegetKeyValuePairsForKeysStartingWith(CharSequence)
methods. This hook is expected to be used byReversedRadixTree
implementations, where keys are stored in the tree in reverse order but results should be returned in normal order. This default implementation simply returns the given key unmodified.- Parameters:
rawKey
- The raw key as stored in the tree- Returns:
- A transformed version of the key
-
searchTree
Traverses the tree and finds the node which matches the longest prefix of the given key. The node returned might be an exact match for the key, in which caseConcurrentRadixTree.SearchResult.charsMatched
will equal the length of the key. The node returned might be an inexact match for the key, in which caseConcurrentRadixTree.SearchResult.charsMatched
will be less than the length of the key. There are two types of inexact match:- An inexact match which ends evenly at the boundary between a node and its children (the rest of the key not matching any children at all). In this case if we we wanted to add nodes to the tree to represent the rest of the key, we could simply add child nodes to the node found.
-
An inexact match which ends in the middle of a the characters for an edge stored in a node (the key
matching only the first few characters of the edge). In this case if we we wanted to add nodes to the
tree to represent the rest of the key, we would have to split the node (let's call this node found: NF):
- Create a new node (N1) which will be the split node, containing the matched characters from the start of the edge in NF
- Create a new node (N2) which will contain the unmatched characters from the rest of the edge in NF, and copy the original edges from NF unmodified into N2
- Create a new node (N3) which will be the new branch, containing the unmatched characters from the rest of the key
- Add N2 as a child of N1
- Add N3 as a child of N1
- In the parent node of NF, replace the edge pointing to NF with an edge pointing instead to N1. If we do this step atomically, reading threads are guaranteed to never see "invalid" data, only either the old data or the new data
ConcurrentRadixTree.SearchResult.classification
is an enum value based on its classification of the match according to the descriptions above.- Parameters:
key
- a key for which the node matching the longest prefix of the key is required- Returns:
- A
ConcurrentRadixTree.SearchResult
object which contains the node matching the longest prefix of the key, its parent node, the number of characters of the key which were matched in total and within the edge of the matched node, and aConcurrentRadixTree.SearchResult.classification
of the match as described above
-
getNode
- Specified by:
getNode
in interfacePrettyPrintable
-