低配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的更多相关文章
- [评测]低配环境下,PostgresQL和Mysql读写性能简单对比(欢迎大家提出Mysql优化意见)
[评测]低配环境下,PostgresQL和Mysql读写性能简单对比 原文链接:https://www.cnblogs.com/blog5277/p/10658426.html 原文作者:博客园--曲 ...
- 【Java】利用注解和反射实现一个"低配版"的依赖注入
在Spring中,我们可以通过 @Autowired注解的方式为一个方法中注入参数,那么这种方法背后到底发生了什么呢,这篇文章将讲述如何用Java的注解和反射实现一个“低配版”的依赖注入. 下面是我们 ...
- Linux笔记 #08# shell编程从零开始到低配学生管理系统
先熟悉一下基本语法(运行环境是装git的时候一起装的那个windows下的bash): #!/bin/bash # 实现两个函数 # appendToFile()追加一行到文件 # readFile( ...
- GTX 750等低配显卡如何玩转Deepfakes?
这里说的Deepfakes软件还是DeepFaceLab,人工智能换脸,是使用深度学习方法来实现的.而深度学习程序对电脑配置要求是非常高的,尤其是跑模型这个环节.很多低配电脑,根本就跑步起来.比如像G ...
- 【Node/JavaScript】论一个低配版Web实时通信库是如何实现的( WebSocket篇)
引论 simple-socket是我写的一个"低配版"的Web实时通信工具(相对于Socket.io),在参考了相关源码和资料的基础上,实现了前后端实时互通的基本功能 选用了Web ...
- 【JavaScript】论一个低配版Web实时通信库是如何实现的之二( EventSource篇)
前情提要 「 话说上回说到!那WebSocket大侠,巧借http之内力,破了敌阵的双工鸳鸯锁,终于突出重围. 然而玄难未了,此时web森林中飞出一只银头红缨枪,划破夜色. "莫非!?&qu ...
- Jenkins 结合 Docker 为 .NET Core 项目实现低配版的 CI&CD
随着项目的不断增多,最开始单体项目手动执行 docker build 命令,手动发布项目就不再适用了.一两个项目可能还吃得消,10 多个项目每天让你构建一次还是够呛.即便你的项目少,每次花费在发布上面 ...
- 搭建react项目(低配版)
react项目低配版,可作为react相关测试的基础环境,方便快速进行测试. git clone git@github.com:whosMeya/simple-react-app.git git ch ...
- 基于canvas和web audio实现低配版MikuTap
导言 最近发掘了一个特别happy的网页小游戏--MikuTap.打开之后沉迷了一下午,导致开发工作没做完差点就要删库跑路了,还好boss瞥了我一眼就没下文了.于是第二天我就继续沉迷,随着一阵抽搐,这 ...
随机推荐
- selenium+python自动化94-行为事件(ActionChains)源码详解
ActionChains简介 actionchains是selenium里面专门处理鼠标相关的操作如:鼠标移动,鼠标按钮操作,按键和上下文菜单(鼠标右键)交互. 这对于做更复杂的动作非常有用,比如悬停 ...
- oracle报ora-12519错误
具体信息如下: ora-12519 tns:no appropriate service handler found the connection descriptor used by the cli ...
- win2008 server ping不同
win2008 server ping不同,网络正常. 下图可以解决!!!
- overflow: auto 图片自适应调整
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 16 MySQL--正确使用索引
count 统计 count(*)和count(字段名) 基本结果是一样的 但是一种情况例外,就是当某字段名下边的数据有null值的时候,不计入这个count中,*则全部列入count中 一 .索引未 ...
- 19 python unit4 常用模块练习题&总结
1.logging模块有几个日志级别? logging模块共有5个级别,分别是: DEBUG INFO WARNING ERROR CRITICAL logging的日志可以分为 debug(), ...
- UI5-文档-4.22-Expression Binding
有时预定义的SAPUI5类型不够灵活,您希望在视图中执行简单的计算或格式化——这正是表达式真正有用的地方.我们使用它们根据数据模型中的当前数字格式化价格. Preview The price is n ...
- 预习 jdbc 技术简介
JDBC简介: JDBC全称为java database connectivity,是sun公司指定的java数据库连接技术的简称. 他是sun公司和数据库开发商共同开发出来的独立于DBMS的应用程序 ...
- C#语法基础
泛型 1.default(T)可以返回T类型的空值,因为你不知道T是值类型还是引用类型,所以别擅自用null 2.泛型约束 很多时候我们不希望使用者太过自由 我们希望他们在使用我们设计的泛型类型时 不 ...
- python+webdriver,选取Select下拉框中的值
在选择下拉框中的值时遇到了困难,用driver.find_element_by_id("").send_keys("")进行赋值不能成功获取下拉框中的值. ...