Пример #1
0
//read the value from hard disk, and put it to the values[_pos].
//before use it, you must make sure that the _pos element in cache is free(unoccupied).
bool LRUCache::readIn(int _pos, int _fileLine)
{
#ifdef DEBUG_LRUCACHE
	//cout<<"pos: "<<_pos<<" "<<"fileline: "<<_fileLine<<endl;
#endif
	VNode* nodePtr = new VNode(true);
	//VNode* nodePtr = NULL;
	FILE* filePtr = fopen(this->dataFilePath.c_str(), "rb");

	//if (nodePtr == NULL)
	//{
		//cerr << "error, can not new a VNode. @LRUCache::readIn" << endl;
		//return false;
	//}

	if (filePtr == NULL)
	{
		cerr << "error, can't open " <<
			"[" << this->dataFilePath << "]" <<
			". @LRUCache::readIn" << endl;
		return false;
	}

	int line = _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::readIn" << endl;
		return false;
	}

	//bool is_node_read = (fread((char *)nodePtr, vNodeSize, 1, filePtr) == 1);
	//fread((char *)nodePtr, vNodeSize, 1, filePtr);
	nodePtr->readNode(filePtr);
	fclose(filePtr);

	if (nodePtr == NULL || nodePtr->getFileLine() != _fileLine)
	{
		cout<<"node file line: "<<nodePtr->getFileLine()<<endl;
		cerr << "error,node fileLine error. @LRUCache::readIn" << endl;
	}

	this->setElem(_pos, _fileLine, nodePtr);

	return true;
}
Пример #2
0
//NOTICE:this must be done in one thread(and only one time)
//load cache's elements from an exist data file. 
bool LRUCache::loadCache(string _filePath)
{
	this->dataFilePath = _filePath;

	FILE* filePtr = fopen(this->dataFilePath.c_str(), "rb");
	if (filePtr == NULL)
	{
		cerr << "error, can not load an exist data file. @LRUCache::loadCache" << endl;
		return false;
	}

	//NOTICE:here we set it to the maxium, to ensure all VNODE in memory
	int defaultLoadSize = this->capacity;
	//int defaultLoadSize = this->capacity / 2;
	size_t vNodeSize = VNode::VNODE_SIZE;
	//size_t vNodeSize = sizeof(VNode);
	int flag = 0;

	flag = fseek(filePtr, 0, SEEK_SET);

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

	//int _tmp_cycle_count = 0;

	while (this->size < defaultLoadSize)
	{
		bool is_reach_EOF = feof(filePtr);
		if(is_reach_EOF)
		{
			break;
		}

		VNode* nodePtr = new VNode(true);
		//VNode* nodePtr = NULL;
		//bool is_node_read = (fread((char *)nodePtr, vNodeSize, 1, filePtr) == 1);
		bool is_node_read = nodePtr->readNode(filePtr);

		if (!is_node_read)
		{
			delete nodePtr;
			break;
		}

		//NOTICE:not consider invalid node
		if(nodePtr->getFileLine() < 0)
		{
			//remove invalid node
			delete nodePtr;
			continue;
		}

		//this->size if the real size, while DEFAULT_NUM is the prefix
		//To maintain a double-linked list, the pos 0 is head, while the pos 1 is tail
		int pos = LRUCache::DEFAULT_NUM + this->size;
		this->setElem(pos, nodePtr->getFileLine(), nodePtr);

		//debug
		//{
			//if (_tmp_cycle_count != nodePtr->getFileLine())
			//{
				//stringstream _ss;
				//_ss << "error file line: " << _tmp_cycle_count << " " << nodePtr->getFileLine() << " " << nodePtr->getChildNum() << endl;
				//Util::logging(_ss.str());
			//}
		//}

		//_tmp_cycle_count++;
	}

	fclose(filePtr);

	return true;
}