Opera 12.15 Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BTreeBase.cpp 59KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. **
  3. ** Copyright (C) 1995-2011 Opera Software ASA. All rights reserved.
  4. **
  5. ** This file is part of the Opera web browser.
  6. ** It may not be distributed under any circumstances.
  7. */
  8. #include "core/pch.h"
  9. #ifdef SEARCH_ENGINE
  10. #include "modules/search_engine/BTreeBase.h"
  11. #ifdef _DEBUG
  12. #define CHECK_INTEGRITY(branch) OP_ASSERT(CheckIntegrity(branch))
  13. #else
  14. #define CHECK_INTEGRITY(branch)
  15. #endif
  16. static int BinarySearch(const BTreeBase::BTreeBranch *branch, const void *item, const TypeDescriptor &m_allocator)
  17. {
  18. int n2, start, end;
  19. start = 0;
  20. end = branch->size;
  21. while (end > start)
  22. {
  23. n2 = (end - start) / 2;
  24. if (m_allocator.Compare(branch->GetItem(start + n2), item))
  25. start = start + n2 + 1;
  26. else
  27. end = start + n2;
  28. }
  29. return start;
  30. }
  31. BTreeBase::BTreeBranch::BTreeBranch(PoolBase *cache, OpFileLength id, BTreeBranch *rbranch, unsigned short nur)
  32. {
  33. parent = rbranch;
  34. size = 0;
  35. disk_id = (BSCache::Item::DiskId)id;
  36. modified = disk_id < 0;
  37. NUR_mark = nur;
  38. owner = NULL;
  39. this->cache = cache;
  40. }
  41. BTreeBase::BTreeBranch::~BTreeBranch() {
  42. OP_DELETEA(data);
  43. }
  44. OP_STATUS BTreeBase::BTreeBranch::Construct(BlockStorage *storage) {
  45. RETURN_OOM_IF_NULL(data = OP_NEWA(char, storage->GetBlockSize() - 12));
  46. Rightmost() = 0;
  47. return OpStatus::OK;
  48. }
  49. OP_STATUS BTreeBase::BTreeBranch::Read(BlockStorage *storage)
  50. {
  51. int data_size;
  52. OpFileLength pos = ((OpFileLength)disk_id) * storage->GetBlockSize();
  53. data_size = storage->DataLength(pos);
  54. if (data_size > storage->GetBlockSize() - 12 || data_size < (int)sizeof(BSCache::Item::DiskId))
  55. {
  56. OP_ASSERT(0); // invalid data
  57. return OpStatus::ERR_PARSING_FAILED;
  58. }
  59. if (!storage->Read(this->Data(), data_size, pos))
  60. return OpStatus::ERR_NO_ACCESS;
  61. this->size = (data_size - sizeof(BSCache::Item::DiskId)) / PtrItemSize();
  62. return OpStatus::OK;
  63. }
  64. OP_STATUS BTreeBase::BTreeBranch::Flush(BlockStorage *storage)
  65. {
  66. OpFileLength did;
  67. BSCache::Item::DiskId old_id;
  68. OP_ASSERT(this->DataSize() <= storage->GetBlockSize() - 12);
  69. if (this->disk_id > 0 && !this->id_reserved) // update
  70. {
  71. if (!storage->Update(this->Data(), this->DataSize(),
  72. ((OpFileLength)disk_id) * storage->GetBlockSize()))
  73. return OpStatus::ERR_NO_DISK;
  74. }
  75. else { // create new
  76. old_id = this->disk_id;
  77. if (this->id_reserved)
  78. {
  79. if ((did = storage->Write(this->Data(), this->DataSize(),
  80. ((OpFileLength)this->disk_id) * storage->GetBlockSize()) / storage->GetBlockSize()) == 0)
  81. return OpStatus::ERR_NO_DISK;
  82. this->id_reserved = FALSE;
  83. }
  84. else {
  85. if ((did = storage->Write(this->Data(), this->DataSize()) / storage->GetBlockSize()) == 0)
  86. return OpStatus::ERR_NO_DISK;
  87. }
  88. this->disk_id = (BSCache::Item::DiskId)did;
  89. OnIdChange(disk_id, old_id);
  90. }
  91. this->modified = FALSE;
  92. return OpStatus::OK;
  93. }
  94. void BTreeBase::BTreeBranch::OnIdChange(DiskId new_id, DiskId old_id)
  95. {
  96. int pos;
  97. if (this->parent == NULL && old_id < 0 && (this->owner->m_root == 0 || this->owner->m_root == old_id))
  98. this->owner->m_root = new_id;
  99. if (this->parent != NULL && old_id != new_id)
  100. {
  101. if (this->size == 0)
  102. pos = this->parent->size; // This should *only* happen if btree-size==3 and insert-pos==3
  103. else
  104. pos = BinarySearch(this->parent, this->GetItem(0), this->cache->m_allocator);
  105. if (pos < this->parent->size)
  106. {
  107. if (this->parent->Pointer(pos) == old_id)
  108. this->parent->Pointer(pos) = new_id;
  109. }
  110. else {
  111. if (this->parent->Rightmost() == old_id)
  112. this->parent->Rightmost() = new_id;
  113. }
  114. }
  115. }
  116. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  117. size_t BTreeBase::BTreeBranch::EstimateMemoryUsed() const
  118. {
  119. return Item::EstimateMemoryUsed() +
  120. sizeof(owner) +
  121. sizeof(size) +
  122. sizeof(parent) +
  123. sizeof(cache) +
  124. sizeof(data) +
  125. cache->m_storage.GetBlockSize() - 12;
  126. }
  127. #endif
  128. void BTreeBase::BTreeBranch::CopyData(const BTreeBase::BTreeBranch* src)
  129. {
  130. op_memcpy(Data(), src->Data(), src->DataSize());
  131. size = src->size;
  132. }
  133. inline void CopyPtrItems(BTreeBase::BTreeBranch* dstBranch, int dstPos,
  134. const BTreeBase::BTreeBranch* srcBranch, int srcPos, int n)
  135. {
  136. op_memmove(dstBranch->PtrItem(dstPos), srcBranch->PtrItem(srcPos), n * srcBranch->PtrItemSize());
  137. }
  138. inline void CopyItem(BTreeBase::BTreeBranch* dstBranch, int dstPos,
  139. const BTreeBase::BTreeBranch* srcBranch, int srcPos)
  140. {
  141. op_memcpy(dstBranch->GetItem(dstPos), srcBranch->GetItem(srcPos), srcBranch->ItemSize());
  142. }
  143. inline void CopyItem(void* dst, const BTreeBase::BTreeBranch* srcBranch, int srcPos)
  144. {
  145. op_memcpy(dst, srcBranch->GetItem(srcPos), srcBranch->ItemSize());
  146. }
  147. inline void CopyItem(BTreeBase::BTreeBranch* dstBranch, int dstPos, const void* src)
  148. {
  149. op_memcpy(dstBranch->GetItem(dstPos), src, dstBranch->ItemSize());
  150. }
  151. BTreeBase::BTreeBase(const TypeDescriptor &allocator, PoolBase *cache, BSCache::Item::DiskId root) : m_allocator(allocator)
  152. {
  153. m_root = root;
  154. m_cache = cache;
  155. m_reserved_id = 0;
  156. }
  157. BTreeBase::~BTreeBase(void)
  158. {
  159. OP_ASSERT(this->m_root >= 0); // closing this tree without knowing it's ID
  160. // m_cache->Flush();
  161. }
  162. BSCache::Item::DiskId BTreeBase::GetId(void)
  163. {
  164. if (this->m_root > 0)
  165. return this->m_root;
  166. if (this->m_root < 0)
  167. {
  168. BTreeBase::BTreeBranch *root;
  169. BSCache::Item::DiskId rv;
  170. #ifdef _DEBUG
  171. OP_ASSERT(OpStatus::IsSuccess(Goto(&root, this->m_root, NULL))); // cannot fail since root already is in memory
  172. #else
  173. OpStatus::Ignore(Goto(&root, this->m_root, NULL));
  174. #endif
  175. rv = SafePointer(root);
  176. GotoB(NULL, root);
  177. return rv;
  178. }
  179. if (m_reserved_id != 0)
  180. return (BSCache::Item::DiskId)(m_reserved_id / m_cache->m_storage.GetBlockSize());
  181. return (BSCache::Item::DiskId)(m_reserved_id = m_cache->m_storage.Reserve()) / m_cache->m_storage.GetBlockSize();
  182. }
  183. OP_STATUS BTreeBase::Insert(const void *item, BOOL overwrite_existing)
  184. {
  185. BTreeBranch *current;
  186. int pos;
  187. OP_BOOLEAN rv;
  188. OP_ASSERT(item != NULL);
  189. m_cache->m_NUR_mark = (m_cache->m_NUR_mark + 1) & NUR_MASK;
  190. if (this->m_root == 0)
  191. {
  192. RETURN_IF_ERROR(NewBranch(&current, NULL));
  193. RETURN_IF_ERROR(Modify(current));
  194. ++(current->size);
  195. CopyItem(current, 0, item);
  196. current->Pointer(0) = 0;
  197. GotoB(NULL, current);
  198. return OpStatus::OK;
  199. }
  200. RETURN_IF_ERROR(rv = SearchItem(&current, &pos, item));
  201. if (rv == OpBoolean::IS_TRUE)
  202. {
  203. if (overwrite_existing)
  204. {
  205. RETURN_IF_ERROR(Modify(current));
  206. CopyItem(current, pos, item);
  207. CHECK_INTEGRITY(current);
  208. }
  209. GotoB(NULL, current);
  210. return OpStatus::OK;
  211. }
  212. rv = InsertItem(current, pos, item);
  213. GotoB(NULL, current);
  214. return rv;
  215. }
  216. OP_BOOLEAN BTreeBase::Delete(const void *item)
  217. {
  218. BTreeBranch *current;
  219. int pos;
  220. OP_BOOLEAN rv;
  221. m_cache->m_NUR_mark = (m_cache->m_NUR_mark + 1) & NUR_MASK;
  222. if (this->m_root == 0)
  223. return OpBoolean::IS_FALSE;
  224. if ((rv = SearchItem(&current, &pos, item)) != OpBoolean::IS_TRUE)
  225. {
  226. GotoB(NULL, current);
  227. return rv;
  228. }
  229. if (current == NULL)
  230. return OpStatus::ERR;
  231. if (OpStatus::IsError(rv = DeleteItem(current, pos)))
  232. {
  233. GotoB(NULL, current);
  234. return rv;
  235. }
  236. GotoB(NULL, current);
  237. RETURN_IF_ERROR(RebalanceRoot());
  238. return OpBoolean::IS_TRUE;
  239. }
  240. OP_STATUS BTreeBase::Delete(const void *first, SearchOperator o1, const void *last, SearchOperator o2)
  241. {
  242. OpString next_item;
  243. BTreeBranch *current, *next;
  244. BSCache::Item::DiskId ptr;
  245. int pos, npos;
  246. OP_BOOLEAN rv;
  247. BOOL eod;
  248. m_cache->m_NUR_mark = (m_cache->m_NUR_mark + 1) & NUR_MASK;
  249. if (this->m_root == 0)
  250. return OpStatus::OK;
  251. RETURN_OOM_IF_NULL(next_item.Reserve((int)m_allocator.size));
  252. // find the position to delete from
  253. switch (o1)
  254. {
  255. case operatorGE:
  256. if (OpStatus::IsError(rv = SearchItem(&current, &pos, first)))
  257. {
  258. GotoB(NULL, current);
  259. return rv;
  260. }
  261. if (rv == OpBoolean::IS_FALSE && pos >= current->size)
  262. {
  263. OP_ASSERT(IsLeaf(current));
  264. GotoB(NULL, current);
  265. return OpStatus::OK;
  266. }
  267. break;
  268. case operatorGT:
  269. if (OpStatus::IsError(rv = SearchItem(&current, &pos, first)))
  270. {
  271. GotoB(NULL, current);
  272. return rv;
  273. }
  274. if (rv == OpBoolean::IS_FALSE && pos >= current->size)
  275. {
  276. OP_ASSERT(IsLeaf(current));
  277. GotoB(NULL, current);
  278. return OpStatus::OK;
  279. }
  280. if (rv == OpBoolean::IS_TRUE)
  281. {
  282. if (OpStatus::IsError(rv = SearchNextItem(&current, &pos, current->GetItem(pos))))
  283. {
  284. GotoB(NULL, current);
  285. return rv;
  286. }
  287. if (rv == OpBoolean::IS_FALSE)
  288. {
  289. GotoB(NULL, current);
  290. return OpStatus::OK;
  291. }
  292. }
  293. break;
  294. default:
  295. current = NULL;
  296. ptr = m_root;
  297. LoopDetector<BSCache::Item::DiskId> loopDetector;
  298. do {
  299. RETURN_IF_ERROR(loopDetector.CheckNext(ptr));
  300. RETURN_IF_ERROR(Goto(&current, ptr, current));
  301. ptr = current->Pointer(0);
  302. } while (!IsLeaf(current));
  303. if (current->size == 0) // empty
  304. {
  305. GotoB(NULL, current);
  306. return OpStatus::OK;
  307. }
  308. pos = 0;
  309. }
  310. if (current == NULL)
  311. return OpStatus::ERR;
  312. // delete the items
  313. while ((o2 != operatorLT && o2 != operatorLE) ||
  314. (o2 == operatorLE && !m_allocator.Compare(last, current->GetItem(pos))) ||
  315. m_allocator.Compare(current->GetItem(pos), last))
  316. {
  317. next = GotoB(current, NULL);
  318. npos = pos;
  319. if (OpStatus::IsError(rv = SearchNextItem(&next, &npos, current->GetItem(pos))))
  320. {
  321. GotoB(NULL, next);
  322. GotoB(NULL, current);
  323. return rv;
  324. }
  325. eod = rv == OpBoolean::IS_FALSE;
  326. if (!eod)
  327. CopyItem(next_item.DataPtr(), next, npos);
  328. GotoB(NULL, next);
  329. rv = DeleteItem(current, pos);
  330. GotoB(NULL, current);
  331. current = NULL;
  332. if (OpStatus::IsError(rv))
  333. return rv;
  334. RETURN_IF_ERROR(RebalanceRoot());
  335. if (eod)
  336. break;
  337. RETURN_IF_ERROR(SearchItem(&current, &pos, next_item.DataPtr()));
  338. }
  339. if (current != NULL)
  340. GotoB(NULL, current);
  341. return OpStatus::OK;
  342. }
  343. OP_BOOLEAN BTreeBase::Search(void *item)
  344. {
  345. BTreeBranch *current;
  346. int pos;
  347. OP_BOOLEAN rv;
  348. m_cache->m_NUR_mark = (m_cache->m_NUR_mark + 1) & NUR_MASK;
  349. if (this->m_root == 0)
  350. return OpBoolean::IS_FALSE;
  351. RETURN_IF_ERROR(rv = SearchItem(&current, &pos, item));
  352. // cannot be empty, since it was checked above
  353. while (pos >= current->size && current->parent != NULL)
  354. {
  355. CopyItem(item, current, 0);
  356. current = GotoB(current->parent, current);
  357. pos = BinarySearch(current, item, m_allocator);
  358. }
  359. if (pos >= current->size)
  360. {
  361. GotoB(NULL, current);
  362. return OpBoolean::IS_FALSE;
  363. }
  364. CopyItem(item, current, pos);
  365. GotoB(NULL, current);
  366. return rv == OpBoolean::IS_TRUE ? rv : OpStatus::OK;
  367. }
  368. OP_BOOLEAN BTreeBase::GetFirst(void *item)
  369. {
  370. BSCache::Item::DiskId ptr;
  371. BTreeBranch *current;
  372. m_cache->m_NUR_mark = (m_cache->m_NUR_mark + 1) & NUR_MASK;
  373. if ((ptr = this->m_root) == 0)
  374. return OpBoolean::IS_FALSE; // empty
  375. current = NULL;
  376. LoopDetector<BSCache::Item::DiskId> loopDetector;
  377. do {
  378. RETURN_IF_ERROR(loopDetector.CheckNext(ptr));
  379. RETURN_IF_ERROR(Goto(&current, ptr, current));
  380. ptr = current->Pointer(0);
  381. } while (!IsLeaf(current));
  382. CopyItem(item, current, 0);
  383. GotoB(NULL, current);
  384. return OpBoolean::IS_TRUE;
  385. }
  386. OP_BOOLEAN BTreeBase::GetLast(void *item)
  387. {
  388. BSCache::Item::DiskId ptr;
  389. BTreeBranch *current;
  390. m_cache->m_NUR_mark = (m_cache->m_NUR_mark + 1) & NUR_MASK;
  391. if ((ptr = this->m_root) == 0)
  392. return OpBoolean::IS_FALSE; // empty
  393. current = NULL;
  394. LoopDetector<BSCache::Item::DiskId> loopDetector;
  395. do {
  396. RETURN_IF_ERROR(loopDetector.CheckNext(ptr));
  397. RETURN_IF_ERROR(Goto(&current, ptr, current));
  398. ptr = current->Rightmost();
  399. } while (!IsLeaf(current));
  400. CopyItem(item, current, current->size - 1);
  401. GotoB(NULL, current);
  402. return OpBoolean::IS_TRUE;
  403. }
  404. IteratorBase *BTreeBase::Search(const void *item, SearchOperator oper)
  405. {
  406. BSCache::Item::DiskId ptr;
  407. BTreeBranch *current;
  408. int pos;
  409. BOOL matched = FALSE;
  410. IteratorBase *rv;
  411. OP_BOOLEAN err;
  412. m_cache->m_NUR_mark = (m_cache->m_NUR_mark + 1) & NUR_MASK;
  413. if ((ptr = this->m_root) == 0)
  414. return NewResult(NULL, 0, oper, item);
  415. current = NULL;
  416. if (oper == operatorLT || oper == operatorLE)
  417. {
  418. LoopDetector<BSCache::Item::DiskId> loopDetector;
  419. do {
  420. RETURN_VALUE_IF_ERROR(loopDetector.CheckNext(ptr), NULL);
  421. RETURN_VALUE_IF_ERROR(Goto(&current, ptr, current), NULL);
  422. ptr = current->Pointer(0);
  423. } while (!IsLeaf(current));
  424. pos = 0;
  425. if ((oper == operatorLT && m_allocator.Compare(item, current->GetItem(0))) ||
  426. (oper == operatorLE && !m_allocator.Compare(current->GetItem(0), item)))
  427. {
  428. GotoB(NULL, current);
  429. current = NULL;
  430. pos = -1;
  431. }
  432. }
  433. else {
  434. err = SearchItem(&current, &pos, item);
  435. RETURN_VALUE_IF_ERROR(err, NULL);
  436. matched = err == OpBoolean::IS_TRUE;
  437. }
  438. if ((rv = NewResult(current, pos, oper, item)) == NULL)
  439. {
  440. GotoB(NULL, current);
  441. return NULL;
  442. }
  443. if (oper == operatorGT && matched && rv->Count() != 0)
  444. {
  445. if (!rv->Next())
  446. {
  447. if (OpStatus::IsError(rv->Error()))
  448. {
  449. OP_DELETE(rv);
  450. return NULL;
  451. }
  452. }
  453. }
  454. if (rv->End() && !rv->Empty())
  455. {
  456. OP_DELETE(rv);
  457. return NewResult(NULL, 0, oper, item);
  458. }
  459. return rv;
  460. }
  461. IteratorBase *BTreeBase::SearchFirst(void)
  462. {
  463. BSCache::Item::DiskId ptr;
  464. BTreeBase::BTreeBranch *current;
  465. m_cache->m_NUR_mark = (m_cache->m_NUR_mark + 1) & NUR_MASK;
  466. if ((ptr = this->m_root) == 0)
  467. return NewResult(NULL, 0, operatorEQ, NULL);
  468. current = NULL;
  469. LoopDetector<BSCache::Item::DiskId> loopDetector;
  470. do {
  471. RETURN_VALUE_IF_ERROR(loopDetector.CheckNext(ptr), NULL);
  472. RETURN_VALUE_IF_ERROR(Goto(&current, ptr, current), NULL);
  473. ptr = current->Pointer(0);
  474. } while (!IsLeaf(current));
  475. return NewResult(current, 0, operatorGE, current->GetItem(0));
  476. }
  477. IteratorBase *BTreeBase::SearchLast(void)
  478. {
  479. BSCache::Item::DiskId ptr;
  480. BTreeBase::BTreeBranch *current;
  481. m_cache->m_NUR_mark = (m_cache->m_NUR_mark + 1) & NUR_MASK;
  482. if ((ptr = this->m_root) == 0)
  483. return NewResult(NULL, 0, operatorEQ, NULL);
  484. current = NULL;
  485. LoopDetector<BSCache::Item::DiskId> loopDetector;
  486. do {
  487. RETURN_VALUE_IF_ERROR(loopDetector.CheckNext(ptr), NULL);
  488. RETURN_VALUE_IF_ERROR(Goto(&current, ptr, current), NULL);
  489. ptr = current->Rightmost();
  490. } while (!IsLeaf(current));
  491. return NewResult(current, current->size - 1, operatorLE, current->GetItem(current->size - 1));
  492. }
  493. BOOL3 BTreeBase::Empty(void)
  494. {
  495. BTreeBase::BTreeBranch *current;
  496. BOOL empty;
  497. if (this->m_root == 0)
  498. return YES;
  499. RETURN_VALUE_IF_ERROR(Goto(&current, this->m_root, NULL), MAYBE);
  500. empty = current->size == 0;
  501. GotoB(NULL, current);
  502. return empty ? YES : NO;
  503. }
  504. OP_STATUS BTreeBase::Clear(void)
  505. {
  506. if (this->m_root != 0)
  507. {
  508. RETURN_IF_ERROR(DeleteBranch(this->m_root));
  509. }
  510. m_root = 0;
  511. return OpStatus::OK;
  512. }
  513. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  514. size_t BTreeBase::EstimateMemoryUsed() const
  515. {
  516. return
  517. sizeof(m_allocator) +
  518. sizeof(m_root) +
  519. sizeof(m_cache) +
  520. sizeof(m_reserved_id);
  521. }
  522. #endif
  523. OP_STATUS BTreeBase::NewBranch(BTreeBase::BTreeBranch **branch, BTreeBase::BTreeBranch *parent) // parent == NULL ... creating root
  524. {
  525. BSCache::Item *bi;
  526. OP_ASSERT(parent == NULL || parent->reference_count > 0);
  527. if (!m_cache->GetStorage()->InTransaction())
  528. {
  529. RETURN_IF_ERROR(m_cache->GetStorage()->BeginTransaction(m_cache->m_flush_mode == BSCache::JournalOnly));
  530. }
  531. RETURN_IF_ERROR(m_cache->Create(&bi, parent, 0));
  532. *branch = (BTreeBase::BTreeBranch *)bi;
  533. (*branch)->modified = TRUE;
  534. (*branch)->owner = this;
  535. if (this->m_root == 0)
  536. {
  537. if (m_reserved_id != 0)
  538. {
  539. (*branch)->disk_id = (BSCache::Item::DiskId)(m_reserved_id / m_cache->m_storage.GetBlockSize());
  540. (*branch)->id_reserved = TRUE;
  541. (*branch)->journalled = TRUE;
  542. ++m_cache->m_journal_count;
  543. m_reserved_id = 0;
  544. }
  545. this->m_root = (*branch)->disk_id;
  546. }
  547. while (parent != NULL)
  548. {
  549. m_cache->Load(&bi, parent); // will always succeed, since parent is in memory
  550. parent = ((BTreeBase::BTreeBranch *)bi)->parent;
  551. }
  552. return OpStatus::OK;
  553. }
  554. OP_STATUS BTreeBase::DeleteBranch(BSCache::Item::DiskId branch)
  555. {
  556. int i;
  557. BTreeBranch *rb;
  558. RETURN_IF_ERROR(Goto(&rb, branch, NULL));
  559. if (!IsLeaf(rb))
  560. {
  561. for (i = 0; i < rb->size; ++i)
  562. {
  563. RETURN_IF_ERROR(DeleteBranch(rb->Pointer(i)));
  564. }
  565. RETURN_IF_ERROR(DeleteBranch(rb->Rightmost()));
  566. }
  567. return DeleteLeaf(rb);
  568. }
  569. OP_STATUS BTreeBase::DeleteLeaf(BTreeBase::BTreeBranch *leaf)
  570. {
  571. if (!m_cache->GetStorage()->InTransaction())
  572. {
  573. RETURN_IF_ERROR(m_cache->GetStorage()->BeginTransaction(m_cache->m_flush_mode == BSCache::JournalOnly));
  574. }
  575. // if (leaf->id_reserved)
  576. // m_cache->m_storage.Write(leaf, 0, ((OpFileLength)leaf->disk_id) * m_cache->m_storage.GetBlockSize());
  577. m_cache->Unlink(leaf);
  578. // if (IsRoot(leaf))
  579. // m_cache->Flush();
  580. // I'm not sure if I should have just deleted these 4 lines, but the Write is invalid at this point and Flush shouldn't be called (perhaps with JournalAll)
  581. GotoB(NULL, leaf);
  582. return OpStatus::OK;
  583. }
  584. OP_STATUS BTreeBase::RebalanceRoot(void)
  585. {
  586. BTreeBase::BTreeBranch *root, *current;
  587. int pos;
  588. OP_STATUS rv;
  589. RETURN_IF_ERROR(Goto(&root, this->m_root, NULL));
  590. if (!IsLeaf(root))
  591. {
  592. if (root->size <= 0)
  593. {
  594. if (OpStatus::IsError(rv = Goto(&current, root->Rightmost(), NULL)))
  595. {
  596. GotoB(NULL, root);
  597. return rv; // root is empty, but that isn't fatal
  598. }
  599. RETURN_IF_ERROR(Modify(root));
  600. root->CopyData(current);
  601. for (pos = 0; pos < root->size; ++pos)
  602. SetParent(root->Pointer(pos), root);
  603. SetParent(root->Rightmost(), root);
  604. CHECK_INTEGRITY(root);
  605. RETURN_IF_ERROR(DeleteLeaf(current));
  606. }
  607. GotoB(NULL, root);
  608. }
  609. else {
  610. if (root->size <= 0)
  611. {
  612. RETURN_IF_ERROR(DeleteLeaf(root));
  613. m_root = 0;
  614. }
  615. else
  616. GotoB(NULL, root);
  617. }
  618. return OpStatus::OK;
  619. }
  620. void BTreeBase::SetParent(BSCache::Item::DiskId branch, BTreeBase::BTreeBranch *parent)
  621. {
  622. BTreeBase::BTreeBranch *tmp, *tmp_parent;
  623. BTreeBase::BTreeBranch *old_parent;
  624. BTreeBase::BTreeBranch *new_parent;
  625. BSCache::Item *bi;
  626. int i;
  627. if (branch == 0)
  628. return;
  629. if ((tmp = m_cache->LoadFromCache(branch)) == NULL)
  630. return;
  631. old_parent = tmp->parent;
  632. new_parent = parent;
  633. if (old_parent != new_parent)
  634. {
  635. // new branch is inserted between referenced child branch and its parent - reference it too
  636. if (parent != NULL && tmp->reference_count > 1)
  637. {
  638. OP_ASSERT(tmp->parent == NULL || tmp->parent->reference_count > 0);
  639. do {
  640. m_cache->Load(&bi, parent);
  641. parent = ((BTreeBase::BTreeBranch *)bi)->parent;
  642. } while (parent != NULL);
  643. if (!IsRoot(old_parent))
  644. {
  645. for (i = 2; i < tmp->reference_count; ++i)
  646. {
  647. parent = new_parent;
  648. do {
  649. m_cache->Load(&bi, parent);
  650. parent = ((BTreeBase::BTreeBranch *)bi)->parent;
  651. } while (parent != NULL);
  652. }
  653. }
  654. else {
  655. for (i = 2; i < tmp->reference_count; ++i)
  656. m_cache->Load(&bi, new_parent);
  657. }
  658. }
  659. tmp->parent = new_parent;
  660. if (old_parent != NULL && tmp->reference_count > 1)
  661. {
  662. if (!IsRoot(old_parent)) // if IsRoot, we have inserted a new branch in between
  663. {
  664. for (i = 2; i < tmp->reference_count; ++i)
  665. {
  666. parent = old_parent;
  667. do {
  668. tmp_parent = parent->parent;
  669. m_cache->Release(parent);
  670. parent = tmp_parent;
  671. } while (parent != NULL);
  672. }
  673. }
  674. do {
  675. parent = old_parent->parent;
  676. m_cache->Release(old_parent);
  677. old_parent = parent;
  678. } while (old_parent != NULL);
  679. }
  680. }
  681. m_cache->Release(tmp);
  682. }
  683. OP_STATUS BTreeBase::Goto(BTreeBase::BTreeBranch **rv, BSCache::Item::DiskId branch, BTreeBase::BTreeBranch *previous)
  684. {
  685. BTreeBase::BTreeBranch *tmp, *parent;
  686. BSCache::Item *bi;
  687. OP_STATUS err;
  688. if (branch == 0)
  689. {
  690. OP_ASSERT(0);
  691. GotoB(NULL, previous);
  692. return OpStatus::ERR_PARSING_FAILED;
  693. }
  694. if (OpStatus::IsError(err = m_cache->Load(&bi, branch)))
  695. {
  696. GotoB(NULL, previous);
  697. return err;
  698. }
  699. *rv = (BTreeBase::BTreeBranch *)bi;
  700. (*rv)->owner = this;
  701. if (previous != NULL && branch != this->m_root)
  702. parent = previous;
  703. else
  704. parent = (*rv)->parent;
  705. tmp = parent;
  706. while (tmp != NULL)
  707. {
  708. if (tmp == *rv) // Found *rv while following ->parent, avoid creating loop
  709. {
  710. OP_ASSERT(0);
  711. GotoB(NULL, previous);
  712. return OpStatus::ERR_PARSING_FAILED;
  713. }
  714. m_cache->Load(&bi, tmp);
  715. tmp = ((BTreeBase::BTreeBranch *)bi)->parent;
  716. }
  717. (*rv)->parent = parent;
  718. if (previous != NULL) {
  719. do {
  720. tmp = previous->parent;
  721. m_cache->Release(previous);
  722. previous = tmp;
  723. } while (previous != NULL);
  724. }
  725. return OpStatus::OK;
  726. }
  727. BTreeBase::BTreeBranch *BTreeBase::GotoB(BTreeBase::BTreeBranch *branch, BTreeBase::BTreeBranch *previous)
  728. {
  729. BTreeBase::BTreeBranch *b, *tmp;
  730. BSCache::Item *bi;
  731. OP_ASSERT(branch == NULL || branch->disk_id == this->m_root || branch->parent != NULL);
  732. b = NULL;
  733. if (branch != NULL)
  734. {
  735. m_cache->Load(&bi, branch);
  736. b = (BTreeBase::BTreeBranch *)bi;
  737. tmp = b->parent;
  738. while (tmp != NULL)
  739. {
  740. m_cache->Load(&bi, tmp);
  741. tmp = ((BTreeBase::BTreeBranch *)bi)->parent;
  742. }
  743. }
  744. if (previous != NULL) {
  745. do {
  746. tmp = previous->parent;
  747. m_cache->Release(previous);
  748. previous = tmp;
  749. } while (previous != NULL);
  750. }
  751. return b;
  752. }
  753. OP_STATUS BTreeBase::Modify(BTreeBase::BTreeBranch *branch)
  754. {
  755. if (!m_cache->GetStorage()->InTransaction())
  756. RETURN_IF_ERROR(m_cache->GetStorage()->BeginTransaction(m_cache->m_flush_mode == BSCache::JournalOnly));
  757. branch->modified = TRUE;
  758. return OpStatus::OK;
  759. }
  760. BSCache::Item::DiskId BTreeBase::SafePointer(BTreeBase::BTreeBranch *branch)
  761. {
  762. if (branch->disk_id < 0)
  763. {
  764. BSCache::Item::DiskId new_id;
  765. new_id = (BSCache::Item::DiskId)(m_cache->m_storage.Reserve() / m_cache->m_storage.GetBlockSize());
  766. if (new_id > 0)
  767. {
  768. branch->id_reserved = TRUE;
  769. branch->journalled = TRUE;
  770. ++m_cache->m_journal_count;
  771. branch->modified = TRUE;
  772. branch->OnIdChange(new_id, branch->disk_id);
  773. branch->disk_id = new_id;
  774. }
  775. }
  776. return branch->disk_id;
  777. }
  778. OP_BOOLEAN BTreeBase::SearchItem(BTreeBranch **rbranch, int *rpos, const void *item)
  779. {
  780. BSCache::Item::DiskId ptr;
  781. BTreeBranch *current;
  782. int pos;
  783. *rbranch = NULL;
  784. *rpos = -1;
  785. // root is assumed to be valid at this point
  786. OP_ASSERT(this->m_root != 0);
  787. if ((ptr = this->m_root) == 0)
  788. return OpBoolean::IS_FALSE;
  789. current = NULL;
  790. LoopDetector<BSCache::Item::DiskId> loopDetector;
  791. do {
  792. RETURN_IF_ERROR(loopDetector.CheckNext(ptr));
  793. RETURN_IF_ERROR(Goto(&current, ptr, current));
  794. pos = BinarySearch(current, item, m_allocator);
  795. if (pos >= current->size)
  796. {
  797. ptr = current->Rightmost();
  798. continue;
  799. }
  800. // current->GetItem(pos) is >= item here
  801. if (!m_allocator.Compare(item, current->GetItem(pos))) // item isn't < current->GetItem(pos) ... must be ==
  802. {
  803. *rbranch = current;
  804. *rpos = pos;
  805. return OpBoolean::IS_TRUE;
  806. }
  807. ptr = current->Pointer(pos);
  808. } while (ptr != 0);
  809. *rbranch = current;
  810. *rpos = pos;
  811. return OpBoolean::IS_FALSE;
  812. }
  813. // modified BTreeBase::Result::_Next()
  814. OP_BOOLEAN BTreeBase::SearchNextItem(BTreeBranch **rbranch, int *rpos, const void *item)
  815. {
  816. OpString8 key_buf;
  817. BSCache::Item::DiskId tmp_ptr;
  818. RETURN_OOM_IF_NULL(key_buf.Reserve((int)m_allocator.size));
  819. ++*rpos;
  820. // go down
  821. LoopDetector<BSCache::Item::DiskId> loopDetector;
  822. while (!IsLeaf(*rbranch) && *rpos <= (*rbranch)->size)
  823. {
  824. tmp_ptr = *rpos == (*rbranch)->size ? (*rbranch)->Rightmost() : (*rbranch)->Pointer(*rpos);
  825. RETURN_IF_ERROR(loopDetector.CheckNext(tmp_ptr));
  826. RETURN_IF_ERROR(Goto(rbranch, tmp_ptr, *rbranch));
  827. *rpos = 0;
  828. }
  829. // cannot go down - go to the first valid uppper position
  830. while (*rpos >= (*rbranch)->size)
  831. {
  832. if (IsRoot(*rbranch))
  833. return OpBoolean::IS_FALSE;
  834. tmp_ptr = SafePointer(*rbranch);
  835. CopyItem(key_buf.DataPtr(), *rbranch, (*rbranch)->size - 1);
  836. if ((*rbranch = GotoB((*rbranch)->parent, *rbranch)) == NULL)
  837. return OpStatus::ERR;
  838. if ((*rbranch)->Rightmost() == tmp_ptr)
  839. *rpos = (*rbranch)->size;
  840. else
  841. *rpos = BinarySearch(*rbranch, key_buf.DataPtr(), m_allocator);
  842. }
  843. return OpBoolean::IS_TRUE;
  844. }
  845. OP_STATUS BTreeBase::InsertItem(BTreeBranch *branch, int pos, const void *item, BSCache::Item::DiskId overflow)
  846. {
  847. int btree_size = BTreeSize();
  848. if (branch->size >= btree_size)
  849. {
  850. BTreeBranch *n, *n2;
  851. OP_STATUS rv;
  852. int i;
  853. if (branch->disk_id == m_root)
  854. {
  855. RETURN_IF_ERROR(NewBranch(&n, branch));
  856. if (OpStatus::IsError(rv = NewBranch(&n2, branch)))
  857. {
  858. OpStatus::Ignore(DeleteLeaf(n));
  859. return rv;
  860. }
  861. if (pos == (btree_size + 1) / 2)
  862. {
  863. n->size = (btree_size - 1) / 2 + 1;
  864. CopyPtrItems(n, 0, branch, 0, n->size);
  865. n->Rightmost() = branch->Pointer(n->size);
  866. n2->size = branch->size - n->size;
  867. CopyPtrItems(n2, 0, branch, n->size, n2->size);
  868. n2->Pointer(0) = overflow;
  869. n2->Rightmost() = branch->Rightmost();
  870. branch->size = 1;
  871. CopyItem(branch, 0, item);
  872. branch->Pointer(0) = SafePointer(n);
  873. branch->Rightmost() = SafePointer(n2);
  874. RETURN_IF_ERROR(Modify(n));
  875. RETURN_IF_ERROR(Modify(n2));
  876. RETURN_IF_ERROR(Modify(branch));
  877. if (!IsLeaf(n))
  878. {
  879. for (i = 0; i < n->size; ++i)
  880. SetParent(n->Pointer(i), n);
  881. SetParent(n->Rightmost(), n);
  882. for (i = 0; i < n2->size; ++i)
  883. SetParent(n2->Pointer(i), n2);
  884. SetParent(n2->Rightmost(), n2);
  885. }
  886. CHECK_INTEGRITY(n);
  887. CHECK_INTEGRITY(n2);
  888. CHECK_INTEGRITY(branch);
  889. GotoB(NULL, n);
  890. GotoB(NULL, n2);
  891. return OpStatus::OK;
  892. }
  893. n->size = (btree_size - 1) / 2 + 1 - (pos <= btree_size / 2);
  894. CopyPtrItems(n, 0, branch, 0, n->size);
  895. n->Rightmost() = branch->Pointer(n->size);
  896. n2->size = branch->size - n->size - 1;
  897. CopyPtrItems(n2, 0, branch, branch->size - n2->size, n2->size);
  898. n2->Rightmost() = branch->Rightmost();
  899. branch->size = 1;
  900. CopyItem(branch, 0, branch, n->size);
  901. branch->Pointer(0) = SafePointer(n);
  902. branch->Rightmost() = SafePointer(n2);
  903. RETURN_IF_ERROR(Modify(n));
  904. RETURN_IF_ERROR(Modify(n2));
  905. RETURN_IF_ERROR(Modify(branch));
  906. if (pos <= btree_size / 2)
  907. {
  908. RETURN_IF_ERROR(InsertItem(n, pos, item, overflow));
  909. }
  910. else {
  911. RETURN_IF_ERROR(InsertItem(n2, pos - n->size - 1, item, overflow));
  912. }
  913. if (!IsLeaf(n))
  914. {
  915. for (i = 0; i < n->size; ++i)
  916. SetParent(n->Pointer(i), n);
  917. SetParent(n->Rightmost(), n);
  918. for (i = 0; i < n2->size; ++i)
  919. SetParent(n2->Pointer(i), n2);
  920. SetParent(n2->Rightmost(), n2);
  921. }
  922. CHECK_INTEGRITY(n);
  923. CHECK_INTEGRITY(n2);
  924. CHECK_INTEGRITY(branch);
  925. GotoB(NULL, n);
  926. GotoB(NULL, n2);
  927. return OpStatus::OK;
  928. } // branch->disk_id == this->m_root
  929. RETURN_IF_ERROR(NewBranch(&n2, branch->parent));
  930. n = GotoB(branch->parent, NULL);
  931. n2->size = btree_size / 2 - (pos > (btree_size + 1) / 2);
  932. CopyPtrItems(n2, 0, branch, branch->size - n2->size, n2->size);
  933. n2->Rightmost() = branch->Rightmost();
  934. if (pos == (btree_size + 1) / 2) // new item is in the middle
  935. {
  936. branch->Rightmost() = n2->Pointer(0);
  937. n2->Pointer(0) = overflow;
  938. branch->size -= n2->size;
  939. RETURN_IF_ERROR(Modify(branch));
  940. rv = InsertItem(n, BinarySearch(n, item, m_allocator), item, SafePointer(n2));
  941. GotoB(NULL, n);
  942. if (OpStatus::IsError(rv))
  943. {
  944. OpStatus::Ignore(DeleteLeaf(n2));
  945. return rv;
  946. }
  947. }
  948. else {
  949. rv = InsertItem(n, BinarySearch(n, branch->GetItem(branch->size - n2->size - 1), m_allocator),
  950. branch->GetItem(branch->size - n2->size - 1), SafePointer(n2));
  951. GotoB(NULL, n);
  952. if (OpStatus::IsError(rv))
  953. {
  954. OpStatus::Ignore(DeleteLeaf(n2));
  955. return rv;
  956. }
  957. branch->Rightmost() = branch->Pointer(branch->size - n2->size - 1);
  958. branch->size -= n2->size + 1;
  959. RETURN_IF_ERROR(Modify(branch));
  960. if (pos < (btree_size + 1) / 2) // new item is on left
  961. {
  962. RETURN_IF_ERROR(InsertItem(branch, pos, item, overflow));
  963. }
  964. else {
  965. RETURN_IF_ERROR(InsertItem(n2, pos - branch->size - 1, item, overflow));
  966. }
  967. }
  968. if (!IsLeaf(n2))
  969. {
  970. for (i = 0; i < n2->size; ++i)
  971. SetParent(n2->Pointer(i), n2);
  972. SetParent(n2->Rightmost(), n2);
  973. }
  974. RETURN_IF_ERROR(Modify(n2));
  975. CHECK_INTEGRITY(n2);
  976. CHECK_INTEGRITY(branch);
  977. GotoB(NULL, n2);
  978. return OpStatus::OK;
  979. }
  980. if (pos >= branch->size)
  981. {
  982. #ifdef _DEBUG
  983. if (branch->size > 0)
  984. {
  985. OP_ASSERT(m_allocator.Compare(branch->GetItem(branch->size - 1), item));
  986. }
  987. #endif
  988. CopyItem(branch, branch->size, item);
  989. branch->Pointer(branch->size) = branch->Rightmost();
  990. branch->Rightmost() = overflow;
  991. }
  992. else {
  993. #ifdef _DEBUG
  994. if (pos > 0)
  995. {
  996. OP_ASSERT(m_allocator.Compare(branch->GetItem(pos - 1), item));
  997. }
  998. OP_ASSERT(m_allocator.Compare(item, branch->GetItem(pos)));
  999. #endif
  1000. CopyPtrItems(branch, pos + 2, branch, pos + 1, branch->size - pos - 1);
  1001. CopyItem(branch, pos + 1, branch, pos);
  1002. CopyItem(branch, pos, item);
  1003. branch->Pointer(pos + 1) = overflow;
  1004. }
  1005. if (!IsLeaf(branch))
  1006. {
  1007. OP_ASSERT(overflow != 0);
  1008. SetParent(overflow, branch);
  1009. }
  1010. #ifdef _DEBUG
  1011. else {
  1012. OP_ASSERT(overflow == 0);
  1013. }
  1014. #endif
  1015. ++(branch->size);
  1016. RETURN_IF_ERROR(Modify(branch));
  1017. CHECK_INTEGRITY(branch);
  1018. return OpStatus::OK;
  1019. } // InsertItem
  1020. OP_STATUS BTreeBase::DeleteItem(BTreeBranch *branch, int pos)
  1021. {
  1022. BTreeBranch *n;
  1023. OP_STATUS rv;
  1024. if (!IsLeaf(branch))
  1025. {
  1026. RETURN_IF_ERROR(Goto(&n, pos >= branch->size - 1 ? branch->Rightmost() : branch->Pointer(pos + 1), branch));
  1027. LoopDetector<BSCache::Item::DiskId> loopDetector;
  1028. while (!IsLeaf(n))
  1029. {
  1030. BSCache::Item::DiskId ptr = n->Pointer(0);
  1031. RETURN_IF_ERROR(loopDetector.CheckNext(ptr));
  1032. RETURN_IF_ERROR(Goto(&n, ptr, n));
  1033. }
  1034. RETURN_IF_ERROR(Modify(branch));
  1035. CopyItem(branch, pos, n, 0);
  1036. rv = DeleteItem(n, 0);
  1037. CHECK_INTEGRITY(branch);
  1038. GotoB(branch, n);
  1039. return rv;
  1040. }
  1041. return RemoveItem(branch, pos);
  1042. }
  1043. OP_STATUS BTreeBase::RemoveItem(BTreeBranch *branch, int pos)
  1044. {
  1045. BTreeBranch *parent = NULL, *n2 = NULL, *lc = NULL, *rc = NULL;
  1046. int i;
  1047. OP_STATUS rv;
  1048. int btree_size = BTreeSize();
  1049. if (branch->size <= btree_size / 2 && !IsRoot(branch))
  1050. {
  1051. parent = GotoB(branch->parent, NULL);
  1052. i = BinarySearch(parent, branch->GetItem(0), m_allocator);
  1053. if (i < parent->size && !m_allocator.Compare(branch->GetItem(0), parent->GetItem(i)))
  1054. ++i;
  1055. --i;
  1056. if (i < 0)
  1057. i = 1; // take right neighbor for the leftmost
  1058. // if root->size == 1, i can be >= size
  1059. if (OpStatus::IsError(rv = Goto(&n2, i >= parent->size ? parent->Rightmost() : parent->Pointer(i), parent)))
  1060. {
  1061. GotoB(NULL, parent);
  1062. return rv;
  1063. }
  1064. parent = GotoB(branch->parent, NULL);
  1065. OP_ASSERT(n2->disk_id != branch->disk_id);
  1066. if (m_allocator.Compare(n2->GetItem(0), branch->GetItem(0)))
  1067. {
  1068. lc = n2;
  1069. rc = branch;
  1070. }
  1071. else {
  1072. lc = branch;
  1073. rc = n2;
  1074. }
  1075. }
  1076. RETURN_IF_ERROR(Modify(branch));
  1077. OP_ASSERT(pos < branch->size);
  1078. if (pos < branch->size)
  1079. {
  1080. m_allocator.Destruct(branch->GetItem(pos));
  1081. CopyPtrItems(branch, pos, branch, pos + 1, branch->size - pos - 1);
  1082. }
  1083. --branch->size;
  1084. if (branch->size >= btree_size / 2 || IsRoot(branch))
  1085. {
  1086. CHECK_INTEGRITY(branch);
  1087. return OpStatus::OK; // parent and n2 aren't referenced here
  1088. }
  1089. // branch is too empty here
  1090. if (n2->disk_id == branch->disk_id)
  1091. {
  1092. // This causes an infinite loop lower down
  1093. OP_ASSERT(!"The B-tree is in an illegal state. Contact search_engine module owner if reproducible");
  1094. return OpStatus::ERR_PARSING_FAILED;
  1095. }
  1096. RETURN_IF_ERROR(Modify(n2));
  1097. RETURN_IF_ERROR(Modify(parent));
  1098. if (n2->size + branch->size >= btree_size)
  1099. {
  1100. Rotate(lc, parent, rc);
  1101. OP_ASSERT(branch->size >= btree_size / 2);
  1102. OP_ASSERT(n2->size >= btree_size / 2);
  1103. OP_ASSERT(IsRoot(parent) || parent->size >= btree_size / 2);
  1104. CHECK_INTEGRITY(branch);
  1105. CHECK_INTEGRITY(parent);
  1106. CHECK_INTEGRITY(n2);
  1107. GotoB(NULL, parent);
  1108. GotoB(NULL, n2);
  1109. return OpStatus::OK;
  1110. }
  1111. if (OpStatus::IsError(rv = Merge(branch, lc, parent, rc)))
  1112. {
  1113. CHECK_INTEGRITY(branch);
  1114. CHECK_INTEGRITY(parent);
  1115. CHECK_INTEGRITY(n2);
  1116. GotoB(NULL, n2);
  1117. GotoB(NULL, parent);
  1118. return rv;
  1119. }
  1120. // n2 is deleted here
  1121. CHECK_INTEGRITY(branch);
  1122. CHECK_INTEGRITY(parent);
  1123. OP_ASSERT(branch->size >= btree_size / 2);
  1124. GotoB(NULL, parent);
  1125. return OpStatus::OK;
  1126. }
  1127. void BTreeBase::Rotate(BTreeBranch *l_child, BTreeBranch *parent, BTreeBranch *g_child)
  1128. {
  1129. int size, shift;
  1130. int pos;
  1131. int i;
  1132. OP_ASSERT(l_child->modified);
  1133. OP_ASSERT(parent->modified);
  1134. OP_ASSERT(g_child->modified);
  1135. if (g_child->size == l_child->size)
  1136. {
  1137. OP_ASSERT(0); // Rotate is not supposed to be called in this case
  1138. return;
  1139. }
  1140. pos = BinarySearch(parent, l_child->GetItem(0), m_allocator);
  1141. if (g_child->size < l_child->size)
  1142. {
  1143. size = (l_child->size + g_child->size) / 2;
  1144. shift = size - g_child->size - 1;
  1145. CopyPtrItems(g_child, shift + 1, g_child, 0, g_child->size);
  1146. CopyItem(g_child, shift, parent, pos);
  1147. g_child->Pointer(shift) = l_child->Rightmost();
  1148. SetParent(g_child->Pointer(shift), g_child);
  1149. for (i = 0; i < shift; ++i)
  1150. {
  1151. CopyPtrItems(g_child, i, l_child, l_child->size - shift + i, 1);
  1152. SetParent(g_child->Pointer(i), g_child);
  1153. }
  1154. CopyItem(parent, pos, l_child, l_child->size - (shift + 1));
  1155. l_child->Rightmost() = l_child->Pointer(l_child->size - (shift + 1));
  1156. l_child->size -= shift + 1;
  1157. g_child->size = size;
  1158. }
  1159. else {
  1160. size = (l_child->size + g_child->size + 1) / 2;
  1161. shift = size - l_child->size - 1;
  1162. CopyItem(l_child, l_child->size, parent, pos);
  1163. l_child->Pointer(l_child->size++) = l_child->Rightmost();
  1164. for (i = 0; i < shift; ++i)
  1165. {
  1166. CopyPtrItems(l_child, l_child->size, g_child, i, 1);
  1167. SetParent(l_child->Pointer(l_child->size++), l_child);
  1168. }
  1169. l_child->Rightmost() = g_child->Pointer(shift);
  1170. SetParent(l_child->Rightmost(), l_child);
  1171. CopyItem(parent, pos, g_child, shift);
  1172. g_child->size -= shift + 1;
  1173. CopyPtrItems(g_child, 0, g_child, shift + 1, g_child->size);
  1174. }
  1175. }
  1176. OP_STATUS BTreeBase::Merge(BTreeBranch *dst, BTreeBranch *l_child, BTreeBranch *parent, BTreeBranch *g_child)
  1177. {
  1178. int pos;
  1179. int i;
  1180. OP_ASSERT(dst->modified);
  1181. OP_ASSERT(l_child->modified);
  1182. OP_ASSERT(parent->modified);
  1183. OP_ASSERT(g_child->modified);
  1184. pos = BinarySearch(parent, l_child->GetItem(0), m_allocator);
  1185. CopyItem(l_child, l_child->size, parent, pos);
  1186. l_child->Pointer(l_child->size++) = l_child->Rightmost();
  1187. for (i = 0; i < g_child->size; ++i)
  1188. CopyPtrItems(l_child, l_child->size++, g_child, i, 1);
  1189. l_child->Rightmost() = g_child->Rightmost();
  1190. if (dst != l_child)
  1191. {
  1192. dst->CopyData(l_child);
  1193. RETURN_IF_ERROR(DeleteLeaf(l_child));
  1194. if (dst != g_child)
  1195. RETURN_IF_ERROR(DeleteLeaf(g_child));
  1196. }
  1197. else
  1198. RETURN_IF_ERROR(DeleteLeaf(g_child));
  1199. for (i = 0; i < dst->size; ++i)
  1200. SetParent(dst->Pointer(i), dst);
  1201. SetParent(dst->Rightmost(), dst);
  1202. if (pos == parent->size - 1)
  1203. parent->Rightmost() = dst->disk_id;
  1204. else parent->Pointer(pos + 1) = dst->disk_id;
  1205. OP_STATUS status = RemoveItem(parent, pos);
  1206. return status;
  1207. }
  1208. #ifdef _DEBUG
  1209. BOOL BTreeBase::CheckIntegrity(const BTreeBranch *branch) const
  1210. {
  1211. int i;
  1212. const BTreeBranch *parent, *child;
  1213. if (branch->size <= 1)
  1214. return TRUE;
  1215. if (branch->parent != NULL && branch->size + 1 < BTreeSize() / 2)
  1216. return FALSE;
  1217. for (i = 1; i < branch->size; ++i)
  1218. {
  1219. if (!m_allocator.Compare(branch->GetItem(i - 1), branch->GetItem(i)))
  1220. return FALSE;
  1221. }
  1222. if (!branch->modified) // This should almost never happen. If it does, the same contents should be on disk
  1223. {
  1224. int block_len, block_size, item_size, num_items;
  1225. char *block;
  1226. OpAutoArray<char> block_a;
  1227. int item;
  1228. OpFileLength pos;
  1229. if (branch->disk_id < 0)
  1230. return FALSE;
  1231. item_size = (int)m_allocator.size_align32 + 4;
  1232. block_size = m_cache->m_storage.GetBlockSize();
  1233. pos = (OpFileLength)branch->disk_id * block_size;
  1234. block_len = m_cache->m_storage.DataLength(pos);
  1235. num_items = (block_len - 4) / item_size;
  1236. if (branch->size != num_items)
  1237. return FALSE;
  1238. block_a.reset(block = OP_NEWA(char, block_size));
  1239. if (block != NULL) // We may be running with "random OOM"
  1240. {
  1241. if (!m_cache->m_storage.Read(block, block_len, pos))
  1242. return FALSE;
  1243. if (branch->Rightmost() != *(BSCache::Item::DiskId *)(block))
  1244. return FALSE;
  1245. for (item = 0; item < num_items; ++item)
  1246. {
  1247. if (branch->Pointer(item) != *(BSCache::Item::DiskId *)(block + 4 + item * item_size))
  1248. return FALSE;
  1249. if (m_allocator.Compare(block + 8 + item * item_size, branch->GetItem(item)) ||
  1250. m_allocator.Compare(branch->GetItem(item), block + 8 + item * item_size))
  1251. return FALSE;
  1252. }
  1253. }
  1254. }
  1255. child = branch;
  1256. while (child->parent != NULL)
  1257. {
  1258. parent = child->parent;
  1259. while (parent != NULL)
  1260. {
  1261. i = BinarySearch(parent, child->GetItem(0), m_allocator);
  1262. if (i > parent->size)
  1263. {
  1264. OP_ASSERT(0);
  1265. return FALSE;
  1266. }
  1267. if (child->parent == parent && child->disk_id > 0 &&
  1268. ((i == parent->size && parent->Rightmost() != child->disk_id) ||
  1269. (i < parent->size && parent->Pointer(i) != child->disk_id)))
  1270. return FALSE;
  1271. if (i < parent->size && !m_allocator.Compare(child->GetItem(child->size - 1), parent->GetItem(i)))
  1272. return FALSE;
  1273. if (i > 0 && !m_allocator.Compare(parent->GetItem(i - 1), child->GetItem(0)))
  1274. return FALSE;
  1275. parent = parent->parent;
  1276. }
  1277. child = child->parent;
  1278. }
  1279. return TRUE;
  1280. }
  1281. #endif
  1282. PoolBase::PoolBase(int max_cache_branches, const TypeDescriptor &allocator) : BSCache(max_cache_branches), m_allocator(allocator)
  1283. {
  1284. }
  1285. BTreeBase::BTreeBranch *PoolBase::LoadFromCache(BSCache::Item::DiskId id)
  1286. {
  1287. BSCache::Item *tmp;
  1288. for (tmp = m_head; tmp != NULL; tmp = tmp->previous)
  1289. if (tmp->disk_id == id)
  1290. {
  1291. Load(&tmp, tmp);
  1292. return (BTreeBase::BTreeBranch *)tmp;
  1293. }
  1294. return NULL;
  1295. }
  1296. OP_STATUS PoolBase::Open(const uni_char* path, BlockStorage::OpenMode mode, int blocksize, OpFileFolder folder)
  1297. {
  1298. RETURN_IF_ERROR(m_storage.Open(path, mode, blocksize, 0, folder));
  1299. if (m_storage.GetBlockSize() != blocksize)
  1300. {
  1301. m_storage.Close();
  1302. return OpStatus::ERR_PARSING_FAILED;
  1303. }
  1304. return OpStatus::OK;
  1305. }
  1306. OP_STATUS PoolBase::Close(void)
  1307. {
  1308. RETURN_IF_ERROR(Flush(ReleaseAll));
  1309. ClearCache();
  1310. if (m_storage.InTransaction())
  1311. {
  1312. RETURN_IF_ERROR(m_storage.Commit());
  1313. }
  1314. m_storage.Close();
  1315. return OpStatus::OK;
  1316. }
  1317. OP_STATUS PoolBase::Abort(void)
  1318. {
  1319. ClearCache();
  1320. if (m_storage.InTransaction())
  1321. return m_storage.Rollback();
  1322. return OpStatus::OK;
  1323. }
  1324. OP_STATUS PoolBase::Commit(void)
  1325. {
  1326. RETURN_IF_ERROR(Flush());
  1327. if (!m_storage.InTransaction())
  1328. return OpStatus::OK;
  1329. return m_storage.Commit();
  1330. }
  1331. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  1332. size_t PoolBase::EstimateMemoryUsed() const
  1333. {
  1334. return BSCache::EstimateMemoryUsed() +
  1335. sizeof(m_allocator);
  1336. }
  1337. #endif
  1338. OP_STATUS BTreeBase::Result::Init(BTreeBase *owner, BTreeBase::BTreeBranch *current, int pos,
  1339. SearchOperator oper, const void *key)
  1340. {
  1341. RETURN_OOM_IF_NULL(m_key_buf = OP_NEWA(char, owner->m_allocator.size * 2));
  1342. m_last_error = OpStatus::OK;
  1343. m_btree = owner;
  1344. m_current = current;
  1345. m_pos = pos;
  1346. m_oper = oper;
  1347. if (key != NULL)
  1348. RETURN_IF_ERROR(owner->m_allocator.Assign(m_key_buf, key));
  1349. else
  1350. op_memset(m_key_buf, 0, owner->m_allocator.size);
  1351. m_loop_detector.Reset();
  1352. return OpStatus::OK;
  1353. }
  1354. void BTreeBase::Result::Free(void)
  1355. {
  1356. if (m_current != NULL)
  1357. {
  1358. m_btree->GotoB(NULL, m_current);
  1359. m_current = NULL;
  1360. }
  1361. if (m_key_buf != NULL)
  1362. {
  1363. OP_DELETEA(m_key_buf);
  1364. m_key_buf = NULL;
  1365. }
  1366. }
  1367. BOOL BTreeBase::Result::_Next(void)
  1368. {
  1369. BSCache::Item::DiskId tmp_ptr, next_ptr;
  1370. if (_End())
  1371. {
  1372. m_last_error = OpStatus::ERR_OUT_OF_RANGE;
  1373. return FALSE;
  1374. }
  1375. if (_Beginning() && m_last_error == OpStatus::ERR_OUT_OF_RANGE)
  1376. m_last_error = OpStatus::OK;
  1377. ++m_pos;
  1378. // go down
  1379. while (!m_btree->IsLeaf(m_current) && m_pos <= m_current->size)
  1380. {
  1381. next_ptr = m_pos == m_current->size ? m_current->Rightmost() : m_current->Pointer(m_pos);
  1382. RETURN_VALUE_IF_ERROR(m_last_error = m_loop_detector.CheckNext(next_ptr), FALSE);
  1383. RETURN_VALUE_IF_ERROR(m_last_error = m_btree->Goto(&m_current, next_ptr, m_current), FALSE);
  1384. m_pos = 0;
  1385. }
  1386. // cannot go down - go to the first valid upper position
  1387. while (m_pos >= m_current->size)
  1388. {
  1389. if (m_btree->IsRoot(m_current))
  1390. return FALSE;
  1391. tmp_ptr = m_btree->SafePointer(m_current);
  1392. RETURN_VALUE_IF_ERROR(m_last_error = m_btree->m_allocator.Assign(m_key_buf + m_btree->m_allocator.size, m_current->GetItem(m_current->size - 1)), FALSE);
  1393. if ((m_current = m_btree->GotoB(m_current->parent, m_current)) == NULL)
  1394. {
  1395. m_last_error = OpStatus::ERR;
  1396. return FALSE;
  1397. }
  1398. if (m_current->Rightmost() == tmp_ptr)
  1399. m_pos = m_current->size;
  1400. else
  1401. m_pos = BinarySearch(m_current, m_key_buf + m_btree->m_allocator.size, m_btree->m_allocator);
  1402. }
  1403. return !_End();
  1404. }
  1405. BOOL BTreeBase::Result::_Prev(void)
  1406. {
  1407. BSCache::Item::DiskId next_ptr;
  1408. m_loop_detector.Reset(); // Cannot do global loop detection when iterating back and forth
  1409. if (_Beginning())
  1410. {
  1411. m_last_error = OpStatus::ERR_OUT_OF_RANGE;
  1412. return FALSE;
  1413. }
  1414. if (_End() && m_last_error == OpStatus::ERR_OUT_OF_RANGE)
  1415. m_last_error = OpStatus::OK;
  1416. --m_pos;
  1417. // go down
  1418. LoopDetector<BSCache::Item::DiskId> loopDetector;
  1419. while (!m_btree->IsLeaf(m_current) && m_pos >= 0)
  1420. {
  1421. next_ptr = m_pos + 1 >= m_current->size ? m_current->Rightmost() : m_current->Pointer(m_pos + 1);
  1422. RETURN_VALUE_IF_ERROR(m_last_error = loopDetector.CheckNext(next_ptr), FALSE);
  1423. RETURN_VALUE_IF_ERROR(m_last_error = m_btree->Goto(&m_current, next_ptr, m_current), FALSE);
  1424. m_pos = m_current->size - 1;
  1425. }
  1426. // cannot go down - go to the first valid uppper position
  1427. while (m_pos < 0)
  1428. {
  1429. if (m_btree->IsRoot(m_current))
  1430. return FALSE;
  1431. RETURN_VALUE_IF_ERROR(m_btree->m_allocator.Assign(m_key_buf + m_btree->m_allocator.size, m_current->GetItem(0)), FALSE);
  1432. if ((m_current = m_btree->GotoB(m_current->parent, m_current)) == NULL)
  1433. {
  1434. m_last_error = OpStatus::ERR;
  1435. return FALSE;
  1436. }
  1437. m_pos = BinarySearch(m_current, m_key_buf + m_btree->m_allocator.size, m_btree->m_allocator) - 1;
  1438. }
  1439. return !_Beginning();
  1440. }
  1441. int BTreeBase::Result::_Count(void) const
  1442. {
  1443. if (m_current == NULL || m_current->size == 0)
  1444. return 0;
  1445. if (m_oper == operatorEQ)
  1446. {
  1447. if (m_pos > m_current->size || m_pos < 0) // used to be somewhere else, but somebody called Next() or Prev()
  1448. return 1;
  1449. return m_btree->m_allocator.Compare(m_key_buf, m_current->GetItem(m_pos)) ? 1 : 0;
  1450. }
  1451. return -1;
  1452. }
  1453. BOOL BTreeBase::Result::_End(void) const
  1454. {
  1455. if (_Count() == 0 || m_pos >= m_current->size)
  1456. return TRUE;
  1457. if (m_pos < 0) // !Empty && Beginning
  1458. return FALSE;
  1459. if (m_oper == operatorLT && !m_btree->m_allocator.Compare(m_current->GetItem(m_pos), m_key_buf))
  1460. return TRUE;
  1461. if (m_oper == operatorLE && m_btree->m_allocator.Compare(m_key_buf, m_current->GetItem(m_pos)))
  1462. return TRUE;
  1463. return FALSE;
  1464. }
  1465. BOOL BTreeBase::Result::_Beginning(void) const
  1466. {
  1467. if (_Count() == 0 || m_pos < 0)
  1468. return TRUE;
  1469. if (m_pos >= m_current->size) // !Empty && End
  1470. return FALSE;
  1471. if (m_oper == operatorGT && !m_btree->m_allocator.Compare(m_key_buf, m_current->GetItem(m_pos)))
  1472. return TRUE;
  1473. if (m_oper == operatorGE && m_btree->m_allocator.Compare(m_current->GetItem(m_pos), m_key_buf))
  1474. return TRUE;
  1475. return FALSE;
  1476. }
  1477. const void *BTreeBase::Result::_Get(void)
  1478. {
  1479. OP_ASSERT(!_End());
  1480. return m_current->GetItem(m_pos);
  1481. }
  1482. class BlockReader : public BlockStorage
  1483. {
  1484. public:
  1485. CHECK_RESULT(OP_STATUS ReadPtr(OpFileLength pos, OpFileLength *ptr))
  1486. {
  1487. *ptr = 0;
  1488. RETURN_IF_ERROR(m_file->SetFilePos(pos));
  1489. return ReadOFL(m_file, ptr);
  1490. }
  1491. CHECK_RESULT(OP_STATUS ReadDeletedBitField(void *bf, OpFileLength pos))
  1492. {
  1493. RETURN_IF_ERROR(m_file->SetFilePos(pos));
  1494. return BlockStorage::ReadFully(m_file, bf, m_blocksize);
  1495. }
  1496. };
  1497. OP_BOOLEAN PoolBase::CheckConsistency(UINT32 root, BOOL thorough)
  1498. {
  1499. int block_len, block_size, item_size, num_items, num_items2;
  1500. char *block, *block2, *deleted, *refs;
  1501. OpAutoArray<char> block_a, block2_a, deleted_a, refs_a;
  1502. int item;
  1503. OpFileLength next, fsize, pos;
  1504. UINT32 ptr, ptr2, rightmost, num_blocks, b, b2;
  1505. block_size = m_storage.GetBlockSize();
  1506. fsize = m_storage.GetFileSize();
  1507. num_blocks = (UINT32)(fsize/block_size);
  1508. item_size = (int)m_allocator.size_align32 + 4;
  1509. block_a .reset(block = OP_NEWA(char, block_size));
  1510. block2_a .reset(block2 = OP_NEWA(char, block_size));
  1511. deleted_a.reset(deleted = OP_NEWA(char, (num_blocks + 7) / 8));
  1512. refs_a .reset(refs = OP_NEWA(char, (num_blocks + 7) / 8));
  1513. RETURN_OOM_IF_NULL(block);
  1514. RETURN_OOM_IF_NULL(block2);
  1515. RETURN_OOM_IF_NULL(deleted);
  1516. RETURN_OOM_IF_NULL(refs);
  1517. op_memset(deleted, 0, (num_blocks + 7) / 8);
  1518. op_memset(refs, 0, (num_blocks + 7) / 8);
  1519. RETURN_IF_ERROR(((BlockReader *)&m_storage)->ReadPtr(0, &next));
  1520. if (next >= fsize)
  1521. return OpBoolean::IS_FALSE; // corrupted file
  1522. for (b = 1; b < num_blocks; b += 8 * block_size + 1)
  1523. {
  1524. pos = (OpFileLength)b * block_size;
  1525. RETURN_IF_ERROR(((BlockReader *)&m_storage)->ReadDeletedBitField(block, pos));
  1526. for (b2 = b+1; b2 < b+1+8*block_size && b2 < num_blocks; ++b2)
  1527. if (block[(b2 - b - 1) / 8] & (1 << ((b2 - b - 1) % 8)))
  1528. deleted[b2 / 8] |= (1 << (b2 % 8));
  1529. deleted[b / 8] |= (1 << (b % 8));
  1530. }
  1531. op_memcpy(refs, deleted, (num_blocks + 7) / 8);
  1532. for (b = 2; b < num_blocks; ++b)
  1533. {
  1534. pos = (OpFileLength)b * block_size;
  1535. if (deleted[b / 8] & (1 << (b % 8)))
  1536. {
  1537. if (m_storage.IsStartBlock(pos))
  1538. return OpBoolean::IS_FALSE;
  1539. continue;
  1540. }
  1541. if (m_storage.IsStartBlocksSupported() && !m_storage.IsStartBlock(pos))
  1542. return OpBoolean::IS_FALSE;
  1543. block_len = m_storage.DataLength(pos);
  1544. if (block_len == 0 && m_head != NULL)
  1545. continue; // empty btree might be tolerated for reserved pages during a transaction
  1546. if (block_len > block_size - 12 || block_len < 4 + item_size || (block_len - 4) % item_size != 0)
  1547. return OpBoolean::IS_FALSE;
  1548. num_items = (block_len - 4) / item_size;
  1549. if (root != 0 && b != root && num_items + 1 < ((block_size - 12 - 4) / item_size) / 2)
  1550. return OpBoolean::IS_FALSE;
  1551. if (!m_storage.Read(block, block_len, pos))
  1552. return OpStatus::ERR_NO_ACCESS;
  1553. rightmost = *(UINT32 *)(block);
  1554. for (item = 0; item <= num_items; ++item)
  1555. {
  1556. ptr = item==num_items ? rightmost : *(UINT32 *)(block + 4 + item * item_size);
  1557. if ( ((rightmost == 0) != (ptr == 0)) // ptr_error
  1558. || (ptr >= num_blocks || (ptr - 1) % (8 * block_size + 1) == 0 || ptr == b) // ptr_bad_error
  1559. || (ptr && (deleted[ptr / 8] & (1 << (ptr % 8))) != 0) // ptr_deleted_error
  1560. || (item > 0 && item < num_items &&
  1561. !m_allocator.Compare(block + 8 + (item - 1) * item_size, block + 8 + item * item_size))) // sort_error
  1562. return OpBoolean::IS_FALSE;
  1563. if (ptr) {
  1564. if ((refs[ptr / 8] & (1 << (ptr % 8))) != 0) // ref_error
  1565. return OpBoolean::IS_FALSE;
  1566. refs[ptr / 8] |= (1 << (ptr % 8));
  1567. }
  1568. if (ptr && thorough)
  1569. {
  1570. pos = (OpFileLength)ptr * block_size;
  1571. block_len = m_storage.DataLength(pos);
  1572. if (block_len > block_size - 12 || block_len < 4 + item_size || (block_len - 4) % item_size != 0)
  1573. return OpBoolean::IS_FALSE;
  1574. num_items2 = (block_len - 4) / item_size;
  1575. if (!m_storage.Read(block2, block_len, pos))
  1576. return OpStatus::ERR_NO_ACCESS;
  1577. if ( (item > 0 && !m_allocator.Compare(block + 8 + (item - 1) * item_size, block2 + 8))
  1578. || (item < num_items && !m_allocator.Compare(block2 + 8 + (num_items2 - 1) * item_size, block + 8 + item * item_size)))
  1579. return OpBoolean::IS_FALSE;
  1580. if (item > 0) {
  1581. ptr2 = ptr;
  1582. LoopDetector<UINT32> loopDetector;
  1583. while (ptr2)
  1584. {
  1585. pos = (OpFileLength)ptr2 * block_size;
  1586. block_len = m_storage.DataLength(pos);
  1587. if (block_len > block_size - 12 || block_len < 4 + item_size || (block_len - 4) % item_size != 0)
  1588. return OpBoolean::IS_FALSE;
  1589. if (!m_storage.Read(block2, block_len, pos))
  1590. return OpStatus::ERR_NO_ACCESS;
  1591. ptr2 = *(UINT32 *)(block2 + 4);
  1592. if (ptr2 &&
  1593. ((ptr2 - 1) % (8 * block_size + 1) == 0 ||
  1594. (deleted[ptr2 / 8] & (1 << (ptr2 % 8))) != 0))
  1595. return OpBoolean::IS_FALSE;
  1596. if (!m_allocator.Compare(block + 8 + (item - 1) * item_size, block2 + 8))
  1597. return OpBoolean::IS_FALSE;
  1598. if (OpStatus::IsError(loopDetector.CheckNext(ptr2)))
  1599. return OpBoolean::IS_FALSE;
  1600. }
  1601. }
  1602. if (item < num_items)
  1603. {
  1604. ptr2 = ptr;
  1605. LoopDetector<UINT32> loopDetector;
  1606. while (ptr2)
  1607. {
  1608. pos = (OpFileLength)ptr2 * block_size;
  1609. block_len = m_storage.DataLength(pos);
  1610. if (block_len > block_size - 12 || block_len < 4 + item_size || (block_len - 4) % item_size != 0)
  1611. return OpBoolean::IS_FALSE;
  1612. num_items2 = (block_len - 4) / item_size;
  1613. if (!m_storage.Read(block2, block_len, pos))
  1614. return OpStatus::ERR_NO_ACCESS;
  1615. ptr2 = *(UINT32 *)(block2);
  1616. if (ptr2 &&
  1617. ((ptr2 - 1) % (8 * block_size + 1) == 0 ||
  1618. (deleted[ptr2 / 8] & (1 << (ptr2 % 8))) != 0))
  1619. return OpBoolean::IS_FALSE;
  1620. if (!m_allocator.Compare(block2 + 8 + (num_items2 - 1) * item_size, block + 8 + item * item_size))
  1621. return OpBoolean::IS_FALSE;
  1622. if (OpStatus::IsError(loopDetector.CheckNext(ptr2)))
  1623. return OpBoolean::IS_FALSE;
  1624. }
  1625. }
  1626. }
  1627. }
  1628. }
  1629. if (root != 0)
  1630. {
  1631. // Check that all branches are referenced, deleted or a deleted-bit-field
  1632. for (b = 2; b < num_blocks; ++b)
  1633. if (b != root && (refs[b / 8] & (1 << (b % 8))) == 0)
  1634. return OpBoolean::IS_FALSE;
  1635. }
  1636. return OpBoolean::IS_TRUE;
  1637. }
  1638. #if defined(LINGOGI) && (defined(_DEBUG) || defined(SELFTEST) || defined(SEARCH_ENGINE_LOG))
  1639. OP_BOOLEAN PoolBase::PrintBTree(UINT32 root)
  1640. {
  1641. int block_len, block_size, item_size, num_items, num_items2;
  1642. char *block, *block2, *deleted, *refs, *referr;
  1643. OpAutoArray<char> block_a, block2_a, deleted_a, refs_a, referr_a;
  1644. int item;
  1645. int ptr_errors, sort_errors, size_errors, ref_errors;
  1646. BOOL ptr_error, ptr_bad_error, ptr_deleted_error, ptr_sort_error, ptr_sort_recursive_error, sort_error, size_error, ref_error;
  1647. OpFileLength next, fsize, pos;
  1648. UINT32 ptr, ptr2, rightmost, num_blocks, b, b2;
  1649. ptr_errors = 0;
  1650. sort_errors = 0;
  1651. size_errors = 0;
  1652. ref_errors = 0;
  1653. block_size = m_storage.GetBlockSize();
  1654. fsize = m_storage.GetFileSize();
  1655. num_blocks = (UINT32)(fsize/block_size);
  1656. item_size = (int)m_allocator.size_align32 + 4;
  1657. block_a .reset(block = OP_NEWA(char, block_size));
  1658. block2_a .reset(block2 = OP_NEWA(char, block_size));
  1659. deleted_a.reset(deleted = OP_NEWA(char, (num_blocks + 7) / 8));
  1660. refs_a .reset(refs = OP_NEWA(char, (num_blocks + 7) / 8));
  1661. referr_a .reset(referr = OP_NEWA(char, (num_blocks + 7) / 8));
  1662. RETURN_OOM_IF_NULL(block);
  1663. RETURN_OOM_IF_NULL(block2);
  1664. RETURN_OOM_IF_NULL(deleted);
  1665. RETURN_OOM_IF_NULL(refs);
  1666. RETURN_OOM_IF_NULL(referr);
  1667. op_memset(deleted, 0, (num_blocks + 7) / 8);
  1668. op_memset(refs, 0, (num_blocks + 7) / 8);
  1669. op_memset(referr, 0, (num_blocks + 7) / 8);
  1670. RETURN_IF_ERROR(((BlockReader *)&m_storage)->ReadPtr(0, &next));
  1671. if (next >= fsize)
  1672. return OpBoolean::IS_FALSE; // corrupted file
  1673. printf("\n<table border=0>\n");
  1674. printf("<tr><td colspan=7>Legend:</td></tr>\n");
  1675. printf("<tr><td><b>000:</b></td><td></td><td></td><td colspan=7>Branch number</td></tr>\n");
  1676. printf("<tr><td><b>000:</b></td><td colspan=2>Free</td><td colspan=7>Free branch</td></tr>\n");
  1677. printf("<tr><td bgcolor=#FF40FF><b>000:</b></td><td></td><td></td><td colspan=7>Bad block size</td></tr>\n");
  1678. printf("<tr><td bgcolor=#FFC0FF><b>000:</b></td><td></td><td></td><td colspan=7>Bad number of items in branch</td></tr>\n");
  1679. printf("<tr><td bgcolor=#808080><b>000:</b></td><td></td><td></td><td colspan=7>Unreferenced (leaked) branch</td></tr>\n");
  1680. printf("<tr><td></td><td><b>000</b></td><td></td><td colspan=7>Pointer to child branch</td></tr>\n");
  1681. printf("<tr><td></td><td><b>---</b></td><td></td><td colspan=7>Leaf</td></tr>\n");
  1682. printf("<tr><td></td><td bgcolor=#FF40FF><b>000</b></td><td></td><td colspan=7>Illegal pointer value</td></tr>\n");
  1683. printf("<tr><td></td><td bgcolor=#808080><b>000</b></td><td></td><td colspan=7>Pointer to free branch</td></tr>\n");
  1684. printf("<tr><td></td><td bgcolor=#FF8080><b>000</b></td><td></td><td colspan=7>Pointer used more than once</td></tr>\n");
  1685. printf("<tr><td></td><td bgcolor=#FFC080><b>000</b></td><td></td><td colspan=7>Child branch sort error</td></tr>\n");
  1686. printf("<tr><td></td><td bgcolor=#FFC0FF><b>000</b></td><td></td><td colspan=7>Recursive child branch sort error</td></tr>\n");
  1687. printf("<tr><td></td><td bgcolor=#FFFF80><b>000</b></td><td></td><td colspan=7>Mixed leaf/nonleaf pointers</td></tr>\n");
  1688. printf("<tr><td></td><td></td><td><b>0000</b></td><td colspan=7>Data</td></tr>\n");
  1689. printf("<tr><td></td><td></td><td bgcolor=#FFC080><b>0000</b></td><td colspan=7>Data sort error</td></tr>\n");
  1690. printf("<tr></tr>\n");
  1691. printf("<tr><td colspan=7>Btree:</td></tr>\n");
  1692. for (b = 1; b < num_blocks; b += 8 * block_size + 1)
  1693. {
  1694. pos = (OpFileLength)b * block_size;
  1695. RETURN_IF_ERROR(((BlockReader *)&m_storage)->ReadDeletedBitField(block, pos));
  1696. for (b2 = b+1; b2 < b+1+8*block_size && b2 < num_blocks; ++b2)
  1697. if (block[(b2 - b - 1) / 8] & (1 << ((b2 - b - 1) % 8)))
  1698. deleted[b2 / 8] |= (1 << (b2 % 8));
  1699. }
  1700. op_memcpy(refs, deleted, (num_blocks + 7) / 8);
  1701. for (b = 2; b < num_blocks; ++b)
  1702. {
  1703. if ((b - 1) % (8 * block_size + 1) == 0)
  1704. continue;
  1705. if (deleted[b / 8] & (1 << (b % 8)))
  1706. continue;
  1707. pos = (OpFileLength)b * block_size;
  1708. block_len = m_storage.DataLength(pos);
  1709. if (block_len > block_size - 12 || block_len < 4 + item_size || (block_len - 4) % item_size != 0)
  1710. continue;
  1711. num_items = (block_len - 4) / item_size;
  1712. if (!m_storage.Read(block, block_len, pos))
  1713. return OpStatus::ERR_NO_ACCESS;
  1714. rightmost = *(UINT32 *)(block);
  1715. for (item = 0; item <= num_items; ++item)
  1716. {
  1717. ptr = item==num_items ? rightmost : *(UINT32 *)(block + 4 + item * item_size);
  1718. if (ptr != 0 && ptr < num_blocks) {
  1719. if ((refs[ptr / 8] & (1 << (ptr % 8))) != 0)
  1720. referr[ptr / 8] |= (1 << (ptr % 8));
  1721. refs[ptr / 8] |= (1 << (ptr % 8));
  1722. }
  1723. }
  1724. }
  1725. for (b = 2; b < num_blocks; ++b)
  1726. {
  1727. if ((b - 1) % (8 * block_size + 1) == 0)
  1728. continue;
  1729. if (deleted[b / 8] & (1 << (b % 8)))
  1730. {
  1731. printf("<tr bgcolor=#%s><td><b><a id=\"%03x\" href=\"#r%03x\">%03x:</a></b></td><td colspan=5>Free</td></tr>\n", b&1 ? "FFFFFF" : "F0F0FF", b, b, b);
  1732. continue;
  1733. }
  1734. pos = (OpFileLength)b * block_size;
  1735. block_len = m_storage.DataLength(pos);
  1736. if (block_len == 0 && m_head != NULL)
  1737. {
  1738. printf("<tr bgcolor=#%s><td><b><a id=\"%03x\" href=\"#r%03x\">%03x:</a></b></td></tr>\n", b&1 ? "FFFFFF" : "F0F0FF", b, b, b);
  1739. continue; // empty btree might be tolerated for reserved pages during a transaction
  1740. }
  1741. if (block_len > block_size - 12 || block_len < 4 + item_size || (block_len - 4) % item_size != 0)
  1742. {
  1743. ++size_errors;
  1744. printf("<tr bgcolor=#%s><td bgcolor=#FF40FF><b><a id=\"%03x\" href=\"#r%03x\">%03x:</a></b></td><td colspan=5>size = (%d-4)/%d</td></tr>\n", b&1 ? "FFFFFF" : "F0F0FF", b, b, b, block_len, item_size);
  1745. continue;
  1746. }
  1747. num_items = (block_len - 4) / item_size;
  1748. size_error = root != 0 && b != root && num_items + 1 < ((block_size - 12 - 4) / item_size) / 2;
  1749. if (size_error)
  1750. ++size_errors;
  1751. ref_error = root != 0 && b != root && (refs[b / 8] & (1 << (b % 8))) == 0;
  1752. if (ref_error)
  1753. ++ref_errors;
  1754. printf("<tr bgcolor=#%s><td%s><b><a id=\"%03x\" href=\"#r%03x\">%03x:</a></b></td>",
  1755. b&1 ? "FFFFFF" : "F0F0FF",
  1756. ref_error ? " bgcolor=#808080" :
  1757. size_error ? " bgcolor=#FFC0FF" : "",
  1758. b, b, b);
  1759. if (!m_storage.Read(block, block_len, pos))
  1760. return OpStatus::ERR_NO_ACCESS;
  1761. rightmost = *(UINT32 *)(block);
  1762. for (item = 0; item <= num_items; ++item)
  1763. {
  1764. ptr = item==num_items ? rightmost : *(UINT32 *)(block + 4 + item * item_size);
  1765. ptr_error = (rightmost == 0) != (ptr == 0);
  1766. ptr_bad_error = ptr >= num_blocks || (ptr - 1) % (8 * block_size + 1) == 0 || ptr == b;
  1767. ptr_deleted_error = ptr && !ptr_bad_error && (deleted[ptr / 8] & (1 << (ptr % 8))) != 0;
  1768. sort_error = item > 0 && item < num_items &&
  1769. !m_allocator.Compare(block + 8 + (item - 1) * item_size, block + 8 + item * item_size);
  1770. ref_error = ptr && !ptr_bad_error && (referr[ptr / 8] & (1 << (ptr % 8))) != 0;
  1771. ptr_sort_error = FALSE;
  1772. ptr_sort_recursive_error = FALSE;
  1773. if (ptr && !ptr_bad_error && !ptr_deleted_error && !ref_error && !ptr_error)
  1774. {
  1775. pos = (OpFileLength)ptr * block_size;
  1776. block_len = m_storage.DataLength(pos);
  1777. if (block_len <= block_size - 12 && block_len >= 4 + item_size && (block_len - 4) % item_size == 0)
  1778. {
  1779. num_items2 = (block_len - 4) / item_size;
  1780. if (!m_storage.Read(block2, block_len, pos))
  1781. return OpStatus::ERR_NO_ACCESS;
  1782. ptr_sort_error = (item > 0 && !m_allocator.Compare(block + 8 + (item - 1) * item_size, block2 + 8))
  1783. || (item < num_items && !m_allocator.Compare(block2 + 8 + (num_items2 - 1) * item_size, block + 8 + item * item_size));
  1784. }
  1785. if (item > 0 && !ptr_sort_error) {
  1786. BOOL read_ok;
  1787. ptr2 = ptr;
  1788. LoopDetector<INT32> loopDetector;
  1789. while (ptr2 && !ptr_sort_recursive_error)
  1790. {
  1791. UINT32 prev_ptr2 = ptr2;
  1792. pos = (OpFileLength)ptr2 * block_size;
  1793. block_len = m_storage.DataLength(pos);
  1794. ptr2 = 0;
  1795. read_ok = FALSE;
  1796. if (block_len <= block_size - 12 && block_len >= 4 + item_size && (block_len - 4) % item_size == 0)
  1797. {
  1798. if (!m_storage.Read(block2, block_len, pos))
  1799. return OpStatus::ERR_NO_ACCESS;
  1800. read_ok = TRUE;
  1801. ptr2 = *(UINT32 *)(block2 + 4);
  1802. if (ptr2 &&
  1803. (ptr2 == prev_ptr2 || ptr2 >= num_blocks || (ptr2 - 1) % (8 * block_size + 1) == 0 ||
  1804. (deleted[ptr2 / 8] & (1 << (ptr2 % 8))) != 0))
  1805. {
  1806. ptr2 = 0;
  1807. read_ok = FALSE;
  1808. }
  1809. }
  1810. ptr_sort_recursive_error = read_ok && !m_allocator.Compare(block + 8 + (item - 1) * item_size, block2 + 8);
  1811. if (OpStatus::IsError(loopDetector.CheckNext(ptr2)))
  1812. break;
  1813. }
  1814. }
  1815. if (item < num_items && !ptr_sort_error && !ptr_sort_recursive_error)
  1816. {
  1817. BOOL read_ok;
  1818. ptr2 = ptr;
  1819. LoopDetector<INT32> loopDetector;
  1820. while (ptr2 && !ptr_sort_recursive_error)
  1821. {
  1822. UINT32 prev_ptr2 = ptr2;
  1823. pos = (OpFileLength)ptr2 * block_size;
  1824. block_len = m_storage.DataLength(pos);
  1825. ptr2 = 0;
  1826. read_ok = FALSE;
  1827. if (block_len <= block_size - 12 && block_len >= 4 + item_size && (block_len - 4) % item_size == 0)
  1828. {
  1829. num_items2 = (block_len - 4) / item_size;
  1830. if (!m_storage.Read(block2, block_len, pos))
  1831. return OpStatus::ERR_NO_ACCESS;
  1832. read_ok = TRUE;
  1833. ptr2 = *(UINT32 *)(block2);
  1834. if (ptr2 &&
  1835. (ptr2 == prev_ptr2 || ptr2 >= num_blocks || (ptr2 - 1) % (8 * block_size + 1) == 0 ||
  1836. (deleted[ptr2 / 8] & (1 << (ptr2 % 8))) != 0))
  1837. {
  1838. ptr2 = 0;
  1839. read_ok = FALSE;
  1840. }
  1841. }
  1842. ptr_sort_recursive_error = read_ok && !m_allocator.Compare(block2 + 8 + (num_items2 - 1) * item_size, block + 8 + item * item_size);
  1843. if (OpStatus::IsError(loopDetector.CheckNext(ptr2)))
  1844. break;
  1845. }
  1846. }
  1847. }
  1848. if (ptr)
  1849. printf("<td%s><b><a id=\"r%03x\" href=\"#%03x\">%03x</a></b></td>",
  1850. ptr_bad_error ? " bgcolor=#FF40FF" :
  1851. ptr_deleted_error ? " bgcolor=#808080" :
  1852. ref_error ? " bgcolor=#FF8080" :
  1853. ptr_error ? " bgcolor=#FFFF80" :
  1854. ptr_sort_error ? " bgcolor=#FFC080" :
  1855. ptr_sort_recursive_error ? " bgcolor=#FFC0FF" : "", ptr, ptr, ptr);
  1856. else
  1857. printf("<td%s><b>---</b></td>",
  1858. ptr_error ? " bgcolor=#FFFF80" : "");
  1859. if (item < num_items) {
  1860. UINT32 data = *(UINT32 *)(block + 8 + item * item_size) >> 16;
  1861. printf("<td%s>%04x</td>",
  1862. sort_error ? " bgcolor=#FFC080" : "", data);
  1863. }
  1864. if (ptr_error || ptr_bad_error || ptr_deleted_error)
  1865. ++ptr_errors;
  1866. if (sort_error)
  1867. ++sort_errors;
  1868. if (ptr_sort_error || ptr_sort_recursive_error)
  1869. ++sort_errors;
  1870. if (ref_error)
  1871. ++ref_errors;
  1872. }
  1873. printf("</tr>\n");
  1874. }
  1875. printf("</table>\n");
  1876. return (ptr_errors + sort_errors + size_errors + ref_errors == 0) ? OpBoolean::IS_TRUE : OpBoolean::IS_FALSE;
  1877. }
  1878. #endif // defined(LINGOGI) && (defined(_DEBUG) || defined(SELFTEST) || defined(SEARCH_ENGINE_LOG))
  1879. BSCache::Item *PoolBase::NewMemoryItem(int id, Item *rbranch, int rnode, unsigned short nur)
  1880. {
  1881. BTreeBase::BTreeBranch* branch = OP_NEW(BTreeBase::BTreeBranch, (this, -id, (BTreeBase::BTreeBranch *)rbranch, nur));
  1882. if (branch != NULL && OpStatus::IsError(branch->Construct(&m_storage)))
  1883. {
  1884. OP_DELETE(branch);
  1885. branch = NULL;
  1886. }
  1887. return branch;
  1888. }
  1889. BSCache::Item *PoolBase::NewDiskItem(OpFileLength id, unsigned short nur)
  1890. {
  1891. BTreeBase::BTreeBranch* branch = OP_NEW(BTreeBase::BTreeBranch, (this, id, NULL, nur));
  1892. if (branch != NULL && OpStatus::IsError(branch->Construct(&m_storage)))
  1893. {
  1894. OP_DELETE(branch);
  1895. branch = NULL;
  1896. }
  1897. return branch;
  1898. }
  1899. #undef CHECK_INTEGRITY
  1900. #endif // SEARCH_ENGINE