struct stFileBlock{
int count;
char content[512];
};
stHeader{
int size;
char *content;
};
struct stBody{
int size;
char *content;
};
typedef std::map<std::string,stBody stMemBlockMap;
typedef std::map<std::string,stBody>::iterator stMemBlockIter;
// 写
std::vector<stFileBlock> memBlockToFileBlock(const stMemBlockMap& memBlockMap)
{
std::vector<stFileBlock> block_vec;
stFileBlock block;
char *temp = block.content;
int curId = 0;
int eatSize = 0;
int leftSize = 512;
int PairSize = 0;
for (stMemBlockIter pos = memBlockMap.begin(); pos != memBlockMap.end();)
{
eatSize = pos->first.size() + pos->sceond.size + sizeof(int) +sizeof(int);
if (eatSize <= leftSize)
{
int size = pos->first.size();
bcopy(&size,temp + curId,sizeof(int)); // 写头
curId += sizeof(int);
bcopy(pos->first.c_str(),temp+curId,pos->first.size()); // 写内容
curId += pos->first.size();
bcopy(&pos->second.size,temp + curId,sizeof(int)); //写头
curId += sizeof(int);
bcopy(pos->second.content,block.content + curId,pos->sceond.size) // 写内容
curId += pos->second.size;
leftSize -= eatSize;
PairSize ++;
++pos;
}
else
{
block.size = PairSize;
block_vec.push_back(block);
temp = block.content;
curId = 0;
PairSize = 0;
leftSize = 512;
}
}
if (eatSize <= leftSize + eatSize)
{
block_vec.push_back(block);
}
}
// 读
stMemBlockMap fileBlockVectorToMap(const std::vector<stFileBlock>& blockvec)
{
stMemBlockMap blockMap;
for (std::vector<stFileBlock>::iterator pos = blockvec.begin(); pos != blockvec.end();++pos)
{
if (pos->count == 0)
{
continue;
}
else
{
int eatSize = 0;
for(int i = 0;i< pos->count;i++)
{
stHeader header = *(stHeader*)(pos->content + eatSize);
eatSize += sizeof(stHeader);
stBody body = *(stBody*)(pos->content + eatSize);
eatSize += sizeof(stBody);
std::string key(header.content,header.size);
blockMap[key] = body;
}
}
}
return blockMap;
}
void writeToFile(stFileBlock * block,int id)
{
}
stFileBlock* readBlockFrom(int id)
{
}
int keyToNum(char *content,int size)
{
}
class KVManager{
std::vector<FILE *hF> fileList;
};
Index_Core
最新推荐文章于 2022-10-11 17:07:04 发布