void HeapFile::Add (Record& addme){ Page tempPage; cout << "File length: " << f.GetLength() << endl; if(0 != f.GetLength()){ f.GetPage(&tempPage, f.GetLength() - 2); //get the last page in the file if(0 == tempPage.Append(&addme)){// the last page is full, add record to a new page and insert into the end of file tempPage.EmptyItOut(); tempPage.Append(&addme); f.AddPage(&tempPage, f.GetLength() - 1); }else{// the last page is not full, add record into the last page. f.AddPage(&tempPage, f.GetLength() - 2); } }else{ // This file is a new file with no page. if(1 == tempPage.Append(&addme)) f.AddPage(&tempPage, 0); else cout << "A new page is full. Can't insert record!" << endl; } }
void HeapFile::Load (Schema& myschema, char* loadpath){ FILE *table = fopen(loadpath, "r"); if( 0 == table) exit(-1); Page tempPage; Record tempRecord; int recordCounter = 0; int pageCounter = 0; while(1 == tempRecord.SuckNextRecord(&myschema, table)){ assert(pageCounter >= 0); assert(recordCounter >= 0); recordCounter++; if(recordCounter % 10000 == 0) cout << "The toal number of record: " << recordCounter << endl; //insert record into page until page is full and insert page into file if(0 == tempPage.Append(&tempRecord)){ // tempPage is full f.AddPage(&tempPage, pageCounter); tempPage.EmptyItOut(); tempPage.Append(&tempRecord); pageCounter++; } } f.AddPage(&tempPage,pageCounter);// insert the last page into the file cout<< "Read " << recordCounter << endl; }