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, u_int32_t 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, 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->locker = 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 );
385 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
386 DB *db = bdb->bi_dn2id->bdi_db;
387 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
388 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
390 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
391 e->e_nname.bv_val, 0, 0 );
393 #ifndef BDB_MULTIPLE_SUFFIXES
394 if ( prefix == DN_SUBTREE_PREFIX && BEI(e)->bei_parent->bei_id == 0 ) {
395 BDB_IDL_ALL(bdb, ids);
401 key.size = e->e_nname.bv_len + 2;
403 key.flags = DB_DBT_USERMEM;
404 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
405 ((char *)key.data)[0] = prefix;
406 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
409 rc = bdb_idl_fetch_key( op->o_bd, db, NULL, &key, ids, NULL, 0 );
412 Debug( LDAP_DEBUG_TRACE,
413 "<= bdb_dn2idl: get failed: %s (%d)\n",
414 db_strerror( rc ), rc, 0 );
417 Debug( LDAP_DEBUG_TRACE,
418 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
420 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
423 op->o_tmpfree( key.data, op->o_tmpmemctx );
428 /* Management routines for a hierarchically structured database.
430 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
431 * entry in this database is a struct diskNode, keyed by entryID and with
432 * the data containing the RDN and entryID of the node's children. We use
433 * a B-Tree with sorted duplicates to store all the children of a node under
434 * the same key. Also, the first item under the key contains the entry's own
435 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
436 * well as top-down. To keep this info first in the list, the high bit of all
437 * subsequent nrdnlen's is always set. This means we can only accomodate
438 * RDNs up to length 32767, but that's fine since full DNs are already
439 * restricted to 8192.
441 * The diskNode is a variable length structure. This definition is not
442 * directly usable for in-memory manipulation.
444 typedef struct diskNode {
445 unsigned char nrdnlen[2];
447 char rdn[1]; /* variable placement */
448 unsigned char entryID[sizeof(ID)]; /* variable placement */
451 /* Sort function for the sorted duplicate data items of a dn2id key.
452 * Sorts based on normalized RDN, in length order.
464 un = (diskNode *)usrkey->data;
465 cn = (diskNode *)curkey->data;
467 /* data is not aligned, cannot compare directly */
468 rc = un->nrdnlen[0] - cn->nrdnlen[0];
470 rc = un->nrdnlen[1] - cn->nrdnlen[1];
473 return strcmp( un->nrdn, cn->nrdn );
476 /* This function constructs a full DN for a given entry.
483 int rlen = 0, nrlen = 0;
490 /* count length of all DN components */
491 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
492 rlen += ei->bei_rdn.bv_len + 1;
493 nrlen += ei->bei_nrdn.bv_len + 1;
494 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
497 /* See if the entry DN was invalidated by a subtree rename */
499 if ( BEI(e)->bei_modrdns >= max ) {
502 /* We found a mismatch, tell the caller to lock it */
503 if ( checkit == 1 ) {
506 /* checkit == 2. do the fix. */
507 free( e->e_name.bv_val );
508 free( e->e_nname.bv_val );
511 e->e_name.bv_len = rlen - 1;
512 e->e_nname.bv_len = nrlen - 1;
513 e->e_name.bv_val = ch_malloc(rlen);
514 e->e_nname.bv_val = ch_malloc(nrlen);
515 ptr = e->e_name.bv_val;
516 nptr = e->e_nname.bv_val;
517 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
518 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
519 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
520 if ( ei->bei_parent ) {
525 BEI(e)->bei_modrdns = max;
532 /* We add two elements to the DN2ID database - a data item under the parent's
533 * entryID containing the child's RDN and entryID, and an item under the
534 * child's entryID containing the parent's entryID.
543 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
544 DB *db = bdb->bi_dn2id->bdi_db;
551 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_add 0x%lx: \"%s\"\n",
552 e->e_id, e->e_ndn, 0 );
554 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
556 rlen = dn_rdnlen( op->o_bd, &e->e_name );
558 nrlen = e->e_nname.bv_len;
559 rlen = e->e_name.bv_len;
562 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
563 d->nrdnlen[1] = nrlen & 0xff;
564 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
565 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
567 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
569 BDB_ID2DISK( e->e_id, ptr );
573 key.size = sizeof(ID);
574 key.flags = DB_DBT_USERMEM;
575 BDB_ID2DISK( eip->bei_id, &nid );
579 /* Need to make dummy root node once. Subsequent attempts
580 * will fail harmlessly.
582 if ( eip->bei_id == 0 ) {
583 diskNode dummy = {{0, 0}, "", "", ""};
585 data.size = sizeof(diskNode);
586 data.flags = DB_DBT_USERMEM;
588 db->put( db, txn, &key, &data, DB_NODUPDATA );
592 data.size = sizeof(diskNode) + rlen + nrlen;
593 data.flags = DB_DBT_USERMEM;
595 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
598 BDB_ID2DISK( e->e_id, &nid );
599 BDB_ID2DISK( eip->bei_id, ptr );
600 d->nrdnlen[0] ^= 0x80;
602 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
605 /* Update all parents' IDL cache entries */
606 if ( rc == 0 && bdb->bi_idl_cache_size ) {
608 char *ptr = ((char *)&tmp[1])-1;
610 key.size = sizeof(ID)+1;
611 tmp[1] = eip->bei_id;
612 *ptr = DN_ONE_PREFIX;
613 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
614 *ptr = DN_SUBTREE_PREFIX;
615 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
616 tmp[1] = eip->bei_id;
617 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
622 op->o_tmpfree( d, op->o_tmpmemctx );
623 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
635 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
636 DB *db = bdb->bi_dn2id->bdi_db;
642 unsigned char dlen[2];
645 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n",
646 e->e_id, e->e_ndn, 0 );
649 key.size = sizeof(ID);
651 key.flags = DB_DBT_USERMEM;
652 BDB_ID2DISK( eip->bei_id, &nid );
655 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
656 data.ulen = data.size;
657 data.dlen = data.size;
658 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
662 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
663 d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
664 d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
665 dlen[0] = d->nrdnlen[0];
666 dlen[1] = d->nrdnlen[1];
667 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
670 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
671 if ( rc ) goto leave;
673 /* We hold this lock until the TXN completes */
674 rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, TXN_ID( txn ), &lock );
675 if ( rc ) goto nolock;
677 /* Delete our ID from the parent's list */
678 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
680 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
681 !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
682 rc = cursor->c_del( cursor, 0 );
687 /* Delete our ID from the tree. With sorted duplicates, this
688 * will leave any child nodes still hanging around. This is OK
689 * for modrdn, which will add our info back in later.
692 BDB_ID2DISK( e->e_id, &nid );
693 rc = cursor->c_get( cursor, &key, &data, DB_SET );
695 rc = cursor->c_del( cursor, 0 );
699 cursor->c_close( cursor );
701 op->o_tmpfree( d, op->o_tmpmemctx );
703 /* Delete IDL cache entries */
704 if ( rc == 0 && bdb->bi_idl_cache_size ) {
706 char *ptr = ((char *)&tmp[1])-1;
708 key.size = sizeof(ID)+1;
709 tmp[1] = eip->bei_id;
710 *ptr = DN_ONE_PREFIX;
711 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
712 *ptr = DN_SUBTREE_PREFIX;
713 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
714 tmp[1] = eip->bei_id;
715 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
718 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
731 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
732 DB *db = bdb->bi_dn2id->bdi_db;
738 unsigned char dlen[2];
741 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
743 nrlen = dn_rdnlen( op->o_bd, in );
744 if (!nrlen) nrlen = in->bv_len;
747 key.size = sizeof(ID);
749 key.ulen = sizeof(ID);
750 key.flags = DB_DBT_USERMEM;
751 parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
752 BDB_ID2DISK( parentID, &idp );
755 data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
756 data.ulen = data.size * 3;
757 data.dlen = data.ulen;
758 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
760 rc = db->cursor( db, NULL, &cursor, bdb->bi_db_opflags );
763 cursor->locker = locker;
766 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
767 d->nrdnlen[1] = nrlen & 0xff;
768 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
769 dlen[0] = d->nrdnlen[0];
770 dlen[1] = d->nrdnlen[1];
771 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
775 rc = bdb_dn2id_lock( bdb, in, 0, locker, lock );
776 if ( rc ) goto leave;
778 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
779 if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
780 strncmp( d->nrdn, in->bv_val, nrlen ))) {
784 ptr = (char *) data.data + data.size - sizeof(ID);
785 BDB_DISK2ID( ptr, &ei->bei_id );
786 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
787 ptr = d->nrdn + nrlen + 1;
788 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
789 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
791 /* How many children does the parent have? */
792 /* FIXME: do we need to lock the parent
793 * entryinfo? Seems safe...
795 cursor->c_count( cursor, &dkids, 0 );
796 ei->bei_parent->bei_dkids = dkids;
801 cursor->c_close( cursor );
802 op->o_tmpfree( d, op->o_tmpmemctx );
804 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: get failed: %s (%d)\n",
805 db_strerror( rc ), rc, 0 );
807 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: got id=0x%lx\n",
821 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
822 DB *db = bdb->bi_dn2id->bdi_db;
831 key.size = sizeof(ID);
833 key.ulen = sizeof(ID);
834 key.flags = DB_DBT_USERMEM;
835 BDB_ID2DISK( ei->bei_id, &nid );
838 data.flags = DB_DBT_USERMEM;
840 rc = db->cursor( db, NULL, &cursor, bdb->bi_db_opflags );
843 cursor->locker = locker;
846 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
847 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
850 rc = cursor->c_get( cursor, &key, &data, DB_SET );
852 if (d->nrdnlen[0] & 0x80) {
856 ptr = (char *) data.data + data.size - sizeof(ID);
857 BDB_DISK2ID( ptr, idp );
858 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
859 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
860 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
862 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
863 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
864 /* How many children does this node have? */
865 cursor->c_count( cursor, &dkids, 0 );
866 ei->bei_dkids = dkids;
869 cursor->c_close( cursor );
870 op->o_tmpfree( d, op->o_tmpmemctx );
880 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
881 DB *db = bdb->bi_dn2id->bdi_db;
889 key.size = sizeof(ID);
891 key.flags = DB_DBT_USERMEM;
892 BDB_ID2DISK( e->e_id, &id );
894 /* IDL cache is in host byte order */
895 if ( bdb->bi_idl_cache_size ) {
896 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
897 if ( rc != LDAP_NO_SUCH_OBJECT ) {
905 data.ulen = sizeof(d);
906 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
907 data.dlen = sizeof(d);
909 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
912 rc = cursor->c_get( cursor, &key, &data, DB_SET );
915 rc = cursor->c_count( cursor, &dkids, 0 );
917 BEI(e)->bei_dkids = dkids;
918 if ( dkids < 2 ) rc = DB_NOTFOUND;
921 cursor->c_close( cursor );
926 * We can't just use bdb_idl_fetch_key because
927 * 1 - our data items are longer than just an entry ID
928 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
930 * We descend the tree recursively, so we define this cookie
931 * to hold our necessary state information. The bdb_dn2idl_internal
932 * function uses this cookie when calling itself.
935 struct dn2id_cookie {
936 struct bdb_info *bdb;
960 EntryInfo *ei = data;
963 bdb_idl_append_one( idl, ei->bei_id );
969 struct dn2id_cookie *cx
972 BDB_IDL_ZERO( cx->tmp );
974 if ( cx->bdb->bi_idl_cache_size ) {
975 char *ptr = ((char *)&cx->id)-1;
978 cx->key.size = sizeof(ID)+1;
979 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
980 ID *ids = cx->depth ? cx->tmp : cx->ids;
982 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
983 if ( cx->rc == LDAP_SUCCESS ) {
985 bdb_idl_append( cx->ids, cx->tmp );
991 *ptr = DN_ONE_PREFIX;
992 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
993 if ( cx->rc == LDAP_SUCCESS ) {
996 if ( cx->rc == DB_NOTFOUND ) {
1001 bdb_cache_entryinfo_lock( cx->ei );
1003 /* If number of kids in the cache differs from on-disk, load
1004 * up all the kids from the database
1006 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
1008 db_recno_t dkids = cx->ei->bei_dkids;
1009 ei.bei_parent = cx->ei;
1011 /* Only one thread should load the cache */
1012 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
1013 bdb_cache_entryinfo_unlock( cx->ei );
1014 ldap_pvt_thread_yield();
1015 bdb_cache_entryinfo_lock( cx->ei );
1016 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
1021 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
1023 bdb_cache_entryinfo_unlock( cx->ei );
1025 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
1026 cx->bdb->bi_db_opflags );
1030 cx->data.data = &cx->dbuf;
1031 cx->data.ulen = sizeof(ID);
1032 cx->data.dlen = sizeof(ID);
1033 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
1035 /* The first item holds the parent ID. Ignore it. */
1036 cx->key.data = &cx->nid;
1037 cx->key.size = sizeof(ID);
1038 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
1040 cx->dbc->c_close( cx->dbc );
1044 /* If the on-disk count is zero we've never checked it.
1048 cx->dbc->c_count( cx->dbc, &dkids, 0 );
1049 cx->ei->bei_dkids = dkids;
1052 cx->data.data = cx->buf;
1053 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1054 cx->data.flags = DB_DBT_USERMEM;
1057 /* Fetch the rest of the IDs in a loop... */
1058 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1059 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1063 DB_MULTIPLE_INIT( ptr, &cx->data );
1065 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
1068 diskNode *d = (diskNode *)j;
1071 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
1072 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
1073 ei.bei_nrdn.bv_len = nrlen;
1074 /* nrdn/rdn are set in-place.
1075 * hdb_cache_load will copy them as needed
1077 ei.bei_nrdn.bv_val = d->nrdn;
1078 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1079 - ei.bei_nrdn.bv_len;
1080 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1081 bdb_idl_append_one( cx->tmp, ei.bei_id );
1082 hdb_cache_load( cx->bdb, &ei, &ei2 );
1088 cx->rc = cx->dbc->c_close( cx->dbc );
1090 bdb_cache_entryinfo_lock( cx->ei );
1091 cx->ei->bei_state ^= CACHE_ENTRY_ONELEVEL;
1092 bdb_cache_entryinfo_unlock( cx->ei );
1097 /* The in-memory cache is in sync with the on-disk data.
1098 * do we have any kids?
1102 if ( cx->ei->bei_ckids > 0 ) {
1103 /* Walk the kids tree; order is irrelevant since bdb_idl_sort
1104 * will sort it later.
1106 avl_apply( cx->ei->bei_kids, apply_func,
1107 cx->tmp, -1, AVL_POSTORDER );
1109 bdb_cache_entryinfo_unlock( cx->ei );
1112 if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1113 bdb_idl_sort( cx->tmp, cx->buf );
1114 if ( cx->bdb->bi_idl_cache_max_size && !BDB_IDL_IS_ZERO( cx->tmp )) {
1115 char *ptr = ((char *)&cx->id)-1;
1117 cx->key.size = sizeof(ID)+1;
1118 *ptr = DN_ONE_PREFIX;
1119 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1123 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1124 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1125 bdb_idl_append( cx->ids, cx->tmp );
1127 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1129 EntryInfo *ei = cx->ei;
1131 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1132 cx->op->o_tmpmemctx );
1133 BDB_IDL_CPY( save, cx->tmp );
1137 for ( cx->id = bdb_idl_first( save, &idcurs );
1139 cx->id = bdb_idl_next( save, &idcurs )) {
1140 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
1142 ( cx->ei->bei_state & CACHE_ENTRY_NO_KIDS ))
1145 BDB_ID2DISK( cx->id, &cx->nid );
1146 hdb_dn2idl_internal( cx );
1147 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1151 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1152 if ( nokids ) ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1154 /* Make sure caller knows it had kids! */
1159 BDB_IDL_CPY( cx->ids, cx->tmp );
1172 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1173 struct dn2id_cookie cx;
1175 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1176 e->e_nname.bv_val, 0, 0 );
1178 #ifndef BDB_MULTIPLE_SUFFIXES
1179 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1180 BEI(e)->bei_parent->bei_id == 0 )
1182 BDB_IDL_ALL( bdb, ids );
1188 BDB_ID2DISK( cx.id, &cx.nid );
1189 cx.ei = e->e_id ? BEI(e) : &bdb->bi_cache.c_dntree;
1191 cx.db = cx.bdb->bi_dn2id->bdi_db;
1192 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1193 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1196 cx.buf = stack + BDB_IDL_UM_SIZE;
1201 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1205 BDB_IDL_ZERO( ids );
1207 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1208 return LDAP_SUCCESS;
1211 cx.key.ulen = sizeof(ID);
1212 cx.key.size = sizeof(ID);
1213 cx.key.flags = DB_DBT_USERMEM;
1217 hdb_dn2idl_internal(&cx);
1218 if ( cx.need_sort ) {
1219 char *ptr = ((char *)&cx.id)-1;
1220 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1221 bdb_idl_sort( cx.ids, cx.tmp );
1223 cx.key.size = sizeof(ID)+1;
1226 bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1229 if ( cx.rc == DB_NOTFOUND )
1230 cx.rc = LDAP_SUCCESS;
1234 #endif /* BDB_HIER */