Module org.eclipse.jgit
Class TernarySearchTree<Value>
- java.lang.Object
-
- org.eclipse.jgit.internal.storage.memory.TernarySearchTree<Value>
-
- Type Parameters:
Value- type of values in this tree
public final class TernarySearchTree<Value> extends Object
A ternary search tree with String keys and generic values. TernarySearchTree is a type of trie (sometimes called a prefix tree) where nodes are arranged in a manner similar to a binary search tree, but with up to three children rather than the binary tree's limit of two. Like other prefix trees, a ternary search tree can be used as an associative map structure with the ability for incremental string search. However, ternary search trees are more space efficient compared to standard prefix trees, at the cost of speed. Keys must not be null or empty. Values cannot be null. This class is thread safe.- Since:
- 6.5
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceTernarySearchTree.Loader<Value>Loader to load key-value pairs to be cached in the tree
-
Constructor Summary
Constructors Constructor Description TernarySearchTree()Construct a new ternary search tree
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Remove all key value pairs from this treebooleancontains(String key)Check whether this tree contains the given key.intdelete(Iterable<String> delete)Delete entriesintdelete(String key)Delete a key-value pair.Valueget(String key)Get the value associated to a key ornull.Map<String,Value>getAll()Get all entries.List<Value>getAllValues()Get all values.Iterable<String>getKeys()Get all keys.Iterable<String>getKeysMatching(String pattern)Get keys matching given pattern using '?' as wildcard character.Iterable<String>getKeysWithPrefix(String prefix)Get keys starting with given prefixReadWriteLockgetLock()Get the lock guarding read and write access to the cache.List<Value>getValuesWithPrefix(String prefix)Get all values with given key prefixMap<String,Value>getWithPrefix(String prefix)Get all entries with given prefixintinsert(String key, Value value)Insert a key-value pair.intinsert(Map<String,Value> map)Insert map of key-value pairs.StringkeyLongestPrefixOf(String query)Find the key which is the longest prefix of the given query string.intreload(Iterable<Map.Entry<String,Value>> loader)Reload the tree entries provided by loaderintreplace(Iterable<Map.Entry<String,Value>> loader)Replace the tree with a new tree containing all entries provided by an iterableintsize()Get the number of key value pairs in this trie
-
-
-
Method Detail
-
getLock
public ReadWriteLock getLock()
Get the lock guarding read and write access to the cache.- Returns:
- lock guarding read and write access to the cache
-
replace
public int replace(Iterable<Map.Entry<String,Value>> loader)
Replace the tree with a new tree containing all entries provided by an iterable- Parameters:
loader- iterable providing key-value pairs to load- Returns:
- number of key-value pairs after replacing finished
-
reload
public int reload(Iterable<Map.Entry<String,Value>> loader)
Reload the tree entries provided by loader- Parameters:
loader- iterable providing key-value pairs to load- Returns:
- number of key-value pairs
-
delete
public int delete(Iterable<String> delete)
Delete entries- Parameters:
delete- iterable providing keys of entries to be deleted- Returns:
- number of key-value pairs
-
size
public int size()
Get the number of key value pairs in this trie- Returns:
- number of key value pairs in this trie
-
get
@Nullable public Value get(String key)
Get the value associated to a key ornull.- Parameters:
key- the key- Returns:
- the value associated to this key
-
contains
public boolean contains(String key)
Check whether this tree contains the given key.- Parameters:
key- a key- Returns:
- whether this tree contains this key
-
insert
public int insert(String key, Value value)
Insert a key-value pair. If the key already exists the old value is overwritten.- Parameters:
key- the keyvalue- the value- Returns:
- number of key-value pairs after adding the entry
-
insert
public int insert(Map<String,Value> map)
Insert map of key-value pairs. Values of existing keys are overwritten. Use this method to insert multiple key-value pairs.- Parameters:
map- map of key-value pairs to insert- Returns:
- number of key-value pairs after adding entries
-
delete
public int delete(String key)
Delete a key-value pair. Does nothing if the key doesn't exist.- Parameters:
key- the key- Returns:
- number of key-value pairs after the deletion
-
clear
public void clear()
Remove all key value pairs from this tree
-
keyLongestPrefixOf
@Nullable public String keyLongestPrefixOf(String query)
Find the key which is the longest prefix of the given query string.- Parameters:
query-- Returns:
- the key which is the longest prefix of the given query string or
nullif none exists.
-
getKeysWithPrefix
public Iterable<String> getKeysWithPrefix(String prefix)
Get keys starting with given prefix- Parameters:
prefix- key prefix- Returns:
- keys starting with given prefix
-
getWithPrefix
public Map<String,Value> getWithPrefix(String prefix)
Get all entries with given prefix- Parameters:
prefix- key prefix- Returns:
- entries with given prefix
-
getValuesWithPrefix
public List<Value> getValuesWithPrefix(String prefix)
Get all values with given key prefix- Parameters:
prefix- key prefix- Returns:
- entries with given prefix
-
-