当前位置: 首页>>代码示例>>C++>>正文


C++ VNode::readNode方法代码示例

本文整理汇总了C++中VNode::readNode方法的典型用法代码示例。如果您正苦于以下问题:C++ VNode::readNode方法的具体用法?C++ VNode::readNode怎么用?C++ VNode::readNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在VNode的用法示例。


在下文中一共展示了VNode::readNode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: readIn

//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;
}
开发者ID:summercyy,项目名称:gStore,代码行数:54,代码来源:LRUCache.cpp

示例2: loadCache

//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;
}
开发者ID:summercyy,项目名称:gStore,代码行数:79,代码来源:LRUCache.cpp


注:本文中的VNode::readNode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。