东西写的太简单了 都不好意思说是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. Java虚拟机汇编代码

    0:将一个常量加载到操作数栈 3:数值从操作数栈存储到局部变量表 4:将int类型的常量加载到操作数栈 5:数值从操作数栈存储到局部变量表 6:将一个局部变量加载到操作栈 7:将一个局部变量加载到操作 ...

  2. 关于BeautifulSoup类中的tag对象的string和text属性

    <dl> <dt> 今开 </dt><dd class="s-down">3.87</dd> </dl> & ...

  3. 解决报错 Page directive: illegal to have multiple occurrences of contentType with different values (old:

    转自:https://blog.csdn.net/dorothy1224/article/details/78064288

  4. ADODB 调用存储过程

    追加参数法调用存储过程 追加参数通过CreateParameter方法,用来指定属性创建新的Parameter对象.具体语法如下: Set parameter = command.CreatePara ...

  5. sftp(paramiko)

    SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的.但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用S ...

  6. 18 网络编程-TCP/IP各层介绍(5层模型讲解)

    1.TCP/IP五层协议讲解 物理层--数据链路层--网络层--传输层--应用层 我们将应用层,表示层,会话层并作应用层,从tcp/ip五层协议的角度来阐述每层的由来与功能,搞清楚了每层的主要协议 就 ...

  7. 自动选择最佳特征进行分类-SVM (Halcon)

    HALCON12里的example,classify_pills_auto_select_features.hdev. 执行流程: 1.选取相关特征(本例选取color和region组的所有特征)(本 ...

  8. 八月(The Summer is Gone)观后感

    第一次看到这部电影时觉得很亲近,黑白画面,国企改革的背景,浓浓的儿时画面感,原谅我只是一个三十不到的人,可能我比较早熟,对八九十年代还有些记忆,更早以前也通过电视.音乐.书籍等了解过一些,而那些听过又 ...

  9. html资源 链接

    http://m.aicai.com/index.do?agentId=1&vt=5

  10. Retrofit2.0+RxJava2.0问题

    问题1:java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path ...