Index_Core

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;
   
   
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值