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 )) {
82 buf[0] = DN_SUBTREE_PREFIX;
83 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
86 LDAP_LOG ( INDEX, ERR,
87 "=> bdb_dn2id_add: subtree (%s) put failed: %d\n",
90 Debug( LDAP_DEBUG_ANY,
91 "=> bdb_dn2id_add: subtree (%s) put failed: %d\n",
97 #ifdef BDB_MULTIPLE_SUFFIXES
98 if( !be_issuffix( op->o_bd, &ptr )) {
100 dnParent( &ptr, &pdn );
102 key.size = pdn.bv_len + 2;
104 pdn.bv_val[-1] = DN_ONE_PREFIX;
105 key.data = pdn.bv_val-1;
108 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
112 LDAP_LOG ( INDEX, ERR,
113 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
116 Debug( LDAP_DEBUG_ANY,
117 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
122 #ifndef BDB_MULTIPLE_SUFFIXES
125 while( !be_issuffix( op->o_bd, &ptr ))
130 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
132 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
136 LDAP_LOG ( INDEX, ERR,
137 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
140 Debug( LDAP_DEBUG_ANY,
141 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
146 #ifdef BDB_MULTIPLE_SUFFIXES
147 if( be_issuffix( op->o_bd, &ptr )) break;
149 dnParent( &ptr, &pdn );
151 key.size = pdn.bv_len + 2;
153 key.data = pdn.bv_val - 1;
156 #ifdef BDB_MULTIPLE_SUFFIXES
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 )) {
220 buf[0] = DN_SUBTREE_PREFIX;
221 rc = db->del( db, txn, &key, 0 );
224 LDAP_LOG ( INDEX, ERR,
225 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
228 Debug( LDAP_DEBUG_ANY,
229 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
235 #ifdef BDB_MULTIPLE_SUFFIXES
236 if( !be_issuffix( op->o_bd, &ptr )) {
238 dnParent( &ptr, &pdn );
240 key.size = pdn.bv_len + 2;
242 pdn.bv_val[-1] = DN_ONE_PREFIX;
243 key.data = pdn.bv_val - 1;
246 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
250 LDAP_LOG ( INDEX, ERR,
251 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
254 Debug( LDAP_DEBUG_ANY,
255 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
260 #ifndef BDB_MULTIPLE_SUFFIXES
263 while( !be_issuffix( op->o_bd, &ptr )) {
267 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
269 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
272 LDAP_LOG ( INDEX, ERR,
273 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
276 Debug( LDAP_DEBUG_ANY,
277 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
282 #ifdef BDB_MULTIPLE_SUFFIXES
283 if( be_issuffix( op->o_bd, &ptr )) break;
285 dnParent( &ptr, &pdn );
287 key.size = pdn.bv_len + 2;
289 key.data = pdn.bv_val - 1;
292 #ifdef BDB_MULTIPLE_SUFFIXES
297 op->o_tmpfree( buf, op->o_tmpmemctx );
299 LDAP_LOG ( INDEX, RESULTS, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
301 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
315 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
316 DB *db = bdb->bi_dn2id->bdi_db;
319 LDAP_LOG ( INDEX, ARGS, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
321 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
324 key.size = dn->bv_len + 2;
325 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
326 ((char *)key.data)[0] = DN_BASE_PREFIX;
327 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
331 data.data = &ei->bei_id;
332 data.ulen = sizeof(ID);
333 data.flags = DB_DBT_USERMEM;
336 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
340 LDAP_LOG ( INDEX, ERR, "<= bdb_dn2id: get failed %s (%d)\n",
341 db_strerror(rc), rc, 0 );
343 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
344 db_strerror( rc ), rc, 0 );
348 LDAP_LOG ( INDEX, RESULTS,
349 "<= bdb_dn2id: got id=0x%08lx\n", ei->bei_id, 0, 0 );
351 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
356 op->o_tmpfree( key.data, op->o_tmpmemctx );
367 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
368 DB *db = bdb->bi_dn2id->bdi_db;
373 LDAP_LOG ( INDEX, ARGS,
374 "=> bdb_dn2id_children( %s )\n", e->e_nname.bv_val, 0, 0 );
376 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
377 e->e_nname.bv_val, 0, 0 );
380 key.size = e->e_nname.bv_len + 2;
381 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
382 ((char *)key.data)[0] = DN_ONE_PREFIX;
383 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
385 #ifdef SLAP_IDL_CACHE
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 );
394 /* we actually could do a empty get... */
397 data.ulen = sizeof(id);
398 data.flags = DB_DBT_USERMEM;
400 data.dlen = sizeof(id);
402 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
403 op->o_tmpfree( key.data, op->o_tmpmemctx );
406 LDAP_LOG ( INDEX, DETAIL1,
407 "<= bdb_dn2id_children( %s ): %s (%d)\n",
408 e->e_nname.bv_val, rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
409 db_strerror(rc)), rc );
411 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %s (%d)\n",
413 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
414 db_strerror(rc) ), rc );
429 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
430 DB *db = bdb->bi_dn2id->bdi_db;
431 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
432 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
435 LDAP_LOG ( INDEX, ARGS, "=> bdb_dn2ididl( \"%s\" )\n",
436 e->e_nname.bv_val, 0, 0 );
438 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n",
439 e->e_nname.bv_val, 0, 0 );
442 #ifndef BDB_MULTIPLE_SUFFIXES
443 if ( prefix == DN_SUBTREE_PREFIX && BEI(e)->bei_parent->bei_id == 0 ) {
444 BDB_IDL_ALL(bdb, ids);
450 key.size = e->e_nname.bv_len + 2;
452 key.flags = DB_DBT_USERMEM;
453 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
454 ((char *)key.data)[0] = prefix;
455 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
457 rc = bdb_idl_fetch_key( op->o_bd, db, NULL, &key, ids );
461 LDAP_LOG ( INDEX, ERR,
462 "<= bdb_dn2ididl: get failed: %s (%d)\n", db_strerror(rc), rc, 0 );
464 Debug( LDAP_DEBUG_TRACE,
465 "<= bdb_dn2idl: get failed: %s (%d)\n",
466 db_strerror( rc ), rc, 0 );
471 LDAP_LOG ( INDEX, RESULTS,
472 "<= bdb_dn2ididl: id=%ld first=%ld last=%ld\n",
473 (long) ids[0], (long) BDB_IDL_FIRST( ids ),
474 (long) BDB_IDL_LAST( ids ) );
476 Debug( LDAP_DEBUG_TRACE,
477 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
479 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
483 op->o_tmpfree( key.data, op->o_tmpmemctx );
488 /* Experimental management routines for a hierarchically structured database.
490 * Unsupported! Use at your own risk!
491 * -- Howard Chu, Symas Corp. 2003.
493 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
494 * entry in this database is a struct diskNode, keyed by entryID and with
495 * the data containing the RDN and entryID of the node's children. We use
496 * a B-Tree with sorted duplicates to store all the children of a node under
497 * the same key. Also, the first item under the key contains the entry's own
498 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
499 * well as top-down. To keep this info first in the list, the nrdnlen is set
500 * to the negative of its value.
502 * The diskNode is a variable length structure. This definition is not
503 * directly usable for in-memory manipulation.
505 typedef struct diskNode {
512 /* Sort function for the sorted duplicate data items of a dn2id key.
513 * 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.
547 int rlen = 0, nrlen = 0;
551 /* count length of all DN components */
552 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
553 rlen += ei->bei_rdn.bv_len + 1;
554 nrlen += ei->bei_nrdn.bv_len + 1;
555 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
558 /* See if the entry DN was invalidated by a subtree rename */
560 if ( BEI(e)->bei_modrdns >= max ) {
563 /* We found a mismatch, tell the caller to lock it */
564 if ( checkit == 1 ) {
567 /* checkit == 2. do the fix. */
568 free( e->e_name.bv_val );
569 free( e->e_nname.bv_val );
572 e->e_name.bv_len = rlen - 1;
573 e->e_nname.bv_len = nrlen - 1;
574 e->e_name.bv_val = ch_malloc(rlen);
575 e->e_nname.bv_val = ch_malloc(nrlen);
576 ptr = e->e_name.bv_val;
577 nptr = e->e_nname.bv_val;
578 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
579 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
580 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
581 if ( ei->bei_parent ) {
586 BEI(e)->bei_modrdns = max;
593 /* We add two elements to the DN2ID database - a data item under the parent's
594 * entryID containing the child's RDN and entryID, and an item under the
595 * child's entryID containing the parent's entryID.
604 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
605 DB *db = bdb->bi_dn2id->bdi_db;
611 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
613 rlen = dn_rdnlen( op->o_bd, &e->e_name );
615 nrlen = e->e_nname.bv_len;
616 rlen = e->e_name.bv_len;
619 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
620 d->entryID = e->e_id;
622 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
624 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
629 key.data = &eip->bei_id;
630 key.size = sizeof(ID);
631 key.flags = DB_DBT_USERMEM;
633 /* Need to make dummy root node once. Subsequent attempts
634 * will fail harmlessly.
636 if ( eip->bei_id == 0 ) {
637 diskNode dummy = {0};
639 data.size = sizeof(diskNode);
640 data.flags = DB_DBT_USERMEM;
642 db->put( db, txn, &key, &data, DB_NODUPDATA );
645 #ifdef SLAP_IDL_CACHE
646 if ( bdb->bi_idl_cache_size ) {
647 bdb_idl_cache_del( bdb, db, &key );
651 data.size = sizeof(diskNode) + rlen + nrlen;
652 data.flags = DB_DBT_USERMEM;
654 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
658 d->entryID = eip->bei_id;
659 d->nrdnlen = 0 - nrlen;
661 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
664 op->o_tmpfree( d, op->o_tmpmemctx );
676 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
677 DB *db = bdb->bi_dn2id->bdi_db;
684 key.size = sizeof(ID);
686 key.data = &eip->bei_id;
687 key.flags = DB_DBT_USERMEM;
690 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len;
691 data.ulen = data.size;
692 data.dlen = data.size;
693 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
695 #ifdef SLAP_IDL_CACHE
696 if ( bdb->bi_idl_cache_size ) {
697 bdb_idl_cache_del( bdb, db, &key );
700 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
703 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
704 d->entryID = e->e_id;
705 d->nrdnlen = BEI(e)->bei_nrdn.bv_len;
706 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
709 /* Delete our ID from the parent's list */
710 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH | DB_RMW );
712 rc = cursor->c_del( cursor, 0 );
714 /* Delete our ID from the tree. With sorted duplicates, this
715 * will leave any child nodes still hanging around. This is OK
716 * for modrdn, which will add our info back in later.
720 rc = cursor->c_get( cursor, &key, &data, DB_SET | DB_RMW );
722 rc = cursor->c_del( cursor, 0 );
724 cursor->c_close( cursor );
725 op->o_tmpfree( d, op->o_tmpmemctx );
738 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
739 DB *db = bdb->bi_dn2id->bdi_db;
745 ID idp = ei->bei_parent->bei_id;
747 nrlen = dn_rdnlen( op->o_bd, in );
748 if (!nrlen) nrlen = in->bv_len;
751 key.size = sizeof(ID);
753 key.ulen = sizeof(ID);
754 key.flags = DB_DBT_USERMEM;
757 data.size = sizeof(diskNode) + nrlen;
758 data.ulen = data.size * 3;
759 data.flags = DB_DBT_USERMEM;
761 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
764 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
766 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
770 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH );
772 ei->bei_id = d->entryID;
773 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
774 ptr = d->nrdn + nrlen + 1;
775 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
776 if ( !ei->bei_parent->bei_dkids ) {
778 /* How many children does the parent have? */
779 /* FIXME: do we need to lock the parent
780 * entryinfo? Seems safe...
782 cursor->c_count( cursor, &dkids, 0 );
783 ei->bei_parent->bei_dkids = dkids;
786 cursor->c_close( cursor );
787 op->o_tmpfree( d, op->o_tmpmemctx );
799 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
800 DB *db = bdb->bi_dn2id->bdi_db;
809 key.size = sizeof(ID);
810 key.data = &ei->bei_id;
811 key.ulen = sizeof(ID);
812 key.flags = DB_DBT_USERMEM;
815 data.flags = DB_DBT_USERMEM;
817 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
820 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
821 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
824 rc = cursor->c_get( cursor, &key, &data, DB_SET );
826 if (d->nrdnlen >= 0) {
831 ei->bei_nrdn.bv_len = 0 - d->nrdnlen;
832 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
833 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
835 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
836 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
837 /* How many children does this node have? */
838 cursor->c_count( cursor, &dkids, 0 );
839 ei->bei_dkids = dkids;
842 cursor->c_close( cursor );
843 op->o_tmpfree( d, op->o_tmpmemctx );
853 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
854 DB *db = bdb->bi_dn2id->bdi_db;
862 key.size = sizeof(ID);
864 key.flags = DB_DBT_USERMEM;
866 #ifdef SLAP_IDL_CACHE
867 if ( bdb->bi_idl_cache_size ) {
868 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
869 if ( rc != LDAP_NO_SUCH_OBJECT ) {
876 data.ulen = sizeof(d);
877 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
878 data.dlen = sizeof(d);
880 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
883 rc = cursor->c_get( cursor, &key, &data, DB_SET );
886 rc = cursor->c_count( cursor, &dkids, 0 );
888 BEI(e)->bei_dkids = dkids;
889 if ( dkids < 2 ) rc = DB_NOTFOUND;
892 cursor->c_close( cursor );
897 * We can't just use bdb_idl_fetch_key because
898 * 1 - our data items are longer than just an entry ID
899 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
901 * We descend the tree recursively, so we define this cookie
902 * to hold our necessary state information. The bdb_dn2idl_internal
903 * function uses this cookie when calling itself.
906 struct dn2id_cookie {
907 struct bdb_info *bdb;
916 ID tmp[BDB_IDL_DB_SIZE];
929 EntryInfo *ei = data;
932 bdb_idl_insert( idl, ei->bei_id );
938 struct dn2id_cookie *cx
941 #ifdef SLAP_IDL_CACHE
942 if ( cx->bdb->bi_idl_cache_size ) {
943 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
944 if ( cx->rc == DB_NOTFOUND ) {
947 if ( cx->rc == LDAP_SUCCESS ) {
952 BDB_IDL_ZERO( cx->tmp );
955 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
957 cx->rc = DB_NOTFOUND;
962 bdb_cache_entryinfo_lock( cx->ei );
964 /* If number of kids in the cache differs from on-disk, load
965 * up all the kids from the database
967 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
969 db_recno_t dkids = cx->ei->bei_dkids;
970 ei.bei_parent = cx->ei;
972 bdb_cache_entryinfo_unlock( cx->ei );
974 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
975 cx->bdb->bi_db_opflags );
976 if ( cx->rc ) return cx->rc;
978 cx->data.data = &cx->dbuf;
979 cx->data.ulen = sizeof(ID);
980 cx->data.dlen = sizeof(ID);
981 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
983 /* The first item holds the parent ID. Ignore it. */
984 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
986 cx->dbc->c_close( cx->dbc );
987 if ( cx->rc == DB_NOTFOUND ) goto saveit;
991 /* If the on-disk count is zero we've never checked it.
995 cx->dbc->c_count( cx->dbc, &dkids, 0 );
996 cx->ei->bei_dkids = dkids;
999 cx->data.data = cx->buf;
1000 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1001 cx->data.flags = DB_DBT_USERMEM;
1003 /* Fetch the rest of the IDs in a loop... */
1004 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1005 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1008 DB_MULTIPLE_INIT( cx->ptr, &cx->data );
1010 DB_MULTIPLE_NEXT( cx->ptr, &cx->data, j, len );
1013 diskNode *d = (diskNode *)j;
1016 AC_MEMCPY( &ei.bei_id, &d->entryID, sizeof(ID) );
1017 AC_MEMCPY( &nrlen, &d->nrdnlen, sizeof(d->nrdnlen) );
1018 ei.bei_nrdn.bv_len = nrlen;
1019 /* nrdn/rdn are set in-place.
1020 * hdb_cache_load will copy them as needed
1022 ei.bei_nrdn.bv_val = d->nrdn;
1023 ei.bei_rdn.bv_len = len - sizeof(diskNode) - 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 ) {
1038 /* Walk the kids tree; order is irrelevant since bdb_idl_insert
1039 * will insert in sorted order.
1041 avl_apply( cx->ei->bei_kids, apply_func, 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", e->e_nname.bv_val, 0, 0 );
1107 #ifndef BDB_MULTIPLE_SUFFIXES
1108 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1109 BEI(e)->bei_parent->bei_id == 0 )
1111 BDB_IDL_ALL( bdb, ids );
1117 cx.ei = e->e_id ? BEI(e) : &bdb->bi_cache.c_dntree;
1119 cx.db = cx.bdb->bi_dn2id->bdi_db;
1120 cx.prefix = op->ors_scope == LDAP_SCOPE_ONELEVEL
1121 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1126 BDB_IDL_ZERO( ids );
1127 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1128 bdb_idl_insert( ids, cx.id );
1132 cx.key.data = &cx.id;
1133 cx.key.ulen = sizeof(ID);
1134 cx.key.size = sizeof(ID);
1135 cx.key.flags = DB_DBT_USERMEM;
1139 return hdb_dn2idl_internal(&cx);
1141 #endif /* BDB_HIER */