Buffer.cpp
资源名称:minisqlc.rar [点击查看]
上传用户:lkd6667
上传日期:2015-05-13
资源大小:1448k
文件大小:21k
源码类别:
其他数据库
开发平台:
C/C++
- #include "Buffer.h"
- _M_Buffer Buffer;
- unsigned int SizeOfPageHead = sizeof(_TB_PAGEHEAD);
- unsigned int BTreeNodeSize = (FILE_PAGESIZE - SizeOfPageHead)/4;
- //////////////////////////////////////////////////////////////////
- //初始化文件页的头信息
- void _TB_PAGEHEAD::Initial(unsigned long mypageid,bool myisfixed)
- {
- this->ulPageID = mypageid;
- this->bIsFixed = myisfixed;
- }
- //////////////////////////////////////////////////////////////////
- //初始化文件头信息
- void _TB_FILECOND::InitialFileCond()
- {
- this->ulPageTotal = 1;
- this->DelFirst.Initialize();
- this->DelLast.Initialize();
- this->NewInsert.ulFilePageID = 1;
- this->NewInsert.uiOffset = SizeOfPageHead;
- }
- //////////////////////////////////////////////////////////////////
- //class _M_Page
- //取得文件头信息
- _TB_FILECOND* _M_Page::Ptr2FileCond()
- {
- return (_TB_FILECOND* )((char*)this->Ptr2PageBegin + SizeOfPageHead);
- }
- //构造函数
- _M_Page::_M_Page()
- {
- this->Ptr2PageBegin = malloc(FILE_PAGESIZE);
- if(!this->Ptr2PageBegin) throw 1000; // 内存开辟失败
- this->Ptr2Head = (_TB_PAGEHEAD*)this->Ptr2PageBegin;
- this->ulFilePageID = 0;
- this->uiFileID = 0;
- }
- //析构函数
- _M_Page::~_M_Page()
- {
- this->ulFilePageID = 0;
- this->uiFileID = 0;
- free(this->Ptr2PageBegin);
- this->Ptr2Head = 0;
- this->Ptr2PageBegin = 0;
- }
- //把内存中的页写回到文件中
- void _M_Page::Back2File() const
- {
- int temp = 0;
- temp = lseek(Buffer.GetPtr2File(this->uiFileID),this->ulFilePageID*FILE_PAGESIZE,0);
- if(temp==-1L) throw 1005;
- temp = write(Buffer.GetPtr2File(this->uiFileID),this->Ptr2PageBegin,FILE_PAGESIZE); // 写回文件
- if(temp!= FILE_PAGESIZE) throw 1002; // 写失败
- }
- //从文件中调入页至开辟好的内存空间中
- void _M_Page::LoadFromFile(unsigned int fileid, unsigned long filepageid)
- {
- this->uiFileID = fileid;
- this->ulFilePageID = filepageid;
- if( Buffer.GetIsNew(fileid) ) // 文件新建,只有一个页
- {
- this->Ptr2Head->Initial(filepageid,1); // 初始化页头信息
- this->Ptr2FileCond()->InitialFileCond(); // 初始化文件头信息
- Buffer.SetIsNew(fileid,0); // 设置使文件不再为新建状态(以免下次访问这个内存页又要进行头信息初始化)
- }
- //文件页是新建的,不是新建文件
- else if( filepageid >= Buffer.GetPageTotal(fileid) ) // 比现有文件总页面要多,那么开辟新的页面加到文件末尾
- {
- if( filepageid - Buffer.GetPageTotal(fileid) > 0)
- {
- //cout<<"";
- throw 1004; // 页编号比现有文件最后一个页编号+1 还要大(浪费磁盘空间)
- }
- this->Ptr2Head->Initial(filepageid,0); // 初始化页头信息
- Buffer.AddPageTotal(fileid,1); // 使文件总页数加1
- }
- // 其他情况从磁盘中读信息
- else
- {
- // 定位到将要取出的文件页的首地址
- lseek(Buffer.GetPtr2File(fileid),filepageid*FILE_PAGESIZE,0);
- // 读到内存中
- int temp = read(Buffer.GetPtr2File(fileid),this->Ptr2PageBegin,FILE_PAGESIZE);
- if( temp!= FILE_PAGESIZE)
- {
- //cout<<"
- throw 1031; // 读失败
- //return;
- }
- }
- }
- //////////////////////////////////////////////////////////////////
- //class _M_Clock
- //construct _M_Clock 成员初始化
- _M_Clock::_M_Clock()
- {
- uiClockSize=MEM_PAGEAMOUNT;
- uiCurrClockPtr=1;
- for(int i=0;i<=MEM_PAGEAMOUNT;i++)
- {
- Ptr2MemPageInfo[i]= new _M_PageInfo;
- }
- }
- //destruct _M_Clock 析构
- _M_Clock::~_M_Clock()
- {
- for(unsigned int i=0;i<=this->uiClockSize;i++)
- delete Ptr2MemPageInfo[i];
- }
- //查找Clock中尚未分配内存空间的页
- unsigned int _M_Clock::GetNullPage()
- {
- for(unsigned int i=this->uiCurrClockPtr;i<=this->uiClockSize;i++)
- {
- if( !this->Ptr2MemPageInfo[i]->Ptr2Page ) // 尚未分配内存页
- {
- this->uiCurrClockPtr = i;
- return this->uiCurrClockPtr;
- }
- }
- return 0;
- }
- //查找Clock中已经被抛弃的页
- unsigned int _M_Clock::GetFreePage()
- {
- for(unsigned int i=1;i<=this->uiClockSize;i++)
- {
- // 被抛弃内存页已经把文件编号置为 0 做为标记
- if( this->Ptr2MemPageInfo[i]->Ptr2Page && !this->Ptr2MemPageInfo[i]->GetFileID() )
- {
- this->uiCurrClockPtr = i;
- return this->uiCurrClockPtr;
- }
- }
- return 0;
- }
- // 查找Clock中最近一页可被替换的页
- unsigned int _M_Clock::GetSwapPage(unsigned int fileid)
- {
- if(! this->GetFreePage() ) // 查找被抛弃的内存页
- // 查找Clock中最早打开的文件所占用的内存页,如果是常驻内存页则关闭该文件(该写回的写回)
- if(! this->NR_Search(fileid) )
- if(! this->U_M_Search(0,0,0) ) // 上次没有用,没有被修改,不变动bIsLastUsed,返回符合这样条件的内存页
- if(! this->U_M_Search(0,1,1) ) // 上次没有用,有被修改,变动bIsLastUsed,返回符合这样条件的内存页
- if(! this->U_M_Search(0,0,0) ) // 上次没有用,没有被修改,不变动bIsLastUsed,返回符合这样条件的内存页
- this->U_M_Search(0,1,1); // 上次没有用,有被修改,变动bIsLastUsed,返回符合这样条件的内存页
- return this->uiCurrClockPtr;
- }
- // 查找Clock中最早打开的文件所占用的内存页,如果是常驻内存页则关闭该文件(该写回的写回)
- // 所有打开的文件已经由_M_Buffer类组织成链表
- unsigned int _M_Clock::NR_Search(unsigned int tarfileid)
- {
- unsigned int NR_FileIDTemp = 0; // 临时文件编号
- unsigned int NR_FileID = 20000; // 欲替换的内存页所属文件编号(程序运行期分配),20000为不可能的一个数字(打开20000个文件方有可能)
- unsigned int ClockPtr = 0; // 与替换的页编号
- unsigned int relativefileid = Buffer[tarfileid]->GetRelativeFileID(); // 取得关联文件编号
- for(unsigned int i=1;i<=this->uiClockSize;i++)
- {
- NR_FileIDTemp = this->Ptr2MemPageInfo[i]->GetFileID();
- // 文件编号不能为当前文件编号及其关联文件的编号,在符合这个条件的基础上,文件编号越小,越早打开,所以更适合被替换
- if( NR_FileIDTemp < NR_FileID && NR_FileIDTemp != tarfileid && NR_FileIDTemp != relativefileid )
- {
- NR_FileID = NR_FileIDTemp;
- ClockPtr = i;
- }
- }
- if( NR_FileID != 20000 )
- {
- // 如果被替换页是第 0 页,则关闭该文件及其关联文件
- if( this->Ptr2MemPageInfo[ClockPtr]->GetFilePageID() == 0 )
- Buffer[NR_FileID]->Close();
- this->uiCurrClockPtr = ClockPtr;
- return this->uiCurrClockPtr;
- }
- else
- return 0;
- }
- // Clock算法实现,通过U_M_Search()四次调用,完成Clock算法
- unsigned int _M_Clock::U_M_Search(bool islastused,bool ismodified,bool changeused)
- {
- for(unsigned int i=1;i<=this->uiClockSize;i++)
- {
- if( this->Ptr2MemPageInfo[this->uiCurrClockPtr]->GetPtr2Head()->bIsFixed == 0 &&
- this->Ptr2MemPageInfo[this->uiCurrClockPtr]->bIsLastUsed == islastused &&
- this->Ptr2MemPageInfo[this->uiCurrClockPtr]->bIsModified == ismodified )
- return this->uiCurrClockPtr;
- else
- {
- if( changeused ) this->Ptr2MemPageInfo[this->uiCurrClockPtr]->bIsLastUsed = 0;
- this->uiCurrClockPtr = (this->uiCurrClockPtr+1)%this->uiClockSize;
- if(!this->uiCurrClockPtr)
- this->uiCurrClockPtr = this->uiClockSize;
- }
- }
- return 0;
- }
- // 查找已经存在的页(假设要找的页已经存在的话)
- unsigned int _M_Clock::GetExsitPage(unsigned int fileid,unsigned long filepageid)
- {
- if( this->Ptr2MemPageInfo[this->uiCurrClockPtr]->Ptr2Page &&
- this->Ptr2MemPageInfo[this->uiCurrClockPtr]->GetFileID() == fileid &&
- this->Ptr2MemPageInfo[this->uiCurrClockPtr]->GetFilePageID() == filepageid )
- return this->uiCurrClockPtr;
- for(unsigned int i=1;i<=this->uiClockSize;i++)
- {
- if(!this->Ptr2MemPageInfo[i]->Ptr2Page) break;
- if( this->Ptr2MemPageInfo[i]->GetFileID() == fileid &&
- this->Ptr2MemPageInfo[i]->GetFilePageID() == filepageid )
- {
- this->uiCurrClockPtr = i;
- return this->uiCurrClockPtr;
- }
- }
- return 0;
- }
- // 根据文件编号和页号取得内存页(通过上面各种方法)
- _M_PageInfo* _M_Clock::GetTargetPage(unsigned int fileid,unsigned long filepageid)
- {
- unsigned int tempint = this->GetExsitPage(fileid,filepageid); // 先找内存页是否已经存在
- if(tempint) return this->Ptr2MemPageInfo[tempint];
- tempint = this->GetNullPage(); // 看看有没有空的内存页尚未开辟使用
- if(!tempint) tempint = this->GetSwapPage(fileid); // 得到替换页
- this->Ptr2MemPageInfo[tempint]->UpdatePageInfo(fileid,filepageid); // 调入文件页至内存,并更新相关信息
- return this->Ptr2MemPageInfo[tempint];
- }
- //关闭内存中的文件页 文件id==fileid
- void _M_Clock::CloseFilePages(unsigned int fileid)
- {
- for(unsigned int i=1;i<this->uiClockSize;i++)
- {
- if( !this->Ptr2MemPageInfo[i]->Ptr2Page ) break;
- if(Ptr2MemPageInfo[i]->GetFileID()==fileid)
- {
- //如果是dirty data写回磁盘 并将 ifmodified 改回0;
- if(Ptr2MemPageInfo[i]->bIsModified)
- {
- Ptr2MemPageInfo[i]->Ptr2Page->Back2File();
- Ptr2MemPageInfo[i]->bIsModified=0;
- }
- //放弃内存页的使用
- this->Ptr2MemPageInfo[i]->SetFileID(0);
- }
- }
- }
- // 设置当前页使之为脏页
- void _M_Clock::SetPageModified()
- {
- this->Ptr2MemPageInfo[this->uiCurrClockPtr]->bIsModified = 1;
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- //class _M_PageInfo
- //构造函数,初始化成员
- _M_PageInfo::_M_PageInfo()
- {
- this->bIsLastUsed = 0;
- this->bIsModified = 0;
- this->Ptr2Page = 0;
- }
- // 析构,根据bIsModified决定是否需要写会文件
- _M_PageInfo::~_M_PageInfo()
- {
- if(this->Ptr2Page){
- if(this->Ptr2Page->uiFileID && this->Ptr2Page->ulFilePageID==0)
- // 把文件页总数写到磁盘中,已备下次读取
- this->Ptr2Page->Ptr2FileCond()->ulPageTotal = Buffer.GetPageTotal(this->Ptr2Page->uiFileID);
- if(this->Ptr2Page->uiFileID && this->bIsModified){ // 若为脏页,写回
- this->Ptr2Page->Back2File();
- }
- delete this->Ptr2Page;
- this->Ptr2Page = 0;
- }
- this->bIsLastUsed = 0;
- this->bIsModified = 0;
- }
- // 页替换、开辟等
- void _M_PageInfo::UpdatePageInfo(unsigned int fileid,unsigned long filepageid)
- {
- if( this->bIsModified) // 若为脏页,写回
- this->Ptr2Page->Back2File();
- this->bIsLastUsed = 1;
- this->bIsModified = 0;
- if( !this->Ptr2Page ){ // 尚未开辟内存空间
- this->Ptr2Page = new _M_Page; // 新的内存页对象
- }
- // 若文件新建 或者 该页为文件第0页 后者 该页在原来文件中不存在 则要求写回
- if( Buffer.GetIsNew(fileid) || filepageid >= Buffer.GetPageTotal(fileid) || filepageid==0 )
- this->bIsModified = 1;
- this->Ptr2Page->LoadFromFile(fileid,filepageid); // 读到内存中
- }
- // 取得文件头信息地址
- _TB_FILECOND* _M_PageInfo::GetPtr2FileCond() const
- {
- return this->Ptr2Page->Ptr2FileCond();
- }
- // 取得页头信息地址
- _TB_PAGEHEAD* _M_PageInfo::GetPtr2Head() const
- {
- return this->Ptr2Page->Ptr2Head;
- }
- // 取得所分配的内存页目前内容所属的文件编号
- unsigned int _M_PageInfo::GetFileID() const
- {
- return this->Ptr2Page->uiFileID;
- }
- // 设置新的文件编号(抛弃页时设为0即可)
- void _M_PageInfo::SetFileID(unsigned int fileid)
- {
- this->Ptr2Page->uiFileID = fileid;
- }
- // 取得所分配的内存页目前内容在文件中的页编号
- unsigned long _M_PageInfo::GetFilePageID() const
- {
- return this->Ptr2Page->ulFilePageID;
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- //class _M_File
- // 初始化,打开和新建文件,若当前文件开的太多,导致无法再打开新文件,可自动关闭最早打开的文件
- _M_File::_M_File(const char *name,unsigned int fileid)
- {
- this->uiFileID = fileid;
- this->IsNew = 0;
- this->ulPageTotal = 1;
- strcpy(this->FileName,name);
- this->Ptr2File = open(name,_O_BINARY|O_RDWR,0664);
- if(this->Ptr2File==-1){ // 文件不存在
- this->Ptr2File = open(name,_O_BINARY|O_RDWR|O_CREAT,0664); // 新建文件(打开文件)
- if(this->Ptr2File==-1) // 文件不能被打开(新建)
- {
- if( Buffer._F_First)
- Buffer._F_First->Close(); // 关闭最早打开的文件
- this->Ptr2File = open(name,_O_BINARY|O_RDWR|O_CREAT,0664); // 新建文件(打开文件)
- if(this->Ptr2File==-1) // 文件不能被新建(打开)
- throw 1006; // 文件还是不能被打开(新建),可能为磁盘空间不足等意外
- }
- this->IsNew = 1;
- }
- this->_F_Next = 0;
- }
- //取相关文件
- unsigned int _M_File::GetRelativeFileID() const
- {
- int FNLength =strlen(this->FileName);//记录文件名长度,以便修改文件扩展名
- char FNTemp1[MAX_FILENAME_LEN];//存文件名
- char FNTemp2[MAX_FILENAME_LEN];
- strcpy(FNTemp1,this->FileName);
- FNTemp1[FNLength-3]=' ';//除去扩展名
- _M_File * pMFTemp;
- pMFTemp=Buffer._F_First;
- while(pMFTemp)//对所有打开文件进行搜索
- {
- strcpy(FNTemp2,pMFTemp->FileName);
- FNTemp2[FNLength-3]=' ';//除去扩展名
- if((strcmp(FNTemp1,FNTemp2)==0)&&(pMFTemp->uiFileID!=this->uiFileID))
- return pMFTemp->uiFileID;
- pMFTemp=pMFTemp->_F_Next;
- }
- //error
- //throw:没有找到相关文件
- return 0;
- }
- // 析构,关闭文件
- void _M_File::Deconstruct()
- {
- if(this->Ptr2File)
- close(this->Ptr2File);
- }
- // 关闭文件,同时属于该文件的内存页改写回的写回.为保证一致性,同时关闭关联文件
- void _M_File::Close()
- {
- Buffer.CloseTable(this->uiFileID);
- }
- // 根据页号取得属于该文件的内存页
- _M_PageInfo* _M_File::GetPageInfo(unsigned long filepageid) const
- {
- return Buffer.MemPageClock->GetTargetPage(this->uiFileID,filepageid);
- }
- // 取得目前总的页数
- unsigned long _M_File::GetPageTotal() const
- {
- return this->ulPageTotal;
- }
- // 取得文件内记录删除维护信息
- _F_FileAddr _M_File::GetDelListCond() const
- {
- _F_FileAddr temp;
- temp.ulFilePageID = 0;
- temp.uiOffset = SizeOfPageHead;
- return temp;
- }
- // 取得Catalog模块在文件中可写的第一个位置
- _F_FileAddr _M_File::GetCataPoint() const
- {
- _F_FileAddr temp;
- temp.ulFilePageID = 0;
- temp.uiOffset = SizeOfPageHead + sizeof(_TB_FILECOND);
- return temp;
- }
- // 取得Catalog模块在文件中可写的第一个位置
- _F_FileAddr _M_File::GetIdxPoint() const
- {
- _F_FileAddr temp;
- temp.ulFilePageID = 0;
- temp.uiOffset = SizeOfPageHead + sizeof(_TB_FILECOND);
- return temp;
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- //_F_FileAddr
- //初始化
- void _F_FileAddr::Initialize()
- {
- this->ulFilePageID = 0;
- this->uiOffset = 0;
- }
- //根据页编号和偏移量,取出当前文件该地址在内存中的地址,同时测试页偏移量是否溢出
- //根据页编号和偏移量,取出当前文件该地址在内存中的地址,同时测试页偏移量是否溢出
- void* _F_FileAddr::MemAddr() const
- {
- if( this->ulFilePageID==0 && this->uiOffset==0 )return NULL;
- return (void*)((char*)Buffer._F_Current->GetPageInfo(this->ulFilePageID)->Ptr2Page->Ptr2PageBegin + this->uiOffset);
- }
- //根据页编号和偏移量,取出当前文件该地址在内存中的地址,同时测试页偏移量是否溢出
- void* _F_FileAddr::MemAddr(_M_File *TargetMFile) const
- {
- if( this->ulFilePageID==0 && this->uiOffset==0 )return NULL;
- return (void*)((char*)TargetMFile->GetPageInfo(this->ulFilePageID)->Ptr2Page->Ptr2PageBegin + this->uiOffset);
- }
- //根据页编号和偏移量,取出当前文件该地址在内存中的地址,同时测试页偏移量是否溢出
- void* _F_FileAddr::MemAddr(_M_File &TargetMFile) const
- {
- if( this->ulFilePageID==0 && this->uiOffset==0 )return NULL;
- return (void*)((char*)TargetMFile.GetPageInfo(this->ulFilePageID)->Ptr2Page->Ptr2PageBegin + this->uiOffset);
- }
- // _F_FileAddr >= 操作
- bool _F_FileAddr::operator>=(_F_FileAddr& other) const
- {
- if( (this->ulFilePageID > other.ulFilePageID) || (this->uiOffset >= other.uiOffset && this->ulFilePageID >= other.ulFilePageID))
- return true;
- return false;
- }
- // _F_FileAddr > 操作
- bool _F_FileAddr::operator>(_F_FileAddr& other) const
- {
- if( (this->ulFilePageID > other.ulFilePageID) || (this->uiOffset > other.uiOffset && this->ulFilePageID >= other.ulFilePageID))
- return true;
- return false;
- }
- // _F_FileAddr == 操作
- bool _F_FileAddr::operator==(_F_FileAddr& other) const
- {
- if((this->ulFilePageID == other.ulFilePageID) && (this->uiOffset == other.uiOffset))
- return true;
- return false;
- }
- // _F_FileAddr != 操作
- bool _F_FileAddr::operator!=(_F_FileAddr& other) const
- {
- if((this->ulFilePageID != other.ulFilePageID) || (this->uiOffset != other.uiOffset))
- return true;
- return false;
- }
- // _F_FileAddr == const int 操作只对0操作
- bool _F_FileAddr::operator==(const int zero) const
- {
- if( (this->ulFilePageID==0) &&( this->uiOffset==0) )
- return true;
- return false;
- }
- //在当前页中滑动offset量(可正可负)
- void _F_FileAddr::ShiftOffset(int offset)
- {
- int temp=(int)this->uiOffset+offset;
- //if temp <0 出错
- this->uiOffset =(unsigned int) temp;
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- //class _M_Buffer
- // Buffer初始化
- void _M_Buffer::Start()
- {
- if( MEM_PAGEAMOUNT < 3) throw 1033; // 至少要求有 3 页,2页头信息(目录管理和索引管理各一页),另外 1 页记录存取
- this->_F_First = 0;
- this->_F_Last = 0;
- this->_F_Current = 0;
- this->uiFileCount = 0;
- this->MemPageClock = new _M_Clock();
- }
- // Buffer结束,写回内存页,关闭文件
- void _M_Buffer::End()
- {
- delete this->MemPageClock;
- this->_F_Last = 0;
- this->uiFileCount = 0;
- _M_File* temp = this->_F_First;
- this->_F_First = 0;
- _M_File* temp2 = 0;
- while(temp)
- {
- temp2 = temp->_F_Next;
- temp->Deconstruct();
- temp = temp2;
- }
- }
- // 根据文件编号返回内存文件对象是否为新建
- bool _M_Buffer::GetIsNew(unsigned int fileid) const
- {
- return (*this)[fileid]->IsNew;
- }
- // 根据文件编号设置内存文件对象是否为新建
- void _M_Buffer::SetIsNew(unsigned int fileid,bool isnew)
- {
- (*this)[fileid]->IsNew = isnew;
- }
- unsigned long _M_Buffer::GetPageTotal(unsigned int fileid) const
- {
- return (*this)[fileid]->ulPageTotal;
- }
- void _M_Buffer::AddPageTotal(unsigned int fileid,int add)
- {
- (*this)[fileid]->ulPageTotal += add;
- }
- int _M_Buffer::GetPtr2File(unsigned int fileid) const
- {
- return (*this)[fileid]->Ptr2File;
- }
- // 根据文件编号返回内存文件对象
- _M_File* _M_Buffer::operator [](unsigned int fileid) const
- {
- //Error Dispatch 0< fileid <= Buffer->uiFileCount
- _M_File* temp = 0;
- _M_File* temp2 = this->_F_First;
- while( temp2 )
- {
- if( temp2->uiFileID == fileid ){
- temp = temp2;
- break;
- }
- temp2 = temp2->_F_Next;
- }
- return temp;
- }
- //找到文件名为filename的_M_File
- _M_File _M_Buffer::operator [](const char* filename)
- {
- //先找当前文件,概率比较大
- if( this->_F_Current && strcmp(filename,this->_F_Current->FileName)==0)
- return *(this->_F_Current);
- _M_File * pMFTemp;
- pMFTemp = this->_F_First;
- while(pMFTemp) //对所有打开文件进行遍历
- {
- if(strcmp(filename,pMFTemp->FileName)==0)
- {
- this->_F_Current=pMFTemp;
- return *pMFTemp; //找到返回文件信息并将当前Buffer的指针针向当前文件
- }
- pMFTemp=pMFTemp->_F_Next;
- }
- //在打开文件中没有要找的文件开始如下操作
- pMFTemp = new _M_File(filename,++this->uiFileCount);
- if(!this->_F_First)//没有文件打开
- this->_F_First=this->_F_Last=pMFTemp;
- else
- {
- this->_F_Last->_F_Next=pMFTemp;
- this->_F_Last=pMFTemp;
- }
- this->_F_Current=pMFTemp;
- if(pMFTemp->IsNew)
- this->MemPageClock->GetTargetPage(pMFTemp->uiFileID,0);
- else //将文件的总页数读到_M_File 中
- pMFTemp->ulPageTotal=this->MemPageClock->GetTargetPage(pMFTemp->uiFileID,0)->GetPtr2FileCond()->ulPageTotal;
- return *pMFTemp;
- }
- // 根据文件编号关闭内存文件对象
- void _M_Buffer::CloseFile(unsigned int fileid)
- {
- this->MemPageClock->CloseFilePages(fileid);
- _M_File* temp = this->_F_First;
- _M_File* temp2 = 0;
- while(temp->uiFileID != fileid)
- {
- temp2 = temp;
- temp = temp->_F_Next;
- }
- if( temp == this->_F_First )
- this->_F_First = this->_F_First->_F_Next;
- else
- temp2->_F_Next = temp->_F_Next;
- temp->Deconstruct();
- }
- void _M_Buffer::CloseTable(unsigned int fileid)
- {
- unsigned int tempfileid = (*this)[fileid]->GetRelativeFileID();
- if( tempfileid )
- this->CloseFile(tempfileid);
- this->CloseFile(fileid);
- }
- //根据欲写入的文件地址,把其他模块内存中的数据写入buffer中,最终自动写入文件中
- _F_FileAddr MemWrite(const void* source,size_t length,_F_FileAddr* dest)
- {
- if( (int)(FILE_PAGESIZE - dest->uiOffset) - (int)length < 0) // 判断是否溢出
- {
- dest->ulFilePageID = Buffer._F_Current->GetPageTotal(); // 若溢出修正 dest
- //Buffer.MemPageClock->GetTargetPage(Buffer._F_Current->uiFileID,dest->ulFilePageID);
- dest->uiOffset = sizeof(_TB_PAGEHEAD);
- }
- memcpy(dest->MemAddr(),source,length);
- Buffer.MemPageClock->SetPageModified();
- _F_FileAddr end = *dest;
- end.ShiftOffset((int)length);
- return end;
- }
- //根据欲写入的文件地址,测试把其他模块内存中的数据写入实际未写入。
- _F_FileAddr MemWriteTest(size_t length,_F_FileAddr* dest)
- {
- if((int)(FILE_PAGESIZE - dest->uiOffset) - (int)length < 0) // 判断是否溢出
- {
- dest->ulFilePageID = Buffer._F_Current->GetPageTotal(); // 若溢出修正 dest
- dest->uiOffset = SizeOfPageHead;
- }
- _F_FileAddr end = *dest;
- end.ShiftOffset((int)length);
- return end;
- }