int DirPage::insertRecords(RecordSet* rs, int s){ //return number of records inserted. int start = 0; long dpNum;//DataPage Num. int recLen; DataPage* dp; int sid,i = 0; int n = rs->getNumOfRecords(); vector<Record*> rec = rs->getAllRecords(); int *attrType = rs->getAttrType(); char *recStr; for(i=s;i<n;i++){ recLen = rec[i]->getSize(); dpNum = findNextPartialPage(start,recLen);//find the first partial page if(dpNum == -1){ //no sufficient page. //try to create new DP and add it,vector<DirEntry*> dirEntries to DE. if(DEcount < MAX_DE ){ //create new DataPage. dp = createDataPage(); start = DEcount - 1; }else{ //further insertion not possible error("@DirPage Insertion : DirPage Full."); //return the number of records inserted. writeToPage(); return i - s; } }else{ dp = new DataPage(dirEntries[dpNum]->getPageID()); start = dpNum; } recStr = rec[i]->toRecordString(attrType); sid = dp->insertRecord(recStr,rec[i]->getSize()); delete[] recStr; if(sid!=-1){ //successful insertion //update dirPage entries. lg("@DirPage Insertion: Record "<<i<<" - inserted in DataPage "<<dp->getPageid()); dirEntries[start]->setTFS(dp->getTotalFreeSize()); dirEntries[start]->writeDE(p,start); start = 0;//check from first again for next record. updateMaximumSpaceParam(); }else{ error("@DirPage Insertion : DataPage "<<dp->getPageid()<<" is full. Retrying next Page in DE"); //insertion error //retry the insertion here again to select a diff DP. i--;//so the same record will be referenced again start++;//search from next page! } if(dp!=NULL) delete dp; } writeToPage(); return i - s; }
int DirPage::deleteRecords(Where *where, int* attrType, int numOfAttr){ if(DEcount == 0){ lg2("@DirPage_"<<pageid<<" : No Records to delete."); return 0; } DataPage* datapage; long noOfRecs = 0; long dataPid; long maxTFS; DirEntry *de; // dirEntries; for(vector<DirEntry*>::size_type i = 0; i<dirEntries.size() ; i++){ //create DataPage for every entry and try retreiving records. de = dirEntries[i]; dataPid = de->getPageID(); datapage = new DataPage(dataPid); noOfRecs += datapage->deleteRecords(where,attrType,numOfAttr); maxTFS = datapage->getTotalFreeSize(); de->setTFS(maxTFS); if(maxSpaceAvailable < maxTFS) maxSpaceAvailable = maxTFS; de->writeDE(p,i); writeToPage(); delete datapage; } return noOfRecs; }
DirPage::~DirPage() { // TODO Auto-generated destructor stub int n = dirEntries.size(); for(int i=0;i<n;i++){ delete dirEntries[i]; } writeToPage(); //delete [] p; }
DirPage::DirPage() { nextPage = 0; prevPage = 0; maxSpaceAvailable = 0; DEcount = 0; memcpy(p,this,sizeof(DirPage)); writeToPage(); printDetails(); }
long DirPage::updateRecords(Where *where, Modify *modify, RecordSet* rs){ if(DEcount == 0){ lg2("@DirPage_"<<pageid<<" : No Records to update."); return 0; } DataPage* datapage; long noOfRecs = 0; long dataPid; long maxTFS; DirEntry *de; // dirEntries; for(vector<DirEntry*>::size_type i = modify->getStartDirEntry(); i<dirEntries.size() ; i++){ //create DataPage for every entry and try retreiving records. de = dirEntries[i]; dataPid = de->getPageID(); datapage = new DataPage(dataPid); noOfRecs += datapage->updateRecords(where,modify,rs); maxTFS = datapage->getTotalFreeSize(); de->setTFS(maxTFS); if(maxSpaceAvailable < maxTFS) maxSpaceAvailable = maxTFS; de->writeDE(p,i); if(modify->isThereMore()){ writeToPage(); modify->setStartDirEntry(i); delete datapage; return noOfRecs; } delete datapage; } writeToPage(); return noOfRecs; }
bool Queue::write(AddressType& Address, uint8_t* FromBuf, AddressType WantedSize) { PageCursor Cursor(this); while (WantedSize) { AddressType FoundSize = writeToPage(Address, WantedSize, Cursor); if (FoundSize == 0) return false; uint8_t* ToBuf = Cursor.getBufferPtr(); memcpy(ToBuf, FromBuf, FoundSize); Address += FoundSize; WantedSize -= FoundSize; } return true; }
DataPage* DirPage::createDataPage(){ //create a datapage. //get tfs and store it in DE if(DEcount == MAX_DE){ error("No Free space to insert DE"); return NULL; } DataPage *dp = new DataPage(); long pid = dp->getPageid(); long tfs = dp->getTotalFreeSize(); DirEntry *de = new DirEntry(pid,tfs); dirEntries.push_back(de); de->writeDE(p,DEcount); DEcount++; if(tfs>maxSpaceAvailable) maxSpaceAvailable = tfs; writeToPage(); return dp; }
void Queue::freezeEof(AddressType& Address) { assert(Address <= kMaxEofAddress && "WASM stream too big to process"); if (EofFrozen && Address != EofPtr->getEobAddress()) { fail(); Address = 0; } // This call zero-fills pages if writing hasn't reached Address yet. PageCursor Cursor(this); writeToPage(Address, 0, Cursor); EofPtr->setEobAddress(Address); EofFrozen = true; if (!isBroken(Cursor)) { Cursor.setMaxAddress(Address); // TODO(karlschimpf): If adding threads, make this update thread safe. // If any pages exist after Cursor, remove them. LastPage = Cursor.CurPage; if (Cursor.CurPage->Next) Cursor.CurPage->Next.reset(); } }