关键词搜索

源码搜索 ×
×

tars源码分析之23

发布2022-07-17浏览731次

详情内容

手撕红黑树,终于来了,代码有点长,可以看看关键部分:

  1. #include "util/tc_rbtree.h"
  2. #include "util/tc_pack.h"
  3. #include "util/tc_common.h"
  4. #include <string.h>
  5. namespace tars
  6. {
  7. bool TC_RBTree::Block::hasLeft()
  8. {
  9. return _pHead->_iLeftChild != 0;
  10. }
  11. bool TC_RBTree::Block::moveToLeft()
  12. {
  13. _iHead = getBlockHead()->_iLeftChild;
  14. _pHead = getBlockHead(_iHead);
  15. return _iHead != 0;
  16. }
  17. bool TC_RBTree::Block::hasRight()
  18. {
  19. return _pHead->_iRightChild != 0;
  20. }
  21. bool TC_RBTree::Block::moveToRight()
  22. {
  23. _iHead = getBlockHead()->_iRightChild;
  24. _pHead = getBlockHead(_iHead);
  25. return _iHead != 0;
  26. }
  27. bool TC_RBTree::Block::hasParent()
  28. {
  29. return _pHead->_iParent != 0;
  30. }
  31. bool TC_RBTree::Block::moveToParent()
  32. {
  33. _iHead = getBlockHead()->_iParent;
  34. _pHead = getBlockHead(_iHead);
  35. return _iHead != 0;
  36. }
  37. /*-----------------------------------------------------------
  38. | i right
  39. | / \ ==> / \
  40. | a right i y
  41. | / \ / \
  42. | b y a b
  43. -----------------------------------------------------------*/
  44. void TC_RBTree::Block::rotateLeft(TC_RBTree::Block::tagBlockHead *i, uint32_t iAddr)
  45. {
  46. assert(i != NULL);
  47. // assert(iAddr == _pMap->getRelative(i));
  48. tagBlockHead *r = getBlockHead(i->_iRightChild);
  49. uint32_t iRAddr = i->_iRightChild;
  50. tagBlockHead *p = getBlockHead(i->_iParent);
  51. assert(r != NULL);
  52. _pMap->saveValue(&i->_iRightChild, r->_iLeftChild);
  53. if(r->_iLeftChild != 0)
  54. {
  55. _pMap->saveValue(&getBlockHead(r->_iLeftChild)->_iParent, iAddr);
  56. }
  57. _pMap->saveValue(&r->_iLeftChild, iAddr);
  58. _pMap->saveValue(&r->_iParent, i->_iParent);
  59. if(p != NULL)
  60. {
  61. if(iAddr == p->_iLeftChild)
  62. {
  63. _pMap->saveValue(&p->_iLeftChild, iRAddr);
  64. }
  65. else
  66. {
  67. _pMap->saveValue(&p->_iRightChild, iRAddr);
  68. }
  69. }
  70. else
  71. {
  72. _pMap->saveValue(&_pMap->_pHead->_iRootAddr, iRAddr);
  73. }
  74. _pMap->saveValue(&i->_iParent, iRAddr);
  75. }
  76. /*-----------------------------------------------------------
  77. | i left
  78. | / \ / \
  79. | left y ==> a node
  80. | / \ / \
  81. | a b b y
  82. -----------------------------------------------------------*/
  83. void TC_RBTree::Block::rotateRight(TC_RBTree::Block::tagBlockHead *i, uint32_t iAddr)
  84. {
  85. assert(i != NULL);
  86. // assert(iAddr == _pMap->getRelative(i));
  87. tagBlockHead *l = getBlockHead(i->_iLeftChild);
  88. uint32_t iLAddr = i->_iLeftChild;
  89. tagBlockHead *p = getBlockHead(i->_iParent);
  90. assert(l != NULL);
  91. _pMap->saveValue(&i->_iLeftChild, l->_iRightChild);
  92. if(l->_iRightChild != 0)
  93. {
  94. _pMap->saveValue(&getBlockHead(l->_iRightChild)->_iParent, iAddr);
  95. }
  96. _pMap->saveValue(&l->_iRightChild, iAddr);
  97. _pMap->saveValue(&l->_iParent, i->_iParent);
  98. if(p != NULL)
  99. {
  100. if(iAddr == p->_iRightChild)
  101. {
  102. _pMap->saveValue(&p->_iRightChild, iLAddr);
  103. }
  104. else
  105. {
  106. _pMap->saveValue(&p->_iLeftChild, iLAddr);
  107. }
  108. }
  109. else
  110. {
  111. _pMap->saveValue(&_pMap->_pHead->_iRootAddr, iLAddr);
  112. }
  113. _pMap->saveValue(&i->_iParent, iLAddr);
  114. }
  115. void TC_RBTree::Block::eraseFixUp(TC_RBTree::Block::tagBlockHead *i, uint32_t iAddr, TC_RBTree::Block::tagBlockHead *p, uint32_t iPAddr)
  116. {
  117. // assert(iAddr == _pMap->getRelative(i));
  118. // assert(iPAddr == _pMap->getRelative(p));
  119. tagBlockHead *o;
  120. uint32_t iOAddr;
  121. while(((!i || i->_iColor == BLACK)) && i != getBlockHead(_pMap->_pHead->_iRootAddr))
  122. {
  123. assert(p != NULL);
  124. if(p->_iLeftChild == iAddr)//_pMap->getRelative(i))
  125. {
  126. o = getBlockHead(p->_iRightChild);
  127. iOAddr = p->_iRightChild;
  128. if(o->_iColor == RED)
  129. {
  130. _pMap->saveValue(&o->_iColor, BLACK);
  131. _pMap->saveValue(&p->_iColor, RED);
  132. rotateLeft(p, iPAddr);
  133. o = getBlockHead(p->_iRightChild);
  134. iOAddr = p->_iRightChild;
  135. }
  136. if((o->_iLeftChild == 0 || getBlockHead(o->_iLeftChild)->_iColor == BLACK)
  137. && (o->_iRightChild == 0 || getBlockHead(o->_iRightChild)->_iColor == BLACK))
  138. {
  139. _pMap->saveValue(&o->_iColor, RED);
  140. i = p;
  141. iAddr = iPAddr;
  142. p = getBlockHead(i->_iParent);
  143. iPAddr = i->_iParent;
  144. }
  145. else
  146. {
  147. if(o->_iRightChild == 0 || getBlockHead(o->_iRightChild)->_iColor == BLACK)
  148. {
  149. _pMap->saveValue(&getBlockHead(o->_iLeftChild)->_iColor, BLACK);
  150. _pMap->saveValue(&o->_iColor, RED);
  151. rotateRight(o, iOAddr);
  152. o = getBlockHead(p->_iRightChild);
  153. iOAddr = p->_iRightChild;
  154. }
  155. _pMap->saveValue(&o->_iColor, p->_iColor);
  156. _pMap->saveValue(&p->_iColor, BLACK);
  157. _pMap->saveValue(&getBlockHead(o->_iRightChild)->_iColor, BLACK);
  158. rotateLeft(p, iPAddr);
  159. i = getBlockHead(_pMap->_pHead->_iRootAddr);
  160. iAddr = _pMap->_pHead->_iRootAddr;
  161. break;
  162. }
  163. }
  164. else
  165. {
  166. o = getBlockHead(p->_iLeftChild);
  167. iOAddr = p->_iLeftChild;
  168. if(o->_iColor == RED)
  169. {
  170. _pMap->saveValue(&o->_iColor, BLACK);
  171. _pMap->saveValue(&p->_iColor, RED);
  172. rotateRight(p, iPAddr);
  173. o = getBlockHead(p->_iLeftChild);
  174. iOAddr = p->_iLeftChild;
  175. }
  176. if((o->_iLeftChild == 0 || getBlockHead(o->_iLeftChild)->_iColor == BLACK)
  177. && (o->_iRightChild == 0 || getBlockHead(o->_iRightChild)->_iColor == BLACK))
  178. {
  179. _pMap->saveValue(&o->_iColor, RED);
  180. i = p;
  181. iAddr = iPAddr;
  182. p = getBlockHead(i->_iParent);
  183. iPAddr = i->_iParent;
  184. }
  185. else
  186. {
  187. if(o->_iLeftChild == 0 || getBlockHead(o->_iLeftChild)->_iColor == BLACK)
  188. {
  189. _pMap->saveValue(&getBlockHead(o->_iRightChild)->_iColor, BLACK);
  190. _pMap->saveValue(&o->_iColor, RED);
  191. rotateLeft(o, iOAddr);
  192. o = getBlockHead(p->_iLeftChild);
  193. iOAddr = p->_iLeftChild;
  194. }
  195. _pMap->saveValue(&o->_iColor, p->_iColor);
  196. _pMap->saveValue(&p->_iColor, BLACK);
  197. _pMap->saveValue(&getBlockHead(o->_iLeftChild)->_iColor, BLACK);
  198. rotateRight(p, iPAddr);
  199. i = getBlockHead(_pMap->_pHead->_iRootAddr);
  200. iAddr = _pMap->_pHead->_iRootAddr;
  201. break;
  202. }
  203. }
  204. }
  205. if(i)
  206. {
  207. _pMap->saveValue(&i->_iColor, BLACK);
  208. }
  209. }
  210. void TC_RBTree::Block::erase(TC_RBTree::Block::tagBlockHead *i, uint32_t iAddr)
  211. {
  212. assert(i != NULL);
  213. // assert(iAddr == _pMap->getRelative(i));
  214. tagBlockHead *c = NULL;
  215. uint32_t iCAddr = 0;
  216. tagBlockHead *p = NULL;
  217. uint32_t iPAddr = 0;
  218. char color;
  219. if(i->_iLeftChild == 0)
  220. {
  221. c = getBlockHead(i->_iRightChild);
  222. iCAddr = i->_iRightChild;
  223. }
  224. else if(i->_iRightChild == 0)
  225. {
  226. c = getBlockHead(i->_iLeftChild);
  227. iCAddr = i->_iLeftChild;
  228. }
  229. else
  230. {
  231. tagBlockHead *old;
  232. uint32_t iOldAddr;
  233. tagBlockHead *left;
  234. old = i;
  235. iOldAddr = iAddr;
  236. //获取右子树的最左节点
  237. iAddr = i->_iRightChild;
  238. i = getBlockHead(i->_iRightChild);
  239. while((left = getBlockHead(i->_iLeftChild)) != NULL)
  240. {
  241. iAddr = i->_iLeftChild;
  242. i = left;
  243. }
  244. c = getBlockHead(i->_iRightChild);
  245. iCAddr = i->_iRightChild;
  246. p = getBlockHead(i->_iParent);
  247. iPAddr = i->_iParent;
  248. color = i->_iColor;
  249. if(c != NULL)
  250. {
  251. _pMap->saveValue(&c->_iParent, iPAddr);
  252. }
  253. if(p != NULL)
  254. {
  255. if(p->_iLeftChild == iAddr)
  256. {
  257. _pMap->saveValue(&p->_iLeftChild, iCAddr);
  258. }
  259. else
  260. {
  261. _pMap->saveValue(&p->_iRightChild, iCAddr);
  262. }
  263. }
  264. else
  265. {
  266. _pMap->saveValue(&_pMap->_pHead->_iRootAddr, iCAddr);
  267. }
  268. if(i->_iParent == iOldAddr)
  269. {
  270. p = i;
  271. iPAddr = iAddr;
  272. }
  273. _pMap->saveValue(&i->_iParent, old->_iParent);
  274. _pMap->saveValue(&i->_iColor, old->_iColor);
  275. _pMap->saveValue(&i->_iRightChild, old->_iRightChild);
  276. _pMap->saveValue(&i->_iLeftChild, old->_iLeftChild);
  277. if(old->_iParent != 0)
  278. {
  279. tagBlockHead *op = getBlockHead(old->_iParent);
  280. if( op->_iLeftChild == iOldAddr)
  281. {
  282. _pMap->saveValue(&op->_iLeftChild, iAddr);
  283. }
  284. else
  285. {
  286. _pMap->saveValue(&op->_iRightChild, iAddr);
  287. }
  288. }
  289. else
  290. {
  291. _pMap->saveValue(&_pMap->_pHead->_iRootAddr, iAddr);
  292. }
  293. _pMap->saveValue(&getBlockHead(old->_iLeftChild)->_iParent, iAddr);
  294. if(old->_iRightChild)
  295. {
  296. _pMap->saveValue(&getBlockHead(old->_iRightChild)->_iParent, iAddr);
  297. }
  298. goto COLOR;
  299. }
  300. p = getBlockHead(i->_iParent);
  301. iPAddr = i->_iParent;
  302. color = i->_iColor;
  303. if(c != NULL)
  304. {
  305. _pMap->saveValue(&c->_iParent, iPAddr);
  306. }
  307. if(p != NULL)
  308. {
  309. if(p->_iLeftChild == iAddr)
  310. {
  311. _pMap->saveValue(&p->_iLeftChild, iCAddr);
  312. }
  313. else
  314. {
  315. _pMap->saveValue(&p->_iRightChild, iCAddr);
  316. }
  317. }
  318. else
  319. {
  320. _pMap->saveValue(&_pMap->_pHead->_iRootAddr, iCAddr);
  321. }
  322. COLOR:
  323. if(color == BLACK)
  324. {
  325. eraseFixUp(c, iCAddr, p, iPAddr);
  326. }
  327. }
  328. void TC_RBTree::Block::insertFixUp(TC_RBTree::Block::tagBlockHead *i, uint32_t iAddr)
  329. {
  330. tagBlockHead *p = NULL;
  331. uint32_t iPAddr = 0;
  332. tagBlockHead *g = NULL;
  333. uint32_t iGAddr = 0;
  334. // assert(iAddr == _pMap->getRelative(i));
  335. while((p = getBlockHead(i->_iParent)) != NULL && p->_iColor == RED)
  336. {
  337. iPAddr = i->_iParent;
  338. g = getBlockHead(p->_iParent);
  339. iGAddr = p->_iParent;
  340. if(i->_iParent == g->_iLeftChild)
  341. {
  342. {
  343. tagBlockHead *u = getBlockHead(g->_iRightChild);
  344. if(u != NULL && u->_iColor == RED)
  345. {
  346. _pMap->saveValue(&u->_iColor, BLACK);
  347. _pMap->saveValue(&p->_iColor, BLACK);
  348. _pMap->saveValue(&g->_iColor, RED);
  349. i = g;
  350. iAddr = iGAddr;
  351. continue;
  352. }
  353. }
  354. if(p->_iRightChild == iAddr)
  355. {
  356. rotateLeft(p, iPAddr);
  357. tagBlockHead *t;
  358. uint32_t iTAddr;
  359. t = p;
  360. iTAddr = iPAddr;
  361. p = i;
  362. iPAddr = iAddr;
  363. i = t;
  364. iAddr = iTAddr;
  365. }
  366. _pMap->saveValue(&p->_iColor, BLACK);
  367. _pMap->saveValue(&g->_iColor, RED);
  368. rotateRight(g, iGAddr);
  369. }
  370. else
  371. {
  372. {
  373. tagBlockHead *u = getBlockHead(g->_iLeftChild);
  374. if(u != NULL && u->_iColor == RED)
  375. {
  376. _pMap->saveValue(&u->_iColor, BLACK);
  377. _pMap->saveValue(&p->_iColor, BLACK);
  378. _pMap->saveValue(&g->_iColor, RED);
  379. i = g;
  380. iAddr = iGAddr;
  381. continue;
  382. }
  383. }
  384. if(p->_iLeftChild == iAddr)
  385. {
  386. rotateRight(p, iPAddr);
  387. tagBlockHead *t;
  388. uint32_t iTAddr;
  389. t = p;
  390. iTAddr = iPAddr;
  391. p = i;
  392. iPAddr = iAddr;
  393. i = t;
  394. iAddr = iTAddr;
  395. }
  396. _pMap->saveValue(&p->_iColor, BLACK);
  397. _pMap->saveValue(&g->_iColor, RED);
  398. rotateLeft(g, iGAddr);
  399. }
  400. }
  401. _pMap->saveValue(&getBlockHead(_pMap->_pHead->_iRootAddr)->_iColor, BLACK);
  402. }
  403. void TC_RBTree::Block::insertGetSetList(TC_RBTree::Block::tagBlockHead *i)
  404. {
  405. //挂在Set链表的头部
  406. if(_pMap->_pHead->_iSetHead == 0)
  407. {
  408. assert(_pMap->_pHead->_iSetTail == 0);
  409. _pMap->saveValue(&_pMap->_pHead->_iSetHead , _iHead);
  410. _pMap->saveValue(&_pMap->_pHead->_iSetTail , _iHead);
  411. }
  412. else
  413. {
  414. assert(_pMap->_pHead->_iSetTail != 0);
  415. _pMap->saveValue(&i->_iSetNext, _pMap->_pHead->_iSetHead);
  416. _pMap->saveValue(&getBlockHead(_pMap->_pHead->_iSetHead)->_iSetPrev, _iHead);
  417. _pMap->saveValue(&_pMap->_pHead->_iSetHead, _iHead);
  418. }
  419. //挂在Get链表头部
  420. if(_pMap->_pHead->_iGetHead == 0)
  421. {
  422. assert(_pMap->_pHead->_iGetTail == 0);
  423. _pMap->saveValue(&_pMap->_pHead->_iGetHead, _iHead);
  424. _pMap->saveValue(&_pMap->_pHead->_iGetTail, _iHead);
  425. }
  426. else
  427. {
  428. assert(_pMap->_pHead->_iGetTail != 0);
  429. _pMap->saveValue(&i->_iGetNext, _pMap->_pHead->_iGetHead);
  430. _pMap->saveValue(&getBlockHead(_pMap->_pHead->_iGetHead)->_iGetPrev, _iHead);
  431. _pMap->saveValue(&_pMap->_pHead->_iGetHead, _iHead);
  432. }
  433. }
  434. void TC_RBTree::Block::insertRBTree(tagBlockHead *i, const string &k)
  435. {
  436. if(_pMap->_pHead->_iRootAddr == 0)
  437. {
  438. _pMap->saveValue(&_pMap->_pHead->_iRootAddr, _iHead);
  439. }
  440. else
  441. {
  442. Block last = _pMap->getLastBlock(k);
  443. assert(last.getBlockHead()->_iLeftChild == 0 || last.getBlockHead()->_iRightChild == 0);
  444. _pMap->saveValue(&i->_iParent, last.getHead());
  445. BlockData data;
  446. int iRet = last.getBlockData(data);
  447. assert(iRet == TC_RBTree::RT_OK || iRet == TC_RBTree::RT_ONLY_KEY);
  448. if(_pMap->_lessf(k, data._key))
  449. {
  450. _pMap->saveValue(&last.getBlockHead()->_iLeftChild , _iHead);
  451. }
  452. else if(_pMap->_lessf(data._key, k))
  453. {
  454. _pMap->saveValue(&last.getBlockHead()->_iRightChild , _iHead);
  455. }
  456. else
  457. {
  458. assert(true);
  459. }
  460. }
  461. //插入新节点后, 需要调整
  462. insertFixUp(i, _iHead);
  463. }
  464. int TC_RBTree::Block::set(const string& k, const string& v, bool bNewBlock, bool bOnlyKey, vector<TC_RBTree::BlockData> &vtData)
  465. {
  466. TC_PackIn pi;
  467. pi << k;
  468. if(!bOnlyKey)
  469. {
  470. pi << v;
  471. }
  472. const char *pData = pi.topacket().c_str();
  473. uint32_t iDataLen = pi.topacket().length();
  474. //首先分配刚刚够的长度, 不能多一个chunk, 也不能少一个chunk
  475. int ret = allocate(iDataLen, vtData);
  476. if(ret != TC_RBTree::RT_OK)
  477. {
  478. return ret;
  479. }
  480. tagBlockHead *i = getBlockHead();
  481. uint32_t iUseSize = i->_iSize - sizeof(tagBlockHead);
  482. //没有下一个chunk, 一个chunk就可以装下数据了
  483. if(!i->_bNextChunk)
  484. {
  485. memcpy(i->_cData, (char*)pData, iDataLen);
  486. //先copy数据, 再复制数据长度
  487. i->_iDataLen = iDataLen;
  488. }
  489. else
  490. {
  491. //copy到当前的block中
  492. memcpy(i->_cData, (char*)pData, iUseSize);
  493. //剩余程度
  494. uint32_t iLeftLen = iDataLen - iUseSize;
  495. uint32_t iCopyLen = iUseSize;
  496. tagChunkHead *pChunk = getChunkHead(i->_iNextChunk);
  497. while(true)
  498. {
  499. //计算chunk的可用大小
  500. iUseSize = pChunk->_iSize - sizeof(tagChunkHead);
  501. if(!pChunk->_bNextChunk)
  502. {
  503. assert(iUseSize >= iLeftLen);
  504. //copy到当前的chunk中
  505. memcpy(pChunk->_cData, (char*)pData + iCopyLen, iLeftLen);
  506. //最后一个chunk, 才有数据长度, 先copy数据再赋值长度
  507. pChunk->_iDataLen = iLeftLen;
  508. iCopyLen += iLeftLen;
  509. iLeftLen -= iLeftLen;
  510. break;
  511. }
  512. else
  513. {
  514. //copy到当前的chunk中
  515. memcpy(pChunk->_cData, (char*)pData + iCopyLen, iUseSize);
  516. iCopyLen += iUseSize;
  517. iLeftLen -= iUseSize;
  518. pChunk = getChunkHead(pChunk->_iNextChunk);
  519. }
  520. }
  521. assert(iLeftLen == 0);
  522. }
  523. //是新块, 需要插入到树中
  524. if(bNewBlock)
  525. {
  526. //插入到二叉树中
  527. insertRBTree(i, k);
  528. //插入到get/set链表中
  529. insertGetSetList(i);
  530. _pMap->incDirtyCount();
  531. _pMap->incElementCount();
  532. }
  533. if(bOnlyKey)
  534. {
  535. //原始数据是脏数据
  536. if(i->_bDirty)
  537. {
  538. _pMap->delDirtyCount();
  539. }
  540. //数据被修改, 设置为脏数据
  541. i->_bDirty = false;
  542. //原始数据不是OnlyKey数据
  543. if(!i->_bOnlyKey)
  544. {
  545. _pMap->incOnlyKeyCount();
  546. }
  547. }
  548. else
  549. {
  550. //原始数据不是脏数据
  551. if(!i->_bDirty)
  552. {
  553. _pMap->incDirtyCount();
  554. }
  555. //数据被修改, 设置为脏数据
  556. i->_bDirty = true;
  557. //原始数据是OnlyKey数据
  558. if(i->_bOnlyKey)
  559. {
  560. _pMap->delOnlyKeyCount();
  561. }
  562. }
  563. //设置是否只有Key
  564. i->_bOnlyKey = bOnlyKey;
  565. return TC_RBTree::RT_OK;
  566. }
  567. int TC_RBTree::Block::getBlockData(TC_RBTree::BlockData &data)
  568. {
  569. data._dirty = isDirty();
  570. data._synct = getSyncTime();
  571. string s;
  572. int ret = get(s);
  573. if(ret != TC_RBTree::RT_OK)
  574. {
  575. return ret;
  576. }
  577. try
  578. {
  579. TC_PackOut po(s.c_str(), s.length());
  580. po >> data._key;
  581. //如果不是只有Key
  582. if(!isOnlyKey())
  583. {
  584. po >> data._value;
  585. }
  586. else
  587. {
  588. return TC_RBTree::RT_ONLY_KEY;
  589. }
  590. }
  591. catch(exception &ex)
  592. {
  593. assert(false);
  594. ret = TC_RBTree::RT_DECODE_ERR;
  595. }
  596. return ret;
  597. }
  598. int TC_RBTree::Block::get(void *pData, uint32_t &iDataLen)
  599. {
  600. tagBlockHead *i = getBlockHead();
  601. //没有下一个chunk, 一个chunk就可以装下数据了
  602. if(!i->_bNextChunk)
  603. {
  604. memcpy(pData, i->_cData, min(i->_iDataLen, iDataLen));
  605. iDataLen = i->_iDataLen;
  606. return TC_RBTree::RT_OK;
  607. }
  608. else
  609. {
  610. uint32_t iUseSize = i->_iSize - sizeof(tagBlockHead);
  611. uint32_t iCopyLen = min(iUseSize, iDataLen);
  612. //copy到当前的block中
  613. memcpy(pData, i->_cData, iCopyLen);
  614. if (iDataLen < iUseSize)
  615. {
  616. return TC_RBTree::RT_NOTALL_ERR; //copy数据不完全
  617. }
  618. //已经copy长度
  619. uint32_t iHasLen = iCopyLen;
  620. //最大剩余长度
  621. uint32_t iLeftLen = iDataLen - iCopyLen;
  622. tagChunkHead *pChunk = getChunkHead(i->_iNextChunk);
  623. while(iHasLen < iDataLen)
  624. {
  625. iUseSize = pChunk->_iSize - sizeof(tagChunkHead);
  626. if(!pChunk->_bNextChunk)
  627. {
  628. //copy到当前的chunk中
  629. uint32_t iCopyLen = min(pChunk->_iDataLen, iLeftLen);
  630. memcpy((char*)pData + iHasLen, pChunk->_cData, iCopyLen);
  631. iDataLen = iHasLen + iCopyLen;
  632. if(iLeftLen < pChunk->_iDataLen)
  633. {
  634. return TC_RBTree::RT_NOTALL_ERR; //copy不完全
  635. }
  636. return TC_RBTree::RT_OK;
  637. }
  638. else
  639. {
  640. uint32_t iCopyLen = min(iUseSize, iLeftLen);
  641. //copy当前的chunk
  642. memcpy((char*)pData + iHasLen, pChunk->_cData, iCopyLen);
  643. if (iLeftLen <= iUseSize)
  644. {
  645. iDataLen = iHasLen + iCopyLen;
  646. return TC_RBTree::RT_NOTALL_ERR; //copy不完全
  647. }
  648. //copy当前chunk完全
  649. iHasLen += iCopyLen;
  650. iLeftLen -= iCopyLen;
  651. pChunk = getChunkHead(pChunk->_iNextChunk);
  652. }
  653. }
  654. }
  655. return TC_RBTree::RT_OK;
  656. }
  657. int TC_RBTree::Block::get(string &s)
  658. {
  659. uint32_t iLen = getDataLen();
  660. char *cData = new char[iLen];
  661. uint32_t iGetLen = iLen;
  662. int ret = get(cData, iGetLen);
  663. if(ret == TC_RBTree::RT_OK)
  664. {
  665. s.assign(cData, iGetLen);
  666. }
  667. delete[] cData;
  668. return ret;
  669. }
  670. void TC_RBTree::Block::setDirty(bool b)
  671. {
  672. tagBlockHead *i = getBlockHead();
  673. if(i->_bDirty != b)
  674. {
  675. if (b)
  676. {
  677. _pMap->incDirtyCount();
  678. }
  679. else
  680. {
  681. _pMap->delDirtyCount();
  682. }
  683. _pMap->saveValue(&i->_bDirty, b);
  684. }
  685. }
  686. void TC_RBTree::Block::deallocate()
  687. {
  688. tagBlockHead *i = getBlockHead();
  689. vector<uint32_t> v;
  690. v.push_back(_iHead);
  691. if(i->_bNextChunk)
  692. {
  693. deallocate(i->_iNextChunk);
  694. }
  695. _pMap->_pDataAllocator->deallocateMemBlock(v);
  696. }
  697. void TC_RBTree::Block::makeNew(uint32_t iAllocSize)
  698. {
  699. tagBlockHead *i = getBlockHead();
  700. i->_iSize = iAllocSize;
  701. i->_iColor = TC_RBTree::RED;
  702. i->_iParent = 0;
  703. i->_iLeftChild = 0;
  704. i->_iRightChild = 0;
  705. i->_iSetNext = 0;
  706. i->_iSetPrev = 0;
  707. i->_iGetNext = 0;
  708. i->_iGetPrev = 0;
  709. i->_iSyncTime = 0;
  710. i->_bNextChunk = false;
  711. i->_iDataLen = 0;
  712. i->_bDirty = true;
  713. i->_bOnlyKey = false;
  714. }
  715. void TC_RBTree::Block::erase()
  716. {
  717. tagBlockHead *i = getBlockHead();
  718. 从树删除节点/
  719. erase(i, _iHead);
  720. //修改脏数据链表/
  721. if(_pMap->_pHead->_iDirtyTail == _iHead)
  722. {
  723. _pMap->saveValue(&_pMap->_pHead->_iDirtyTail, i->_iSetPrev);
  724. }
  725. //修改回写数据链表/
  726. if(_pMap->_pHead->_iSyncTail == _iHead)
  727. {
  728. _pMap->saveValue(&_pMap->_pHead->_iSyncTail, i->_iSetPrev);
  729. }
  730. //修改备份数据链表/
  731. if(_pMap->_pHead->_iBackupTail == _iHead)
  732. {
  733. _pMap->saveValue(&_pMap->_pHead->_iBackupTail, i->_iGetPrev);
  734. }
  735. 修改Set链表的数据//
  736. {
  737. bool bHead = (_pMap->_pHead->_iSetHead == _iHead);
  738. bool bTail = (_pMap->_pHead->_iSetTail == _iHead);
  739. if(!bHead)
  740. {
  741. if(bTail)
  742. {
  743. assert(i->_iSetNext == 0);
  744. //是尾部, 尾部指针指向上一个元素
  745. _pMap->saveValue(&_pMap->_pHead->_iSetTail, i->_iSetPrev);
  746. _pMap->saveValue(&getBlockHead(i->_iSetPrev)->_iSetNext, (uint32_t)0);
  747. }
  748. else
  749. {
  750. //不是头部也不是尾部
  751. assert(i->_iSetNext != 0);
  752. _pMap->saveValue(&getBlockHead(i->_iSetPrev)->_iSetNext, i->_iSetNext);
  753. _pMap->saveValue(&getBlockHead(i->_iSetNext)->_iSetPrev, i->_iSetPrev);
  754. }
  755. }
  756. else
  757. {
  758. if(bTail)
  759. {
  760. assert(i->_iSetNext == 0);
  761. assert(i->_iSetPrev == 0);
  762. //头部也是尾部, 指针都设置为0
  763. _pMap->saveValue(&_pMap->_pHead->_iSetHead, (uint32_t)0);
  764. _pMap->saveValue(&_pMap->_pHead->_iSetTail, (uint32_t)0);
  765. }
  766. else
  767. {
  768. //头部不是尾部, 头部指针指向下一个元素
  769. assert(i->_iSetNext != 0);
  770. _pMap->saveValue(&_pMap->_pHead->_iSetHead, i->_iSetNext);
  771. //下一个元素上指针为0
  772. _pMap->saveValue(&getBlockHead(i->_iSetNext)->_iSetPrev, (uint32_t)0);
  773. }
  774. }
  775. }
  776. 修改Get链表的数据//
  777. //
  778. {
  779. bool bHead = (_pMap->_pHead->_iGetHead == _iHead);
  780. bool bTail = (_pMap->_pHead->_iGetTail == _iHead);
  781. if(!bHead)
  782. {
  783. if(bTail)
  784. {
  785. assert(i->_iGetNext == 0);
  786. //是尾部, 尾部指针指向上一个元素
  787. _pMap->saveValue(&_pMap->_pHead->_iGetTail, i->_iGetPrev);
  788. _pMap->saveValue(&getBlockHead(getBlockHead()->_iGetPrev)->_iGetNext, (uint32_t)0);
  789. }
  790. else
  791. {
  792. //不是头部也不是尾部
  793. assert(getBlockHead()->_iGetNext != 0);
  794. _pMap->saveValue(&getBlockHead(i->_iGetPrev)->_iGetNext, i->_iGetNext);
  795. _pMap->saveValue(&getBlockHead(i->_iGetNext)->_iGetPrev, i->_iGetPrev);
  796. }
  797. }
  798. else
  799. {
  800. if(bTail)
  801. {
  802. assert(i->_iGetNext == 0);
  803. assert(i->_iGetPrev == 0);
  804. //头部也是尾部, 指针都设置为0
  805. _pMap->saveValue(&_pMap->_pHead->_iGetHead, (uint32_t)0);
  806. _pMap->saveValue(&_pMap->_pHead->_iGetTail, (uint32_t)0);
  807. }
  808. else
  809. {
  810. //头部不是尾部, 头部指针指向下一个元素
  811. assert(i->_iGetNext != 0);
  812. _pMap->saveValue(&_pMap->_pHead->_iGetHead, i->_iGetNext);
  813. //下一个元素上指针为0
  814. _pMap->saveValue(&getBlockHead(i->_iGetNext)->_iGetPrev, (uint32_t)0);
  815. }
  816. }
  817. }
  818. //脏数据///
  819. //
  820. if (isDirty())
  821. {
  822. _pMap->delDirtyCount();
  823. }
  824. if(isOnlyKey())
  825. {
  826. _pMap->delOnlyKeyCount();
  827. }
  828. //元素个数减少
  829. _pMap->delElementCount();
  830. //保留头部指针的现场
  831. _pMap->saveValue(&i->_iSize, 0, false);
  832. _pMap->saveValue(&i->_iColor, TC_RBTree::RED, false);
  833. _pMap->saveValue(&i->_iParent, 0, false);
  834. _pMap->saveValue(&i->_iLeftChild, 0, false);
  835. _pMap->saveValue(&i->_iRightChild, 0, false);
  836. _pMap->saveValue(&i->_iSetNext, 0, false);
  837. _pMap->saveValue(&i->_iSetPrev, 0, false);
  838. _pMap->saveValue(&i->_iGetNext, 0, false);
  839. _pMap->saveValue(&i->_iGetPrev, 0, false);
  840. _pMap->saveValue(&i->_iSyncTime, 0, false);
  841. _pMap->saveValue(&i->_bNextChunk, 0, false);
  842. _pMap->saveValue(&i->_iDataLen, 0, false);
  843. _pMap->saveValue(&i->_bDirty, 0, false);
  844. _pMap->saveValue(&i->_bOnlyKey, 0, false);
  845. //使修改生效, 在释放内存之前生效, 即使归还内存失败也只是丢掉了内存块
  846. _pMap->doUpdate();
  847. //归还到内存中
  848. deallocate();
  849. }
  850. void TC_RBTree::Block::refreshSetList()
  851. {
  852. tagBlockHead *i = getBlockHead();
  853. assert(_pMap->_pHead->_iSetHead != 0);
  854. assert(_pMap->_pHead->_iSetTail != 0);
  855. //修改同步链表
  856. if(_pMap->_pHead->_iSyncTail == _iHead && _pMap->_pHead->_iSetHead != _iHead)
  857. {
  858. _pMap->saveValue(&_pMap->_pHead->_iSyncTail, i->_iSetPrev);
  859. }
  860. //修改脏数据尾部链表指针, 不仅一个元素
  861. if(_pMap->_pHead->_iDirtyTail == _iHead && _pMap->_pHead->_iSetHead != _iHead)
  862. {
  863. //脏数据尾部位置前移
  864. _pMap->saveValue(&_pMap->_pHead->_iDirtyTail, i->_iSetPrev);
  865. }
  866. else if (_pMap->_pHead->_iDirtyTail == 0)
  867. {
  868. //原来没有脏数据
  869. _pMap->saveValue(&_pMap->_pHead->_iDirtyTail, _iHead);
  870. }
  871. //是头部数据或者set新数据时走到这个分支
  872. if(_pMap->_pHead->_iSetHead == _iHead)
  873. {
  874. //刷新Get链
  875. refreshGetList();
  876. return;
  877. }
  878. uint32_t iPrev = i->_iSetPrev;
  879. uint32_t iNext = i->_iSetNext;
  880. assert(iPrev != 0);
  881. //挂在链表头部
  882. _pMap->saveValue(&i->_iSetNext, _pMap->_pHead->_iSetHead);
  883. _pMap->saveValue(&getBlockHead(_pMap->_pHead->_iSetHead)->_iSetPrev, _iHead);
  884. _pMap->saveValue(&_pMap->_pHead->_iSetHead, _iHead);
  885. _pMap->saveValue(&i->_iSetPrev, (uint32_t)0);
  886. //上一个元素的Next指针指向下一个元素
  887. _pMap->saveValue(&getBlockHead(iPrev)->_iSetNext, iNext);
  888. //下一个元素的Prev指向上一个元素
  889. if (iNext != 0)
  890. {
  891. _pMap->saveValue(&getBlockHead(iNext)->_iSetPrev, iPrev);
  892. }
  893. else
  894. {
  895. //改变尾部指针
  896. assert(_pMap->_pHead->_iSetTail == _iHead);
  897. _pMap->saveValue(&_pMap->_pHead->_iSetTail, iPrev);
  898. }
  899. //刷新Get链
  900. refreshGetList();
  901. }
  902. void TC_RBTree::Block::refreshGetList()
  903. {
  904. assert(_pMap->_pHead->_iGetHead != 0);
  905. assert(_pMap->_pHead->_iGetTail != 0);
  906. //是头部数据
  907. if(_pMap->_pHead->_iGetHead == _iHead)
  908. {
  909. return;
  910. }
  911. tagBlockHead *i = getBlockHead();
  912. uint32_t iPrev = i->_iGetPrev;
  913. uint32_t iNext = i->_iGetNext;
  914. assert(iPrev != 0);
  915. //是正在备份的数据
  916. if(_pMap->_pHead->_iBackupTail == _iHead)
  917. {
  918. _pMap->saveValue(&_pMap->_pHead->_iBackupTail, iPrev);
  919. }
  920. //挂在链表头部
  921. _pMap->saveValue(&i->_iGetNext, _pMap->_pHead->_iGetHead);
  922. _pMap->saveValue(&getBlockHead(_pMap->_pHead->_iGetHead)->_iGetPrev, _iHead);
  923. _pMap->saveValue(&_pMap->_pHead->_iGetHead, _iHead);
  924. _pMap->saveValue(&i->_iGetPrev, (uint32_t)0);
  925. //上一个元素的Next指针指向下一个元素
  926. _pMap->saveValue(&getBlockHead(iPrev)->_iGetNext, iNext);
  927. //下一个元素的Prev指向上一个元素
  928. if (iNext != 0)
  929. {
  930. _pMap->saveValue(&getBlockHead(iNext)->_iGetPrev, iPrev);
  931. }
  932. else
  933. {
  934. //改变尾部指针
  935. assert(_pMap->_pHead->_iGetTail == _iHead);
  936. _pMap->saveValue(&_pMap->_pHead->_iGetTail, iPrev);
  937. }
  938. }
  939. void TC_RBTree::Block::deallocate(uint32_t iChunk)
  940. {
  941. tagChunkHead *pChunk = getChunkHead(iChunk);
  942. vector<uint32_t> v;
  943. v.push_back(iChunk);
  944. //获取所有后续的chunk地址
  945. while(true)
  946. {
  947. if(pChunk->_bNextChunk)
  948. {
  949. v.push_back(pChunk->_iNextChunk);
  950. pChunk = getChunkHead(pChunk->_iNextChunk);
  951. }
  952. else
  953. {
  954. break;
  955. }
  956. }
  957. //空间全部释放掉
  958. _pMap->_pDataAllocator->deallocateMemBlock(v);
  959. }
  960. int TC_RBTree::Block::allocate(uint32_t iDataLen, vector<TC_RBTree::BlockData> &vtData)
  961. {
  962. uint32_t fn = 0;
  963. tagBlockHead *i = getBlockHead();
  964. //一个块的真正的数据容量
  965. fn = getBlockHead()->_iSize - sizeof(tagBlockHead);
  966. if(fn >= iDataLen)
  967. {
  968. //一个block就可以了, 后续的chunk都要释放掉
  969. if(getBlockHead()->_bNextChunk)
  970. {
  971. uint32_t iNextChunk = i->_iNextChunk;
  972. //先修改自己的区块
  973. _pMap->saveValue(&i->_bNextChunk, false);
  974. _pMap->saveValue(&i->_iDataLen, (uint32_t)0);
  975. //修改成功后再释放区块, 从而保证, 不会core的时候导致整个内存块不可用
  976. deallocate(iNextChunk);
  977. }
  978. return TC_RBTree::RT_OK;
  979. }
  980. //计算还需要多少长度
  981. fn = iDataLen - fn;
  982. if(i->_bNextChunk)
  983. {
  984. tagChunkHead *pChunk = getChunkHead(i->_iNextChunk);
  985. while(true)
  986. {
  987. if(fn <= (pChunk->_iSize - sizeof(tagChunkHead)))
  988. {
  989. //已经不需要chunk了, 释放多余的chunk
  990. if(pChunk->_bNextChunk)
  991. {
  992. uint32_t iNextChunk = pChunk->_iNextChunk;
  993. _pMap->saveValue(&pChunk->_bNextChunk, false);
  994. //一旦异常core, 最坏的情况就是少了可用的区块, 但是整个内存结构还是可用的
  995. deallocate(iNextChunk);
  996. }
  997. return TC_RBTree::RT_OK ;
  998. }
  999. //计算, 还需要多少长度
  1000. fn -= pChunk->_iSize - sizeof(tagChunkHead);
  1001. if(pChunk->_bNextChunk)
  1002. {
  1003. pChunk = getChunkHead(pChunk->_iNextChunk);
  1004. }
  1005. else
  1006. {
  1007. //没有后续chunk了, 需要新分配fn的空间
  1008. vector<uint32_t> chunks;
  1009. int ret = allocateChunk(fn, chunks, vtData);
  1010. if(ret != TC_RBTree::RT_OK)
  1011. {
  1012. //没有内存可以分配
  1013. return ret;
  1014. }
  1015. _pMap->saveValue(&pChunk->_bNextChunk, true);
  1016. _pMap->saveValue(&pChunk->_iNextChunk, chunks[0]);
  1017. //chunk指向分配的第一个chunk
  1018. pChunk = getChunkHead(chunks[0]);
  1019. //修改第一个chunk的属性, 保证异常core的时候, chunk链表不会有问题
  1020. _pMap->saveValue(&pChunk->_bNextChunk, false);
  1021. _pMap->saveValue(&pChunk->_iDataLen, (uint32_t)0);
  1022. //连接每个chunk
  1023. return joinChunk(pChunk, chunks);
  1024. }
  1025. }
  1026. }
  1027. else
  1028. {
  1029. //没有后续chunk了, 需要新分配fn空间
  1030. vector<uint32_t> chunks;
  1031. int ret = allocateChunk(fn, chunks, vtData);
  1032. if(ret != TC_RBTree::RT_OK)
  1033. {
  1034. //没有内存可以分配
  1035. return ret;
  1036. }
  1037. _pMap->saveValue(&i->_bNextChunk, true);
  1038. _pMap->saveValue(&i->_iNextChunk, chunks[0]);
  1039. //chunk指向分配的第一个chunk
  1040. tagChunkHead *pChunk = getChunkHead(chunks[0]);
  1041. //修改第一个chunk的属性, 保证异常core的时候, chunk链表不会有问题
  1042. _pMap->saveValue(&pChunk->_bNextChunk, false);
  1043. _pMap->saveValue(&pChunk->_iDataLen, (uint32_t)0);
  1044. //连接每个chunk
  1045. return joinChunk(pChunk, chunks);
  1046. }
  1047. return TC_RBTree::RT_OK;
  1048. }
  1049. int TC_RBTree::Block::joinChunk(tagChunkHead *pChunk, const vector<uint32_t> chunks)
  1050. {
  1051. for (size_t i = 0; i < chunks.size(); ++i)
  1052. {
  1053. if (i == chunks.size() - 1)
  1054. {
  1055. _pMap->saveValue(&pChunk->_bNextChunk, false);
  1056. return TC_RBTree::RT_OK;
  1057. }
  1058. else
  1059. {
  1060. _pMap->saveValue(&pChunk->_bNextChunk, true);
  1061. _pMap->saveValue(&pChunk->_iNextChunk, chunks[i+1]);
  1062. pChunk = getChunkHead(chunks[i+1]);
  1063. _pMap->saveValue(&pChunk->_bNextChunk, false);
  1064. _pMap->saveValue(&pChunk->_iDataLen, (uint32_t)0);
  1065. }
  1066. }
  1067. return TC_RBTree::RT_OK;
  1068. }
  1069. int TC_RBTree::Block::allocateChunk(uint32_t fn, vector<uint32_t> &chunks, vector<TC_RBTree::BlockData> &vtData)
  1070. {
  1071. assert(fn > 0);
  1072. while(true)
  1073. {
  1074. uint32_t iAllocSize = fn;
  1075. //分配空间
  1076. uint32_t t = _pMap->_pDataAllocator->allocateChunk(_iHead, iAllocSize, vtData);
  1077. if (t == 0)
  1078. {
  1079. //没有内存可以分配了, 先把分配的归还
  1080. _pMap->_pDataAllocator->deallocateMemBlock(chunks);
  1081. chunks.clear();
  1082. return TC_RBTree::RT_NO_MEMORY;
  1083. }
  1084. //设置分配的数据块的大小
  1085. getChunkHead(t)->_iSize = iAllocSize;
  1086. chunks.push_back(t);
  1087. //分配够了
  1088. if(fn <= (iAllocSize - sizeof(tagChunkHead)))
  1089. {
  1090. break;
  1091. }
  1092. //还需要多少空间
  1093. fn -= iAllocSize - sizeof(tagChunkHead);
  1094. }
  1095. return TC_RBTree::RT_OK;
  1096. }
  1097. uint32_t TC_RBTree::Block::getDataLen()
  1098. {
  1099. tagBlockHead *i = getBlockHead();
  1100. uint32_t n = 0;
  1101. if(!i->_bNextChunk)
  1102. {
  1103. n += i->_iDataLen;
  1104. return n;
  1105. }
  1106. //当前块的大小
  1107. n += i->_iSize - sizeof(tagBlockHead);
  1108. tagChunkHead *pChunk = getChunkHead(i->_iNextChunk);
  1109. while (true)
  1110. {
  1111. if(pChunk->_bNextChunk)
  1112. {
  1113. //当前块的大小
  1114. n += pChunk->_iSize - sizeof(tagChunkHead);
  1115. pChunk = getChunkHead(pChunk->_iNextChunk);
  1116. }
  1117. else
  1118. {
  1119. n += pChunk->_iDataLen;
  1120. break;
  1121. }
  1122. }
  1123. return n;
  1124. }
  1125. //
  1126. uint32_t TC_RBTree::BlockAllocator::allocateMemBlock(uint32_t &iAllocSize, vector<TC_RBTree::BlockData> &vtData)
  1127. {
  1128. begin:
  1129. size_t iIndex;
  1130. size_t iTmp1 = (size_t)iAllocSize;
  1131. _pChunkAllocator->allocate2(iTmp1, iTmp1, iIndex);
  1132. iAllocSize = (uint32_t)iTmp1;
  1133. if(iIndex == 0)
  1134. {
  1135. uint32_t ret = _pMap->eraseExcept(0, vtData);
  1136. if(ret == 0)
  1137. return 0; //没有空间可以释放了
  1138. goto begin;
  1139. }
  1140. //分配的新的MemBlock, 初始化一下
  1141. Block block(_pMap, (uint32_t)iIndex);
  1142. block.makeNew(iAllocSize);
  1143. _pMap->incChunkCount();
  1144. return (uint32_t)iIndex;
  1145. }
  1146. uint32_t TC_RBTree::BlockAllocator::allocateChunk(uint32_t iAddr, uint32_t &iAllocSize, vector<TC_RBTree::BlockData> &vtData)
  1147. {
  1148. begin:
  1149. size_t iIndex;
  1150. size_t iTmp1 = (size_t)iAllocSize;
  1151. _pChunkAllocator->allocate2(iTmp1, iTmp1, iIndex);
  1152. iAllocSize = (uint32_t)iTmp1;
  1153. if(iIndex == 0)
  1154. {
  1155. uint32_t ret = _pMap->eraseExcept(iAddr, vtData);
  1156. if(ret == 0)
  1157. return 0; //没有空间可以释放了
  1158. goto begin;
  1159. }
  1160. _pMap->incChunkCount();
  1161. return (uint32_t)iIndex;
  1162. }
  1163. void TC_RBTree::BlockAllocator::deallocateMemBlock(const vector<uint32_t> &v)
  1164. {
  1165. for(size_t i = 0; i < v.size(); i++)
  1166. {
  1167. _pChunkAllocator->deallocate2(v[i]);
  1168. _pMap->delChunkCount();
  1169. }
  1170. }
  1171. void TC_RBTree::BlockAllocator::deallocateMemBlock(uint32_t v)
  1172. {
  1173. _pChunkAllocator->deallocate2(v);
  1174. _pMap->delChunkCount();
  1175. }
  1176. ///
  1177. TC_RBTree::RBTreeLockItem::RBTreeLockItem(TC_RBTree *pMap, uint32_t iAddr)
  1178. : _pMap(pMap)
  1179. , _iAddr(iAddr)
  1180. {
  1181. }
  1182. TC_RBTree::RBTreeLockItem::RBTreeLockItem(const RBTreeLockItem &mcmdi)
  1183. : _pMap(mcmdi._pMap)
  1184. , _iAddr(mcmdi._iAddr)
  1185. {
  1186. }
  1187. TC_RBTree::RBTreeLockItem &TC_RBTree::RBTreeLockItem::operator=(const TC_RBTree::RBTreeLockItem &mcmdi)
  1188. {
  1189. if(this != &mcmdi)
  1190. {
  1191. _pMap = mcmdi._pMap;
  1192. _iAddr = mcmdi._iAddr;
  1193. }
  1194. return (*this);
  1195. }
  1196. bool TC_RBTree::RBTreeLockItem::operator==(const TC_RBTree::RBTreeLockItem &mcmdi)
  1197. {
  1198. return _pMap == mcmdi._pMap && _iAddr == mcmdi._iAddr;
  1199. }
  1200. bool TC_RBTree::RBTreeLockItem::operator!=(const TC_RBTree::RBTreeLockItem &mcmdi)
  1201. {
  1202. return _pMap != mcmdi._pMap || _iAddr != mcmdi._iAddr;
  1203. }
  1204. bool TC_RBTree::RBTreeLockItem::isDirty()
  1205. {
  1206. Block block(_pMap, _iAddr);
  1207. return block.isDirty();
  1208. }
  1209. bool TC_RBTree::RBTreeLockItem::isOnlyKey()
  1210. {
  1211. Block block(_pMap, _iAddr);
  1212. return block.isOnlyKey();
  1213. }
  1214. time_t TC_RBTree::RBTreeLockItem::getSyncTime()
  1215. {
  1216. Block block(_pMap, _iAddr);
  1217. return block.getSyncTime();
  1218. }
  1219. int TC_RBTree::RBTreeLockItem::get(string& k, string& v)
  1220. {
  1221. string s;
  1222. Block block(_pMap, _iAddr);
  1223. int ret = block.get(s);
  1224. if(ret != TC_RBTree::RT_OK)
  1225. {
  1226. return ret;
  1227. }
  1228. try
  1229. {
  1230. TC_PackOut po(s.c_str(), s.length());
  1231. po >> k;
  1232. if(!block.isOnlyKey())
  1233. {
  1234. po >> v;
  1235. }
  1236. else
  1237. {
  1238. v = "";
  1239. return TC_RBTree::RT_ONLY_KEY;
  1240. }
  1241. }
  1242. catch ( exception &ex )
  1243. {
  1244. assert(false);
  1245. return TC_RBTree::RT_EXCEPTION_ERR;
  1246. }
  1247. return TC_RBTree::RT_OK;
  1248. }
  1249. int TC_RBTree::RBTreeLockItem::get(string& k)
  1250. {
  1251. string s;
  1252. Block block(_pMap, _iAddr);
  1253. int ret = block.get(s);
  1254. if(ret != TC_RBTree::RT_OK)
  1255. {
  1256. return ret;
  1257. }
  1258. try
  1259. {
  1260. TC_PackOut po(s.c_str(), s.length());
  1261. po >> k;
  1262. }
  1263. catch ( exception &ex )
  1264. {
  1265. assert(false);
  1266. return TC_RBTree::RT_EXCEPTION_ERR;
  1267. }
  1268. return TC_RBTree::RT_OK;
  1269. }
  1270. int TC_RBTree::RBTreeLockItem::set(const string& k, const string& v, bool bNewBlock, vector<TC_RBTree::BlockData> &vtData)
  1271. {
  1272. Block block(_pMap, _iAddr);
  1273. return block.set(k, v, bNewBlock, false, vtData);
  1274. }
  1275. int TC_RBTree::RBTreeLockItem::set(const string& k, bool bNewBlock, vector<TC_RBTree::BlockData> &vtData)
  1276. {
  1277. Block block(_pMap, _iAddr);
  1278. return block.set(k, "", bNewBlock, true, vtData);
  1279. }
  1280. bool TC_RBTree::RBTreeLockItem::equal(const string &k, string &k1, string &v, int &ret)
  1281. {
  1282. ret = get(k1, v);
  1283. if ((ret == TC_RBTree::RT_OK || ret == TC_RBTree::RT_ONLY_KEY) && k == k1)
  1284. {
  1285. return true;
  1286. }
  1287. return false;
  1288. }
  1289. bool TC_RBTree::RBTreeLockItem::equal(const string& k, string &k1, int &ret)
  1290. {
  1291. ret = get(k1);
  1292. if (ret == TC_RBTree::RT_OK && k == k1)
  1293. {
  1294. return true;
  1295. }
  1296. return false;
  1297. }
  1298. void TC_RBTree::RBTreeLockItem::nextItem(int iType)
  1299. {
  1300. Block block(_pMap, _iAddr);
  1301. if(iType == RBTreeLockIterator::IT_SET)
  1302. {
  1303. _iAddr = block.getBlockHead()->_iSetNext;
  1304. }
  1305. else if(iType == RBTreeLockIterator::IT_GET)
  1306. {
  1307. _iAddr = block.getBlockHead()->_iGetNext;
  1308. }
  1309. else if(iType == RBTreeLockIterator::IT_RBTREE)
  1310. {
  1311. if(block.hasRight())
  1312. {
  1313. //移到右子树的最左节点
  1314. block.moveToRight();
  1315. while(block.hasLeft())
  1316. block.moveToLeft();
  1317. }
  1318. else
  1319. {
  1320. while(block.hasParent() && block.getHead() == block.getBlockHead(block.getBlockHead()->_iParent)->_iRightChild)
  1321. block.moveToParent();
  1322. block.moveToParent();
  1323. }
  1324. _iAddr = block.getHead();
  1325. }
  1326. }
  1327. void TC_RBTree::RBTreeLockItem::prevItem(int iType)
  1328. {
  1329. Block block(_pMap, _iAddr);
  1330. if(iType == RBTreeLockIterator::IT_SET)
  1331. {
  1332. _iAddr = block.getBlockHead()->_iSetPrev;
  1333. }
  1334. else if(iType == RBTreeLockIterator::IT_GET)
  1335. {
  1336. _iAddr = block.getBlockHead()->_iGetPrev;
  1337. }
  1338. else if(iType == RBTreeLockIterator::IT_RBTREE)
  1339. {
  1340. if(block.hasLeft())
  1341. {
  1342. //移到右子树的最左节:点
  1343. block.moveToLeft();
  1344. while(block.hasRight())
  1345. block.moveToRight();
  1346. }
  1347. else
  1348. {
  1349. while(block.hasParent() && block.getHead() == block.getBlockHead(block.getBlockHead()->_iParent)->_iLeftChild)
  1350. block.moveToParent();
  1351. block.moveToParent();
  1352. }
  1353. _iAddr = block.getHead();
  1354. }
  1355. }
  1356. ///
  1357. TC_RBTree::RBTreeLockIterator::RBTreeLockIterator()
  1358. : _pMap(NULL), _iItem(NULL, 0),_iType(IT_RBTREE),_iOrder(IT_NEXT)
  1359. {
  1360. }
  1361. TC_RBTree::RBTreeLockIterator::RBTreeLockIterator(TC_RBTree *pMap, uint32_t iAddr, int iType, int iOrder)
  1362. : _pMap(pMap), _iItem(_pMap, iAddr), _iType(iType), _iOrder(iOrder)
  1363. {
  1364. }
  1365. TC_RBTree::RBTreeLockIterator::RBTreeLockIterator(const RBTreeLockIterator &it)
  1366. : _pMap(it._pMap),_iItem(it._iItem), _iType(it._iType), _iOrder(it._iOrder)
  1367. {
  1368. }
  1369. TC_RBTree::RBTreeLockIterator& TC_RBTree::RBTreeLockIterator::operator=(const RBTreeLockIterator &it)
  1370. {
  1371. if(this != &it)
  1372. {
  1373. _pMap = it._pMap;
  1374. _iItem = it._iItem;
  1375. _iType = it._iType;
  1376. _iOrder = it._iOrder;
  1377. }
  1378. return (*this);
  1379. }
  1380. bool TC_RBTree::RBTreeLockIterator::operator==(const RBTreeLockIterator& mcmi)
  1381. {
  1382. if (_iItem.getAddr() != 0 || mcmi._iItem.getAddr() != 0)
  1383. {
  1384. return _iItem == mcmi._iItem
  1385. && _iType == mcmi._iType
  1386. && _iOrder == mcmi._iOrder;
  1387. }
  1388. return true;
  1389. }
  1390. bool TC_RBTree::RBTreeLockIterator::operator!=(const RBTreeLockIterator& mcmi)
  1391. {
  1392. if (_iItem.getAddr() != 0 || mcmi._iItem.getAddr() != 0 )
  1393. {
  1394. return _iItem != mcmi._iItem
  1395. || _iType != mcmi._iType
  1396. || _iOrder != mcmi._iOrder;
  1397. }
  1398. return false;
  1399. }
  1400. TC_RBTree::RBTreeLockIterator& TC_RBTree::RBTreeLockIterator::operator++()
  1401. {
  1402. if(_iOrder == IT_NEXT)
  1403. {
  1404. _iItem.nextItem(_iType);
  1405. }
  1406. else
  1407. {
  1408. _iItem.prevItem(_iType);
  1409. }
  1410. return (*this);
  1411. }
  1412. TC_RBTree::RBTreeLockIterator TC_RBTree::RBTreeLockIterator::operator++(int)
  1413. {
  1414. RBTreeLockIterator it(*this);
  1415. if(_iOrder == IT_NEXT)
  1416. {
  1417. _iItem.nextItem(_iType);
  1418. }
  1419. else
  1420. {
  1421. _iItem.prevItem(_iType);
  1422. }
  1423. return it;
  1424. }
  1425. ///
  1426. TC_RBTree::RBTreeItem::RBTreeItem(TC_RBTree *pMap, const string &key, bool bEnd)
  1427. : _pMap(pMap)
  1428. , _key(key)
  1429. , _bEnd(bEnd)
  1430. {
  1431. }
  1432. TC_RBTree::RBTreeItem::RBTreeItem(const TC_RBTree::RBTreeItem &mcmdi)
  1433. : _pMap(mcmdi._pMap)
  1434. , _key(mcmdi._key)
  1435. , _bEnd(mcmdi._bEnd)
  1436. {
  1437. }
  1438. TC_RBTree::RBTreeItem &TC_RBTree::RBTreeItem::operator=(const TC_RBTree::RBTreeItem &mcmdi)
  1439. {
  1440. if(this != &mcmdi)
  1441. {
  1442. _pMap = mcmdi._pMap;
  1443. _key = mcmdi._key;
  1444. _bEnd = mcmdi._bEnd;
  1445. }
  1446. return (*this);
  1447. }
  1448. bool TC_RBTree::RBTreeItem::operator==(const TC_RBTree::RBTreeItem &mcmdi)
  1449. {
  1450. if(_bEnd == mcmdi._bEnd)
  1451. {
  1452. return true;
  1453. }
  1454. return _pMap == mcmdi._pMap && _key == mcmdi._key;
  1455. }
  1456. bool TC_RBTree::RBTreeItem::operator!=(const TC_RBTree::RBTreeItem &mcmdi)
  1457. {
  1458. if(_bEnd == mcmdi._bEnd)
  1459. {
  1460. return false;
  1461. }
  1462. return _pMap != mcmdi._pMap || _key != mcmdi._key;
  1463. }
  1464. int TC_RBTree::RBTreeItem::get(TC_RBTree::BlockData &stData)
  1465. {
  1466. if(_bEnd)
  1467. {
  1468. return TC_RBTree::RT_NO_DATA;
  1469. }
  1470. TC_RBTree::lock_iterator it = _pMap->find(_key);
  1471. if(it != _pMap->end())
  1472. {
  1473. Block block(_pMap, it->getAddr());
  1474. return block.getBlockData(stData);
  1475. }
  1476. return TC_RBTree::RT_NO_DATA;
  1477. }
  1478. void TC_RBTree::RBTreeItem::nextItem()
  1479. {
  1480. if(_bEnd)
  1481. {
  1482. return;
  1483. }
  1484. string k;
  1485. int ret;
  1486. TC_RBTree::lock_iterator it = _pMap->upper_bound(_key);
  1487. while(it != _pMap->end())
  1488. {
  1489. ret = it->get(k);
  1490. if(ret != TC_RBTree::RT_OK && ret != TC_RBTree::RT_ONLY_KEY)
  1491. {
  1492. //数据有错
  1493. ++it;
  1494. continue;
  1495. }
  1496. _key = k;
  1497. _bEnd = false;
  1498. return;
  1499. }
  1500. _bEnd = true;
  1501. }
  1502. void TC_RBTree::RBTreeItem::prevItem()
  1503. {
  1504. if(_bEnd)
  1505. {
  1506. return;
  1507. }
  1508. string k;
  1509. int ret;
  1510. TC_RBTree::lock_iterator it1 = _pMap->lower_bound(_key);
  1511. TC_RBTree::lock_iterator it(_pMap, it1->getAddr(), TC_RBTree::lock_iterator::IT_RBTREE, TC_RBTree::lock_iterator::IT_PREV);
  1512. while(it != _pMap->end())
  1513. {
  1514. ret = it->get(k);
  1515. if(ret != TC_RBTree::RT_OK && ret != TC_RBTree::RT_ONLY_KEY)
  1516. {
  1517. //数据有错
  1518. ++it;
  1519. continue;
  1520. }
  1521. //判断就是当前的key, 取下一个元素
  1522. if(k == _key)
  1523. {
  1524. ++it;
  1525. continue;
  1526. }
  1527. _key = k;
  1528. _bEnd = false;
  1529. return;
  1530. }
  1531. _bEnd = true;
  1532. }
  1533. ///
  1534. TC_RBTree::RBTreeIterator::RBTreeIterator()
  1535. : _pMap(NULL), _iItem(NULL, "", true),_iOrder(IT_NEXT)
  1536. {
  1537. }
  1538. TC_RBTree::RBTreeIterator::RBTreeIterator(TC_RBTree *pMap, const string &key, bool bEnd, int iOrder)
  1539. : _pMap(pMap), _iItem(_pMap, key, bEnd), _iOrder(iOrder)
  1540. {
  1541. }
  1542. TC_RBTree::RBTreeIterator::RBTreeIterator(const RBTreeIterator &it)
  1543. : _pMap(it._pMap),_iItem(it._iItem), _iOrder(it._iOrder)
  1544. {
  1545. }
  1546. TC_RBTree::RBTreeIterator& TC_RBTree::RBTreeIterator::operator=(const RBTreeIterator &it)
  1547. {
  1548. if(this != &it)
  1549. {
  1550. _pMap = it._pMap;
  1551. _iItem = it._iItem;
  1552. _iOrder = it._iOrder;
  1553. }
  1554. return (*this);
  1555. }
  1556. bool TC_RBTree::RBTreeIterator::operator==(const RBTreeIterator& mcmi)
  1557. {
  1558. if (!_iItem.isEnd() || !mcmi._iItem.isEnd())
  1559. {
  1560. return _iItem == mcmi._iItem && _iOrder == mcmi._iOrder;
  1561. }
  1562. return true;
  1563. }
  1564. bool TC_RBTree::RBTreeIterator::operator!=(const RBTreeIterator& mcmi)
  1565. {
  1566. if (!_iItem.isEnd() || !mcmi._iItem.isEnd())
  1567. {
  1568. return _iItem != mcmi._iItem || _iOrder != mcmi._iOrder;
  1569. }
  1570. return false;
  1571. }
  1572. TC_RBTree::RBTreeIterator& TC_RBTree::RBTreeIterator::operator++()
  1573. {
  1574. if(_iOrder == IT_NEXT)
  1575. {
  1576. _iItem.nextItem();
  1577. }
  1578. else
  1579. {
  1580. _iItem.prevItem();
  1581. }
  1582. return (*this);
  1583. }
  1584. TC_RBTree::RBTreeIterator TC_RBTree::RBTreeIterator::operator++(int)
  1585. {
  1586. RBTreeIterator it(*this);
  1587. if(_iOrder == IT_NEXT)
  1588. {
  1589. _iItem.nextItem();
  1590. }
  1591. else
  1592. {
  1593. _iItem.prevItem();
  1594. }
  1595. return it;
  1596. }
  1597. ///
  1598. void TC_RBTree::init(void *pAddr)
  1599. {
  1600. _pHead = static_cast<tagMapHead*>(pAddr);
  1601. _pstModifyHead = static_cast<tagModifyHead*>((void*)((char*)pAddr + sizeof(tagMapHead)));
  1602. }
  1603. void TC_RBTree::initDataBlockSize(uint32_t iMinDataSize, uint32_t iMaxDataSize, float fFactor)
  1604. {
  1605. _iMinDataSize = iMinDataSize;
  1606. _iMaxDataSize = iMaxDataSize;
  1607. _fFactor = fFactor;
  1608. }
  1609. void TC_RBTree::create(void *pAddr, size_t iSize)
  1610. {
  1611. if(sizeof(tagMapHead)
  1612. + sizeof(tagModifyHead)
  1613. + sizeof(TC_MemMultiChunkAllocator::tagChunkAllocatorHead)
  1614. + 10 > iSize)
  1615. {
  1616. throw TC_RBTree_Exception("[TC_RBTree::create] mem size not enougth.");
  1617. }
  1618. if(_iMinDataSize == 0 || _iMaxDataSize == 0 || _fFactor < 1.0)
  1619. {
  1620. throw TC_RBTree_Exception("[TC_RBTree::create] init data size error:" + TC_Common::tostr(_iMinDataSize) + "|" + TC_Common::tostr(_iMaxDataSize) + "|" + TC_Common::tostr(_fFactor));
  1621. }
  1622. init(pAddr);
  1623. //block size用2个字节存储的
  1624. if(sizeof(Block::tagBlockHead) + _pHead->_iMaxDataSize > (uint16_t)(-1))
  1625. {
  1626. throw TC_RBTree_Exception("[TC_RBTree::create] init block size error, block size must be less then " + TC_Common::tostr((uint16_t)(-1) - sizeof(Block::tagBlockHead)));
  1627. }
  1628. _pHead->_cMaxVersion = MAX_VERSION;
  1629. _pHead->_cMinVersion = MIN_VERSION;
  1630. _pHead->_bReadOnly = false;
  1631. _pHead->_bAutoErase = true;
  1632. _pHead->_cEraseMode = ERASEBYGET;
  1633. _pHead->_iMemSize = iSize;
  1634. _pHead->_iMinDataSize = _iMinDataSize;
  1635. _pHead->_iMaxDataSize = _iMaxDataSize;
  1636. _pHead->_fFactor = _fFactor;
  1637. _pHead->_iElementCount = 0;
  1638. _pHead->_iEraseCount = 10;
  1639. _pHead->_iSetHead = 0;
  1640. _pHead->_iSetTail = 0;
  1641. _pHead->_iGetHead = 0;
  1642. _pHead->_iGetTail = 0;
  1643. _pHead->_iDirtyCount = 0;
  1644. _pHead->_iDirtyTail = 0;
  1645. _pHead->_iSyncTime = 60 * 10; //缺省十分钟回写一次
  1646. _pHead->_iUsedChunk = 0;
  1647. _pHead->_iGetCount = 0;
  1648. _pHead->_iHitCount = 0;
  1649. _pHead->_iBackupTail = 0;
  1650. _pHead->_iSyncTail = 0;
  1651. _pHead->_iRootAddr = 0;
  1652. void *pDataAddr = (char*)_pHead + sizeof(tagMapHead) + sizeof(tagModifyHead);
  1653. _pDataAllocator->create(pDataAddr, iSize - ((char*)pDataAddr - (char*)_pHead), sizeof(Block::tagBlockHead) + _pHead->_iMinDataSize, sizeof(Block::tagBlockHead) + _pHead->_iMaxDataSize, _pHead->_fFactor);
  1654. }
  1655. void TC_RBTree::connect(void *pAddr, size_t iSize)
  1656. {
  1657. init(pAddr);
  1658. if(_pHead->_cMaxVersion != MAX_VERSION || _pHead->_cMinVersion != MIN_VERSION)
  1659. {
  1660. ostringstream os;
  1661. os << (int)_pHead->_cMaxVersion << "." << (int)_pHead->_cMinVersion << " != " << ((int)MAX_VERSION) << "." << ((int)MIN_VERSION);
  1662. throw TC_RBTree_Exception("[TC_RBTree::connect] rbtree map version not equal:" + os.str() + " (data != code)");
  1663. }
  1664. if(_pHead->_iMemSize != iSize)
  1665. {
  1666. throw TC_RBTree_Exception("[TC_RBTree::connect] rbtree map size not equal:" + TC_Common::tostr(_pHead->_iMemSize) + "!=" + TC_Common::tostr(iSize));
  1667. }
  1668. void *pDataAddr = (char*)_pHead + sizeof(tagMapHead) + sizeof(tagModifyHead);
  1669. _pDataAllocator->connect(pDataAddr);
  1670. _iMinDataSize = _pHead->_iMinDataSize;
  1671. _iMaxDataSize = _pHead->_iMaxDataSize;
  1672. _fFactor = _pHead->_fFactor;
  1673. }
  1674. int TC_RBTree::append(void *pAddr, size_t iSize)
  1675. {
  1676. if(iSize <= _pHead->_iMemSize)
  1677. {
  1678. return -1;
  1679. }
  1680. init(pAddr);
  1681. if(_pHead->_cMaxVersion != MAX_VERSION || _pHead->_cMinVersion != MIN_VERSION)
  1682. {
  1683. ostringstream os;
  1684. os << (int)_pHead->_cMaxVersion << "." << (int)_pHead->_cMinVersion << " != " << ((int)MAX_VERSION) << "." << ((int)MIN_VERSION);
  1685. throw TC_RBTree_Exception("[TC_RBTree::append] rbtree map version not equal:" + os.str() + " (data != code)");
  1686. }
  1687. _pHead->_iMemSize = iSize;
  1688. void *pDataAddr = (char*)_pHead + sizeof(tagMapHead) + sizeof(tagModifyHead);
  1689. _pDataAllocator->append(pDataAddr, iSize - ((size_t)pDataAddr - (size_t)pAddr));
  1690. _iMinDataSize = _pHead->_iMinDataSize;
  1691. _iMaxDataSize = _pHead->_iMaxDataSize;
  1692. _fFactor = _pHead->_fFactor;
  1693. return 0;
  1694. }
  1695. void TC_RBTree::clear()
  1696. {
  1697. FailureRecover check(this);
  1698. assert(_pHead);
  1699. _pHead->_iElementCount = 0;
  1700. _pHead->_iSetHead = 0;
  1701. _pHead->_iSetTail = 0;
  1702. _pHead->_iGetHead = 0;
  1703. _pHead->_iGetTail = 0;
  1704. _pHead->_iDirtyCount = 0;
  1705. _pHead->_iDirtyTail = 0;
  1706. _pHead->_iUsedChunk = 0;
  1707. _pHead->_iGetCount = 0;
  1708. _pHead->_iHitCount = 0;
  1709. _pHead->_iBackupTail = 0;
  1710. _pHead->_iSyncTail = 0;
  1711. _pHead->_iRootAddr = 0;
  1712. _pDataAllocator->rebuild();
  1713. }
  1714. int TC_RBTree::dump2file(const string &sFile)
  1715. {
  1716. FILE *fp = fopen(sFile.c_str(), "wb");
  1717. if(fp == NULL)
  1718. {
  1719. return RT_DUMP_FILE_ERR;
  1720. }
  1721. size_t ret = fwrite((void*)_pHead, 1, _pHead->_iMemSize, fp);
  1722. fclose(fp);
  1723. if(ret == _pHead->_iMemSize)
  1724. {
  1725. return RT_OK;
  1726. }
  1727. return RT_DUMP_FILE_ERR;
  1728. }
  1729. int TC_RBTree::load5file(const string &sFile)
  1730. {
  1731. FILE *fp = fopen(sFile.c_str(), "rb");
  1732. if(fp == NULL)
  1733. {
  1734. return RT_LOAL_FILE_ERR;
  1735. }
  1736. fseek(fp, 0L, SEEK_END);
  1737. size_t fs = ftell(fp);
  1738. if(fs != _pHead->_iMemSize)
  1739. {
  1740. fclose(fp);
  1741. return RT_LOAL_FILE_ERR;
  1742. }
  1743. fseek(fp, 0L, SEEK_SET);
  1744. size_t iSize = 1024*1024*10;
  1745. size_t iLen = 0;
  1746. char *pBuffer = new char[iSize];
  1747. bool bEof = false;
  1748. while(true)
  1749. {
  1750. int ret = fread(pBuffer, 1, iSize, fp);
  1751. if(ret != (int)iSize)
  1752. {
  1753. if(feof(fp))
  1754. {
  1755. bEof = true;
  1756. }
  1757. else
  1758. {
  1759. fclose(fp);
  1760. delete[] pBuffer;
  1761. return RT_LOAL_FILE_ERR;
  1762. }
  1763. }
  1764. //检查版本
  1765. if(iLen == 0)
  1766. {
  1767. if(pBuffer[0] != MAX_VERSION || pBuffer[1] != MIN_VERSION)
  1768. {
  1769. fclose(fp);
  1770. delete[] pBuffer;
  1771. return RT_VERSION_MISMATCH_ERR;
  1772. }
  1773. }
  1774. memcpy((char*)_pHead + iLen, pBuffer, ret);
  1775. iLen += ret;
  1776. if(bEof)
  1777. {
  1778. break;
  1779. }
  1780. }
  1781. fclose(fp);
  1782. delete[] pBuffer;
  1783. if(iLen == _pHead->_iMemSize)
  1784. {
  1785. return RT_OK;
  1786. }
  1787. return RT_LOAL_FILE_ERR;
  1788. }
  1789. int TC_RBTree::checkDirty(const string &k)
  1790. {
  1791. FailureRecover check(this);
  1792. incGetCount();
  1793. int ret = TC_RBTree::RT_OK;
  1794. lock_iterator it= find(_pHead->_iRootAddr, k, ret);
  1795. if(ret != TC_RBTree::RT_OK)
  1796. {
  1797. return ret;
  1798. }
  1799. //没有数据
  1800. if(it == end())
  1801. {
  1802. return TC_RBTree::RT_NO_DATA;
  1803. }
  1804. //只有Key
  1805. if(it->isOnlyKey())
  1806. {
  1807. return TC_RBTree::RT_ONLY_KEY;
  1808. }
  1809. Block block(this, it->getAddr());
  1810. if (block.isDirty())
  1811. {
  1812. return TC_RBTree::RT_DIRTY_DATA;
  1813. }
  1814. return TC_RBTree::RT_OK;
  1815. }
  1816. int TC_RBTree::setDirty(const string& k)
  1817. {
  1818. FailureRecover check(this);
  1819. if(_pHead->_bReadOnly) return RT_READONLY;
  1820. incGetCount();
  1821. int ret = TC_RBTree::RT_OK;
  1822. lock_iterator it= find(_pHead->_iRootAddr, k, ret);
  1823. if(ret != TC_RBTree::RT_OK)
  1824. {
  1825. return ret;
  1826. }
  1827. //没有数据或只有Key
  1828. if(it == end())
  1829. {
  1830. return TC_RBTree::RT_NO_DATA;
  1831. }
  1832. //只有Key
  1833. if(it->isOnlyKey())
  1834. {
  1835. return TC_RBTree::RT_ONLY_KEY;
  1836. }
  1837. Block block(this, it->getAddr());
  1838. block.setDirty(true);
  1839. block.refreshSetList();
  1840. return TC_RBTree::RT_OK;
  1841. }
  1842. int TC_RBTree::setClean(const string& k)
  1843. {
  1844. FailureRecover check(this);
  1845. if(_pHead->_bReadOnly) return RT_READONLY;
  1846. incGetCount();
  1847. int ret = TC_RBTree::RT_OK;
  1848. lock_iterator it= find(_pHead->_iRootAddr, k, ret);
  1849. if(ret != TC_RBTree::RT_OK)
  1850. {
  1851. return ret;
  1852. }
  1853. //没有数据或只有Key
  1854. if(it == end())
  1855. {
  1856. return TC_RBTree::RT_NO_DATA;
  1857. }
  1858. //只有Key
  1859. if(it->isOnlyKey())
  1860. {
  1861. return TC_RBTree::RT_ONLY_KEY;
  1862. }
  1863. Block block(this, it->getAddr());
  1864. block.setDirty(false);
  1865. block.refreshSetList();
  1866. return TC_RBTree::RT_OK;
  1867. }
  1868. int TC_RBTree::get(const string& k, string &v, time_t &iSyncTime)
  1869. {
  1870. FailureRecover check(this);
  1871. incGetCount();
  1872. int ret = TC_RBTree::RT_OK;
  1873. lock_iterator it = find(_pHead->_iRootAddr, k, v, ret);
  1874. if(ret != TC_RBTree::RT_OK && ret != TC_RBTree::RT_ONLY_KEY)
  1875. {
  1876. return ret;
  1877. }
  1878. //没有数据
  1879. if(it == end())
  1880. {
  1881. return TC_RBTree::RT_NO_DATA;
  1882. }
  1883. //只有Key
  1884. if(it->isOnlyKey())
  1885. {
  1886. return TC_RBTree::RT_ONLY_KEY;
  1887. }
  1888. Block block(this, it->getAddr());
  1889. iSyncTime = block.getSyncTime();
  1890. //如果只读, 则不刷新get链表
  1891. if(!_pHead->_bReadOnly)
  1892. {
  1893. block.refreshGetList();
  1894. }
  1895. return TC_RBTree::RT_OK;
  1896. }
  1897. int TC_RBTree::get(const string& k, string &v)
  1898. {
  1899. time_t iSyncTime;
  1900. return get(k, v, iSyncTime);
  1901. }
  1902. int TC_RBTree::set(const string& k, const string& v, bool bDirty, vector<BlockData> &vtData)
  1903. {
  1904. FailureRecover check(this);
  1905. incGetCount();
  1906. if(_pHead->_bReadOnly) return RT_READONLY;
  1907. int ret = TC_RBTree::RT_OK;
  1908. lock_iterator it = find(_pHead->_iRootAddr, k, ret);
  1909. bool bNewBlock = false;
  1910. if(ret != TC_RBTree::RT_OK)
  1911. {
  1912. return ret;
  1913. }
  1914. if(it == end())
  1915. {
  1916. TC_PackIn pi;
  1917. pi << k;
  1918. pi << v;
  1919. uint32_t iAllocSize = sizeof(Block::tagBlockHead) + pi.length();
  1920. //先分配空间, 并获得淘汰的数据
  1921. uint32_t iAddr = _pDataAllocator->allocateMemBlock(iAllocSize, vtData);
  1922. if(iAddr == 0)
  1923. {
  1924. return TC_RBTree::RT_NO_MEMORY;
  1925. }
  1926. it = RBTreeLockIterator(this, iAddr, RBTreeLockIterator::IT_GET, RBTreeLockIterator::IT_NEXT);
  1927. bNewBlock = true;
  1928. }
  1929. ret = it->set(k, v, bNewBlock, vtData);
  1930. if(ret != TC_RBTree::RT_OK)
  1931. {
  1932. //新记录, 写失败了, 要删除该块
  1933. if(bNewBlock)
  1934. {
  1935. Block block(this, it->getAddr());
  1936. block.erase();
  1937. }
  1938. return ret;
  1939. }
  1940. Block block(this, it->getAddr());
  1941. if(bNewBlock)
  1942. {
  1943. block.setSyncTime(time(NULL));
  1944. }
  1945. block.setDirty(bDirty);
  1946. block.refreshSetList();
  1947. return TC_RBTree::RT_OK;
  1948. }
  1949. int TC_RBTree::set(const string& k, vector<BlockData> &vtData)
  1950. {
  1951. FailureRecover check(this);
  1952. incGetCount();
  1953. if(_pHead->_bReadOnly) return RT_READONLY;
  1954. int ret = TC_RBTree::RT_OK;
  1955. lock_iterator it = find(_pHead->_iRootAddr, k, ret);
  1956. bool bNewBlock = false;
  1957. if(ret != TC_RBTree::RT_OK)
  1958. {
  1959. return ret;
  1960. }
  1961. if(it == end())
  1962. {
  1963. TC_PackIn pi;
  1964. pi << k;
  1965. uint32_t iAllocSize = sizeof(Block::tagBlockHead) + pi.length();
  1966. //先分配空间, 并获得淘汰的数据
  1967. uint32_t iAddr = _pDataAllocator->allocateMemBlock(iAllocSize, vtData);
  1968. if(iAddr == 0)
  1969. {
  1970. return TC_RBTree::RT_NO_MEMORY;
  1971. }
  1972. it = RBTreeLockIterator(this, iAddr, RBTreeLockIterator::IT_GET, RBTreeLockIterator::IT_NEXT);
  1973. bNewBlock = true;
  1974. }
  1975. ret = it->set(k, bNewBlock, vtData);
  1976. if(ret != TC_RBTree::RT_OK)
  1977. {
  1978. //新记录, 写失败了, 要删除该块
  1979. if(bNewBlock)
  1980. {
  1981. Block block(this, it->getAddr());
  1982. block.erase();
  1983. }
  1984. return ret;
  1985. }
  1986. Block block(this, it->getAddr());
  1987. if(bNewBlock)
  1988. {
  1989. block.setSyncTime(time(NULL));
  1990. }
  1991. block.refreshSetList();
  1992. return TC_RBTree::RT_OK;
  1993. }
  1994. int TC_RBTree::del(const string& k, BlockData &data)
  1995. {
  1996. FailureRecover check(this);
  1997. incGetCount();
  1998. if(_pHead->_bReadOnly) return RT_READONLY;
  1999. int ret = TC_RBTree::RT_OK;
  2000. data._key = k;
  2001. lock_iterator it = find(_pHead->_iRootAddr, k, data._value, ret);
  2002. if(ret != TC_RBTree::RT_OK && ret != TC_RBTree::RT_ONLY_KEY)
  2003. {
  2004. return ret;
  2005. }
  2006. if(it == end())
  2007. {
  2008. return TC_RBTree::RT_NO_DATA;
  2009. }
  2010. Block block(this, it->getAddr());
  2011. ret = block.getBlockData(data);
  2012. block.erase();
  2013. return ret;
  2014. }
  2015. int TC_RBTree::erase(int radio, BlockData &data, bool bCheckDirty)
  2016. {
  2017. FailureRecover check(this);
  2018. if(_pHead->_bReadOnly) return RT_READONLY;
  2019. if(radio <= 0) radio = 1;
  2020. if(radio >= 100) radio = 100;
  2021. uint32_t iAddr = _pHead->_iGetTail;
  2022. //到链表头部
  2023. if(iAddr == 0)
  2024. {
  2025. return RT_OK;
  2026. }
  2027. //删除到指定比率了
  2028. if((_pHead->_iUsedChunk * 100. / allBlockChunkCount()) < radio)
  2029. {
  2030. return RT_OK;
  2031. }
  2032. Block block(this, iAddr);
  2033. if(bCheckDirty)
  2034. {
  2035. if(block.isDirty())
  2036. {
  2037. if(_pHead->_iDirtyTail == 0)
  2038. {
  2039. _pHead->_iDirtyTail = iAddr;
  2040. }
  2041. return RT_OK;
  2042. }
  2043. }
  2044. int ret = block.getBlockData(data);
  2045. block.erase();
  2046. if(ret == TC_RBTree::RT_OK)
  2047. {
  2048. return TC_RBTree::RT_ERASE_OK;
  2049. }
  2050. return ret;
  2051. }
  2052. void TC_RBTree::sync()
  2053. {
  2054. FailureRecover check(this);
  2055. _pHead->_iSyncTail = _pHead->_iDirtyTail;
  2056. }
  2057. int TC_RBTree::sync(time_t iNowTime, BlockData &data)
  2058. {
  2059. FailureRecover check(this);
  2060. uint32_t iAddr = _pHead->_iSyncTail;
  2061. //到链表头部了, 返回RT_OK
  2062. if(iAddr == 0)
  2063. {
  2064. return RT_OK;
  2065. }
  2066. Block block(this, iAddr);
  2067. _pHead->_iSyncTail = block.getBlockHead()->_iSetPrev;
  2068. //当前数据是干净数据
  2069. if( !block.isDirty())
  2070. {
  2071. if(_pHead->_iDirtyTail == iAddr)
  2072. {
  2073. _pHead->_iDirtyTail = block.getBlockHead()->_iSetPrev;
  2074. }
  2075. return RT_NONEED_SYNC;
  2076. }
  2077. int ret = block.getBlockData(data);
  2078. if(ret != TC_RBTree::RT_OK)
  2079. {
  2080. //没有获取完整的记录
  2081. if(_pHead->_iDirtyTail == iAddr)
  2082. {
  2083. _pHead->_iDirtyTail = block.getBlockHead()->_iSetPrev;
  2084. }
  2085. return ret;
  2086. }
  2087. //脏数据且超过_pHead->_iSyncTime没有回写, 需要回写
  2088. if(_pHead->_iSyncTime + data._synct < iNowTime && block.isDirty())
  2089. {
  2090. block.setDirty(false);
  2091. block.setSyncTime(iNowTime);
  2092. if(_pHead->_iDirtyTail == iAddr)
  2093. {
  2094. _pHead->_iDirtyTail = block.getBlockHead()->_iSetPrev;
  2095. }
  2096. return RT_NEED_SYNC;
  2097. }
  2098. //脏数据, 但是不需要回写, 脏链表不往前推
  2099. return RT_NONEED_SYNC;
  2100. }
  2101. void TC_RBTree::backup(bool bForceFromBegin)
  2102. {
  2103. FailureRecover check(this);
  2104. if(bForceFromBegin || _pHead->_iBackupTail == 0)
  2105. {
  2106. //移动备份指针到Get链尾部, 准备开始备份数据
  2107. _pHead->_iBackupTail = _pHead->_iGetTail;
  2108. }
  2109. }
  2110. int TC_RBTree::backup(BlockData &data)
  2111. {
  2112. FailureRecover check(this);
  2113. uint32_t iAddr = _pHead->_iBackupTail;
  2114. //到链表头部了, 返回RT_OK
  2115. if(iAddr == 0)
  2116. {
  2117. return RT_OK;
  2118. }
  2119. Block block(this, iAddr);
  2120. int ret = block.getBlockData(data);
  2121. //迁移一次
  2122. _pHead->_iBackupTail = block.getBlockHead()->_iGetPrev;
  2123. if(ret == TC_RBTree::RT_OK)
  2124. {
  2125. return TC_RBTree::RT_NEED_BACKUP;
  2126. }
  2127. return ret;
  2128. }
  2129. /
  2130. TC_RBTree::nolock_iterator TC_RBTree::nolock_begin()
  2131. {
  2132. FailureRecover check(this);
  2133. if(_pHead->_iRootAddr == 0)
  2134. {
  2135. return nolock_end();
  2136. }
  2137. Block root(this, _pHead->_iRootAddr);
  2138. //最左节点
  2139. while(root.hasLeft())
  2140. {
  2141. root.moveToLeft();
  2142. }
  2143. string key;
  2144. lock_iterator it(this, root.getHead(), lock_iterator::IT_RBTREE, lock_iterator::IT_NEXT);
  2145. while(true)
  2146. {
  2147. RBTreeLockItem item(this, it->getAddr());
  2148. int ret = item.get(key);
  2149. if(ret != TC_RBTree::RT_OK && ret != TC_RBTree::RT_ONLY_KEY)
  2150. {
  2151. //当前元素有问题, 取下一个元素
  2152. ++it;
  2153. continue;
  2154. }
  2155. else
  2156. {
  2157. break;
  2158. }
  2159. }
  2160. return nolock_iterator(this, key, false, lock_iterator::IT_NEXT);
  2161. }
  2162. TC_RBTree::nolock_iterator TC_RBTree::nolock_rbegin()
  2163. {
  2164. FailureRecover check(this);
  2165. if(_pHead->_iRootAddr == 0)
  2166. {
  2167. return nolock_end();
  2168. }
  2169. Block root(this, _pHead->_iRootAddr);
  2170. //最右节点
  2171. while(root.hasRight())
  2172. {
  2173. root.moveToRight();
  2174. }
  2175. string key;
  2176. lock_iterator it(this, root.getHead(), lock_iterator::IT_RBTREE, lock_iterator::IT_PREV);
  2177. while(true)
  2178. {
  2179. RBTreeLockItem item(this, it->getAddr());
  2180. int ret = item.get(key);
  2181. if(ret != TC_RBTree::RT_OK && ret != TC_RBTree::RT_ONLY_KEY)
  2182. {
  2183. ++it;
  2184. continue;
  2185. }
  2186. else
  2187. {
  2188. break;
  2189. }
  2190. }
  2191. return nolock_iterator(this, key, false, lock_iterator::IT_PREV);
  2192. }
  2193. ///
  2194. TC_RBTree::lock_iterator TC_RBTree::begin()
  2195. {
  2196. FailureRecover check(this);
  2197. if(_pHead->_iRootAddr == 0)
  2198. {
  2199. return end();
  2200. }
  2201. Block root(this, _pHead->_iRootAddr);
  2202. //最左节点
  2203. while(root.hasLeft())
  2204. {
  2205. root.moveToLeft();
  2206. }
  2207. return lock_iterator(this, root.getHead(), lock_iterator::IT_RBTREE, lock_iterator::IT_NEXT);
  2208. }
  2209. TC_RBTree::lock_iterator TC_RBTree::rbegin()
  2210. {
  2211. FailureRecover check(this);
  2212. if(_pHead->_iRootAddr == 0)
  2213. {
  2214. return end();
  2215. }
  2216. Block root(this, _pHead->_iRootAddr);
  2217. //最左节点
  2218. while(root.hasRight())
  2219. {
  2220. root.moveToRight();
  2221. }
  2222. return lock_iterator(this, root.getHead(), lock_iterator::IT_RBTREE, lock_iterator::IT_PREV);
  2223. }
  2224. TC_RBTree::lock_iterator TC_RBTree::find(const string& k)
  2225. {
  2226. FailureRecover check(this);
  2227. int ret;
  2228. return find(_pHead->_iRootAddr, k, ret);
  2229. }
  2230. TC_RBTree::lock_iterator TC_RBTree::rfind(const string& k)
  2231. {
  2232. FailureRecover check(this);
  2233. int ret;
  2234. return find(_pHead->_iRootAddr, k, ret, false);
  2235. }
  2236. TC_RBTree::lock_iterator TC_RBTree::lower_bound(const string &k)
  2237. {
  2238. FailureRecover check(this);
  2239. if(_pHead->_iRootAddr == 0)
  2240. {
  2241. return end();
  2242. }
  2243. Block last = getLastBlock(k);
  2244. RBTreeLockItem item(this, last.getHead());
  2245. string k1;
  2246. item.get(k1);
  2247. //k 大于 map中的数据
  2248. if(_lessf(k1, k))
  2249. {
  2250. lock_iterator it(this, last.getHead(), lock_iterator::IT_RBTREE, lock_iterator::IT_NEXT);
  2251. ++it;
  2252. return it;
  2253. }
  2254. return lock_iterator(this, last.getHead(), lock_iterator::IT_RBTREE, lock_iterator::IT_NEXT);
  2255. }
  2256. TC_RBTree::lock_iterator TC_RBTree::upper_bound(const string &k)
  2257. {
  2258. FailureRecover check(this);
  2259. if(_pHead->_iRootAddr == 0)
  2260. {
  2261. return end();
  2262. }
  2263. Block last = getLastBlock(k);
  2264. RBTreeLockItem item(this, last.getHead());
  2265. string k1;
  2266. item.get(k1);
  2267. //k < map中的数据
  2268. if(_lessf(k, k1))
  2269. {
  2270. lock_iterator it(this, last.getHead(), lock_iterator::IT_RBTREE, lock_iterator::IT_NEXT);
  2271. return it;
  2272. }
  2273. //k >= map中的数据
  2274. lock_iterator it(this, last.getHead(), lock_iterator::IT_RBTREE, lock_iterator::IT_NEXT);
  2275. ++it;
  2276. return it;
  2277. }
  2278. pair<TC_RBTree::lock_iterator, TC_RBTree::lock_iterator> TC_RBTree::equal_range(const string& k1, const string &k2)
  2279. {
  2280. FailureRecover check(this);
  2281. pair<lock_iterator, lock_iterator> pit;
  2282. if(_lessf(k2, k1))
  2283. {
  2284. pit.first = end();
  2285. pit.second = end();
  2286. return pit;
  2287. }
  2288. pit.first = lower_bound(k1);
  2289. pit.second = upper_bound(k2);
  2290. return pit;
  2291. }
  2292. ///
  2293. ///
  2294. TC_RBTree::lock_iterator TC_RBTree::beginSetTime()
  2295. {
  2296. FailureRecover check(this);
  2297. return lock_iterator(this, _pHead->_iSetHead, lock_iterator::IT_SET, lock_iterator::IT_NEXT);
  2298. }
  2299. TC_RBTree::lock_iterator TC_RBTree::rbeginSetTime()
  2300. {
  2301. FailureRecover check(this);
  2302. return lock_iterator(this, _pHead->_iSetTail, lock_iterator::IT_SET, lock_iterator::IT_PREV);
  2303. }
  2304. TC_RBTree::lock_iterator TC_RBTree::beginGetTime()
  2305. {
  2306. FailureRecover check(this);
  2307. return lock_iterator(this, _pHead->_iGetHead, lock_iterator::IT_GET, lock_iterator::IT_NEXT);
  2308. }
  2309. TC_RBTree::lock_iterator TC_RBTree::rbeginGetTime()
  2310. {
  2311. FailureRecover check(this);
  2312. return lock_iterator(this, _pHead->_iGetTail, lock_iterator::IT_GET, lock_iterator::IT_PREV);
  2313. }
  2314. TC_RBTree::lock_iterator TC_RBTree::beginDirty()
  2315. {
  2316. FailureRecover check(this);
  2317. return lock_iterator(this, _pHead->_iDirtyTail, lock_iterator::IT_SET, lock_iterator::IT_PREV);
  2318. }
  2319. /
  2320. string TC_RBTree::desc()
  2321. {
  2322. ostringstream s;
  2323. {
  2324. s << "[Version = " << (int)_pHead->_cMaxVersion << "." << (int)_pHead->_cMinVersion << "]" << endl;
  2325. s << "[ReadOnly = " << _pHead->_bReadOnly << "]" << endl;
  2326. s << "[AutoErase = " << _pHead->_bAutoErase << "]" << endl;
  2327. s << "[MemSize = " << _pHead->_iMemSize << "]" << endl;
  2328. s << "[Capacity = " << _pDataAllocator->getCapacity() << "]" << endl;
  2329. s << "[SingleBlockCount = " << TC_Common::tostr(singleBlockChunkCount()) << "]" << endl;
  2330. s << "[AllBlockChunk = " << allBlockChunkCount() << "]" << endl;
  2331. s << "[UsedChunk = " << _pHead->_iUsedChunk << "]" << endl;
  2332. s << "[FreeChunk = " << allBlockChunkCount() - _pHead->_iUsedChunk << "]" << endl;
  2333. s << "[MinDataSize = " << _pHead->_iMinDataSize << "]" << endl;
  2334. s << "[MaxDataSize = " << _pHead->_iMaxDataSize << "]" << endl;
  2335. s << "[ElementCount = " << _pHead->_iElementCount << "]" << endl;
  2336. s << "[EraseCount = " << _pHead->_iEraseCount << "]" << endl;
  2337. s << "[RootAddr = " << _pHead->_iRootAddr << "]" << endl;
  2338. s << "[SetHead = " << _pHead->_iSetHead << "]" << endl;
  2339. s << "[SetTail = " << _pHead->_iSetTail << "]" << endl;
  2340. s << "[GetHead = " << _pHead->_iGetHead << "]" << endl;
  2341. s << "[GetTail = " << _pHead->_iGetTail << "]" << endl;
  2342. s << "[DirtyTail = " << _pHead->_iDirtyTail << "]" << endl;
  2343. s << "[SyncTail = " << _pHead->_iSyncTail << "]" << endl;
  2344. s << "[SyncTime = " << _pHead->_iSyncTime << "]" << endl;
  2345. s << "[BackupTail = " << _pHead->_iBackupTail << "]" << endl;
  2346. s << "[DirtyCount = " << _pHead->_iDirtyCount << "]" << endl;
  2347. s << "[GetCount = " << _pHead->_iGetCount << "]" << endl;
  2348. s << "[HitCount = " << _pHead->_iHitCount << "]" << endl;
  2349. s << "[ModifyStatus = " << (int)_pstModifyHead->_cModifyStatus << "]" << endl;
  2350. s << "[ModifyIndex = " << _pstModifyHead->_iNowIndex << "]" << endl;
  2351. s << "[OnlyKeyCount = " << _pHead->_iOnlyKeyCount << "]" << endl;
  2352. }
  2353. vector<TC_MemChunk::tagChunkHead> vChunkHead = _pDataAllocator->getBlockDetail();
  2354. s << "*************************************************************************" << endl;
  2355. s << "[DiffBlockCount = " << vChunkHead.size() << "]" << endl;
  2356. for(size_t i = 0; i < vChunkHead.size(); i++)
  2357. {
  2358. s << "[BlockSize = " << vChunkHead[i]._iBlockSize << "]";
  2359. s << "[BlockCount = " << vChunkHead[i]._iBlockCount << "]";
  2360. s << "[BlockAvailable = " << vChunkHead[i]._blockAvailable << "]" << endl;
  2361. }
  2362. return s.str();
  2363. }
  2364. uint32_t TC_RBTree::eraseExcept(uint32_t iNowAddr, vector<BlockData> &vtData)
  2365. {
  2366. //在真正数据淘汰前使修改生效
  2367. doUpdate();
  2368. //不能被淘汰
  2369. if(!_pHead->_bAutoErase) return 0;
  2370. uint32_t n = _pHead->_iEraseCount;
  2371. if(n == 0) n = 10;
  2372. uint32_t d = n;
  2373. while (d != 0)
  2374. {
  2375. uint32_t iAddr;
  2376. //判断按照哪种方式淘汰
  2377. if(_pHead->_cEraseMode == TC_RBTree::ERASEBYSET)
  2378. {
  2379. iAddr = _pHead->_iSetTail;
  2380. }
  2381. else
  2382. {
  2383. iAddr = _pHead->_iGetTail;
  2384. }
  2385. if (iAddr == 0)
  2386. {
  2387. break;
  2388. }
  2389. Block block(this, iAddr);
  2390. //当前block正在分配空间, 不能删除, 移到上一个数据
  2391. if (iNowAddr == iAddr)
  2392. {
  2393. if(_pHead->_cEraseMode == TC_RBTree::ERASEBYSET)
  2394. {
  2395. iAddr = block.getBlockHead()->_iSetPrev;
  2396. }
  2397. else
  2398. {
  2399. iAddr = block.getBlockHead()->_iGetPrev;
  2400. }
  2401. }
  2402. if(iAddr == 0)
  2403. {
  2404. break;
  2405. }
  2406. Block block1(this, iAddr);
  2407. BlockData data;
  2408. int ret = block1.getBlockData(data);
  2409. if(ret == TC_RBTree::RT_OK)
  2410. {
  2411. vtData.push_back(data);
  2412. d--;
  2413. }
  2414. else if(ret == TC_RBTree::RT_NO_DATA)
  2415. {
  2416. d--;
  2417. }
  2418. //做现场保护
  2419. FailureRecover check(this);
  2420. //删除数据
  2421. block1.erase();
  2422. }
  2423. return n-d;
  2424. }
  2425. TC_RBTree::Block TC_RBTree::getLastBlock(const string &k)
  2426. {
  2427. assert(_pHead->_iRootAddr != 0);
  2428. uint32_t iNowAddr = _pHead->_iRootAddr;
  2429. Block block(this, iNowAddr);
  2430. while(iNowAddr != 0)
  2431. {
  2432. BlockData data;
  2433. int iRet = block.getBlockData(data);
  2434. if(iRet != TC_RBTree::RT_OK && iRet != TC_RBTree::RT_ONLY_KEY)
  2435. {
  2436. block.erase();
  2437. //数据块有问题,删除后,自恢复
  2438. iNowAddr = _pHead->_iRootAddr;
  2439. continue;
  2440. }
  2441. iNowAddr = block.getHead();
  2442. if(_lessf(k, data._key))
  2443. {
  2444. if(!block.moveToLeft())
  2445. break;
  2446. }
  2447. else if(_lessf(data._key, k))
  2448. {
  2449. if(!block.moveToRight())
  2450. break;
  2451. }
  2452. else
  2453. {
  2454. break;
  2455. }
  2456. }
  2457. return Block(this, iNowAddr);
  2458. }
  2459. TC_RBTree::lock_iterator TC_RBTree::find(uint32_t iAddr, const string& k, int &ret, bool bOrder)
  2460. {
  2461. if(iAddr == 0)
  2462. return end();
  2463. ret = TC_RBTree::RT_OK;
  2464. string k1;
  2465. Block block(this, iAddr);
  2466. RBTreeLockItem mcmdi(this, block.getHead());
  2467. if(mcmdi.equal(k, k1, ret))
  2468. {
  2469. incHitCount();
  2470. if(bOrder)
  2471. {
  2472. return lock_iterator(this, block.getHead(), lock_iterator::IT_RBTREE, lock_iterator::IT_NEXT);
  2473. }
  2474. else
  2475. {
  2476. return lock_iterator(this, block.getHead(), lock_iterator::IT_RBTREE, lock_iterator::IT_PREV);
  2477. }
  2478. }
  2479. if(_lessf(k, k1))
  2480. {
  2481. if(block.moveToLeft())
  2482. {
  2483. return find(block.getHead(), k, ret);
  2484. }
  2485. }
  2486. else
  2487. {
  2488. if(block.moveToRight())
  2489. {
  2490. return find(block.getHead(), k, ret);
  2491. }
  2492. }
  2493. return end();
  2494. }
  2495. TC_RBTree::lock_iterator TC_RBTree::find(uint32_t iAddr, const string& k, string &v, int &ret)
  2496. {
  2497. if(iAddr == 0)
  2498. return end();
  2499. ret = TC_RBTree::RT_OK;
  2500. string k1;
  2501. Block block(this, iAddr);
  2502. RBTreeLockItem mcmdi(this, block.getHead());
  2503. if(mcmdi.equal(k, k1, v, ret))
  2504. {
  2505. incHitCount();
  2506. return lock_iterator(this, block.getHead(), lock_iterator::IT_RBTREE, lock_iterator::IT_NEXT);
  2507. }
  2508. if(_lessf(k, k1))
  2509. {
  2510. if(block.moveToLeft())
  2511. {
  2512. return find(block.getHead(), k, v, ret);
  2513. }
  2514. }
  2515. else
  2516. {
  2517. if(block.moveToRight())
  2518. {
  2519. return find(block.getHead(), k, v, ret);
  2520. }
  2521. }
  2522. return end();
  2523. }
  2524. void TC_RBTree::doRecover()
  2525. {
  2526. //==1 copy过程中, 程序失败, 恢复原数据
  2527. if(_pstModifyHead->_cModifyStatus == 1)
  2528. {
  2529. for(int i = _pstModifyHead->_iNowIndex-1; i >=0; i--)
  2530. {
  2531. if(_pstModifyHead->_stModifyData[i]._cBytes == sizeof(uint64_t))
  2532. {
  2533. *(uint64_t*)((char*)_pHead + _pstModifyHead->_stModifyData[i]._iModifyAddr) = _pstModifyHead->_stModifyData[i]._iModifyValue;
  2534. }
  2535. //#if __WORDSIZE == 64
  2536. else if(_pstModifyHead->_stModifyData[i]._cBytes == sizeof(uint32_t))
  2537. {
  2538. *(uint32_t*)((char*)_pHead + _pstModifyHead->_stModifyData[i]._iModifyAddr) = (uint32_t)_pstModifyHead->_stModifyData[i]._iModifyValue;
  2539. }
  2540. //#endif
  2541. else if(_pstModifyHead->_stModifyData[i]._cBytes == sizeof(uint16_t))
  2542. {
  2543. *(uint16_t*)((char*)_pHead + _pstModifyHead->_stModifyData[i]._iModifyAddr) = (uint16_t)_pstModifyHead->_stModifyData[i]._iModifyValue;
  2544. }
  2545. else if(_pstModifyHead->_stModifyData[i]._cBytes == sizeof(uint8_t))
  2546. {
  2547. *(uint8_t*)((char*)_pHead + _pstModifyHead->_stModifyData[i]._iModifyAddr) = (bool)_pstModifyHead->_stModifyData[i]._iModifyValue;
  2548. }
  2549. else
  2550. {
  2551. assert(true);
  2552. }
  2553. }
  2554. _pstModifyHead->_iNowIndex = 0;
  2555. _pstModifyHead->_cModifyStatus = 0;
  2556. }
  2557. }
  2558. void TC_RBTree::doUpdate()
  2559. {
  2560. //==2,已经修改成功, 清除
  2561. if(_pstModifyHead->_cModifyStatus == 1)
  2562. {
  2563. _pstModifyHead->_iNowIndex = 0;
  2564. _pstModifyHead->_cModifyStatus = 0;
  2565. }
  2566. }
  2567. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载