Public Member Functions | |
FileScan (const std::string &name, BufMgr *bufMgr) | |
~FileScan () | |
void | insertRecord (const std::string &rec, RecordId &outRid) |
void | deleteRecord (RecordId rid) |
void | startScan (int offset, int length, Datatype type, std::string filter, Operator op) |
void | endScan () |
void | scanNext (RecordId &outRid) |
std::string | getRecord () |
std::string | getRandomRecord (RecordId rid) |
void | gotoMark (RecordId rid) |
bool | matchRec (const std::string &rec) |
void | flushFile () |
void | markDirty () |
Protected Attributes | |
BufMgr * | bufMgr |
Definition at line 20 of file filescan.h.
badgerdb::FileScan::FileScan | ( | const std::string & | name, |
BufMgr * | bufMgr | ||
) |
Constructor.
Definition at line 16 of file filescan.cpp.
{ try { file = new PageFile(name, false); //dont create new file //std::cout << "File:" << name << " opened.\n"; } catch(FileNotFoundException e) { file = new PageFile(name, true); //dont create new file //std::cout << "File:" << name << " created.\n"; } bufMgr = bufferMgr; curDirtyFlag = false; curPage = NULL; filePageIter = file->begin(); }
void badgerdb::FileScan::deleteRecord | ( | RecordId | rid | ) |
Delete a record. No need to mark the page dirty by calling markDirty().
Definition at line 79 of file filescan.cpp.
void badgerdb::FileScan::endScan | ( | ) |
Reset scan parameters.
Definition at line 126 of file filescan.cpp.
void badgerdb::FileScan::flushFile | ( | ) |
Flush file from buffer manager and reset all parameters.
Definition at line 87 of file filescan.cpp.
std::string badgerdb::FileScan::getRandomRecord | ( | RecordId | rid | ) |
Read a record with RecordId rid from file.
Definition at line 226 of file filescan.cpp.
std::string badgerdb::FileScan::getRecord | ( | ) |
Read current record, whose RecordId was returned by last call to scanNext().
Definition at line 221 of file filescan.cpp.
{
return *pageRecordIter;
}
void badgerdb::FileScan::gotoMark | ( | RecordId | rid | ) |
Point to particular record in the file so that next calls to scanNext() scan file beyond this particular record.
Definition at line 35 of file filescan.cpp.
void badgerdb::FileScan::insertRecord | ( | const std::string & | rec, |
RecordId & | outRid | ||
) |
Insert a new record in file, RecordId of inserted record returned in outRid. No need to mark the page dirty by calling markDirty().
Definition at line 48 of file filescan.cpp.
{ Page *iPage; PageId iPageNo; for (FileIterator iter = file->begin(); iter != file->end(); ++iter) { iPageNo = iter.getCurrentPageNo(); if(iPageNo == Page::INVALID_NUMBER) break; bufMgr->readPage(file, iPageNo, iPage); try { outRid = iPage->insertRecord(rec); bufMgr->unPinPage(file, iPageNo, true); return; } catch(InsufficientSpaceException &e) { bufMgr->unPinPage(file, iPageNo, false); } } bufMgr->allocPage(file, iPageNo, iPage); outRid = iPage->insertRecord(rec); bufMgr->unPinPage(file, iPageNo, true); }
void badgerdb::FileScan::markDirty | ( | ) |
Mark current page being scanned in FileScan dirty.
Definition at line 287 of file filescan.cpp.
{
curDirtyFlag = true;
}
bool badgerdb::FileScan::matchRec | ( | const std::string & | rec | ) |
Check if a record satisfies the scan parameters.
Definition at line 238 of file filescan.cpp.
{ // no filtering requested if (filter.empty()) return true; // see if offset + length is beyond end of record // maybe this should be an error??? if ((offset + length -1 ) >= rec.size()) return false; double diff = 0; // < 0 if attr < fltr switch(type) { case INTEGER: int iattr, ifltr; // word-alignment problem possible memcpy(&iattr, rec.c_str() + offset, length); memcpy(&ifltr, filter.c_str(), length); //ifltr = atoi(filter.c_str()); diff = iattr - ifltr; break; case DOUBLE: double fattr, ffltr; // word-alignment problem possible memcpy(&fattr, rec.c_str() + offset, length); memcpy(&ffltr, filter.c_str(), length); //ffltr = atof(filter.c_str()); diff = fattr - ffltr; break; case STRING: diff = strncmp(rec.c_str() + offset, filter.c_str(), length); break; } switch(op) { case LT: if (diff < 0.0) return true; break; case LTE: if (diff <= 0.0) return true; break; case EQ: if (diff == 0.0) return true; break; case GTE: if (diff >= 0.0) return true; break; case GT: if (diff > 0.0) return true; break; case NE: if (diff != 0.0) return true; break; default: break; } return false; }
void badgerdb::FileScan::scanNext | ( | RecordId & | outRid | ) |
Returns RecordId of next record that satisfies the scan parameters, if any.
Definition at line 139 of file filescan.cpp.
{ std::string rec; if(filePageIter == file->end()) { throw EndOfFileException(); } // special case of the first record of the first page of the file if (curPage == NULL) { // need to get the first page of the file filePageIter = file->begin(); if(filePageIter == file->end()) { throw EndOfFileException(); } // read the first page of the file bufMgr->readPage(file, (*filePageIter).page_number(), curPage); curDirtyFlag = false; // get the first record off the page pageRecordIter = curPage->begin(); if(pageRecordIter != curPage->end()) { // get pointer to record rec = *pageRecordIter; if(matchRec(rec)) { outRid = pageRecordIter.getCurrentRecord(); return; } } } // Loop, looking for a record that satisfied the predicate. // First try and get the next record off the current page pageRecordIter++; while(1) { while (pageRecordIter == curPage->end()) { // unpin the current page bufMgr->unPinPage(file, (*filePageIter).page_number(), curDirtyFlag); curPage = NULL; curDirtyFlag = false; filePageIter++; if (filePageIter == file->end()) { curPage = NULL; throw EndOfFileException(); } // read the next page of the file bufMgr->readPage(file, (*filePageIter).page_number(), curPage); // get the first record off the page pageRecordIter = curPage->begin(); } while (pageRecordIter != curPage->end()) { rec = *pageRecordIter; if(matchRec(rec)) { // return rid of the record outRid = pageRecordIter.getCurrentRecord(); return; } pageRecordIter++; } } }
void badgerdb::FileScan::startScan | ( | int | offset, |
int | length, | ||
Datatype | type, | ||
std::string | filter, | ||
Operator | op | ||
) |
To set up scan parameters used by scanNext. If just want to scan all the records in the file, no need to call this, you may directly call scanNext().
Definition at line 108 of file filescan.cpp.
{ curPage = NULL; curDirtyFlag = false; filePageIter = file->begin(); if (filterIn.empty()) { // no filtering requested filter.clear(); return; } offset = offsetIn; length = lengthIn; type = typeIn; filter = filterIn; op = opIn; }