-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathHeapFile.cpp
40 lines (36 loc) · 1.31 KB
/
HeapFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <db/HeapFile.h>
#include <db/Page.h>
#include <db/PageId.h>
#include <db/HeapPage.h>
#include <db/BufferPool.h>
#include <db/Database.h>
#include <unistd.h>
using namespace db;
std::vector<Page *> HeapFile::insertTuple(TransactionId tid, Tuple &t) {
BufferPool &bufferPool = Database::getBufferPool();
HeapPage *page = nullptr;
for (int i = 0; i < numPages; i++) {
auto *pid = new HeapPageId(tableid, i);
auto *currPage = dynamic_cast<HeapPage *>(bufferPool.getPage(pid));
if (currPage->getNumEmptySlots() > 0) {
page = dynamic_cast<HeapPage *>(bufferPool.getPage(pid));
break;
}
}
if (page == nullptr) {
page = new HeapPage(HeapPageId(tableid, numPages), static_cast<uint8_t *>(HeapPage::createEmptyPageData()));
numPages++;
}
page->insertTuple(&t);
return {page};
}
std::vector<Page *> HeapFile::deleteTuple(TransactionId tid, Tuple &t) {
auto *page = dynamic_cast<HeapPage *>(Database::getBufferPool().getPage(t.getRecordId()->getPageId()));
page->deleteTuple(&t);
return {page};
}
void HeapFile::writePage(Page *p) {
auto data = p->getPageData();
auto offset = p->getId().pageNumber() * Database::getBufferPool().getPageSize();
pwrite(fd, data, Database::getBufferPool().getPageSize(), offset);
}