8 #include "file_iterator.h"
9 #include "page_iterator.h"
10 #include "exceptions/file_not_found_exception.h"
11 #include "exceptions/invalid_page_exception.h"
12 #include "exceptions/page_not_pinned_exception.h"
13 #include "exceptions/page_pinned_exception.h"
14 #include "exceptions/buffer_exceeded_exception.h"
16 #define PRINT_ERROR(str) \
18 std::cerr << "On Line No:" << __LINE__ << "\n"; \
19 std::cerr << str << "\n"; \
23 using namespace badgerdb;
26 PageId pid[num], pageno1, pageno2, pageno3, i;
28 Page *page, *page2, *page3;
31 File *file1ptr, *file2ptr, *file3ptr, *file4ptr, *file5ptr;
45 const std::string& filename =
"test.db";
61 for (
int i = 0; i < 5; ++i) {
75 iter != new_file.
end();
79 page_iter != (*iter).end();
81 std::cout <<
"Found record: " << *page_iter
82 <<
" on page " << (*iter).page_number() <<
"\n";
92 std::cout <<
"Third page has a new record: "
110 const std::string& filename1 =
"test.1";
111 const std::string& filename2 =
"test.2";
112 const std::string& filename3 =
"test.3";
113 const std::string& filename4 =
"test.4";
114 const std::string& filename5 =
"test.5";
166 std::cout <<
"\n" <<
"Passed all tests." <<
"\n";
172 for (i = 0; i < num; i++)
174 bufMgr->
allocPage(file1ptr, pid[i], page);
175 sprintf((
char*)tmpbuf,
"test.1 Page %d %7.1f", pid[i], (
float)pid[i]);
177 bufMgr->
unPinPage(file1ptr, pid[i],
true);
181 for (i = 0; i < num; i++)
183 bufMgr->
readPage(file1ptr, pid[i], page);
184 sprintf((
char*)&tmpbuf,
"test.1 Page %d %7.1f", pid[i], (
float)pid[i]);
185 if(strncmp(page->
getRecord(rid[i]).c_str(), tmpbuf, strlen(tmpbuf)) != 0)
187 PRINT_ERROR(
"ERROR :: CONTENTS DID NOT MATCH");
189 bufMgr->
unPinPage(file1ptr, pid[i],
false);
191 std::cout<<
"Test 1 passed" <<
"\n";
199 for (i = 0; i < num/3; i++)
201 bufMgr->
allocPage(file2ptr, pageno2, page2);
202 sprintf((
char*)tmpbuf,
"test.2 Page %d %7.1f", pageno2, (
float)pageno2);
205 int index = rand() % num;
206 pageno1 = pid[index];
207 bufMgr->
readPage(file1ptr, pageno1, page);
208 sprintf((
char*)tmpbuf,
"test.1 Page %d %7.1f", pageno1, (
float)pageno1);
209 if(strncmp(page->
getRecord(rid[index]).c_str(), tmpbuf, strlen(tmpbuf)) != 0)
211 PRINT_ERROR(
"ERROR :: CONTENTS DID NOT MATCH");
214 bufMgr->
allocPage(file3ptr, pageno3, page3);
215 sprintf((
char*)tmpbuf,
"test.3 Page %d %7.1f", pageno3, (
float)pageno3);
218 bufMgr->
readPage(file2ptr, pageno2, page2);
219 sprintf((
char*)&tmpbuf,
"test.2 Page %d %7.1f", pageno2, (
float)pageno2);
220 if(strncmp(page2->
getRecord(rid2).c_str(), tmpbuf, strlen(tmpbuf)) != 0)
222 PRINT_ERROR(
"ERROR :: CONTENTS DID NOT MATCH");
225 bufMgr->
readPage(file3ptr, pageno3, page3);
226 sprintf((
char*)&tmpbuf,
"test.3 Page %d %7.1f", pageno3, (
float)pageno3);
227 if(strncmp(page3->
getRecord(rid3).c_str(), tmpbuf, strlen(tmpbuf)) != 0)
229 PRINT_ERROR(
"ERROR :: CONTENTS DID NOT MATCH");
232 bufMgr->
unPinPage(file1ptr, pageno1,
false);
235 for (i = 0; i < num/3; i++) {
242 std::cout <<
"Test 2 passed" <<
"\n";
249 bufMgr->
readPage(file4ptr, 1, page);
250 PRINT_ERROR(
"ERROR :: File4 should not exist. Exception should have been thrown before execution reaches this point.");
256 std::cout <<
"Test 3 passed" <<
"\n";
266 PRINT_ERROR(
"ERROR :: Page is already unpinned. Exception should have been thrown before execution reaches this point.");
272 std::cout <<
"Test 4 passed" <<
"\n";
277 for (i = 0; i < num; i++) {
278 bufMgr->
allocPage(file5ptr, pid[i], page);
279 sprintf((
char*)tmpbuf,
"test.5 Page %d %7.1f", pid[i], (
float)pid[i]);
287 PRINT_ERROR(
"ERROR :: No more frames left for allocation. Exception should have been thrown before execution reaches this point.");
293 std::cout <<
"Test 5 passed" <<
"\n";
295 for (i = 1; i <= num; i++)
302 for (i = 1; i <= num; i++) {
303 bufMgr->
readPage(file1ptr, i, page);
309 PRINT_ERROR(
"ERROR :: Pages pinned for file being flushed. Exception should have been thrown before execution reaches this point.");
315 std::cout <<
"Test 6 passed" <<
"\n";
317 for (i = 1; i <= num; i++)
327 for (i = 1; i <= num; i++) {
328 bufMgr->
readPage(file1ptr, i, page);
332 PRINT_ERROR(
"ERROR :: This should delete the first page in file.");
An exception that is thrown when a page which is expected to be pinned in the buffer pool is found to...
The central class which manages the buffer pool including frame allocation and deallocation to pages ...
void readPage(File *file, const PageId PageNo, Page *&page)
std::string getRecord(const RecordId &record_id) const
Iterator for iterating over the records in a page.
Class which represents a file in the filesystem containing database pages.
void unPinPage(File *file, const PageId PageNo, const bool dirty)
static File create(const std::string &filename)
std::uint32_t PageId
Identifier for a page in a file.
An exception that is thrown when a file operation is requested for a filename that doesn't exist...
An exception that is thrown when an attempt is made to access an invalid page in a file...
Class which represents a fixed-size database page containing records.
void writePage(const Page &new_page)
void flushFile(const File *file)
PageId page_number() const
Iterator for iterating over the pages in a file.
static void remove(const std::string &filename)
An exception that is thrown when a page which is not expected to be pinned in the buffer pool is foun...
Page readPage(const PageId page_number) const
void disposePage(File *file, const PageId PageNo)
Identifier for a record in a page.
An exception that is thrown when buffer capacity is exceeded.
void allocPage(File *file, PageId &PageNo, Page *&page)
RecordId insertRecord(const std::string &record_data)