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>
34 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
35 DB *db = bdb->bi_dn2id->bdi_db;
40 struct berval ptr, pdn;
42 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
43 e->e_ndn, (long) e->e_id, 0 );
44 assert( e->e_id != NOID );
47 key.size = e->e_nname.bv_len + 2;
49 key.flags = DB_DBT_USERMEM;
50 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
52 buf[0] = DN_BASE_PREFIX;
54 ptr.bv_len = e->e_nname.bv_len;
55 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
56 ptr.bv_val[ptr.bv_len] = '\0';
60 data.size = sizeof( nid );
61 BDB_ID2DISK( e->e_id, &nid );
63 /* store it -- don't override */
64 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
66 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
67 db_strerror(rc), rc, 0 );
71 #ifndef BDB_MULTIPLE_SUFFIXES
72 if( !be_issuffix( op->o_bd, &ptr ))
75 buf[0] = DN_SUBTREE_PREFIX;
76 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
78 Debug( LDAP_DEBUG_ANY,
79 "=> bdb_dn2id_add: subtree (%s) put failed: %d\n",
84 #ifdef BDB_MULTIPLE_SUFFIXES
85 if( !be_issuffix( op->o_bd, &ptr ))
88 dnParent( &ptr, &pdn );
90 key.size = pdn.bv_len + 2;
92 pdn.bv_val[-1] = DN_ONE_PREFIX;
93 key.data = pdn.bv_val-1;
96 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
99 Debug( LDAP_DEBUG_ANY,
100 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
106 #ifndef BDB_MULTIPLE_SUFFIXES
107 while( !be_issuffix( op->o_bd, &ptr ))
112 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
114 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
117 Debug( LDAP_DEBUG_ANY,
118 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
122 #ifdef BDB_MULTIPLE_SUFFIXES
123 if( be_issuffix( op->o_bd, &ptr )) break;
125 dnParent( &ptr, &pdn );
127 key.size = pdn.bv_len + 2;
129 key.data = pdn.bv_val - 1;
135 op->o_tmpfree( buf, op->o_tmpmemctx );
136 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
147 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
148 DB *db = bdb->bi_dn2id->bdi_db;
152 struct berval pdn, ptr;
154 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
155 e->e_ndn, e->e_id, 0 );
158 key.size = e->e_nname.bv_len + 2;
159 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
161 key.flags = DB_DBT_USERMEM;
162 buf[0] = DN_BASE_PREFIX;
164 ptr.bv_len = e->e_nname.bv_len;
165 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
166 ptr.bv_val[ptr.bv_len] = '\0';
169 rc = db->del( db, txn, &key, 0 );
171 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
172 db_strerror(rc), rc, 0 );
176 #ifndef BDB_MULTIPLE_SUFFIXES
177 if( !be_issuffix( op->o_bd, &ptr ))
180 buf[0] = DN_SUBTREE_PREFIX;
181 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
183 Debug( LDAP_DEBUG_ANY,
184 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
189 #ifdef BDB_MULTIPLE_SUFFIXES
190 if( !be_issuffix( op->o_bd, &ptr ))
193 dnParent( &ptr, &pdn );
195 key.size = pdn.bv_len + 2;
197 pdn.bv_val[-1] = DN_ONE_PREFIX;
198 key.data = pdn.bv_val - 1;
201 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
204 Debug( LDAP_DEBUG_ANY,
205 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
211 #ifndef BDB_MULTIPLE_SUFFIXES
212 while( !be_issuffix( op->o_bd, &ptr ))
217 ptr.bv_val[-1] = 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: subtree (%s) delete failed: %d\n",
226 #ifdef BDB_MULTIPLE_SUFFIXES
227 if( be_issuffix( op->o_bd, &ptr )) break;
229 dnParent( &ptr, &pdn );
231 key.size = pdn.bv_len + 2;
233 key.data = pdn.bv_val - 1;
239 op->o_tmpfree( buf, op->o_tmpmemctx );
240 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
251 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
252 DB *db = bdb->bi_dn2id->bdi_db;
257 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id(\"%s\")\n", dn->bv_val, 0, 0 );
259 key.size = dn->bv_len + 2;
260 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
261 ((char *)key.data)[0] = DN_BASE_PREFIX;
262 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
267 data.ulen = sizeof(ID);
268 data.flags = DB_DBT_USERMEM;
271 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
274 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
275 db_strerror( rc ), rc, 0 );
277 BDB_DISK2ID( &nid, &ei->bei_id );
278 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
282 op->o_tmpfree( key.data, op->o_tmpmemctx );
293 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
294 DB *db = bdb->bi_dn2id->bdi_db;
298 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
299 e->e_nname.bv_val, 0, 0 );
301 key.size = e->e_nname.bv_len + 2;
302 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
303 ((char *)key.data)[0] = DN_ONE_PREFIX;
304 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
306 if ( bdb->bi_idl_cache_size ) {
307 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
308 if ( rc != LDAP_NO_SUCH_OBJECT ) {
309 op->o_tmpfree( key.data, op->o_tmpmemctx );
313 /* we actually could do a empty get... */
316 data.ulen = sizeof(id);
317 data.flags = DB_DBT_USERMEM;
319 data.dlen = sizeof(id);
321 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
322 op->o_tmpfree( key.data, op->o_tmpmemctx );
324 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
326 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
327 db_strerror(rc) ), rc );
342 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
343 DB *db = bdb->bi_dn2id->bdi_db;
344 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
345 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
347 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
348 e->e_nname.bv_val, 0, 0 );
350 #ifndef BDB_MULTIPLE_SUFFIXES
351 if ( prefix == DN_SUBTREE_PREFIX && BEI(e)->bei_parent->bei_id == 0 ) {
352 BDB_IDL_ALL(bdb, ids);
358 key.size = e->e_nname.bv_len + 2;
360 key.flags = DB_DBT_USERMEM;
361 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
362 ((char *)key.data)[0] = prefix;
363 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
366 rc = bdb_idl_fetch_key( op->o_bd, db, locker, &key, ids, NULL, 0 );
369 Debug( LDAP_DEBUG_TRACE,
370 "<= bdb_dn2idl: get failed: %s (%d)\n",
371 db_strerror( rc ), rc, 0 );
374 Debug( LDAP_DEBUG_TRACE,
375 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
377 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
380 op->o_tmpfree( key.data, op->o_tmpmemctx );
385 /* Management routines for a hierarchically structured database.
387 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
388 * entry in this database is a struct diskNode, keyed by entryID and with
389 * the data containing the RDN and entryID of the node's children. We use
390 * a B-Tree with sorted duplicates to store all the children of a node under
391 * the same key. Also, the first item under the key contains the entry's own
392 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
393 * well as top-down. To keep this info first in the list, the high bit of all
394 * subsequent nrdnlen's is always set. This means we can only accomodate
395 * RDNs up to length 32767, but that's fine since full DNs are already
396 * restricted to 8192.
398 * The diskNode is a variable length structure. This definition is not
399 * directly usable for in-memory manipulation.
401 typedef struct diskNode {
402 unsigned char nrdnlen[2];
404 char rdn[1]; /* variable placement */
405 unsigned char entryID[sizeof(ID)]; /* variable placement */
408 /* This function constructs a full DN for a given entry.
415 int rlen = 0, nrlen = 0;
422 /* count length of all DN components */
423 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
424 rlen += ei->bei_rdn.bv_len + 1;
425 nrlen += ei->bei_nrdn.bv_len + 1;
426 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
429 /* See if the entry DN was invalidated by a subtree rename */
431 if ( BEI(e)->bei_modrdns >= max ) {
434 /* We found a mismatch, tell the caller to lock it */
435 if ( checkit == 1 ) {
438 /* checkit == 2. do the fix. */
439 free( e->e_name.bv_val );
440 free( e->e_nname.bv_val );
443 e->e_name.bv_len = rlen - 1;
444 e->e_nname.bv_len = nrlen - 1;
445 e->e_name.bv_val = ch_malloc(rlen);
446 e->e_nname.bv_val = ch_malloc(nrlen);
447 ptr = e->e_name.bv_val;
448 nptr = e->e_nname.bv_val;
449 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
450 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
451 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
452 if ( ei->bei_parent ) {
457 BEI(e)->bei_modrdns = max;
464 /* We add two elements to the DN2ID database - a data item under the parent's
465 * entryID containing the child's RDN and entryID, and an item under the
466 * child's entryID containing the parent's entryID.
475 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
476 DB *db = bdb->bi_dn2id->bdi_db;
483 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
485 rlen = dn_rdnlen( op->o_bd, &e->e_name );
487 nrlen = e->e_nname.bv_len;
488 rlen = e->e_name.bv_len;
491 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
492 d->nrdnlen[1] = nrlen & 0xff;
493 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
494 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
496 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
498 BDB_ID2DISK( e->e_id, ptr );
502 key.size = sizeof(ID);
503 key.flags = DB_DBT_USERMEM;
504 BDB_ID2DISK( eip->bei_id, &nid );
508 /* Need to make dummy root node once. Subsequent attempts
509 * will fail harmlessly.
511 if ( eip->bei_id == 0 ) {
512 diskNode dummy = {{0, 0}, "", "", ""};
514 data.size = sizeof(diskNode);
515 data.flags = DB_DBT_USERMEM;
517 db->put( db, txn, &key, &data, DB_NODUPDATA );
521 data.size = sizeof(diskNode) + rlen + nrlen;
522 data.flags = DB_DBT_USERMEM;
524 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
527 BDB_ID2DISK( e->e_id, &nid );
528 BDB_ID2DISK( eip->bei_id, ptr );
529 d->nrdnlen[0] ^= 0x80;
531 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
534 /* Update all parents' IDL cache entries */
535 if ( rc == 0 && bdb->bi_idl_cache_size ) {
537 char *ptr = ((char *)&tmp[1])-1;
539 key.size = sizeof(ID)+1;
540 tmp[1] = eip->bei_id;
541 *ptr = DN_ONE_PREFIX;
542 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
543 *ptr = DN_SUBTREE_PREFIX;
544 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
545 tmp[1] = eip->bei_id;
546 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
549 op->o_tmpfree( d, op->o_tmpmemctx );
561 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
562 DB *db = bdb->bi_dn2id->bdi_db;
568 unsigned char dlen[2];
571 key.size = sizeof(ID);
573 key.flags = DB_DBT_USERMEM;
574 BDB_ID2DISK( eip->bei_id, &nid );
577 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
578 data.ulen = data.size;
579 data.dlen = data.size;
580 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
583 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
586 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
587 d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
588 d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
589 dlen[0] = d->nrdnlen[0];
590 dlen[1] = d->nrdnlen[1];
591 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
594 /* Delete our ID from the parent's list */
595 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
597 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
598 !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
599 rc = cursor->c_del( cursor, 0 );
604 /* Delete our ID from the tree. With sorted duplicates, this
605 * will leave any child nodes still hanging around. This is OK
606 * for modrdn, which will add our info back in later.
609 BDB_ID2DISK( e->e_id, &nid );
610 rc = cursor->c_get( cursor, &key, &data, DB_SET );
612 rc = cursor->c_del( cursor, 0 );
614 cursor->c_close( cursor );
615 op->o_tmpfree( d, op->o_tmpmemctx );
617 /* Delete IDL cache entries */
618 if ( rc == 0 && bdb->bi_idl_cache_size ) {
620 char *ptr = ((char *)&tmp[1])-1;
622 key.size = sizeof(ID)+1;
623 tmp[1] = eip->bei_id;
624 *ptr = DN_ONE_PREFIX;
625 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
626 *ptr = DN_SUBTREE_PREFIX;
627 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
628 tmp[1] = eip->bei_id;
629 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
643 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
644 DB *db = bdb->bi_dn2id->bdi_db;
650 unsigned char dlen[2];
653 nrlen = dn_rdnlen( op->o_bd, in );
654 if (!nrlen) nrlen = in->bv_len;
657 key.size = sizeof(ID);
659 key.ulen = sizeof(ID);
660 key.flags = DB_DBT_USERMEM;
661 parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
662 BDB_ID2DISK( parentID, &idp );
665 data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
666 data.ulen = data.size * 3;
667 data.dlen = data.ulen;
668 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
670 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
673 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
674 d->nrdnlen[1] = nrlen & 0xff;
675 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
676 dlen[0] = d->nrdnlen[0];
677 dlen[1] = d->nrdnlen[1];
678 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
682 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
683 if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
684 strncmp( d->nrdn, in->bv_val, nrlen ))) {
688 ptr = (char *) data.data + data.size - sizeof(ID);
689 BDB_DISK2ID( ptr, &ei->bei_id );
690 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
691 ptr = d->nrdn + nrlen + 1;
692 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
693 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
695 /* How many children does the parent have? */
696 /* FIXME: do we need to lock the parent
697 * entryinfo? Seems safe...
699 cursor->c_count( cursor, &dkids, 0 );
700 ei->bei_parent->bei_dkids = dkids;
703 cursor->c_close( cursor );
704 op->o_tmpfree( d, op->o_tmpmemctx );
717 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
718 DB *db = bdb->bi_dn2id->bdi_db;
727 key.size = sizeof(ID);
729 key.ulen = sizeof(ID);
730 key.flags = DB_DBT_USERMEM;
731 BDB_ID2DISK( ei->bei_id, &nid );
734 data.flags = DB_DBT_USERMEM;
736 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
738 if ( !txn && locker ) {
739 cursor->locker = locker;
742 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
743 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
746 rc = cursor->c_get( cursor, &key, &data, DB_SET );
748 if (d->nrdnlen[0] & 0x80) {
752 ptr = (char *) data.data + data.size - sizeof(ID);
753 BDB_DISK2ID( ptr, idp );
754 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
755 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
756 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
758 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
759 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
760 /* How many children does this node have? */
761 cursor->c_count( cursor, &dkids, 0 );
762 ei->bei_dkids = dkids;
765 cursor->c_close( cursor );
766 op->o_tmpfree( d, op->o_tmpmemctx );
776 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
777 DB *db = bdb->bi_dn2id->bdi_db;
785 key.size = sizeof(ID);
787 key.flags = DB_DBT_USERMEM;
788 BDB_ID2DISK( e->e_id, &id );
790 /* IDL cache is in host byte order */
791 if ( bdb->bi_idl_cache_size ) {
792 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
793 if ( rc != LDAP_NO_SUCH_OBJECT ) {
801 data.ulen = sizeof(d);
802 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
803 data.dlen = sizeof(d);
805 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
808 rc = cursor->c_get( cursor, &key, &data, DB_SET );
811 rc = cursor->c_count( cursor, &dkids, 0 );
813 BEI(e)->bei_dkids = dkids;
814 if ( dkids < 2 ) rc = DB_NOTFOUND;
817 cursor->c_close( cursor );
822 * We can't just use bdb_idl_fetch_key because
823 * 1 - our data items are longer than just an entry ID
824 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
826 * We descend the tree recursively, so we define this cookie
827 * to hold our necessary state information. The bdb_dn2idl_internal
828 * function uses this cookie when calling itself.
831 struct dn2id_cookie {
832 struct bdb_info *bdb;
857 EntryInfo *ei = data;
860 bdb_idl_append_one( idl, ei->bei_id );
866 struct dn2id_cookie *cx
869 BDB_IDL_ZERO( cx->tmp );
871 if ( cx->bdb->bi_idl_cache_size ) {
872 char *ptr = ((char *)&cx->id)-1;
875 cx->key.size = sizeof(ID)+1;
876 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
877 ID *ids = cx->depth ? cx->tmp : cx->ids;
879 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
880 if ( cx->rc == LDAP_SUCCESS ) {
882 bdb_idl_append( cx->ids, cx->tmp );
888 *ptr = DN_ONE_PREFIX;
889 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
890 if ( cx->rc == LDAP_SUCCESS ) {
893 if ( cx->rc == DB_NOTFOUND ) {
898 bdb_cache_entryinfo_lock( cx->ei );
900 /* If number of kids in the cache differs from on-disk, load
901 * up all the kids from the database
903 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
905 db_recno_t dkids = cx->ei->bei_dkids;
906 ei.bei_parent = cx->ei;
908 /* Only one thread should load the cache */
909 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
910 bdb_cache_entryinfo_unlock( cx->ei );
911 ldap_pvt_thread_yield();
912 bdb_cache_entryinfo_lock( cx->ei );
913 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
918 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
920 bdb_cache_entryinfo_unlock( cx->ei );
922 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
923 cx->bdb->bi_db_opflags );
927 cx->data.data = &cx->dbuf;
928 cx->data.ulen = sizeof(ID);
929 cx->data.dlen = sizeof(ID);
930 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
932 /* The first item holds the parent ID. Ignore it. */
933 cx->key.data = &cx->nid;
934 cx->key.size = sizeof(ID);
935 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
937 cx->dbc->c_close( cx->dbc );
941 /* If the on-disk count is zero we've never checked it.
945 cx->dbc->c_count( cx->dbc, &dkids, 0 );
946 cx->ei->bei_dkids = dkids;
949 cx->data.data = cx->buf;
950 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
951 cx->data.flags = DB_DBT_USERMEM;
954 /* Fetch the rest of the IDs in a loop... */
955 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
956 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
960 DB_MULTIPLE_INIT( ptr, &cx->data );
962 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
965 diskNode *d = (diskNode *)j;
968 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
969 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
970 ei.bei_nrdn.bv_len = nrlen;
971 /* nrdn/rdn are set in-place.
972 * hdb_cache_load will copy them as needed
974 ei.bei_nrdn.bv_val = d->nrdn;
975 ei.bei_rdn.bv_len = len - sizeof(diskNode)
976 - ei.bei_nrdn.bv_len;
977 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
978 bdb_idl_append_one( cx->tmp, ei.bei_id );
979 hdb_cache_load( cx->bdb, &ei, &ei2 );
985 cx->rc = cx->dbc->c_close( cx->dbc );
987 bdb_cache_entryinfo_lock( cx->ei );
988 cx->ei->bei_state ^= CACHE_ENTRY_ONELEVEL;
989 bdb_cache_entryinfo_unlock( cx->ei );
994 /* The in-memory cache is in sync with the on-disk data.
995 * do we have any kids?
999 if ( cx->ei->bei_ckids > 0 ) {
1000 /* Walk the kids tree; order is irrelevant since bdb_idl_sort
1001 * will sort it later.
1003 avl_apply( cx->ei->bei_kids, apply_func,
1004 cx->tmp, -1, AVL_POSTORDER );
1006 bdb_cache_entryinfo_unlock( cx->ei );
1009 if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1010 bdb_idl_sort( cx->tmp, cx->buf );
1011 if ( cx->bdb->bi_idl_cache_max_size && !BDB_IDL_IS_ZERO( cx->tmp )) {
1012 char *ptr = ((char *)&cx->id)-1;
1014 cx->key.size = sizeof(ID)+1;
1015 *ptr = DN_ONE_PREFIX;
1016 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1020 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1021 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1022 bdb_idl_append( cx->ids, cx->tmp );
1024 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1026 EntryInfo *ei = cx->ei;
1028 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1029 cx->op->o_tmpmemctx );
1030 BDB_IDL_CPY( save, cx->tmp );
1034 for ( cx->id = bdb_idl_first( save, &idcurs );
1036 cx->id = bdb_idl_next( save, &idcurs )) {
1037 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
1039 ( cx->ei->bei_state & CACHE_ENTRY_NO_KIDS ))
1042 BDB_ID2DISK( cx->id, &cx->nid );
1043 hdb_dn2idl_internal( cx );
1044 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1048 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1049 if ( nokids ) ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1051 /* Make sure caller knows it had kids! */
1056 BDB_IDL_CPY( cx->ids, cx->tmp );
1070 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1071 struct dn2id_cookie cx;
1073 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1074 e->e_nname.bv_val, 0, 0 );
1076 #ifndef BDB_MULTIPLE_SUFFIXES
1077 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1078 BEI(e)->bei_parent->bei_id == 0 )
1080 BDB_IDL_ALL( bdb, ids );
1086 BDB_ID2DISK( cx.id, &cx.nid );
1087 cx.ei = e->e_id ? BEI(e) : &bdb->bi_cache.c_dntree;
1089 cx.db = cx.bdb->bi_dn2id->bdi_db;
1090 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1091 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1094 cx.buf = stack + BDB_IDL_UM_SIZE;
1100 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1104 BDB_IDL_ZERO( ids );
1106 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1107 return LDAP_SUCCESS;
1110 cx.key.ulen = sizeof(ID);
1111 cx.key.size = sizeof(ID);
1112 cx.key.flags = DB_DBT_USERMEM;
1116 hdb_dn2idl_internal(&cx);
1117 if ( cx.need_sort ) {
1118 char *ptr = ((char *)&cx.id)-1;
1119 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1120 bdb_idl_sort( cx.ids, cx.tmp );
1122 cx.key.size = sizeof(ID)+1;
1125 if ( cx.bdb->bi_idl_cache_max_size )
1126 bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1129 if ( cx.rc == DB_NOTFOUND )
1130 cx.rc = LDAP_SUCCESS;
1134 #endif /* BDB_HIER */