东西写的太简单了 都不好意思说是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. 使用Python调用动态库

    我个人在日常使用电脑时,经常需要使用Google,于是就要切换代理,基本上是一会儿切换为代理,一会儿切换成直连,老是打开internet 选项去设置,很不方便,于是我萌生了一个想法: 做一个开关,我想 ...

  2. create a bootable USB stick on Ubuntu

    https://tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu?_ga=2.141187314.17572770 ...

  3. UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position

    python实现爬虫遇到编码问题: error:UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX ...

  4. leetcode415

    public class Solution { public string AddStrings(string num1, string num2) { //判断num1和num2的长度,进行对齐 i ...

  5. can't load package the specified module could not be found

    can't load package the specified module could not be found 用 Dependency Walker 2.2Dependency Walker ...

  6. Simple2D-26 Simple2D 最后的工作,开发结束

    开始的时候打算将 Simple2D 做成一个库的,但现在没有那个功夫了. 要渲染顶点数据,就必须将渲染函数放置到 glClear( ) 函数和 SwapBuffers( ) 函数之间,但又不希望开发时 ...

  7. python内置函数 eval()、exec()以及complie()函数

    1.eval函数 eval() 函数用来执行一个字符串表达式,并返回表达式的值. eval(expression[, globals[, locals]]) 参数 expression -- 表达式. ...

  8. C# 通用方法

    一. /// <summary> /// 删除字符串中的中文 /// </summary> public static string Delete(string str) { ...

  9. Apache配置本地域名

    打开Apache的安装目录,找到httpd.conf文件,分别去掉下面两行文字前面的#号. LoadModule vhost_alias_module modules/mod_vhost_alias. ...

  10. hibernate注解主键生成策略

    Id生成策略: @GeneratedValue,JPA通用策略生成器 . JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.  TABLE:使用一个特定的数据库表格来 ...