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

📄 block.cpp

📁 有计算机图形学、图像处理、dbms、sniffer、中游俄罗斯外挂、othello、遗传算法、舌苔分析等程序。
💻 CPP
字号:
// Block.cpp: implementation of the CBlock class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Database.h"
#include "Block.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBlock::CBlock(UINT nGrowBytes)//:CMemFile(nGrowBytes)
{
	CMemFile::CMemFile(nGrowBytes);
	
	Buffer=NULL;
    ID=DB_NULL;
	rtag=0x1;
	wtag=0x0;
}
CBlock::CBlock(BYTE* lpBuffer, UINT nBufferSize,PDB db_addr,bool vew,UINT nGrowBytes)
:CMemFile(lpBuffer,nBufferSize,nGrowBytes)
{
	Buffer=lpBuffer;
	ID=db_addr;
    rtag=0x1;
	wtag=0x0;
    view=vew;
}


void CBlock::Attach( BYTE* lpBuffer, UINT nBufferSize, UINT nGrowBytes)
{
	CMemFile::Attach(lpBuffer,nBufferSize,nGrowBytes);
   
	rtag=0x1;
	wtag=0x0;
	Buffer=lpBuffer;
}
CBlock::~CBlock()
{
	delete []Buffer;
}


/*void CBlock::InsertSpace(Space *s)
{
	Space *after=st;
	for(;after->offset<(s->offset+s->large);after=after->next);
	if(after->offset==(s->offset+s->large))//完成一部分ResetSpace()的功能
	{
		after->offset=s->offset;
	    after->large=after->large+s->large;
	}
    s->next=after;
	s->prior=after->prior;
	after->prior->next=s;
	after->prior=s;
	if(after==st)
		st=s;
}

void CBlock::DeleteSpace(Space *s)
{
	s->prior->next=s->next;
	s->next->prior=s->prior;
	if(st==s)
	    st=s->next;
    delete(s);
}

void CBlock::ResetSpace()
{
	UINT BlockSize=(record_num>=0)?MAIN_BLOCK:FLOOD_BLOCK;
    Space *s=st;
    while(s->offset!=BlockSize&&s->large!=0)//两条件可去掉一个
	{
		if(s->offset=end&&s->offset!=BlockSize)
		{
			remain_bytes+=s->large;
			end+=s->large;
			DeleteSpace(s);
		}
		if((s->offset+s->large)==s->next->offset&&s->next->offset!=BlockSize)
		{
		    s->large=s->large+s->next->large;
			DeleteSpace(s->next);
		}
		else
			s=s->next;
	}
}*/
int CBlock::GetTime()
{
	return 0;
}

BOOL CBlock::WriteRecord(Record *rcd,UINT uOffset,PDB addr1,PDB addr2)
{
	rtag=0x1;
	wtag=0x1;
    BYTE exist=1;
	
	PDB nex;
	CMemFile cmf;
	BYTE *buffer=NULL;
	BYTE *buf1=NULL;
	BYTE *buf2=NULL;
	CBlock *next=NULL;

	UINT rcd_head=view ? VIEW_RECORD_HEAD : RECORD_HEAD;
	if( (uOffset+rcd->m_length+rcd_head) >= MAIN_BLOCK)
	{
		nex=(ID&0xFFFFFFFF00000000) + PDB( (BLOCK(ID)+1)*MAIN_BLOCK  );
        next=CMemory::ReadDBBlock(nex,view);//bool this->view
		ASSERT(next);
        buffer=new BYTE[2*MAIN_BLOCK];
		buf1=this->Detach();
		buf2=next->Detach();
        cmf.Attach(buffer,2*MAIN_BLOCK);
		cmf.SeekToBegin();
		cmf.Write(buf1,MAIN_BLOCK);
		cmf.Write(buf2,MAIN_BLOCK);

		cmf.Seek(uOffset,CFile::begin);
		
		cmf.Write(&rcd->m_length,sizeof(UINT));
		if(view)
		{
			cmf.Write(&addr1,sizeof(PDB));
            cmf.Write(&addr2,sizeof(PDB));
		}
		else
		{
		    cmf.Write(&exist,sizeof(BYTE));
		    cmf.Write(&rcd->ID,sizeof(UINT));
		    cmf.Write(&rcd->time,sizeof(int));
		}
		cmf.Write(rcd->body,rcd->m_length);//buffer应该还在
		
		buffer=cmf.Detach();
		memcpy(buf1,buffer,MAIN_BLOCK);
		memcpy(buf2,buffer+MAIN_BLOCK,MAIN_BLOCK);
		this->Attach(buf1,MAIN_BLOCK);
		next->Attach(buf2,MAIN_BLOCK);
		delete []buffer;
		return TRUE;
	}
	else
	{
        this->Seek(uOffset,CFile::begin);
		
		Write(&rcd->m_length,sizeof(UINT));
		if(view)
		{
			Write(&addr1,sizeof(PDB));
            Write(&addr2,sizeof(PDB));
		}
		else
		{
			Write(&exist,sizeof(BYTE));
			Write(&rcd->ID,sizeof(UINT));
			Write(&rcd->time,sizeof(int));
		}
	    Write(rcd->body,rcd->m_length);
		return TRUE;
	}
}

