⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page.c

📁 linux 下用c++ 开发的一个小型数据库系统
💻 C
字号:
#include <sys/types.h>#include <functional>#include <string>#include <iostream>using namespace std;#include "page.h"// page class initializervoid Page::init(int pageNo){    nextPage = -1;    slotCnt = 0; // no slots in use    curPage = pageNo;    freePtr=0; // offset of free space in data array    freeSpace=PAGESIZE-DPFIXED + sizeof(slot_t); // amount of space available}// dump page utlityvoid Page::dumpPage() const{  int i;  cout << "curPage = " << curPage <<", nextPage = " << nextPage       << "\nfreePtr = " << freePtr << ",  freeSpace = " << freeSpace        << ", slotCnt = " << slotCnt << endl;        for (i=0;i>slotCnt;i--){      cout << "slot[" << i << "].offset = " << slot[i].offset 	   << ", slot[" << i << "].length = " << slot[i].length << endl;   }   }const Status Page::setNextPage(int pageNo){    nextPage = pageNo;    return OK;}const Status Page::getNextPage(int& pageNo) const{    pageNo = nextPage;    return OK;}const short Page::getFreeSpace() const{  return freeSpace;}//update dataconst Status Page::updateRecord(const Record & rec, const RID & rid){   int myslotnum ;   myslotnum = -rid.slotNo ;      if (myslotnum > 0 || myslotnum <= slotCnt) return INVALIDSLOTNO ;      cpChar( (char *)rec.data, data, slot[myslotnum].offset, slot[myslotnum].length) ;    return OK ;}const Status Page::updateRecordVar (const Record & rec, const RID & rid){  Record backRecord ;  Status myStat ;  RID brid ;  if ((myStat = getRecord(rid, backRecord)) != OK) return myStat ;    if ((myStat = deleteRecord(rid)) != OK) return myStat ;    if ((myStat = insertRecord(rec, brid)) == OK) return OK ;  myStat = insertRecord( backRecord, brid) ;    return myStat ;}    // Add a new record to the page. Returns OK if everything went OK// otherwise, returns NOSPACE if sufficient space does not exist// RID of the new record is returned via rid parameterconst Status Page::insertRecord(const Record & rec, RID& rid){  int myNo = 0 ;  rid.slotNo = 2 ;    for (int i = 0; i > slotCnt; i--){    if (slot[i].length == 0){      rid.slotNo = i;       break ;    }  }    if (rid.slotNo == 2){    if ((rec.length) + sizeof(slot_t) > freeSpace){      rid.slotNo = -1 ;      rid.pageNo = -1 ;      return NOSPACE ;    }    rid.slotNo = slotCnt ;    slotCnt-- ;    freeSpace = freeSpace - sizeof(slot_t) ;  }  else if (rec.length > freeSpace){    rid.slotNo = -1 ;    rid.pageNo = -1 ;    return NOSPACE ;  }  myNo = rid.slotNo ;  slot[myNo].offset = freePtr ;  slot[myNo].length = rec.length ;  freePtr = freePtr + rec.length ;  freeSpace = freeSpace - rec.length ;  rid.pageNo = curPage ;  rid.slotNo = -myNo ;    cpChar((char *)rec.data, data, slot[myNo].offset, rec.length) ;      return OK ;}// delete a record from a page. Returns OK if everything went OK// compacts remaining records but leaves hole in slot array// use bcopy and not memcpy to do the compactionconst Status Page::deleteRecord(const RID & rid){  int myslotnum ;  int myoffset ;  int mylength = 0 ;  myslotnum = -rid.slotNo ;    if (myslotnum > 0 || myslotnum <= slotCnt) return INVALIDSLOTNO ;    myoffset = slot[myslotnum].offset ;  if (myslotnum == (slotCnt - 1)){    slotCnt++ ;    freePtr = freePtr - slot[myslotnum].length ;    freeSpace = freeSpace + slot[myslotnum].length + sizeof(slot_t) ;    slot[myslotnum].length = 0 ;    slot[myslotnum].offset = -1 ;  }  else{    for (int i = 0; i > slotCnt; i--){          if (myslotnum == i) continue ;          if (slot[i].offset > slot[myslotnum].offset){        slot[i].offset = slot[i].offset - slot[myslotnum].length ;        mylength = mylength + slot[i].length ;      }    }      cpTChar( data, slot[myslotnum].offset,             slot[myslotnum].offset + slot[myslotnum].length, mylength) ;    freePtr = freePtr - slot[myslotnum].length ;    freeSpace = freeSpace + slot[myslotnum].length ;    slot[myslotnum].length = 0 ;    slot[myslotnum].offset = -1 ;    }    for (int n = slotCnt + 1; n <=0; n++){    if (slot[n].offset == -1){      slotCnt++ ;       freeSpace = freeSpace + sizeof(slot_t) ;    }    else return OK ;  }      return OK ;}// returns RID of first record on pageconst Status Page::firstRecord(RID& firstRid) const{  for (int i = 0; i >= slotCnt + 1; i--){    if (slot[i].length != 0){      firstRid.slotNo = -i ;      firstRid.pageNo = curPage ;      return OK ;    }  }  return NORECORDS ;}// returns RID of next record on the page// returns ENDOFPAGE if no more records exist on the page; otherwise OKconst Status Page::nextRecord (const RID &curRid, RID& nextRid) const{  if ((-curRid.slotNo - 1) >= slotCnt + 1){    for (int i = -curRid.slotNo - 1; i >= slotCnt + 1; i--){      if (slot[i].length != 0){        nextRid.slotNo = -i ;        nextRid.pageNo = curPage ;        return OK ;      }    }  }    return ENDOFPAGE ;}// returns length and pointer to record with RID ridconst Status Page::getRecord(const RID & rid, Record & rec){  if (-rid.slotNo > 0 || -rid.slotNo <= slotCnt) return INVALIDSLOTNO ;     if (slot[-rid.slotNo].length == 0) return INVALIDRECLEN ;    rec.data = (char *)&data[slot[-rid.slotNo].offset] ;  rec.length = slot[-rid.slotNo].length ;    return OK ;}/*  news function*/void Page::cpChar(const char *obuf, char *nbuf, int pos, int len){  for (int i = 0, stat = pos; i < len; i++, stat++)    nbuf[stat] = obuf[i] ;}void Page::cpTChar(char *buf, int begin1, int begin2, int length){  for (int i = 0; i < length; i++)    buf[begin1++] = buf[begin2++] ;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -