// ********************************************************** // Add a new record to the page. Returns OK if everything went OK // otherwise, returns DONE if sufficient space does not exist // RID of the new record is returned via rid parameter. Status HFPage::insertRecord(char* recPtr, int recLen, RID& rid) { // fill in the body //--- check if does not have enough free Space ---- //--- get empty slot no --- int emptySlotNo = getEmptySlotNo(); if((int)this->freeSpace < recLen + (int)sizeof(slot_t) && emptySlotNo == -1) { return MINIBASE_FIRST_ERROR(HEAPFILE, NO_SPACE); //return DONE; } else if((int)this->freeSpace < recLen && emptySlotNo != -1) { return MINIBASE_FIRST_ERROR(HEAPFILE, NO_SPACE); //return DONE; } else { //--- check if is the first record on page--- if(this->slotCnt == 0) { //--- modify RID value ---- rid.pageNo = this->curPage; rid.slotNo = this->slotCnt; //--- add new slot ---- modifySlot(0, recLen); //--- add the record data into page ---- addData(0, recPtr, recLen); //--- update HFpage info --- this->slotCnt ++; } else { if(emptySlotNo == -1) { emptySlotNo = this->slotCnt; this->slotCnt ++; } //--- modify RID value --- rid.pageNo = this->curPage; rid.slotNo = emptySlotNo; //--- add new slot ---- modifySlot(emptySlotNo, recLen); //--- add the record data into page --- addData(emptySlotNo, recPtr, recLen); } //--- update HFpage info --- this->usedPtr = this->usedPtr - recLen; if(this->slotCnt == 1) this->freeSpace = MAX_SPACE - DPFIXED - recLen; else this->freeSpace = this->usedPtr - (this->slotCnt - 1) * sizeof(slot_t); return OK; } }
// ********************************************************** // Add a new record to the page. Returns OK if everything went OK // otherwise, returns DONE if sufficient space does not exist // RID of the new record is returned via rid parameter. Status HFPage::insertRecord(char* recPtr, int recLen, RID& rid) { // fill in the body //--- check if does not have enough free Space ---- if(this->freeSpace < recLen + sizeof(slot_t)) { return DONE; } else { //--- check if is the first record on page--- if(this->slotCnt == 0) { //--- modify RID value ---- rid.pageNo = this->curPage; rid.slotNo = this->slotCnt; //--- add new slot ---- modifySlot(0, recLen); //--- add the record data into page ---- addData(0, recPtr, recLen); //--- update HFpage info --- this->slotCnt ++; } else { //--- get empty slot no --- int emptySlotNo = getEmptySlotNo(); if(emptySlotNo == -1) { emptySlotNo = this->slotCnt; this->slotCnt ++; } //--- modify RID value --- rid.pageNo = this->curPage; rid.slotNo = emptySlotNo; //--- add new slot ---- modifySlot(emptySlotNo, recLen); //--- add the record data into page --- addData(emptySlotNo, recPtr, recLen); } //--- update HFpage info --- this->usedPtr = this->usedPtr - recLen; this->freeSpace = this->freeSpace - recLen; return OK; } }