-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathBufferPool.cpp
85 lines (74 loc) · 2.31 KB
/
BufferPool.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <db/BufferPool.h>
#include <db/Database.h>
using namespace db;
void BufferPool::evictPage() {
auto it = pages.begin();
if (it != pages.end()) {
flushPage(it->first);
pages.erase(it);
}
}
void BufferPool::flushAllPages() {
for (const auto &item: pages) {
flushPage(item.first);
}
}
void BufferPool::discardPage(const PageId *pid) {
auto it = pages.find(pid);
if (it != pages.end()) {
pages.erase(it);
}
}
void BufferPool::flushPage(const PageId *pid) {
auto it = pages.find(pid);
if (it != pages.end() && it->second->isDirty().has_value()) {
it->second->markDirty(std::nullopt);
Database::getCatalog().getDatabaseFile(pid->getTableId())->writePage(it->second);
}
}
void BufferPool::flushPages(const TransactionId &tid) {
for (const auto &item: pages) {
if (item.second->isDirty() == tid) {
flushPage(item.first);
}
}
}
void BufferPool::insertTuple(const TransactionId &tid, int tableId, Tuple *t) {
auto f = Database::getCatalog().getDatabaseFile(tableId);
auto dirtypages = f->insertTuple(tid, *t);
for (auto page: dirtypages) {
page->markDirty(tid);
const PageId *pid = &page->getId();
if (pages.size() >= numPages && pages.find(pid) == pages.end()) {
evictPage();
}
pages[pid] = page;
}
}
void BufferPool::deleteTuple(const TransactionId &tid, Tuple *t) {
int tableId = t->getRecordId()->getPageId()->getTableId();
auto f = Database::getCatalog().getDatabaseFile(tableId);
auto dirtypages = f->insertTuple(tid, *t);
for (auto page: dirtypages) {
page->markDirty(tid);
const PageId *pid = &page->getId();
if (pages.size() >= numPages && pages.find(pid) == pages.end()) {
evictPage();
}
pages[pid] = page;
}
}
Page *BufferPool::getPage(const PageId *pid) {
auto it = pages.find(pid);
if (it != pages.end()) {
return it->second;
}
if (pages.size() >= numPages) {
evictPage();
}
Page *page = Database::getCatalog().getDatabaseFile(pid->getTableId())->readPage(*pid);
pages[pid] = page;
return page;
}
const PagesMap &BufferPool::getPages() const { return pages; }
const int &BufferPool::getNumPages() const { return numPages; }