public class BPlusTreeFile extends java.lang.Object implements DbFile
Modifier and Type | Field and Description |
---|---|
private java.io.File |
f |
private int |
keyField |
private int |
tableid |
private TupleDesc |
td |
Constructor and Description |
---|
BPlusTreeFile(java.io.File f,
int key,
TupleDesc td)
Constructs a B+ tree file backed by the specified file.
|
Modifier and Type | Method and Description |
---|---|
java.util.ArrayList<Page> |
deleteTuple(TransactionId tid,
Tuple t)
Delete a tuple from this BPlusTreeFile.
|
(package private) BPlusTreeLeafPage |
findLeafPage(TransactionId tid,
Field f,
BPlusTreePageId pid,
Permissions perm)
Recursive function which finds and locks the leaf page in the B+ tree corresponding to
the left-most page possibly containing the key field f.
|
private int |
getEmptyPage(TransactionId tid,
java.util.HashSet<Page> dirtypages)
Get the page number of the first empty page in this BPlusTreeFile.
|
java.io.File |
getFile()
Returns the File backing this BPlusTreeFile on disk.
|
int |
getId()
Returns an ID uniquely identifying this BPlusTreeFile.
|
private BPlusTreeRootPtrPage |
getRootPtrPage(TransactionId tid)
Get a read lock on the root pointer page.
|
TupleDesc |
getTupleDesc()
Returns the TupleDesc of the table stored in this DbFile.
|
private void |
handleMinOccupancyInternalPage(TransactionId tid,
BPlusTreeInternalPage page,
java.util.HashSet<Page> dirtypages)
Handle the case when an internal page becomes less than half full due to deletions.
|
private void |
handleMinOccupancyLeafPage(TransactionId tid,
BPlusTreeLeafPage page,
java.util.HashSet<Page> dirtypages)
Handle the case when a leaf page becomes less than half full due to deletions.
|
DbFileIterator |
indexIterator(TransactionId tid,
IndexPredicate ipred)
get the specified tuples from the file based on its IndexPredicate value on
behalf of the specified transaction.
|
java.util.ArrayList<Page> |
insertTuple(TransactionId tid,
Tuple t)
Insert a tuple into this BPlusTreeFile, keeping the tuples in sorted order.
|
DbFileIterator |
iterator(TransactionId tid)
Get an iterator for all tuples in this B+ tree file in sorted order.
|
int |
keyField()
Returns the index of the field that this B+ tree is keyed on
|
private void |
mergeInternalPages(TransactionId tid,
BPlusTreeInternalPage leftPage,
BPlusTreeInternalPage rightPage,
BPlusTreeInternalPage parent,
BPlusTreeEntry parentEntry,
java.util.HashSet<Page> dirtypages)
Merge two internal pages by moving all entries from the right page to the left page.
|
private void |
mergeLeafPages(TransactionId tid,
BPlusTreeLeafPage leftPage,
BPlusTreeLeafPage rightPage,
BPlusTreeInternalPage parent,
BPlusTreeEntry parentEntry,
java.util.HashSet<Page> dirtypages)
Merge two leaf pages by moving all tuples from the right page to the left page.
|
int |
numPages()
Returns the number of pages in this BPlusTreeFile.
|
Page |
readPage(PageId pid)
Read a page from the file on disk.
|
private void |
setEmptyPage(TransactionId tid,
java.util.HashSet<Page> dirtypages,
int emptyPageNo)
Mark a page in this BPlusTreeFile as empty.
|
private BPlusTreeInternalPage |
splitInternalPage(BPlusTreeInternalPage page,
TransactionId tid,
java.util.HashSet<Page> dirtypages,
Field field)
Recursively split internal pages as needed to make room for new entries.
|
private BPlusTreeLeafPage |
splitLeafPage(BPlusTreeLeafPage page,
TransactionId tid,
java.util.HashSet<Page> dirtypages,
Field field)
Split a leaf page to make room for new tuples and recursively split the parent node
as needed.
|
private void |
updateParentPointer(TransactionId tid,
BPlusTreePageId pid,
BPlusTreePageId child,
java.util.HashSet<Page> dirtypages)
Helper function to update the parent pointer of a node.
|
private void |
updateParentPointers(TransactionId tid,
BPlusTreeInternalPage page,
java.util.HashSet<Page> dirtypages)
Update the parent pointer of every child of the given page
|
void |
writePage(Page page)
Write a page to disk.
|
private final java.io.File f
private final TupleDesc td
private final int tableid
private int keyField
public BPlusTreeFile(java.io.File f, int key, TupleDesc td)
f
- - the file that stores the on-disk backing store for this B+ tree
file.key
- - the field which index is keyed ontd
- - the tuple descriptor of tuples in the filepublic java.io.File getFile()
public int getId()
public TupleDesc getTupleDesc()
getTupleDesc
in interface DbFile
public Page readPage(PageId pid)
public void writePage(Page page) throws java.io.IOException
public int numPages()
public int keyField()
BPlusTreeLeafPage findLeafPage(TransactionId tid, Field f, BPlusTreePageId pid, Permissions perm) throws DbException, TransactionAbortedException
tid
- - the transaction idf
- - the field to search forpid
- - the current page being searchedperm
- - the permissions with which to lock the leaf pageDbException
TransactionAbortedException
private BPlusTreeLeafPage splitLeafPage(BPlusTreeLeafPage page, TransactionId tid, java.util.HashSet<Page> dirtypages, Field field) throws DbException, java.io.IOException, TransactionAbortedException
page
- - the leaf page to splittid
- - the transaction iddirtypages
- - the list of dirty pages which should be updated with all new dirty pagesfield
- - the key field of the tuple to be inserted after the split is complete. Necessary to know
which of the two pages to return.DbException
java.io.IOException
TransactionAbortedException
private BPlusTreeInternalPage splitInternalPage(BPlusTreeInternalPage page, TransactionId tid, java.util.HashSet<Page> dirtypages, Field field) throws DbException, java.io.IOException, TransactionAbortedException
page
- - the internal page to splittid
- - transaction iddirtypages
- - the list of dirty pages which should be updated with all new dirty pagesfield
- - the key field of the entry to be inserted after the split is complete. Necessary to know
which of the two pages to return.DbException
java.io.IOException
TransactionAbortedException
private void updateParentPointer(TransactionId tid, BPlusTreePageId pid, BPlusTreePageId child, java.util.HashSet<Page> dirtypages) throws DbException, java.io.IOException, TransactionAbortedException
tid
- - transaction idpid
- - id of the parent nodechild
- - id of the child node to be updated with the parent pointerdirtypages
- - the list of dirty pages which should be updated with all new dirty pagesDbException
java.io.IOException
TransactionAbortedException
private void updateParentPointers(TransactionId tid, BPlusTreeInternalPage page, java.util.HashSet<Page> dirtypages) throws DbException, java.io.IOException, TransactionAbortedException
tid
- - transaction idpage
- - the parent pagedirtypages
- - the list of dirty pages which should be updated with all new dirty pagesDbException
java.io.IOException
TransactionAbortedException
public java.util.ArrayList<Page> insertTuple(TransactionId tid, Tuple t) throws DbException, java.io.IOException, TransactionAbortedException
insertTuple
in interface DbFile
tid
- - the transaction idt
- - the tuple to insertDbException
- if the tuple cannot be addedjava.io.IOException
- if the needed file can't be read/writtenTransactionAbortedException
private void handleMinOccupancyLeafPage(TransactionId tid, BPlusTreeLeafPage page, java.util.HashSet<Page> dirtypages) throws DbException, java.io.IOException, TransactionAbortedException
tid
- - the transaction idpage
- - the leaf page which is less than half fulldirtypages
- - the list of dirty pages which should be updated with all new dirty pagesDbException
java.io.IOException
TransactionAbortedException
private void handleMinOccupancyInternalPage(TransactionId tid, BPlusTreeInternalPage page, java.util.HashSet<Page> dirtypages) throws DbException, java.io.IOException, TransactionAbortedException
tid
- - the transaction idpage
- - the internal page which is less than half fulldirtypages
- - the list of dirty pages which should be updated with all new dirty pagesDbException
java.io.IOException
TransactionAbortedException
private void mergeLeafPages(TransactionId tid, BPlusTreeLeafPage leftPage, BPlusTreeLeafPage rightPage, BPlusTreeInternalPage parent, BPlusTreeEntry parentEntry, java.util.HashSet<Page> dirtypages) throws DbException, java.io.IOException, TransactionAbortedException
tid
- - the transaction idleftPage
- - the left leaf pagerightPage
- - the right leaf pageparent
- - the parent of the two pagesparentEntry
- - the entry in the parent corresponding to the leftPage and rightPagedirtypages
- - the list of dirty pages which should be updated with all new dirty pagesDbException
java.io.IOException
TransactionAbortedException
private void mergeInternalPages(TransactionId tid, BPlusTreeInternalPage leftPage, BPlusTreeInternalPage rightPage, BPlusTreeInternalPage parent, BPlusTreeEntry parentEntry, java.util.HashSet<Page> dirtypages) throws DbException, java.io.IOException, TransactionAbortedException
tid
- - the transaction idleftPage
- - the left internal pagerightPage
- - the right internal pageparent
- - the parent of the two pagesparentEntry
- - the entry in the parent corresponding to the leftPage and rightPagedirtypages
- - the list of dirty pages which should be updated with all new dirty pagesDbException
java.io.IOException
TransactionAbortedException
public java.util.ArrayList<Page> deleteTuple(TransactionId tid, Tuple t) throws DbException, java.io.IOException, TransactionAbortedException
deleteTuple
in interface DbFile
tid
- - the transaction idt
- - the tuple to deleteDbException
- if the tuple cannot be deleted or is not a member
of the filejava.io.IOException
TransactionAbortedException
private BPlusTreeRootPtrPage getRootPtrPage(TransactionId tid) throws DbException, java.io.IOException, TransactionAbortedException
tid
- - transaction idDbException
java.io.IOException
TransactionAbortedException
private int getEmptyPage(TransactionId tid, java.util.HashSet<Page> dirtypages) throws DbException, java.io.IOException, TransactionAbortedException
tid
- - transaction iddirtypages
- - the list of dirty pages which should be updated with all new dirty pagesDbException
java.io.IOException
TransactionAbortedException
private void setEmptyPage(TransactionId tid, java.util.HashSet<Page> dirtypages, int emptyPageNo) throws DbException, java.io.IOException, TransactionAbortedException
tid
- - transaction iddirtypages
- - the list of dirty pages which should be updated with all new dirty pagesemptyPageNo
- - the page number of the empty pageDbException
java.io.IOException
TransactionAbortedException
public DbFileIterator indexIterator(TransactionId tid, IndexPredicate ipred)
tid
- - the transaction idipred
- - the index predicate value to filter onpublic DbFileIterator iterator(TransactionId tid)