低配NOSQL

Posted itdef

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了低配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;
}

  

以上是关于低配NOSQL的主要内容,如果未能解决你的问题,请参考以下文章

HTML+CSS+JAVASCRIPT 高仿低配网页版网易云音乐播放器

150行代码写个低配版WPS?:手把手教你实现+附完整源码

Java利用Java的注解和反射实现一个"低配版"的依赖注入

Node/JavaScript论一个低配版Web实时通信库是如何实现的1( WebSocket篇)

实现一个基于 IConfiguration 的低配版 FeatureFlag

用Java仿一个低配版的Everything软件