example_tc_hashmap.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_hashmap.h"
  17. #include "util/tc_common.h"
  18. #include "util/tc_shm.h"
  19. #include "util/tc_option.h"
  20. #include <stdlib.h>
  21. #include <iostream>
  22. using namespace tars;
  23. ////////////////////////////////////////////////////////////////////
  24. //
  25. TC_Shm g_shm;
  26. TC_HashMap g_hmap;
  27. vector<pair<string, string> > prepare()
  28. {
  29. cout << "start pareparing data" << endl;
  30. size_t count = 10000;
  31. vector<pair<string, string> > v;
  32. vector<string> vs;
  33. vs.push_back("a");
  34. vs.push_back("ab");
  35. vs.push_back("abc");
  36. vs.push_back("abcd");
  37. vs.push_back("abcde");
  38. vs.push_back("abcdef");
  39. vs.push_back("abcdefg");
  40. vs.push_back("abcdefgh");
  41. vs.push_back("abcdefghi");
  42. vs.push_back("abcdefghij");
  43. vs.push_back("abcdefghijk");
  44. vs.push_back("abcdefghijkl");
  45. vs.push_back("abcdefghijklm");
  46. srand(time(NULL));
  47. for(size_t i = 0; i < count; i++)
  48. {
  49. pair<string, string> data;
  50. ostringstream s;
  51. s << i << "_" << rand();
  52. data.first = s.str() + "_" + vs[rand() % vs.size()];
  53. data.second = s.str() + "_" + vs[rand() % vs.size()];
  54. v.push_back(data);
  55. }
  56. cout << "pareparing data OK:" << v.size() << endl;
  57. return v;
  58. }
  59. void initMem()
  60. {
  61. size_t iMemSize = 1024*10;
  62. g_shm.init(iMemSize, 8888);
  63. }
  64. void initMap()
  65. {
  66. if (g_shm.iscreate())
  67. {
  68. size_t iAvgSize = 10;
  69. g_hmap.initDataBlockSize(iAvgSize, iAvgSize, 1.0);
  70. g_hmap.create(g_shm.getPointer(), g_shm.size());
  71. g_hmap.setEraseCount(3);
  72. }
  73. else
  74. {
  75. g_hmap.connect(g_shm.getPointer(), g_shm.size());
  76. }
  77. cout << "init mem" << endl;
  78. }
  79. void testSendRand()
  80. {
  81. vector<pair<string, string> > v = prepare();
  82. vector<TC_HashMap::BlockData> vRecords;
  83. for(int i = 0; i < 10000; i++)
  84. {
  85. size_t index = rand() % v.size();
  86. ostringstream s;
  87. s << i ;
  88. v[index].first = s.str();
  89. v[index].second = s.str();
  90. cout << "set:" << v[index].first << ":" << v[index].second << ":" << g_hmap.size() << ":" << g_hmap.dirtyCount() << endl;
  91. int ret = g_hmap.set(v[index].first, v[index].second, true, vRecords);
  92. assert(ret == 0);
  93. }
  94. cout << g_hmap.size() << "|" << g_hmap.dirtyCount() << endl;
  95. }
  96. void testGetRand()
  97. {
  98. vector<pair<string, string> > v = prepare();
  99. for(int i = 0; i < 200000; i++)
  100. {
  101. size_t index = rand() % v.size();
  102. time_t iSyncTime;
  103. int ret = g_hmap.get(v[index].first, v[index].second, iSyncTime);
  104. assert(ret == 0);
  105. cout << "get:" << v[index].first << ":" << v[index].second << ":" << g_hmap.size() << ":" << g_hmap.dirtyCount() << endl;
  106. }
  107. cout << g_hmap.size() << "|" << g_hmap.dirtyCount() << endl;
  108. }
  109. void usage(char *argv)
  110. {
  111. cout << argv << " --help" << endl;
  112. cout << "\t --release" << endl;
  113. cout << "\t --view" << endl;
  114. cout << "\t --sync [--count=]" << endl;
  115. cout << "\t --set-rand" << endl;
  116. cout << "\t --set-tars [--key=] [--value=]" << endl;
  117. cout << "\t --del [--key=]" << endl;
  118. cout << "\t --get [--key=]" << endl;
  119. cout << "\t --get-rand" << endl;
  120. cout << "\t --erase [--key=]" << endl;
  121. cout << "\t --isdirty [--key=]" << endl;
  122. cout << "\t --find [--key=]" << endl;
  123. cout << "\t --listh [--asc]" << endl;
  124. cout << "\t --listt [--asc]" << endl;
  125. cout << "\t --listg [--asc]" << endl;
  126. cout << "\t --listd" << endl;
  127. }
  128. int main(int argc, char *argv[])
  129. {
  130. {
  131. TC_Option option;
  132. option.decode(argc, argv);
  133. map<string, string> m = option.getMulti();
  134. if(option.hasParam("help") || option.getMulti().empty())
  135. {
  136. usage(argv[0]);
  137. return 0;
  138. }
  139. initMem();
  140. if(option.hasParam("release"))
  141. {
  142. g_shm.del();
  143. return 0;
  144. }
  145. initMap();
  146. /*
  147. fork();
  148. fork();
  149. fork();
  150. */
  151. if(option.hasParam("set-rand"))
  152. {
  153. testSendRand();
  154. }
  155. else if(option.hasParam("set-tars"))
  156. {
  157. vector<TC_HashMap::BlockData> vRecords;
  158. string k;
  159. string v;
  160. k = option.getValue("key");
  161. v = option.getValue("value");
  162. int ret = g_hmap.set(k, v, true, vRecords);
  163. assert(ret == 0);
  164. cout << "set-tars:" << k << ":" << v << " ret=" << ret << endl;
  165. for(size_t i = 0; i < vRecords.size(); i++)
  166. {
  167. cout << "set-tars erase:" << vRecords[i]._key << ":" << vRecords[i]._value << endl;
  168. }
  169. }
  170. else if(option.hasParam("get-rand"))
  171. {
  172. testGetRand();
  173. }
  174. else if(option.hasParam("del"))
  175. {
  176. TC_HashMap::BlockData stRecord;
  177. string k;
  178. k = option.getValue("key");
  179. int ret = g_hmap.del(k, stRecord);
  180. cout << "del:" << k << " ret=" << ret << endl;
  181. cout << "del:" << stRecord._key << endl;
  182. }
  183. else if(option.hasParam("get"))
  184. {
  185. string k;
  186. string v;
  187. k = option.getValue("key");
  188. time_t iSyncTime;
  189. int ret = g_hmap.get(k, v, iSyncTime);
  190. cout << "get:" << k << ":" << v << " ret=" << ret << endl;
  191. }
  192. else if(option.hasParam("erase"))
  193. {
  194. int ratio = TC_Common::strto<int>(option.getValue("ratio"));
  195. TC_HashMap::BlockData data;
  196. int ret = TC_HashMap::RT_OK;
  197. do
  198. {
  199. ret = g_hmap.erase(ratio, data, true);
  200. cout << "erase: " << data._key << "|" << data._value << "|ret:" << ret << endl;
  201. }while(/*ret == TC_HashMap::RT_ERASE_OK*/false);
  202. }
  203. else if(option.hasParam("set-dirty"))
  204. {
  205. string k = option.getValue("key");
  206. int ret = g_hmap.setDirty(k);
  207. cout << "set-dirty: " << k << "|" << ret << endl;
  208. }
  209. else if(option.hasParam("set-clean"))
  210. {
  211. string k = option.getValue("key");
  212. int ret = g_hmap.setClean(k);
  213. cout << "set-clean: " << k << "|" << ret << endl;
  214. }
  215. else if(option.hasParam("isdirty"))
  216. {
  217. string k;
  218. k = option.getValue("key");
  219. int ret = g_hmap.checkDirty(k);
  220. cout << "isdirty:" << k << " ret=" << ret << endl;
  221. }
  222. else if(option.hasParam("find"))
  223. {
  224. string k;
  225. k = option.getValue("key");
  226. TC_HashMap::lock_iterator it = g_hmap.find(k);
  227. if(it != g_hmap.end())
  228. {
  229. string v;
  230. int ret = it->get(k, v);
  231. if(ret != TC_HashMap::RT_OK)
  232. {
  233. cout << "get error: ret=" << ret << endl;
  234. }
  235. else
  236. {
  237. cout << "find ok:" << k << " value=" << v << endl;
  238. }
  239. }
  240. else
  241. {
  242. cout << "find none:" << k << endl;
  243. }
  244. }
  245. else if(option.hasParam("sync"))
  246. {
  247. TC_HashMap::BlockData data;
  248. // size_t n = atoi(option.getValue("count").c_str());
  249. size_t n = 0;
  250. while(true)
  251. {
  252. int ret = g_hmap.sync(time(NULL), data);
  253. if (ret == TC_HashMap::RT_OK)
  254. {
  255. break;
  256. }
  257. else
  258. {
  259. ++n;
  260. cout << "sync:" << data._key << endl;
  261. }
  262. }
  263. cout << g_hmap.desc() << endl;
  264. }
  265. else if(option.hasParam("view"))
  266. {
  267. cout << g_hmap.desc() << endl;
  268. }
  269. else if(option.hasParam("listh"))
  270. {
  271. TC_HashMap::lock_iterator it;
  272. if(option.hasParam("asc"))
  273. {
  274. it = g_hmap.begin();
  275. }
  276. else
  277. {
  278. it = g_hmap.rbegin();
  279. }
  280. while(it != g_hmap.end())
  281. {
  282. string k;
  283. string v;
  284. it->get(k, v);
  285. ++it;
  286. // usleep(100000);
  287. cout << k << ":" << v << endl;
  288. }
  289. }
  290. else if(option.hasParam("listd"))
  291. {
  292. TC_HashMap::lock_iterator it = g_hmap.beginDirty();
  293. while(it != g_hmap.end())
  294. {
  295. string k;
  296. string v;
  297. it->get(k, v);
  298. it++;
  299. // usleep(100000);
  300. cout << k << ":" << v << endl;
  301. }
  302. }
  303. else if(option.hasParam("listt"))
  304. {
  305. TC_HashMap::lock_iterator it;
  306. if(option.hasParam("asc"))
  307. {
  308. it = g_hmap.beginSetTime();
  309. }
  310. else
  311. {
  312. it = g_hmap.rbeginSetTime();
  313. }
  314. while(it != g_hmap.end())
  315. {
  316. string k;
  317. string v;
  318. it->get(k, v);
  319. ++it;
  320. // usleep(100000);
  321. cout << k << ":" << v << endl;
  322. }
  323. }
  324. else if(option.hasParam("listg"))
  325. {
  326. TC_HashMap::lock_iterator it;
  327. if(option.hasParam("asc"))
  328. {
  329. it = g_hmap.beginGetTime();
  330. }
  331. else
  332. {
  333. it = g_hmap.rbeginGetTime();
  334. }
  335. while(it != g_hmap.end())
  336. {
  337. string k;
  338. string v;
  339. it->get(k, v);
  340. ++it;
  341. cout << k << ":" << v << endl;
  342. }
  343. }
  344. else
  345. {
  346. usage(argv[0]);
  347. }
  348. }
  349. return 0;
  350. }