BadgerDB
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
filescan.cpp
1 
8 #include "filescan.h"
9 #include "exceptions/end_of_file_exception.h"
10 
11 namespace badgerdb {
12 
13 FileScan::FileScan(const std::string &name, BufMgr *bufferMgr)
14 {
15  file = new PageFile(name, false); //dont create new file
16  bufMgr = bufferMgr;
17  curDirtyFlag = false;
18  curPage = NULL;
19  filePageIter = file->begin();
20 }
21 
22 FileScan::~FileScan()
23 {
24  // generally must unpin last page of the scan
25  if (curPage != NULL)
26  {
27  bufMgr->unPinPage(file, (*filePageIter).page_number(), curDirtyFlag);
28  curPage = NULL;
29  curDirtyFlag = false;
30  filePageIter = file->begin();
31  }
32  bufMgr->flushFile(file);
33  delete file;
34 }
35 
36 void FileScan::scanNext(RecordId& outRid)
37 {
38  std::string rec;
39 
40  if (filePageIter == file->end())
41  {
42  throw EndOfFileException();
43  }
44 
45  // special case of the first record of the first page of the file
46  if (curPage == NULL)
47  {
48  // need to get the first page of the file
49  filePageIter = file->begin();
50  if(filePageIter == file->end())
51  {
52  throw EndOfFileException();
53  }
54 
55  // read the first page of the file
56  bufMgr->readPage(file, (*filePageIter).page_number(), curPage);
57  curDirtyFlag = false;
58 
59  // get the first record off the page
60  pageRecordIter = curPage->begin();
61 
62  if(pageRecordIter != curPage->end())
63  {
64  // get pointer to record
65  rec = *pageRecordIter;
66 
67  outRid = pageRecordIter.getCurrentRecord();
68  return;
69  }
70  }
71 
72  // Loop, looking for a record that satisfied the predicate.
73  // First try and get the next record off the current page
74  pageRecordIter++;
75 
76  while (pageRecordIter == curPage->end())
77  {
78  // unpin the current page
79  bufMgr->unPinPage(file, (*filePageIter).page_number(), curDirtyFlag);
80  curPage = NULL;
81  curDirtyFlag = false;
82 
83  filePageIter++;
84  if (filePageIter == file->end())
85  {
86  curPage = NULL;
87  throw EndOfFileException();
88  }
89 
90  // read the next page of the file
91  bufMgr->readPage(file, (*filePageIter).page_number(), curPage);
92 
93  // get the first record off the page
94  pageRecordIter = curPage->begin();
95  }
96 
97  // curRec points at a valid record
98  // see if the record satisfies the scan's predicate
99  // get a pointer to the record
100  rec = *pageRecordIter;
101 
102  // return rid of the record
103  outRid = pageRecordIter.getCurrentRecord();
104  return;
105 }
106 
107 // returns pointer to the current record. page is left pinned
108 // and the scan logic is required to unpin the page
109 std::string FileScan::getRecord()
110 {
111  return *pageRecordIter;
112 }
113 
114 // mark current page of scan dirty
115 void FileScan::markDirty()
116 {
117  curDirtyFlag = true;
118 }
119 
120 }
void readPage(File *file, const PageId PageNo, Page *&page)
Definition: buffer.cpp:120
void unPinPage(File *file, const PageId PageNo, const bool dirty)
Definition: buffer.cpp:154
void flushFile(const File *file)
Definition: buffer.cpp:171
FileIterator end()
Definition: file.cpp:312
PageIterator begin()
Definition: page.cpp:211
PageIterator end()
Definition: page.cpp:215
FileIterator begin()
Definition: file.cpp:307