Ejemplo n.º 1
0
void FileHandle::AddBlockInfo(BlockInfo* block)
{
	BlockInfo *bp = block->GetFile()->GetFirstBlock();
	if (bp == NULL) block->GetFile()->SetFirstBlock(block);
	else
	{
		while (bp->GetNext() != NULL)
			bp = bp->GetNext();
		bp->SetNext(block);
	}
	block->GetFile()->IncreaseRecordAmount();
	block->GetFile()->IncreaseRecordLength();
}
Ejemplo n.º 2
0
BlockInfo* FileHandle::GetBlockInfo(FileInfo* file, int block_pos)
{
	BlockInfo* bp = file->GetFirstBlock();
	while (bp != NULL)
	{
		if (bp->get_block_num() == block_pos)
			return bp;
		bp = bp->GetNext();
	}
	return NULL;
}
Ejemplo n.º 3
0
BlockHandle::~BlockHandle()
{
	BlockInfo* b = first_block_;
	//释放所有块
	while (block_count_ > 0)
	{
		BlockInfo* bn = b->GetNext();
		delete b;
		b = bn;
		block_count_--;
	}
}
Ejemplo n.º 4
0
void FileHandle::IncreaseAge()
{
	FileInfo* fp = first_file_;
	while (fp != NULL)
	{
		BlockInfo* bp = fp->GetFirstBlock();
		while (bp != NULL)
		{
			bp->IncreaseAge();
			bp = bp->GetNext();
		}
		fp = fp->GetNext();
	}
}
Ejemplo n.º 5
0
//LRU算法返回该表所包含的所有文件中最老的块的指针(最近最少使用的块)
BlockInfo* FileHandle::LRUAlgorithm()
{
	FileInfo* fp = first_file_;
	//最老的块的前一块
	BlockInfo* oldestbefore = NULL;
	//最老的块
	BlockInfo* oldest = fp->GetFirstBlock();
	//找到最老的块
	while (fp != NULL)
	{
		BlockInfo* bpbefore = NULL;
		BlockInfo* bp = fp->GetFirstBlock();
		while (bp != NULL)
		{
			if (bp->get_age() > oldest->get_age())
			{
				oldestbefore = bpbefore;
				oldest = bp;
			}
			bpbefore = bp;
			bp = bp->GetNext();
		}
		fp = fp->GetNext();
	}
	//如果最老的块被修改过,则把它的内容写回文件
	if (oldest->get_dirty())
		oldest->WriteInfo(path_);
	//如果最老的块的前一位置是空块,则说明第一块就是最老的块。这时因为要移除oldest,所有该文件第一块就变成了oldest->next
	if (oldestbefore == NULL) oldest->GetFile()->SetFirstBlock(oldest->GetNext());
	else oldestbefore->SetNext(oldest->GetNext());
	//将最老的块年龄重置为0
	oldest->ResetAge();
	//oldest的next置为null
	oldest->SetNext(NULL);
	//返回最老的块的指针
	return oldest;
}
Ejemplo n.º 6
0
FileHandle::~FileHandle()
{
	WriteToDisk();
	FileInfo* fp = first_file_;
	while (fp != NULL)
	{
		FileInfo* fpn = fp->GetNext();
		BlockInfo* bp = fp->GetFirstBlock();
		while (bp != NULL)
		{
			BlockInfo* bpn = bp->GetNext();
			delete bp;
			bp = bpn;
		}
		delete fp;
		fp = fpn;
	}
}
Ejemplo n.º 7
0
void FileHandle::WriteToDisk()
{
	FileInfo* fp = first_file_;
	while (fp != NULL)
	{
		BlockInfo* bp = fp->GetFirstBlock();
		while (bp != NULL)
		{
			//如果该块被修改过,则写到文件中
			if (bp->get_dirty())
			{
				bp->WriteInfo(path_);
				bp->set_dirty(false);
			}
			bp = bp->GetNext();
		}
		fp = fp->GetNext();
	}
}