BOOL CBlock::WriteBack()
{
	CString s;
	UINT FileNo=UINT(FILE(ID));;
	s.Format("D:\\DB\\%d",FileNo);
	if(!view)
		s+=".db";
	else
		s+=".view";
    CFile cdbf(LPCTSTR(s),CFile::modeReadWrite);
    cdbf.Seek(LONG( ID&0xFFFFFFFF ),CFile::begin);
	cdbf.Write(this->Detach(),MAIN_BLOCK);
	return TRUE;
}

Record *CBlock::ReadRecord(UINT offset,Relationship *R)
{
	BYTE exist;

	Record *Ret=new Record;
	Ret->pattern=R;
    

	PDB nex;
	CMemFile cmf;
	BYTE *buffer=NULL;
	BYTE *buf1=NULL;
	BYTE *buf2=NULL;
	CBlock *next=NULL;
	UINT RcdLength=0;

	UINT rcd_head=view ? VIEW_RECORD_HEAD:RECORD_HEAD;
	if( (offset+rcd_head) <MAIN_BLOCK)
	{
		this->Seek(offset,CFile::begin);
		Read(&RcdLength,sizeof(UINT));
	}
	if( (offset+rcd_head+RcdLength) >= MAIN_BLOCK)
	{
		nex=(ID&0xFFFFFFFF00000000) + PDB( (BLOCK(ID)+1)*MAIN_BLOCK  );
        next=CMemory::ReadDBBlock(nex);
		ASSERT(next);
        buffer=new BYTE[2*MAIN_BLOCK];
		buf1=this->Detach();
		buf2=next->Detach();
        cmf.Attach(buffer,2*MAIN_BLOCK);
		cmf.SeekToBegin();
		cmf.Write(buf1,MAIN_BLOCK);
		cmf.Write(buf2,MAIN_BLOCK);

		cmf.Seek(offset,CFile::begin);
		cmf.Read(&Ret->m_length,sizeof(UINT));
		if(view)
		{
			cmf.Seek(2*sizeof(PDB),CFile::current);//跳过两个addr
		}
		else
		{
			cmf.Read(&exist,sizeof(BYTE));
			if(!exist)
				return NULL;
			cmf.Read(&Ret->ID,sizeof(UINT));
			cmf.Read(&Ret->time,sizeof(int));
		}
		Ret->body=new BYTE[Ret->m_length];
		cmf.Read(Ret->body,Ret->m_length);
		
		delete [](cmf.Detach());
		this->Attach(buf1,MAIN_BLOCK);
		next->Attach(buf2,MAIN_BLOCK);

		return Ret;
	}
	else
	{
		this->Seek(offset,CFile::begin);
		Read(&Ret->m_length,sizeof(UINT));
		ASSERT(Ret->m_length==RcdLength);
		if(view)
		{
			Seek(2*sizeof(PDB),CFile::current);//跳过两个addr
		}
		else
		{
			Read(&exist,sizeof(BYTE));
			if(!exist)
				return NULL;
			Read(&Ret->ID,sizeof(UINT));
			Read(&Ret->time,sizeof(int));
		}
		Ret->body=new BYTE[Ret->m_length];
		Read(Ret->body,Ret->m_length);

		return Ret;
	}
}

