idl_new.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. /* Append an ID to an IDL, realloc-ing the space if needs be */
  91. /* ID presented is not to be already in the IDL. */
  92. static int
  93. idl_append_extend( IDList **orig_idl, ID id)
  94. {
  95. IDList *idl = *orig_idl;
  96. if (idl == NULL) {
  97. idl = idl_alloc(1);
  98. idl_append(idl, id);
  99. *orig_idl = idl;
  100. return 0;
  101. }
  102. if ( idl->b_nids == idl->b_nmax ) {
  103. size_t x = 0;
  104. /* No more room, need to extend */
  105. /* Allocate new IDL with twice the space of this one */
  106. IDList *idl_new = NULL;
  107. idl_new = idl_alloc(idl->b_nmax * 2);
  108. if (NULL == idl_new) {
  109. return ENOMEM;
  110. }
  111. /* copy over the existing contents */
  112. idl_new->b_nids = idl->b_nids;
  113. for (x = 0; x < idl->b_nids;x++) {
  114. idl_new->b_ids[x] = idl->b_ids[x];
  115. }
  116. idl_free(idl);
  117. idl = idl_new;
  118. }
  119. idl->b_ids[idl->b_nids] = id;
  120. idl->b_nids++;
  121. *orig_idl = idl;
  122. return 0;
  123. }
  124. size_t idl_new_get_allidslimit(struct attrinfo *a)
  125. {
  126. idl_private *priv = NULL;
  127. PR_ASSERT(NULL != a);
  128. PR_ASSERT(NULL != a->ai_idl);
  129. priv = a->ai_idl;
  130. return priv->idl_allidslimit;
  131. }
  132. /* routine to initialize the private data used by the IDL code per-attribute */
  133. int idl_new_init_private(backend *be,struct attrinfo *a)
  134. {
  135. idl_private *priv = NULL;
  136. struct ldbminfo *li = (struct ldbminfo *)be->be_database->plg_private;
  137. PR_ASSERT(NULL != a);
  138. PR_ASSERT(NULL == a->ai_idl);
  139. priv = (idl_private*) slapi_ch_calloc(sizeof(idl_private),1);
  140. if (NULL == priv) {
  141. return -1; /* Memory allocation failure */
  142. }
  143. priv->idl_allidslimit = li->li_allidsthreshold;
  144. /* Initialize the structure */
  145. a->ai_idl = (void*)priv;
  146. return 0;
  147. }
  148. /* routine to release resources used by IDL private data structure */
  149. int idl_new_release_private(struct attrinfo *a)
  150. {
  151. PR_ASSERT(NULL != a);
  152. if (NULL != a->ai_idl)
  153. {
  154. slapi_ch_free((void**)&(a->ai_idl) );
  155. }
  156. return 0;
  157. }
  158. IDList * idl_new_fetch(
  159. backend *be,
  160. DB* db,
  161. DBT *inkey,
  162. DB_TXN *txn,
  163. struct attrinfo *a,
  164. int *flag_err
  165. )
  166. {
  167. int ret = 0;
  168. DBC *cursor = NULL;
  169. IDList *idl = NULL;
  170. DBT key;
  171. DBT data;
  172. ID id = 0;
  173. size_t count = 0;
  174. #ifdef DB_USE_BULK_FETCH
  175. /* beware that a large buffer on the stack might cause a stack overflow on some platforms */
  176. char buffer[BULK_FETCH_BUFFER_SIZE];
  177. void *ptr;
  178. DBT dataret;
  179. #endif
  180. if (NEW_IDL_NOOP == *flag_err)
  181. {
  182. *flag_err = 0;
  183. return NULL;
  184. }
  185. /* Make a cursor */
  186. ret = db->cursor(db,txn,&cursor,0);
  187. if (0 != ret) {
  188. ldbm_nasty(filename,1,ret);
  189. cursor = NULL;
  190. goto error;
  191. }
  192. memset(&data, 0, sizeof(data));
  193. #ifdef DB_USE_BULK_FETCH
  194. data.ulen = sizeof(buffer);
  195. data.size = sizeof(buffer);
  196. data.data = buffer;
  197. data.flags = DB_DBT_USERMEM;
  198. memset(&dataret, 0, sizeof(dataret));
  199. #else
  200. data.ulen = sizeof(id);
  201. data.size = sizeof(id);
  202. data.data = &id;
  203. data.flags = DB_DBT_USERMEM;
  204. #endif
  205. /*
  206. * We're not expecting the key to change in value
  207. * so we can just use the input key as a buffer.
  208. * This avoids memory management of the key.
  209. */
  210. memset(&key, 0, sizeof(key));
  211. key.ulen = inkey->size;
  212. key.size = inkey->size;
  213. key.data = inkey->data;
  214. key.flags = DB_DBT_USERMEM;
  215. /* Position cursor at the first matching key */
  216. #ifdef DB_USE_BULK_FETCH
  217. ret = cursor->c_get(cursor,&key,&data,DB_SET|DB_MULTIPLE);
  218. #else
  219. ret = cursor->c_get(cursor,&key,&data,DB_SET);
  220. #endif
  221. if (0 != ret) {
  222. if (DB_NOTFOUND != ret) {
  223. #ifdef DB_USE_BULK_FETCH
  224. if (ret == ENOMEM) {
  225. LDAPDebug(LDAP_DEBUG_ANY, "database index is corrupt; "
  226. "data item for key %s is too large for our buffer "
  227. "(need=%d actual=%d)\n",
  228. key.data, data.size, data.ulen);
  229. }
  230. #endif
  231. ldbm_nasty(filename,2,ret);
  232. }
  233. goto error; /* Not found is OK, return NULL IDL */
  234. }
  235. /* Iterate over the duplicates, amassing them into an IDL */
  236. #ifdef DB_USE_BULK_FETCH
  237. for (;;) {
  238. DB_MULTIPLE_INIT(ptr, &data);
  239. for (;;) {
  240. DB_MULTIPLE_NEXT(ptr, &data, dataret.data, dataret.size);
  241. if (dataret.data == NULL) break;
  242. if (ptr == NULL) break;
  243. if (dataret.size != sizeof(ID)) {
  244. LDAPDebug(LDAP_DEBUG_ANY, "database index is corrupt; "
  245. "key %s has a data item with the wrong size (%d)\n",
  246. key.data, dataret.size, 0);
  247. goto error;
  248. }
  249. memcpy(&id, dataret.data, sizeof(ID));
  250. /* we got another ID, add it to our IDL */
  251. idl_append_extend(&idl, id);
  252. count++;
  253. }
  254. LDAPDebug(LDAP_DEBUG_TRACE, "bulk fetch buffer nids=%d\n", count, 0, 0);
  255. #if defined(DB_ALLIDS_ON_READ)
  256. /* enforce the allids read limit */
  257. if (NEW_IDL_NO_ALLID != *flag_err &&
  258. NULL != a && count > idl_new_get_allidslimit(a)) {
  259. idl->b_nids = 1;
  260. idl->b_ids[0] = ALLID;
  261. ret = DB_NOTFOUND; /* fool the code below into thinking that we finished the dups */
  262. break;
  263. }
  264. #endif
  265. ret = cursor->c_get(cursor,&key,&data,DB_NEXT_DUP|DB_MULTIPLE);
  266. if (0 != ret) {
  267. break;
  268. }
  269. }
  270. #else
  271. for (;;) {
  272. ret = cursor->c_get(cursor,&key,&data,DB_NEXT_DUP);
  273. count++;
  274. if (0 != ret) {
  275. break;
  276. }
  277. /* we got another ID, add it to our IDL */
  278. idl_append_extend(&idl, id);
  279. #if defined(DB_ALLIDS_ON_READ)
  280. /* enforce the allids read limit */
  281. if (count > idl_new_get_allidslimit(a)) {
  282. idl->b_nids = 1;
  283. idl->b_ids[0] = ALLID;
  284. ret = DB_NOTFOUND; /* fool the code below into thinking that we finished the dups */
  285. break;
  286. }
  287. #endif
  288. }
  289. #endif
  290. if (ret != DB_NOTFOUND) {
  291. idl_free(idl); idl = NULL;
  292. ldbm_nasty(filename,59,ret);
  293. goto error;
  294. }
  295. ret = 0;
  296. /* check for allids value */
  297. if (idl != NULL && idl->b_nids == 1 && idl->b_ids[0] == ALLID) {
  298. idl_free(idl);
  299. idl = idl_allids(be);
  300. LDAPDebug(LDAP_DEBUG_TRACE, "idl_new_fetch %s returns allids\n",
  301. key.data, 0, 0);
  302. } else {
  303. LDAPDebug(LDAP_DEBUG_TRACE, "idl_new_fetch %s returns nids=%lu\n",
  304. key.data, (u_long)IDL_NIDS(idl), 0);
  305. }
  306. error:
  307. /* Close the cursor */
  308. if (NULL != cursor) {
  309. if (0 != cursor->c_close(cursor)) {
  310. ldbm_nasty(filename,3,ret);
  311. }
  312. }
  313. *flag_err = ret;
  314. return idl;
  315. }
  316. int idl_new_insert_key(
  317. backend *be,
  318. DB* db,
  319. DBT *key,
  320. ID id,
  321. DB_TXN *txn,
  322. struct attrinfo *a,
  323. int *disposition
  324. )
  325. {
  326. int ret = 0;
  327. DBT data;
  328. #if defined(DB_ALLIDS_ON_WRITE)
  329. DBC *cursor = NULL;
  330. db_recno_t count;
  331. ID tmpid = 0;
  332. /* Make a cursor */
  333. ret = db->cursor(db,txn,&cursor,0);
  334. if (0 != ret) {
  335. ldbm_nasty(filename,58,ret);
  336. cursor = NULL;
  337. goto error;
  338. }
  339. memset(data, 0, sizeof(data)); /* bdb says data = {0} is not sufficient */
  340. data.ulen = sizeof(id);
  341. data.size = sizeof(id);
  342. data.flags = DB_DBT_USERMEM;
  343. data.data = &tmpid;
  344. ret = cursor->c_get(cursor,key,&data,DB_SET);
  345. if (0 == ret) {
  346. if (tmpid == ALLID) {
  347. if (NULL != disposition) {
  348. *disposition = IDL_INSERT_ALLIDS;
  349. }
  350. goto error; /* allid: don't bother inserting any more */
  351. }
  352. } else if (DB_NOTFOUND != ret) {
  353. ldbm_nasty(filename,12,ret);
  354. goto error;
  355. }
  356. if (NULL != disposition) {
  357. *disposition = IDL_INSERT_NORMAL;
  358. }
  359. data.data = &id;
  360. /* insert it */
  361. ret = cursor->c_put(cursor, key, &data, DB_NODUPDATA);
  362. if (0 != ret) {
  363. if (DB_KEYEXIST == ret) {
  364. /* this is okay */
  365. ret = 0;
  366. } else {
  367. ldbm_nasty(filename,50,ret);
  368. }
  369. } else {
  370. /* check for allidslimit exceeded in database */
  371. if (cursor->c_count(cursor, &count, 0) != 0) {
  372. LDAPDebug(LDAP_DEBUG_ANY, "could not obtain count for key %s\n",
  373. key->data, 0, 0);
  374. goto error;
  375. }
  376. if ((size_t)count > idl_new_get_allidslimit(a)) {
  377. LDAPDebug(LDAP_DEBUG_TRACE, "allidslimit exceeded for key %s\n",
  378. key->data, 0, 0);
  379. cursor->c_close(cursor);
  380. cursor = NULL;
  381. if ((ret = idl_new_store_allids(be, db, key, txn)) == 0) {
  382. if (NULL != disposition) {
  383. *disposition = IDL_INSERT_NOW_ALLIDS;
  384. }
  385. }
  386. }
  387. error:
  388. /* Close the cursor */
  389. if (NULL != cursor) {
  390. if (0 != cursor->c_close(cursor)) {
  391. ldbm_nasty(filename,56,ret);
  392. }
  393. }
  394. #else
  395. memset(&data, 0, sizeof(data)); /* bdb says data = {0} is not sufficient */
  396. data.ulen = sizeof(id);
  397. data.size = sizeof(id);
  398. data.flags = DB_DBT_USERMEM;
  399. data.data = &id;
  400. if (NULL != disposition) {
  401. *disposition = IDL_INSERT_NORMAL;
  402. }
  403. ret = db->put(db, txn, key, &data, DB_NODUPDATA);
  404. if (0 != ret) {
  405. if (DB_KEYEXIST == ret) {
  406. /* this is okay */
  407. ret = 0;
  408. } else {
  409. ldbm_nasty(filename,50,ret);
  410. }
  411. }
  412. #endif
  413. return ret;
  414. }
  415. int idl_new_delete_key(
  416. backend *be,
  417. DB *db,
  418. DBT *key,
  419. ID id,
  420. DB_TXN *txn,
  421. struct attrinfo *a
  422. )
  423. {
  424. int ret = 0;
  425. DBC *cursor = NULL;
  426. DBT data = {0};
  427. ID tmpid = 0;
  428. /* Make a cursor */
  429. ret = db->cursor(db,txn,&cursor,0);
  430. if (0 != ret) {
  431. ldbm_nasty(filename,21,ret);
  432. cursor = NULL;
  433. goto error;
  434. }
  435. data.ulen = sizeof(id);
  436. data.size = sizeof(id);
  437. data.flags = DB_DBT_USERMEM;
  438. data.data = &tmpid;
  439. ret = cursor->c_get(cursor,key,&data,DB_SET);
  440. if (0 == ret) {
  441. if (tmpid == ALLID) {
  442. goto error; /* allid: never delete it */
  443. }
  444. } else if (DB_NOTFOUND != ret) {
  445. ldbm_nasty(filename,22,ret);
  446. goto error;
  447. }
  448. /* Position cursor at the key, value pair */
  449. data.data = &id;
  450. ret = cursor->c_get(cursor,key,&data,DB_GET_BOTH);
  451. if (0 != ret) {
  452. if (DB_NOTFOUND == ret) {
  453. ret = 0; /* Not Found is OK, return immediately */
  454. } else {
  455. ldbm_nasty(filename,23,ret);
  456. }
  457. goto error;
  458. }
  459. /* We found it, so delete it */
  460. ret = cursor->c_del(cursor,0);
  461. error:
  462. /* Close the cursor */
  463. if (NULL != cursor) {
  464. if (0 != cursor->c_close(cursor)) {
  465. ldbm_nasty(filename,24,ret);
  466. }
  467. }
  468. return ret;
  469. }
  470. #if defined(DB_ALLIDS_ON_WRITE)
  471. static int idl_new_store_allids(backend *be, DB *db, DBT *key, DB_TXN *txn)
  472. {
  473. int ret = 0;
  474. DBC *cursor = NULL;
  475. DBT data = {0};
  476. ID id = 0;
  477. /* Make a cursor */
  478. ret = db->cursor(db,txn,&cursor,0);
  479. if (0 != ret) {
  480. ldbm_nasty(filename,31,ret);
  481. cursor = NULL;
  482. goto error;
  483. }
  484. data.ulen = sizeof(ID);
  485. data.size = sizeof(ID);
  486. data.data = &id;
  487. data.flags = DB_DBT_USERMEM;
  488. /* Position cursor at the key */
  489. ret = cursor->c_get(cursor,key,&data,DB_SET);
  490. if (ret == 0) {
  491. /* We found it, so delete all duplicates */
  492. ret = cursor->c_del(cursor,0);
  493. while (0 == ret) {
  494. ret = cursor->c_get(cursor,key,&data,DB_NEXT_DUP);
  495. if (0 != ret) {
  496. break;
  497. }
  498. ret = cursor->c_del(cursor,0);
  499. }
  500. if (0 != ret && DB_NOTFOUND != ret) {
  501. ldbm_nasty(filename,54,ret);
  502. goto error;
  503. } else {
  504. ret = 0;
  505. }
  506. } else {
  507. if (DB_NOTFOUND == ret) {
  508. ret = 0; /* Not Found is OK */
  509. } else {
  510. ldbm_nasty(filename,32,ret);
  511. goto error;
  512. }
  513. }
  514. /* store the ALLID value */
  515. id = ALLID;
  516. ret = cursor->c_put(cursor, key, &data, DB_NODUPDATA);
  517. if (0 != ret) {
  518. ldbm_nasty(filename,53,ret);
  519. goto error;
  520. }
  521. LDAPDebug(LDAP_DEBUG_TRACE, "key %s has been set to allids\n",
  522. key->data, 0, 0);
  523. error:
  524. /* Close the cursor */
  525. if (NULL != cursor) {
  526. if (0 != cursor->c_close(cursor)) {
  527. ldbm_nasty(filename,33,ret);
  528. }
  529. }
  530. return ret;
  531. /* If this function is called in "no-allids" mode, then it's a bug */
  532. ldbm_nasty(filename,63,0);
  533. return -1;
  534. }
  535. #endif
  536. int idl_new_store_block(
  537. backend *be,
  538. DB *db,
  539. DBT *key,
  540. IDList *idl,
  541. DB_TXN *txn,
  542. struct attrinfo *a
  543. )
  544. {
  545. int ret = 0;
  546. DBC *cursor = NULL;
  547. DBT data = {0};
  548. ID id = 0;
  549. size_t x = 0;
  550. #if defined(DB_ALLIDS_ON_WRITE)
  551. db_recno_t count;
  552. #endif
  553. if (NULL == idl)
  554. {
  555. return ret;
  556. }
  557. /*
  558. * Really we need an extra entry point to the DB here, which
  559. * inserts a list of duplicate keys. In the meantime, we'll
  560. * just do it by brute force.
  561. */
  562. #if defined(DB_ALLIDS_ON_WRITE)
  563. /* allids check on input idl */
  564. if (ALLIDS(idl) || (idl->b_nids > (ID)idl_new_get_allidslimit(a))) {
  565. return idl_new_store_allids(be, db, key, txn);
  566. }
  567. #endif
  568. /* Make a cursor */
  569. ret = db->cursor(db,txn,&cursor,0);
  570. if (0 != ret) {
  571. ldbm_nasty(filename,41,ret);
  572. cursor = NULL;
  573. goto error;
  574. }
  575. /* initialize data DBT */
  576. data.data = &id;
  577. data.ulen = sizeof(id);
  578. data.size = sizeof(id);
  579. data.flags = DB_DBT_USERMEM;
  580. /* Position cursor at the key, value pair */
  581. ret = cursor->c_get(cursor,key,&data,DB_GET_BOTH);
  582. if (ret == DB_NOTFOUND) {
  583. ret = 0;
  584. } else if (ret != 0) {
  585. ldbm_nasty(filename,47,ret);
  586. goto error;
  587. }
  588. /* Iterate over the IDs in the idl */
  589. for (x = 0; x < idl->b_nids; x++) {
  590. /* insert an id */
  591. id = idl->b_ids[x];
  592. ret = cursor->c_put(cursor, key, &data, DB_NODUPDATA);
  593. if (0 != ret) {
  594. if (DB_KEYEXIST == ret) {
  595. ret = 0; /* exist is okay */
  596. } else {
  597. ldbm_nasty(filename,48,ret);
  598. goto error;
  599. }
  600. }
  601. }
  602. #if defined(DB_ALLIDS_ON_WRITE)
  603. /* check for allidslimit exceeded in database */
  604. if (cursor->c_count(cursor, &count, 0) != 0) {
  605. LDAPDebug(LDAP_DEBUG_ANY, "could not obtain count for key %s\n",
  606. key->data, 0, 0);
  607. goto error;
  608. }
  609. if ((size_t)count > idl_new_get_allidslimit(a)) {
  610. LDAPDebug(LDAP_DEBUG_TRACE, "allidslimit exceeded for key %s\n",
  611. key->data, 0, 0);
  612. cursor->c_close(cursor);
  613. cursor = NULL;
  614. ret = idl_new_store_allids(be, db, key, txn);
  615. }
  616. #endif
  617. error:
  618. /* Close the cursor */
  619. if (NULL != cursor) {
  620. if (0 != cursor->c_close(cursor)) {
  621. ldbm_nasty(filename,49,ret);
  622. }
  623. }
  624. return ret;
  625. }
  626. /* idl_new_compare_dups: comparing ID, pass to libdb for callback */
  627. int idl_new_compare_dups(
  628. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 3200
  629. DB *db,
  630. #endif
  631. const DBT *a,
  632. const DBT *b
  633. )
  634. {
  635. ID a_copy, b_copy;
  636. memmove(&a_copy, a->data, sizeof(ID));
  637. memmove(&b_copy, b->data, sizeof(ID));
  638. return a_copy - b_copy;
  639. }