低配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瞥了我一眼就没下文了.于是第二天我就继续沉迷,随着一阵抽搐,这 ...
随机推荐
- linux双网卡绑定实现冗余与负载均衡
1 编辑/etc/modprobe.conf 在/etc/modprobe.conf里加入如下两行: alias bond0 bonding options bond0 mode=1 miimon ...
- thinkphp5隐藏apache下的index.php
在应用入口文件同级目录添加.htaccess文件,内容如下: <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews Re ...
- 玩转laravel5.4的入门动作(一)
安装前 1 laravel是用composer来做的依赖关系,所以先下载composer 下载地址在这里https://getcomposer.org/download/ windows lin ...
- CUDA C Programming Guide 在线教程学习笔记 Part 1
1. 简介 2. 编程模型 ▶ SM version 指的是硬件构架和特性,CUDA version 指的是软件平台版本. 3. 编程接口.参考 http://chenrudan.github.io/ ...
- leetcode13
public class Solution { private int ChangeToInt(char c) { ; string s = c.ToString(); switch (s) { ca ...
- ADO 读写文本文件
' 创建配置文件 Open ThisWorkbook.Path & "\schema.ini" For Append As #1 ...
- 用yield 实现协程 (包子模型)
协程是一种轻量级的线程 无需线程上下级的开销, 所有的协程都在一个线程内执行 import time def consumer(name): print('%s is start to eat bao ...
- Gradle 多项目构建
Gradle可以轻松处理各种大小规模的项目.小项目由一个单一的构建文件和一个源代码树构成. 大项目可以将其拆分成更小的,相互依赖的模块,以便更容易理解. 多项目构建的结构特征: 在项目的根目录或主目录 ...
- 第一模块第一章 review
---恢复内容开始--- 练习题: 1.简述编译型与解释型语言的区别,且分别列出你知道的那些属于编译型,哪些属于解释型 机器语言:由于计算机内部只能接受二进制代码,因此,用二进制代码0和1描述的指令称 ...
- ABAP-HTTP支持
ABAP 中对HTTP的支持 SAP Web Application Server -> Internet Communication Framework. http://help.sap.c ...