WiscDB
|
00001 00008 #include "fileScanner.h" 00009 #include "exceptions/end_of_file_exception.h" 00010 00011 namespace wiscdb { 00012 00013 FileScanner::FileScanner(const std::string &name, BufferManager *bufferMgr){ 00014 file = new PageFile(name, false); //dont create new file 00015 bufMgr = bufferMgr; 00016 curDirtyFlag = false; 00017 curPage = NULL; 00018 filePageIter = file->begin(); 00019 } 00020 00021 FileScanner::~FileScanner(){ 00022 // generally must unpin last page of the scan 00023 if (curPage != NULL) 00024 { 00025 bufMgr->unPinPage(file, (*filePageIter).page_number(), curDirtyFlag); 00026 curPage = NULL; 00027 curDirtyFlag = false; 00028 filePageIter = file->begin(); 00029 } 00030 delete file; 00031 } 00032 00033 void FileScanner::scanNext(RecordId& outRid){ 00034 std::string rec; 00035 00036 if (filePageIter == file->end()) 00037 { 00038 throw EndOfFileException(); 00039 } 00040 00041 // special case of the first record of the first page of the file 00042 if (curPage == NULL) 00043 { 00044 // need to get the first page of the file 00045 filePageIter = file->begin(); 00046 if(filePageIter == file->end()) 00047 { 00048 throw EndOfFileException(); 00049 } 00050 00051 // read the first page of the file 00052 bufMgr->readPage(file, (*filePageIter).page_number(), curPage); 00053 curDirtyFlag = false; 00054 00055 // get the first record off the page 00056 pageRecordIter = curPage->begin(); 00057 00058 if(pageRecordIter != curPage->end()) 00059 { 00060 // get pointer to record 00061 rec = *pageRecordIter; 00062 00063 outRid = pageRecordIter.getCurrentRecord(); 00064 return; 00065 } 00066 } 00067 00068 // Loop, looking for a record that satisfied the predicate. 00069 // First try and get the next record off the current page 00070 pageRecordIter++; 00071 00072 while (pageRecordIter == curPage->end()) 00073 { 00074 // unpin the current page 00075 bufMgr->unPinPage(file, (*filePageIter).page_number(), curDirtyFlag); 00076 curPage = NULL; 00077 curDirtyFlag = false; 00078 00079 filePageIter++; 00080 if (filePageIter == file->end()) 00081 { 00082 curPage = NULL; 00083 throw EndOfFileException(); 00084 } 00085 00086 // read the next page of the file 00087 bufMgr->readPage(file, (*filePageIter).page_number(), curPage); 00088 00089 // get the first record off the page 00090 pageRecordIter = curPage->begin(); 00091 } 00092 00093 // curRec points at a valid record 00094 // see if the record satisfies the scan's predicate 00095 // get a pointer to the record 00096 rec = *pageRecordIter; 00097 00098 // return rid of the record 00099 outRid = pageRecordIter.getCurrentRecord(); 00100 return; 00101 } 00102 00103 // returns pointer to the current record. page is left pinned 00104 // and the scan logic is required to unpin the page 00105 std::string FileScanner::getRecord(){ 00106 return *pageRecordIter; 00107 } 00108 00109 // mark current page of scan dirty 00110 void FileScanner::markDirty(){ 00111 curDirtyFlag = true; 00112 } 00113 00114 }