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-2008 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>
26 #define bdb_dn2id_lock BDB_SYMBOL(dn2id_lock)
29 bdb_dn2id_lock( struct bdb_info *bdb, struct berval *dn,
30 int rw, DB_TXN *txn, DB_LOCK *lock )
40 db_rw = DB_LOCK_WRITE;
44 lockobj.data = dn->bv_val;
45 lockobj.size = dn->bv_len;
47 rc = LOCK_GET(bdb->bi_dbenv, TXN_ID(txn), DB_LOCK_NOWAIT,
48 &lockobj, db_rw, lock);
60 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
61 DB *db = bdb->bi_dn2id->bdi_db;
66 struct berval ptr, pdn;
68 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add 0x%lx: \"%s\"\n",
69 e->e_id, e->e_ndn, 0 );
70 assert( e->e_id != NOID );
73 key.size = e->e_nname.bv_len + 2;
75 key.flags = DB_DBT_USERMEM;
76 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
78 buf[0] = DN_BASE_PREFIX;
80 ptr.bv_len = e->e_nname.bv_len;
81 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
82 ptr.bv_val[ptr.bv_len] = '\0';
86 data.size = sizeof( nid );
87 BDB_ID2DISK( e->e_id, &nid );
89 /* store it -- don't override */
90 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
92 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add 0x%lx: put failed: %s %d\n",
93 e->e_id, db_strerror(rc), rc );
97 #ifndef BDB_MULTIPLE_SUFFIXES
98 if( !be_issuffix( op->o_bd, &ptr ))
101 buf[0] = DN_SUBTREE_PREFIX;
102 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
104 Debug( LDAP_DEBUG_ANY,
105 "=> bdb_dn2id_add 0x%lx: subtree (%s) put failed: %d\n",
106 e->e_id, ptr.bv_val, rc );
110 #ifdef BDB_MULTIPLE_SUFFIXES
111 if( !be_issuffix( op->o_bd, &ptr ))
114 dnParent( &ptr, &pdn );
116 key.size = pdn.bv_len + 2;
118 pdn.bv_val[-1] = DN_ONE_PREFIX;
119 key.data = pdn.bv_val-1;
122 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
125 Debug( LDAP_DEBUG_ANY,
126 "=> bdb_dn2id_add 0x%lx: parent (%s) insert failed: %d\n",
127 e->e_id, ptr.bv_val, rc );
132 #ifndef BDB_MULTIPLE_SUFFIXES
133 while( !be_issuffix( op->o_bd, &ptr ))
138 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
140 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
143 Debug( LDAP_DEBUG_ANY,
144 "=> bdb_dn2id_add 0x%lx: subtree (%s) insert failed: %d\n",
145 e->e_id, ptr.bv_val, rc );
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 );
162 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
173 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
174 DB *db = bdb->bi_dn2id->bdi_db;
178 struct berval pdn, ptr;
181 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete 0x%lx: \"%s\"\n",
182 e->e_id, e->e_ndn, 0 );
185 key.size = e->e_nname.bv_len + 2;
186 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
188 key.flags = DB_DBT_USERMEM;
189 buf[0] = DN_BASE_PREFIX;
191 ptr.bv_len = e->e_nname.bv_len;
192 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
193 ptr.bv_val[ptr.bv_len] = '\0';
195 /* We hold this lock until the TXN completes */
196 rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, txn, &lock );
200 rc = db->del( db, txn, &key, 0 );
202 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete 0x%lx: delete failed: %s %d\n",
203 e->e_id, db_strerror(rc), rc );
207 #ifndef BDB_MULTIPLE_SUFFIXES
208 if( !be_issuffix( op->o_bd, &ptr ))
211 buf[0] = DN_SUBTREE_PREFIX;
212 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
214 Debug( LDAP_DEBUG_ANY,
215 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
216 e->e_id, ptr.bv_val, rc );
220 #ifdef BDB_MULTIPLE_SUFFIXES
221 if( !be_issuffix( op->o_bd, &ptr ))
224 dnParent( &ptr, &pdn );
226 key.size = pdn.bv_len + 2;
228 pdn.bv_val[-1] = DN_ONE_PREFIX;
229 key.data = pdn.bv_val - 1;
232 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
235 Debug( LDAP_DEBUG_ANY,
236 "=> bdb_dn2id_delete 0x%lx: parent (%s) delete failed: %d\n",
237 e->e_id, ptr.bv_val, rc );
242 #ifndef BDB_MULTIPLE_SUFFIXES
243 while( !be_issuffix( op->o_bd, &ptr ))
248 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
250 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
252 Debug( LDAP_DEBUG_ANY,
253 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
254 e->e_id, ptr.bv_val, rc );
257 #ifdef BDB_MULTIPLE_SUFFIXES
258 if( be_issuffix( op->o_bd, &ptr )) break;
260 dnParent( &ptr, &pdn );
262 key.size = pdn.bv_len + 2;
264 key.data = pdn.bv_val - 1;
270 op->o_tmpfree( buf, op->o_tmpmemctx );
271 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
283 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
284 DB *db = bdb->bi_dn2id->bdi_db;
290 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id(\"%s\")\n", dn->bv_val, 0, 0 );
293 key.size = dn->bv_len + 2;
294 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
295 ((char *)key.data)[0] = DN_BASE_PREFIX;
296 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
301 data.ulen = sizeof(ID);
302 data.flags = DB_DBT_USERMEM;
304 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
305 if ( rc ) goto func_leave;
307 rc = bdb_dn2id_lock( bdb, dn, 0, txn, lock );
308 if ( rc ) goto nolock;
311 rc = cursor->c_get( cursor, &key, &data, DB_SET );
314 cursor->c_close( cursor );
318 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
319 db_strerror( rc ), rc, 0 );
321 BDB_DISK2ID( &nid, &ei->bei_id );
322 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%lx\n",
325 op->o_tmpfree( key.data, op->o_tmpmemctx );
336 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
337 DB *db = bdb->bi_dn2id->bdi_db;
341 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
342 e->e_nname.bv_val, 0, 0 );
344 key.size = e->e_nname.bv_len + 2;
345 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
346 ((char *)key.data)[0] = DN_ONE_PREFIX;
347 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
349 if ( bdb->bi_idl_cache_size ) {
350 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
351 if ( rc != LDAP_NO_SUCH_OBJECT ) {
352 op->o_tmpfree( key.data, op->o_tmpmemctx );
356 /* we actually could do a empty get... */
359 data.ulen = sizeof(id);
360 data.flags = DB_DBT_USERMEM;
362 data.dlen = sizeof(id);
364 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
365 op->o_tmpfree( key.data, op->o_tmpmemctx );
367 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
369 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
370 db_strerror(rc) ), rc );
386 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
387 DB *db = bdb->bi_dn2id->bdi_db;
388 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
389 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
391 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
394 #ifndef BDB_MULTIPLE_SUFFIXES
395 if ( prefix == DN_SUBTREE_PREFIX
396 && ( ei->bei_id == 0 || ei->bei_parent->bei_id == 0 )) {
397 BDB_IDL_ALL(bdb, ids);
403 key.size = ndn->bv_len + 2;
405 key.flags = DB_DBT_USERMEM;
406 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
407 ((char *)key.data)[0] = prefix;
408 AC_MEMCPY( &((char *)key.data)[1], ndn->bv_val, key.size - 1 );
411 rc = bdb_idl_fetch_key( op->o_bd, db, txn, &key, ids, NULL, 0 );
414 Debug( LDAP_DEBUG_TRACE,
415 "<= bdb_dn2idl: get failed: %s (%d)\n",
416 db_strerror( rc ), rc, 0 );
419 Debug( LDAP_DEBUG_TRACE,
420 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
422 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
425 op->o_tmpfree( key.data, op->o_tmpmemctx );
430 /* Management routines for a hierarchically structured database.
432 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
433 * entry in this database is a struct diskNode, keyed by entryID and with
434 * the data containing the RDN and entryID of the node's children. We use
435 * a B-Tree with sorted duplicates to store all the children of a node under
436 * the same key. Also, the first item under the key contains the entry's own
437 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
438 * well as top-down. To keep this info first in the list, the high bit of all
439 * subsequent nrdnlen's is always set. This means we can only accomodate
440 * RDNs up to length 32767, but that's fine since full DNs are already
441 * restricted to 8192.
443 * The diskNode is a variable length structure. This definition is not
444 * directly usable for in-memory manipulation.
446 typedef struct diskNode {
447 unsigned char nrdnlen[2];
449 char rdn[1]; /* variable placement */
450 unsigned char entryID[sizeof(ID)]; /* variable placement */
453 /* Sort function for the sorted duplicate data items of a dn2id key.
454 * Sorts based on normalized RDN, in length order.
466 un = (diskNode *)usrkey->data;
467 cn = (diskNode *)curkey->data;
469 /* data is not aligned, cannot compare directly */
470 rc = un->nrdnlen[0] - cn->nrdnlen[0];
472 rc = un->nrdnlen[1] - cn->nrdnlen[1];
475 return strcmp( un->nrdn, cn->nrdn );
478 /* This function constructs a full DN for a given entry.
485 int rlen = 0, nrlen = 0;
492 /* count length of all DN components */
493 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
494 rlen += ei->bei_rdn.bv_len + 1;
495 nrlen += ei->bei_nrdn.bv_len + 1;
496 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
499 /* See if the entry DN was invalidated by a subtree rename */
501 if ( BEI(e)->bei_modrdns >= max ) {
504 /* We found a mismatch, tell the caller to lock it */
505 if ( checkit == 1 ) {
508 /* checkit == 2. do the fix. */
509 free( e->e_name.bv_val );
510 free( e->e_nname.bv_val );
513 e->e_name.bv_len = rlen - 1;
514 e->e_nname.bv_len = nrlen - 1;
515 e->e_name.bv_val = ch_malloc(rlen);
516 e->e_nname.bv_val = ch_malloc(nrlen);
517 ptr = e->e_name.bv_val;
518 nptr = e->e_nname.bv_val;
519 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
520 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
521 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
522 if ( ei->bei_parent ) {
527 BEI(e)->bei_modrdns = max;
534 /* We add two elements to the DN2ID database - a data item under the parent's
535 * entryID containing the child's RDN and entryID, and an item under the
536 * child's entryID containing the parent's entryID.
545 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
546 DB *db = bdb->bi_dn2id->bdi_db;
553 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_add 0x%lx: \"%s\"\n",
554 e->e_id, e->e_ndn, 0 );
556 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
558 rlen = dn_rdnlen( op->o_bd, &e->e_name );
560 nrlen = e->e_nname.bv_len;
561 rlen = e->e_name.bv_len;
564 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
565 d->nrdnlen[1] = nrlen & 0xff;
566 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
567 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
569 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
571 BDB_ID2DISK( e->e_id, ptr );
575 key.size = sizeof(ID);
576 key.flags = DB_DBT_USERMEM;
577 BDB_ID2DISK( eip->bei_id, &nid );
581 /* Need to make dummy root node once. Subsequent attempts
582 * will fail harmlessly.
584 if ( eip->bei_id == 0 ) {
585 diskNode dummy = {{0, 0}, "", "", ""};
587 data.size = sizeof(diskNode);
588 data.flags = DB_DBT_USERMEM;
590 db->put( db, txn, &key, &data, DB_NODUPDATA );
594 data.size = sizeof(diskNode) + rlen + nrlen;
595 data.flags = DB_DBT_USERMEM;
597 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
600 BDB_ID2DISK( e->e_id, &nid );
601 BDB_ID2DISK( eip->bei_id, ptr );
602 d->nrdnlen[0] ^= 0x80;
604 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
607 /* Update all parents' IDL cache entries */
608 if ( rc == 0 && bdb->bi_idl_cache_size ) {
610 char *ptr = ((char *)&tmp[1])-1;
612 key.size = sizeof(ID)+1;
613 tmp[1] = eip->bei_id;
614 *ptr = DN_ONE_PREFIX;
615 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
616 *ptr = DN_SUBTREE_PREFIX;
617 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
618 tmp[1] = eip->bei_id;
619 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
624 op->o_tmpfree( d, op->o_tmpmemctx );
625 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
637 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
638 DB *db = bdb->bi_dn2id->bdi_db;
644 unsigned char dlen[2];
647 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n",
648 e->e_id, e->e_ndn, 0 );
651 key.size = sizeof(ID);
653 key.flags = DB_DBT_USERMEM;
654 BDB_ID2DISK( eip->bei_id, &nid );
657 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
658 data.ulen = data.size;
659 data.dlen = data.size;
660 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
664 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
665 d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
666 d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
667 dlen[0] = d->nrdnlen[0];
668 dlen[1] = d->nrdnlen[1];
669 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
672 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
673 if ( rc ) goto func_leave;
675 /* We hold this lock until the TXN completes */
676 rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, txn, &lock );
677 if ( rc ) goto nolock;
679 /* Delete our ID from the parent's list */
680 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
682 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
683 !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
684 rc = cursor->c_del( cursor, 0 );
689 /* Delete our ID from the tree. With sorted duplicates, this
690 * will leave any child nodes still hanging around. This is OK
691 * for modrdn, which will add our info back in later.
694 BDB_ID2DISK( e->e_id, &nid );
695 rc = cursor->c_get( cursor, &key, &data, DB_SET );
697 rc = cursor->c_del( cursor, 0 );
701 cursor->c_close( cursor );
703 op->o_tmpfree( d, op->o_tmpmemctx );
705 /* Delete IDL cache entries */
706 if ( rc == 0 && bdb->bi_idl_cache_size ) {
708 char *ptr = ((char *)&tmp[1])-1;
710 key.size = sizeof(ID)+1;
711 tmp[1] = eip->bei_id;
712 *ptr = DN_ONE_PREFIX;
713 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
714 *ptr = DN_SUBTREE_PREFIX;
715 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
716 tmp[1] = eip->bei_id;
717 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
720 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
733 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
734 DB *db = bdb->bi_dn2id->bdi_db;
740 unsigned char dlen[2];
743 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
745 nrlen = dn_rdnlen( op->o_bd, in );
746 if (!nrlen) nrlen = in->bv_len;
749 key.size = sizeof(ID);
751 key.ulen = sizeof(ID);
752 key.flags = DB_DBT_USERMEM;
753 parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
754 BDB_ID2DISK( parentID, &idp );
757 data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
758 data.ulen = data.size * 3;
759 data.dlen = data.ulen;
760 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
762 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
765 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
766 d->nrdnlen[1] = nrlen & 0xff;
767 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
768 dlen[0] = d->nrdnlen[0];
769 dlen[1] = d->nrdnlen[1];
770 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
774 rc = bdb_dn2id_lock( bdb, in, 0, txn, lock );
775 if ( rc ) goto func_leave;
777 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
778 if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
779 strncmp( d->nrdn, in->bv_val, nrlen ))) {
783 ptr = (char *) data.data + data.size - sizeof(ID);
784 BDB_DISK2ID( ptr, &ei->bei_id );
785 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
786 ptr = d->nrdn + nrlen + 1;
787 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
788 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
790 /* How many children does the parent have? */
791 /* FIXME: do we need to lock the parent
792 * entryinfo? Seems safe...
794 cursor->c_count( cursor, &dkids, 0 );
795 ei->bei_parent->bei_dkids = dkids;
800 cursor->c_close( cursor );
801 op->o_tmpfree( d, op->o_tmpmemctx );
803 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: get failed: %s (%d)\n",
804 db_strerror( rc ), rc, 0 );
806 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: got id=0x%lx\n",
820 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
821 DB *db = bdb->bi_dn2id->bdi_db;
830 key.size = sizeof(ID);
832 key.ulen = sizeof(ID);
833 key.flags = DB_DBT_USERMEM;
834 BDB_ID2DISK( ei->bei_id, &nid );
837 data.flags = DB_DBT_USERMEM;
839 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
842 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
843 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
846 rc = cursor->c_get( cursor, &key, &data, DB_SET );
848 if (d->nrdnlen[0] & 0x80) {
852 ptr = (char *) data.data + data.size - sizeof(ID);
853 BDB_DISK2ID( ptr, idp );
854 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
855 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
856 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
858 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
859 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
860 /* How many children does this node have? */
861 cursor->c_count( cursor, &dkids, 0 );
862 ei->bei_dkids = dkids;
865 cursor->c_close( cursor );
866 op->o_tmpfree( d, op->o_tmpmemctx );
876 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
877 DB *db = bdb->bi_dn2id->bdi_db;
885 key.size = sizeof(ID);
887 key.flags = DB_DBT_USERMEM;
888 BDB_ID2DISK( e->e_id, &id );
890 /* IDL cache is in host byte order */
891 if ( bdb->bi_idl_cache_size ) {
892 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
893 if ( rc != LDAP_NO_SUCH_OBJECT ) {
901 data.ulen = sizeof(d);
902 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
903 data.dlen = sizeof(d);
905 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
908 rc = cursor->c_get( cursor, &key, &data, DB_SET );
911 rc = cursor->c_count( cursor, &dkids, 0 );
913 BEI(e)->bei_dkids = dkids;
914 if ( dkids < 2 ) rc = DB_NOTFOUND;
917 cursor->c_close( cursor );
922 * We can't just use bdb_idl_fetch_key because
923 * 1 - our data items are longer than just an entry ID
924 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
926 * We descend the tree recursively, so we define this cookie
927 * to hold our necessary state information. The bdb_dn2idl_internal
928 * function uses this cookie when calling itself.
931 struct dn2id_cookie {
932 struct bdb_info *bdb;
957 EntryInfo *ei = data;
960 bdb_idl_append_one( idl, ei->bei_id );
966 struct dn2id_cookie *cx
969 BDB_IDL_ZERO( cx->tmp );
971 if ( cx->bdb->bi_idl_cache_size ) {
972 char *ptr = ((char *)&cx->id)-1;
975 cx->key.size = sizeof(ID)+1;
976 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
977 ID *ids = cx->depth ? cx->tmp : cx->ids;
979 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
980 if ( cx->rc == LDAP_SUCCESS ) {
982 bdb_idl_append( cx->ids, cx->tmp );
988 *ptr = DN_ONE_PREFIX;
989 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
990 if ( cx->rc == LDAP_SUCCESS ) {
993 if ( cx->rc == DB_NOTFOUND ) {
998 bdb_cache_entryinfo_lock( cx->ei );
1000 /* If number of kids in the cache differs from on-disk, load
1001 * up all the kids from the database
1003 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
1005 db_recno_t dkids = cx->ei->bei_dkids;
1006 ei.bei_parent = cx->ei;
1008 /* Only one thread should load the cache */
1009 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
1010 bdb_cache_entryinfo_unlock( cx->ei );
1011 ldap_pvt_thread_yield();
1012 bdb_cache_entryinfo_lock( cx->ei );
1013 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
1018 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
1020 bdb_cache_entryinfo_unlock( cx->ei );
1022 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
1023 cx->bdb->bi_db_opflags );
1027 cx->data.data = &cx->dbuf;
1028 cx->data.ulen = sizeof(ID);
1029 cx->data.dlen = sizeof(ID);
1030 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
1032 /* The first item holds the parent ID. Ignore it. */
1033 cx->key.data = &cx->nid;
1034 cx->key.size = sizeof(ID);
1035 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
1037 cx->dbc->c_close( cx->dbc );
1041 /* If the on-disk count is zero we've never checked it.
1045 cx->dbc->c_count( cx->dbc, &dkids, 0 );
1046 cx->ei->bei_dkids = dkids;
1049 cx->data.data = cx->buf;
1050 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1051 cx->data.flags = DB_DBT_USERMEM;
1054 /* Fetch the rest of the IDs in a loop... */
1055 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1056 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1060 DB_MULTIPLE_INIT( ptr, &cx->data );
1062 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
1065 diskNode *d = (diskNode *)j;
1068 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
1069 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
1070 ei.bei_nrdn.bv_len = nrlen;
1071 /* nrdn/rdn are set in-place.
1072 * hdb_cache_load will copy them as needed
1074 ei.bei_nrdn.bv_val = d->nrdn;
1075 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1076 - ei.bei_nrdn.bv_len;
1077 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1078 bdb_idl_append_one( cx->tmp, ei.bei_id );
1079 hdb_cache_load( cx->bdb, &ei, &ei2 );
1085 cx->rc = cx->dbc->c_close( cx->dbc );
1087 bdb_cache_entryinfo_lock( cx->ei );
1088 cx->ei->bei_state ^= CACHE_ENTRY_ONELEVEL;
1089 bdb_cache_entryinfo_unlock( cx->ei );
1094 /* The in-memory cache is in sync with the on-disk data.
1095 * do we have any kids?
1099 if ( cx->ei->bei_ckids > 0 ) {
1100 /* Walk the kids tree; order is irrelevant since bdb_idl_sort
1101 * will sort it later.
1103 avl_apply( cx->ei->bei_kids, apply_func,
1104 cx->tmp, -1, AVL_POSTORDER );
1106 bdb_cache_entryinfo_unlock( cx->ei );
1109 if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1110 bdb_idl_sort( cx->tmp, cx->buf );
1111 if ( cx->bdb->bi_idl_cache_max_size && !BDB_IDL_IS_ZERO( cx->tmp )) {
1112 char *ptr = ((char *)&cx->id)-1;
1114 cx->key.size = sizeof(ID)+1;
1115 *ptr = DN_ONE_PREFIX;
1116 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1120 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1121 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1122 bdb_idl_append( cx->ids, cx->tmp );
1124 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1126 EntryInfo *ei = cx->ei;
1128 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1129 cx->op->o_tmpmemctx );
1130 BDB_IDL_CPY( save, cx->tmp );
1134 for ( cx->id = bdb_idl_first( save, &idcurs );
1136 cx->id = bdb_idl_next( save, &idcurs )) {
1137 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
1139 ( cx->ei->bei_state & CACHE_ENTRY_NO_KIDS ))
1142 BDB_ID2DISK( cx->id, &cx->nid );
1143 hdb_dn2idl_internal( cx );
1144 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1148 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1150 bdb_cache_entryinfo_lock( ei );
1151 ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1152 bdb_cache_entryinfo_unlock( ei );
1155 /* Make sure caller knows it had kids! */
1160 BDB_IDL_CPY( cx->ids, cx->tmp );
1175 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1176 struct dn2id_cookie cx;
1178 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1179 ndn->bv_val, 0, 0 );
1181 #ifndef BDB_MULTIPLE_SUFFIXES
1182 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1183 ( ei->bei_id == 0 ||
1184 ei->bei_parent->bei_id == 0 ))
1186 BDB_IDL_ALL( bdb, ids );
1192 BDB_ID2DISK( cx.id, &cx.nid );
1195 cx.db = cx.bdb->bi_dn2id->bdi_db;
1196 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1197 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1200 cx.buf = stack + BDB_IDL_UM_SIZE;
1206 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1210 BDB_IDL_ZERO( ids );
1212 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1213 return LDAP_SUCCESS;
1216 cx.key.ulen = sizeof(ID);
1217 cx.key.size = sizeof(ID);
1218 cx.key.flags = DB_DBT_USERMEM;
1222 hdb_dn2idl_internal(&cx);
1223 if ( cx.need_sort ) {
1224 char *ptr = ((char *)&cx.id)-1;
1225 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1226 bdb_idl_sort( cx.ids, cx.tmp );
1228 cx.key.size = sizeof(ID)+1;
1231 if ( cx.bdb->bi_idl_cache_max_size )
1232 bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1235 if ( cx.rc == DB_NOTFOUND )
1236 cx.rc = LDAP_SUCCESS;
1240 #endif /* BDB_HIER */