当前位置:Gxlcms > 数据库问题 > 低配NOSQL

低配NOSQL

时间:2021-07-01 10:21:17 帮助过:3人阅读

东西写的太简单了 都不好意思说是NOSQL

其实就是STL 的map容器记录了写入的信息

解析了下数据仅此。

分析的时候想了很多

比如学习redis的自写hash,动态调整hash表容量。

比如右值或者C语言直接操作内存 提升效率

比如多线程操作互斥 网络连接 记录操作时间等等

但是c++写起来,心智负担太多。

实在是太繁琐 一点激情都没了

还是简单一点 写个完整的获益更多。

最后就是这个简单完整的小代码

  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <string>
  4. #include <map>
  5. #include <vector>
  6. #include <assert.h>
  7. using namespace std;
  8. enum Command{
  9. AddKeyValue = 101,
  10. GetKeyValue,
  11. SetValue,
  12. ReplaceValue,
  13. DeleteKeyValue,
  14. AppendValue,
  15. PreappendValue,
  16. InvalidCommand
  17. };
  18. enum TokenVecIndex{
  19. commandIndex = 0,
  20. keyIndex,
  21. valueIndex,
  22. invalidIndex
  23. };
  24. std::unordered_map<std::string,Command> CommandString =
  25. {
  26. {"add",AddKeyValue},
  27. {"get",GetKeyValue},
  28. {"set",SetValue},
  29. {"replace",ReplaceValue},
  30. {"del",DeleteKeyValue},
  31. {"append",AppendValue},
  32. {"preapp",PreappendValue}
  33. };
  34. std::unordered_map<std::string,std::string> ItemHashStorage;
  35. void splitWithSTLFind(const string& str, const string& delim, vector<string>& ret)
  36. {
  37. size_t front = str.find_first_not_of(delim);
  38. size_t back = str.find_first_of(delim, front) ;
  39. while(back != std::string::npos &&
  40. front != std::string::npos){
  41. ret.emplace(ret.end(),str.substr(front, back - front));
  42. front = str.find_first_not_of(delim, back +1);
  43. back = str.find_first_of(delim, front);
  44. }
  45. if(front != std::string::npos){
  46. ret.emplace(ret.end(),str.substr(front, back - front));
  47. }
  48. }
  49. bool CheckToken(std::vector<std::string>& tokenVec)
  50. {
  51. bool bRet = false;
  52. if( (tokenVec[commandIndex] == "get" || tokenVec[commandIndex] == "del")
  53. && tokenVec.size() != 2){
  54. return bRet;
  55. }
  56. if(tokenVec.size() != 3)
  57. return bRet;
  58. bRet = true;
  59. return bRet;
  60. }
  61. bool GetCommand(const std::string& input,std::vector<std::string>& tokenVec){
  62. std::string delim = " ";
  63. tokenVec.clear();
  64. splitWithSTLFind(input,delim,tokenVec);
  65. return CheckToken(tokenVec);
  66. }
  67. bool SetValueFunc(const std::vector<std::string>& tokenVec){
  68. ItemHashStorage[tokenVec[keyIndex]] = tokenVec[valueIndex];
  69. return true;
  70. }
  71. bool AddKeyValueFunc(const std::vector<std::string>& tokenVec){
  72. if( ItemHashStorage.find(tokenVec[keyIndex]) != ItemHashStorage.end())
  73. return true;
  74. SetValueFunc(tokenVec);
  75. return true;
  76. }
  77. bool ReplaceValueFunc(const std::vector<std::string>& tokenVec){
  78. if( ItemHashStorage.find(tokenVec[keyIndex]) == ItemHashStorage.end())
  79. return false;
  80. SetValueFunc(tokenVec);
  81. return true;
  82. }
  83. bool GetKeyValueFunc(const std::vector<std::string>& tokenVec,string& retValueString){
  84. auto it = ItemHashStorage.find(tokenVec[keyIndex]);
  85. if( it == ItemHashStorage.end())
  86. return false;
  87. retValueString = it->second;
  88. return true;
  89. }
  90. bool PreappendValueFunc(const std::vector<std::string>& tokenVec){
  91. auto it = ItemHashStorage.find(tokenVec[keyIndex]);
  92. if( it == ItemHashStorage.end())
  93. return false;
  94. string s = tokenVec[valueIndex];
  95. s.append(it->second);
  96. std::swap(s,it->second);
  97. return true;
  98. }
  99. bool DeleteKeyValueFunc(const std::vector<std::string>& tokenVec){
  100. auto it = ItemHashStorage.find(tokenVec[keyIndex]);
  101. if( it == ItemHashStorage.end())
  102. return true;
  103. ItemHashStorage.erase(it);
  104. return true;
  105. }
  106. bool AppendValueFunc(const std::vector<std::string>& tokenVec){
  107. auto it = ItemHashStorage.find(tokenVec[keyIndex]);
  108. if( it == ItemHashStorage.end())
  109. return false;
  110. (it->second).append(tokenVec[valueIndex]);
  111. return true;
  112. }
  113. bool Excute(const std::vector<std::string>& tokenVec,string& retValueString){
  114. bool bRet = false;
  115. auto it = CommandString.find(tokenVec[commandIndex]);
  116. if( it == CommandString.end())
  117. return bRet;
  118. switch(it->second){
  119. case AddKeyValue:
  120. bRet = AddKeyValueFunc(tokenVec);
  121. break;
  122. case GetKeyValue:
  123. bRet = GetKeyValueFunc(tokenVec,retValueString);
  124. break;
  125. case SetValue:
  126. bRet = SetValueFunc(tokenVec);
  127. break;
  128. case ReplaceValue:
  129. bRet = ReplaceValueFunc(tokenVec);
  130. break;
  131. case DeleteKeyValue:
  132. bRet = DeleteKeyValueFunc(tokenVec);
  133. break;
  134. case AppendValue:
  135. bRet = AppendValueFunc(tokenVec);
  136. break;
  137. case PreappendValue:
  138. bRet = PreappendValueFunc(tokenVec);
  139. break;
  140. default:
  141. break;
  142. }
  143. return bRet;
  144. }
  145. std::vector<std::string> testStringVec1={
  146. " add ",
  147. " add testkey1 testvalue1",
  148. " add testkey1 testvalue1",
  149. " add testkey1 testvalue1",
  150. " add"
  151. };
  152. std::vector<std::string> testStringVec2={
  153. " add ",
  154. " add testkey2 testvalue1",
  155. " add testkey3 testvalue1",
  156. " add testkey4 testvalue1",
  157. " add"
  158. };
  159. int main(int argc, char *argv[])
  160. {
  161. // test
  162. for(auto it:testStringVec1 ){
  163. std::vector<std::string> tokenVec;
  164. if( GetCommand(it,tokenVec)){
  165. std::string s;
  166. Excute(tokenVec,s);
  167. }
  168. }
  169. assert(ItemHashStorage.size()==1);
  170. for(auto it:testStringVec2 ){
  171. std::vector<std::string> tokenVec;
  172. if( GetCommand(it,tokenVec)){
  173. std::string s;
  174. Excute(tokenVec,s);
  175. }
  176. }
  177. assert(ItemHashStorage.size()==4);
  178. {
  179. std::vector<std::string> tokenVec;
  180. string commandStr= "get testkey4";
  181. string s;
  182. GetCommand(commandStr,tokenVec);
  183. Excute(tokenVec,s);
  184. assert(s==string("testvalue1"));
  185. }
  186. {
  187. std::vector<std::string> tokenVec;
  188. string commandStr= "get testkey4 testkey4";
  189. string s;
  190. GetCommand(commandStr,tokenVec);
  191. Excute(tokenVec,s);
  192. assert(s==string("testvalue1"));
  193. }
  194. {
  195. std::vector<std::string> tokenVec;
  196. string commandStr= "get nothing testkey4";
  197. string s;
  198. GetCommand(commandStr,tokenVec);
  199. assert( false == Excute(tokenVec,s));
  200. assert(s == string(""));
  201. }
  202. {
  203. std::vector<std::string> tokenVec;
  204. string commandStr= "set testkey2 testkey4";
  205. string s;
  206. GetCommand(commandStr,tokenVec);
  207. Excute(tokenVec,s);
  208. GetCommand("get testkey2",tokenVec);
  209. Excute(tokenVec,s);
  210. assert(s == ("testkey4"));
  211. }
  212. {
  213. std::vector<std::string> tokenVec;
  214. string commandStr= "replace testkey3 testkey33";
  215. string s;
  216. GetCommand(commandStr,tokenVec);
  217. Excute(tokenVec,s);
  218. GetCommand("get testkey3",tokenVec);
  219. Excute(tokenVec,s);
  220. assert(s == ("testkey33"));
  221. }
  222. {
  223. std::vector<std::string> tokenVec;
  224. string commandStr= "del testkey3 testkey33";
  225. string s;
  226. GetCommand(commandStr,tokenVec);
  227. assert(Excute(tokenVec,s));
  228. GetCommand("get testkey3",tokenVec);
  229. assert(false == Excute(tokenVec,s));
  230. assert(s== (""));
  231. }
  232. {
  233. std::vector<std::string> tokenVec;
  234. string commandStr= "append testkey1 -appendValue";
  235. string s;
  236. GetCommand(commandStr,tokenVec);
  237. assert(Excute(tokenVec,s));
  238. GetCommand("get testkey1",tokenVec);
  239. assert(Excute(tokenVec,s));
  240. assert(s== "testvalue1-appendValue");
  241. }
  242. {
  243. std::vector<std::string> tokenVec;
  244. string commandStr= "preapp testkey1 Pre-";
  245. string s;
  246. GetCommand(commandStr,tokenVec);
  247. assert(Excute(tokenVec,s));
  248. GetCommand("get testkey1",tokenVec);
  249. assert(Excute(tokenVec,s));
  250. assert(s== "Pre-testvalue1-appendValue");
  251. }
  252. return 0;
  253. }

  

低配NOSQL

标签:ash   amp   int   check   ++   代码   with   erase   key   

人气教程排行