低配NOSQL

东西写的太简单了 都不好意思说是NOSQL

其实就是STL 的map容器记录了写入的信息

解析了下数据仅此。

分析的时候想了很多

比如学习redis的自写hash,动态调整hash表容量。

比如右值或者C语言直接操作内存 提升效率

比如多线程操作互斥 网络连接 记录操作时间等等

但是c++写起来,心智负担太多。

实在是太繁琐 一点激情都没了

还是简单一点 写个完整的获益更多。

最后就是这个简单完整的小代码

#include <iostream>
#include <unordered_map>
#include <string>
#include <map>
#include <vector>
#include <assert.h>
using namespace std;

enum Command{
    AddKeyValue = 101,
    GetKeyValue,
    SetValue,
    ReplaceValue,
    DeleteKeyValue,
    AppendValue,
    PreappendValue,
    InvalidCommand
};

enum TokenVecIndex{
    commandIndex = 0,
    keyIndex,
    valueIndex,
    invalidIndex
};

std::unordered_map<std::string,Command> CommandString =
{
    {"add",AddKeyValue},
    {"get",GetKeyValue},
    {"set",SetValue},
    {"replace",ReplaceValue},
    {"del",DeleteKeyValue},
    {"append",AppendValue},
    {"preapp",PreappendValue}
};

std::unordered_map<std::string,std::string> ItemHashStorage;

void splitWithSTLFind(const string& str, const string& delim, vector<string>& ret)
{
    size_t front = str.find_first_not_of(delim);
    size_t back = str.find_first_of(delim, front) ;

    while(back != std::string::npos &&
          front !=  std::string::npos){
        ret.emplace(ret.end(),str.substr(front, back - front));
        front = str.find_first_not_of(delim, back +1);
        back = str.find_first_of(delim, front);
    }
    if(front != std::string::npos){
        ret.emplace(ret.end(),str.substr(front, back - front));
    }
}


bool CheckToken(std::vector<std::string>& tokenVec)
{
    bool bRet = false;

    if( (tokenVec[commandIndex] == "get" || tokenVec[commandIndex] == "del")
            && tokenVec.size() != 2){
        return bRet;
    }

    if(tokenVec.size() != 3)
        return bRet;

    bRet = true;
    return bRet;
}

bool GetCommand(const std::string& input,std::vector<std::string>& tokenVec){
    std::string delim = " ";
    tokenVec.clear();
    splitWithSTLFind(input,delim,tokenVec);
    return CheckToken(tokenVec);
}

bool SetValueFunc(const std::vector<std::string>& tokenVec){
    ItemHashStorage[tokenVec[keyIndex]] = tokenVec[valueIndex];
    return true;
}


bool AddKeyValueFunc(const std::vector<std::string>& tokenVec){
    if( ItemHashStorage.find(tokenVec[keyIndex]) != ItemHashStorage.end())
        return true;
    SetValueFunc(tokenVec);
    return true;
}

bool ReplaceValueFunc(const std::vector<std::string>& tokenVec){
    if( ItemHashStorage.find(tokenVec[keyIndex]) == ItemHashStorage.end())
        return false;
    SetValueFunc(tokenVec);
    return true;
}

bool GetKeyValueFunc(const std::vector<std::string>& tokenVec,string& retValueString){
    auto it = ItemHashStorage.find(tokenVec[keyIndex]);
    if(  it == ItemHashStorage.end())
        return false;
    retValueString = it->second;
    return true;
}

bool PreappendValueFunc(const std::vector<std::string>& tokenVec){
    auto it = ItemHashStorage.find(tokenVec[keyIndex]);
    if(  it == ItemHashStorage.end())
        return false;
    string s = tokenVec[valueIndex];
    s.append(it->second);
    std::swap(s,it->second);
    return true;
}

bool DeleteKeyValueFunc(const std::vector<std::string>& tokenVec){
    auto it = ItemHashStorage.find(tokenVec[keyIndex]);
    if(  it == ItemHashStorage.end())
        return true;
    ItemHashStorage.erase(it);
    return true;
}

bool AppendValueFunc(const std::vector<std::string>& tokenVec){
    auto it = ItemHashStorage.find(tokenVec[keyIndex]);
    if(  it == ItemHashStorage.end())
        return false;
    (it->second).append(tokenVec[valueIndex]);
    return true;
}

