idl_new.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* New IDL code for new indexing scheme */
  13. /* Note to future editors:
  14. This file is now full of redundant code
  15. (the DB_ALLIDS_ON_WRITE==true and DB_USE_BULK_FETCH==false code).
  16. It should be stripped out at the beginning of a
  17. major release cycle.
  18. */
  19. #include "back-ldbm.h"
  20. static char* filename = "idl_new.c";
  21. /* Bulk fetch feature first in DB 3.3 */
  22. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 3300
  23. #define DB_USE_BULK_FETCH 1
  24. #define BULK_FETCH_BUFFER_SIZE (8*1024)
  25. #else
  26. #undef DB_USE_BULK_FETCH
  27. #endif
  28. /* We used to implement allids for inserts, but that's a bad idea.
  29. Why ? Because:
  30. 1) Allids results in hard to understand query behavior.
  31. 2) The get() calls needed to check for allids on insert cost performance.
  32. 3) Tests show that there is no significant performance benefit to having allids on writes,
  33. either for updates or searches.
  34. Set this to revert to that code */
  35. /* #undef DB_ALLIDS_ON_WRITE */
  36. /* We still enforce allids threshold on reads, to save time and space fetching vast id lists */
  37. #define DB_ALLIDS_ON_READ 1
  38. #if !defined(DB_NEXT_DUP)
  39. #define DB_NEXT_DUP 0
  40. #endif
  41. #if !defined(DB_GET_BOTH)
  42. #define DB_GET_BOTH 0
  43. #endif
  44. /* Structure used to hide private idl-specific data in the attrinfo object */
  45. struct idl_private {
  46. size_t idl_allidslimit;
  47. int dummy;
  48. };
  49. static int idl_tune = DEFAULT_IDL_TUNE; /* tuning parameters for IDL code */
  50. /* Currently none for new IDL code */
  51. #if defined(DB_ALLIDS_ON_WRITE)
  52. static int idl_new_store_allids(backend *be, DB *db, DBT *key, DB_TXN *txn);
  53. #endif
  54. void idl_new_set_tune(int val)
  55. {
  56. idl_tune = val;
  57. }
  58. int idl_new_get_tune() {
  59. return idl_tune;
  60. }
  61. size_t idl_new_get_allidslimit(struct attrinfo *a, int allidslimit)
  62. {
  63. idl_private *priv = NULL;
  64. if (allidslimit) {
  65. return (size_t)allidslimit;
  66. }
  67. PR_ASSERT(NULL != a);
  68. PR_ASSERT(NULL != a->ai_idl);
  69. priv = a->ai_idl;
  70. return priv->idl_allidslimit;
  71. }
  72. int idl_new_exceeds_allidslimit(size_t count, struct attrinfo *a, int allidslimit)
  73. {
  74. size_t limit = idl_new_get_allidslimit(a, allidslimit);
  75. return (limit != (size_t)-1) && (count > limit);
  76. }
  77. /* routine to initialize the private data used by the IDL code per-attribute */
  78. int idl_new_init_private(backend *be,struct attrinfo *a)
  79. {
  80. idl_private *priv = NULL;
  81. struct ldbminfo *li = (struct ldbminfo *)be->be_database->plg_private;
  82. PR_ASSERT(NULL != a);
  83. PR_ASSERT(NULL == a->ai_idl);
  84. priv = (idl_private*) slapi_ch_calloc(sizeof(idl_private),1);
  85. if (NULL == priv) {
  86. return -1; /* Memory allocation failure */
  87. }
  88. priv->idl_allidslimit = (size_t)li->li_allidsthreshold;
  89. /* Initialize the structure */
  90. a->ai_idl = (void*)priv;
  91. return 0;
  92. }
  93. /* routine to release resources used by IDL private data structure */
  94. int idl_new_release_private(struct attrinfo *a)
  95. {
  96. PR_ASSERT(NULL != a);
  97. if (NULL != a->ai_idl)
  98. {
  99. slapi_ch_free((void**)&(a->ai_idl) );
  100. }
  101. return 0;
  102. }
  103. IDList *
  104. idl_new_fetch(
  105. backend *be,
  106. DB* db,
  107. DBT *inkey,
  108. DB_TXN *txn,
  109. struct attrinfo *a,
  110. int *flag_err,
  111. int allidslimit
  112. )
  113. {
  114. int ret = 0;
  115. int idl_rc = 0;
  116. DBC *cursor = NULL;
  117. IDList *idl = NULL;
  118. DBT key;
  119. DBT data;
  120. ID id = 0;
  121. size_t count = 0;
  122. #ifdef DB_USE_BULK_FETCH
  123. /* beware that a large buffer on the stack might cause a stack overflow on some platforms */
  124. char buffer[BULK_FETCH_BUFFER_SIZE];
  125. void *ptr;
  126. DBT dataret;
  127. #endif
  128. back_txn s_txn;
  129. struct ldbminfo *li = (struct ldbminfo *)be->be_database->plg_private;
  130. if (NEW_IDL_NOOP == *flag_err)
  131. {
  132. *flag_err = 0;
  133. return NULL;
  134. }
  135. dblayer_txn_init(li, &s_txn);
  136. if (txn) {
  137. dblayer_read_txn_begin(be, txn, &s_txn);
  138. }
  139. /* Make a cursor */
  140. ret = db->cursor(db,txn,&cursor,0);
  141. if (0 != ret) {
  142. ldbm_nasty(filename,1,ret);
  143. cursor = NULL;
  144. goto error;
  145. }
  146. memset(&data, 0, sizeof(data));
  147. #ifdef DB_USE_BULK_FETCH
  148. data.ulen = sizeof(buffer);
  149. data.size = sizeof(buffer);
  150. data.data = buffer;
  151. data.flags = DB_DBT_USERMEM;
  152. memset(&dataret, 0, sizeof(dataret));
  153. #else
  154. data.ulen = sizeof(id);
  155. data.size = sizeof(id);
  156. data.data = &id;
  157. data.flags = DB_DBT_USERMEM;
  158. #endif
  159. /*
  160. * We're not expecting the key to change in value
  161. * so we can just use the input key as a buffer.
  162. * This avoids memory management of the key.
  163. */
  164. memset(&key, 0, sizeof(key));
  165. key.ulen = inkey->size;
  166. key.size = inkey->size;
  167. key.data = inkey->data;
  168. key.flags = DB_DBT_USERMEM;
  169. /* Position cursor at the first matching key */
  170. #ifdef DB_USE_BULK_FETCH
  171. ret = cursor->c_get(cursor,&key,&data,DB_SET|DB_MULTIPLE);
  172. #else
  173. ret = cursor->c_get(cursor,&key,&data,DB_SET);
  174. #endif
  175. if (0 != ret) {
  176. if (DB_NOTFOUND != ret) {
  177. #ifdef DB_USE_BULK_FETCH
  178. if (ret == DB_BUFFER_SMALL) {
  179. LDAPDebug(LDAP_DEBUG_ANY, "database index is corrupt; "
  180. "data item for key %s is too large for our buffer "
  181. "(need=%d actual=%d)\n",
  182. key.data, data.size, data.ulen);
  183. }
  184. #endif
  185. ldbm_nasty(filename,2,ret);
  186. }
  187. goto error; /* Not found is OK, return NULL IDL */
  188. }
  189. /* Iterate over the duplicates, amassing them into an IDL */
  190. #ifdef DB_USE_BULK_FETCH
  191. for (;;) {
  192. ID lastid = 0;
  193. DB_MULTIPLE_INIT(ptr, &data);
  194. for (;;) {
  195. DB_MULTIPLE_NEXT(ptr, &data, dataret.data, dataret.size);
  196. if (dataret.data == NULL) break;
  197. if (ptr == NULL) break;
  198. if (*(int32_t *)ptr < -1) {
  199. LDAPDebug1Arg(LDAP_DEBUG_TRACE, "DB_MULTIPLE buffer is corrupt; "
  200. "next offset [%d] is less than zero\n",
  201. *(int32_t *)ptr);
  202. /* retry the read */
  203. break;
  204. }
  205. if (dataret.size != sizeof(ID)) {
  206. LDAPDebug(LDAP_DEBUG_ANY, "database index is corrupt; "
  207. "key %s has a data item with the wrong size (%d)\n",
  208. key.data, dataret.size, 0);
  209. goto error;
  210. }
  211. memcpy(&id, dataret.data, sizeof(ID));
  212. if (id == lastid) { /* dup */
  213. LDAPDebug1Arg(LDAP_DEBUG_TRACE, "Detected duplicate id "
  214. "%d due to DB_MULTIPLE error - skipping\n",
  215. id);
  216. continue; /* get next one */
  217. }
  218. /* note the last id read to check for dups */
  219. lastid = id;
  220. /* we got another ID, add it to our IDL */
  221. idl_rc = idl_append_extend(&idl, id);
  222. if (idl_rc) {
  223. LDAPDebug(LDAP_DEBUG_ANY, "unable to extend id list (err=%d)\n", idl_rc, 0, 0);
  224. idl_free(&idl);
  225. goto error;
  226. }
  227. count++;
  228. }
  229. LDAPDebug(LDAP_DEBUG_TRACE, "bulk fetch buffer nids=%d\n", count, 0, 0);
  230. #if defined(DB_ALLIDS_ON_READ)
  231. /* enforce the allids read limit */
  232. if ((NEW_IDL_NO_ALLID != *flag_err) && (NULL != a) &&
  233. (idl != NULL) && idl_new_exceeds_allidslimit(count, a, allidslimit)) {
  234. idl->b_nids = 1;
  235. idl->b_ids[0] = ALLID;
  236. ret = DB_NOTFOUND; /* fool the code below into thinking that we finished the dups */
  237. LDAPDebug(LDAP_DEBUG_BACKLDBM, "search for key for attribute index %s "
  238. "exceeded allidslimit %d - count is %d\n",
  239. a->ai_type, allidslimit, count);
  240. break;
  241. }
  242. #endif
  243. ret = cursor->c_get(cursor,&key,&data,DB_NEXT_DUP|DB_MULTIPLE);
  244. if (0 != ret) {
  245. break;
  246. }
  247. }
  248. #else
  249. for (;;) {
  250. ret = cursor->c_get(cursor,&key,&data,DB_NEXT_DUP);
  251. count++;
  252. if (0 != ret) {
  253. break;
  254. }
  255. /* we got another ID, add it to our IDL */
  256. idl_rc = idl_append_extend(&idl, id);
  257. if (idl_rc) {
  258. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  259. "unable to extend id list (err=%d)\n", idl_rc);
  260. idl_free(&idl);
  261. goto error;
  262. }
  263. #if defined(DB_ALLIDS_ON_READ)
  264. /* enforce the allids read limit */
  265. if (idl && idl_new_exceeds_allidslimit(count, a, allidslimit)) {
  266. idl->b_nids = 1;
  267. idl->b_ids[0] = ALLID;
  268. ret = DB_NOTFOUND; /* fool the code below into thinking that we finished the dups */
  269. break;
  270. }
  271. #endif
  272. }
  273. #endif
  274. if (ret != DB_NOTFOUND) {
  275. idl_free(&idl);
  276. ldbm_nasty(filename,59,ret);
  277. goto error;
  278. }
  279. ret = 0;
  280. /* check for allids value */
  281. if (idl != NULL && idl->b_nids == 1 && idl->b_ids[0] == ALLID) {
  282. idl_free(&idl);
  283. idl = idl_allids(be);
  284. LDAPDebug(LDAP_DEBUG_TRACE, "idl_new_fetch %s returns allids\n",
  285. key.data, 0, 0);
  286. } else {
  287. LDAPDebug(LDAP_DEBUG_TRACE, "idl_new_fetch %s returns nids=%lu\n",
  288. key.data, (u_long)IDL_NIDS(idl), 0);
  289. }
  290. error:
  291. /* Close the cursor */
  292. if (NULL != cursor) {
  293. int ret2 = cursor->c_close(cursor);
  294. if (ret2) {
  295. ldbm_nasty(filename,3,ret2);
  296. if (!ret) {
  297. /* if cursor close returns DEADLOCK, we must bubble that up
  298. to the higher layers for retries */
  299. ret = ret2;
  300. }
  301. }
  302. }
  303. if (ret) {
  304. dblayer_read_txn_abort(be, &s_txn);
  305. } else {
  306. dblayer_read_txn_commit(be, &s_txn);
  307. }
  308. *flag_err = ret;
  309. return idl;
  310. }
  311. /*
  312. * Perform the range search in the idl layer instead of the index layer
  313. * to improve the performance.
  314. */
  315. IDList *
  316. idl_new_range_fetch(
  317. backend *be,
  318. DB* db,
  319. DBT *lowerkey,
  320. DBT *upperkey,
  321. DB_TXN *txn,
  322. struct attrinfo *ai,
  323. int *flag_err,
  324. int allidslimit,
  325. int sizelimit,
  326. time_t stoptime,
  327. int lookthrough_limit,
  328. int operator
  329. )
  330. {
  331. int ret = 0;
  332. int idl_rc = 0;
  333. DBC *cursor = NULL;
  334. IDList *idl = NULL;
  335. DBT cur_key = {0};
  336. DBT data = {0};
  337. ID id = 0;
  338. size_t count = 0;
  339. #ifdef DB_USE_BULK_FETCH
  340. /* beware that a large buffer on the stack might cause a stack overflow on some platforms */
  341. char buffer[BULK_FETCH_BUFFER_SIZE];
  342. void *ptr;
  343. DBT dataret;
  344. #endif
  345. back_txn s_txn;
  346. struct ldbminfo *li = (struct ldbminfo *)be->be_database->plg_private;
  347. time_t curtime;
  348. void *saved_key = NULL;
  349. if (NULL == flag_err) {
  350. return NULL;
  351. }
  352. *flag_err = 0;
  353. if (NEW_IDL_NOOP == *flag_err) {
  354. return NULL;
  355. }
  356. dblayer_txn_init(li, &s_txn);
  357. if (txn) {
  358. dblayer_read_txn_begin(be, txn, &s_txn);
  359. }
  360. /* Make a cursor */
  361. ret = db->cursor(db, txn, &cursor, 0);
  362. if (0 != ret) {
  363. ldbm_nasty(filename,1,ret);
  364. cursor = NULL;
  365. goto error;
  366. }
  367. memset(&data, 0, sizeof(data));
  368. #ifdef DB_USE_BULK_FETCH
  369. data.ulen = sizeof(buffer);
  370. data.size = sizeof(buffer);
  371. data.data = buffer;
  372. data.flags = DB_DBT_USERMEM;
  373. memset(&dataret, 0, sizeof(dataret));
  374. #else
  375. data.ulen = sizeof(id);
  376. data.size = sizeof(id);
  377. data.data = &id;
  378. data.flags = DB_DBT_USERMEM;
  379. #endif
  380. /*
  381. * We're not expecting the key to change in value
  382. * so we can just use the input key as a buffer.
  383. * This avoids memory management of the key.
  384. */
  385. memset(&cur_key, 0, sizeof(cur_key));
  386. cur_key.ulen = lowerkey->size;
  387. cur_key.size = lowerkey->size;
  388. saved_key = cur_key.data = slapi_ch_malloc(lowerkey->size);
  389. memcpy(cur_key.data, lowerkey->data, lowerkey->size);
  390. cur_key.flags = DB_DBT_MALLOC;
  391. /* Position cursor at the first matching key */
  392. #ifdef DB_USE_BULK_FETCH
  393. ret = cursor->c_get(cursor, &cur_key, &data, DB_SET|DB_MULTIPLE);
  394. #else
  395. ret = cursor->c_get(cursor, &cur_key, &data, DB_SET);
  396. #endif
  397. if (0 != ret) {
  398. if (DB_NOTFOUND != ret) {
  399. #ifdef DB_USE_BULK_FETCH
  400. if (ret == DB_BUFFER_SMALL) {
  401. LDAPDebug(LDAP_DEBUG_ANY, "database index is corrupt; "
  402. "data item for key %s is too large for our buffer "
  403. "(need=%d actual=%d)\n",
  404. cur_key.data, data.size, data.ulen);
  405. }
  406. #endif
  407. ldbm_nasty(filename,2,ret);
  408. }
  409. goto error; /* Not found is OK, return NULL IDL */
  410. }
  411. /* Iterate over the duplicates, amassing them into an IDL */
  412. #ifdef DB_USE_BULK_FETCH
  413. while (cur_key.data &&
  414. (upperkey && upperkey->data ?
  415. ((operator == SLAPI_OP_LESS) ?
  416. DBTcmp(&cur_key, upperkey, ai->ai_key_cmp_fn) < 0 :
  417. DBTcmp(&cur_key, upperkey, ai->ai_key_cmp_fn) <= 0) :
  418. PR_TRUE /* e.g., (x > a) */)) {
  419. ID lastid = 0;
  420. DB_MULTIPLE_INIT(ptr, &data);
  421. /* lookthrough limit & sizelimit check */
  422. if (idl) {
  423. if ((lookthrough_limit != -1) &&
  424. (idl->b_nids > (ID)lookthrough_limit)) {
  425. idl_free(&idl);
  426. idl = idl_allids( be );
  427. LDAPDebug0Args(LDAP_DEBUG_TRACE,
  428. "idl_new_range_fetch - lookthrough_limit exceeded\n");
  429. *flag_err = LDAP_ADMINLIMIT_EXCEEDED;
  430. goto error;
  431. }
  432. if ((sizelimit > 0) && (idl->b_nids > (ID)sizelimit)) {
  433. LDAPDebug0Args(LDAP_DEBUG_TRACE,
  434. "idl_new_range_fetch - sizelimit exceeded\n");
  435. *flag_err = LDAP_SIZELIMIT_EXCEEDED;
  436. goto error;
  437. }
  438. }
  439. /* timelimit check */
  440. if (stoptime > 0) { /* timelimit is set */
  441. curtime = current_time();
  442. if (curtime >= stoptime) {
  443. LDAPDebug0Args(LDAP_DEBUG_TRACE,
  444. "idl_new_range_fetch - timelimit exceeded\n");
  445. *flag_err = LDAP_TIMELIMIT_EXCEEDED;
  446. goto error;
  447. }
  448. }
  449. while (PR_TRUE) {
  450. DB_MULTIPLE_NEXT(ptr, &data, dataret.data, dataret.size);
  451. if (dataret.data == NULL) break;
  452. if (ptr == NULL) break;
  453. if (*(int32_t *)ptr < -1) {
  454. LDAPDebug1Arg(LDAP_DEBUG_TRACE, "DB_MULTIPLE buffer is corrupt; "
  455. "next offset [%d] is less than zero\n",
  456. *(int32_t *)ptr);
  457. /* retry the read */
  458. break;
  459. }
  460. if (dataret.size != sizeof(ID)) {
  461. LDAPDebug(LDAP_DEBUG_ANY, "database index is corrupt; "
  462. "key %s has a data item with the wrong size (%d)\n",
  463. cur_key.data, dataret.size, 0);
  464. goto error;
  465. }
  466. memcpy(&id, dataret.data, sizeof(ID));
  467. if (id == lastid) { /* dup */
  468. LDAPDebug1Arg(LDAP_DEBUG_TRACE, "Detedted duplicate id "
  469. "%d due to DB_MULTIPLE error - skipping\n",
  470. id);
  471. continue; /* get next one */
  472. }
  473. /* note the last id read to check for dups */
  474. lastid = id;
  475. /* we got another ID, add it to our IDL */
  476. idl_rc = idl_append_extend(&idl, id);
  477. if (idl_rc) {
  478. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  479. "unable to extend id list (err=%d)\n", idl_rc);
  480. idl_free(&idl);
  481. goto error;
  482. }
  483. count++;
  484. }
  485. LDAPDebug(LDAP_DEBUG_TRACE, "bulk fetch buffer nids=%d\n", count, 0, 0);
  486. #if defined(DB_ALLIDS_ON_READ)
  487. /* enforce the allids read limit */
  488. if ((NEW_IDL_NO_ALLID != *flag_err) && ai && (idl != NULL) &&
  489. idl_new_exceeds_allidslimit(count, ai, allidslimit)) {
  490. idl->b_nids = 1;
  491. idl->b_ids[0] = ALLID;
  492. ret = DB_NOTFOUND; /* fool the code below into thinking that we finished the dups */
  493. break;
  494. }
  495. #endif
  496. ret = cursor->c_get(cursor, &cur_key, &data, DB_NEXT_DUP|DB_MULTIPLE);
  497. if (saved_key != cur_key.data) {
  498. /* key was allocated in c_get */
  499. slapi_ch_free(&saved_key);
  500. saved_key = cur_key.data;
  501. }
  502. if (ret) {
  503. if (upperkey && upperkey->data && DBT_EQ(&cur_key, upperkey)) {
  504. /* this is the last key */
  505. break;
  506. }
  507. /* First set the cursor (DB_NEXT_NODUP does not take DB_MULTIPLE) */
  508. ret = cursor->c_get(cursor, &cur_key, &data, DB_NEXT_NODUP);
  509. if (saved_key != cur_key.data) {
  510. /* key was allocated in c_get */
  511. slapi_ch_free(&saved_key);
  512. saved_key = cur_key.data;
  513. }
  514. if (ret) {
  515. break;
  516. }
  517. /* Read the dup data */
  518. ret = cursor->c_get(cursor, &cur_key, &data, DB_SET|DB_MULTIPLE);
  519. if (saved_key != cur_key.data) {
  520. /* key was allocated in c_get */
  521. slapi_ch_free(&saved_key);
  522. saved_key = cur_key.data;
  523. }
  524. if (ret) {
  525. break;
  526. }
  527. }
  528. }
  529. #else
  530. while (upperkey && upperkey->data ?
  531. ((operator == SLAPI_OP_LESS) ?
  532. DBTcmp(&cur_key, upperkey, ai->ai_key_cmp_fn) < 0 :
  533. DBTcmp(&cur_key, upperkey, ai->ai_key_cmp_fn) <= 0) :
  534. PR_TRUE /* e.g., (x > a) */) {
  535. /* lookthrough limit & sizelimit check */
  536. if (idl) {
  537. if ((lookthrough_limit != -1) &&
  538. (idl->b_nids > (ID)lookthrough_limit)) {
  539. idl_free(&idl);
  540. idl = idl_allids( be );
  541. LDAPDebug0Args(LDAP_DEBUG_TRACE,
  542. "idl_new_range_fetch - lookthrough_limit exceeded\n");
  543. *flag_err = LDAP_ADMINLIMIT_EXCEEDED;
  544. goto error;
  545. }
  546. if ((sizelimit > 0) && (idl->b_nids > (ID)sizelimit)) {
  547. LDAPDebug0Args(LDAP_DEBUG_TRACE,
  548. "idl_new_range_fetch - sizelimit exceeded\n");
  549. *flag_err = LDAP_SIZELIMIT_EXCEEDED;
  550. goto error;
  551. }
  552. }
  553. /* timelimit check */
  554. if (stoptime > 0) { /* timelimit is set */
  555. curtime = current_time();
  556. if (curtime >= stoptime) {
  557. LDAPDebug0Args(LDAP_DEBUG_TRACE,
  558. "idl_new_range_fetch - timelimit exceeded\n");
  559. *flag_err = LDAP_TIMELIMIT_EXCEEDED;
  560. goto error;
  561. }
  562. }
  563. ret = cursor->c_get(cursor,&cur_key,&data,DB_NEXT_DUP);
  564. if (saved_key != cur_key.data) {
  565. /* key was allocated in c_get */
  566. slapi_ch_free(&saved_key);
  567. saved_key = cur_key.data;
  568. }
  569. count++;
  570. if (ret) {
  571. if (upperkey && upperkey->data && DBT_EQ(&cur_key, upperkey)) {
  572. /* this is the last key */
  573. break;
  574. }
  575. ret = cursor->c_get(cursor, &cur_key, &data, DB_NEXT_NODUP);
  576. if (saved_key != cur_key.data) {
  577. /* key was allocated in c_get */
  578. slapi_ch_free(&saved_key);
  579. saved_key = cur_key.data;
  580. }
  581. if (ret) {
  582. break;
  583. }
  584. }
  585. /* we got another ID, add it to our IDL */
  586. idl_rc = idl_append_extend(&idl, id);
  587. if (idl_rc) {
  588. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  589. "unable to extend id list (err=%d)\n", idl_rc);
  590. idl_free(&idl);
  591. goto error;
  592. }
  593. #if defined(DB_ALLIDS_ON_READ)
  594. /* enforce the allids read limit */
  595. if (idl && idl_new_exceeds_allidslimit(count, ai, allidslimit)) {
  596. idl->b_nids = 1;
  597. idl->b_ids[0] = ALLID;
  598. ret = DB_NOTFOUND; /* fool the code below into thinking that we finished the dups */
  599. break;
  600. }
  601. #endif
  602. }
  603. #endif
  604. if (ret) {
  605. if (ret == DB_NOTFOUND) {
  606. ret = 0; /* normal case */
  607. } else {
  608. idl_free(&idl);
  609. ldbm_nasty(filename,59,ret);
  610. goto error;
  611. }
  612. }
  613. /* check for allids value */
  614. if (idl && (idl->b_nids == 1) && (idl->b_ids[0] == ALLID)) {
  615. idl_free(&idl);
  616. idl = idl_allids(be);
  617. LDAPDebug1Arg(LDAP_DEBUG_TRACE, "idl_new_fetch %s returns allids\n",
  618. cur_key.data);
  619. } else {
  620. LDAPDebug2Args(LDAP_DEBUG_TRACE, "idl_new_fetch %s returns nids=%lu\n",
  621. cur_key.data, (u_long)IDL_NIDS(idl));
  622. }
  623. error:
  624. DBT_FREE_PAYLOAD(cur_key);
  625. /* Close the cursor */
  626. if (NULL != cursor) {
  627. int ret2 = cursor->c_close(cursor);
  628. if (ret2) {
  629. ldbm_nasty(filename,3,ret2);
  630. if (!ret) {
  631. /* if cursor close returns DEADLOCK, we must bubble that up
  632. to the higher layers for retries */
  633. ret = ret2;
  634. }
  635. }
  636. }
  637. if (ret) {
  638. dblayer_read_txn_abort(be, &s_txn);
  639. } else {
  640. dblayer_read_txn_commit(be, &s_txn);
  641. }
  642. *flag_err = ret;
  643. /* sort idl */
  644. if (idl && !ALLIDS(idl)) {
  645. qsort((void *)&idl->b_ids[0], idl->b_nids,
  646. (size_t)sizeof(ID), idl_sort_cmp);
  647. }
  648. return idl;
  649. }
  650. int idl_new_insert_key(
  651. backend *be,
  652. DB* db,
  653. DBT *key,
  654. ID id,
  655. DB_TXN *txn,
  656. struct attrinfo *a,
  657. int *disposition
  658. )
  659. {
  660. int ret = 0;
  661. DBT data;
  662. #if defined(DB_ALLIDS_ON_WRITE)
  663. DBC *cursor = NULL;
  664. db_recno_t count;
  665. ID tmpid = 0;
  666. /* Make a cursor */
  667. ret = db->cursor(db,txn,&cursor,0);
  668. if (0 != ret) {
  669. ldbm_nasty(filename,58,ret);
  670. cursor = NULL;
  671. goto error;
  672. }
  673. memset(data, 0, sizeof(data)); /* bdb says data = {0} is not sufficient */
  674. data.ulen = sizeof(id);
  675. data.size = sizeof(id);
  676. data.flags = DB_DBT_USERMEM;
  677. data.data = &tmpid;
  678. ret = cursor->c_get(cursor,key,&data,DB_SET);
  679. if (0 == ret) {
  680. if (tmpid == ALLID) {
  681. if (NULL != disposition) {
  682. *disposition = IDL_INSERT_ALLIDS;
  683. }
  684. goto error; /* allid: don't bother inserting any more */
  685. }
  686. } else if (DB_NOTFOUND != ret) {
  687. ldbm_nasty(filename,12,ret);
  688. goto error;
  689. }
  690. if (NULL != disposition) {
  691. *disposition = IDL_INSERT_NORMAL;
  692. }
  693. data.data = &id;
  694. /* insert it */
  695. ret = cursor->c_put(cursor, key, &data, DB_NODUPDATA);
  696. if (0 != ret) {
  697. if (DB_KEYEXIST == ret) {
  698. /* this is okay */
  699. ret = 0;
  700. } else {
  701. ldbm_nasty(filename,50,ret);
  702. }
  703. } else {
  704. /* check for allidslimit exceeded in database */
  705. if (cursor->c_count(cursor, &count, 0) != 0) {
  706. LDAPDebug(LDAP_DEBUG_ANY, "could not obtain count for key %s\n",
  707. key->data, 0, 0);
  708. goto error;
  709. }
  710. if ((size_t)count > idl_new_get_allidslimit(a, 0)) {
  711. LDAPDebug(LDAP_DEBUG_TRACE, "allidslimit exceeded for key %s\n",
  712. key->data, 0, 0);
  713. cursor->c_close(cursor);
  714. cursor = NULL;
  715. if ((ret = idl_new_store_allids(be, db, key, txn)) == 0) {
  716. if (NULL != disposition) {
  717. *disposition = IDL_INSERT_NOW_ALLIDS;
  718. }
  719. }
  720. }
  721. error:
  722. /* Close the cursor */
  723. if (NULL != cursor) {
  724. int ret2 = cursor->c_close(cursor);
  725. if (ret2) {
  726. ldbm_nasty(filename,56,ret2);
  727. }
  728. }
  729. #else
  730. memset(&data, 0, sizeof(data)); /* bdb says data = {0} is not sufficient */
  731. data.ulen = sizeof(id);
  732. data.size = sizeof(id);
  733. data.flags = DB_DBT_USERMEM;
  734. data.data = &id;
  735. if (NULL != disposition) {
  736. *disposition = IDL_INSERT_NORMAL;
  737. }
  738. ret = db->put(db, txn, key, &data, DB_NODUPDATA);
  739. if (0 != ret) {
  740. if (DB_KEYEXIST == ret) {
  741. /* this is okay */
  742. ret = 0;
  743. } else {
  744. ldbm_nasty(filename,60,ret);
  745. }
  746. }
  747. #endif
  748. return ret;
  749. }
  750. int idl_new_delete_key(
  751. backend *be,
  752. DB *db,
  753. DBT *key,
  754. ID id,
  755. DB_TXN *txn,
  756. struct attrinfo *a
  757. )
  758. {
  759. int ret = 0;
  760. DBC *cursor = NULL;
  761. DBT data = {0};
  762. /* Make a cursor */
  763. ret = db->cursor(db,txn,&cursor,0);
  764. if (0 != ret) {
  765. ldbm_nasty(filename,21,ret);
  766. cursor = NULL;
  767. goto error;
  768. }
  769. data.ulen = sizeof(id);
  770. data.size = sizeof(id);
  771. data.flags = DB_DBT_USERMEM;
  772. data.data = &id;
  773. /* Position cursor at the key, value pair */
  774. ret = cursor->c_get(cursor,key,&data,DB_GET_BOTH);
  775. if (0 == ret) {
  776. if (id == ALLID) {
  777. goto error; /* allid: never delete it */
  778. }
  779. } else {
  780. if (DB_NOTFOUND == ret) {
  781. ret = 0; /* Not Found is OK, return immediately */
  782. } else {
  783. ldbm_nasty(filename,22,ret);
  784. }
  785. goto error;
  786. }
  787. /* We found it, so delete it */
  788. ret = cursor->c_del(cursor,0);
  789. error:
  790. /* Close the cursor */
  791. if (NULL != cursor) {
  792. int ret2 = cursor->c_close(cursor);
  793. if (ret2) {
  794. ldbm_nasty(filename,24,ret2);
  795. if (!ret) {
  796. /* if cursor close returns DEADLOCK, we must bubble that up
  797. to the higher layers for retries */
  798. ret = ret2;
  799. }
  800. }
  801. }
  802. return ret;
  803. }
  804. #if defined(DB_ALLIDS_ON_WRITE)
  805. static int idl_new_store_allids(backend *be, DB *db, DBT *key, DB_TXN *txn)
  806. {
  807. int ret = 0;
  808. DBC *cursor = NULL;
  809. DBT data = {0};
  810. ID id = 0;
  811. /* Make a cursor */
  812. ret = db->cursor(db,txn,&cursor,0);
  813. if (0 != ret) {
  814. ldbm_nasty(filename,31,ret);
  815. cursor = NULL;
  816. goto error;
  817. }
  818. data.ulen = sizeof(ID);
  819. data.size = sizeof(ID);
  820. data.data = &id;
  821. data.flags = DB_DBT_USERMEM;
  822. /* Position cursor at the key */
  823. ret = cursor->c_get(cursor,key,&data,DB_SET);
  824. if (ret == 0) {
  825. /* We found it, so delete all duplicates */
  826. ret = cursor->c_del(cursor,0);
  827. while (0 == ret) {
  828. ret = cursor->c_get(cursor,key,&data,DB_NEXT_DUP);
  829. if (0 != ret) {
  830. break;
  831. }
  832. ret = cursor->c_del(cursor,0);
  833. }
  834. if (0 != ret && DB_NOTFOUND != ret) {
  835. ldbm_nasty(filename,54,ret);
  836. goto error;
  837. } else {
  838. ret = 0;
  839. }
  840. } else {
  841. if (DB_NOTFOUND == ret) {
  842. ret = 0; /* Not Found is OK */
  843. } else {
  844. ldbm_nasty(filename,32,ret);
  845. goto error;
  846. }
  847. }
  848. /* store the ALLID value */
  849. id = ALLID;
  850. ret = cursor->c_put(cursor, key, &data, DB_NODUPDATA);
  851. if (0 != ret) {
  852. ldbm_nasty(filename,53,ret);
  853. goto error;
  854. }
  855. LDAPDebug(LDAP_DEBUG_TRACE, "key %s has been set to allids\n",
  856. key->data, 0, 0);
  857. error:
  858. /* Close the cursor */
  859. if (NULL != cursor) {
  860. int ret2 = cursor->c_close(cursor);
  861. if (ret2) {
  862. ldbm_nasty(filename,33,ret2);
  863. }
  864. }
  865. return ret;
  866. #ifdef KRAZY_K0DE
  867. /* If this function is called in "no-allids" mode, then it's a bug */
  868. ldbm_nasty(filename,63,0);
  869. return -1;
  870. #endif
  871. }
  872. #endif
  873. int idl_new_store_block(
  874. backend *be,
  875. DB *db,
  876. DBT *key,
  877. IDList *idl,
  878. DB_TXN *txn,
  879. struct attrinfo *a
  880. )
  881. {
  882. int ret = 0;
  883. DBC *cursor = NULL;
  884. DBT data = {0};
  885. ID id = 0;
  886. size_t x = 0;
  887. #if defined(DB_ALLIDS_ON_WRITE)
  888. db_recno_t count;
  889. #endif
  890. if (NULL == idl)
  891. {
  892. return ret;
  893. }
  894. /*
  895. * Really we need an extra entry point to the DB here, which
  896. * inserts a list of duplicate keys. In the meantime, we'll
  897. * just do it by brute force.
  898. */
  899. #if defined(DB_ALLIDS_ON_WRITE)
  900. /* allids check on input idl */
  901. if (ALLIDS(idl) || (idl->b_nids > (ID)idl_new_get_allidslimit(a, 0))) {
  902. return idl_new_store_allids(be, db, key, txn);
  903. }
  904. #endif
  905. /* Make a cursor */
  906. ret = db->cursor(db,txn,&cursor,0);
  907. if (0 != ret) {
  908. ldbm_nasty(filename,41,ret);
  909. cursor = NULL;
  910. goto error;
  911. }
  912. /* initialize data DBT */
  913. data.data = &id;
  914. data.ulen = sizeof(id);
  915. data.size = sizeof(id);
  916. data.flags = DB_DBT_USERMEM;
  917. /* Position cursor at the key, value pair */
  918. ret = cursor->c_get(cursor,key,&data,DB_GET_BOTH);
  919. if (ret == DB_NOTFOUND) {
  920. ret = 0;
  921. } else if (ret != 0) {
  922. ldbm_nasty(filename,47,ret);
  923. goto error;
  924. }
  925. /* Iterate over the IDs in the idl */
  926. for (x = 0; x < idl->b_nids; x++) {
  927. /* insert an id */
  928. id = idl->b_ids[x];
  929. ret = cursor->c_put(cursor, key, &data, DB_NODUPDATA);
  930. if (0 != ret) {
  931. if (DB_KEYEXIST == ret) {
  932. ret = 0; /* exist is okay */
  933. } else {
  934. ldbm_nasty(filename,48,ret);
  935. goto error;
  936. }
  937. }
  938. }
  939. #if defined(DB_ALLIDS_ON_WRITE)
  940. /* check for allidslimit exceeded in database */
  941. if (cursor->c_count(cursor, &count, 0) != 0) {
  942. LDAPDebug(LDAP_DEBUG_ANY, "could not obtain count for key %s\n",
  943. key->data, 0, 0);
  944. goto error;
  945. }
  946. if ((size_t)count > idl_new_get_allidslimit(a, 0)) {
  947. LDAPDebug(LDAP_DEBUG_TRACE, "allidslimit exceeded for key %s\n",
  948. key->data, 0, 0);
  949. cursor->c_close(cursor);
  950. cursor = NULL;
  951. ret = idl_new_store_allids(be, db, key, txn);
  952. }
  953. #endif
  954. error:
  955. /* Close the cursor */
  956. if (NULL != cursor) {
  957. int ret2 = cursor->c_close(cursor);
  958. if (ret2) {
  959. ldbm_nasty(filename,49,ret2);
  960. if (!ret) {
  961. /* if cursor close returns DEADLOCK, we must bubble that up
  962. to the higher layers for retries */
  963. ret = ret2;
  964. }
  965. }
  966. }
  967. return ret;
  968. }
  969. /* idl_new_compare_dups: comparing ID, pass to libdb for callback */
  970. int idl_new_compare_dups(
  971. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 3200
  972. DB *db,
  973. #endif
  974. const DBT *a,
  975. const DBT *b
  976. )
  977. {
  978. ID a_copy, b_copy;
  979. memmove(&a_copy, a->data, sizeof(ID));
  980. memmove(&b_copy, b->data, sizeof(ID));
  981. return a_copy - b_copy;
  982. }