1 /* dn2id.c - routines to deal with the dn2id index */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
34 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
35 DB *db = bdb->bi_dn2id->bdi_db;
39 struct berval ptr, pdn;
42 LDAP_LOG ( INDEX, ARGS, "bdb_dn2id_add( \"%s\", 0x%08lx )\n",
43 e->e_ndn, (long) e->e_id, 0 );
45 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
46 e->e_ndn, (long) e->e_id, 0 );
48 assert( e->e_id != NOID );
51 key.size = e->e_nname.bv_len + 2;
53 key.flags = DB_DBT_USERMEM;
54 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
56 buf[0] = DN_BASE_PREFIX;
58 ptr.bv_len = e->e_nname.bv_len;
59 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
60 ptr.bv_val[ptr.bv_len] = '\0';
63 data.data = (char *) &e->e_id;
64 data.size = sizeof( e->e_id );
66 /* store it -- don't override */
67 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
70 LDAP_LOG ( INDEX, ERR, "bdb_dn2id_add: put failed: %s %d\n",
71 db_strerror(rc), rc, 0 );
73 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
74 db_strerror(rc), rc, 0 );
79 #ifndef BDB_MULTIPLE_SUFFIXES
80 if( !be_issuffix( op->o_bd, &ptr ))
83 buf[0] = DN_SUBTREE_PREFIX;
84 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
87 LDAP_LOG ( INDEX, ERR,
88 "=> bdb_dn2id_add: subtree (%s) put failed: %d\n",
91 Debug( LDAP_DEBUG_ANY,
92 "=> bdb_dn2id_add: subtree (%s) put failed: %d\n",
98 #ifdef BDB_MULTIPLE_SUFFIXES
99 if( !be_issuffix( op->o_bd, &ptr ))
102 dnParent( &ptr, &pdn );
104 key.size = pdn.bv_len + 2;
106 pdn.bv_val[-1] = DN_ONE_PREFIX;
107 key.data = pdn.bv_val-1;
110 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
114 LDAP_LOG ( INDEX, ERR,
115 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
118 Debug( LDAP_DEBUG_ANY,
119 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
126 #ifndef BDB_MULTIPLE_SUFFIXES
127 while( !be_issuffix( op->o_bd, &ptr ))
132 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
134 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
138 LDAP_LOG ( INDEX, ERR,
139 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
142 Debug( LDAP_DEBUG_ANY,
143 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
148 #ifdef BDB_MULTIPLE_SUFFIXES
149 if( be_issuffix( op->o_bd, &ptr )) break;
151 dnParent( &ptr, &pdn );
153 key.size = pdn.bv_len + 2;
155 key.data = pdn.bv_val - 1;
161 op->o_tmpfree( buf, op->o_tmpmemctx );
163 LDAP_LOG ( INDEX, RESULTS, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
165 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
177 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
178 DB *db = bdb->bi_dn2id->bdi_db;
182 struct berval pdn, ptr;
185 LDAP_LOG ( INDEX, ARGS,
186 "=> bdb_dn2id_delete ( \"%s\", 0x%08lx )\n", e->e_ndn, e->e_id, 0);
188 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
189 e->e_ndn, e->e_id, 0 );
193 key.size = e->e_nname.bv_len + 2;
194 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
196 key.flags = DB_DBT_USERMEM;
197 buf[0] = DN_BASE_PREFIX;
199 ptr.bv_len = e->e_nname.bv_len;
200 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
201 ptr.bv_val[ptr.bv_len] = '\0';
204 rc = db->del( db, txn, &key, 0 );
207 LDAP_LOG ( INDEX, ERR,
208 "=> bdb_dn2id_delete: delete failed: %s %d\n",
209 db_strerror(rc), rc, 0 );
211 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
212 db_strerror(rc), rc, 0 );
217 #ifndef BDB_MULTIPLE_SUFFIXES
218 if( !be_issuffix( op->o_bd, &ptr ))
221 buf[0] = DN_SUBTREE_PREFIX;
222 rc = db->del( db, txn, &key, 0 );
225 LDAP_LOG ( INDEX, ERR,
226 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
229 Debug( LDAP_DEBUG_ANY,
230 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
236 #ifdef BDB_MULTIPLE_SUFFIXES
237 if( !be_issuffix( op->o_bd, &ptr ))
240 dnParent( &ptr, &pdn );
242 key.size = pdn.bv_len + 2;
244 pdn.bv_val[-1] = DN_ONE_PREFIX;
245 key.data = pdn.bv_val - 1;
248 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
252 LDAP_LOG ( INDEX, ERR,
253 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
256 Debug( LDAP_DEBUG_ANY,
257 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
264 #ifndef BDB_MULTIPLE_SUFFIXES
265 while( !be_issuffix( op->o_bd, &ptr ))
270 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
272 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
275 LDAP_LOG ( INDEX, ERR,
276 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
279 Debug( LDAP_DEBUG_ANY,
280 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
285 #ifdef BDB_MULTIPLE_SUFFIXES
286 if( be_issuffix( op->o_bd, &ptr )) break;
288 dnParent( &ptr, &pdn );
290 key.size = pdn.bv_len + 2;
292 key.data = pdn.bv_val - 1;
298 op->o_tmpfree( buf, op->o_tmpmemctx );
300 LDAP_LOG ( INDEX, RESULTS, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
302 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
316 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
317 DB *db = bdb->bi_dn2id->bdi_db;
320 LDAP_LOG ( INDEX, ARGS, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
322 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
325 key.size = dn->bv_len + 2;
326 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
327 ((char *)key.data)[0] = DN_BASE_PREFIX;
328 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
332 data.data = &ei->bei_id;
333 data.ulen = sizeof(ID);
334 data.flags = DB_DBT_USERMEM;
337 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
341 LDAP_LOG ( INDEX, ERR, "<= bdb_dn2id: get failed %s (%d)\n",
342 db_strerror(rc), rc, 0 );
344 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
345 db_strerror( rc ), rc, 0 );
349 LDAP_LOG ( INDEX, RESULTS,
350 "<= bdb_dn2id: got id=0x%08lx\n", ei->bei_id, 0, 0 );
352 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
357 op->o_tmpfree( key.data, op->o_tmpmemctx );
368 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
369 DB *db = bdb->bi_dn2id->bdi_db;
374 LDAP_LOG ( INDEX, ARGS,
375 "=> bdb_dn2id_children( %s )\n", e->e_nname.bv_val, 0, 0 );
377 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
378 e->e_nname.bv_val, 0, 0 );
381 key.size = e->e_nname.bv_len + 2;
382 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
383 ((char *)key.data)[0] = DN_ONE_PREFIX;
384 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
386 if ( bdb->bi_idl_cache_size ) {
387 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
388 if ( rc != LDAP_NO_SUCH_OBJECT ) {
389 op->o_tmpfree( key.data, op->o_tmpmemctx );
393 /* we actually could do a empty get... */
396 data.ulen = sizeof(id);
397 data.flags = DB_DBT_USERMEM;
399 data.dlen = sizeof(id);
401 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
402 op->o_tmpfree( key.data, op->o_tmpmemctx );
405 LDAP_LOG ( INDEX, DETAIL1,
406 "<= bdb_dn2id_children( %s ): %s (%d)\n",
407 e->e_nname.bv_val, rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
408 db_strerror(rc)), rc );
410 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %s (%d)\n",
412 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
413 db_strerror(rc) ), rc );
428 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
429 DB *db = bdb->bi_dn2id->bdi_db;
430 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
431 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
434 LDAP_LOG ( INDEX, ARGS, "=> bdb_dn2ididl( \"%s\" )\n",
435 e->e_nname.bv_val, 0, 0 );
437 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n",
438 e->e_nname.bv_val, 0, 0 );
441 #ifndef BDB_MULTIPLE_SUFFIXES
442 if ( prefix == DN_SUBTREE_PREFIX && BEI(e)->bei_parent->bei_id == 0 ) {
443 BDB_IDL_ALL(bdb, ids);
449 key.size = e->e_nname.bv_len + 2;
451 key.flags = DB_DBT_USERMEM;
452 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
453 ((char *)key.data)[0] = prefix;
454 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
456 rc = bdb_idl_fetch_key( op->o_bd, db, NULL, &key, ids );
460 LDAP_LOG ( INDEX, ERR,
461 "<= bdb_dn2ididl: get failed: %s (%d)\n", db_strerror(rc), rc, 0 );
463 Debug( LDAP_DEBUG_TRACE,
464 "<= bdb_dn2idl: get failed: %s (%d)\n",
465 db_strerror( rc ), rc, 0 );
470 LDAP_LOG ( INDEX, RESULTS,
471 "<= bdb_dn2ididl: id=%ld first=%ld last=%ld\n",
472 (long) ids[0], (long) BDB_IDL_FIRST( ids ),
473 (long) BDB_IDL_LAST( ids ) );
475 Debug( LDAP_DEBUG_TRACE,
476 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
478 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
482 op->o_tmpfree( key.data, op->o_tmpmemctx );
487 /* Experimental management routines for a hierarchically structured database.
489 * Unsupported! Use at your own risk!
490 * -- Howard Chu, Symas Corp. 2003.
492 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
493 * entry in this database is a struct diskNode, keyed by entryID and with
494 * the data containing the RDN and entryID of the node's children. We use
495 * a B-Tree with sorted duplicates to store all the children of a node under
496 * the same key. Also, the first item under the key contains the entry's own
497 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
498 * well as top-down. To keep this info first in the list, the nrdnlen is set
499 * to the negative of its value.
501 * The diskNode is a variable length structure. This definition is not
502 * directly usable for in-memory manipulation.
504 typedef struct diskNode {
511 /* Sort function for the sorted duplicate data items of a dn2id key.
512 * Sorts based on normalized RDN, in length order.
520 signed char *u = (signed char *)&(((diskNode *)(usrkey->data))->nrdnlen);
521 signed char *c = (signed char *)&(((diskNode *)(curkey->data))->nrdnlen);
524 /* data is not aligned, cannot compare directly */
525 #ifdef WORDS_BIGENDIAN
526 for( i = 0; i < (int)sizeof(short); i++)
528 for( i = sizeof(short)-1; i >= 0; i--)
534 return strcmp( u+sizeof(short), c+sizeof(short) );
537 /* This function constructs a full DN for a given entry.
544 int rlen = 0, nrlen = 0;
548 /* count length of all DN components */
549 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
550 rlen += ei->bei_rdn.bv_len + 1;
551 nrlen += ei->bei_nrdn.bv_len + 1;
552 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
555 /* See if the entry DN was invalidated by a subtree rename */
557 if ( BEI(e)->bei_modrdns >= max ) {
560 /* We found a mismatch, tell the caller to lock it */
561 if ( checkit == 1 ) {
564 /* checkit == 2. do the fix. */
565 free( e->e_name.bv_val );
566 free( e->e_nname.bv_val );
569 e->e_name.bv_len = rlen - 1;
570 e->e_nname.bv_len = nrlen - 1;
571 e->e_name.bv_val = ch_malloc(rlen);
572 e->e_nname.bv_val = ch_malloc(nrlen);
573 ptr = e->e_name.bv_val;
574 nptr = e->e_nname.bv_val;
575 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
576 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
577 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
578 if ( ei->bei_parent ) {
583 BEI(e)->bei_modrdns = max;
590 /* We add two elements to the DN2ID database - a data item under the parent's
591 * entryID containing the child's RDN and entryID, and an item under the
592 * child's entryID containing the parent's entryID.
601 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
602 DB *db = bdb->bi_dn2id->bdi_db;
608 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
610 rlen = dn_rdnlen( op->o_bd, &e->e_name );
612 nrlen = e->e_nname.bv_len;
613 rlen = e->e_name.bv_len;
616 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
617 d->entryID = e->e_id;
619 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
621 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
626 key.data = &eip->bei_id;
627 key.size = sizeof(ID);
628 key.flags = DB_DBT_USERMEM;
630 /* Need to make dummy root node once. Subsequent attempts
631 * will fail harmlessly.
633 if ( eip->bei_id == 0 ) {
634 diskNode dummy = {0};
636 data.size = sizeof(diskNode);
637 data.flags = DB_DBT_USERMEM;
639 db->put( db, txn, &key, &data, DB_NODUPDATA );
642 if ( bdb->bi_idl_cache_size ) {
643 bdb_idl_cache_del( bdb, db, &key );
646 data.size = sizeof(diskNode) + rlen + nrlen;
647 data.flags = DB_DBT_USERMEM;
649 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
653 d->entryID = eip->bei_id;
654 d->nrdnlen = 0 - nrlen;
656 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
659 op->o_tmpfree( d, op->o_tmpmemctx );
671 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
672 DB *db = bdb->bi_dn2id->bdi_db;
679 key.size = sizeof(ID);
681 key.data = &eip->bei_id;
682 key.flags = DB_DBT_USERMEM;
685 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len;
686 data.ulen = data.size;
687 data.dlen = data.size;
688 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
690 if ( bdb->bi_idl_cache_size ) {
691 bdb_idl_cache_del( bdb, db, &key );
693 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
696 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
697 d->entryID = e->e_id;
698 d->nrdnlen = BEI(e)->bei_nrdn.bv_len;
699 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
702 /* Delete our ID from the parent's list */
703 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH | DB_RMW );
705 rc = cursor->c_del( cursor, 0 );
707 /* Delete our ID from the tree. With sorted duplicates, this
708 * will leave any child nodes still hanging around. This is OK
709 * for modrdn, which will add our info back in later.
713 rc = cursor->c_get( cursor, &key, &data, DB_SET | DB_RMW );
715 rc = cursor->c_del( cursor, 0 );
717 cursor->c_close( cursor );
718 op->o_tmpfree( d, op->o_tmpmemctx );
731 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
732 DB *db = bdb->bi_dn2id->bdi_db;
738 ID idp = ei->bei_parent->bei_id;
740 nrlen = dn_rdnlen( op->o_bd, in );
741 if (!nrlen) nrlen = in->bv_len;
744 key.size = sizeof(ID);
746 key.ulen = sizeof(ID);
747 key.flags = DB_DBT_USERMEM;
750 data.size = sizeof(diskNode) + nrlen;
751 data.ulen = data.size * 3;
752 data.flags = DB_DBT_USERMEM;
754 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
757 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
759 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
763 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH );
765 ei->bei_id = d->entryID;
766 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
767 ptr = d->nrdn + nrlen + 1;
768 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
769 if ( !ei->bei_parent->bei_dkids ) {
771 /* How many children does the parent have? */
772 /* FIXME: do we need to lock the parent
773 * entryinfo? Seems safe...
775 cursor->c_count( cursor, &dkids, 0 );
776 ei->bei_parent->bei_dkids = dkids;
779 cursor->c_close( cursor );
780 op->o_tmpfree( d, op->o_tmpmemctx );
792 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
793 DB *db = bdb->bi_dn2id->bdi_db;
802 key.size = sizeof(ID);
803 key.data = &ei->bei_id;
804 key.ulen = sizeof(ID);
805 key.flags = DB_DBT_USERMEM;
808 data.flags = DB_DBT_USERMEM;
810 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
813 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
814 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
817 rc = cursor->c_get( cursor, &key, &data, DB_SET );
819 if (d->nrdnlen >= 0) {
824 ei->bei_nrdn.bv_len = 0 - d->nrdnlen;
825 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
826 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
828 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
829 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
830 /* How many children does this node have? */
831 cursor->c_count( cursor, &dkids, 0 );
832 ei->bei_dkids = dkids;
835 cursor->c_close( cursor );
836 op->o_tmpfree( d, op->o_tmpmemctx );
846 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
847 DB *db = bdb->bi_dn2id->bdi_db;
855 key.size = sizeof(ID);
857 key.flags = DB_DBT_USERMEM;
859 if ( bdb->bi_idl_cache_size ) {
860 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
861 if ( rc != LDAP_NO_SUCH_OBJECT ) {
867 data.ulen = sizeof(d);
868 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
869 data.dlen = sizeof(d);
871 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
874 rc = cursor->c_get( cursor, &key, &data, DB_SET );
877 rc = cursor->c_count( cursor, &dkids, 0 );
879 BEI(e)->bei_dkids = dkids;
880 if ( dkids < 2 ) rc = DB_NOTFOUND;
883 cursor->c_close( cursor );
888 * We can't just use bdb_idl_fetch_key because
889 * 1 - our data items are longer than just an entry ID
890 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
892 * We descend the tree recursively, so we define this cookie
893 * to hold our necessary state information. The bdb_dn2idl_internal
894 * function uses this cookie when calling itself.
897 struct dn2id_cookie {
898 struct bdb_info *bdb;
907 ID tmp[BDB_IDL_DB_SIZE];
920 EntryInfo *ei = data;
923 bdb_idl_insert( idl, ei->bei_id );
929 struct dn2id_cookie *cx
932 if ( cx->bdb->bi_idl_cache_size ) {
933 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
934 if ( cx->rc == DB_NOTFOUND ) {
937 if ( cx->rc == LDAP_SUCCESS ) {
941 BDB_IDL_ZERO( cx->tmp );
944 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
946 cx->rc = DB_NOTFOUND;
951 bdb_cache_entryinfo_lock( cx->ei );
953 /* If number of kids in the cache differs from on-disk, load
954 * up all the kids from the database
956 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
958 db_recno_t dkids = cx->ei->bei_dkids;
959 ei.bei_parent = cx->ei;
961 bdb_cache_entryinfo_unlock( cx->ei );
963 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
964 cx->bdb->bi_db_opflags );
965 if ( cx->rc ) return cx->rc;
967 cx->data.data = &cx->dbuf;
968 cx->data.ulen = sizeof(ID);
969 cx->data.dlen = sizeof(ID);
970 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
972 /* The first item holds the parent ID. Ignore it. */
973 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
975 cx->dbc->c_close( cx->dbc );
976 if ( cx->rc == DB_NOTFOUND ) goto saveit;
980 /* If the on-disk count is zero we've never checked it.
984 cx->dbc->c_count( cx->dbc, &dkids, 0 );
985 cx->ei->bei_dkids = dkids;
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 diskNode *d = (diskNode *)j;
1005 AC_MEMCPY( &ei.bei_id, &d->entryID, sizeof(ID) );
1006 AC_MEMCPY( &nrlen, &d->nrdnlen, sizeof(d->nrdnlen) );
1007 ei.bei_nrdn.bv_len = nrlen;
1008 /* nrdn/rdn are set in-place.
1009 * hdb_cache_load will copy them as needed
1011 ei.bei_nrdn.bv_val = d->nrdn;
1012 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1013 - ei.bei_nrdn.bv_len;
1014 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1015 bdb_idl_insert( cx->tmp, ei.bei_id );
1016 hdb_cache_load( cx->bdb, &ei, &ei2 );
1020 cx->rc = cx->dbc->c_close( cx->dbc );
1022 /* The in-memory cache is in sync with the on-disk data.
1023 * do we have any kids?
1026 if ( cx->ei->bei_ckids > 0 ) {
1027 /* Walk the kids tree; order is irrelevant since bdb_idl_insert
1028 * will insert in sorted order.
1030 avl_apply( cx->ei->bei_kids, apply_func,
1031 cx->tmp, -1, AVL_POSTORDER );
1033 bdb_cache_entryinfo_unlock( cx->ei );
1037 if ( cx->bdb->bi_idl_cache_max_size ) {
1038 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1042 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1043 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1044 if (cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS) {
1045 bdb_idl_union( cx->ids, cx->tmp );
1048 EntryInfo *ei = cx->ei;
1050 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1051 cx->op->o_tmpmemctx );
1052 BDB_IDL_CPY( save, cx->tmp );
1053 bdb_idl_union( cx->ids, cx->tmp );
1056 for ( cx->id = bdb_idl_first( save, &idcurs );
1058 cx->id = bdb_idl_next( save, &idcurs )) {
1060 hdb_dn2idl_internal( cx );
1061 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1064 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1065 if ( nokids ) ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1067 /* Make sure caller knows it had kids! */
1072 BDB_IDL_CPY( cx->ids, cx->tmp );
1085 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1086 struct dn2id_cookie cx;
1089 LDAP_LOG ( INDEX, ARGS,
1090 "=> hdb_dn2ididl( \"%s\" )\n", e->e_nname.bv_val, 0, 0 );
1092 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl( \"%s\" )\n",
1093 e->e_nname.bv_val, 0, 0 );
1096 #ifndef BDB_MULTIPLE_SUFFIXES
1097 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1098 BEI(e)->bei_parent->bei_id == 0 )
1100 BDB_IDL_ALL( bdb, ids );
1106 cx.ei = e->e_id ? BEI(e) : &bdb->bi_cache.c_dntree;
1108 cx.db = cx.bdb->bi_dn2id->bdi_db;
1109 cx.prefix = op->ors_scope == LDAP_SCOPE_ONELEVEL
1110 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1115 BDB_IDL_ZERO( ids );
1116 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1117 bdb_idl_insert( ids, cx.id );
1121 cx.key.data = &cx.id;
1122 cx.key.ulen = sizeof(ID);
1123 cx.key.size = sizeof(ID);
1124 cx.key.flags = DB_DBT_USERMEM;
1128 return hdb_dn2idl_internal(&cx);
1130 #endif /* BDB_HIER */