-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbufferManager.cpp
120 lines (111 loc) · 3.06 KB
/
bufferManager.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "global.h"
BufferManager::BufferManager()
{
logger.log("BufferManager::BufferManager");
}
/**
* @brief Function called to read a page from the buffer manager. If the page is
* not present in the pool, the page is read and then inserted into the pool.
*
* @param tableName
* @param pageIndex
* @return Page
*/
Page BufferManager::getPage(string tableName, int pageIndex)
{
logger.log("BufferManager::getPage");
string pageName = "../data/temp/"+tableName + "_Page" + to_string(pageIndex);
if (this->inPool(pageName))
return this->getFromPool(pageName);
else
return this->insertIntoPool(tableName, pageIndex);
}
/**
* @brief Checks to see if a page exists in the pool
*
* @param pageName
* @return true
* @return false
*/
bool BufferManager::inPool(string pageName)
{
logger.log("BufferManager::inPool");
for (auto page : this->pages)
{
if (pageName == page.pageName)
return true;
}
return false;
}
/**
* @brief If the page is present in the pool, then this function returns the
* page. Note that this function will fail if the page is not present in the
* pool.
*
* @param pageName
* @return Page
*/
Page BufferManager::getFromPool(string pageName)
{
logger.log("BufferManager::getFromPool");
for (auto page : this->pages)
if (pageName == page.pageName)
return page;
}
/**
* @brief Inserts page indicated by tableName and pageIndex into pool. If the
* pool is full, the pool ejects the oldest inserted page from the pool and adds
* the current page at the end. It naturally follows a queue data structure.
*
* @param tableName
* @param pageIndex
* @return Page
*/
Page BufferManager::insertIntoPool(string tableName, int pageIndex)
{
logger.log("BufferManager::insertIntoPool");
Page page(tableName, pageIndex);
if (this->pages.size() >= BLOCK_COUNT)
pages.pop_front();
pages.push_back(page);
return page;
}
/**
* @brief The buffer manager is also responsible for writing pages. This is
* called when new tables are created using assignment statements.
*
* @param tableName
* @param pageIndex
* @param rows
* @param rowCount
*/
void BufferManager::writePage(string tableName, int pageIndex, vector<vector<int>> rows, int rowCount)
{
logger.log("BufferManager::writePage");
Page page(tableName, pageIndex, rows, rowCount);
page.writePage();
}
/**
* @brief Deletes file names fileName
*
* @param fileName
*/
void BufferManager::deleteFile(string fileName)
{
if (remove(fileName.c_str()))
logger.log("BufferManager::deleteFile: Err");
else logger.log("BufferManager::deleteFile: Success");
}
/**
* @brief Overloaded function that calls deleteFile(fileName) by constructing
* the fileName from the tableName and pageIndex.
*
* @param tableName
* @param pageIndex
*/
void BufferManager::deleteFile(string tableName, int pageIndex)
{
logger.log("BufferManager::deleteFile");
string fileName = "../data/temp/"+tableName + "_Page" + to_string(pageIndex);
this->deleteFile(fileName);
}