idl_new.c 32 KB

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