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