Package com.github.basking2.sdsai.dsds
Class BTree<K,STOREKEY,V>
- java.lang.Object
-
- com.github.basking2.sdsai.dsds.BTree<K,STOREKEY,V>
-
- Type Parameters:
K- The user key type. SeeMapfor its use.STOREKEY- The storage key that K will be transformed in to. TheNodeStoremust be able to convert a K to a STOREKEY and must be able to generate a STOREKEY for storing internal nodes.V- The user value type. SettMapfor its use.
- All Implemented Interfaces:
BTreeMap<K,V>,java.util.Map<K,V>
public class BTree<K,STOREKEY,V> extends java.lang.Object implements BTreeMap<K,V>
A B-Tree that uses a
NodeStoreobject to persistNodes and user data to blob storage. This is tailored to NoSQL document / key-value systems.There is no concurrency built into this object so edits to a BTree must be coordinated through a locking mechanism or some otherwise atomic service. Developers looking to add transactionality should consider implementing a
NodeStoreto collect and atomically batch-update edits.
-
-
Constructor Summary
Constructors Constructor Description BTree(K rootKey, NodeStore<K,STOREKEY,V> nodeStore)BTree(K rootKey, NodeStore<K,STOREKEY,V> nodeStore, int minData)Attempt to retrieve the root node stored at therootKeyor, if none is found, construct a new empty root node withminData*2as the size of themaxDatavalue to theNode's constructors.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears the datastructure by doing a depth-first node traversal to delete all the user data and the node data from theNodeStore.booleancontainsKey(java.lang.Object key)Check theNodeStorefor the given key.booleancontainsValue(java.lang.Object value)voiddestroy()Whereas clearing will destroy all but the root node, this method callsclear()and deletes the root node key.voideachDepthFirst(NodeFunction<K,STOREKEY> callable)Recrusively depth-first executeNodeFunction.call(Node)on each.java.util.Set<java.util.Map.Entry<K,V>>entrySet()booleanequals(java.lang.Object o)Vget(java.lang.Object key)Retrieve the object from theNodeStore.BTreeLocation<K,STOREKEY>getEnd()NodeLocation<K,STOREKEY>getEndNode()java.util.Iterator<K>getIterator()Return aBTreeLocationwrapped in an iterator.BTreeLocation<K,STOREKEY>getLocation(K key)java.util.Iterator<Node<K,STOREKEY>>getNodeIterator()java.util.Iterator<K>getReverseIterator()Return aBTreeLocationwrapped in an iterator.java.util.Iterator<Node<K,STOREKEY>>getReverseNodeIterator()BTreeLocation<K,STOREKEY>getStart()NodeLocation<K,STOREKEY>getStartNode()inthashCode()Return the hash code of therootKeyused to create this tree.booleanisEmpty()Return true if the fetched root node has no data in it.java.util.Set<K>keySet()Vput(K key, V value)Put the value into the BTree stored in theNodeStore.voidputAll(java.util.Map<? extends K,? extends V> m)CallsMap.entrySet()andput(K, V)s each elment.booleanputKey(K key)This function encodes and writes the key into the data structure.Vremove(java.lang.Object keyObject)Removes the object from theNodeStoreand restructures the BTree.BTreeSelection<K,STOREKEY>select(K lower, K upper)Select an inclusive range from lower to upper.intsize()Iterate through the entire datastrucuture and counts each element.java.util.Collection<V>values()Iterate through then entire datastructure and retrieves each element.
-
-
-
Constructor Detail
-
BTree
public BTree(K rootKey, NodeStore<K,STOREKEY,V> nodeStore)
- Parameters:
rootKey-nodeStore-
-
BTree
public BTree(K rootKey, NodeStore<K,STOREKEY,V> nodeStore, int minData)
Attempt to retrieve the root node stored at the
rootKeyor, if none is found, construct a new empty root node withminData*2as the size of themaxDatavalue to theNode's constructors.The
rootKeywill never be altered where as internal nodes are not guaranteed to have the same key.
-
-
Method Detail
-
containsKey
public boolean containsKey(java.lang.Object key)
Check theNodeStorefor the given key.
-
clear
public void clear()
Clears the datastructure by doing a depth-first node traversal to delete all the user data and the node data from theNodeStore. This useseachDepthFirst(com.github.basking2.sdsai.dsds.node.NodeFunction<K, STOREKEY>)to visit each node. Notice that this will not delete the rootNodekey as other pieces of code may be accessing this object.
-
destroy
public void destroy()
Whereas clearing will destroy all but the root node, this method callsclear()and deletes the root node key.
-
eachDepthFirst
public void eachDepthFirst(NodeFunction<K,STOREKEY> callable)
Recrusively depth-first executeNodeFunction.call(Node)on each. This is used byclear()to destroy the datastructure in an orderly way. This is the same order provided bygetNodeIterator()andNodeLocation.
-
containsValue
public boolean containsValue(java.lang.Object value)
-
equals
public boolean equals(java.lang.Object o)
-
keySet
public java.util.Set<K> keySet()
-
get
public V get(java.lang.Object key)
Retrieve the object from theNodeStore.- Specified by:
getin interfacejava.util.Map<K,STOREKEY>- Returns:
- The user data returned by
NodeStore.loadData(Object).
-
hashCode
public int hashCode()
Return the hash code of therootKeyused to create this tree.
-
isEmpty
public boolean isEmpty()
Return true if the fetched root node has no data in it.
-
put
public V put(K key, V value)
Put the value into the BTree stored in theNodeStore. This first checks for the object inNodeStore. If it already exists then no BTree structural change is made and the object is replaced. The old value is returned.- Specified by:
putin interfacejava.util.Map<K,STOREKEY>- Parameters:
key- the user key which will be converted to a STOREKEY byNodeStore.convert(Object).value- The value stored.
-
putKey
public boolean putKey(K key)
This function encodes and writes the key into the data structure. This is useful for situations where the user data is already in a data store and we are just building additional indexes to it.
-
putAll
public void putAll(java.util.Map<? extends K,? extends V> m)
CallsMap.entrySet()andput(K, V)s each elment.
-
remove
public V remove(java.lang.Object keyObject)
Removes the object from theNodeStoreand restructures the BTree.
-
size
public int size()
Iterate through the entire datastrucuture and counts each element. Avoid using this.
-
values
public java.util.Collection<V> values()
Iterate through then entire datastructure and retrieves each element.
-
getStart
public BTreeLocation<K,STOREKEY> getStart()
-
getEnd
public BTreeLocation<K,STOREKEY> getEnd()
-
getIterator
public java.util.Iterator<K> getIterator()
Return aBTreeLocationwrapped in an iterator. This will callBTreeLocation.hasNext()andBTreeLocation.next()to iterate through this BTree.
-
getReverseIterator
public java.util.Iterator<K> getReverseIterator()
Return aBTreeLocationwrapped in an iterator. This will callBTreeLocation.prev()andBTreeLocation.hasPrev()to iterator through the BTree in reverse.
-
getStartNode
public NodeLocation<K,STOREKEY> getStartNode()
-
getEndNode
public NodeLocation<K,STOREKEY> getEndNode()
-
getLocation
public BTreeLocation<K,STOREKEY> getLocation(K key)
-
select
public BTreeSelection<K,STOREKEY> select(K lower, K upper)
Select an inclusive range from lower to upper.- Parameters:
lower- The lower boundary of the selection, inclusive.upper- The upper boundary of the selection, exclusive.
-
-