1 /* dn2id.c - routines to deal with the dn2id index */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
25 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
26 DB *db = bdb->bi_dn2id->bdi_db;
30 struct berval ptr, pdn;
33 LDAP_LOG ( INDEX, ARGS, "bdb_dn2id_add( \"%s\", 0x%08lx )\n",
34 e->e_ndn, (long) e->e_id, 0 );
36 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
37 e->e_ndn, (long) e->e_id, 0 );
39 assert( e->e_id != NOID );
42 key.size = e->e_nname.bv_len + 2;
44 key.flags = DB_DBT_USERMEM;
45 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
47 buf[0] = DN_BASE_PREFIX;
49 ptr.bv_len = e->e_nname.bv_len;
50 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
51 ptr.bv_val[ptr.bv_len] = '\0';
54 data.data = (char *) &e->e_id;
55 data.size = sizeof( e->e_id );
57 /* store it -- don't override */
58 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
61 LDAP_LOG ( INDEX, ERR, "bdb_dn2id_add: put failed: %s %d\n",
62 db_strerror(rc), rc, 0 );
64 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
65 db_strerror(rc), rc, 0 );
70 #ifndef BDB_MULTIPLE_SUFFIXES
71 if( !be_issuffix( op->o_bd, &ptr )) {
73 buf[0] = DN_SUBTREE_PREFIX;
74 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
77 LDAP_LOG ( INDEX, ERR,
78 "=> bdb_dn2id_add: subtree (%s) put failed: %d\n",
81 Debug( LDAP_DEBUG_ANY,
82 "=> bdb_dn2id_add: subtree (%s) put failed: %d\n",
88 #ifdef BDB_MULTIPLE_SUFFIXES
89 if( !be_issuffix( op->o_bd, &ptr )) {
91 dnParent( &ptr, &pdn );
93 key.size = pdn.bv_len + 2;
95 pdn.bv_val[-1] = DN_ONE_PREFIX;
96 key.data = pdn.bv_val-1;
99 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
103 LDAP_LOG ( INDEX, ERR,
104 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
107 Debug( LDAP_DEBUG_ANY,
108 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
113 #ifndef BDB_MULTIPLE_SUFFIXES
116 while( !be_issuffix( op->o_bd, &ptr )) {
120 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
122 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
126 LDAP_LOG ( INDEX, ERR,
127 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
130 Debug( LDAP_DEBUG_ANY,
131 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
136 #ifdef BDB_MULTIPLE_SUFFIXES
137 if( be_issuffix( op->o_bd, &ptr )) break;
139 dnParent( &ptr, &pdn );
141 key.size = pdn.bv_len + 2;
143 key.data = pdn.bv_val - 1;
146 #ifdef BDB_MULTIPLE_SUFFIXES
151 op->o_tmpfree( buf, op->o_tmpmemctx );
153 LDAP_LOG ( INDEX, RESULTS, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
155 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
167 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
168 DB *db = bdb->bi_dn2id->bdi_db;
172 struct berval pdn, ptr;
175 LDAP_LOG ( INDEX, ARGS,
176 "=> bdb_dn2id_delete ( \"%s\", 0x%08lx )\n", e->e_ndn, e->e_id, 0);
178 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
179 e->e_ndn, e->e_id, 0 );
183 key.size = e->e_nname.bv_len + 2;
184 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
186 key.flags = DB_DBT_USERMEM;
187 buf[0] = DN_BASE_PREFIX;
189 ptr.bv_len = e->e_nname.bv_len;
190 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
191 ptr.bv_val[ptr.bv_len] = '\0';
194 rc = db->del( db, txn, &key, 0 );
197 LDAP_LOG ( INDEX, ERR,
198 "=> bdb_dn2id_delete: delete failed: %s %d\n",
199 db_strerror(rc), rc, 0 );
201 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
202 db_strerror(rc), rc, 0 );
207 #ifndef BDB_MULTIPLE_SUFFIXES
208 if( !be_issuffix( op->o_bd, &ptr )) {
210 buf[0] = DN_SUBTREE_PREFIX;
211 rc = db->del( db, txn, &key, 0 );
214 LDAP_LOG ( INDEX, ERR,
215 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
218 Debug( LDAP_DEBUG_ANY,
219 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
225 #ifdef BDB_MULTIPLE_SUFFIXES
226 if( !be_issuffix( op->o_bd, &ptr )) {
228 dnParent( &ptr, &pdn );
230 key.size = pdn.bv_len + 2;
232 pdn.bv_val[-1] = DN_ONE_PREFIX;
233 key.data = pdn.bv_val - 1;
236 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
240 LDAP_LOG ( INDEX, ERR,
241 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
244 Debug( LDAP_DEBUG_ANY,
245 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
250 #ifndef BDB_MULTIPLE_SUFFIXES
253 while( !be_issuffix( op->o_bd, &ptr )) {
257 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
259 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
262 LDAP_LOG ( INDEX, ERR,
263 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
266 Debug( LDAP_DEBUG_ANY,
267 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
272 #ifdef BDB_MULTIPLE_SUFFIXES
273 if( be_issuffix( op->o_bd, &ptr )) break;
275 dnParent( &ptr, &pdn );
277 key.size = pdn.bv_len + 2;
279 key.data = pdn.bv_val - 1;
282 #ifdef BDB_MULTIPLE_SUFFIXES
287 op->o_tmpfree( buf, op->o_tmpmemctx );
289 LDAP_LOG ( INDEX, RESULTS, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
291 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
305 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
306 DB *db = bdb->bi_dn2id->bdi_db;
309 LDAP_LOG ( INDEX, ARGS, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
311 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
314 key.size = dn->bv_len + 2;
315 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
316 ((char *)key.data)[0] = DN_BASE_PREFIX;
317 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
321 data.data = &ei->bei_id;
322 data.ulen = sizeof(ID);
323 data.flags = DB_DBT_USERMEM;
326 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
330 LDAP_LOG ( INDEX, ERR, "<= bdb_dn2id: get failed %s (%d)\n",
331 db_strerror(rc), rc, 0 );
333 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
334 db_strerror( rc ), rc, 0 );
338 LDAP_LOG ( INDEX, RESULTS,
339 "<= bdb_dn2id: got id=0x%08lx\n", ei->bei_id, 0, 0 );
341 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
346 op->o_tmpfree( key.data, op->o_tmpmemctx );
357 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
358 DB *db = bdb->bi_dn2id->bdi_db;
363 LDAP_LOG ( INDEX, ARGS,
364 "=> bdb_dn2id_children( %s )\n", e->e_nname.bv_val, 0, 0 );
366 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
367 e->e_nname.bv_val, 0, 0 );
370 key.size = e->e_nname.bv_len + 2;
371 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
372 ((char *)key.data)[0] = DN_ONE_PREFIX;
373 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
375 #ifdef SLAP_IDL_CACHE
376 if ( bdb->bi_idl_cache_size ) {
377 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
378 if ( rc != LDAP_NO_SUCH_OBJECT ) {
379 op->o_tmpfree( key.data, op->o_tmpmemctx );
384 /* we actually could do a empty get... */
387 data.ulen = sizeof(id);
388 data.flags = DB_DBT_USERMEM;
390 data.dlen = sizeof(id);
392 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
393 op->o_tmpfree( key.data, op->o_tmpmemctx );
396 LDAP_LOG ( INDEX, DETAIL1,
397 "<= bdb_dn2id_children( %s ): %s (%d)\n",
398 e->e_nname.bv_val, rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
399 db_strerror(rc)), rc );
401 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %s (%d)\n",
403 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
404 db_strerror(rc) ), rc );
419 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
420 DB *db = bdb->bi_dn2id->bdi_db;
421 int prefix = op->ors_scope == LDAP_SCOPE_SUBTREE ? DN_SUBTREE_PREFIX :
425 LDAP_LOG ( INDEX, ARGS,
426 "=> bdb_dn2ididl( \"%s\" )\n", e->e_nname.bv_val, 0, 0 );
428 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", e->e_nname.bv_val, 0, 0 );
431 #ifndef BDB_MULTIPLE_SUFFIXES
432 if (prefix == DN_SUBTREE_PREFIX && BEI(e)->bei_parent->bei_id == 0 )
434 BDB_IDL_ALL(bdb, ids);
440 key.size = e->e_nname.bv_len + 2;
442 key.flags = DB_DBT_USERMEM;
443 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
444 ((char *)key.data)[0] = prefix;
445 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
447 rc = bdb_idl_fetch_key( op->o_bd, db, NULL, &key, ids );
451 LDAP_LOG ( INDEX, ERR,
452 "<= bdb_dn2ididl: get failed: %s (%d)\n", db_strerror(rc), rc, 0 );
454 Debug( LDAP_DEBUG_TRACE,
455 "<= bdb_dn2idl: get failed: %s (%d)\n",
456 db_strerror( rc ), rc, 0 );
461 LDAP_LOG ( INDEX, RESULTS,
462 "<= bdb_dn2ididl: id=%ld first=%ld last=%ld\n",
463 (long) ids[0], (long) BDB_IDL_FIRST( ids ),
464 (long) BDB_IDL_LAST( ids ) );
466 Debug( LDAP_DEBUG_TRACE,
467 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
469 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
473 op->o_tmpfree( key.data, op->o_tmpmemctx );
478 /* Experimental management routines for a hierarchically structured database.
480 * Unsupported! Use at your own risk!
481 * -- Howard Chu, Symas Corp. 2003.
483 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
484 * entry in this database is a struct diskNode, keyed by entryID and with
485 * the data containing the RDN and entryID of the node's children. We use
486 * a B-Tree with sorted duplicates to store all the children of a node under
487 * the same key. Also, the first item under the key contains the entry's own
488 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
489 * well as top-down. To keep this info first in the list, the nrdnlen is set
490 * to the negative of its value.
492 * The diskNode is a variable length structure. This definition is not
493 * directly usable for in-memory manipulation.
495 typedef struct diskNode {
502 /* Sort function for the sorted duplicate data items of a dn2id key.
503 * Sorts based on normalized RDN, in length order.
512 char *u = (char *)&(((diskNode *)(usrkey->data))->nrdnlen);
513 char *c = (char *)&(((diskNode *)(curkey->data))->nrdnlen);
516 /* data is not aligned, cannot compare directly */
517 #ifdef WORDS_BIGENDIAN
518 for( i = 0; i < (int)sizeof(short); i++)
520 for( i = sizeof(short)-1; i >= 0; i--)
526 return strcmp( u+sizeof(short), c+sizeof(short) );
529 /* This function constructs a full DN for a given entry.
537 int rlen = 0, nrlen = 0;
541 /* count length of all DN components */
542 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
543 rlen += ei->bei_rdn.bv_len + 1;
544 nrlen += ei->bei_nrdn.bv_len + 1;
545 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
548 /* See if the entry DN was invalidated by a subtree rename */
550 if ( BEI(e)->bei_modrdns >= max ) {
553 /* We found a mismatch, tell the caller to lock it */
554 if ( checkit == 1 ) {
557 /* checkit == 2. do the fix. */
558 free( e->e_name.bv_val );
559 free( e->e_nname.bv_val );
562 e->e_name.bv_len = rlen - 1;
563 e->e_nname.bv_len = nrlen - 1;
564 e->e_name.bv_val = ch_malloc(rlen);
565 e->e_nname.bv_val = ch_malloc(nrlen);
566 ptr = e->e_name.bv_val;
567 nptr = e->e_nname.bv_val;
568 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
569 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
570 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
571 if ( ei->bei_parent ) {
576 BEI(e)->bei_modrdns = max;
583 /* We add two elements to the DN2ID database - a data item under the parent's
584 * entryID containing the child's RDN and entryID, and an item under the
585 * child's entryID containing the parent's entryID.
594 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
595 DB *db = bdb->bi_dn2id->bdi_db;
601 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
603 rlen = dn_rdnlen( op->o_bd, &e->e_name );
605 nrlen = e->e_nname.bv_len;
606 rlen = e->e_name.bv_len;
609 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
610 d->entryID = e->e_id;
612 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
614 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
619 key.data = &eip->bei_id;
620 key.size = sizeof(ID);
621 key.flags = DB_DBT_USERMEM;
623 #ifdef SLAP_IDL_CACHE
624 if ( bdb->bi_idl_cache_size ) {
625 bdb_idl_cache_del( bdb, db, &key );
629 data.size = sizeof(diskNode) + rlen + nrlen;
630 data.flags = DB_DBT_USERMEM;
632 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
636 d->entryID = eip->bei_id;
637 d->nrdnlen = 0 - nrlen;
639 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
642 op->o_tmpfree( d, op->o_tmpmemctx );
654 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
655 DB *db = bdb->bi_dn2id->bdi_db;
662 key.size = sizeof(ID);
664 key.data = &eip->bei_id;
665 key.flags = DB_DBT_USERMEM;
668 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len;
669 data.ulen = data.size;
670 data.dlen = data.size;
671 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
673 #ifdef SLAP_IDL_CACHE
674 if ( bdb->bi_idl_cache_size ) {
675 bdb_idl_cache_del( bdb, db, &key );
678 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
681 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
682 d->entryID = e->e_id;
683 d->nrdnlen = BEI(e)->bei_nrdn.bv_len;
684 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
687 /* Delete our ID from the parent's list */
688 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH | DB_RMW );
690 rc = cursor->c_del( cursor, 0 );
692 /* Delete our ID from the tree. With sorted duplicates, this
693 * will leave any child nodes still hanging around. This is OK
694 * for modrdn, which will add our info back in later.
698 rc = cursor->c_get( cursor, &key, &data, DB_SET | DB_RMW );
700 rc = cursor->c_del( cursor, 0 );
702 cursor->c_close( cursor );
703 op->o_tmpfree( d, op->o_tmpmemctx );
716 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
717 DB *db = bdb->bi_dn2id->bdi_db;
723 ID idp = ei->bei_parent->bei_id;
725 nrlen = dn_rdnlen( op->o_bd, in );
726 if (!nrlen) nrlen = in->bv_len;
729 key.size = sizeof(ID);
731 key.ulen = sizeof(ID);
732 key.flags = DB_DBT_USERMEM;
735 data.size = sizeof(diskNode) + nrlen;
736 data.ulen = data.size * 3;
737 data.flags = DB_DBT_USERMEM;
739 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
742 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
744 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
748 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH );
750 ei->bei_id = d->entryID;
751 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
752 ptr = d->nrdn + nrlen + 1;
753 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
754 if ( !ei->bei_parent->bei_dkids ) {
756 /* How many children does the parent have? */
757 /* FIXME: do we need to lock the parent
758 * entryinfo? Seems safe...
760 cursor->c_count( cursor, &dkids, 0 );
761 ei->bei_parent->bei_dkids = dkids;
764 cursor->c_close( cursor );
765 op->o_tmpfree( d, op->o_tmpmemctx );
776 diskNode *d = (diskNode *)buf;
779 AC_MEMCPY( &ei->bei_id, &d->entryID, sizeof(ID) );
780 AC_MEMCPY( &ei->bei_nrdn.bv_len, &d->nrdnlen, sizeof(d->nrdnlen) );
781 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 0, &ei->bei_nrdn );
782 ei->bei_rdn.bv_len = len - sizeof(diskNode) - ei->bei_nrdn.bv_len;
783 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
784 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
794 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
795 DB *db = bdb->bi_dn2id->bdi_db;
804 key.size = sizeof(ID);
805 key.data = &ei->bei_id;
806 key.ulen = sizeof(ID);
807 key.flags = DB_DBT_USERMEM;
810 data.flags = DB_DBT_USERMEM;
812 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
815 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
816 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
819 rc = cursor->c_get( cursor, &key, &data, DB_SET );
821 if (d->nrdnlen >= 0) {
826 ei->bei_nrdn.bv_len = 0 - d->nrdnlen;
827 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
828 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
830 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
831 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
832 /* How many children does this node have? */
833 cursor->c_count( cursor, &dkids, 0 );
834 ei->bei_dkids = dkids;
837 cursor->c_close( cursor );
838 op->o_tmpfree( d, op->o_tmpmemctx );
848 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
849 DB *db = bdb->bi_dn2id->bdi_db;
857 key.size = sizeof(ID);
859 key.flags = DB_DBT_USERMEM;
861 #ifdef SLAP_IDL_CACHE
862 if ( bdb->bi_idl_cache_size ) {
863 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
864 if ( rc != LDAP_NO_SUCH_OBJECT ) {
871 data.ulen = sizeof(d);
872 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
873 data.dlen = sizeof(d);
875 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
878 rc = cursor->c_get( cursor, &key, &data, DB_SET );
881 rc = cursor->c_count( cursor, &dkids, 0 );
883 BEI(e)->bei_dkids = dkids;
884 if ( dkids < 2 ) rc = DB_NOTFOUND;
887 cursor->c_close( cursor );
892 * We can't just use bdb_idl_fetch_key because
893 * 1 - our data items are longer than just an entry ID
894 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
896 * We descend the tree recursively, so we define this cookie
897 * to hold our necessary state information. The bdb_dn2idl_internal
898 * function uses this cookie when calling itself.
901 struct dn2id_cookie {
902 struct bdb_info *bdb;
911 ID tmp[BDB_IDL_DB_SIZE];
929 EntryInfo *ei = data;
930 struct apply_arg *ap = arg;
932 bdb_idl_insert( ap->idl, ei->bei_id );
941 struct dn2id_cookie *cx
944 EntryInfo **eilist = NULL, **ptr;
945 #ifdef SLAP_IDL_CACHE
946 if ( cx->bdb->bi_idl_cache_size ) {
947 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
948 if ( cx->rc == DB_NOTFOUND ) {
951 if ( cx->rc == LDAP_SUCCESS ) {
956 BDB_IDL_ZERO( cx->tmp );
958 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
960 ei.bei_parent = cx->ei;
962 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
963 cx->bdb->bi_db_opflags );
964 if ( cx->rc ) return cx->rc;
966 cx->data.data = &cx->dbuf;
967 cx->data.ulen = sizeof(ID);
968 cx->data.dlen = sizeof(ID);
969 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
971 /* The first item holds the parent ID. Ignore it. */
972 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
973 if ( cx->rc == DB_NOTFOUND ) goto saveit;
974 if ( cx->rc ) return cx->rc;
976 if ( !cx->ei->bei_dkids ) {
978 cx->dbc->c_count( cx->dbc, &dkids, 0 );
979 cx->ei->bei_dkids = dkids;
982 if ( cx->prefix == DN_SUBTREE_PREFIX && cx->ei->bei_dkids > 1 ) {
983 eilist = cx->op->o_tmpalloc( sizeof(EntryInfo *) * cx->ei->bei_dkids, cx->op->o_tmpmemctx );
984 eilist[cx->ei->bei_dkids-1] = NULL;
988 cx->data.data = cx->buf;
989 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
990 cx->data.flags = DB_DBT_USERMEM;
992 /* Fetch the rest of the IDs in a loop... */
993 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
994 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
997 DB_MULTIPLE_INIT( cx->ptr, &cx->data );
999 DB_MULTIPLE_NEXT( cx->ptr, &cx->data, j, len );
1002 hdb_dn2ei( j, len, &ei );
1003 bdb_idl_insert( cx->tmp, ei.bei_id );
1004 hdb_entryinfo_load( cx->bdb, &ei, &ei2 );
1010 cx->dbc->c_close( cx->dbc );
1012 if ( cx->ei->bei_ckids > 0 ) {
1013 struct apply_arg ap;
1014 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1015 eilist = cx->op->o_tmpalloc( sizeof(EntryInfo *) * cx->ei->bei_dkids, cx->op->o_tmpmemctx );
1016 eilist[cx->ei->bei_dkids-1] = NULL;
1020 bdb_cache_entryinfo_lock( cx->ei );
1021 avl_apply( cx->ei->bei_kids, apply_func, &ap, -1, AVL_POSTORDER );
1022 bdb_cache_entryinfo_unlock( cx->ei );
1026 /* If we got some records, treat as success */
1027 if (!BDB_IDL_IS_ZERO(cx->tmp)) {
1032 #ifdef SLAP_IDL_CACHE
1033 if ( cx->bdb->bi_idl_cache_max_size ) {
1034 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1039 if ( cx->rc == 0 ) {
1040 /* If eilist is NULL, cx->tmp is empty... */
1041 if ( cx->prefix == DN_SUBTREE_PREFIX && eilist ) {
1042 bdb_idl_union( cx->ids, cx->tmp );
1043 for (ptr = eilist; *ptr; ptr++) {
1045 cx->id = cx->ei->bei_id;
1046 hdb_dn2idl_internal( cx );
1047 cx->op->o_tmpfree( eilist, cx->op->o_tmpmemctx );
1050 BDB_IDL_CPY( cx->ids, cx->tmp );
1063 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1064 struct dn2id_cookie cx;
1067 LDAP_LOG ( INDEX, ARGS,
1068 "=> hdb_dn2ididl( \"%s\" )\n", e->e_nname.bv_val, 0, 0 );
1070 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl( \"%s\" )\n", e->e_nname.bv_val, 0, 0 );
1073 #ifndef BDB_MULTIPLE_SUFFIXES
1074 if ( op->ors_scope == LDAP_SCOPE_SUBTREE &&
1075 BEI(e)->bei_parent->bei_id == 0 ) {
1076 BDB_IDL_ALL( bdb, ids );
1084 cx.db = cx.bdb->bi_dn2id->bdi_db;
1085 cx.prefix = op->ors_scope == LDAP_SCOPE_SUBTREE ? DN_SUBTREE_PREFIX :
1091 BDB_IDL_ZERO( ids );
1092 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1093 bdb_idl_insert( ids, cx.id );
1097 cx.key.data = &cx.id;
1098 cx.key.ulen = sizeof(ID);
1099 cx.key.size = sizeof(ID);
1100 cx.key.flags = DB_DBT_USERMEM;
1104 return hdb_dn2idl_internal(&cx);
1106 #endif /* BDB_HIER */