东西写的太简单了 都不好意思说是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的更多相关文章

  1. [评测]低配环境下,PostgresQL和Mysql读写性能简单对比(欢迎大家提出Mysql优化意见)

    [评测]低配环境下,PostgresQL和Mysql读写性能简单对比 原文链接:https://www.cnblogs.com/blog5277/p/10658426.html 原文作者:博客园--曲 ...

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

    在Spring中,我们可以通过 @Autowired注解的方式为一个方法中注入参数,那么这种方法背后到底发生了什么呢,这篇文章将讲述如何用Java的注解和反射实现一个“低配版”的依赖注入. 下面是我们 ...

  3. Linux笔记 #08# shell编程从零开始到低配学生管理系统

    先熟悉一下基本语法(运行环境是装git的时候一起装的那个windows下的bash): #!/bin/bash # 实现两个函数 # appendToFile()追加一行到文件 # readFile( ...

  4. GTX 750等低配显卡如何玩转Deepfakes?

    这里说的Deepfakes软件还是DeepFaceLab,人工智能换脸,是使用深度学习方法来实现的.而深度学习程序对电脑配置要求是非常高的,尤其是跑模型这个环节.很多低配电脑,根本就跑步起来.比如像G ...

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

    引论 simple-socket是我写的一个"低配版"的Web实时通信工具(相对于Socket.io),在参考了相关源码和资料的基础上,实现了前后端实时互通的基本功能 选用了Web ...

  6. 【JavaScript】论一个低配版Web实时通信库是如何实现的之二( EventSource篇)

    前情提要 「 话说上回说到!那WebSocket大侠,巧借http之内力,破了敌阵的双工鸳鸯锁,终于突出重围. 然而玄难未了,此时web森林中飞出一只银头红缨枪,划破夜色. "莫非!?&qu ...

  7. Jenkins 结合 Docker 为 .NET Core 项目实现低配版的 CI&CD

    随着项目的不断增多,最开始单体项目手动执行 docker build 命令,手动发布项目就不再适用了.一两个项目可能还吃得消,10 多个项目每天让你构建一次还是够呛.即便你的项目少,每次花费在发布上面 ...

  8. 搭建react项目(低配版)

    react项目低配版,可作为react相关测试的基础环境,方便快速进行测试. git clone git@github.com:whosMeya/simple-react-app.git git ch ...

  9. 基于canvas和web audio实现低配版MikuTap

    导言 最近发掘了一个特别happy的网页小游戏--MikuTap.打开之后沉迷了一下午,导致开发工作没做完差点就要删库跑路了,还好boss瞥了我一眼就没下文了.于是第二天我就继续沉迷,随着一阵抽搐,这 ...

随机推荐

  1. CYQ.Data 批量添加数据性能测试(每秒千、万)---003

    原文地址:https://www.cnblogs.com/cyq1162/p/3216267.html 今天有网友火晋地同学进了CYQ.Data官方群了,他正在折腾了一个各大ORM性能测试的比较的软件 ...

  2. Linux运维面试贩卖思路如下

    1.自我介绍 2.技术介绍 3.上家公司情况介绍.多少人的团队.运维多少人.多少设备.公司什么业务.访问量多少.并发多少.架构多大,然后介绍公司架构.CDN->负载均衡->web-> ...

  3. 使用Larave5.6l提交POST请求出现The page has expired due to inactivity错误

    使用Larave5.6l提交POST请求出现The page has expired due to inactivity错误 一般是由于没有添加 csrf造成的 在表单下面的 第一个行 添加如下代码即 ...

  4. jdk1,8 HashMap

    Java源码分析:HashMap 1.8 相对于1.7 到底更新了什么? 上面网站总结很详细  源码除了具体操作其余全罗列.这里就不总结了

  5. 国内各类“壳子”浏览器,userAgent 一览

    [测试环境]:测试日期:2014-6-20 我本机的chrome是36.0的,IE是10.0的.下列各浏览器大多数都是最新版,少数是半年以内的版本. 内核 chrome 版本 36.0 userAge ...

  6. 【独家】完美解决appium安装app时,需要手动确认安装的问题

    appium初始化driver时,如果未安装该app会先进行安装,安装时,很多安卓手机都会弹框,需要手动确认安装. 如小米的机器, 这是个头疼的问题,之前在网上找遍了,只有通过adb去点相对坐标成功了 ...

  7. RAD 10.1多标签页bug

    frm->Parent= ActiveControl取不到了 ::setparent(frm-> 多屏幕显示器,次副屏幕上无法最大化了. ::SetParent(myform->Ha ...

  8. 使用Ping来做等待的时间计算

    利用ping两次发送消息之间的间隔时间.ping在发送多个消息时,在得到上一次消息的回应后,它会再等待1秒的时间才发送下一次消息,而这个回应时间因机型.系统和网络配置而不同,其中IP地址尤其关键,只有 ...

  9. 使用plsql进行查询的时候出现错误:动态执行表不可访问,本会话的自动统计被终止

  10. kubectl 获取信息

    获取pod所在节点的ip kubectlget po tiller-deploy-8694f8fddc-c2rql -n kube-system -o jsonpath='{.status.hostI ...