tree_data.cc 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "tree_data.h"
  20. #include "global.h"
  21. #include "task/task_pkey.h"
  22. #include "buffer_flush.h"
  23. #include "algorithm/relative_hour_calculator.h"
  24. #ifndef likely
  25. #if __GCC_MAJOR >= 3
  26. #define likely(x) __builtin_expect(!!(x), 1)
  27. #define unlikely(x) __builtin_expect(!!(x), 0)
  28. #else
  29. #define likely(x) (x)
  30. #define unlikely(x) (x)
  31. #endif
  32. #endif
  33. #define GET_TREE_VALUE(x, t) \
  34. do { \
  35. if (unlikely(offset_ + sizeof(t) > size_)) \
  36. goto ERROR_RET; \
  37. x = (typeof(x)) * (t *)(p_content_ + offset_); \
  38. offset_ += sizeof(t); \
  39. } while (0)
  40. #define GET_TREE_VALUE_AT_OFFSET(x, t, offset) \
  41. do { \
  42. if (unlikely(offset + sizeof(t) > size_)) \
  43. goto ERROR_RET; \
  44. x = (typeof(x)) * (t *)(p_content_ + offset); \
  45. } while (0)
  46. #define SET_TREE_VALUE_AT_OFFSET(x, t, offset) \
  47. do { \
  48. if (unlikely(offset + sizeof(t) > size_)) \
  49. goto ERROR_RET; \
  50. *(t *)(p_content_ + offset) = x; \
  51. } while (0)
  52. #define SET_TREE_VALUE(x, t) \
  53. do { \
  54. if (unlikely(offset_ + sizeof(t) > size_)) \
  55. goto ERROR_RET; \
  56. *(t *)(p_content_ + offset_) = x; \
  57. offset_ += sizeof(t); \
  58. } while (0)
  59. #define SET_TREE_BIN_VALUE(p, len) \
  60. do { \
  61. if (unlikely(offset_ + sizeof(int) + len > size_)) \
  62. goto ERROR_RET; \
  63. *(int *)(p_content_ + offset_) = len; \
  64. offset_ += sizeof(int); \
  65. if (likely(len != 0)) \
  66. memcpy(p_content_ + offset_, p, len); \
  67. offset_ += len; \
  68. } while (0)
  69. #define SKIP_TREE_SIZE(s) \
  70. do { \
  71. if (unlikely(offset_ + s > size_)) \
  72. goto ERROR_RET; \
  73. offset_ += s; \
  74. } while (0)
  75. TreeData::TreeData(MallocBase *pstMalloc) : t_tree_(*pstMalloc)
  76. {
  77. p_tree_root_ = NULL;
  78. index_depth_ = 0;
  79. need_new_bufer_size = 0;
  80. key_size_ = 0;
  81. handle_ = INVALID_HANDLE;
  82. table_index_ = -1;
  83. size_ = 0;
  84. _root_size = 0;
  85. mallocator_ = pstMalloc;
  86. memset(err_message_, 0, sizeof(err_message_));
  87. key_index_ = -1;
  88. expire_id_ = -1;
  89. m_iLAId = -1;
  90. m_iLCmodId = -1;
  91. offset_ = 0;
  92. row_offset_ = 0;
  93. affected_rows_ = 0;
  94. index_part_of_uniq_field_ = false;
  95. p_record_ = INVALID_HANDLE;
  96. }
  97. TreeData::~TreeData()
  98. {
  99. handle_ = INVALID_HANDLE;
  100. _root_size = 0;
  101. }
  102. int TreeData::do_init(uint8_t uchKeyIdx, int iKeySize, const char *pchKey,
  103. int laId, int expireId, int nodeIdx)
  104. {
  105. int ks = iKeySize != 0 ? iKeySize : 1 + *(unsigned char *)pchKey;
  106. int uiDataSize = 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) * 3 +
  107. sizeof(MEM_HANDLE_T) + ks;
  108. handle_ = INVALID_HANDLE;
  109. _root_size = 0;
  110. handle_ = mallocator_->Malloc(uiDataSize);
  111. if (handle_ == INVALID_HANDLE) {
  112. snprintf(err_message_, sizeof(err_message_), "malloc error");
  113. need_new_bufer_size = uiDataSize;
  114. return (EC_NO_MEM);
  115. }
  116. _root_size = mallocator_->chunk_size(handle_);
  117. p_tree_root_ = Pointer<RootData>();
  118. p_tree_root_->data_type_ =
  119. ((table_index_ << 7) & 0x80) + DATA_TYPE_TREE_ROOT;
  120. p_tree_root_->tree_size_ = 0;
  121. p_tree_root_->total_raw_size_ = 0;
  122. p_tree_root_->node_count_ = 0;
  123. p_tree_root_->row_count_ = 0;
  124. p_tree_root_->root_handle_ = INVALID_HANDLE;
  125. p_tree_root_->get_request_count_ = 1;
  126. m_uiLAOffset = 0;
  127. key_size_ = iKeySize;
  128. key_index_ = uchKeyIdx;
  129. m_iLAId = laId;
  130. expire_id_ = expireId;
  131. if (nodeIdx != -1) {
  132. table_index_ = nodeIdx;
  133. }
  134. if (table_index_ != 0 && table_index_ != 1) {
  135. snprintf(err_message_, sizeof(err_message_), "node idx error");
  136. return -100;
  137. }
  138. if (iKeySize != 0) {
  139. memcpy(p_tree_root_->p_key_, pchKey, iKeySize);
  140. } else {
  141. memcpy(p_tree_root_->p_key_, pchKey, ks);
  142. }
  143. t_tree_.do_attach(INVALID_HANDLE);
  144. return (0);
  145. }
  146. int TreeData::do_init(const char *pchKey)
  147. {
  148. if (DTCColExpand::instance()->is_expanding())
  149. table_index_ =
  150. (DTCColExpand::instance()->cur_table_idx() + 1) % 2;
  151. else
  152. table_index_ = DTCColExpand::instance()->cur_table_idx() % 2;
  153. if (table_index_ != 0 && table_index_ != 1) {
  154. snprintf(err_message_, sizeof(err_message_),
  155. "attach error, nodeIdx[%d] error", table_index_);
  156. return -1;
  157. }
  158. p_table_ = TableDefinitionManager::instance()->get_table_def_by_idx(
  159. table_index_);
  160. if (p_table_ == NULL) {
  161. snprintf(err_message_, sizeof(err_message_),
  162. "attach error, tabledef[NULL]");
  163. return -1;
  164. }
  165. return do_init(p_table_->key_fields() - 1, p_table_->key_format(),
  166. pchKey, p_table_->lastacc_field_id(),
  167. p_table_->expire_time_field_id());
  168. }
  169. int TreeData::do_attach(MEM_HANDLE_T hHandle, uint8_t uchKeyIdx, int iKeySize,
  170. int laid, int lcmodid, int expireid)
  171. {
  172. _root_size = mallocator_->chunk_size(hHandle);
  173. if (unlikely(_root_size == 0)) {
  174. snprintf(err_message_, sizeof(err_message_), "attach error: %s",
  175. mallocator_->get_err_msg());
  176. return (-1);
  177. }
  178. handle_ = hHandle;
  179. p_tree_root_ = Pointer<RootData>();
  180. unsigned char uchType;
  181. uchType = p_tree_root_->data_type_;
  182. if (unlikely((uchType & 0x7f) != DATA_TYPE_TREE_ROOT)) {
  183. snprintf(err_message_, sizeof(err_message_),
  184. "invalid data type: %u", uchType);
  185. return (-2);
  186. }
  187. m_uiLAOffset = 0;
  188. key_size_ = iKeySize;
  189. key_index_ = uchKeyIdx;
  190. expire_id_ = expireid;
  191. m_iLAId = laid;
  192. m_iLCmodId = lcmodid;
  193. t_tree_.do_attach(p_tree_root_->root_handle_);
  194. return (0);
  195. }
  196. int TreeData::do_attach(MEM_HANDLE_T hHandle)
  197. {
  198. handle_ = hHandle;
  199. char *p = Pointer<char>();
  200. table_index_ = (*p >> 7) & 0x01;
  201. if (table_index_ != 0 && table_index_ != 1) {
  202. snprintf(err_message_, sizeof(err_message_),
  203. "attach error, nodeIdx[%d] error", table_index_);
  204. return -1;
  205. }
  206. p_table_ = TableDefinitionManager::instance()->get_table_def_by_idx(
  207. table_index_);
  208. if (p_table_ == NULL) {
  209. snprintf(err_message_, sizeof(err_message_),
  210. "attach error, tabledef[NULL]");
  211. return -1;
  212. }
  213. return do_attach(hHandle, p_table_->key_fields() - 1,
  214. p_table_->key_format(), p_table_->lastacc_field_id(),
  215. p_table_->lastcmod_field_id(),
  216. p_table_->expire_time_field_id());
  217. }
  218. int TreeData::encode_tree_row(const RowValue &stRow, unsigned char uchOp)
  219. {
  220. SET_TREE_VALUE(uchOp, unsigned char);
  221. for (int j = 1; j <= stRow.num_fields(); j++) //¿½±´Ò»ÐÐÊý¾Ý
  222. {
  223. if (stRow.table_definition()->is_discard(j))
  224. continue;
  225. const DTCValue *const v = stRow.field_value(j);
  226. switch (stRow.field_type(j)) {
  227. case DField::Signed:
  228. if (unlikely(stRow.field_size(j) >
  229. (int)sizeof(int32_t)))
  230. SET_TREE_VALUE(v->s64, int64_t);
  231. else
  232. SET_TREE_VALUE(v->s64, int32_t);
  233. break;
  234. case DField::Unsigned:
  235. if (unlikely(stRow.field_size(j) >
  236. (int)sizeof(uint32_t)))
  237. SET_TREE_VALUE(v->u64, uint64_t);
  238. else
  239. SET_TREE_VALUE(v->u64, uint32_t);
  240. break;
  241. case DField::Float:
  242. if (likely(stRow.field_size(j) > (int)sizeof(float)))
  243. SET_TREE_VALUE(v->flt, double);
  244. else
  245. SET_TREE_VALUE(v->flt, float);
  246. break;
  247. case DField::String:
  248. case DField::Binary:
  249. default: {
  250. SET_TREE_BIN_VALUE(v->bin.ptr, v->bin.len);
  251. break;
  252. }
  253. } //end of switch
  254. }
  255. return 0;
  256. ERROR_RET:
  257. snprintf(err_message_, sizeof(err_message_), "encode row error");
  258. return (-100);
  259. }
  260. int TreeData::expand_tree_chunk(MEM_HANDLE_T *pRecord, ALLOC_SIZE_T expand_size)
  261. {
  262. if (pRecord == NULL) {
  263. snprintf(err_message_, sizeof(err_message_),
  264. "tree data not init yet");
  265. return (-1);
  266. }
  267. uint32_t dataSize = *(uint32_t *)(p_content_ + sizeof(unsigned char));
  268. if (dataSize + expand_size > size_) {
  269. ALLOC_HANDLE_T hTmp = mallocator_->ReAlloc(
  270. (*pRecord), dataSize + expand_size);
  271. if (hTmp == INVALID_HANDLE) {
  272. snprintf(err_message_, sizeof(err_message_),
  273. "realloc error[%s]",
  274. mallocator_->get_err_msg());
  275. need_new_bufer_size = dataSize + expand_size;
  276. return (EC_NO_MEM);
  277. }
  278. p_tree_root_->tree_size_ -= size_;
  279. *pRecord = hTmp;
  280. size_ = mallocator_->chunk_size(hTmp);
  281. p_content_ = Pointer<char>(*pRecord);
  282. p_tree_root_->tree_size_ += size_;
  283. }
  284. return (0);
  285. }
  286. int TreeData::insert_sub_tree(uint8_t uchCondIdxCnt,
  287. const RowValue &stCondition, KeyComparator pfComp,
  288. ALLOC_HANDLE_T hRoot)
  289. {
  290. int iRet;
  291. if (uchCondIdxCnt != TTREE_INDEX_POS) {
  292. snprintf(err_message_, sizeof(err_message_),
  293. "index field error");
  294. return (-100);
  295. }
  296. bool isAllocNode = false;
  297. DTCValue value = stCondition[TTREE_INDEX_POS];
  298. char *indexKey = reinterpret_cast<char *>(&value);
  299. CmpCookie cookie(p_table_, uchCondIdxCnt);
  300. iRet = t_tree_.do_insert(indexKey, &cookie, pfComp, hRoot, isAllocNode);
  301. if (iRet == 0 && isAllocNode) {
  302. p_tree_root_->tree_size_ += sizeof(TtreeNode);
  303. }
  304. return iRet;
  305. }
  306. int TreeData::do_find(uint8_t uchCondIdxCnt, const RowValue &stCondition,
  307. KeyComparator pfComp, ALLOC_HANDLE_T *&hRecord)
  308. {
  309. int iRet;
  310. if (uchCondIdxCnt != TTREE_INDEX_POS) {
  311. snprintf(err_message_, sizeof(err_message_),
  312. "index field error");
  313. return (-100);
  314. }
  315. DTCValue value = stCondition[TTREE_INDEX_POS];
  316. char *indexKey = reinterpret_cast<char *>(&value);
  317. CmpCookie cookie(p_table_, uchCondIdxCnt);
  318. iRet = t_tree_.do_find(indexKey, &cookie, pfComp, hRecord);
  319. return iRet;
  320. }
  321. int TreeData::insert_row_flag(const RowValue &stRow, KeyComparator pfComp,
  322. unsigned char uchFlag)
  323. {
  324. int iRet;
  325. uint32_t rowCnt = 0;
  326. MEM_HANDLE_T *pRecord = NULL;
  327. MEM_HANDLE_T hRecord = INVALID_HANDLE;
  328. int trowSize = calc_tree_row_size(stRow, 0);
  329. int tSize = 0;
  330. offset_ = 0;
  331. iRet = do_find(TTREE_INDEX_POS, stRow, pfComp, pRecord);
  332. if (iRet == -100)
  333. return iRet;
  334. if (pRecord == NULL) {
  335. tSize = trowSize + sizeof(unsigned char) + sizeof(uint32_t) * 2;
  336. hRecord = mallocator_->Malloc(tSize);
  337. if (hRecord == INVALID_HANDLE) {
  338. need_new_bufer_size = tSize;
  339. snprintf(err_message_, sizeof(err_message_),
  340. "malloc error");
  341. return (EC_NO_MEM);
  342. }
  343. size_ = mallocator_->chunk_size(hRecord);
  344. p_content_ = Pointer<char>(hRecord);
  345. *p_content_ = DATA_TYPE_TREE_NODE; //RawFormat->DataType
  346. offset_ += sizeof(unsigned char);
  347. *(uint32_t *)(p_content_ + offset_) = 0; //RawFormat->data_size
  348. offset_ += sizeof(uint32_t);
  349. *(uint32_t *)(p_content_ + offset_) = 0; //RawFormat->RowCount
  350. offset_ += sizeof(uint32_t);
  351. iRet = encode_tree_row(stRow, uchFlag);
  352. if (iRet != 0) {
  353. goto ERROR_INSERT_RET;
  354. }
  355. iRet = insert_sub_tree(TTREE_INDEX_POS, stRow, pfComp, hRecord);
  356. if (iRet != 0) {
  357. snprintf(err_message_, sizeof(err_message_),
  358. "insert error");
  359. need_new_bufer_size = sizeof(TtreeNode);
  360. mallocator_->Free(hRecord);
  361. goto ERROR_INSERT_RET;
  362. }
  363. p_tree_root_->tree_size_ += size_;
  364. p_tree_root_->node_count_++;
  365. } else {
  366. p_content_ = Pointer<char>(*pRecord);
  367. size_ = mallocator_->chunk_size(*pRecord);
  368. iRet = expand_tree_chunk(pRecord, trowSize);
  369. if (iRet != 0) {
  370. snprintf(err_message_, sizeof(err_message_),
  371. "expand tree chunk error");
  372. return iRet;
  373. }
  374. offset_ = *(uint32_t *)(p_content_ +
  375. sizeof(unsigned char)); //datasize
  376. iRet = encode_tree_row(stRow, uchFlag);
  377. if (iRet != 0) {
  378. goto ERROR_INSERT_RET;
  379. }
  380. }
  381. /*每次insert数据之后,更新头部信息*/
  382. rowCnt = *(uint32_t *)(p_content_ + sizeof(unsigned char) +
  383. sizeof(uint32_t));
  384. *(uint32_t *)(p_content_ + sizeof(unsigned char)) = offset_;
  385. *(uint32_t *)(p_content_ + sizeof(unsigned char) + sizeof(uint32_t)) =
  386. rowCnt + 1;
  387. p_tree_root_->root_handle_ = t_tree_.Root();
  388. p_tree_root_->row_count_ += 1;
  389. p_tree_root_->total_raw_size_ += trowSize;
  390. ERROR_INSERT_RET:
  391. offset_ = 0;
  392. size_ = 0;
  393. hRecord = INVALID_HANDLE;
  394. p_content_ = NULL;
  395. return (iRet);
  396. }
  397. int TreeData::insert_row(const RowValue &stRow, KeyComparator pfComp,
  398. bool isDirty)
  399. {
  400. return insert_row_flag(stRow, pfComp,
  401. isDirty ? OPER_INSERT : OPER_SELECT);
  402. }
  403. unsigned TreeData::ask_for_destroy_size(void)
  404. {
  405. if (unlikely(_root_size == 0)) {
  406. snprintf(err_message_, sizeof(err_message_), "attach error: %s",
  407. mallocator_->get_err_msg());
  408. return (-1);
  409. }
  410. return p_tree_root_->tree_size_ + _root_size;
  411. }
  412. int TreeData::destory()
  413. {
  414. if (unlikely(_root_size == 0)) {
  415. snprintf(err_message_, sizeof(err_message_), "attach error: %s",
  416. mallocator_->get_err_msg());
  417. return (-1);
  418. }
  419. t_tree_.destory();
  420. mallocator_->Free(handle_);
  421. handle_ = INVALID_HANDLE;
  422. _root_size = 0;
  423. return (0);
  424. }
  425. int TreeData::copy_raw_all(RawData *new_data)
  426. {
  427. int iRet;
  428. uint32_t totalNodeCnt = p_tree_root_->node_count_;
  429. if (totalNodeCnt == 0) {
  430. return 1;
  431. }
  432. pResCookie resCookie;
  433. MEM_HANDLE_T pCookie[totalNodeCnt];
  434. resCookie.p_handle = pCookie;
  435. resCookie.need_find_node_count = 0;
  436. iRet = t_tree_.traverse_forward(Visit, &resCookie);
  437. if (iRet != 0) {
  438. snprintf(err_message_, sizeof(err_message_),
  439. " traverse tree-data rows error:%d", iRet);
  440. return (-1);
  441. }
  442. ALLOC_SIZE_T headlen = sizeof(unsigned char) + sizeof(uint32_t) * 2;
  443. for (uint32_t i = 0; i < resCookie.has_got_node_count; i++) {
  444. char *pch = Pointer<char>(pCookie[i]);
  445. ALLOC_SIZE_T dtsize =
  446. *(uint32_t *)(pch + sizeof(unsigned char));
  447. uint32_t rowcnt = *(uint32_t *)(pch + sizeof(unsigned char) +
  448. sizeof(uint32_t));
  449. iRet = new_data->append_n_records(rowcnt, pch + headlen,
  450. dtsize - headlen);
  451. if (iRet != 0)
  452. return iRet;
  453. }
  454. if ((iRet = new_data->do_attach(new_data->get_handle())) != 0)
  455. return (iRet);
  456. return 0;
  457. }
  458. int TreeData::copy_tree_all(RawData *new_data)
  459. {
  460. int iRet;
  461. if (p_table_->num_fields() < 1) {
  462. log4cplus_error("field nums is too short");
  463. return -1;
  464. }
  465. unsigned int uiTotalRows = new_data->total_rows();
  466. if (uiTotalRows == 0)
  467. return (0);
  468. new_data->rewind();
  469. RowValue stOldRow(p_table_);
  470. for (unsigned int i = 0; i < uiTotalRows; i++) {
  471. unsigned char uchRowFlags;
  472. stOldRow.default_value();
  473. if (new_data->decode_row(stOldRow, uchRowFlags, 0) != 0) {
  474. log4cplus_error("raw-data decode row error: %s",
  475. new_data->get_err_msg());
  476. return (-1);
  477. }
  478. iRet = insert_row(stOldRow, KeyCompare, false);
  479. if (iRet == EC_NO_MEM) {
  480. /*这里为了下次完全重新建立T树,把未建立完的树全部删除*/
  481. need_new_bufer_size =
  482. new_data->data_size() - new_data->data_start();
  483. destroy_sub_tree();
  484. return (EC_NO_MEM);
  485. }
  486. }
  487. return (0);
  488. }
  489. int TreeData::decode_tree_row(RowValue &stRow, unsigned char &uchRowFlags,
  490. int iDecodeFlag)
  491. {
  492. row_offset_ = offset_;
  493. GET_TREE_VALUE(uchRowFlags, unsigned char);
  494. for (int j = 1; j <= stRow.num_fields(); j++) {
  495. if (stRow.table_definition()->is_discard(j))
  496. continue;
  497. if (j == m_iLAId)
  498. m_uiLAOffset = offset_;
  499. switch (stRow.field_type(j)) {
  500. case DField::Signed:
  501. if (unlikely(stRow.field_size(j) >
  502. (int)sizeof(int32_t))) {
  503. GET_TREE_VALUE(stRow.field_value(j)->s64,
  504. int64_t);
  505. } else {
  506. GET_TREE_VALUE(stRow.field_value(j)->s64,
  507. int32_t);
  508. }
  509. break;
  510. case DField::Unsigned:
  511. if (unlikely(stRow.field_size(j) >
  512. (int)sizeof(uint32_t))) {
  513. GET_TREE_VALUE(stRow.field_value(j)->u64,
  514. uint64_t);
  515. } else {
  516. GET_TREE_VALUE(stRow.field_value(j)->u64,
  517. uint32_t);
  518. }
  519. break;
  520. case DField::Float:
  521. if (likely(stRow.field_size(j) > (int)sizeof(float))) {
  522. GET_TREE_VALUE(stRow.field_value(j)->flt,
  523. double);
  524. } else {
  525. GET_TREE_VALUE(stRow.field_value(j)->flt,
  526. float);
  527. }
  528. break;
  529. case DField::String:
  530. case DField::Binary:
  531. default: {
  532. GET_TREE_VALUE(stRow.field_value(j)->bin.len, int);
  533. stRow.field_value(j)->bin.ptr = p_content_ + offset_;
  534. SKIP_TREE_SIZE((uint32_t)stRow.field_value(j)->bin.len);
  535. break;
  536. }
  537. } //end of switch
  538. }
  539. return (0);
  540. ERROR_RET:
  541. snprintf(err_message_, sizeof(err_message_), "get value error");
  542. return (-100);
  543. }
  544. int TreeData::compare_tree_data(RowValue *stpNodeRow)
  545. {
  546. uint32_t rowCnt = p_tree_root_->node_count_;
  547. if (rowCnt == 0) {
  548. return 1;
  549. }
  550. const uint8_t *ufli = p_table_->uniq_fields_list();
  551. for (int i = 0;
  552. !index_part_of_uniq_field_ && i < p_table_->uniq_fields(); i++) {
  553. if (ufli[i] == TTREE_INDEX_POS) {
  554. index_part_of_uniq_field_ = true;
  555. break;
  556. }
  557. }
  558. if (index_part_of_uniq_field_) {
  559. MEM_HANDLE_T *pRecord = NULL;
  560. RowValue stOldRow(p_table_);
  561. char *indexKey = reinterpret_cast<char *>(
  562. stpNodeRow->field_value(TTREE_INDEX_POS));
  563. CmpCookie cookie(p_table_, TTREE_INDEX_POS);
  564. int iRet =
  565. t_tree_.do_find(indexKey, &cookie, KeyCompare, pRecord);
  566. if (iRet == -100)
  567. return iRet;
  568. if (pRecord != NULL) {
  569. p_content_ = Pointer<char>(*pRecord);
  570. uint32_t rows = *(uint32_t *)(p_content_ +
  571. sizeof(unsigned char) +
  572. sizeof(uint32_t));
  573. offset_ = sizeof(unsigned char) + sizeof(uint32_t) * 2;
  574. size_ = mallocator_->chunk_size(*pRecord);
  575. for (uint32_t j = 0; j < rows; j++) {
  576. stOldRow.default_value();
  577. unsigned char uchRowFlags;
  578. if (decode_tree_row(stOldRow, uchRowFlags, 0) !=
  579. 0) {
  580. return (-2);
  581. }
  582. if (stpNodeRow->Compare(
  583. stOldRow,
  584. p_table_->uniq_fields_list(),
  585. p_table_->uniq_fields()) == 0) {
  586. p_record_ = *pRecord;
  587. return 0;
  588. }
  589. }
  590. }
  591. } else {
  592. pResCookie resCookie;
  593. MEM_HANDLE_T pCookie[rowCnt];
  594. resCookie.p_handle = pCookie;
  595. resCookie.need_find_node_count = 0;
  596. if (t_tree_.traverse_forward(Visit, &resCookie) != 0) {
  597. snprintf(err_message_, sizeof(err_message_),
  598. " traverse tree-data rows error");
  599. return (-1);
  600. }
  601. RowValue stOldRow(p_table_);
  602. for (uint32_t i = 0; i < resCookie.has_got_node_count;
  603. i++) { //逐行拷贝数据
  604. p_content_ = Pointer<char>(pCookie[i]);
  605. uint32_t rows = *(uint32_t *)(p_content_ +
  606. sizeof(unsigned char) +
  607. sizeof(uint32_t));
  608. offset_ = sizeof(unsigned char) + sizeof(uint32_t) * 2;
  609. size_ = mallocator_->chunk_size(pCookie[i]);
  610. for (uint32_t j = 0; j < rows; j++) {
  611. stOldRow.default_value();
  612. unsigned char uchRowFlags;
  613. if (decode_tree_row(stOldRow, uchRowFlags, 0) !=
  614. 0) {
  615. return (-2);
  616. }
  617. if (stpNodeRow->Compare(
  618. stOldRow,
  619. p_table_->uniq_fields_list(),
  620. p_table_->uniq_fields()) == 0) {
  621. p_record_ = pCookie[i];
  622. return 0;
  623. }
  624. }
  625. }
  626. }
  627. return 1;
  628. }
  629. int TreeData::replace_tree_data(DTCJobOperation &job_op, Node *p_node,
  630. RawData *affected_data, bool async,
  631. unsigned char &RowFlag, bool setrows)
  632. {
  633. int iRet;
  634. unsigned int uiTotalRows = 0;
  635. uint32_t iDelete = 0;
  636. DTCTableDefinition *stpNodeTab, *stpTaskTab;
  637. RowValue *stpNodeRow, *stpTaskRow;
  638. stpNodeTab = p_table_;
  639. stpTaskTab = job_op.table_definition();
  640. RowValue stNewRow(stpTaskTab);
  641. RowValue stNewNodeRow(stpNodeTab);
  642. affected_rows_ = 0;
  643. stpTaskRow = &stNewRow;
  644. stpNodeRow = &stNewNodeRow;
  645. if (stpNodeTab == stpTaskTab)
  646. stpNodeRow = stpTaskRow;
  647. stNewRow.default_value();
  648. job_op.update_row(*stpTaskRow);
  649. if (stpNodeTab != stpTaskTab)
  650. stpNodeRow->Copy(stpTaskRow);
  651. else
  652. stpNodeRow = stpTaskRow;
  653. iRet = compare_tree_data(stpNodeRow);
  654. if (iRet < 0) {
  655. snprintf(err_message_, sizeof(err_message_),
  656. "compare tree data error:%d", iRet);
  657. return iRet;
  658. } else if (iRet == 0) {
  659. DTCValue new_value = (*stpTaskRow)[TTREE_INDEX_POS];
  660. char *NewIndex = reinterpret_cast<char *>(&new_value);
  661. CmpCookie cookie(p_table_, TTREE_INDEX_POS);
  662. if (KeyCompare(NewIndex, &cookie, *mallocator_, p_record_) !=
  663. 0) //Index字段变更
  664. {
  665. char *tmp_pchContent = p_content_;
  666. uint32_t tmp_size = size_;
  667. ALLOC_SIZE_T tmp_uiOffset = offset_;
  668. iRet = insert_row(*stpTaskRow, KeyCompare, m_async);
  669. p_content_ = tmp_pchContent;
  670. size_ = tmp_size;
  671. offset_ = tmp_uiOffset;
  672. if (iRet == EC_NO_MEM)
  673. return iRet;
  674. else if (iRet == 0) {
  675. offset_ = row_offset_;
  676. RowValue stOldRow(p_table_);
  677. stOldRow.default_value();
  678. unsigned char uchRowFlags;
  679. if (decode_tree_row(stOldRow, uchRowFlags, 0) !=
  680. 0) {
  681. return (-2);
  682. }
  683. RowFlag = uchRowFlags;
  684. uiTotalRows = get_row_count();
  685. offset_ = row_offset_;
  686. if (delete_cur_row(stOldRow) == 0)
  687. iDelete++;
  688. if (uiTotalRows > 0 && uiTotalRows == iDelete &&
  689. get_row_count() ==
  690. 0) //RowFormat上的内容已删光
  691. {
  692. //删除tree node
  693. bool isFreeNode = false;
  694. DTCValue value = (stOldRow)
  695. [TTREE_INDEX_POS]; //for轮询的最后一行数据
  696. char *indexKey =
  697. reinterpret_cast<char *>(
  698. &value);
  699. CmpCookie cookie(p_table_,
  700. TTREE_INDEX_POS);
  701. int iret = t_tree_.Delete(indexKey,
  702. &cookie,
  703. KeyCompare,
  704. isFreeNode);
  705. if (iret != 0) {
  706. snprintf(
  707. err_message_,
  708. sizeof(err_message_),
  709. "delete stTree failed:%d",
  710. iret);
  711. return -4;
  712. }
  713. if (isFreeNode)
  714. p_tree_root_->tree_size_ -=
  715. sizeof(TtreeNode);
  716. p_tree_root_->tree_size_ -= size_;
  717. p_tree_root_->node_count_--;
  718. p_tree_root_->root_handle_ =
  719. t_tree_.Root();
  720. //释放handle
  721. mallocator_->Free(p_record_);
  722. }
  723. }
  724. } else //Index字段不变
  725. {
  726. MEM_HANDLE_T *pRawHandle = NULL;
  727. int iRet = do_find(TTREE_INDEX_POS, *stpNodeRow,
  728. KeyCompare, pRawHandle);
  729. if (iRet == -100 || iRet == 0)
  730. return iRet;
  731. iRet = replace_cur_row(*stpNodeRow, m_async,
  732. pRawHandle); // 加进cache
  733. if (iRet == EC_NO_MEM) {
  734. return iRet;
  735. }
  736. if (iRet != 0) {
  737. /*标记加入黑名单*/
  738. job_op.push_black_list_size(need_size());
  739. return (-6);
  740. }
  741. }
  742. affected_rows_ = 2;
  743. }
  744. return 0;
  745. }
  746. int TreeData::replace_sub_raw_data(DTCJobOperation &job_op,
  747. MEM_HANDLE_T hRecord)
  748. {
  749. DTCTableDefinition *stpNodeTab, *stpTaskTab;
  750. RowValue *stpNodeRow, *stpTaskRow, *stpCurRow;
  751. stpNodeTab = p_table_;
  752. stpTaskTab = job_op.table_definition();
  753. RowValue stNewRow(stpTaskTab);
  754. RowValue stNewNodeRow(stpNodeTab);
  755. RowValue stCurRow(stpNodeTab);
  756. stpTaskRow = &stNewRow;
  757. stpNodeRow = &stNewNodeRow;
  758. stpCurRow = &stCurRow;
  759. if (stpNodeTab == stpTaskTab)
  760. stpNodeRow = stpTaskRow;
  761. p_content_ = Pointer<char>(hRecord);
  762. unsigned int uiTotalRows = get_row_count();
  763. offset_ = sizeof(unsigned char) +
  764. sizeof(uint32_t) * 2; //offset DataType + data_size + RowCount
  765. size_ = mallocator_->chunk_size(hRecord);
  766. unsigned char uchRowFlags;
  767. uint32_t iDelete = 0;
  768. uint32_t iInsert = 0;
  769. for (unsigned int i = 0; i < uiTotalRows; i++) {
  770. if (decode_tree_row(*stpNodeRow, uchRowFlags, 0) != 0)
  771. return (-1);
  772. if (stpNodeTab != stpTaskTab)
  773. stpTaskRow->Copy(stpNodeRow);
  774. stpCurRow->Copy(stpNodeRow);
  775. //如果不符合查询条件
  776. if (job_op.compare_row(*stpTaskRow) == 0)
  777. continue;
  778. MEM_HANDLE_T *pRawHandle = NULL;
  779. int iRet = do_find(TTREE_INDEX_POS, *stpCurRow, KeyCompare,
  780. pRawHandle);
  781. if (iRet == -100 || iRet == 0)
  782. return iRet;
  783. job_op.update_row(*stpTaskRow); //修改数据
  784. if (stpNodeTab != stpTaskTab)
  785. stpNodeRow->Copy(stpTaskRow);
  786. if (affected_rows_ == 0) {
  787. iRet = 0;
  788. DTCValue new_value = (*stpTaskRow)[TTREE_INDEX_POS];
  789. char *NewIndex = reinterpret_cast<char *>(&new_value);
  790. CmpCookie cookie(p_table_, TTREE_INDEX_POS);
  791. if (KeyCompare(NewIndex, &cookie, *mallocator_,
  792. hRecord) != 0) //update Index字段
  793. {
  794. char *tmp_pchContent = p_content_;
  795. uint32_t tmp_size = size_;
  796. ALLOC_SIZE_T tmp_uiOffset = offset_;
  797. iRet = insert_row(*stpTaskRow, KeyCompare,
  798. m_async);
  799. p_content_ = tmp_pchContent;
  800. size_ = tmp_size;
  801. offset_ = tmp_uiOffset;
  802. if (iRet == EC_NO_MEM) {
  803. return iRet;
  804. } else if (iRet == 0) {
  805. iInsert++;
  806. offset_ = row_offset_;
  807. if (delete_cur_row(*stpCurRow) == 0)
  808. iDelete++;
  809. }
  810. } else {
  811. iRet = replace_cur_row(*stpNodeRow, m_async,
  812. pRawHandle); // 加进cache
  813. if (iRet == EC_NO_MEM) {
  814. return iRet;
  815. }
  816. if (iRet != 0) {
  817. /*标记加入黑名单*/
  818. job_op.push_black_list_size(
  819. need_size());
  820. return (-6);
  821. }
  822. }
  823. affected_rows_ += 2;
  824. } else {
  825. if (delete_cur_row(*stpCurRow) == 0) {
  826. iDelete++;
  827. affected_rows_++;
  828. }
  829. }
  830. }
  831. if (uiTotalRows > 0 &&
  832. uiTotalRows - iDelete == 0) //RowFormat上的内容已删光
  833. {
  834. //删除tree node
  835. bool isFreeNode = false;
  836. DTCValue value =
  837. (*stpCurRow)[TTREE_INDEX_POS]; //for轮询的最后一行数据
  838. char *indexKey = reinterpret_cast<char *>(&value);
  839. CmpCookie cookie(p_table_, TTREE_INDEX_POS);
  840. int iret = t_tree_.Delete(indexKey, &cookie, KeyCompare,
  841. isFreeNode);
  842. if (iret != 0) {
  843. snprintf(err_message_, sizeof(err_message_),
  844. "delete stTree failed:%d", iret);
  845. return -4;
  846. }
  847. if (isFreeNode)
  848. p_tree_root_->tree_size_ -= sizeof(TtreeNode);
  849. p_tree_root_->tree_size_ -= size_;
  850. p_tree_root_->node_count_--;
  851. p_tree_root_->root_handle_ = t_tree_.Root();
  852. //释放handle
  853. mallocator_->Free(hRecord);
  854. }
  855. return 0;
  856. }
  857. /*
  858. * encode到私有内存,防止replace,update引起重新rellocate导致value引用了过期指针
  859. */
  860. int TreeData::encode_to_private_area(RawData &raw, RowValue &value,
  861. unsigned char value_flag)
  862. {
  863. int ret = raw.do_init(
  864. key(), raw.calc_row_size(value, p_table_->key_fields() - 1));
  865. if (0 != ret) {
  866. log4cplus_error("init raw-data struct error, ret=%d, err=%s",
  867. ret, raw.get_err_msg());
  868. return -1;
  869. }
  870. ret = raw.insert_row(value, false, false);
  871. if (0 != ret) {
  872. log4cplus_error("insert row to raw-data error: ret=%d, err=%s",
  873. ret, raw.get_err_msg());
  874. return -2;
  875. }
  876. raw.rewind();
  877. ret = raw.decode_row(value, value_flag, 0);
  878. if (0 != ret) {
  879. log4cplus_error("decode raw-data to row error: ret=%d, err=%s",
  880. ret, raw.get_err_msg());
  881. return -3;
  882. }
  883. return 0;
  884. }
  885. int TreeData::update_sub_raw_data(DTCJobOperation &job_op, MEM_HANDLE_T hRecord)
  886. {
  887. DTCTableDefinition *stpNodeTab, *stpTaskTab;
  888. RowValue *stpNodeRow, *stpTaskRow, *stpCurRow;
  889. stpNodeTab = p_table_;
  890. stpTaskTab = job_op.table_definition();
  891. RowValue stNewRow(stpTaskTab);
  892. RowValue stNewNodeRow(stpNodeTab);
  893. RowValue stCurRow(stpNodeTab);
  894. stpTaskRow = &stNewRow;
  895. stpNodeRow = &stNewNodeRow;
  896. stpCurRow = &stCurRow;
  897. if (stpNodeTab == stpTaskTab)
  898. stpNodeRow = stpTaskRow;
  899. p_content_ = Pointer<char>(hRecord);
  900. unsigned int uiTotalRows = get_row_count();
  901. offset_ = sizeof(unsigned char) +
  902. sizeof(uint32_t) * 2; //offset DataType + data_size + RowCount
  903. size_ = mallocator_->chunk_size(hRecord);
  904. unsigned char uchRowFlags;
  905. uint32_t iDelete = 0;
  906. uint32_t iInsert = 0;
  907. for (unsigned int i = 0; i < uiTotalRows; i++) {
  908. if (decode_tree_row(*stpNodeRow, uchRowFlags, 0) != 0)
  909. return (-1);
  910. if (stpNodeTab != stpTaskTab)
  911. stpTaskRow->Copy(stpNodeRow);
  912. stpCurRow->Copy(stpNodeRow);
  913. //如果不符合查询条件
  914. if (job_op.compare_row(*stpTaskRow) == 0)
  915. continue;
  916. MEM_HANDLE_T *pRawHandle = NULL;
  917. int iRet = do_find(TTREE_INDEX_POS, *stpCurRow, KeyCompare,
  918. pRawHandle);
  919. if (iRet == -100 || iRet == 0)
  920. return iRet;
  921. job_op.update_row(*stpTaskRow); //修改数据
  922. if (stpNodeTab != stpTaskTab)
  923. stpNodeRow->Copy(stpTaskRow);
  924. iRet = 0;
  925. DTCValue new_value = (*stpTaskRow)[TTREE_INDEX_POS];
  926. char *NewIndex = reinterpret_cast<char *>(&new_value);
  927. CmpCookie cookie(p_table_, TTREE_INDEX_POS);
  928. if (KeyCompare(NewIndex, &cookie, *mallocator_, hRecord) !=
  929. 0) //update Index字段
  930. {
  931. char *tmp_pchContent = p_content_;
  932. uint32_t tmp_size = size_;
  933. ALLOC_SIZE_T tmp_uiOffset = offset_;
  934. iRet = insert_row(*stpTaskRow, KeyCompare, m_async);
  935. p_content_ = tmp_pchContent;
  936. size_ = tmp_size;
  937. offset_ = tmp_uiOffset;
  938. if (iRet == EC_NO_MEM) {
  939. return iRet;
  940. } else if (iRet == 0) {
  941. iInsert++;
  942. offset_ = row_offset_;
  943. if (delete_cur_row(*stpCurRow) == 0)
  944. iDelete++;
  945. }
  946. } else {
  947. // 在私有区间decode
  948. RawData stTmpRows(&g_stSysMalloc, 1);
  949. if (encode_to_private_area(stTmpRows, *stpNodeRow,
  950. uchRowFlags)) {
  951. log4cplus_error(
  952. "encode rowvalue to private rawdata area failed");
  953. return -3;
  954. }
  955. iRet = replace_cur_row(*stpNodeRow, m_async,
  956. pRawHandle); // 加进cache
  957. if (iRet == EC_NO_MEM) {
  958. return iRet;
  959. }
  960. if (iRet != 0) {
  961. /*标记加入黑名单*/
  962. job_op.push_black_list_size(need_size());
  963. return (-6);
  964. }
  965. }
  966. affected_rows_++;
  967. if (uchRowFlags & OPER_DIRTY)
  968. dirty_rows_count_--;
  969. if (m_async)
  970. dirty_rows_count_++;
  971. }
  972. if (uiTotalRows > 0 &&
  973. uiTotalRows - iDelete == 0) //RowFormat上的内容已删光
  974. {
  975. //删除tree node
  976. bool isFreeNode = false;
  977. DTCValue value =
  978. (*stpCurRow)[TTREE_INDEX_POS]; //for轮询的最后一行数据
  979. char *indexKey = reinterpret_cast<char *>(&value);
  980. CmpCookie cookie(p_table_, TTREE_INDEX_POS);
  981. int iret = t_tree_.Delete(indexKey, &cookie, KeyCompare,
  982. isFreeNode);
  983. if (iret != 0) {
  984. snprintf(err_message_, sizeof(err_message_),
  985. "delete stTree failed:%d", iret);
  986. return -4;
  987. }
  988. if (isFreeNode)
  989. p_tree_root_->tree_size_ -= sizeof(TtreeNode);
  990. p_tree_root_->tree_size_ -= size_;
  991. p_tree_root_->node_count_--;
  992. p_tree_root_->root_handle_ = t_tree_.Root();
  993. //释放handle
  994. mallocator_->Free(hRecord);
  995. }
  996. if (iInsert != iDelete) {
  997. snprintf(err_message_, sizeof(err_message_),
  998. "update index change error: insert:%d, delete:%d",
  999. iInsert, iDelete);
  1000. return (-10);
  1001. }
  1002. return 0;
  1003. }
  1004. int TreeData::delete_sub_raw_data(DTCJobOperation &job_op, MEM_HANDLE_T hRecord)
  1005. {
  1006. int iRet;
  1007. DTCTableDefinition *stpNodeTab, *stpTaskTab;
  1008. RowValue *stpNodeRow, *stpTaskRow;
  1009. stpNodeTab = p_table_;
  1010. stpTaskTab = job_op.table_definition();
  1011. RowValue stNodeRow(stpNodeTab);
  1012. RowValue stTaskRow(stpTaskTab);
  1013. if (stpNodeTab == stpTaskTab) {
  1014. stpNodeRow = &stTaskRow;
  1015. stpTaskRow = &stTaskRow;
  1016. } else {
  1017. stpNodeRow = &stNodeRow;
  1018. stpTaskRow = &stTaskRow;
  1019. }
  1020. unsigned int iAffectRows = 0;
  1021. unsigned char uchRowFlags;
  1022. p_content_ = Pointer<char>(hRecord);
  1023. unsigned int uiTotalRows = get_row_count();
  1024. offset_ = sizeof(unsigned char) +
  1025. sizeof(uint32_t) * 2; //offset DataType + data_size + RowCount
  1026. size_ = mallocator_->chunk_size(hRecord);
  1027. for (unsigned int i = 0; i < uiTotalRows; i++) {
  1028. if ((decode_tree_row(*stpNodeRow, uchRowFlags, 0)) != 0) {
  1029. return (-2);
  1030. }
  1031. if (stpNodeTab != stpTaskTab) {
  1032. stpTaskRow->Copy(stpNodeRow);
  1033. }
  1034. if (job_op.compare_row(*stpTaskRow) != 0) { //符合del条件
  1035. iRet = delete_cur_row(*stpNodeRow);
  1036. if (iRet != 0) {
  1037. log4cplus_error(
  1038. "tree-data delete row error: %d", iRet);
  1039. return (-5);
  1040. }
  1041. iAffectRows++;
  1042. rows_count_--;
  1043. if (uchRowFlags & OPER_DIRTY)
  1044. dirty_rows_count_--;
  1045. }
  1046. }
  1047. if (iAffectRows > uiTotalRows)
  1048. return (-3);
  1049. else if (iAffectRows == uiTotalRows &&
  1050. uiTotalRows > 0) //RowFormat上的内容已删光
  1051. {
  1052. //删除tree node
  1053. bool isFreeNode = false;
  1054. DTCValue value =
  1055. (*stpNodeRow)[TTREE_INDEX_POS]; //for轮询的最后一行数据
  1056. char *indexKey = reinterpret_cast<char *>(&value);
  1057. CmpCookie cookie(p_table_, TTREE_INDEX_POS);
  1058. int iret = t_tree_.Delete(indexKey, &cookie, KeyCompare,
  1059. isFreeNode);
  1060. if (iret != 0) {
  1061. snprintf(err_message_, sizeof(err_message_),
  1062. "delete stTree failed:%d\t%s", iret,
  1063. t_tree_.get_err_msg());
  1064. return -4;
  1065. }
  1066. if (isFreeNode)
  1067. p_tree_root_->tree_size_ -= sizeof(TtreeNode);
  1068. p_tree_root_->tree_size_ -= size_;
  1069. p_tree_root_->node_count_--;
  1070. p_tree_root_->root_handle_ = t_tree_.Root();
  1071. //释放handle
  1072. mallocator_->Free(hRecord);
  1073. }
  1074. return (0);
  1075. }
  1076. int TreeData::skip_row(const RowValue &stRow)
  1077. {
  1078. if (p_content_ == NULL) {
  1079. snprintf(err_message_, sizeof(err_message_),
  1080. "rawdata not init yet");
  1081. return (-1);
  1082. }
  1083. offset_ = row_offset_;
  1084. if (offset_ >= get_data_size()) {
  1085. snprintf(err_message_, sizeof(err_message_),
  1086. "already at end of data");
  1087. return (-2);
  1088. }
  1089. SKIP_TREE_SIZE(sizeof(unsigned char)); // flag
  1090. for (int j = key_index_ + 1; j <= stRow.num_fields();
  1091. j++) //拷贝一行数据
  1092. {
  1093. //id: bug fix skip discard
  1094. if (stRow.table_definition()->is_discard(j))
  1095. continue;
  1096. int temp = 0;
  1097. switch (stRow.field_type(j)) {
  1098. case DField::Unsigned:
  1099. case DField::Signed:
  1100. GET_TREE_VALUE_AT_OFFSET(temp, int, offset_);
  1101. if (stRow.field_size(j) > (int)sizeof(int32_t))
  1102. SKIP_TREE_SIZE(sizeof(int64_t));
  1103. else
  1104. SKIP_TREE_SIZE(sizeof(int32_t));
  1105. ;
  1106. break;
  1107. case DField::Float: //浮点数
  1108. if (stRow.field_size(j) > (int)sizeof(float))
  1109. SKIP_TREE_SIZE(sizeof(double));
  1110. else
  1111. SKIP_TREE_SIZE(sizeof(float));
  1112. break;
  1113. case DField::String: //字符串
  1114. case DField::Binary: //二进制数据
  1115. default: {
  1116. int iLen;
  1117. GET_TREE_VALUE(iLen, int);
  1118. SKIP_TREE_SIZE(iLen);
  1119. break;
  1120. }
  1121. } //end of switch
  1122. }
  1123. return (0);
  1124. ERROR_RET:
  1125. snprintf(err_message_, sizeof(err_message_), "skip row error");
  1126. return (-100);
  1127. }
  1128. int TreeData::replace_cur_row(const RowValue &stRow, bool isDirty,
  1129. MEM_HANDLE_T *hRecord)
  1130. {
  1131. int iRet = 0;
  1132. ALLOC_SIZE_T uiOldOffset;
  1133. ALLOC_SIZE_T uiNextRowsSize;
  1134. ALLOC_SIZE_T uiNewRowSize = 0;
  1135. ALLOC_SIZE_T uiCurRowSize = 0;
  1136. ALLOC_SIZE_T uiNextRowsOffset;
  1137. ALLOC_SIZE_T uiDataSize = get_data_size();
  1138. uiOldOffset = offset_;
  1139. if ((iRet = skip_row(stRow)) != 0) {
  1140. goto ERROR_RET;
  1141. }
  1142. unsigned char uchRowFlag;
  1143. GET_TREE_VALUE_AT_OFFSET(uchRowFlag, unsigned char, row_offset_);
  1144. if (isDirty)
  1145. uchRowFlag = OPER_UPDATE;
  1146. uiNewRowSize = calc_tree_row_size(stRow, key_index_);
  1147. uiCurRowSize = offset_ - row_offset_;
  1148. uiNextRowsOffset = offset_;
  1149. uiNextRowsSize = uiDataSize - offset_;
  1150. if (uiNewRowSize > uiCurRowSize) {
  1151. // enlarge buffer
  1152. MEM_HANDLE_T hTmp = mallocator_->ReAlloc(
  1153. *hRecord, uiDataSize + uiNewRowSize - uiCurRowSize);
  1154. if (hTmp == INVALID_HANDLE) {
  1155. snprintf(err_message_, sizeof(err_message_),
  1156. "realloc error");
  1157. need_new_bufer_size =
  1158. uiDataSize + uiNewRowSize - uiCurRowSize;
  1159. iRet = EC_NO_MEM;
  1160. goto ERROR_RET;
  1161. }
  1162. p_tree_root_->tree_size_ -= size_;
  1163. *hRecord = hTmp;
  1164. size_ = mallocator_->chunk_size(*hRecord);
  1165. p_tree_root_->tree_size_ += size_;
  1166. p_content_ = Pointer<char>(*hRecord);
  1167. // move data
  1168. if (uiNextRowsSize > 0)
  1169. memmove(p_content_ + uiNextRowsOffset +
  1170. (uiNewRowSize - uiCurRowSize),
  1171. p_content_ + uiNextRowsOffset, uiNextRowsSize);
  1172. // copy new row
  1173. offset_ = row_offset_;
  1174. iRet = encode_tree_row(stRow, uchRowFlag);
  1175. if (iRet != 0) {
  1176. if (uiNextRowsSize > 0)
  1177. memmove(p_content_ + uiNextRowsOffset,
  1178. p_content_ + uiNextRowsOffset +
  1179. (uiNewRowSize - uiCurRowSize),
  1180. uiNextRowsSize);
  1181. iRet = -1;
  1182. goto ERROR_RET;
  1183. }
  1184. } else {
  1185. // back up old row
  1186. void *pTmpBuf = MALLOC(uiCurRowSize);
  1187. if (pTmpBuf == NULL) {
  1188. need_new_bufer_size = uiCurRowSize;
  1189. snprintf(err_message_, sizeof(err_message_),
  1190. "malloc error: %m");
  1191. return (-ENOMEM);
  1192. }
  1193. memmove(pTmpBuf, p_content_ + row_offset_, uiCurRowSize);
  1194. // copy new row
  1195. offset_ = row_offset_;
  1196. iRet = encode_tree_row(stRow, uchRowFlag);
  1197. if (iRet != 0) {
  1198. memmove(p_content_ + row_offset_, pTmpBuf,
  1199. uiCurRowSize);
  1200. FREE(pTmpBuf);
  1201. iRet = -1;
  1202. goto ERROR_RET;
  1203. }
  1204. // move data
  1205. if (uiNextRowsSize > 0 && offset_ != uiNextRowsOffset)
  1206. memmove(p_content_ + offset_,
  1207. p_content_ + uiNextRowsOffset, uiNextRowsSize);
  1208. FREE(pTmpBuf);
  1209. // shorten buffer
  1210. MEM_HANDLE_T hTmp = mallocator_->ReAlloc(
  1211. *hRecord, uiDataSize + uiNewRowSize - uiCurRowSize);
  1212. if (hTmp != INVALID_HANDLE) {
  1213. p_tree_root_->tree_size_ -= size_;
  1214. *hRecord = hTmp;
  1215. size_ = mallocator_->chunk_size(*hRecord);
  1216. p_tree_root_->tree_size_ += size_;
  1217. p_content_ = Pointer<char>(*hRecord);
  1218. }
  1219. }
  1220. set_data_size(uiDataSize - uiCurRowSize + uiNewRowSize);
  1221. p_tree_root_->total_raw_size_ += (uiNewRowSize - uiCurRowSize);
  1222. ERROR_RET:
  1223. offset_ = uiOldOffset + uiNewRowSize - uiCurRowSize;
  1224. return (iRet);
  1225. }
  1226. int TreeData::delete_cur_row(const RowValue &stRow)
  1227. {
  1228. int iRet = 0;
  1229. ALLOC_SIZE_T uiOldOffset;
  1230. ALLOC_SIZE_T uiNextRowsSize;
  1231. uiOldOffset = offset_;
  1232. if ((iRet = skip_row(stRow)) != 0) {
  1233. log4cplus_error("skip error: %d,%s", iRet, get_err_msg());
  1234. goto ERROR_RET;
  1235. }
  1236. uiNextRowsSize = get_data_size() - offset_;
  1237. memmove(p_content_ + row_offset_, p_content_ + offset_, uiNextRowsSize);
  1238. set_row_count(get_row_count() - 1);
  1239. set_data_size(get_data_size() - (offset_ - row_offset_));
  1240. p_tree_root_->row_count_--;
  1241. p_tree_root_->total_raw_size_ -= (offset_ - row_offset_);
  1242. offset_ = row_offset_;
  1243. return (iRet);
  1244. ERROR_RET:
  1245. offset_ = uiOldOffset;
  1246. return (iRet);
  1247. }
  1248. int TreeData::get_sub_raw_data(DTCJobOperation &job_op, MEM_HANDLE_T hRecord)
  1249. {
  1250. // int laid = job_op.flag_no_cache() ? -1 : job_op.table_definition()->lastacc_field_id();
  1251. if (job_op.result_full())
  1252. return 0;
  1253. DTCTableDefinition *stpNodeTab, *stpTaskTab;
  1254. RowValue *stpNodeRow, *stpTaskRow;
  1255. stpNodeTab = p_table_;
  1256. stpTaskTab = job_op.table_definition();
  1257. RowValue stNodeRow(stpNodeTab);
  1258. RowValue stTaskRow(stpTaskTab);
  1259. if (stpNodeTab == stpTaskTab) {
  1260. stpNodeRow = &stTaskRow;
  1261. stpTaskRow = &stTaskRow;
  1262. } else {
  1263. stpNodeRow = &stNodeRow;
  1264. stpTaskRow = &stTaskRow;
  1265. }
  1266. p_content_ = Pointer<char>(hRecord);
  1267. uint32_t rows = get_row_count();
  1268. offset_ = sizeof(unsigned char) + sizeof(uint32_t) * 2;
  1269. size_ = mallocator_->chunk_size(hRecord);
  1270. unsigned char uchRowFlags;
  1271. for (unsigned int j = 0; j < rows; j++) {
  1272. job_op.update_key(
  1273. *stpNodeRow); // use stpNodeRow is fine, as just modify key field
  1274. if ((decode_tree_row(*stpNodeRow, uchRowFlags, 0)) != 0) {
  1275. return (-2);
  1276. }
  1277. // this pointer compare is ok, as these two is both come from tabledefmanager. if they mean same, they are same object.
  1278. if (stpNodeTab != stpTaskTab) {
  1279. stpTaskRow->Copy(stpNodeRow);
  1280. }
  1281. if (job_op.compare_row(*stpTaskRow) == 0) //如果不符合查询条件
  1282. continue;
  1283. if (stpTaskTab->expire_time_field_id() > 0)
  1284. stpTaskRow->update_expire_time();
  1285. //当前行添加到task中
  1286. log4cplus_debug("append_row flag");
  1287. job_op.append_row(stpTaskRow);
  1288. if (job_op.all_rows() && job_op.result_full()) {
  1289. job_op.set_total_rows((int)rows);
  1290. break;
  1291. }
  1292. }
  1293. return 0;
  1294. }
  1295. int TreeData::get_sub_raw(DTCJobOperation &job_op, unsigned int nodeCnt,
  1296. bool isAsc, SubRowProcess subRowProc)
  1297. {
  1298. pResCookie resCookie;
  1299. MEM_HANDLE_T pCookie[nodeCnt];
  1300. resCookie.p_handle = pCookie;
  1301. if (job_op.all_rows() &&
  1302. job_op.requestInfo.limit_count() >
  1303. 0) //condition: ONLY `LIMIT` without `WHERE`
  1304. resCookie.need_find_node_count =
  1305. job_op.requestInfo.limit_start() +
  1306. job_op.requestInfo.limit_count();
  1307. else
  1308. resCookie.need_find_node_count = 0;
  1309. t_tree_.traverse_forward(Visit, &resCookie);
  1310. if (isAsc) //升序
  1311. {
  1312. for (int i = 0; i < (int)resCookie.has_got_node_count; i++) {
  1313. int iRet = (this->*subRowProc)(job_op, pCookie[i]);
  1314. if (iRet != 0)
  1315. return iRet;
  1316. }
  1317. } else //降序
  1318. {
  1319. for (int i = (int)resCookie.has_got_node_count - 1; i >= 0;
  1320. i--) {
  1321. int iRet = (this->*subRowProc)(job_op, pCookie[i]);
  1322. if (iRet != 0)
  1323. return iRet;
  1324. }
  1325. }
  1326. return 0;
  1327. }
  1328. int TreeData::match_index_condition(DTCJobOperation &job_op,
  1329. unsigned int NodeCnt,
  1330. SubRowProcess subRowProc)
  1331. {
  1332. const DTCFieldValue *condition = job_op.request_condition();
  1333. int numfields = 0; //条件字段个数
  1334. bool isAsc = !(p_table_->is_desc_order(TTREE_INDEX_POS));
  1335. if (condition)
  1336. numfields = condition->num_fields();
  1337. int indexIdArr[numfields]; //开辟空间比实际使用的大
  1338. int indexCount = 0; //条件索引个数
  1339. int firstEQIndex = -1; //第一个EQ在indexIdArr中的位置
  1340. for (int i = 0; i < numfields; i++) {
  1341. if (condition->field_id(i) == TTREE_INDEX_POS) {
  1342. if (firstEQIndex == -1 &&
  1343. condition->field_operation(i) == DField::EQ)
  1344. firstEQIndex = i;
  1345. indexIdArr[indexCount++] = i;
  1346. }
  1347. }
  1348. if (indexCount == 0 ||
  1349. (indexCount == 1 && condition->field_operation(indexIdArr[0]) ==
  1350. DField::NE)) { //平板类型
  1351. int iret = get_sub_raw(job_op, NodeCnt, isAsc, subRowProc);
  1352. if (iret != 0)
  1353. return iret;
  1354. } else if (firstEQIndex != -1) //有至少一个EQ条件
  1355. {
  1356. MEM_HANDLE_T *pRecord = NULL;
  1357. char *indexKey = reinterpret_cast<char *>(
  1358. condition->field_value(firstEQIndex));
  1359. CmpCookie cookie(p_table_, TTREE_INDEX_POS);
  1360. int iRet =
  1361. t_tree_.do_find(indexKey, &cookie, KeyCompare, pRecord);
  1362. if (iRet == -100)
  1363. return iRet;
  1364. if (pRecord != NULL) {
  1365. iRet = (this->*subRowProc)(job_op, *pRecord);
  1366. if (iRet != 0)
  1367. return iRet;
  1368. }
  1369. } else {
  1370. int leftId = -1;
  1371. int rightId = -1;
  1372. for (int i = 0; i < indexCount; i++) {
  1373. switch (condition->field_operation(indexIdArr[i])) {
  1374. case DField::LT:
  1375. case DField::LE:
  1376. if (rightId == -1)
  1377. rightId = indexIdArr[i];
  1378. break;
  1379. case DField::GT:
  1380. case DField::GE:
  1381. if (leftId == -1)
  1382. leftId = indexIdArr[i];
  1383. break;
  1384. default:
  1385. break;
  1386. }
  1387. }
  1388. if (leftId != -1 && rightId == -1) //GE
  1389. {
  1390. pResCookie resCookie;
  1391. MEM_HANDLE_T pCookie[NodeCnt];
  1392. resCookie.p_handle = pCookie;
  1393. resCookie.need_find_node_count = 0;
  1394. char *indexKey = reinterpret_cast<char *>(
  1395. condition->field_value(leftId));
  1396. CmpCookie cookie(p_table_, TTREE_INDEX_POS);
  1397. if (t_tree_.traverse_forward(indexKey, &cookie,
  1398. KeyCompare, Visit,
  1399. &resCookie) != 0) {
  1400. snprintf(err_message_, sizeof(err_message_),
  1401. " traverse tree-data rows error");
  1402. return (-1);
  1403. }
  1404. if (isAsc) {
  1405. for (int i = 0;
  1406. i < (int)resCookie.has_got_node_count;
  1407. i++) {
  1408. int iRet = (this->*subRowProc)(
  1409. job_op, pCookie[i]);
  1410. if (iRet != 0)
  1411. return iRet;
  1412. }
  1413. } else {
  1414. for (int i = (int)resCookie.has_got_node_count -
  1415. 1;
  1416. i >= 0; i--) {
  1417. int iRet = (this->*subRowProc)(
  1418. job_op, pCookie[i]);
  1419. if (iRet != 0)
  1420. return iRet;
  1421. }
  1422. }
  1423. } else if (leftId == -1 && rightId != -1) //LE
  1424. {
  1425. pResCookie resCookie;
  1426. MEM_HANDLE_T pCookie[NodeCnt];
  1427. resCookie.p_handle = pCookie;
  1428. resCookie.need_find_node_count = NodeCnt;
  1429. char *indexKey = reinterpret_cast<char *>(
  1430. condition->field_value(rightId));
  1431. CmpCookie cookie(p_table_, TTREE_INDEX_POS);
  1432. if (t_tree_.traverse_backward(indexKey, &cookie,
  1433. KeyCompare, Visit,
  1434. &resCookie) != 0) {
  1435. snprintf(err_message_, sizeof(err_message_),
  1436. " traverse tree-data rows error");
  1437. return (-1);
  1438. }
  1439. if (isAsc) {
  1440. for (int i = (int)resCookie.has_got_node_count -
  1441. 1;
  1442. i >= 0; i--) {
  1443. int iRet = (this->*subRowProc)(
  1444. job_op, pCookie[i]);
  1445. if (iRet != 0)
  1446. return iRet;
  1447. }
  1448. } else {
  1449. for (int i = 0;
  1450. i < (int)resCookie.has_got_node_count;
  1451. i++) {
  1452. int iRet = (this->*subRowProc)(
  1453. job_op, pCookie[i]);
  1454. if (iRet != 0)
  1455. return iRet;
  1456. }
  1457. }
  1458. } else if (leftId != -1 && rightId != -1) //range
  1459. {
  1460. pResCookie resCookie;
  1461. MEM_HANDLE_T pCookie[NodeCnt];
  1462. resCookie.p_handle = pCookie;
  1463. resCookie.need_find_node_count = 0;
  1464. char *beginKey = reinterpret_cast<char *>(
  1465. condition->field_value(leftId));
  1466. char *endKey = reinterpret_cast<char *>(
  1467. condition->field_value(rightId));
  1468. CmpCookie cookie(p_table_, TTREE_INDEX_POS);
  1469. if (t_tree_.traverse_forward(beginKey, endKey, &cookie,
  1470. KeyCompare, Visit,
  1471. &resCookie) != 0) {
  1472. snprintf(err_message_, sizeof(err_message_),
  1473. " traverse tree-data rows error");
  1474. return (-1);
  1475. }
  1476. if (isAsc) {
  1477. for (int i = 0;
  1478. i < (int)resCookie.has_got_node_count;
  1479. i++) {
  1480. int iRet = (this->*subRowProc)(
  1481. job_op, pCookie[i]);
  1482. if (iRet != 0)
  1483. return iRet;
  1484. }
  1485. } else {
  1486. for (int i = (int)resCookie.has_got_node_count -
  1487. 1;
  1488. i >= 0; i--) {
  1489. int iRet = (this->*subRowProc)(
  1490. job_op, pCookie[i]);
  1491. if (iRet != 0)
  1492. return iRet;
  1493. }
  1494. }
  1495. } else //may all NE, raw data process
  1496. {
  1497. int iret =
  1498. get_sub_raw(job_op, NodeCnt, isAsc, subRowProc);
  1499. if (iret != 0)
  1500. return iret;
  1501. }
  1502. }
  1503. return 0;
  1504. }
  1505. int TreeData::get_dirty_row_count()
  1506. {
  1507. unsigned int uiTotalNodes = p_tree_root_->node_count_;
  1508. int dirty_rows = 0;
  1509. pResCookie resCookie;
  1510. MEM_HANDLE_T pCookie[uiTotalNodes];
  1511. resCookie.p_handle = pCookie;
  1512. resCookie.need_find_node_count = 0;
  1513. RowValue stRow(p_table_);
  1514. t_tree_.traverse_forward(Visit, &resCookie);
  1515. for (int i = 0; i < (int)resCookie.has_got_node_count; i++) {
  1516. p_content_ = Pointer<char>(pCookie[i]);
  1517. uint32_t rows = get_row_count();
  1518. offset_ = sizeof(unsigned char) + sizeof(uint32_t) * 2;
  1519. size_ = mallocator_->chunk_size(pCookie[i]);
  1520. unsigned char uchRowFlags;
  1521. for (unsigned int j = 0; j < rows; j++) {
  1522. if (decode_tree_row(stRow, uchRowFlags, 0) != 0) {
  1523. log4cplus_error(
  1524. "subraw-data decode row error: %s",
  1525. get_err_msg());
  1526. return (-1);
  1527. }
  1528. if (uchRowFlags & OPER_DIRTY)
  1529. dirty_rows++;
  1530. }
  1531. }
  1532. return dirty_rows;
  1533. }
  1534. int TreeData::flush_tree_data(DTCFlushRequest *flush_req, Node *p_node,
  1535. unsigned int &affected_count)
  1536. {
  1537. unsigned int uiTotalNodes = p_tree_root_->node_count_;
  1538. affected_count = 0;
  1539. DTCValue astKey[p_table_->key_fields()];
  1540. TaskPackedKey::unpack_key(p_table_, key(), astKey);
  1541. RowValue stRow(p_table_); //一行数据
  1542. for (int i = 0; i < p_table_->key_fields(); i++)
  1543. stRow[i] = astKey[i];
  1544. rows_count_ = 0;
  1545. dirty_rows_count_ = 0;
  1546. pResCookie resCookie;
  1547. MEM_HANDLE_T pCookie[uiTotalNodes];
  1548. resCookie.p_handle = pCookie;
  1549. resCookie.need_find_node_count = 0;
  1550. t_tree_.traverse_forward(Visit, &resCookie);
  1551. for (int i = 0; i < (int)resCookie.has_got_node_count; i++) {
  1552. p_content_ = Pointer<char>(pCookie[i]);
  1553. uint32_t rows = get_row_count();
  1554. offset_ = sizeof(unsigned char) + sizeof(uint32_t) * 2;
  1555. size_ = mallocator_->chunk_size(pCookie[i]);
  1556. unsigned char uchRowFlags;
  1557. for (unsigned int j = 0; j < rows; j++) {
  1558. if (decode_tree_row(stRow, uchRowFlags, 0) != 0) {
  1559. log4cplus_error(
  1560. "subraw-data decode row error: %s",
  1561. get_err_msg());
  1562. return (-1);
  1563. }
  1564. if ((uchRowFlags & OPER_DIRTY) == false)
  1565. continue;
  1566. if (flush_req && flush_req->flush_row(stRow) != 0) {
  1567. log4cplus_error(
  1568. "do_flush() invoke flushRow() failed.");
  1569. return (-2);
  1570. }
  1571. set_cur_row_flag(uchRowFlags & ~OPER_DIRTY);
  1572. dirty_rows_count_--;
  1573. affected_count++;
  1574. }
  1575. }
  1576. return 0;
  1577. }
  1578. int TreeData::get_tree_data(DTCJobOperation &job_op)
  1579. {
  1580. uint32_t rowCnt = p_tree_root_->row_count_;
  1581. if (rowCnt == 0) {
  1582. return 0;
  1583. }
  1584. job_op.prepare_result(); //准备返回结果对象
  1585. if (job_op.all_rows() &&
  1586. (job_op.count_only() || !job_op.in_range((int)rowCnt, 0))) {
  1587. if (job_op.is_batch_request()) {
  1588. if ((int)rowCnt > 0)
  1589. job_op.add_total_rows((int)rowCnt);
  1590. } else {
  1591. job_op.set_total_rows((int)rowCnt);
  1592. }
  1593. } else {
  1594. int iret =
  1595. match_index_condition(job_op, p_tree_root_->node_count_,
  1596. &TreeData::get_sub_raw_data);
  1597. if (iret != 0)
  1598. return iret;
  1599. }
  1600. return 0;
  1601. }
  1602. int TreeData::update_tree_data(DTCJobOperation &job_op, Node *p_node,
  1603. RawData *affected_data, bool async, bool setrows)
  1604. {
  1605. uint32_t rowCnt = p_tree_root_->node_count_;
  1606. if (rowCnt == 0) {
  1607. return 0;
  1608. }
  1609. m_pstNode = p_node;
  1610. m_async = async;
  1611. dirty_rows_count_ = 0;
  1612. return match_index_condition(job_op, rowCnt,
  1613. &TreeData::update_sub_raw_data);
  1614. }
  1615. int TreeData::delete_tree_data(DTCJobOperation &job_op)
  1616. {
  1617. uint32_t rowCnt = p_tree_root_->node_count_;
  1618. if (rowCnt == 0) {
  1619. return 0;
  1620. }
  1621. rows_count_ = 0;
  1622. dirty_rows_count_ = 0;
  1623. job_op.prepare_result(); //准备返回结果对象
  1624. if (job_op.all_rows() &&
  1625. (job_op.count_only() || !job_op.in_range((int)rowCnt, 0))) {
  1626. if (job_op.is_batch_request()) {
  1627. if ((int)rowCnt > 0)
  1628. job_op.add_total_rows((int)rowCnt);
  1629. } else {
  1630. job_op.set_total_rows((int)rowCnt);
  1631. }
  1632. } else {
  1633. int iret = match_index_condition(
  1634. job_op, rowCnt, &TreeData::delete_sub_raw_data);
  1635. if (iret != 0)
  1636. return iret;
  1637. }
  1638. return 0;
  1639. }
  1640. int TreeData::get_expire_time(DTCTableDefinition *t, uint32_t &expire)
  1641. {
  1642. expire = 0;
  1643. if (unlikely(handle_ == INVALID_HANDLE)) {
  1644. snprintf(err_message_, sizeof(err_message_),
  1645. "root tree data not init yet");
  1646. return (-1);
  1647. }
  1648. if (expire_id_ == -1) {
  1649. expire = 0;
  1650. return 0;
  1651. }
  1652. MEM_HANDLE_T root = get_tree_root();
  1653. if (unlikely(root == INVALID_HANDLE)) {
  1654. snprintf(err_message_, sizeof(err_message_),
  1655. "root tree data not init yet");
  1656. return (-1);
  1657. }
  1658. MEM_HANDLE_T firstHanle = t_tree_.first_node();
  1659. if (unlikely(firstHanle == INVALID_HANDLE)) {
  1660. snprintf(err_message_, sizeof(err_message_),
  1661. "root tree data not init yet");
  1662. return (-1);
  1663. }
  1664. offset_ = 0;
  1665. size_ = mallocator_->chunk_size(firstHanle);
  1666. p_content_ = Pointer<char>(firstHanle);
  1667. SKIP_TREE_SIZE(sizeof(unsigned char));
  1668. for (int j = key_index_ + 1; j <= p_table_->num_fields(); j++) {
  1669. if (j == expire_id_) {
  1670. expire = *((uint32_t *)(p_content_ + offset_));
  1671. break;
  1672. }
  1673. switch (p_table_->field_type(j)) {
  1674. case DField::Unsigned:
  1675. case DField::Signed:
  1676. if (p_table_->field_size(j) > (int)sizeof(int32_t))
  1677. SKIP_TREE_SIZE(sizeof(int64_t));
  1678. else
  1679. SKIP_TREE_SIZE(sizeof(int32_t));
  1680. ;
  1681. break;
  1682. case DField::Float:
  1683. if (p_table_->field_size(j) > (int)sizeof(float))
  1684. SKIP_TREE_SIZE(sizeof(double));
  1685. else
  1686. SKIP_TREE_SIZE(sizeof(float));
  1687. break;
  1688. case DField::String:
  1689. case DField::Binary:
  1690. default:
  1691. uint32_t iLen = 0;
  1692. GET_TREE_VALUE(iLen, int);
  1693. SKIP_TREE_SIZE(iLen);
  1694. break;
  1695. } //end of switch
  1696. }
  1697. return 0;
  1698. offset_ = 0;
  1699. size_ = 0;
  1700. p_content_ = NULL;
  1701. ERROR_RET:
  1702. snprintf(err_message_, sizeof(err_message_), "get expire error");
  1703. return (-100);
  1704. }
  1705. ALLOC_SIZE_T TreeData::calc_tree_row_size(const RowValue &stRow, int keyIdx)
  1706. {
  1707. if (keyIdx == -1)
  1708. log4cplus_error("TreeData may not init yet...");
  1709. ALLOC_SIZE_T tSize = 1; // flag
  1710. for (int j = keyIdx + 1; j <= stRow.num_fields(); j++) //¿½±´Ò»ÐÐÊý¾Ý
  1711. {
  1712. if (stRow.table_definition()->is_discard(j))
  1713. continue;
  1714. switch (stRow.field_type(j)) {
  1715. case DField::Signed:
  1716. case DField::Unsigned:
  1717. tSize += unlikely(stRow.field_size(j) >
  1718. (int)sizeof(int32_t)) ?
  1719. sizeof(int64_t) :
  1720. sizeof(int32_t);
  1721. break;
  1722. case DField::Float: //¸¡µãÊý
  1723. tSize += likely(stRow.field_size(j) >
  1724. (int)sizeof(float)) ?
  1725. sizeof(double) :
  1726. sizeof(float);
  1727. break;
  1728. case DField::String: //×Ö·û´®
  1729. case DField::Binary: //¶þ½øÖÆÊý¾Ý
  1730. default: {
  1731. tSize += sizeof(int);
  1732. tSize += stRow.field_value(j)->bin.len;
  1733. break;
  1734. }
  1735. } //end of switch
  1736. }
  1737. if (tSize < 2)
  1738. log4cplus_info("key_index_:%d, stRow.num_fields():%d tSize:%d",
  1739. keyIdx, stRow.num_fields(), tSize);
  1740. return (tSize);
  1741. }
  1742. int TreeData::destroy_sub_tree()
  1743. {
  1744. t_tree_.destory();
  1745. p_tree_root_->row_count_ = 0;
  1746. p_tree_root_->root_handle_ = INVALID_HANDLE;
  1747. p_tree_root_->tree_size_ = 0;
  1748. p_tree_root_->total_raw_size_ = 0;
  1749. p_tree_root_->node_count_ = 0;
  1750. return 0;
  1751. }
  1752. unsigned int TreeData::get_row_count()
  1753. {
  1754. return *(uint32_t *)(p_content_ + sizeof(unsigned char) +
  1755. sizeof(uint32_t));
  1756. }
  1757. unsigned int TreeData::get_data_size()
  1758. {
  1759. return *(uint32_t *)(p_content_ + sizeof(unsigned char));
  1760. }
  1761. int TreeData::set_row_count(unsigned int count)
  1762. {
  1763. SET_TREE_VALUE_AT_OFFSET(count, uint32_t,
  1764. sizeof(unsigned char) + sizeof(uint32_t));
  1765. ERROR_RET:
  1766. snprintf(err_message_, sizeof(err_message_), "set data rowcount error");
  1767. return (-100);
  1768. }
  1769. int TreeData::set_data_size(unsigned int data_size)
  1770. {
  1771. SET_TREE_VALUE_AT_OFFSET(data_size, uint32_t, sizeof(unsigned char));
  1772. ERROR_RET:
  1773. snprintf(err_message_, sizeof(err_message_), "set data size error");
  1774. return (-100);
  1775. }
  1776. int TreeData::set_cur_row_flag(unsigned char uchFlag)
  1777. {
  1778. if (row_offset_ >= get_data_size()) {
  1779. snprintf(err_message_, sizeof(err_message_), "no more rows");
  1780. return (-1);
  1781. }
  1782. *(unsigned char *)(p_content_ + row_offset_) = uchFlag;
  1783. return (0);
  1784. }