Esempio n. 1
0
void BufferManager::deleteRecord(const std::string& fileName, int blockOffset, int start)//给recordManager
{
	int n = fileName.find(".table",0);
	std::string tableName = fileName.substr(0, n);

	Table* tablePtr = Cat.getTablePtr(tableName);
	std::string ss = "";
	for (int i = 0; i < tablePtr->recordSize; i++)
		ss += '\0';
	std::list<Block*>::iterator it = readFileBlock(fileName, blockOffset);
	
	if (start + tablePtr->recordSize>BLOCK_SIZE)
	{
		int length1 = BLOCK_SIZE - start;
		int length2 = tablePtr->recordSize - length1;
		(*it)->changeContent(start,ss.substr(0,length1));
		it = readFileBlock(fileName, blockOffset+1);
		(*it)->changeContent(0,ss.substr(0,length2));
	}
	else
		(*it)->changeContent(start, ss);

	int recordOffset = (blockOffset*BLOCK_SIZE + start) / tablePtr->recordSize;
	tablePtr->emptyRecordOffset.push_back(recordOffset);
}
Esempio n. 2
0
void BufferManager::readFile(const std::string& fileName)
{
	int blockNum; int n;
	if ((n = fileName.find(".table", 0)) != std::string::npos)
	{
		Table* tablePtr = Cat.getTablePtr(fileName.substr(0,n));
		blockNum = tablePtr->blockNum;
	}
	if ((n = fileName.find(".index", 0)) != std::string::npos)
	{
		Index* indexPtr = Cat.getIndexPtr(fileName.substr(0, n));
		blockNum = indexPtr->blockNum;
	}
	std::list<Block*>::iterator it;
	for (int i = 0; i < blockNum; i++)
		it = readFileBlock(fileName, i);
}
Esempio n. 3
0
std::string BufferManager::getRecord(const std::string& fileName, int blockOffset, int start)
{
	int n = fileName.find(".table",0);
	Table* tablePtr = Cat.getTablePtr(fileName.substr(0, n));
	
	std::list<Block*>::iterator it = readFileBlock(fileName, blockOffset);
	std::string record = "";
	if (start + tablePtr->recordSize > BLOCK_SIZE)
	{
		int length1 = BLOCK_SIZE - start;
		int length2 = tablePtr->recordSize - length1;
		for (int i = 0; i < length1; i++)
			record += (*it)->content[start + i];
		it = readFileBlock(fileName, blockOffset+1);
		for (int i = 0; i < length2; i++)
			record += (*it)->content[i];
	}
	else
		for (int i = 0; i < tablePtr->recordSize; i++)
			record += (*it)->content[i+start];
	return record;
}
Esempio n. 4
0
int BufferManager::insertRecord(const std::string& fileName, const std::string& content)
{
	int n = fileName.find(".table", 0);
	std::string tableName = fileName.substr(0, fileName.size()-6);
	Table* tablePtr = Cat.getTablePtr(tableName);

	std::list<Block*>::iterator it;

	if (tablePtr->emptyRecordOffset.empty() == 1)//没有空位
	{
		it = addBlockInFile(fileName);
		(*it)->blockOffset = tablePtr->blockNum;
		(*it)->fileName = fileName;
		(*it)->isDirty = 1;
		tablePtr->blockNum++;
		(*it)->changeContent(0, content);
		int recordOffset = ((tablePtr->blockNum - 1)*BLOCK_SIZE )/ tablePtr->recordSize;
		int num;
		if (BLOCK_SIZE % tablePtr->recordSize == 0)
			num = BLOCK_SIZE / tablePtr->recordSize;
		else
			num = BLOCK_SIZE / tablePtr->recordSize + 1;

		for (int i = 1; i < num; i++)
			tablePtr->emptyRecordOffset.push_back(recordOffset + i);
		
		return (*it)->blockOffset << 16;
	}
	else//有空位
	{
		int recordOffset = *((tablePtr->emptyRecordOffset).begin());
		(tablePtr->emptyRecordOffset).erase((tablePtr->emptyRecordOffset).begin());

		int blockOffset = (recordOffset*tablePtr->recordSize) / BLOCK_SIZE;
		int start = (recordOffset*tablePtr->recordSize) % BLOCK_SIZE;
		it = readFileBlock(fileName, blockOffset);
		if (start + tablePtr->recordSize > BLOCK_SIZE)//记录跨block
		{
			int length1 = BLOCK_SIZE - start;
			int length2 = tablePtr->recordSize - length1;
			(*it)->changeContent(start, content.substr(0, length1));
			if (blockOffset + 1 == tablePtr->blockNum)//所跨block的下一个需要生成
			{
				it = addBlockInFile(fileName);
				(*it)->blockOffset = tablePtr->blockNum;
				(*it)->fileName = fileName;
				(*it)->isDirty = 1;
				tablePtr->blockNum++;
				(*it)->changeContent(0, content.substr(length1, length2));
				int i = recordOffset+1;
				int size = length2;
				while (size < BLOCK_SIZE)
				{
					tablePtr->emptyRecordOffset.push_back(i);
					size += tablePtr->recordSize;
					i++;
				}
			}
			else//所跨block的下一个不需要生成
			{
				it = readFileBlock(fileName,blockOffset+1);
				(*it)->changeContent(0, content.substr(length1, length2));
			}
			
		}
		else//记录不跨block
			(*it)->changeContent(start, content); 

		return (blockOffset << 16) | start;
			
	}
}