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 #ifdef SLAP_IDL_CACHE
387 if ( bdb->bi_idl_cache_size ) {
388 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
389 if ( rc != LDAP_NO_SUCH_OBJECT ) {
390 op->o_tmpfree( key.data, op->o_tmpmemctx );
395 /* we actually could do a empty get... */
398 data.ulen = sizeof(id);
399 data.flags = DB_DBT_USERMEM;
401 data.dlen = sizeof(id);
403 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
404 op->o_tmpfree( key.data, op->o_tmpmemctx );
407 LDAP_LOG ( INDEX, DETAIL1,
408 "<= bdb_dn2id_children( %s ): %s (%d)\n",
409 e->e_nname.bv_val, rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
410 db_strerror(rc)), rc );
412 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %s (%d)\n",
414 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
415 db_strerror(rc) ), rc );
430 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
431 DB *db = bdb->bi_dn2id->bdi_db;
432 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
433 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
436 LDAP_LOG ( INDEX, ARGS, "=> bdb_dn2ididl( \"%s\" )\n",
437 e->e_nname.bv_val, 0, 0 );
439 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n",
440 e->e_nname.bv_val, 0, 0 );
443 #ifndef BDB_MULTIPLE_SUFFIXES
444 if ( prefix == DN_SUBTREE_PREFIX && BEI(e)->bei_parent->bei_id == 0 ) {
445 BDB_IDL_ALL(bdb, ids);
451 key.size = e->e_nname.bv_len + 2;
453 key.flags = DB_DBT_USERMEM;
454 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
455 ((char *)key.data)[0] = prefix;
456 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
458 rc = bdb_idl_fetch_key( op->o_bd, db, NULL, &key, ids );
462 LDAP_LOG ( INDEX, ERR,
463 "<= bdb_dn2ididl: get failed: %s (%d)\n", db_strerror(rc), rc, 0 );
465 Debug( LDAP_DEBUG_TRACE,
466 "<= bdb_dn2idl: get failed: %s (%d)\n",
467 db_strerror( rc ), rc, 0 );
472 LDAP_LOG ( INDEX, RESULTS,
473 "<= bdb_dn2ididl: id=%ld first=%ld last=%ld\n",
474 (long) ids[0], (long) BDB_IDL_FIRST( ids ),
475 (long) BDB_IDL_LAST( ids ) );
477 Debug( LDAP_DEBUG_TRACE,
478 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
480 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
484 op->o_tmpfree( key.data, op->o_tmpmemctx );
489 /* Experimental management routines for a hierarchically structured database.
491 * Unsupported! Use at your own risk!
492 * -- Howard Chu, Symas Corp. 2003.
494 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
495 * entry in this database is a struct diskNode, keyed by entryID and with
496 * the data containing the RDN and entryID of the node's children. We use
497 * a B-Tree with sorted duplicates to store all the children of a node under
498 * the same key. Also, the first item under the key contains the entry's own
499 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
500 * well as top-down. To keep this info first in the list, the nrdnlen is set
501 * to the negative of its value.
503 * The diskNode is a variable length structure. This definition is not
504 * directly usable for in-memory manipulation.
506 typedef struct diskNode {
513 /* Sort function for the sorted duplicate data items of a dn2id key.
514 * Sorts based on normalized RDN, in length order.
522 char *u = (char *)&(((diskNode *)(usrkey->data))->nrdnlen);
523 char *c = (char *)&(((diskNode *)(curkey->data))->nrdnlen);
526 /* data is not aligned, cannot compare directly */
527 #ifdef WORDS_BIGENDIAN
528 for( i = 0; i < (int)sizeof(short); i++)
530 for( i = sizeof(short)-1; i >= 0; i--)
536 return strcmp( u+sizeof(short), c+sizeof(short) );
539 /* This function constructs a full DN for a given entry.
546 int rlen = 0, nrlen = 0;
550 /* count length of all DN components */
551 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
552 rlen += ei->bei_rdn.bv_len + 1;
553 nrlen += ei->bei_nrdn.bv_len + 1;
554 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
557 /* See if the entry DN was invalidated by a subtree rename */
559 if ( BEI(e)->bei_modrdns >= max ) {
562 /* We found a mismatch, tell the caller to lock it */
563 if ( checkit == 1 ) {
566 /* checkit == 2. do the fix. */
567 free( e->e_name.bv_val );
568 free( e->e_nname.bv_val );
571 e->e_name.bv_len = rlen - 1;
572 e->e_nname.bv_len = nrlen - 1;
573 e->e_name.bv_val = ch_malloc(rlen);
574 e->e_nname.bv_val = ch_malloc(nrlen);
575 ptr = e->e_name.bv_val;
576 nptr = e->e_nname.bv_val;
577 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
578 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
579 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
580 if ( ei->bei_parent ) {
585 BEI(e)->bei_modrdns = max;
592 /* We add two elements to the DN2ID database - a data item under the parent's
593 * entryID containing the child's RDN and entryID, and an item under the
594 * child's entryID containing the parent's entryID.
603 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
604 DB *db = bdb->bi_dn2id->bdi_db;
610 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
612 rlen = dn_rdnlen( op->o_bd, &e->e_name );
614 nrlen = e->e_nname.bv_len;
615 rlen = e->e_name.bv_len;
618 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
619 d->entryID = e->e_id;
621 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
623 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
628 key.data = &eip->bei_id;
629 key.size = sizeof(ID);
630 key.flags = DB_DBT_USERMEM;
632 /* Need to make dummy root node once. Subsequent attempts
633 * will fail harmlessly.
635 if ( eip->bei_id == 0 ) {
636 diskNode dummy = {0};
638 data.size = sizeof(diskNode);
639 data.flags = DB_DBT_USERMEM;
641 db->put( db, txn, &key, &data, DB_NODUPDATA );
644 #ifdef SLAP_IDL_CACHE
645 if ( bdb->bi_idl_cache_size ) {
646 bdb_idl_cache_del( bdb, db, &key );
650 data.size = sizeof(diskNode) + rlen + nrlen;
651 data.flags = DB_DBT_USERMEM;
653 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
657 d->entryID = eip->bei_id;
658 d->nrdnlen = 0 - nrlen;
660 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
663 op->o_tmpfree( d, op->o_tmpmemctx );
675 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
676 DB *db = bdb->bi_dn2id->bdi_db;
683 key.size = sizeof(ID);
685 key.data = &eip->bei_id;
686 key.flags = DB_DBT_USERMEM;
689 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len;
690 data.ulen = data.size;
691 data.dlen = data.size;
692 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
694 #ifdef SLAP_IDL_CACHE
695 if ( bdb->bi_idl_cache_size ) {
696 bdb_idl_cache_del( bdb, db, &key );
699 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
702 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
703 d->entryID = e->e_id;
704 d->nrdnlen = BEI(e)->bei_nrdn.bv_len;
705 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
708 /* Delete our ID from the parent's list */
709 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH | DB_RMW );
711 rc = cursor->c_del( cursor, 0 );
713 /* Delete our ID from the tree. With sorted duplicates, this
714 * will leave any child nodes still hanging around. This is OK
715 * for modrdn, which will add our info back in later.
719 rc = cursor->c_get( cursor, &key, &data, DB_SET | DB_RMW );
721 rc = cursor->c_del( cursor, 0 );
723 cursor->c_close( cursor );
724 op->o_tmpfree( d, op->o_tmpmemctx );
737 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
738 DB *db = bdb->bi_dn2id->bdi_db;
744 ID idp = ei->bei_parent->bei_id;
746 nrlen = dn_rdnlen( op->o_bd, in );
747 if (!nrlen) nrlen = in->bv_len;
750 key.size = sizeof(ID);
752 key.ulen = sizeof(ID);
753 key.flags = DB_DBT_USERMEM;
756 data.size = sizeof(diskNode) + nrlen;
757 data.ulen = data.size * 3;
758 data.flags = DB_DBT_USERMEM;
760 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
763 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
765 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
769 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH );
771 ei->bei_id = d->entryID;
772 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
773 ptr = d->nrdn + nrlen + 1;
774 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
775 if ( !ei->bei_parent->bei_dkids ) {
777 /* How many children does the parent have? */
778 /* FIXME: do we need to lock the parent
779 * entryinfo? Seems safe...
781 cursor->c_count( cursor, &dkids, 0 );
782 ei->bei_parent->bei_dkids = dkids;
785 cursor->c_close( cursor );
786 op->o_tmpfree( d, op->o_tmpmemctx );
798 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
799 DB *db = bdb->bi_dn2id->bdi_db;
808 key.size = sizeof(ID);
809 key.data = &ei->bei_id;
810 key.ulen = sizeof(ID);
811 key.flags = DB_DBT_USERMEM;
814 data.flags = DB_DBT_USERMEM;
816 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
819 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
820 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
823 rc = cursor->c_get( cursor, &key, &data, DB_SET );
825 if (d->nrdnlen >= 0) {
830 ei->bei_nrdn.bv_len = 0 - d->nrdnlen;
831 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
832 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
834 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
835 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
836 /* How many children does this node have? */
837 cursor->c_count( cursor, &dkids, 0 );
838 ei->bei_dkids = dkids;
841 cursor->c_close( cursor );
842 op->o_tmpfree( d, op->o_tmpmemctx );
852 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
853 DB *db = bdb->bi_dn2id->bdi_db;
861 key.size = sizeof(ID);
863 key.flags = DB_DBT_USERMEM;
865 #ifdef SLAP_IDL_CACHE
866 if ( bdb->bi_idl_cache_size ) {
867 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
868 if ( rc != LDAP_NO_SUCH_OBJECT ) {
875 data.ulen = sizeof(d);
876 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
877 data.dlen = sizeof(d);
879 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
882 rc = cursor->c_get( cursor, &key, &data, DB_SET );
885 rc = cursor->c_count( cursor, &dkids, 0 );
887 BEI(e)->bei_dkids = dkids;
888 if ( dkids < 2 ) rc = DB_NOTFOUND;
891 cursor->c_close( cursor );
896 * We can't just use bdb_idl_fetch_key because
897 * 1 - our data items are longer than just an entry ID
898 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
900 * We descend the tree recursively, so we define this cookie
901 * to hold our necessary state information. The bdb_dn2idl_internal
902 * function uses this cookie when calling itself.
905 struct dn2id_cookie {
906 struct bdb_info *bdb;
915 ID tmp[BDB_IDL_DB_SIZE];
928 EntryInfo *ei = data;
931 bdb_idl_insert( idl, ei->bei_id );
937 struct dn2id_cookie *cx
940 #ifdef SLAP_IDL_CACHE
941 if ( cx->bdb->bi_idl_cache_size ) {
942 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
943 if ( cx->rc == DB_NOTFOUND ) {
946 if ( cx->rc == LDAP_SUCCESS ) {
951 BDB_IDL_ZERO( cx->tmp );
954 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
956 cx->rc = DB_NOTFOUND;
961 bdb_cache_entryinfo_lock( cx->ei );
963 /* If number of kids in the cache differs from on-disk, load
964 * up all the kids from the database
966 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
968 db_recno_t dkids = cx->ei->bei_dkids;
969 ei.bei_parent = cx->ei;
971 bdb_cache_entryinfo_unlock( cx->ei );
973 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
974 cx->bdb->bi_db_opflags );
975 if ( cx->rc ) return cx->rc;
977 cx->data.data = &cx->dbuf;
978 cx->data.ulen = sizeof(ID);
979 cx->data.dlen = sizeof(ID);
980 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
982 /* The first item holds the parent ID. Ignore it. */
983 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
985 cx->dbc->c_close( cx->dbc );
986 if ( cx->rc == DB_NOTFOUND ) goto saveit;
990 /* If the on-disk count is zero we've never checked it.
994 cx->dbc->c_count( cx->dbc, &dkids, 0 );
995 cx->ei->bei_dkids = dkids;
998 cx->data.data = cx->buf;
999 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1000 cx->data.flags = DB_DBT_USERMEM;
1002 /* Fetch the rest of the IDs in a loop... */
1003 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1004 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1007 DB_MULTIPLE_INIT( cx->ptr, &cx->data );
1009 DB_MULTIPLE_NEXT( cx->ptr, &cx->data, j, len );
1012 diskNode *d = (diskNode *)j;
1015 AC_MEMCPY( &ei.bei_id, &d->entryID, sizeof(ID) );
1016 AC_MEMCPY( &nrlen, &d->nrdnlen, sizeof(d->nrdnlen) );
1017 ei.bei_nrdn.bv_len = nrlen;
1018 /* nrdn/rdn are set in-place.
1019 * hdb_cache_load will copy them as needed
1021 ei.bei_nrdn.bv_val = d->nrdn;
1022 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1023 - ei.bei_nrdn.bv_len;
1024 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1025 bdb_idl_insert( cx->tmp, ei.bei_id );
1026 hdb_cache_load( cx->bdb, &ei, &ei2 );
1030 cx->rc = cx->dbc->c_close( cx->dbc );
1032 /* The in-memory cache is in sync with the on-disk data.
1033 * do we have any kids?
1036 if ( cx->ei->bei_ckids > 0 ) {
1037 /* Walk the kids tree; order is irrelevant since bdb_idl_insert
1038 * will insert in sorted order.
1040 avl_apply( cx->ei->bei_kids, apply_func,
1041 cx->tmp, -1, AVL_POSTORDER );
1043 bdb_cache_entryinfo_unlock( cx->ei );
1047 #ifdef SLAP_IDL_CACHE
1048 if ( cx->bdb->bi_idl_cache_max_size ) {
1049 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1054 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1055 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1056 if (cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS) {
1057 bdb_idl_union( cx->ids, cx->tmp );
1060 EntryInfo *ei = cx->ei;
1062 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1063 cx->op->o_tmpmemctx );
1064 BDB_IDL_CPY( save, cx->tmp );
1065 bdb_idl_union( cx->ids, cx->tmp );
1068 for ( cx->id = bdb_idl_first( save, &idcurs );
1070 cx->id = bdb_idl_next( save, &idcurs )) {
1072 hdb_dn2idl_internal( cx );
1073 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1076 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1077 if ( nokids ) ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1079 /* Make sure caller knows it had kids! */
1084 BDB_IDL_CPY( cx->ids, cx->tmp );
1097 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1098 struct dn2id_cookie cx;
1101 LDAP_LOG ( INDEX, ARGS,
1102 "=> hdb_dn2ididl( \"%s\" )\n", e->e_nname.bv_val, 0, 0 );
1104 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl( \"%s\" )\n",
1105 e->e_nname.bv_val, 0, 0 );
1108 #ifndef BDB_MULTIPLE_SUFFIXES
1109 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1110 BEI(e)->bei_parent->bei_id == 0 )
1112 BDB_IDL_ALL( bdb, ids );
1118 cx.ei = e->e_id ? BEI(e) : &bdb->bi_cache.c_dntree;
1120 cx.db = cx.bdb->bi_dn2id->bdi_db;
1121 cx.prefix = op->ors_scope == LDAP_SCOPE_ONELEVEL
1122 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1127 BDB_IDL_ZERO( ids );
1128 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1129 bdb_idl_insert( ids, cx.id );
1133 cx.key.data = &cx.id;
1134 cx.key.ulen = sizeof(ID);
1135 cx.key.size = sizeof(ID);
1136 cx.key.flags = DB_DBT_USERMEM;
1140 return hdb_dn2idl_internal(&cx);
1142 #endif /* BDB_HIER */