bool Excute(const std::vector<std::string>& tokenVec,string& retValueString){
    bool bRet = false;
    auto it =  CommandString.find(tokenVec[commandIndex]);
    if( it  == CommandString.end())
        return bRet;
    switch(it->second){
    case AddKeyValue:
        bRet = AddKeyValueFunc(tokenVec);
        break;
    case GetKeyValue:
        bRet = GetKeyValueFunc(tokenVec,retValueString);
        break;
    case SetValue:
        bRet = SetValueFunc(tokenVec);
        break;
    case ReplaceValue:
        bRet = ReplaceValueFunc(tokenVec);
        break;
    case DeleteKeyValue:
        bRet = DeleteKeyValueFunc(tokenVec);
        break;
    case AppendValue:
        bRet = AppendValueFunc(tokenVec);
        break;
    case PreappendValue:
        bRet = PreappendValueFunc(tokenVec);
        break;
    default:
        break;
    }

    return bRet;
}


std::vector<std::string> testStringVec1={
    "  add ",
    "  add  testkey1 testvalue1",
    "  add  testkey1 testvalue1",
    "  add  testkey1 testvalue1",
    "  add"
};

std::vector<std::string> testStringVec2={
    "  add ",
    "  add  testkey2 testvalue1",
    "  add  testkey3 testvalue1",
    "  add  testkey4 testvalue1",
    "  add"
};

int main(int argc, char *argv[])
{
    // test
    for(auto it:testStringVec1 ){
        std::vector<std::string> tokenVec;
        if( GetCommand(it,tokenVec)){
            std::string s;
            Excute(tokenVec,s);
        }
    }
    assert(ItemHashStorage.size()==1);

    for(auto it:testStringVec2 ){
        std::vector<std::string> tokenVec;
        if( GetCommand(it,tokenVec)){
            std::string s;
            Excute(tokenVec,s);
        }
    }
    assert(ItemHashStorage.size()==4);

    {
        std::vector<std::string> tokenVec;
        string commandStr= "get testkey4";
        string s;
        GetCommand(commandStr,tokenVec);
        Excute(tokenVec,s);
        assert(s==string("testvalue1"));
    }

    {
        std::vector<std::string> tokenVec;
        string commandStr= "get testkey4 testkey4";
        string s;
        GetCommand(commandStr,tokenVec);
        Excute(tokenVec,s);
        assert(s==string("testvalue1"));
    }

    {
        std::vector<std::string> tokenVec;
        string commandStr= "get nothing testkey4";
        string s;
        GetCommand(commandStr,tokenVec);
        assert( false == Excute(tokenVec,s));
        assert(s == string(""));
    }

    {
        std::vector<std::string> tokenVec;
        string commandStr= "set testkey2 testkey4";
        string s;
        GetCommand(commandStr,tokenVec);
        Excute(tokenVec,s);
        GetCommand("get testkey2",tokenVec);
        Excute(tokenVec,s);
        assert(s == ("testkey4"));
    }

    {
        std::vector<std::string> tokenVec;
        string commandStr= "replace testkey3 testkey33";
        string s;
        GetCommand(commandStr,tokenVec);
        Excute(tokenVec,s);
        GetCommand("get testkey3",tokenVec);
        Excute(tokenVec,s);
        assert(s == ("testkey33"));
    }

    {
        std::vector<std::string> tokenVec;
        string commandStr= "del testkey3 testkey33";
        string s;
        GetCommand(commandStr,tokenVec);
        assert(Excute(tokenVec,s));
        GetCommand("get testkey3",tokenVec);
        assert(false == Excute(tokenVec,s));
        assert(s== (""));
    }

    {
        std::vector<std::string> tokenVec;
        string commandStr= "append testkey1 -appendValue";
        string s;
        GetCommand(commandStr,tokenVec);
        assert(Excute(tokenVec,s));
        GetCommand("get testkey1",tokenVec);
        assert(Excute(tokenVec,s));
        assert(s== "testvalue1-appendValue");
    }

    {
        std::vector<std::string> tokenVec;
        string commandStr= "preapp testkey1 Pre-";
        string s;
        GetCommand(commandStr,tokenVec);
        assert(Excute(tokenVec,s));
        GetCommand("get testkey1",tokenVec);
        assert(Excute(tokenVec,s));
        assert(s== "Pre-testvalue1-appendValue");
    }
    return 0;
}

  

作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
阿里打赏 微信打赏
原文地址:https://www.cnblogs.com/itdef/p/6962202.html