低配NOSQL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了低配NOSQL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6514字,纯文字阅读大概需要10分钟。
内容图文
东西写的太简单了 都不好意思说是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
标签:ash amp int check ++ 代码 with erase key
本文系统来源:http://www.cnblogs.com/itdef/p/6962202.html
内容总结
以上是互联网集市为您收集整理的低配NOSQL全部内容,希望文章能够帮你解决低配NOSQL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。