本文整理汇总了C++中VNode::getFileLine方法的典型用法代码示例。如果您正苦于以下问题:C++ VNode::getFileLine方法的具体用法?C++ VNode::getFileLine怎么用?C++ VNode::getFileLine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类VNode
的用法示例。
在下文中一共展示了VNode::getFileLine方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: flush
//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;
}
示例2: fopen
//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;
}
示例3: 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;
}
示例4: fopen
//traverse the tree_node_file_path file, load the mapping from entity id to file line.
bool
VSTree::loadEntityID2FileLineMap()
{
FILE* filePtr = fopen(VSTree::tree_node_file_path.c_str(), "rb");
if (filePtr == NULL)
{
cerr << "error, can not open tree node file. @VSTree::loadEntityID2FileLineMap" << endl;
return false;
}
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. @VSTree::loadEntityID2FileLineMap" << endl;
return false;
}
this->entityID2FileLineMap.clear();
VNode* nodePtr = new VNode();
int cycle_count = 0;
while (!feof(filePtr))
{
bool is_node_read = (fread((char *)nodePtr,vNodeSize,1,filePtr) == 1);
if (is_node_read)
{
this->updateEntityID2FileLineMap(nodePtr);
//debug
{
stringstream _ss;
if (cycle_count != nodePtr->getFileLine())
{
_ss << "line=" << cycle_count << " nodeLine=" << nodePtr->getFileLine() << endl;
Util::logging(_ss.str());
}
}
cycle_count ++;
}
}
delete nodePtr;
fclose(filePtr);
return true;
}
示例5: sort
void
VSTree::split(VNode* _p_node_being_split, const SigEntry& _insert_entry, VNode* _p_insert_node)
{
#ifdef DEBUG_VSTREE
stringstream _ss;
_ss << "**********************split happen at "
<< _p_node_being_split->getFileLine() << endl;
_ss << _p_node_being_split->to_str() << endl;
Util::logging(_ss.str());
#endif
// first, add the new child node(if not leaf) or child entry(if leaf) to the full node.
bool just_insert_entry = (_p_insert_node == NULL);
if(just_insert_entry)
{
_p_node_being_split->addChildEntry(_insert_entry, true);
}
else
{
_p_node_being_split->addChildNode(_p_insert_node, true);
}
SigEntry entryA, entryB;
//BETTER: use hanming, xor result or the vector included angle to guess the distince.
//And then also use the farest two as seeds.
//
//two seeds to generate two new nodes.
//seedA kernel: the SigEntry with the minimal count of signature.
//seedB kernel: the SigEntry with the maximal count of signature.
int maxCount = 0; // record the minimal signature count.
int entryA_index = 0; // record the seedA kernel index.
for(int i = 0; i < VNode::MAX_CHILD_NUM; i++)
{
int currentCount = (int) _p_node_being_split->getChildEntry(i).getSigCount();
if(maxCount < currentCount)
{
maxCount = currentCount;
entryA_index = i;
}
}
entryA = _p_node_being_split->getChildEntry(entryA_index);
maxCount = 0;
int entryB_index = 0; // record the seedB kernel index.
for(int i = 0; i < VNode::MAX_CHILD_NUM; i++)
{
//NOTICE:I think xOR should be used here to choose the farest two
int currentCount = entryA.xOR(_p_node_being_split->getChildEntry(i));
//int currentCount = entryA.xEpsilen(_p_node_being_split->getChildEntry(i));
if(i != entryA_index && maxCount <= currentCount)
{
maxCount = currentCount;
entryB_index = i;
}
}
entryB = _p_node_being_split->getChildEntry(entryB_index);
// AEntryIndex: the entry index near seedA.
// BEntryIndex: the entry index near seedB.
std::vector<int> entryIndex_nearA, entryIndex_nearB;
entryIndex_nearA.clear();
entryIndex_nearB.clear();
entryIndex_nearA.push_back(entryA_index);
entryIndex_nearB.push_back(entryB_index);
int nearA_max_size, nearB_max_size;
bool nearA_tooSmall, nearB_tooSmall;
for(int i = 0; i < VNode::MAX_CHILD_NUM; i++)
{
if(i == entryA_index || i == entryB_index) continue;
//should guarantee that each new node has at least MIN_CHILD_NUM children.
nearA_max_size = VNode::MAX_CHILD_NUM - entryIndex_nearB.size();
nearA_tooSmall = (nearA_max_size <= VNode::MIN_CHILD_NUM);
if(nearA_tooSmall)
{
for(; i < VNode::MAX_CHILD_NUM; i++)
{
if (i == entryA_index || i == entryB_index) continue;
entryIndex_nearA.push_back(i);
}
break;
}
nearB_max_size = VNode::MAX_CHILD_NUM - entryIndex_nearA.size();
nearB_tooSmall = (nearB_max_size <= VNode::MIN_CHILD_NUM);
if(nearB_tooSmall)
{
for(; i < VNode::MAX_CHILD_NUM; i++)
{
if(i == entryA_index || i == entryB_index) continue;
entryIndex_nearB.push_back(i);
}
break;
}
//.........这里部分代码省略.........
示例6:
//insert an new Entry, whose entity doesn't exist before
bool
VSTree::insertEntry(const SigEntry& _entry)
{
//choose the best leaf node to insert the _entry
VNode* choosedNodePtr = this->chooseNode(this->getRoot(), _entry);
#ifdef DEBUG_VSTREE
if (_entry.getEntityId() == 4000001)
{
stringstream _ss;
if (choosedNodePtr)
{
_ss << "insert " << _entry.getEntityId()
<< " into [" << choosedNodePtr->getFileLine() << "],\t";
_ss << "whose childnum is " << choosedNodePtr->getChildNum() << endl;
}
else
{
_ss << "insert " << _entry.getEntityId() << " , can not choose a leaf node to insert entry. @VSTree::insert" << endl;
}
Util::logging(_ss.str());
}
#endif
if (choosedNodePtr == NULL)
{
cerr << "error, can not choose a leaf node to insert entry. @VSTree::insert" << endl;
return false;
}
if (choosedNodePtr->isFull())
{
//if the choosed leaf node to insert is full, the node should be split.
this->split(choosedNodePtr, _entry, NULL);
//debug
// if (!choosedNodePtr->checkState())
// {
// stringstream _ss;
// _ss << "node " << choosedNodePtr->getFileLine() << " childFileLine error. after split" << endl;
// Util::logging(_ss.str());
// }
}
else
{
choosedNodePtr->addChildEntry(_entry, false);
choosedNodePtr->refreshAncestorSignature(*(this->node_buffer));
//debug
// if (!choosedNodePtr->checkState())
// {
// stringstream _ss;
// _ss << "node " << choosedNodePtr->getFileLine() << " childFileLine error. after addChildEntry" << endl;
// _ss <<"child num=" << choosedNodePtr->getChildNum() << endl;
// _ss <<"node num=" << this->node_num << " entry num=" << this->entry_num << endl;
// Util::logging(_ss.str());
// }
// update the entityID2FileLineMap.
this->entityID2FileLineMap[_entry.getEntityId()] = choosedNodePtr->getFileLine();
}
this->entry_num ++;
return true;
}
示例7: if
//the _entry_index in _child is to be removed.
//node can only be deleted in this function.
void
VSTree::coalesce(VNode* _child, int _entry_index)
{
#ifdef DEBUG
cout << "coalesce happen" <<endl;
#endif
//found the father and index
VNode* _father = _child->getFather(*(this->node_buffer));
int cn = _child->getChildNum();
if(_father == NULL) //this is already root
{
//NOTICE:when root is leaf, at least one key, otherwise the tree is empty
//But when root is internal, at least two key, if one key then shrink
//(1-key internal root is not permitted)
//
//Notice that leaf-root case has been discussed in upper function removeEntry()
//so here the root must be internal node
_child->removeChild(_entry_index);
if(cn == 2)
{
//only one key after remove, shrink root
VNode* newRoot = _child->getChild(0, *(this->node_buffer));
newRoot->setAsRoot(true);
cout<<"shrink root in coalesce() -- to swap node file"<<endl;
this->swapNodeFileLine(newRoot, _child);
this->root_file_line = newRoot->getFileLine();
this->height--;
this->removeNode(_child);
}
return;
}
if(cn > VNode::MIN_CHILD_NUM)
{
cout<<"no need to move or union in coalesce()"<<endl;
_child->removeChild(_entry_index);
_child->refreshAncestorSignature(*(this->node_buffer));
return;
}
int fn = _father->getChildNum();
int i, _child_index = -1;
for (i = 0; i < fn; ++i)
{
if (_father->getChildFileLine(i) == _child->getFileLine())
{
break;
}
}
if(i == fn)
{
cerr << "not found the leaf node in VSTree::coalesce()" << endl;
return;
}
else
{
_child_index = i;
}
//_child->removeChild(_entry_index);
//_child->setChildNum(cn);
//NOTICE:we do not consider the efficiency here, so just ensure the operation is right
//BETTER:find good way to ensure signatures are separated(maybe similar ones together)
int ccase = 0;
VNode* p = NULL;
int n = 0;
if(_child_index < fn - 1)
{
p = _father->getChild(_child_index+1, *(this->node_buffer));
n = p->getChildNum();
if(n > VNode::MIN_CHILD_NUM)
{
ccase = 2;
}
else
{
ccase = 1;
}
}
if(_child_index > 0)
{
VNode* tp = _father->getChild(_child_index-1, *(this->node_buffer));
int tn = tp->getChildNum();
if(ccase < 2)
{
if(ccase == 0)
ccase = 3;
if(tn > VNode::MIN_CHILD_NUM)
ccase = 4;
}
if(ccase > 2)
{
//.........这里部分代码省略.........
示例8: 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;
}