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( \"%s\", 0x%08lx )\n",
66 e->e_ndn, (long) e->e_id, 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: put failed: %s %d\n",
90 db_strerror(rc), rc, 0 );
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: subtree (%s) put failed: %d\n",
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: parent (%s) insert failed: %d\n",
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: subtree (%s) insert failed: %d\n",
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: %d\n", rc, 0, 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( \"%s\", 0x%08lx )\n",
179 e->e_ndn, e->e_id, 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: delete failed: %s %d\n",
200 db_strerror(rc), rc, 0 );
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: subtree (%s) delete failed: %d\n",
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: parent (%s) delete failed: %d\n",
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: subtree (%s) delete failed: %d\n",
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 %d\n", rc, 0, 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 );
289 key.size = dn->bv_len + 2;
290 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
291 ((char *)key.data)[0] = DN_BASE_PREFIX;
292 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
297 data.ulen = sizeof(ID);
298 data.flags = DB_DBT_USERMEM;
300 rc = bdb_dn2id_lock( bdb, dn, 0, locker, lock );
303 rc = db->cursor( db, NULL, &cursor, bdb->bi_db_opflags );
306 CURSOR_SETLOCKER(cursor, locker);
310 rc = cursor->c_get( cursor, &key, &data, DB_SET );
313 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
314 db_strerror( rc ), rc, 0 );
316 BDB_DISK2ID( &nid, &ei->bei_id );
317 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
321 cursor->c_close( cursor );
322 op->o_tmpfree( key.data, op->o_tmpmemctx );
333 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
334 DB *db = bdb->bi_dn2id->bdi_db;
338 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
339 e->e_nname.bv_val, 0, 0 );
341 key.size = e->e_nname.bv_len + 2;
342 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
343 ((char *)key.data)[0] = DN_ONE_PREFIX;
344 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
346 if ( bdb->bi_idl_cache_size ) {
347 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
348 if ( rc != LDAP_NO_SUCH_OBJECT ) {
349 op->o_tmpfree( key.data, op->o_tmpmemctx );
353 /* we actually could do a empty get... */
356 data.ulen = sizeof(id);
357 data.flags = DB_DBT_USERMEM;
359 data.dlen = sizeof(id);
361 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
362 op->o_tmpfree( key.data, op->o_tmpmemctx );
364 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
366 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
367 db_strerror(rc) ), rc );
383 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
384 DB *db = bdb->bi_dn2id->bdi_db;
385 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
386 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
388 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
391 #ifndef BDB_MULTIPLE_SUFFIXES
392 if ( prefix == DN_SUBTREE_PREFIX
393 && ( ei->bei_id == 0 || ei->bei_parent->bei_id == 0 )) {
394 BDB_IDL_ALL(bdb, ids);
400 key.size = ndn->bv_len + 2;
402 key.flags = DB_DBT_USERMEM;
403 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
404 ((char *)key.data)[0] = prefix;
405 AC_MEMCPY( &((char *)key.data)[1], ndn->bv_val, key.size - 1 );
408 rc = bdb_idl_fetch_key( op->o_bd, db, locker, &key, ids, NULL, 0 );
411 Debug( LDAP_DEBUG_TRACE,
412 "<= bdb_dn2idl: get failed: %s (%d)\n",
413 db_strerror( rc ), rc, 0 );
416 Debug( LDAP_DEBUG_TRACE,
417 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
419 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
422 op->o_tmpfree( key.data, op->o_tmpmemctx );
427 /* Management routines for a hierarchically structured database.
429 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
430 * entry in this database is a struct diskNode, keyed by entryID and with
431 * the data containing the RDN and entryID of the node's children. We use
432 * a B-Tree with sorted duplicates to store all the children of a node under
433 * the same key. Also, the first item under the key contains the entry's own
434 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
435 * well as top-down. To keep this info first in the list, the high bit of all
436 * subsequent nrdnlen's is always set. This means we can only accomodate
437 * RDNs up to length 32767, but that's fine since full DNs are already
438 * restricted to 8192.
440 * The diskNode is a variable length structure. This definition is not
441 * directly usable for in-memory manipulation.
443 typedef struct diskNode {
444 unsigned char nrdnlen[2];
446 char rdn[1]; /* variable placement */
447 unsigned char entryID[sizeof(ID)]; /* variable placement */
450 /* This function constructs a full DN for a given entry.
457 int rlen = 0, nrlen = 0;
464 /* count length of all DN components */
465 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
466 rlen += ei->bei_rdn.bv_len + 1;
467 nrlen += ei->bei_nrdn.bv_len + 1;
468 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
471 /* See if the entry DN was invalidated by a subtree rename */
473 if ( BEI(e)->bei_modrdns >= max ) {
476 /* We found a mismatch, tell the caller to lock it */
477 if ( checkit == 1 ) {
480 /* checkit == 2. do the fix. */
481 free( e->e_name.bv_val );
482 free( e->e_nname.bv_val );
485 e->e_name.bv_len = rlen - 1;
486 e->e_nname.bv_len = nrlen - 1;
487 e->e_name.bv_val = ch_malloc(rlen);
488 e->e_nname.bv_val = ch_malloc(nrlen);
489 ptr = e->e_name.bv_val;
490 nptr = e->e_nname.bv_val;
491 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
492 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
493 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
494 if ( ei->bei_parent ) {
499 BEI(e)->bei_modrdns = max;
506 /* We add two elements to the DN2ID database - a data item under the parent's
507 * entryID containing the child's RDN and entryID, and an item under the
508 * child's entryID containing the parent's entryID.
517 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
518 DB *db = bdb->bi_dn2id->bdi_db;
525 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
527 rlen = dn_rdnlen( op->o_bd, &e->e_name );
529 nrlen = e->e_nname.bv_len;
530 rlen = e->e_name.bv_len;
533 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
534 d->nrdnlen[1] = nrlen & 0xff;
535 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
536 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
538 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
540 BDB_ID2DISK( e->e_id, ptr );
544 key.size = sizeof(ID);
545 key.flags = DB_DBT_USERMEM;
546 BDB_ID2DISK( eip->bei_id, &nid );
550 /* Need to make dummy root node once. Subsequent attempts
551 * will fail harmlessly.
553 if ( eip->bei_id == 0 ) {
554 diskNode dummy = {{0, 0}, "", "", ""};
556 data.size = sizeof(diskNode);
557 data.flags = DB_DBT_USERMEM;
559 db->put( db, txn, &key, &data, DB_NODUPDATA );
563 data.size = sizeof(diskNode) + rlen + nrlen;
564 data.flags = DB_DBT_USERMEM;
566 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
569 BDB_ID2DISK( e->e_id, &nid );
570 BDB_ID2DISK( eip->bei_id, ptr );
571 d->nrdnlen[0] ^= 0x80;
573 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
576 /* Update all parents' IDL cache entries */
577 if ( rc == 0 && bdb->bi_idl_cache_size ) {
579 char *ptr = ((char *)&tmp[1])-1;
581 key.size = sizeof(ID)+1;
582 tmp[1] = eip->bei_id;
583 *ptr = DN_ONE_PREFIX;
584 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
585 *ptr = DN_SUBTREE_PREFIX;
586 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
587 tmp[1] = eip->bei_id;
588 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
591 op->o_tmpfree( d, op->o_tmpmemctx );
603 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
604 DB *db = bdb->bi_dn2id->bdi_db;
610 unsigned char dlen[2];
614 key.size = sizeof(ID);
616 key.flags = DB_DBT_USERMEM;
617 BDB_ID2DISK( eip->bei_id, &nid );
620 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
621 data.ulen = data.size;
622 data.dlen = data.size;
623 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
625 /* We hold this lock until the TXN completes */
626 rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, TXN_ID( txn ), &lock );
630 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
633 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
634 d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
635 d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
636 dlen[0] = d->nrdnlen[0];
637 dlen[1] = d->nrdnlen[1];
638 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
641 /* Delete our ID from the parent's list */
642 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
644 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
645 !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
646 rc = cursor->c_del( cursor, 0 );
651 /* Delete our ID from the tree. With sorted duplicates, this
652 * will leave any child nodes still hanging around. This is OK
653 * for modrdn, which will add our info back in later.
656 BDB_ID2DISK( e->e_id, &nid );
657 rc = cursor->c_get( cursor, &key, &data, DB_SET );
659 rc = cursor->c_del( cursor, 0 );
661 cursor->c_close( cursor );
662 op->o_tmpfree( d, op->o_tmpmemctx );
664 /* Delete IDL cache entries */
665 if ( rc == 0 && bdb->bi_idl_cache_size ) {
667 char *ptr = ((char *)&tmp[1])-1;
669 key.size = sizeof(ID)+1;
670 tmp[1] = eip->bei_id;
671 *ptr = DN_ONE_PREFIX;
672 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
673 *ptr = DN_SUBTREE_PREFIX;
674 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
675 tmp[1] = eip->bei_id;
676 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
691 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
692 DB *db = bdb->bi_dn2id->bdi_db;
698 unsigned char dlen[2];
701 nrlen = dn_rdnlen( op->o_bd, in );
702 if (!nrlen) nrlen = in->bv_len;
705 key.size = sizeof(ID);
707 key.ulen = sizeof(ID);
708 key.flags = DB_DBT_USERMEM;
709 parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
710 BDB_ID2DISK( parentID, &idp );
713 data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
714 data.ulen = data.size * 3;
715 data.dlen = data.ulen;
716 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
718 rc = bdb_dn2id_lock( bdb, in, 0, locker, lock );
721 rc = db->cursor( db, NULL, &cursor, bdb->bi_db_opflags );
724 CURSOR_SETLOCKER( cursor, locker );
727 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
728 d->nrdnlen[1] = nrlen & 0xff;
729 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
730 dlen[0] = d->nrdnlen[0];
731 dlen[1] = d->nrdnlen[1];
732 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
736 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
737 if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
738 strncmp( d->nrdn, in->bv_val, nrlen ))) {
742 ptr = (char *) data.data + data.size - sizeof(ID);
743 BDB_DISK2ID( ptr, &ei->bei_id );
744 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
745 ptr = d->nrdn + nrlen + 1;
746 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
747 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
749 /* How many children does the parent have? */
750 /* FIXME: do we need to lock the parent
751 * entryinfo? Seems safe...
753 cursor->c_count( cursor, &dkids, 0 );
754 ei->bei_parent->bei_dkids = dkids;
757 cursor->c_close( cursor );
758 op->o_tmpfree( d, op->o_tmpmemctx );
770 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
771 DB *db = bdb->bi_dn2id->bdi_db;
780 key.size = sizeof(ID);
782 key.ulen = sizeof(ID);
783 key.flags = DB_DBT_USERMEM;
784 BDB_ID2DISK( ei->bei_id, &nid );
787 data.flags = DB_DBT_USERMEM;
789 rc = db->cursor( db, NULL, &cursor, bdb->bi_db_opflags );
792 CURSOR_SETLOCKER(cursor, locker);
795 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
796 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
799 rc = cursor->c_get( cursor, &key, &data, DB_SET );
801 if (d->nrdnlen[0] & 0x80) {
805 ptr = (char *) data.data + data.size - sizeof(ID);
806 BDB_DISK2ID( ptr, idp );
807 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
808 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
809 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
811 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
812 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
813 /* How many children does this node have? */
814 cursor->c_count( cursor, &dkids, 0 );
815 ei->bei_dkids = dkids;
818 cursor->c_close( cursor );
819 op->o_tmpfree( d, op->o_tmpmemctx );
829 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
830 DB *db = bdb->bi_dn2id->bdi_db;
838 key.size = sizeof(ID);
840 key.flags = DB_DBT_USERMEM;
841 BDB_ID2DISK( e->e_id, &id );
843 /* IDL cache is in host byte order */
844 if ( bdb->bi_idl_cache_size ) {
845 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
846 if ( rc != LDAP_NO_SUCH_OBJECT ) {
854 data.ulen = sizeof(d);
855 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
856 data.dlen = sizeof(d);
858 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
861 rc = cursor->c_get( cursor, &key, &data, DB_SET );
864 rc = cursor->c_count( cursor, &dkids, 0 );
866 BEI(e)->bei_dkids = dkids;
867 if ( dkids < 2 ) rc = DB_NOTFOUND;
870 cursor->c_close( cursor );
875 * We can't just use bdb_idl_fetch_key because
876 * 1 - our data items are longer than just an entry ID
877 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
879 * We descend the tree recursively, so we define this cookie
880 * to hold our necessary state information. The bdb_dn2idl_internal
881 * function uses this cookie when calling itself.
884 struct dn2id_cookie {
885 struct bdb_info *bdb;
910 EntryInfo *ei = data;
913 bdb_idl_append_one( idl, ei->bei_id );
919 struct dn2id_cookie *cx
922 BDB_IDL_ZERO( cx->tmp );
924 if ( cx->bdb->bi_idl_cache_size ) {
925 char *ptr = ((char *)&cx->id)-1;
928 cx->key.size = sizeof(ID)+1;
929 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
930 ID *ids = cx->depth ? cx->tmp : cx->ids;
932 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
933 if ( cx->rc == LDAP_SUCCESS ) {
935 bdb_idl_append( cx->ids, cx->tmp );
941 *ptr = DN_ONE_PREFIX;
942 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
943 if ( cx->rc == LDAP_SUCCESS ) {
946 if ( cx->rc == DB_NOTFOUND ) {
951 bdb_cache_entryinfo_lock( cx->ei );
953 /* If number of kids in the cache differs from on-disk, load
954 * up all the kids from the database
956 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
958 db_recno_t dkids = cx->ei->bei_dkids;
959 ei.bei_parent = cx->ei;
961 /* Only one thread should load the cache */
962 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
963 bdb_cache_entryinfo_unlock( cx->ei );
964 ldap_pvt_thread_yield();
965 bdb_cache_entryinfo_lock( cx->ei );
966 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
971 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
973 bdb_cache_entryinfo_unlock( cx->ei );
975 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
976 cx->bdb->bi_db_opflags );
980 cx->data.data = &cx->dbuf;
981 cx->data.ulen = sizeof(ID);
982 cx->data.dlen = sizeof(ID);
983 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
985 /* The first item holds the parent ID. Ignore it. */
986 cx->key.data = &cx->nid;
987 cx->key.size = sizeof(ID);
988 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
990 cx->dbc->c_close( cx->dbc );
994 /* If the on-disk count is zero we've never checked it.
998 cx->dbc->c_count( cx->dbc, &dkids, 0 );
999 cx->ei->bei_dkids = dkids;
1002 cx->data.data = cx->buf;
1003 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1004 cx->data.flags = DB_DBT_USERMEM;
1007 /* Fetch the rest of the IDs in a loop... */
1008 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1009 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1013 DB_MULTIPLE_INIT( ptr, &cx->data );
1015 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
1018 diskNode *d = (diskNode *)j;
1021 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
1022 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
1023 ei.bei_nrdn.bv_len = nrlen;
1024 /* nrdn/rdn are set in-place.
1025 * hdb_cache_load will copy them as needed
1027 ei.bei_nrdn.bv_val = d->nrdn;
1028 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1029 - ei.bei_nrdn.bv_len;
1030 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1031 bdb_idl_append_one( cx->tmp, ei.bei_id );
1032 hdb_cache_load( cx->bdb, &ei, &ei2 );
1038 cx->rc = cx->dbc->c_close( cx->dbc );
1040 bdb_cache_entryinfo_lock( cx->ei );
1041 cx->ei->bei_state ^= CACHE_ENTRY_ONELEVEL;
1042 bdb_cache_entryinfo_unlock( cx->ei );
1047 /* The in-memory cache is in sync with the on-disk data.
1048 * do we have any kids?
1052 if ( cx->ei->bei_ckids > 0 ) {
1053 /* Walk the kids tree; order is irrelevant since bdb_idl_sort
1054 * will sort it later.
1056 avl_apply( cx->ei->bei_kids, apply_func,
1057 cx->tmp, -1, AVL_POSTORDER );
1059 bdb_cache_entryinfo_unlock( cx->ei );
1062 if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1063 bdb_idl_sort( cx->tmp, cx->buf );
1064 if ( cx->bdb->bi_idl_cache_max_size && !BDB_IDL_IS_ZERO( cx->tmp )) {
1065 char *ptr = ((char *)&cx->id)-1;
1067 cx->key.size = sizeof(ID)+1;
1068 *ptr = DN_ONE_PREFIX;
1069 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1073 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1074 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1075 bdb_idl_append( cx->ids, cx->tmp );
1077 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1079 EntryInfo *ei = cx->ei;
1081 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1082 cx->op->o_tmpmemctx );
1083 BDB_IDL_CPY( save, cx->tmp );
1087 for ( cx->id = bdb_idl_first( save, &idcurs );
1089 cx->id = bdb_idl_next( save, &idcurs )) {
1090 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
1092 ( cx->ei->bei_state & CACHE_ENTRY_NO_KIDS ))
1095 BDB_ID2DISK( cx->id, &cx->nid );
1096 hdb_dn2idl_internal( cx );
1097 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1101 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1102 if ( nokids ) ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1104 /* Make sure caller knows it had kids! */
1109 BDB_IDL_CPY( cx->ids, cx->tmp );
1124 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1125 struct dn2id_cookie cx;
1127 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1128 ndn->bv_val, 0, 0 );
1130 #ifndef BDB_MULTIPLE_SUFFIXES
1131 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1132 ( ei->bei_id == 0 ||
1133 ei->bei_parent->bei_id == 0 ))
1135 BDB_IDL_ALL( bdb, ids );
1141 BDB_ID2DISK( cx.id, &cx.nid );
1144 cx.db = cx.bdb->bi_dn2id->bdi_db;
1145 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1146 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1149 cx.buf = stack + BDB_IDL_UM_SIZE;
1155 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1159 BDB_IDL_ZERO( ids );
1161 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1162 return LDAP_SUCCESS;
1165 cx.key.ulen = sizeof(ID);
1166 cx.key.size = sizeof(ID);
1167 cx.key.flags = DB_DBT_USERMEM;
1171 hdb_dn2idl_internal(&cx);
1172 if ( cx.need_sort ) {
1173 char *ptr = ((char *)&cx.id)-1;
1174 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1175 bdb_idl_sort( cx.ids, cx.tmp );
1177 cx.key.size = sizeof(ID)+1;
1180 if ( cx.bdb->bi_idl_cache_max_size )
1181 bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1184 if ( cx.rc == DB_NOTFOUND )
1185 cx.rc = LDAP_SUCCESS;
1189 #endif /* BDB_HIER */