Insert a tuple into this BTreeFile, keeping the tuples in sorted order. May cause pages to split if the page where tuple t belongs is full. @param tid - the transaction id @param t - the tuple to insert @return a list of all pages that were dirtied by this operation. Could include many pages since
(TransactionId tid, Tuple t)
| 529 | * @see #splitLeafPage(TransactionId, Map, BTreeLeafPage, Field) |
| 530 | */ |
| 531 | public List<Page> insertTuple(TransactionId tid, Tuple t) |
| 532 | throws DbException, IOException, TransactionAbortedException { |
| 533 | Map<PageId, Page> dirtypages = new HashMap<>(); |
| 534 | |
| 535 | // get a read lock on the root pointer page and use it to locate the root page |
| 536 | BTreeRootPtrPage rootPtr = getRootPtrPage(tid, dirtypages); |
| 537 | BTreePageId rootId = rootPtr.getRootId(); |
| 538 | |
| 539 | if(rootId == null) { // the root has just been created, so set the root pointer to point to it |
| 540 | rootId = new BTreePageId(tableid, numPages(), BTreePageId.LEAF); |
| 541 | rootPtr = (BTreeRootPtrPage) getPage(tid, dirtypages, BTreeRootPtrPage.getId(tableid), Permissions.READ_WRITE); |
| 542 | rootPtr.setRootId(rootId); |
| 543 | } |
| 544 | |
| 545 | // find and lock the left-most leaf page corresponding to the key field, |
| 546 | // and split the leaf page if there are no more slots available |
| 547 | BTreeLeafPage leafPage = findLeafPage(tid, dirtypages, rootId, Permissions.READ_WRITE, t.getField(keyField)); |
| 548 | if(leafPage.getNumEmptySlots() == 0) { |
| 549 | leafPage = splitLeafPage(tid, dirtypages, leafPage, t.getField(keyField)); |
| 550 | } |
| 551 | |
| 552 | // insert the tuple into the leaf page |
| 553 | leafPage.insertTuple(t); |
| 554 | |
| 555 | return new ArrayList<>(dirtypages.values()); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Handle the case when a B+ tree page becomes less than half full due to deletions. |