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-2005 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 = db->del( db, txn, &key, 0 );
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 );
341 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
342 DB *db = bdb->bi_dn2id->bdi_db;
343 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
344 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
346 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
347 e->e_nname.bv_val, 0, 0 );
349 #ifndef BDB_MULTIPLE_SUFFIXES
350 if ( prefix == DN_SUBTREE_PREFIX && BEI(e)->bei_parent->bei_id == 0 ) {
351 BDB_IDL_ALL(bdb, ids);
357 key.size = e->e_nname.bv_len + 2;
359 key.flags = DB_DBT_USERMEM;
360 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
361 ((char *)key.data)[0] = prefix;
362 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
365 rc = bdb_idl_fetch_key( op->o_bd, db, NULL, &key, ids, NULL, 0 );
368 Debug( LDAP_DEBUG_TRACE,
369 "<= bdb_dn2idl: get failed: %s (%d)\n",
370 db_strerror( rc ), rc, 0 );
373 Debug( LDAP_DEBUG_TRACE,
374 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
376 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
379 op->o_tmpfree( key.data, op->o_tmpmemctx );
384 /* Management routines for a hierarchically structured database.
386 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
387 * entry in this database is a struct diskNode, keyed by entryID and with
388 * the data containing the RDN and entryID of the node's children. We use
389 * a B-Tree with sorted duplicates to store all the children of a node under
390 * the same key. Also, the first item under the key contains the entry's own
391 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
392 * well as top-down. To keep this info first in the list, the high bit of all
393 * subsequent nrdnlen's is always set. This means we can only accomodate
394 * RDNs up to length 32767, but that's fine since full DNs are already
395 * restricted to 8192.
397 * The diskNode is a variable length structure. This definition is not
398 * directly usable for in-memory manipulation.
400 typedef struct diskNode {
401 unsigned char nrdnlen[2];
403 char rdn[1]; /* variable placement */
404 unsigned char entryID[sizeof(ID)]; /* variable placement */
407 /* This function constructs a full DN for a given entry.
414 int rlen = 0, nrlen = 0;
421 /* count length of all DN components */
422 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
423 rlen += ei->bei_rdn.bv_len + 1;
424 nrlen += ei->bei_nrdn.bv_len + 1;
425 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
428 /* See if the entry DN was invalidated by a subtree rename */
430 if ( BEI(e)->bei_modrdns >= max ) {
433 /* We found a mismatch, tell the caller to lock it */
434 if ( checkit == 1 ) {
437 /* checkit == 2. do the fix. */
438 free( e->e_name.bv_val );
439 free( e->e_nname.bv_val );
442 e->e_name.bv_len = rlen - 1;
443 e->e_nname.bv_len = nrlen - 1;
444 e->e_name.bv_val = ch_malloc(rlen);
445 e->e_nname.bv_val = ch_malloc(nrlen);
446 ptr = e->e_name.bv_val;
447 nptr = e->e_nname.bv_val;
448 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
449 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
450 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
451 if ( ei->bei_parent ) {
456 BEI(e)->bei_modrdns = max;
463 /* We add two elements to the DN2ID database - a data item under the parent's
464 * entryID containing the child's RDN and entryID, and an item under the
465 * child's entryID containing the parent's entryID.
474 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
475 DB *db = bdb->bi_dn2id->bdi_db;
482 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
484 rlen = dn_rdnlen( op->o_bd, &e->e_name );
486 nrlen = e->e_nname.bv_len;
487 rlen = e->e_name.bv_len;
490 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
491 d->nrdnlen[1] = nrlen & 0xff;
492 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
493 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
495 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
497 BDB_ID2DISK( e->e_id, ptr );
501 key.size = sizeof(ID);
502 key.flags = DB_DBT_USERMEM;
503 BDB_ID2DISK( eip->bei_id, &nid );
507 /* Need to make dummy root node once. Subsequent attempts
508 * will fail harmlessly.
510 if ( eip->bei_id == 0 ) {
511 diskNode dummy = {{0, 0}, "", "", ""};
513 data.size = sizeof(diskNode);
514 data.flags = DB_DBT_USERMEM;
516 db->put( db, txn, &key, &data, DB_NODUPDATA );
520 data.size = sizeof(diskNode) + rlen + nrlen;
521 data.flags = DB_DBT_USERMEM;
523 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
526 BDB_ID2DISK( e->e_id, &nid );
527 BDB_ID2DISK( eip->bei_id, ptr );
528 d->nrdnlen[0] ^= 0x80;
530 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
533 /* Update all parents' IDL cache entries */
534 if ( rc == 0 && bdb->bi_idl_cache_size ) {
536 char *ptr = ((char *)&tmp[1])-1;
538 key.size = sizeof(ID)+1;
539 tmp[1] = eip->bei_id;
540 *ptr = DN_ONE_PREFIX;
541 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
542 *ptr = DN_SUBTREE_PREFIX;
543 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
544 tmp[1] = eip->bei_id;
545 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
548 op->o_tmpfree( d, op->o_tmpmemctx );
560 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
561 DB *db = bdb->bi_dn2id->bdi_db;
567 unsigned char dlen[2];
570 key.size = sizeof(ID);
572 key.flags = DB_DBT_USERMEM;
573 BDB_ID2DISK( eip->bei_id, &nid );
576 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
577 data.ulen = data.size;
578 data.dlen = data.size;
579 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
582 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
585 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
586 d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
587 d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
588 dlen[0] = d->nrdnlen[0];
589 dlen[1] = d->nrdnlen[1];
590 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
593 /* Delete our ID from the parent's list */
594 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
596 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
597 !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
598 rc = cursor->c_del( cursor, 0 );
603 /* Delete our ID from the tree. With sorted duplicates, this
604 * will leave any child nodes still hanging around. This is OK
605 * for modrdn, which will add our info back in later.
608 BDB_ID2DISK( e->e_id, &nid );
609 rc = cursor->c_get( cursor, &key, &data, DB_SET );
611 rc = cursor->c_del( cursor, 0 );
613 cursor->c_close( cursor );
614 op->o_tmpfree( d, op->o_tmpmemctx );
616 /* Delete IDL cache entries */
617 if ( rc == 0 && bdb->bi_idl_cache_size ) {
619 char *ptr = ((char *)&tmp[1])-1;
621 key.size = sizeof(ID)+1;
622 tmp[1] = eip->bei_id;
623 *ptr = DN_ONE_PREFIX;
624 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
625 *ptr = DN_SUBTREE_PREFIX;
626 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
627 tmp[1] = eip->bei_id;
628 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
642 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
643 DB *db = bdb->bi_dn2id->bdi_db;
649 unsigned char dlen[2];
652 nrlen = dn_rdnlen( op->o_bd, in );
653 if (!nrlen) nrlen = in->bv_len;
656 key.size = sizeof(ID);
658 key.ulen = sizeof(ID);
659 key.flags = DB_DBT_USERMEM;
660 parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
661 BDB_ID2DISK( parentID, &idp );
664 data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
665 data.ulen = data.size * 3;
666 data.dlen = data.ulen;
667 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
669 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
672 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
673 d->nrdnlen[1] = nrlen & 0xff;
674 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
675 dlen[0] = d->nrdnlen[0];
676 dlen[1] = d->nrdnlen[1];
677 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
681 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
682 if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
683 strncmp( d->nrdn, in->bv_val, nrlen ))) {
687 ptr = (char *) data.data + data.size - sizeof(ID);
688 BDB_DISK2ID( ptr, &ei->bei_id );
689 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
690 ptr = d->nrdn + nrlen + 1;
691 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
692 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
694 /* How many children does the parent have? */
695 /* FIXME: do we need to lock the parent
696 * entryinfo? Seems safe...
698 cursor->c_count( cursor, &dkids, 0 );
699 ei->bei_parent->bei_dkids = dkids;
702 cursor->c_close( cursor );
703 op->o_tmpfree( d, op->o_tmpmemctx );
716 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
717 DB *db = bdb->bi_dn2id->bdi_db;
726 key.size = sizeof(ID);
728 key.ulen = sizeof(ID);
729 key.flags = DB_DBT_USERMEM;
730 BDB_ID2DISK( ei->bei_id, &nid );
733 data.flags = DB_DBT_USERMEM;
735 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
737 if ( !txn && locker ) {
738 cursor->locker = locker;
741 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
742 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
745 rc = cursor->c_get( cursor, &key, &data, DB_SET );
747 if (d->nrdnlen[0] & 0x80) {
751 ptr = (char *) data.data + data.size - sizeof(ID);
752 BDB_DISK2ID( ptr, idp );
753 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
754 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
755 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
757 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
758 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
759 /* How many children does this node have? */
760 cursor->c_count( cursor, &dkids, 0 );
761 ei->bei_dkids = dkids;
764 cursor->c_close( cursor );
765 op->o_tmpfree( d, op->o_tmpmemctx );
775 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
776 DB *db = bdb->bi_dn2id->bdi_db;
784 key.size = sizeof(ID);
786 key.flags = DB_DBT_USERMEM;
787 BDB_ID2DISK( e->e_id, &id );
789 /* IDL cache is in host byte order */
790 if ( bdb->bi_idl_cache_size ) {
791 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
792 if ( rc != LDAP_NO_SUCH_OBJECT ) {
800 data.ulen = sizeof(d);
801 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
802 data.dlen = sizeof(d);
804 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
807 rc = cursor->c_get( cursor, &key, &data, DB_SET );
810 rc = cursor->c_count( cursor, &dkids, 0 );
812 BEI(e)->bei_dkids = dkids;
813 if ( dkids < 2 ) rc = DB_NOTFOUND;
816 cursor->c_close( cursor );
821 * We can't just use bdb_idl_fetch_key because
822 * 1 - our data items are longer than just an entry ID
823 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
825 * We descend the tree recursively, so we define this cookie
826 * to hold our necessary state information. The bdb_dn2idl_internal
827 * function uses this cookie when calling itself.
830 struct dn2id_cookie {
831 struct bdb_info *bdb;
855 EntryInfo *ei = data;
858 bdb_idl_append_one( idl, ei->bei_id );
864 struct dn2id_cookie *cx
867 BDB_IDL_ZERO( cx->tmp );
869 if ( cx->bdb->bi_idl_cache_size ) {
870 char *ptr = ((char *)&cx->id)-1;
873 cx->key.size = sizeof(ID)+1;
874 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
875 ID *ids = cx->depth ? cx->tmp : cx->ids;
877 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
878 if ( cx->rc == LDAP_SUCCESS ) {
880 bdb_idl_append( cx->ids, cx->tmp );
886 *ptr = DN_ONE_PREFIX;
887 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
888 if ( cx->rc == LDAP_SUCCESS ) {
891 if ( cx->rc == DB_NOTFOUND ) {
896 bdb_cache_entryinfo_lock( cx->ei );
898 /* If number of kids in the cache differs from on-disk, load
899 * up all the kids from the database
901 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
903 db_recno_t dkids = cx->ei->bei_dkids;
904 ei.bei_parent = cx->ei;
906 /* Only one thread should load the cache */
907 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
908 bdb_cache_entryinfo_unlock( cx->ei );
909 ldap_pvt_thread_yield();
910 bdb_cache_entryinfo_lock( cx->ei );
911 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
916 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
918 bdb_cache_entryinfo_unlock( cx->ei );
920 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
921 cx->bdb->bi_db_opflags );
925 cx->data.data = &cx->dbuf;
926 cx->data.ulen = sizeof(ID);
927 cx->data.dlen = sizeof(ID);
928 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
930 /* The first item holds the parent ID. Ignore it. */
931 cx->key.data = &cx->nid;
932 cx->key.size = sizeof(ID);
933 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
935 cx->dbc->c_close( cx->dbc );
939 /* If the on-disk count is zero we've never checked it.
943 cx->dbc->c_count( cx->dbc, &dkids, 0 );
944 cx->ei->bei_dkids = dkids;
947 cx->data.data = cx->buf;
948 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
949 cx->data.flags = DB_DBT_USERMEM;
951 /* Fetch the rest of the IDs in a loop... */
952 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
953 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
957 DB_MULTIPLE_INIT( ptr, &cx->data );
959 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
962 diskNode *d = (diskNode *)j;
965 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
966 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
967 ei.bei_nrdn.bv_len = nrlen;
968 /* nrdn/rdn are set in-place.
969 * hdb_cache_load will copy them as needed
971 ei.bei_nrdn.bv_val = d->nrdn;
972 ei.bei_rdn.bv_len = len - sizeof(diskNode)
973 - ei.bei_nrdn.bv_len;
974 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
975 bdb_idl_append_one( cx->tmp, ei.bei_id );
976 hdb_cache_load( cx->bdb, &ei, &ei2 );
980 cx->rc = cx->dbc->c_close( cx->dbc );
982 bdb_cache_entryinfo_lock( cx->ei );
983 cx->ei->bei_state ^= CACHE_ENTRY_ONELEVEL;
984 bdb_cache_entryinfo_unlock( cx->ei );
989 /* The in-memory cache is in sync with the on-disk data.
990 * do we have any kids?
994 if ( cx->ei->bei_ckids > 0 ) {
995 /* Walk the kids tree; order is irrelevant since bdb_idl_sort
996 * will sort it later.
998 avl_apply( cx->ei->bei_kids, apply_func,
999 cx->tmp, -1, AVL_POSTORDER );
1001 bdb_cache_entryinfo_unlock( cx->ei );
1004 if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1005 bdb_idl_sort( cx->tmp, cx->buf );
1006 if ( cx->bdb->bi_idl_cache_max_size ) {
1007 char *ptr = ((char *)&cx->id)-1;
1009 cx->key.size = sizeof(ID)+1;
1010 *ptr = DN_ONE_PREFIX;
1011 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1015 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1016 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1017 bdb_idl_append( cx->ids, cx->tmp );
1019 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1021 EntryInfo *ei = cx->ei;
1023 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1024 cx->op->o_tmpmemctx );
1025 BDB_IDL_CPY( save, cx->tmp );
1029 for ( cx->id = bdb_idl_first( save, &idcurs );
1031 cx->id = bdb_idl_next( save, &idcurs )) {
1032 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
1034 ( cx->ei->bei_state & CACHE_ENTRY_NO_KIDS ))
1037 BDB_ID2DISK( cx->id, &cx->nid );
1038 hdb_dn2idl_internal( cx );
1039 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1043 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1044 if ( nokids ) ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1046 /* Make sure caller knows it had kids! */
1051 BDB_IDL_CPY( cx->ids, cx->tmp );
1064 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1065 struct dn2id_cookie cx;
1067 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1068 e->e_nname.bv_val, 0, 0 );
1070 #ifndef BDB_MULTIPLE_SUFFIXES
1071 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1072 BEI(e)->bei_parent->bei_id == 0 )
1074 BDB_IDL_ALL( bdb, ids );
1080 BDB_ID2DISK( cx.id, &cx.nid );
1081 cx.ei = e->e_id ? BEI(e) : &bdb->bi_cache.c_dntree;
1083 cx.db = cx.bdb->bi_dn2id->bdi_db;
1084 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1085 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1088 cx.buf = stack + BDB_IDL_UM_SIZE;
1093 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1097 BDB_IDL_ZERO( ids );
1099 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1100 return LDAP_SUCCESS;
1103 cx.key.ulen = sizeof(ID);
1104 cx.key.size = sizeof(ID);
1105 cx.key.flags = DB_DBT_USERMEM;
1109 hdb_dn2idl_internal(&cx);
1110 if ( cx.need_sort ) {
1111 char *ptr = ((char *)&cx.id)-1;
1112 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1113 bdb_idl_sort( cx.ids, cx.tmp );
1115 cx.key.size = sizeof(ID)+1;
1118 bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1121 if ( cx.rc == DB_NOTFOUND )
1122 cx.rc = LDAP_SUCCESS;
1126 #endif /* BDB_HIER */