Exemplo n.º 1
0
//NOTICE:this can only be done by one thread
//write out all the elements to hard disk. 
bool LRUCache::flush()
{
#ifdef DEBUG_VSTREE
	cout<<"to flush in LRUCache"<<endl;
#endif
	FILE* filePtr = fopen(this->dataFilePath.c_str(), "r+b");

	if (filePtr == NULL)
	{
		cerr << "error, can't open file. @LRUCache::flush" << endl;
		return false;
	}

	int startIndex = LRUCache::DEFAULT_NUM;
	int endIndex = startIndex + this->size;
	size_t vNodeSize = VNode::VNODE_SIZE;
	//size_t vNodeSize = sizeof(VNode);

	//NOTICE:values are continuous
	for (int i = startIndex; i < endIndex; ++i)
	{
		VNode* nodePtr = this->values[i];
		int line = this->keys[i];
		//cout<<"file line to write "<<line<<endl;

#ifdef DEBUG
		if (nodePtr->getFileLine() != line)
		{
			cout << "line error at !!!" << line << " " << nodePtr->getFileLine() << endl;
		}
#endif

		if (nodePtr == NULL)
		{
			cerr << "error, VNode do not exist. @LRUCache::flush" << endl;
			return false;
		}

		if(!nodePtr->isDirty())
		{
			continue;
		}

		int flag = 0;
		long long seekPos = (long long)line * vNodeSize;
		flag = fseek(filePtr, seekPos, SEEK_SET);

		if (flag != 0)
		{
			cerr << "error, can't seek to the fileLine. @LRUCache::flush" << endl;
			return false;
		}

		//fwrite((char *)nodePtr, vNodeSize, 1, filePtr);
		nodePtr->writeNode(filePtr);
	}
	fclose(filePtr);

	return true;
}
Exemplo n.º 2
0
//just write the values[_pos] to the hard disk, the VNode in memory will not be free. 
bool 
LRUCache::writeOut(int _pos, int _fileLine)
{
	VNode* nodePtr = this->values[_pos];
	FILE* filePtr = fopen(this->dataFilePath.c_str(), "r+b");

	if (nodePtr == NULL)
	{
		cerr << "error, VNode do not exist. @LRUCache::writeOut" << endl;
		return false;
	}
	if (filePtr == NULL)
	{
		cerr << "error, can't open file. @LRUCache::writeOut" << endl;
		return false;
	}

	if (nodePtr->getFileLine() != _fileLine)
	{
		cerr << "error, fileLine " << _fileLine <<" "<< nodePtr->getFileLine() << " wrong. @LRUCache::writeOut" << endl;
	}

	if(!nodePtr->isDirty())
	{
		//cout<<"the node not dirty!"<<endl;
		fclose(filePtr);
		return true;
	}
	else //is modified
	{
		nodePtr->setDirty(false);
	}

	int line = _fileLine == -1 ? nodePtr->getFileLine() : _fileLine;
	size_t vNodeSize = VNode::VNODE_SIZE;
	//size_t vNodeSize = sizeof(VNode);
	int flag = 0;
	long long seekPos = (long long)line * vNodeSize;

	flag = fseek(filePtr, seekPos, SEEK_SET);

	if (flag != 0)
	{
		cerr << "error, can't seek to the fileLine. @LRUCache::writeOut" << endl;
		return false;
	}

	//fwrite((char *)nodePtr, vNodeSize, 1, filePtr);
	nodePtr->writeNode(filePtr);
	fclose(filePtr);

	return true;
}