Class BPlusTree<KeyType,KeyBufferType>
java.lang.Object
com.semedy.reasoner.edb.persistentstore.bplustree.BPlusTree<KeyType,KeyBufferType>
- Type Parameters:
KeyType-KeyBufferType-
- All Implemented Interfaces:
Closeable,AutoCloseable
This class implements a B+ Tree on disk.
The disk IO is handled by the BPlusTreeFile class.
The disk IO is handled by the BPlusTreeFile class.
- Author:
- angele
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionBPlusTree(String filename, int numberOfKeys, int keyLengthInBytes, KeysFactory<KeyType, KeyBufferType> keysFactory, LeafFactory<KeyType, KeyBufferType> leafFactory, int additionalHeadSpace, long[] grounds, boolean compress) Initializes a fixed key number and fixed key length treeBPlusTree(String filename, int numberOfKeys, KeysFactory<KeyType, KeyBufferType> keysFactory, int nodeLengthInBytes, LeafFactory<KeyType, KeyBufferType> leafFactory, long[] grounds, boolean compress) Initializes a variable key number, key length tree -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd a key (without a value)booleanInserting a record into the tree.voidbulkAddKey(KeyType key, long record, boolean shadow) bulk add a new recordbooleancheckSanity(String message, DataFile df, boolean shadow) check sanity of whole treevoidclose()close a bplus tree
Dirty nodes are flushed to file File is closed.booleancontainsKey(KeyType key, boolean shadow) Contains given keybooleancontainsKeyBuffer(KeyBufferType key, boolean shadow) Contains given keyvoidcreate()create a new bplus tree
Existing file is overwrittenbooleandelete()delete files
close must have been called firstvoidclose bulk loading modefirstKey(boolean shadow) voidfirstKeyBuffer(KeyBufferType key, boolean shadow) voidflush()voidFlushing dirty nodes to filelongAttempts to find the record for the key within the tree.longgetBuffer(KeyBufferType key, boolean shadow) Attempts to find the record for the key within the tree.getChild(int offset, BPlusInternalNode<KeyType, KeyBufferType> node, int index, boolean shadow) intgetFlags()byte[]Get all header infogetInsertPosition(KeyType key, boolean shadow) Attempts to find the insert position within the tree.
Returns position (leaf and index in leaf)getInsertPositionByBuffer(KeyBufferType key, boolean shadow) Attempts to find the insert position within the tree.
Returns position (leaf and index in leaf)getNode(int offset, boolean shadow) getRoot()getSearchResult(KeyType key, boolean shadow) Attempts to find the key within the tree.
Returns search result (leaf and index in leaf)getSearchResultBuffer(KeyBufferType key, boolean shadow) Attempts to find the key within the tree.
Returns search result (leaf and index in leaf)longsize on discbooleanisEmpty()The root should never be null, but we check anyway.iterator()nextKeyAfter(KeyType key, boolean shadow) voidnextKeyAfterBuffer(KeyBufferType key, boolean next, KeyBufferType result, boolean shadow) voidnextKeyAfterBuffer(KeyBufferType key, KeyBufferType result, boolean shadow) booleanopen()open an existing bplus treevoidprintTree(boolean shadow) Prints the tree.booleanRemoves the record from the tree.rightBrother(BPlusNode<KeyType, KeyBufferType> b, boolean shadow) get the right brother of a noderightSibling(BPlusNode<KeyType, KeyBufferType> b, boolean shadow) get the right sibling of a nodesearchFor(KeyBufferType key, boolean next, boolean shadow) Search for a key inside treesearchFor(KeyBufferType key, BPlusNode<KeyType, KeyBufferType> sRoot, boolean next, boolean shadow) Search for a key inside treebooleansearchForRecord(long record, boolean shadow) voidsetCompressCache(boolean b) booleanSet the record for a given key.voidsetWriteThrough(boolean writethrough) if writeThrough is true, all insert or remove operations are directly written to file
Thus after an insert or remove operation tree is
is always consistent on the disc
Otherwise nodes are cached and written to the file
by calling flushDirtyNodes or closelongsize()number of elements in bplus treevoidStart bulk loading.voidvoidvoidvoidwriteAdditionalHeadSpace(int additional) voidwriteFlags(int flags)
-
Field Details
-
_treeFile
the file
-
-
Constructor Details
-
BPlusTree
public BPlusTree(String filename, int numberOfKeys, int keyLengthInBytes, KeysFactory<KeyType, KeyBufferType> keysFactory, LeafFactory<KeyType, KeyBufferType> leafFactory, int additionalHeadSpace, long[] grounds, boolean compress) Initializes a fixed key number and fixed key length tree- Parameters:
filename- , file name for the bpus nodes filenumberOfKeys- , @keyLengthInBytes, length of keys in byteskeyLengthInBytes-keysFactory-leafFactory-additionalHeadSpace-
-
BPlusTree
public BPlusTree(String filename, int numberOfKeys, KeysFactory<KeyType, KeyBufferType> keysFactory, int nodeLengthInBytes, LeafFactory<KeyType, KeyBufferType> leafFactory, long[] grounds, boolean compress) Initializes a variable key number, key length tree- Parameters:
filename- , file name for the bpus nodes filenumberOfKeys-keysFactory-nodeLengthInBytes-leafFactory-
-
-
Method Details
-
setCompressCache
public void setCompressCache(boolean b) -
getSizeOnDisc
public long getSizeOnDisc()size on disc- Returns:
-
getHeader
public byte[] getHeader()Get all header info- Returns:
-
open
open an existing bplus tree- Throws:
IOExceptionEDBException
-
create
create a new bplus tree
Existing file is overwritten- Throws:
IOExceptionEDBException
-
close
close a bplus tree
Dirty nodes are flushed to file File is closed.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
flush
- Throws:
IOException
-
delete
public boolean delete()delete files
close must have been called first- Returns:
-
flushDirtyNodes
Flushing dirty nodes to file- Throws:
IOExceptionEDBException
-
size
public long size()number of elements in bplus tree- Returns:
-
printTree
Prints the tree.- Throws:
IOException
-
setWriteThrough
public void setWriteThrough(boolean writethrough) if writeThrough is true, all insert or remove operations are directly written to file
Thus after an insert or remove operation tree is
is always consistent on the disc
Otherwise nodes are cached and written to the file
by calling flushDirtyNodes or close- Parameters:
writethrough-
-
removeKey
Removes the record from the tree.- Parameters:
key-shadow-- Throws:
IOExceptionEDBException
-
isEmpty
public boolean isEmpty()The root should never be null, but we check anyway.- Returns:
- the status of the tree.
-
get
Attempts to find the record for the key within the tree.- Parameters:
key-shadow- , read from shadow tree- Returns:
- the record for the key, or null if not found.
- Throws:
IOException
-
getSearchResult
Attempts to find the key within the tree.
Returns search result (leaf and index in leaf)- Parameters:
key-shadow- , read from shadow tree- Returns:
- the record for the key, or null if not found.
- Throws:
IOException
-
getSearchResultBuffer
Attempts to find the key within the tree.
Returns search result (leaf and index in leaf)- Parameters:
key-shadow- , read from shadow tree- Returns:
- the record for the key, or null if not found.
- Throws:
IOException
-
getInsertPosition
Attempts to find the insert position within the tree.
Returns position (leaf and index in leaf)- Parameters:
key-shadow- , read from shadow tree- Returns:
- the insert position as BplusPosition
- Throws:
IOException
-
getInsertPositionByBuffer
Attempts to find the insert position within the tree.
Returns position (leaf and index in leaf)- Parameters:
key-shadow- , read from shadow tree- Returns:
- the insert position as BplusPosition
- Throws:
IOException
-
getBuffer
Attempts to find the record for the key within the tree.- Parameters:
key-shadow- , read from shadow tree- Returns:
- the record for the key, or null if not found.
- Throws:
IOException
-
containsKey
Contains given key- Parameters:
key-shadow- , read from shadow tree- Returns:
- Throws:
IOException
-
containsKeyBuffer
Contains given key- Parameters:
key-shadow- , read from shadow tree- Returns:
- Throws:
IOException
-
getRoot
-
rightSibling
public BPlusNode<KeyType,KeyBufferType> rightSibling(BPlusNode<KeyType, KeyBufferType> b, boolean shadow) throws IOExceptionget the right sibling of a node- Parameters:
b- , node the right sibling is looked forshadow- , read from shadow tree- Returns:
- the right sibling node
- Throws:
IOException
-
rightBrother
public BPlusNode<KeyType,KeyBufferType> rightBrother(BPlusNode<KeyType, KeyBufferType> b, boolean shadow) throws IOExceptionget the right brother of a node- Parameters:
b- , node the right sibling is looked forshadow- , read from shadow tree- Returns:
- the right sibling node
- Throws:
IOException
-
getChild
public BPlusNode<KeyType,KeyBufferType> getChild(int offset, BPlusInternalNode<KeyType, KeyBufferType> node, int index, boolean shadow) throws IOException- Throws:
IOException
-
getNode
- Throws:
IOException
-
searchForRecord
- Throws:
IOException
-
checkSanity
check sanity of whole tree- Returns:
- Throws:
IOException
-
searchFor
public BPlusPosition<KeyType,KeyBufferType> searchFor(KeyBufferType key, BPlusNode<KeyType, KeyBufferType> sRoot, boolean next, boolean shadow) throws IOExceptionSearch for a key inside tree- Parameters:
key- , the key to search fornext- , if next is true and then the next key after given key is searched forshadow- , read from shadow tree- Returns:
- Throws:
IOException
-
searchFor
public BPlusPosition<KeyType,KeyBufferType> searchFor(KeyBufferType key, boolean next, boolean shadow) throws IOException Search for a key inside tree- Parameters:
key- , the key to search fornext- , if next is true and then the next key after given key is searched forshadow- , read from shadow tree- Returns:
- Throws:
IOException
-
addKey
Add a key (without a value)- Parameters:
key-shadow-- Returns:
- Throws:
IOExceptionEDBException
-
addKey
Inserting a record into the tree.- Parameters:
key-value-shadow-- Throws:
IOExceptionEDBException
-
setRecord
Set the record for a given key.
Returns false if key is not there- Parameters:
key-value-shadow-- Throws:
IOExceptionEDBException
-
startBulkLoading
Start bulk loading. Loading is sped up tremendiously
Works only if bplus tree is empty, otherwise normal (cached)
inserts are performed- Throws:
IOExceptionEDBException
-
bulkAddKey
bulk add a new record- Parameters:
key-record-- Throws:
IOExceptionEDBException
-
finishBulkLoading
close bulk loading mode- Throws:
IOExceptionEDBException
-
firstKey
- Throws:
IOException
-
firstKeyBuffer
- Throws:
IOException
-
nextKey
- Throws:
IOException
-
nextKeyAfter
- Throws:
IOException
-
nextKeyAfterBuffer
public void nextKeyAfterBuffer(KeyBufferType key, KeyBufferType result, boolean shadow) throws IOException - Throws:
IOException
-
nextKeyAfterBuffer
public void nextKeyAfterBuffer(KeyBufferType key, boolean next, KeyBufferType result, boolean shadow) throws IOException - Throws:
IOException
-
iterator
-
transactionRollback
- Throws:
IOException
-
transactionBegin
- Throws:
IOException
-
transactionCommit
- Throws:
IOExceptionEDBException
-
writeFlags
- Throws:
IOException
-
writeAdditionalHeadSpace
- Throws:
IOException
-
getFlags
public int getFlags()
-