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 TypeMethodDescriptionboolean
Add a key (without a value)boolean
Inserting a record into the tree.void
bulkAddKey
(KeyType key, long record, boolean shadow) bulk add a new recordboolean
checkSanity
(String message, DataFile df, boolean shadow) check sanity of whole treevoid
close()
close a bplus tree
Dirty nodes are flushed to file File is closed.boolean
containsKey
(KeyType key, boolean shadow) Contains given keyboolean
containsKeyBuffer
(KeyBufferType key, boolean shadow) Contains given keyvoid
create()
create a new bplus tree
Existing file is overwrittenboolean
delete()
delete files
close must have been called firstvoid
close bulk loading modefirstKey
(boolean shadow) void
firstKeyBuffer
(KeyBufferType key, boolean shadow) void
flush()
void
Flushing dirty nodes to filelong
Attempts to find the record for the key within the tree.long
getBuffer
(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) int
getFlags()
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)long
size on discboolean
isEmpty()
The root should never be null, but we check anyway.iterator()
nextKeyAfter
(KeyType key, boolean shadow) void
nextKeyAfterBuffer
(KeyBufferType key, boolean next, KeyBufferType result, boolean shadow) void
nextKeyAfterBuffer
(KeyBufferType key, KeyBufferType result, boolean shadow) boolean
open()
open an existing bplus treevoid
printTree
(boolean shadow) Prints the tree.boolean
Removes 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 treeboolean
searchForRecord
(long record, boolean shadow) void
setCompressCache
(boolean b) boolean
Set the record for a given key.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 closelong
size()
number of elements in bplus treevoid
Start bulk loading.void
void
void
void
writeAdditionalHeadSpace
(int additional) void
writeFlags
(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:
IOException
EDBException
-
create
create a new bplus tree
Existing file is overwritten- Throws:
IOException
EDBException
-
close
close a bplus tree
Dirty nodes are flushed to file File is closed.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in 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:
IOException
EDBException
-
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:
IOException
EDBException
-
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:
IOException
EDBException
-
addKey
Inserting a record into the tree.- Parameters:
key
-value
-shadow
-- Throws:
IOException
EDBException
-
setRecord
Set the record for a given key.
Returns false if key is not there- Parameters:
key
-value
-shadow
-- Throws:
IOException
EDBException
-
startBulkLoading
Start bulk loading. Loading is sped up tremendiously
Works only if bplus tree is empty, otherwise normal (cached)
inserts are performed- Throws:
IOException
EDBException
-
bulkAddKey
bulk add a new record- Parameters:
key
-record
-- Throws:
IOException
EDBException
-
finishBulkLoading
close bulk loading mode- Throws:
IOException
EDBException
-
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:
IOException
EDBException
-
writeFlags
- Throws:
IOException
-
writeAdditionalHeadSpace
- Throws:
IOException
-
getFlags
public int getFlags()
-