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-2007 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, BDB_LOCKER locker, DB_LOCK *lock )
37 db_rw = DB_LOCK_WRITE;
41 lockobj.data = dn->bv_val;
42 lockobj.size = dn->bv_len;
44 rc = LOCK_GET(bdb->bi_dbenv, BDB_LOCKID(locker), DB_LOCK_NOWAIT,
45 &lockobj, db_rw, lock);
57 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
58 DB *db = bdb->bi_dn2id->bdi_db;
63 struct berval ptr, pdn;
65 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add 0x%lx: \"%s\"\n",
66 e->e_id, e->e_ndn, 0 );
67 assert( e->e_id != NOID );
70 key.size = e->e_nname.bv_len + 2;
72 key.flags = DB_DBT_USERMEM;
73 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
75 buf[0] = DN_BASE_PREFIX;
77 ptr.bv_len = e->e_nname.bv_len;
78 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
79 ptr.bv_val[ptr.bv_len] = '\0';
83 data.size = sizeof( nid );
84 BDB_ID2DISK( e->e_id, &nid );
86 /* store it -- don't override */
87 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
89 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add 0x%lx: put failed: %s %d\n",
90 e->e_id, db_strerror(rc), rc );
94 #ifndef BDB_MULTIPLE_SUFFIXES
95 if( !be_issuffix( op->o_bd, &ptr ))
98 buf[0] = DN_SUBTREE_PREFIX;
99 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
101 Debug( LDAP_DEBUG_ANY,
102 "=> bdb_dn2id_add 0x%lx: subtree (%s) put failed: %d\n",
103 e->e_id, ptr.bv_val, rc );
107 #ifdef BDB_MULTIPLE_SUFFIXES
108 if( !be_issuffix( op->o_bd, &ptr ))
111 dnParent( &ptr, &pdn );
113 key.size = pdn.bv_len + 2;
115 pdn.bv_val[-1] = DN_ONE_PREFIX;
116 key.data = pdn.bv_val-1;
119 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
122 Debug( LDAP_DEBUG_ANY,
123 "=> bdb_dn2id_add 0x%lx: parent (%s) insert failed: %d\n",
124 e->e_id, ptr.bv_val, rc );
129 #ifndef BDB_MULTIPLE_SUFFIXES
130 while( !be_issuffix( op->o_bd, &ptr ))
135 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
137 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
140 Debug( LDAP_DEBUG_ANY,
141 "=> bdb_dn2id_add 0x%lx: subtree (%s) insert failed: %d\n",
142 e->e_id, ptr.bv_val, rc );
145 #ifdef BDB_MULTIPLE_SUFFIXES
146 if( be_issuffix( op->o_bd, &ptr )) break;
148 dnParent( &ptr, &pdn );
150 key.size = pdn.bv_len + 2;
152 key.data = pdn.bv_val - 1;
158 op->o_tmpfree( buf, op->o_tmpmemctx );
159 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
170 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
171 DB *db = bdb->bi_dn2id->bdi_db;
175 struct berval pdn, ptr;
178 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete 0x%lx: \"%s\"\n",
179 e->e_id, e->e_ndn, 0 );
182 key.size = e->e_nname.bv_len + 2;
183 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
185 key.flags = DB_DBT_USERMEM;
186 buf[0] = DN_BASE_PREFIX;
188 ptr.bv_len = e->e_nname.bv_len;
189 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
190 ptr.bv_val[ptr.bv_len] = '\0';
192 /* We hold this lock until the TXN completes */
193 rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, TXN_ID( txn ), &lock );
197 rc = db->del( db, txn, &key, 0 );
199 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete 0x%lx: delete failed: %s %d\n",
200 e->e_id, db_strerror(rc), rc );
204 #ifndef BDB_MULTIPLE_SUFFIXES
205 if( !be_issuffix( op->o_bd, &ptr ))
208 buf[0] = DN_SUBTREE_PREFIX;
209 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
211 Debug( LDAP_DEBUG_ANY,
212 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
213 e->e_id, ptr.bv_val, rc );
217 #ifdef BDB_MULTIPLE_SUFFIXES
218 if( !be_issuffix( op->o_bd, &ptr ))
221 dnParent( &ptr, &pdn );
223 key.size = pdn.bv_len + 2;
225 pdn.bv_val[-1] = DN_ONE_PREFIX;
226 key.data = pdn.bv_val - 1;
229 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
232 Debug( LDAP_DEBUG_ANY,
233 "=> bdb_dn2id_delete 0x%lx: parent (%s) delete failed: %d\n",
234 e->e_id, ptr.bv_val, rc );
239 #ifndef BDB_MULTIPLE_SUFFIXES
240 while( !be_issuffix( op->o_bd, &ptr ))
245 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
247 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
249 Debug( LDAP_DEBUG_ANY,
250 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
251 e->e_id, ptr.bv_val, rc );
254 #ifdef BDB_MULTIPLE_SUFFIXES
255 if( be_issuffix( op->o_bd, &ptr )) break;
257 dnParent( &ptr, &pdn );
259 key.size = pdn.bv_len + 2;
261 key.data = pdn.bv_val - 1;
267 op->o_tmpfree( buf, op->o_tmpmemctx );
268 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
280 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
281 DB *db = bdb->bi_dn2id->bdi_db;
287 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id(\"%s\")\n", dn->bv_val, 0, 0 );
290 key.size = dn->bv_len + 2;
291 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
292 ((char *)key.data)[0] = DN_BASE_PREFIX;
293 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
298 data.ulen = sizeof(ID);
299 data.flags = DB_DBT_USERMEM;
301 rc = db->cursor( db, NULL, &cursor, bdb->bi_db_opflags );
302 if ( rc ) goto leave;
304 rc = bdb_dn2id_lock( bdb, dn, 0, locker, lock );
305 if ( rc ) goto nolock;
308 CURSOR_SETLOCKER(cursor, locker);
312 rc = cursor->c_get( cursor, &key, &data, DB_SET );
315 cursor->c_close( cursor );
319 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
320 db_strerror( rc ), rc, 0 );
322 BDB_DISK2ID( &nid, &ei->bei_id );
323 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%lx\n",
326 op->o_tmpfree( key.data, op->o_tmpmemctx );
337 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
338 DB *db = bdb->bi_dn2id->bdi_db;
342 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
343 e->e_nname.bv_val, 0, 0 );
345 key.size = e->e_nname.bv_len + 2;
346 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
347 ((char *)key.data)[0] = DN_ONE_PREFIX;
348 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
350 if ( bdb->bi_idl_cache_size ) {
351 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
352 if ( rc != LDAP_NO_SUCH_OBJECT ) {
353 op->o_tmpfree( key.data, op->o_tmpmemctx );
357 /* we actually could do a empty get... */
360 data.ulen = sizeof(id);
361 data.flags = DB_DBT_USERMEM;
363 data.dlen = sizeof(id);
365 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
366 op->o_tmpfree( key.data, op->o_tmpmemctx );
368 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
370 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
371 db_strerror(rc) ), rc );
387 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
388 DB *db = bdb->bi_dn2id->bdi_db;
389 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
390 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
392 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
395 #ifndef BDB_MULTIPLE_SUFFIXES
396 if ( prefix == DN_SUBTREE_PREFIX
397 && ( ei->bei_id == 0 || ei->bei_parent->bei_id == 0 )) {
398 BDB_IDL_ALL(bdb, ids);
404 key.size = ndn->bv_len + 2;
406 key.flags = DB_DBT_USERMEM;
407 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
408 ((char *)key.data)[0] = prefix;
409 AC_MEMCPY( &((char *)key.data)[1], ndn->bv_val, key.size - 1 );
412 rc = bdb_idl_fetch_key( op->o_bd, db, locker, &key, ids, NULL, 0 );
415 Debug( LDAP_DEBUG_TRACE,
416 "<= bdb_dn2idl: get failed: %s (%d)\n",
417 db_strerror( rc ), rc, 0 );
420 Debug( LDAP_DEBUG_TRACE,
421 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
423 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
426 op->o_tmpfree( key.data, op->o_tmpmemctx );
431 /* Management routines for a hierarchically structured database.
433 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
434 * entry in this database is a struct diskNode, keyed by entryID and with
435 * the data containing the RDN and entryID of the node's children. We use
436 * a B-Tree with sorted duplicates to store all the children of a node under
437 * the same key. Also, the first item under the key contains the entry's own
438 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
439 * well as top-down. To keep this info first in the list, the high bit of all
440 * subsequent nrdnlen's is always set. This means we can only accomodate
441 * RDNs up to length 32767, but that's fine since full DNs are already
442 * restricted to 8192.
444 * The diskNode is a variable length structure. This definition is not
445 * directly usable for in-memory manipulation.
447 typedef struct diskNode {
448 unsigned char nrdnlen[2];
450 char rdn[1]; /* variable placement */
451 unsigned char entryID[sizeof(ID)]; /* variable placement */
454 /* Sort function for the sorted duplicate data items of a dn2id key.
455 * Sorts based on normalized RDN, in length order.
467 un = (diskNode *)usrkey->data;
468 cn = (diskNode *)curkey->data;
470 /* data is not aligned, cannot compare directly */
471 rc = un->nrdnlen[0] - cn->nrdnlen[0];
473 rc = un->nrdnlen[1] - cn->nrdnlen[1];
476 return strcmp( un->nrdn, cn->nrdn );
479 /* This function constructs a full DN for a given entry.
486 int rlen = 0, nrlen = 0;
493 /* count length of all DN components */
494 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
495 rlen += ei->bei_rdn.bv_len + 1;
496 nrlen += ei->bei_nrdn.bv_len + 1;
497 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
500 /* See if the entry DN was invalidated by a subtree rename */
502 if ( BEI(e)->bei_modrdns >= max ) {
505 /* We found a mismatch, tell the caller to lock it */
506 if ( checkit == 1 ) {
509 /* checkit == 2. do the fix. */
510 free( e->e_name.bv_val );
511 free( e->e_nname.bv_val );
514 e->e_name.bv_len = rlen - 1;
515 e->e_nname.bv_len = nrlen - 1;
516 e->e_name.bv_val = ch_malloc(rlen);
517 e->e_nname.bv_val = ch_malloc(nrlen);
518 ptr = e->e_name.bv_val;
519 nptr = e->e_nname.bv_val;
520 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
521 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
522 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
523 if ( ei->bei_parent ) {
528 BEI(e)->bei_modrdns = max;
535 /* We add two elements to the DN2ID database - a data item under the parent's
536 * entryID containing the child's RDN and entryID, and an item under the
537 * child's entryID containing the parent's entryID.
546 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
547 DB *db = bdb->bi_dn2id->bdi_db;
554 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_add 0x%lx: \"%s\"\n",
555 e->e_id, e->e_ndn, 0 );
557 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
559 rlen = dn_rdnlen( op->o_bd, &e->e_name );
561 nrlen = e->e_nname.bv_len;
562 rlen = e->e_name.bv_len;
565 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
566 d->nrdnlen[1] = nrlen & 0xff;
567 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
568 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
570 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
572 BDB_ID2DISK( e->e_id, ptr );
576 key.size = sizeof(ID);
577 key.flags = DB_DBT_USERMEM;
578 BDB_ID2DISK( eip->bei_id, &nid );
582 /* Need to make dummy root node once. Subsequent attempts
583 * will fail harmlessly.
585 if ( eip->bei_id == 0 ) {
586 diskNode dummy = {{0, 0}, "", "", ""};
588 data.size = sizeof(diskNode);
589 data.flags = DB_DBT_USERMEM;
591 db->put( db, txn, &key, &data, DB_NODUPDATA );
595 data.size = sizeof(diskNode) + rlen + nrlen;
596 data.flags = DB_DBT_USERMEM;
598 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
601 BDB_ID2DISK( e->e_id, &nid );
602 BDB_ID2DISK( eip->bei_id, ptr );
603 d->nrdnlen[0] ^= 0x80;
605 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
608 /* Update all parents' IDL cache entries */
609 if ( rc == 0 && bdb->bi_idl_cache_size ) {
611 char *ptr = ((char *)&tmp[1])-1;
613 key.size = sizeof(ID)+1;
614 tmp[1] = eip->bei_id;
615 *ptr = DN_ONE_PREFIX;
616 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
617 *ptr = DN_SUBTREE_PREFIX;
618 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
619 tmp[1] = eip->bei_id;
620 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
625 op->o_tmpfree( d, op->o_tmpmemctx );
626 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
638 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
639 DB *db = bdb->bi_dn2id->bdi_db;
645 unsigned char dlen[2];
648 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n",
649 e->e_id, e->e_ndn, 0 );
652 key.size = sizeof(ID);
654 key.flags = DB_DBT_USERMEM;
655 BDB_ID2DISK( eip->bei_id, &nid );
658 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
659 data.ulen = data.size;
660 data.dlen = data.size;
661 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
665 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
666 d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
667 d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
668 dlen[0] = d->nrdnlen[0];
669 dlen[1] = d->nrdnlen[1];
670 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
673 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
674 if ( rc ) goto leave;
676 /* We hold this lock until the TXN completes */
677 rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, TXN_ID( txn ), &lock );
678 if ( rc ) goto nolock;
680 /* Delete our ID from the parent's list */
681 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
683 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
684 !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
685 rc = cursor->c_del( cursor, 0 );
690 /* Delete our ID from the tree. With sorted duplicates, this
691 * will leave any child nodes still hanging around. This is OK
692 * for modrdn, which will add our info back in later.
695 BDB_ID2DISK( e->e_id, &nid );
696 rc = cursor->c_get( cursor, &key, &data, DB_SET );
698 rc = cursor->c_del( cursor, 0 );
702 cursor->c_close( cursor );
704 op->o_tmpfree( d, op->o_tmpmemctx );
706 /* Delete IDL cache entries */
707 if ( rc == 0 && bdb->bi_idl_cache_size ) {
709 char *ptr = ((char *)&tmp[1])-1;
711 key.size = sizeof(ID)+1;
712 tmp[1] = eip->bei_id;
713 *ptr = DN_ONE_PREFIX;
714 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
715 *ptr = DN_SUBTREE_PREFIX;
716 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
717 tmp[1] = eip->bei_id;
718 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
721 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
734 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
735 DB *db = bdb->bi_dn2id->bdi_db;
741 unsigned char dlen[2];
744 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
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;
754 parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
755 BDB_ID2DISK( parentID, &idp );
758 data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
759 data.ulen = data.size * 3;
760 data.dlen = data.ulen;
761 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
763 rc = db->cursor( db, NULL, &cursor, bdb->bi_db_opflags );
766 CURSOR_SETLOCKER( cursor, locker );
769 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
770 d->nrdnlen[1] = nrlen & 0xff;
771 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
772 dlen[0] = d->nrdnlen[0];
773 dlen[1] = d->nrdnlen[1];
774 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
778 rc = bdb_dn2id_lock( bdb, in, 0, locker, lock );
779 if ( rc ) goto leave;
781 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
782 if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
783 strncmp( d->nrdn, in->bv_val, nrlen ))) {
787 ptr = (char *) data.data + data.size - sizeof(ID);
788 BDB_DISK2ID( ptr, &ei->bei_id );
789 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
790 ptr = d->nrdn + nrlen + 1;
791 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
792 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
794 /* How many children does the parent have? */
795 /* FIXME: do we need to lock the parent
796 * entryinfo? Seems safe...
798 cursor->c_count( cursor, &dkids, 0 );
799 ei->bei_parent->bei_dkids = dkids;
804 cursor->c_close( cursor );
805 op->o_tmpfree( d, op->o_tmpmemctx );
807 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: get failed: %s (%d)\n",
808 db_strerror( rc ), rc, 0 );
810 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: got id=0x%lx\n",
824 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
825 DB *db = bdb->bi_dn2id->bdi_db;
834 key.size = sizeof(ID);
836 key.ulen = sizeof(ID);
837 key.flags = DB_DBT_USERMEM;
838 BDB_ID2DISK( ei->bei_id, &nid );
841 data.flags = DB_DBT_USERMEM;
843 rc = db->cursor( db, NULL, &cursor, bdb->bi_db_opflags );
846 CURSOR_SETLOCKER(cursor, locker);
849 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
850 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
853 rc = cursor->c_get( cursor, &key, &data, DB_SET );
855 if (d->nrdnlen[0] & 0x80) {
859 ptr = (char *) data.data + data.size - sizeof(ID);
860 BDB_DISK2ID( ptr, idp );
861 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
862 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
863 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
865 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
866 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
867 /* How many children does this node have? */
868 cursor->c_count( cursor, &dkids, 0 );
869 ei->bei_dkids = dkids;
872 cursor->c_close( cursor );
873 op->o_tmpfree( d, op->o_tmpmemctx );
883 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
884 DB *db = bdb->bi_dn2id->bdi_db;
892 key.size = sizeof(ID);
894 key.flags = DB_DBT_USERMEM;
895 BDB_ID2DISK( e->e_id, &id );
897 /* IDL cache is in host byte order */
898 if ( bdb->bi_idl_cache_size ) {
899 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
900 if ( rc != LDAP_NO_SUCH_OBJECT ) {
908 data.ulen = sizeof(d);
909 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
910 data.dlen = sizeof(d);
912 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
915 rc = cursor->c_get( cursor, &key, &data, DB_SET );
918 rc = cursor->c_count( cursor, &dkids, 0 );
920 BEI(e)->bei_dkids = dkids;
921 if ( dkids < 2 ) rc = DB_NOTFOUND;
924 cursor->c_close( cursor );
929 * We can't just use bdb_idl_fetch_key because
930 * 1 - our data items are longer than just an entry ID
931 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
933 * We descend the tree recursively, so we define this cookie
934 * to hold our necessary state information. The bdb_dn2idl_internal
935 * function uses this cookie when calling itself.
938 struct dn2id_cookie {
939 struct bdb_info *bdb;
964 EntryInfo *ei = data;
967 bdb_idl_append_one( idl, ei->bei_id );
973 struct dn2id_cookie *cx
976 BDB_IDL_ZERO( cx->tmp );
978 if ( cx->bdb->bi_idl_cache_size ) {
979 char *ptr = ((char *)&cx->id)-1;
982 cx->key.size = sizeof(ID)+1;
983 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
984 ID *ids = cx->depth ? cx->tmp : cx->ids;
986 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
987 if ( cx->rc == LDAP_SUCCESS ) {
989 bdb_idl_append( cx->ids, cx->tmp );
995 *ptr = DN_ONE_PREFIX;
996 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
997 if ( cx->rc == LDAP_SUCCESS ) {
1000 if ( cx->rc == DB_NOTFOUND ) {
1005 bdb_cache_entryinfo_lock( cx->ei );
1007 /* If number of kids in the cache differs from on-disk, load
1008 * up all the kids from the database
1010 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
1012 db_recno_t dkids = cx->ei->bei_dkids;
1013 ei.bei_parent = cx->ei;
1015 /* Only one thread should load the cache */
1016 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
1017 bdb_cache_entryinfo_unlock( cx->ei );
1018 ldap_pvt_thread_yield();
1019 bdb_cache_entryinfo_lock( cx->ei );
1020 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
1025 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
1027 bdb_cache_entryinfo_unlock( cx->ei );
1029 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
1030 cx->bdb->bi_db_opflags );
1034 cx->data.data = &cx->dbuf;
1035 cx->data.ulen = sizeof(ID);
1036 cx->data.dlen = sizeof(ID);
1037 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
1039 /* The first item holds the parent ID. Ignore it. */
1040 cx->key.data = &cx->nid;
1041 cx->key.size = sizeof(ID);
1042 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
1044 cx->dbc->c_close( cx->dbc );
1048 /* If the on-disk count is zero we've never checked it.
1052 cx->dbc->c_count( cx->dbc, &dkids, 0 );
1053 cx->ei->bei_dkids = dkids;
1056 cx->data.data = cx->buf;
1057 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1058 cx->data.flags = DB_DBT_USERMEM;
1061 /* Fetch the rest of the IDs in a loop... */
1062 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1063 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1067 DB_MULTIPLE_INIT( ptr, &cx->data );
1069 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
1072 diskNode *d = (diskNode *)j;
1075 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
1076 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
1077 ei.bei_nrdn.bv_len = nrlen;
1078 /* nrdn/rdn are set in-place.
1079 * hdb_cache_load will copy them as needed
1081 ei.bei_nrdn.bv_val = d->nrdn;
1082 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1083 - ei.bei_nrdn.bv_len;
1084 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1085 bdb_idl_append_one( cx->tmp, ei.bei_id );
1086 hdb_cache_load( cx->bdb, &ei, &ei2 );
1092 cx->rc = cx->dbc->c_close( cx->dbc );
1094 bdb_cache_entryinfo_lock( cx->ei );
1095 cx->ei->bei_state ^= CACHE_ENTRY_ONELEVEL;
1096 bdb_cache_entryinfo_unlock( cx->ei );
1101 /* The in-memory cache is in sync with the on-disk data.
1102 * do we have any kids?
1106 if ( cx->ei->bei_ckids > 0 ) {
1107 /* Walk the kids tree; order is irrelevant since bdb_idl_sort
1108 * will sort it later.
1110 avl_apply( cx->ei->bei_kids, apply_func,
1111 cx->tmp, -1, AVL_POSTORDER );
1113 bdb_cache_entryinfo_unlock( cx->ei );
1116 if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1117 bdb_idl_sort( cx->tmp, cx->buf );
1118 if ( cx->bdb->bi_idl_cache_max_size && !BDB_IDL_IS_ZERO( cx->tmp )) {
1119 char *ptr = ((char *)&cx->id)-1;
1121 cx->key.size = sizeof(ID)+1;
1122 *ptr = DN_ONE_PREFIX;
1123 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1127 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1128 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1129 bdb_idl_append( cx->ids, cx->tmp );
1131 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1133 EntryInfo *ei = cx->ei;
1135 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1136 cx->op->o_tmpmemctx );
1137 BDB_IDL_CPY( save, cx->tmp );
1141 for ( cx->id = bdb_idl_first( save, &idcurs );
1143 cx->id = bdb_idl_next( save, &idcurs )) {
1144 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
1146 ( cx->ei->bei_state & CACHE_ENTRY_NO_KIDS ))
1149 BDB_ID2DISK( cx->id, &cx->nid );
1150 hdb_dn2idl_internal( cx );
1151 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1155 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1156 if ( nokids ) ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1158 /* Make sure caller knows it had kids! */
1163 BDB_IDL_CPY( cx->ids, cx->tmp );
1178 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1179 struct dn2id_cookie cx;
1181 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1182 ndn->bv_val, 0, 0 );
1184 #ifndef BDB_MULTIPLE_SUFFIXES
1185 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1186 ( ei->bei_id == 0 ||
1187 ei->bei_parent->bei_id == 0 ))
1189 BDB_IDL_ALL( bdb, ids );
1195 BDB_ID2DISK( cx.id, &cx.nid );
1198 cx.db = cx.bdb->bi_dn2id->bdi_db;
1199 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1200 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1203 cx.buf = stack + BDB_IDL_UM_SIZE;
1209 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1213 BDB_IDL_ZERO( ids );
1215 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1216 return LDAP_SUCCESS;
1219 cx.key.ulen = sizeof(ID);
1220 cx.key.size = sizeof(ID);
1221 cx.key.flags = DB_DBT_USERMEM;
1225 hdb_dn2idl_internal(&cx);
1226 if ( cx.need_sort ) {
1227 char *ptr = ((char *)&cx.id)-1;
1228 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1229 bdb_idl_sort( cx.ids, cx.tmp );
1231 cx.key.size = sizeof(ID)+1;
1234 if ( cx.bdb->bi_idl_cache_max_size )
1235 bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1238 if ( cx.rc == DB_NOTFOUND )
1239 cx.rc = LDAP_SUCCESS;
1243 #endif /* BDB_HIER */