Record *CBlock::DeleteRecord(UINT uOffset,Relationship *R)
{
	rtag=0x1;
	wtag=0x1;
    BYTE exist=0;

	Record *deleted=new Record(R);
    
	PDB nex;
	CMemFile cmf;
	BYTE *buffer=NULL;
	BYTE *buf1=NULL;
	BYTE *buf2=NULL;
	CBlock *next=NULL;
	UINT RcdLength=0;
	
    if( (uOffset+RECORD_HEAD) <MAIN_BLOCK)
	{
		this->Seek(uOffset,CFile::begin);
		Read(&RcdLength,sizeof(UINT));
	}

	if( (uOffset+RECORD_HEAD+RcdLength) >= MAIN_BLOCK)
	{
		nex=(ID&0xFFFFFFFF00000000) + PDB( (BLOCK(ID)+1)*MAIN_BLOCK  );
        next=CMemory::ReadDBBlock(nex);
		ASSERT(next);
        buffer=new BYTE[2*MAIN_BLOCK];
		buf1=this->Detach();
		buf2=next->Detach();
        cmf.Attach(buffer,2*MAIN_BLOCK);
		cmf.SeekToBegin();
		cmf.Write(buf1,MAIN_BLOCK);
		cmf.Write(buf2,MAIN_BLOCK);

		cmf.Seek(uOffset,CFile::begin);
		cmf.Read(&deleted->m_length,sizeof(UINT));
		cmf.Write(&exist,sizeof(BYTE));//exist==0
		cmf.Read(&deleted->ID,sizeof(UINT));
		cmf.Read(&deleted->time,sizeof(int));
		cmf.Read(deleted->body,deleted->m_length);
		
		CMemory::DeleteID(deleted->ID,RECORD_ID,R->ID);
		
		delete [](cmf.Detach());
		this->Attach(buf1,MAIN_BLOCK);
		next->Attach(buf2,MAIN_BLOCK);

		return deleted;
	}
	else
	{
		Seek(uOffset,CFile::begin);
		Read(&deleted->m_length,sizeof(UINT));
		Write(&exist,sizeof(BYTE));//exist==0
		Read(&deleted->ID,sizeof(UINT));
		Read(&deleted->time,sizeof(int));
		Read(deleted->body,deleted->m_length);

		CMemory::DeleteID(deleted->ID,RECORD_ID,R->ID);

		return deleted;
	}
}

/*Record CBlock::UpdateRecord(UINT uOffset, CUpdate up,Relationship *R)
{
    Record rcd;
	Record willRet;
	rcd.pattern=R;

	BYTE exist;
	PDB nex;
	CMemFile cmf;
	BYTE *buffer=NULL;
	BYTE *buf1=NULL;
	BYTE *buf2=NULL;
	CBlock *next=NULL;
	UINT RcdLength=0;
 
	if( (uOffset+RECORD_HEAD) <MAIN_BLOCK)
	{
		this->Seek(uOffset,CFile::begin);
		Read(&RcdLength,sizeof(UINT));
	}
	if( (uOffset+RECORD_HEAD+RcdLength) >= MAIN_BLOCK)
	{
		nex=(ID&0xFFFFFFFF00000000) + PDB( (BLOCK(ID)+1)*MAIN_BLOCK  );
        next=CMemory::ReadDBBlock(nex);
		ASSERT(next);
        buffer=new BYTE[2*MAIN_BLOCK];
		buf1=this->Detach();
		buf2=next->Detach();
        cmf.Attach(buffer,2*MAIN_BLOCK);
		cmf.SeekToBegin();
		cmf.Write(buf1,MAIN_BLOCK);
		cmf.Write(buf2,MAIN_BLOCK);

		cmf.Seek(uOffset,CFile::begin);
		cmf.Read(&rcd.length,sizeof(UINT));
		cmf.Read(&exist,sizeof(BYTE));
		cmf.Read(&rcd.ID,sizeof(UINT));
		cmf.Read(&rcd.time,sizeof(int));
		rcd.body=new BYTE[rcd.length];
		cmf.Read(rcd.body,rcd.length);
		willRet=rcd;

		//Update
		WORD i;
		WORD AttrNo=0;
		cmf.Seek(uOffset+RECORD_HEAD-sizeof(int),CFile::begin);//指向时间戳
		int time=CMemory::GetTime();
		cmf.Write(&time,sizeof(int));//指向记录体
		for(i=0;i<up.current;i++)
		{
			AttrNo=up.up_itms[i].index;
			rcd.Update(AttrNo,up.up_itms[i].value);//更改记录体
		}
		cmf.Write(rcd.body,rcd.length);//写新记录体
		
		delete [](cmf.Detach());
		this->Attach(buf1,MAIN_BLOCK);
		next->Attach(buf2,MAIN_BLOCK);

		return willRet;
	}
	else
	{
		this->Seek(uOffset,CFile::begin);
		Read(&rcd.length,sizeof(UINT));
		ASSERT(rcd.length==RcdLength);
		Read(&exist,sizeof(BYTE));
		Read(&rcd.ID,sizeof(UINT));
		Read(&rcd.time,sizeof(int));
		rcd.body=new BYTE[rcd.length];
		Read(rcd.body,rcd.length);
		willRet=rcd;

        WORD i;
		WORD AttrNo=0;
		Seek(uOffset+RECORD_HEAD-sizeof(int),CFile::begin);//指向时间戳
		int time=CMemory::GetTime();
		Write(&time,sizeof(int));//指向记录体
		for(i=0;i<up.current;i++)
		{
			AttrNo=up.up_itms[i].index;
			rcd.Update(AttrNo,up.up_itms[i].value);//更改记录体
		}
		Write(rcd.body,rcd.length);//写新记录体

		return willRet;
	}
}*/

⌨️ 快捷键说明

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