public class BTree<K,V>
extends java.lang.Object
implements java.io.Externalizable
BPage). In addition, the leaf nodes
directly contain (inline) the values associated with the keys, allowing a
single (or sequential) disk read of all the values on the page.
B+Trees are n-airy, yeilding log(N) search cost. They are self-balancing, preventing search performance degradation when the size of the tree grows.
Keys and associated values must be Serializable objects. The
user is responsible to supply a serializable Comparator object
to be used for the ordering of entries, which are also called Tuple.
The B+Tree allows traversing the keys in forward and reverse order using a
This implementation does not directly support duplicate keys, but it is possible to handle duplicates by inlining or referencing an object collection as a value.
There is no limit on key size or value size, but it is recommended to keep
both as small as possible to reduce disk I/O. This is especially true for
the key size, which impacts all non-leaf BPage objects.
| Modifier and Type | Class and Description |
|---|---|
(package private) class |
BTree.EmptyBrowser
PRIVATE INNER CLASS
Browser returning no element.
|
(package private) class |
BTree.MetaRoot
Used to point to the root page that the reader needs based on the reader's
read action context.
|
| Modifier and Type | Field and Description |
|---|---|
private java.util.concurrent.locks.Lock |
bigLock
Big lock snychronizing all actions
|
private BPage<K,V> |
bpageSerializer
Serializer used for BPages of this tree
|
(package private) int |
bTreeHeight
Height of the B+Tree.
|
(package private) java.util.Comparator<K> |
comparator
Comparator used to index entries.
|
private static boolean |
DEBUG |
static int |
DEFAULT_SIZE
Default page size (number of entries per node)
|
private boolean |
isActionCapable
TRUE if underlying record manager is snapshot capable
|
protected Serializer |
keySerializer
Serializer used to serialize index keys (optional)
|
private BTree.MetaRoot |
metaRoot
Meta root used to access versions of Btree root
|
protected java.util.concurrent.atomic.AtomicInteger |
nbEntries
Total number of entries in the BTree
|
protected int |
pageSize
Number of entries in each BPage.
|
private long |
recordId
This BTree's record ID in the PageManager.
|
protected RecordManager |
recordManager
Page manager used to persist changes in BPages
|
private long |
rootId
Record id of the root BPage
|
(package private) static long |
serialVersionUID
Version id for serialization.
|
protected Serializer |
valueSerializer
Serializer used to serialize index values (optional)
|
| Constructor and Description |
|---|
BTree()
No-argument constructor used by serialization.
|
BTree(RecordManager recman,
java.util.Comparator<K> comparator)
Create a new persistent BTree, with 16 entries per node.
|
BTree(RecordManager recman,
java.util.Comparator<K> comparator,
Serializer keySerializer,
Serializer valueSerializer)
Create a new persistent BTree, with 16 entries per node.
|
BTree(RecordManager recman,
java.util.Comparator<K> comparator,
Serializer keySerializer,
Serializer valueSerializer,
int pageSize)
Create a new persistent BTree with the given number of entries per node.
|
| Modifier and Type | Method and Description |
|---|---|
(package private) void |
abortAction(ActionContext context) |
(package private) ActionContext |
beginAction(boolean readOnly,
java.lang.String whoStarted) |
TupleBrowser<K,V> |
browse()
Get a browser initially positioned at the beginning of the BTree.
|
TupleBrowser<K,V> |
browse(K key)
Get a browser initially positioned just before the given key.
|
(package private) BPage<K,V> |
copyOnWrite(BPage<K,V> page) |
private BTree.MetaRoot |
copyOnWrite(BTree.MetaRoot oldMetaRoot) |
private void |
createInstance(RecordManager recordManager,
java.util.Comparator<K> comparator,
Serializer keySerializer,
Serializer valueSerializer,
int pageSize)
The real BTree constructor.
|
(package private) void |
endAction(ActionContext context) |
V |
find(K key)
Find the value associated with the given key.
|
Tuple<K,V> |
findGreaterOrEqual(K key)
Find the value associated with the given key, or the entry immediately
following this key in the ordered BTree.
|
java.util.Comparator<K> |
getComparator() |
(package private) BTree.MetaRoot |
getMetaRoot()
Returns the meta root that can be used to fetch the root page
|
long |
getRecordId()
Return the persistent record identifier of the BTree.
|
(package private) BPage<K,V> |
getRoot() |
(package private) BPage<K,V> |
getRoot(BTree.MetaRoot meta) |
java.lang.Object |
insert(K key,
V value,
boolean replace)
Insert an entry in the BTree.
|
BTree<K,V> |
load(RecordManager recman,
long recid)
Load a persistent BTree.
|
void |
readExternal(java.io.ObjectInput in)
Implement Externalizable interface.
|
V |
remove(K key)
Remove an entry with the given key from the BTree.
|
(package private) void |
setAsCurrentAction(ActionContext context) |
void |
setPageSize(int pageSize) |
void |
setValueSerializer(Serializer valueSerializer) |
int |
size()
Return the number of entries (size) of the BTree.
|
java.lang.String |
toString() |
(package private) void |
unsetAsCurrentAction(ActionContext context) |
private void |
updateMetaRoot(long newRootId,
int newTreeHeight) |
void |
writeExternal(java.io.ObjectOutput out)
Implement Externalizable interface.
|
private static final boolean DEBUG
static final long serialVersionUID
public static final int DEFAULT_SIZE
protected transient RecordManager recordManager
private transient long recordId
java.util.Comparator<K> comparator
protected Serializer keySerializer
protected Serializer valueSerializer
int bTreeHeight
private long rootId
protected int pageSize
protected java.util.concurrent.atomic.AtomicInteger nbEntries
private transient BPage<K,V> bpageSerializer
private transient boolean isActionCapable
private transient java.util.concurrent.locks.Lock bigLock
private transient BTree.MetaRoot metaRoot
public BTree()
public BTree(RecordManager recman, java.util.Comparator<K> comparator) throws java.io.IOException
recman - Record manager used for persistence.comparator - Comparator used to order index entriesjava.io.IOExceptionpublic BTree(RecordManager recman, java.util.Comparator<K> comparator, Serializer keySerializer, Serializer valueSerializer) throws java.io.IOException
recman - Record manager used for persistence.keySerializer - Serializer used to serialize index keys (optional)valueSerializer - Serializer used to serialize index values (optional)comparator - Comparator used to order index entriesjava.io.IOExceptionpublic BTree(RecordManager recman, java.util.Comparator<K> comparator, Serializer keySerializer, Serializer valueSerializer, int pageSize) throws java.io.IOException
recman - Record manager used for persistence.comparator - Comparator used to order index entrieskeySerializer - Serializer used to serialize index keys (optional)valueSerializer - Serializer used to serialize index values (optional)pageSize - Number of entries per page (must be even).java.io.IOExceptionprivate void createInstance(RecordManager recordManager, java.util.Comparator<K> comparator, Serializer keySerializer, Serializer valueSerializer, int pageSize) throws java.io.IOException
java.io.IOExceptionpublic void setPageSize(int pageSize)
public BTree<K,V> load(RecordManager recman, long recid) throws java.io.IOException
recman - RecordManager used to store the persistent btreerecid - Record id of the BTreejava.io.IOExceptionpublic java.lang.Object insert(K key, V value, boolean replace) throws java.io.IOException
The BTree cannot store duplicate entries. An existing entry can be
replaced using the replace flag. If an entry with the
same key already exists in the BTree, its value is returned.
key - Insert keyvalue - Insert valuereplace - Set to true to replace an existing key-value pair.java.io.IOExceptionpublic V remove(K key) throws java.io.IOException
key - Removal keyjava.io.IOExceptionpublic V find(K key) throws java.io.IOException
key - Lookup key.java.io.IOExceptionpublic Tuple<K,V> findGreaterOrEqual(K key) throws java.io.IOException
key - Lookup key.java.io.IOExceptionpublic TupleBrowser<K,V> browse() throws java.io.IOException
WARNING: If you make structural modifications to the BTree during browsing, you will get inconsistent browing results.
java.io.IOExceptionpublic TupleBrowser<K,V> browse(K key) throws java.io.IOException
WARNING: If you make structural modifications to the BTree during browsing, you will get inconsistent browsing results.
key - Key used to position the browser. If null, the browser
will be positioned after the last entry of the BTree.
(Null is considered to be an "infinite" key)java.io.IOExceptionpublic int size()
public long getRecordId()
BPage<K,V> getRoot() throws java.io.IOException
java.io.IOExceptionBPage<K,V> getRoot(BTree.MetaRoot meta) throws java.io.IOException
meta - The root to search forjava.io.IOExceptionBTree.MetaRoot getMetaRoot() throws java.io.IOException
java.io.IOException - If we had an exception during the fetch operationpublic void readExternal(java.io.ObjectInput in)
throws java.io.IOException,
java.lang.ClassNotFoundException
readExternal in interface java.io.Externalizablejava.io.IOExceptionjava.lang.ClassNotFoundExceptionpublic void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException
writeExternal in interface java.io.Externalizablejava.io.IOExceptionpublic void setValueSerializer(Serializer valueSerializer)
public java.util.Comparator<K> getComparator()
void setAsCurrentAction(ActionContext context)
void unsetAsCurrentAction(ActionContext context)
ActionContext beginAction(boolean readOnly, java.lang.String whoStarted)
void endAction(ActionContext context)
void abortAction(ActionContext context)
BPage<K,V> copyOnWrite(BPage<K,V> page) throws java.io.IOException
java.io.IOExceptionprivate BTree.MetaRoot copyOnWrite(BTree.MetaRoot oldMetaRoot)
private void updateMetaRoot(long newRootId,
int newTreeHeight)
throws java.io.IOException
java.io.IOExceptionpublic java.lang.String toString()
toString in class java.lang.Object