本文整理汇总了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;
}
示例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;
}