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-2009 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>
27 #define bdb_dn2id_lock BDB_SYMBOL(dn2id_lock)
30 bdb_dn2id_lock( struct bdb_info *bdb, struct berval *dn,
31 int rw, DB_TXN *txn, DB_LOCK *lock )
41 db_rw = DB_LOCK_WRITE;
45 lockobj.data = dn->bv_val;
46 lockobj.size = dn->bv_len;
48 rc = LOCK_GET(bdb->bi_dbenv, TXN_ID(txn), DB_LOCK_NOWAIT,
49 &lockobj, db_rw, lock);
53 #define bdb_dn2id_lock(a,b,c,d,e) 0
64 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
65 DB *db = bdb->bi_dn2id->bdi_db;
70 struct berval ptr, pdn;
72 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add 0x%lx: \"%s\"\n",
73 e->e_id, e->e_ndn, 0 );
74 assert( e->e_id != NOID );
77 key.size = e->e_nname.bv_len + 2;
79 key.flags = DB_DBT_USERMEM;
80 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
82 buf[0] = DN_BASE_PREFIX;
84 ptr.bv_len = e->e_nname.bv_len;
85 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
86 ptr.bv_val[ptr.bv_len] = '\0';
90 data.size = sizeof( nid );
91 BDB_ID2DISK( e->e_id, &nid );
93 /* store it -- don't override */
94 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
96 char buf[ SLAP_TEXT_BUFLEN ];
97 snprintf( buf, sizeof( buf ), "%s => bdb_dn2id_add dn=\"%s\" ID=0x%lx",
98 op->o_log_prefix, e->e_name.bv_val, e->e_id );
99 Debug( LDAP_DEBUG_ANY, "%s: put failed: %s %d\n",
100 buf, db_strerror(rc), rc );
104 #ifndef BDB_MULTIPLE_SUFFIXES
105 if( !be_issuffix( op->o_bd, &ptr ))
108 buf[0] = DN_SUBTREE_PREFIX;
109 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
111 Debug( LDAP_DEBUG_ANY,
112 "=> bdb_dn2id_add 0x%lx: subtree (%s) put failed: %d\n",
113 e->e_id, ptr.bv_val, rc );
117 #ifdef BDB_MULTIPLE_SUFFIXES
118 if( !be_issuffix( op->o_bd, &ptr ))
121 dnParent( &ptr, &pdn );
123 key.size = pdn.bv_len + 2;
125 pdn.bv_val[-1] = DN_ONE_PREFIX;
126 key.data = pdn.bv_val-1;
129 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
132 Debug( LDAP_DEBUG_ANY,
133 "=> bdb_dn2id_add 0x%lx: parent (%s) insert failed: %d\n",
134 e->e_id, ptr.bv_val, rc );
139 #ifndef BDB_MULTIPLE_SUFFIXES
140 while( !be_issuffix( op->o_bd, &ptr ))
145 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
147 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
150 Debug( LDAP_DEBUG_ANY,
151 "=> bdb_dn2id_add 0x%lx: subtree (%s) insert failed: %d\n",
152 e->e_id, ptr.bv_val, rc );
155 #ifdef BDB_MULTIPLE_SUFFIXES
156 if( be_issuffix( op->o_bd, &ptr )) break;
158 dnParent( &ptr, &pdn );
160 key.size = pdn.bv_len + 2;
162 key.data = pdn.bv_val - 1;
168 op->o_tmpfree( buf, op->o_tmpmemctx );
169 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
180 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
181 DB *db = bdb->bi_dn2id->bdi_db;
185 struct berval pdn, ptr;
188 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete 0x%lx: \"%s\"\n",
189 e->e_id, e->e_ndn, 0 );
192 key.size = e->e_nname.bv_len + 2;
193 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
195 key.flags = DB_DBT_USERMEM;
196 buf[0] = DN_BASE_PREFIX;
198 ptr.bv_len = e->e_nname.bv_len;
199 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
200 ptr.bv_val[ptr.bv_len] = '\0';
202 /* We hold this lock until the TXN completes */
203 rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, txn, &lock );
207 rc = db->del( db, txn, &key, 0 );
209 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete 0x%lx: delete failed: %s %d\n",
210 e->e_id, db_strerror(rc), rc );
214 #ifndef BDB_MULTIPLE_SUFFIXES
215 if( !be_issuffix( op->o_bd, &ptr ))
218 buf[0] = DN_SUBTREE_PREFIX;
219 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
221 Debug( LDAP_DEBUG_ANY,
222 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
223 e->e_id, ptr.bv_val, rc );
227 #ifdef BDB_MULTIPLE_SUFFIXES
228 if( !be_issuffix( op->o_bd, &ptr ))
231 dnParent( &ptr, &pdn );
233 key.size = pdn.bv_len + 2;
235 pdn.bv_val[-1] = DN_ONE_PREFIX;
236 key.data = pdn.bv_val - 1;
239 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
242 Debug( LDAP_DEBUG_ANY,
243 "=> bdb_dn2id_delete 0x%lx: parent (%s) delete failed: %d\n",
244 e->e_id, ptr.bv_val, rc );
249 #ifndef BDB_MULTIPLE_SUFFIXES
250 while( !be_issuffix( op->o_bd, &ptr ))
255 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
257 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
259 Debug( LDAP_DEBUG_ANY,
260 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
261 e->e_id, ptr.bv_val, rc );
264 #ifdef BDB_MULTIPLE_SUFFIXES
265 if( be_issuffix( op->o_bd, &ptr )) break;
267 dnParent( &ptr, &pdn );
269 key.size = pdn.bv_len + 2;
271 key.data = pdn.bv_val - 1;
277 op->o_tmpfree( buf, op->o_tmpmemctx );
278 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
290 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
291 DB *db = bdb->bi_dn2id->bdi_db;
297 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id(\"%s\")\n", dn->bv_val, 0, 0 );
300 key.size = dn->bv_len + 2;
301 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
302 ((char *)key.data)[0] = DN_BASE_PREFIX;
303 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
308 data.ulen = sizeof(ID);
309 data.flags = DB_DBT_USERMEM;
311 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
312 if ( rc ) goto func_leave;
314 rc = bdb_dn2id_lock( bdb, dn, 0, txn, lock );
315 if ( rc ) goto nolock;
318 rc = cursor->c_get( cursor, &key, &data, DB_SET );
321 cursor->c_close( cursor );
325 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
326 db_strerror( rc ), rc, 0 );
328 BDB_DISK2ID( &nid, &ei->bei_id );
329 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%lx\n",
332 op->o_tmpfree( key.data, op->o_tmpmemctx );
343 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
344 DB *db = bdb->bi_dn2id->bdi_db;
348 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
349 e->e_nname.bv_val, 0, 0 );
351 key.size = e->e_nname.bv_len + 2;
352 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
353 ((char *)key.data)[0] = DN_ONE_PREFIX;
354 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
356 if ( bdb->bi_idl_cache_size ) {
357 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
358 if ( rc != LDAP_NO_SUCH_OBJECT ) {
359 op->o_tmpfree( key.data, op->o_tmpmemctx );
363 /* we actually could do a empty get... */
366 data.ulen = sizeof(id);
367 data.flags = DB_DBT_USERMEM;
369 data.dlen = sizeof(id);
371 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
372 op->o_tmpfree( key.data, op->o_tmpmemctx );
374 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
376 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
377 db_strerror(rc) ), rc );
393 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
394 DB *db = bdb->bi_dn2id->bdi_db;
395 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
396 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
398 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
401 #ifndef BDB_MULTIPLE_SUFFIXES
402 if ( prefix == DN_SUBTREE_PREFIX
403 && ( ei->bei_id == 0 ||
404 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len ))) {
405 BDB_IDL_ALL(bdb, ids);
411 key.size = ndn->bv_len + 2;
413 key.flags = DB_DBT_USERMEM;
414 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
415 ((char *)key.data)[0] = prefix;
416 AC_MEMCPY( &((char *)key.data)[1], ndn->bv_val, key.size - 1 );
419 rc = bdb_idl_fetch_key( op->o_bd, db, txn, &key, ids, NULL, 0 );
422 Debug( LDAP_DEBUG_TRACE,
423 "<= bdb_dn2idl: get failed: %s (%d)\n",
424 db_strerror( rc ), rc, 0 );
427 Debug( LDAP_DEBUG_TRACE,
428 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
430 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
433 op->o_tmpfree( key.data, op->o_tmpmemctx );
438 /* Management routines for a hierarchically structured database.
440 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
441 * entry in this database is a struct diskNode, keyed by entryID and with
442 * the data containing the RDN and entryID of the node's children. We use
443 * a B-Tree with sorted duplicates to store all the children of a node under
444 * the same key. Also, the first item under the key contains the entry's own
445 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
446 * well as top-down. To keep this info first in the list, the high bit of all
447 * subsequent nrdnlen's is always set. This means we can only accomodate
448 * RDNs up to length 32767, but that's fine since full DNs are already
449 * restricted to 8192.
451 * The diskNode is a variable length structure. This definition is not
452 * directly usable for in-memory manipulation.
454 typedef struct diskNode {
455 unsigned char nrdnlen[2];
457 char rdn[1]; /* variable placement */
458 unsigned char entryID[sizeof(ID)]; /* variable placement */
461 /* Sort function for the sorted duplicate data items of a dn2id key.
462 * Sorts based on normalized RDN, in length order.
474 un = (diskNode *)usrkey->data;
475 cn = (diskNode *)curkey->data;
477 /* data is not aligned, cannot compare directly */
478 rc = un->nrdnlen[0] - cn->nrdnlen[0];
480 rc = un->nrdnlen[1] - cn->nrdnlen[1];
483 return strcmp( un->nrdn, cn->nrdn );
486 /* This function constructs a full DN for a given entry.
493 int rlen = 0, nrlen = 0;
500 /* count length of all DN components */
501 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
502 rlen += ei->bei_rdn.bv_len + 1;
503 nrlen += ei->bei_nrdn.bv_len + 1;
504 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
507 /* See if the entry DN was invalidated by a subtree rename */
509 if ( BEI(e)->bei_modrdns >= max ) {
512 /* We found a mismatch, tell the caller to lock it */
513 if ( checkit == 1 ) {
516 /* checkit == 2. do the fix. */
517 free( e->e_name.bv_val );
518 free( e->e_nname.bv_val );
521 e->e_name.bv_len = rlen - 1;
522 e->e_nname.bv_len = nrlen - 1;
523 e->e_name.bv_val = ch_malloc(rlen);
524 e->e_nname.bv_val = ch_malloc(nrlen);
525 ptr = e->e_name.bv_val;
526 nptr = e->e_nname.bv_val;
527 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
528 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
529 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
530 if ( ei->bei_parent ) {
535 BEI(e)->bei_modrdns = max;
536 if ( ptr > e->e_name.bv_val ) ptr[-1] = '\0';
537 if ( nptr > e->e_nname.bv_val ) nptr[-1] = '\0';
542 /* We add two elements to the DN2ID database - a data item under the parent's
543 * entryID containing the child's RDN and entryID, and an item under the
544 * child's entryID containing the parent's entryID.
553 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
554 DB *db = bdb->bi_dn2id->bdi_db;
561 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_add 0x%lx: \"%s\"\n",
562 e->e_id, e->e_ndn, 0 );
564 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
566 rlen = dn_rdnlen( op->o_bd, &e->e_name );
568 nrlen = e->e_nname.bv_len;
569 rlen = e->e_name.bv_len;
572 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
573 d->nrdnlen[1] = nrlen & 0xff;
574 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
575 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
577 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
579 BDB_ID2DISK( e->e_id, ptr );
583 key.size = sizeof(ID);
584 key.flags = DB_DBT_USERMEM;
585 BDB_ID2DISK( eip->bei_id, &nid );
589 /* Need to make dummy root node once. Subsequent attempts
590 * will fail harmlessly.
592 if ( eip->bei_id == 0 ) {
593 diskNode dummy = {{0, 0}, "", "", ""};
595 data.size = sizeof(diskNode);
596 data.flags = DB_DBT_USERMEM;
598 db->put( db, txn, &key, &data, DB_NODUPDATA );
602 data.size = sizeof(diskNode) + rlen + nrlen;
603 data.flags = DB_DBT_USERMEM;
605 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
608 BDB_ID2DISK( e->e_id, &nid );
609 BDB_ID2DISK( eip->bei_id, ptr );
610 d->nrdnlen[0] ^= 0x80;
612 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
615 /* Update all parents' IDL cache entries */
616 if ( rc == 0 && bdb->bi_idl_cache_size ) {
618 char *ptr = ((char *)&tmp[1])-1;
620 key.size = sizeof(ID)+1;
621 tmp[1] = eip->bei_id;
622 *ptr = DN_ONE_PREFIX;
623 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
624 if ( eip->bei_parent ) {
625 *ptr = DN_SUBTREE_PREFIX;
626 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
627 tmp[1] = eip->bei_id;
628 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
630 /* Handle DB with empty suffix */
631 if ( !op->o_bd->be_suffix[0].bv_len && eip ) {
632 tmp[1] = eip->bei_id;
633 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
638 op->o_tmpfree( d, op->o_tmpmemctx );
639 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
651 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
652 DB *db = bdb->bi_dn2id->bdi_db;
658 unsigned char dlen[2];
661 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n",
662 e->e_id, e->e_ndn, 0 );
665 key.size = sizeof(ID);
667 key.flags = DB_DBT_USERMEM;
668 BDB_ID2DISK( eip->bei_id, &nid );
671 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
672 data.ulen = data.size;
673 data.dlen = data.size;
674 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
678 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
679 d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
680 d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
681 dlen[0] = d->nrdnlen[0];
682 dlen[1] = d->nrdnlen[1];
683 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
686 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
687 if ( rc ) goto func_leave;
689 /* We hold this lock until the TXN completes */
690 rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, txn, &lock );
691 if ( rc ) goto nolock;
693 /* Delete our ID from the parent's list */
694 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
696 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
697 !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
698 rc = cursor->c_del( cursor, 0 );
703 /* Delete our ID from the tree. With sorted duplicates, this
704 * will leave any child nodes still hanging around. This is OK
705 * for modrdn, which will add our info back in later.
708 BDB_ID2DISK( e->e_id, &nid );
709 rc = cursor->c_get( cursor, &key, &data, DB_SET );
711 rc = cursor->c_del( cursor, 0 );
715 cursor->c_close( cursor );
717 op->o_tmpfree( d, op->o_tmpmemctx );
719 /* Delete IDL cache entries */
720 if ( rc == 0 && bdb->bi_idl_cache_size ) {
722 char *ptr = ((char *)&tmp[1])-1;
724 key.size = sizeof(ID)+1;
725 tmp[1] = eip->bei_id;
726 *ptr = DN_ONE_PREFIX;
727 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
728 if ( eip ->bei_parent ) {
729 *ptr = DN_SUBTREE_PREFIX;
730 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
731 tmp[1] = eip->bei_id;
732 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
734 /* Handle DB with empty suffix */
735 if ( !op->o_bd->be_suffix[0].bv_len && eip ) {
736 tmp[1] = eip->bei_id;
737 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
741 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
754 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
755 DB *db = bdb->bi_dn2id->bdi_db;
761 unsigned char dlen[2];
764 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
766 nrlen = dn_rdnlen( op->o_bd, in );
767 if (!nrlen) nrlen = in->bv_len;
770 key.size = sizeof(ID);
772 key.ulen = sizeof(ID);
773 key.flags = DB_DBT_USERMEM;
774 parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
775 BDB_ID2DISK( parentID, &idp );
778 data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
779 data.ulen = data.size * 3;
780 data.dlen = data.ulen;
781 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
783 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
786 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
787 d->nrdnlen[1] = nrlen & 0xff;
788 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
789 dlen[0] = d->nrdnlen[0];
790 dlen[1] = d->nrdnlen[1];
791 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
795 rc = bdb_dn2id_lock( bdb, in, 0, txn, lock );
796 if ( rc ) goto func_leave;
798 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
799 if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
800 strncmp( d->nrdn, in->bv_val, nrlen ))) {
804 ptr = (char *) data.data + data.size - sizeof(ID);
805 BDB_DISK2ID( ptr, &ei->bei_id );
806 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
807 ptr = d->nrdn + nrlen + 1;
808 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
809 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
811 /* How many children does the parent have? */
812 /* FIXME: do we need to lock the parent
813 * entryinfo? Seems safe...
815 cursor->c_count( cursor, &dkids, 0 );
816 ei->bei_parent->bei_dkids = dkids;
821 cursor->c_close( cursor );
822 op->o_tmpfree( d, op->o_tmpmemctx );
824 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: get failed: %s (%d)\n",
825 db_strerror( rc ), rc, 0 );
827 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: got id=0x%lx\n",
841 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
842 DB *db = bdb->bi_dn2id->bdi_db;
851 key.size = sizeof(ID);
853 key.ulen = sizeof(ID);
854 key.flags = DB_DBT_USERMEM;
855 BDB_ID2DISK( ei->bei_id, &nid );
858 data.flags = DB_DBT_USERMEM;
860 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
863 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
864 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
867 rc = cursor->c_get( cursor, &key, &data, DB_SET );
869 if (d->nrdnlen[0] & 0x80) {
873 ptr = (char *) data.data + data.size - sizeof(ID);
874 BDB_DISK2ID( ptr, idp );
875 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
876 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
877 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
879 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
880 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
881 /* How many children does this node have? */
882 cursor->c_count( cursor, &dkids, 0 );
883 ei->bei_dkids = dkids;
886 cursor->c_close( cursor );
887 op->o_tmpfree( d, op->o_tmpmemctx );
897 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
898 DB *db = bdb->bi_dn2id->bdi_db;
906 key.size = sizeof(ID);
908 key.flags = DB_DBT_USERMEM;
909 BDB_ID2DISK( e->e_id, &id );
911 /* IDL cache is in host byte order */
912 if ( bdb->bi_idl_cache_size ) {
913 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
914 if ( rc != LDAP_NO_SUCH_OBJECT ) {
922 data.ulen = sizeof(d);
923 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
924 data.dlen = sizeof(d);
926 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
929 rc = cursor->c_get( cursor, &key, &data, DB_SET );
932 rc = cursor->c_count( cursor, &dkids, 0 );
934 BEI(e)->bei_dkids = dkids;
935 if ( dkids < 2 ) rc = DB_NOTFOUND;
938 cursor->c_close( cursor );
943 * We can't just use bdb_idl_fetch_key because
944 * 1 - our data items are longer than just an entry ID
945 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
947 * We descend the tree recursively, so we define this cookie
948 * to hold our necessary state information. The bdb_dn2idl_internal
949 * function uses this cookie when calling itself.
952 struct dn2id_cookie {
953 struct bdb_info *bdb;
978 EntryInfo *ei = data;
981 bdb_idl_append_one( idl, ei->bei_id );
987 struct dn2id_cookie *cx
990 BDB_IDL_ZERO( cx->tmp );
992 if ( cx->bdb->bi_idl_cache_size ) {
993 char *ptr = ((char *)&cx->id)-1;
996 cx->key.size = sizeof(ID)+1;
997 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
998 ID *ids = cx->depth ? cx->tmp : cx->ids;
1000 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
1001 if ( cx->rc == LDAP_SUCCESS ) {
1003 bdb_idl_append( cx->ids, cx->tmp );
1009 *ptr = DN_ONE_PREFIX;
1010 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
1011 if ( cx->rc == LDAP_SUCCESS ) {
1014 if ( cx->rc == DB_NOTFOUND ) {
1019 bdb_cache_entryinfo_lock( cx->ei );
1021 /* If number of kids in the cache differs from on-disk, load
1022 * up all the kids from the database
1024 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
1026 db_recno_t dkids = cx->ei->bei_dkids;
1027 ei.bei_parent = cx->ei;
1029 /* Only one thread should load the cache */
1030 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
1031 bdb_cache_entryinfo_unlock( cx->ei );
1032 ldap_pvt_thread_yield();
1033 bdb_cache_entryinfo_lock( cx->ei );
1034 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
1039 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
1041 bdb_cache_entryinfo_unlock( cx->ei );
1043 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
1044 cx->bdb->bi_db_opflags );
1048 cx->data.data = &cx->dbuf;
1049 cx->data.ulen = sizeof(ID);
1050 cx->data.dlen = sizeof(ID);
1051 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
1053 /* The first item holds the parent ID. Ignore it. */
1054 cx->key.data = &cx->nid;
1055 cx->key.size = sizeof(ID);
1056 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
1058 cx->dbc->c_close( cx->dbc );
1062 /* If the on-disk count is zero we've never checked it.
1066 cx->dbc->c_count( cx->dbc, &dkids, 0 );
1067 cx->ei->bei_dkids = dkids;
1070 cx->data.data = cx->buf;
1071 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1072 cx->data.flags = DB_DBT_USERMEM;
1075 /* Fetch the rest of the IDs in a loop... */
1076 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1077 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1081 DB_MULTIPLE_INIT( ptr, &cx->data );
1083 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
1086 diskNode *d = (diskNode *)j;
1089 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
1090 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
1091 ei.bei_nrdn.bv_len = nrlen;
1092 /* nrdn/rdn are set in-place.
1093 * hdb_cache_load will copy them as needed
1095 ei.bei_nrdn.bv_val = d->nrdn;
1096 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1097 - ei.bei_nrdn.bv_len;
1098 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1099 bdb_idl_append_one( cx->tmp, ei.bei_id );
1100 hdb_cache_load( cx->bdb, &ei, &ei2 );
1106 cx->rc = cx->dbc->c_close( cx->dbc );
1108 bdb_cache_entryinfo_lock( cx->ei );
1109 cx->ei->bei_state &= ~CACHE_ENTRY_ONELEVEL;
1110 bdb_cache_entryinfo_unlock( cx->ei );
1115 /* The in-memory cache is in sync with the on-disk data.
1116 * do we have any kids?
1120 if ( cx->ei->bei_ckids > 0 ) {
1121 /* Walk the kids tree; order is irrelevant since bdb_idl_sort
1122 * will sort it later.
1124 avl_apply( cx->ei->bei_kids, apply_func,
1125 cx->tmp, -1, AVL_POSTORDER );
1127 bdb_cache_entryinfo_unlock( cx->ei );
1130 if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1131 bdb_idl_sort( cx->tmp, cx->buf );
1132 if ( cx->bdb->bi_idl_cache_max_size && !BDB_IDL_IS_ZERO( cx->tmp )) {
1133 char *ptr = ((char *)&cx->id)-1;
1135 cx->key.size = sizeof(ID)+1;
1136 *ptr = DN_ONE_PREFIX;
1137 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1141 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1142 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1143 bdb_idl_append( cx->ids, cx->tmp );
1145 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1147 EntryInfo *ei = cx->ei;
1149 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1150 cx->op->o_tmpmemctx );
1151 BDB_IDL_CPY( save, cx->tmp );
1155 for ( cx->id = bdb_idl_first( save, &idcurs );
1157 cx->id = bdb_idl_next( save, &idcurs )) {
1160 if ( bdb_cache_find_id( cx->op, cx->txn, cx->id, &cx->ei,
1165 if ( !( ei2->bei_state & CACHE_ENTRY_NO_KIDS )) {
1166 BDB_ID2DISK( cx->id, &cx->nid );
1167 hdb_dn2idl_internal( cx );
1168 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1171 bdb_cache_entryinfo_lock( ei2 );
1173 bdb_cache_entryinfo_unlock( ei2 );
1177 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1179 bdb_cache_entryinfo_lock( ei );
1180 ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1181 bdb_cache_entryinfo_unlock( ei );
1184 /* Make sure caller knows it had kids! */
1189 BDB_IDL_CPY( cx->ids, cx->tmp );
1204 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1205 struct dn2id_cookie cx;
1207 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1208 ndn->bv_val, 0, 0 );
1210 #ifndef BDB_MULTIPLE_SUFFIXES
1211 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1212 ( ei->bei_id == 0 ||
1213 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len )))
1215 BDB_IDL_ALL( bdb, ids );
1221 BDB_ID2DISK( cx.id, &cx.nid );
1224 cx.db = cx.bdb->bi_dn2id->bdi_db;
1225 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1226 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1229 cx.buf = stack + BDB_IDL_UM_SIZE;
1235 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1239 BDB_IDL_ZERO( ids );
1241 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1242 return LDAP_SUCCESS;
1245 cx.key.ulen = sizeof(ID);
1246 cx.key.size = sizeof(ID);
1247 cx.key.flags = DB_DBT_USERMEM;
1251 hdb_dn2idl_internal(&cx);
1252 if ( cx.need_sort ) {
1253 char *ptr = ((char *)&cx.id)-1;
1254 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1255 bdb_idl_sort( cx.ids, cx.tmp );
1257 cx.key.size = sizeof(ID)+1;
1260 if ( cx.bdb->bi_idl_cache_max_size )
1261 bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1264 if ( cx.rc == DB_NOTFOUND )
1265 cx.rc = LDAP_SUCCESS;
1269 #endif /* BDB_HIER */