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-2011 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 0x%lx: \"%s\"\n",
43 e->e_id, e->e_ndn, 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 char buf[ SLAP_TEXT_BUFLEN ];
67 snprintf( buf, sizeof( buf ), "%s => bdb_dn2id_add dn=\"%s\" ID=0x%lx",
68 op->o_log_prefix, e->e_name.bv_val, e->e_id );
69 Debug( LDAP_DEBUG_ANY, "%s: put failed: %s %d\n",
70 buf, db_strerror(rc), rc );
74 #ifndef BDB_MULTIPLE_SUFFIXES
75 if( !be_issuffix( op->o_bd, &ptr ))
78 buf[0] = DN_SUBTREE_PREFIX;
79 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
81 Debug( LDAP_DEBUG_ANY,
82 "=> bdb_dn2id_add 0x%lx: subtree (%s) put failed: %d\n",
83 e->e_id, ptr.bv_val, rc );
87 #ifdef BDB_MULTIPLE_SUFFIXES
88 if( !be_issuffix( op->o_bd, &ptr ))
91 dnParent( &ptr, &pdn );
93 key.size = pdn.bv_len + 2;
95 pdn.bv_val[-1] = DN_ONE_PREFIX;
96 key.data = pdn.bv_val-1;
99 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
102 Debug( LDAP_DEBUG_ANY,
103 "=> bdb_dn2id_add 0x%lx: parent (%s) insert failed: %d\n",
104 e->e_id, ptr.bv_val, rc );
109 #ifndef BDB_MULTIPLE_SUFFIXES
110 while( !be_issuffix( op->o_bd, &ptr ))
115 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
117 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
120 Debug( LDAP_DEBUG_ANY,
121 "=> bdb_dn2id_add 0x%lx: subtree (%s) insert failed: %d\n",
122 e->e_id, ptr.bv_val, rc );
125 #ifdef BDB_MULTIPLE_SUFFIXES
126 if( be_issuffix( op->o_bd, &ptr )) break;
128 dnParent( &ptr, &pdn );
130 key.size = pdn.bv_len + 2;
132 key.data = pdn.bv_val - 1;
138 op->o_tmpfree( buf, op->o_tmpmemctx );
139 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
150 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
151 DB *db = bdb->bi_dn2id->bdi_db;
155 struct berval pdn, ptr;
158 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete 0x%lx: \"%s\"\n",
159 e->e_id, e->e_ndn, 0 );
162 key.size = e->e_nname.bv_len + 2;
163 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
165 key.flags = DB_DBT_USERMEM;
166 buf[0] = DN_BASE_PREFIX;
168 ptr.bv_len = e->e_nname.bv_len;
169 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
170 ptr.bv_val[ptr.bv_len] = '\0';
173 rc = db->del( db, txn, &key, 0 );
175 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete 0x%lx: delete failed: %s %d\n",
176 e->e_id, db_strerror(rc), rc );
180 #ifndef BDB_MULTIPLE_SUFFIXES
181 if( !be_issuffix( op->o_bd, &ptr ))
184 buf[0] = DN_SUBTREE_PREFIX;
185 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
187 Debug( LDAP_DEBUG_ANY,
188 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
189 e->e_id, ptr.bv_val, rc );
193 #ifdef BDB_MULTIPLE_SUFFIXES
194 if( !be_issuffix( op->o_bd, &ptr ))
197 dnParent( &ptr, &pdn );
199 key.size = pdn.bv_len + 2;
201 pdn.bv_val[-1] = DN_ONE_PREFIX;
202 key.data = pdn.bv_val - 1;
205 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
208 Debug( LDAP_DEBUG_ANY,
209 "=> bdb_dn2id_delete 0x%lx: parent (%s) delete failed: %d\n",
210 e->e_id, ptr.bv_val, rc );
215 #ifndef BDB_MULTIPLE_SUFFIXES
216 while( !be_issuffix( op->o_bd, &ptr ))
221 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
223 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
225 Debug( LDAP_DEBUG_ANY,
226 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
227 e->e_id, ptr.bv_val, rc );
230 #ifdef BDB_MULTIPLE_SUFFIXES
231 if( be_issuffix( op->o_bd, &ptr )) break;
233 dnParent( &ptr, &pdn );
235 key.size = pdn.bv_len + 2;
237 key.data = pdn.bv_val - 1;
243 op->o_tmpfree( buf, op->o_tmpmemctx );
244 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
256 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
257 DB *db = bdb->bi_dn2id->bdi_db;
262 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id(\"%s\")\n", dn->bv_val, 0, 0 );
265 key.size = dn->bv_len + 2;
266 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
267 ((char *)key.data)[0] = DN_BASE_PREFIX;
268 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
273 data.ulen = sizeof(ID);
274 data.flags = DB_DBT_USERMEM;
276 rc = db->cursor( db, txn, cursor, bdb->bi_db_opflags );
280 rc = (*cursor)->c_get( *cursor, &key, &data, DB_SET );
283 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
284 db_strerror( rc ), rc, 0 );
286 BDB_DISK2ID( &nid, &ei->bei_id );
287 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%lx\n",
290 op->o_tmpfree( key.data, op->o_tmpmemctx );
301 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
302 DB *db = bdb->bi_dn2id->bdi_db;
306 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
307 e->e_nname.bv_val, 0, 0 );
309 key.size = e->e_nname.bv_len + 2;
310 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
311 ((char *)key.data)[0] = DN_ONE_PREFIX;
312 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
314 if ( bdb->bi_idl_cache_size ) {
315 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
316 if ( rc != LDAP_NO_SUCH_OBJECT ) {
317 op->o_tmpfree( key.data, op->o_tmpmemctx );
321 /* we actually could do a empty get... */
324 data.ulen = sizeof(id);
325 data.flags = DB_DBT_USERMEM;
327 data.dlen = sizeof(id);
329 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
330 op->o_tmpfree( key.data, op->o_tmpmemctx );
332 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
334 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
335 db_strerror(rc) ), rc );
351 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
352 DB *db = bdb->bi_dn2id->bdi_db;
353 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
354 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
356 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
359 #ifndef BDB_MULTIPLE_SUFFIXES
360 if ( prefix == DN_SUBTREE_PREFIX
361 && ( ei->bei_id == 0 ||
362 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len ))) {
363 BDB_IDL_ALL(bdb, ids);
369 key.size = ndn->bv_len + 2;
371 key.flags = DB_DBT_USERMEM;
372 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
373 ((char *)key.data)[0] = prefix;
374 AC_MEMCPY( &((char *)key.data)[1], ndn->bv_val, key.size - 1 );
377 rc = bdb_idl_fetch_key( op->o_bd, db, txn, &key, ids, NULL, 0 );
380 Debug( LDAP_DEBUG_TRACE,
381 "<= bdb_dn2idl: get failed: %s (%d)\n",
382 db_strerror( rc ), rc, 0 );
385 Debug( LDAP_DEBUG_TRACE,
386 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
388 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
391 op->o_tmpfree( key.data, op->o_tmpmemctx );
396 /* Management routines for a hierarchically structured database.
398 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
399 * entry in this database is a struct diskNode, keyed by entryID and with
400 * the data containing the RDN and entryID of the node's children. We use
401 * a B-Tree with sorted duplicates to store all the children of a node under
402 * the same key. Also, the first item under the key contains the entry's own
403 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
404 * well as top-down. To keep this info first in the list, the high bit of all
405 * subsequent nrdnlen's is always set. This means we can only accomodate
406 * RDNs up to length 32767, but that's fine since full DNs are already
407 * restricted to 8192.
409 * The diskNode is a variable length structure. This definition is not
410 * directly usable for in-memory manipulation.
412 typedef struct diskNode {
413 unsigned char nrdnlen[2];
415 char rdn[1]; /* variable placement */
416 unsigned char entryID[sizeof(ID)]; /* variable placement */
419 /* Sort function for the sorted duplicate data items of a dn2id key.
420 * Sorts based on normalized RDN, in length order.
432 un = (diskNode *)usrkey->data;
433 cn = (diskNode *)curkey->data;
435 /* data is not aligned, cannot compare directly */
436 rc = un->nrdnlen[0] - cn->nrdnlen[0];
438 rc = un->nrdnlen[1] - cn->nrdnlen[1];
441 return strcmp( un->nrdn, cn->nrdn );
444 /* This function constructs a full DN for a given entry.
451 int rlen = 0, nrlen = 0;
458 /* count length of all DN components */
459 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
460 rlen += ei->bei_rdn.bv_len + 1;
461 nrlen += ei->bei_nrdn.bv_len + 1;
462 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
465 /* See if the entry DN was invalidated by a subtree rename */
467 if ( BEI(e)->bei_modrdns >= max ) {
470 /* We found a mismatch, tell the caller to lock it */
471 if ( checkit == 1 ) {
474 /* checkit == 2. do the fix. */
475 free( e->e_name.bv_val );
476 free( e->e_nname.bv_val );
479 e->e_name.bv_len = rlen - 1;
480 e->e_nname.bv_len = nrlen - 1;
481 e->e_name.bv_val = ch_malloc(rlen);
482 e->e_nname.bv_val = ch_malloc(nrlen);
483 ptr = e->e_name.bv_val;
484 nptr = e->e_nname.bv_val;
485 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
486 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
487 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
488 if ( ei->bei_parent ) {
493 BEI(e)->bei_modrdns = max;
494 if ( ptr > e->e_name.bv_val ) ptr[-1] = '\0';
495 if ( nptr > e->e_nname.bv_val ) nptr[-1] = '\0';
500 /* We add two elements to the DN2ID database - a data item under the parent's
501 * entryID containing the child's RDN and entryID, and an item under the
502 * child's entryID containing the parent's entryID.
511 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
512 DB *db = bdb->bi_dn2id->bdi_db;
519 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_add 0x%lx: \"%s\"\n",
520 e->e_id, e->e_ndn, 0 );
522 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
524 rlen = dn_rdnlen( op->o_bd, &e->e_name );
526 nrlen = e->e_nname.bv_len;
527 rlen = e->e_name.bv_len;
530 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
531 d->nrdnlen[1] = nrlen & 0xff;
532 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
533 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
535 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
537 BDB_ID2DISK( e->e_id, ptr );
541 key.size = sizeof(ID);
542 key.flags = DB_DBT_USERMEM;
543 BDB_ID2DISK( eip->bei_id, &nid );
547 /* Need to make dummy root node once. Subsequent attempts
548 * will fail harmlessly.
550 if ( eip->bei_id == 0 ) {
551 diskNode dummy = {{0, 0}, "", "", ""};
553 data.size = sizeof(diskNode);
554 data.flags = DB_DBT_USERMEM;
556 db->put( db, txn, &key, &data, DB_NODUPDATA );
560 data.size = sizeof(diskNode) + rlen + nrlen;
561 data.flags = DB_DBT_USERMEM;
563 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
566 BDB_ID2DISK( e->e_id, &nid );
567 BDB_ID2DISK( eip->bei_id, ptr );
568 d->nrdnlen[0] ^= 0x80;
570 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
573 /* Update all parents' IDL cache entries */
574 if ( rc == 0 && bdb->bi_idl_cache_size ) {
576 char *ptr = ((char *)&tmp[1])-1;
578 key.size = sizeof(ID)+1;
579 tmp[1] = eip->bei_id;
580 *ptr = DN_ONE_PREFIX;
581 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
582 if ( eip->bei_parent ) {
583 *ptr = DN_SUBTREE_PREFIX;
584 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
585 tmp[1] = eip->bei_id;
586 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
588 /* Handle DB with empty suffix */
589 if ( !op->o_bd->be_suffix[0].bv_len && eip ) {
590 tmp[1] = eip->bei_id;
591 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
596 op->o_tmpfree( d, op->o_tmpmemctx );
597 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
609 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
610 DB *db = bdb->bi_dn2id->bdi_db;
616 unsigned char dlen[2];
619 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n",
620 e->e_id, e->e_ndn, 0 );
623 key.size = sizeof(ID);
625 key.flags = DB_DBT_USERMEM;
626 BDB_ID2DISK( eip->bei_id, &nid );
629 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
630 data.ulen = data.size;
631 data.dlen = data.size;
632 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
636 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
637 d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
638 d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
639 dlen[0] = d->nrdnlen[0];
640 dlen[1] = d->nrdnlen[1];
641 memcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val, BEI(e)->bei_nrdn.bv_len+1 );
644 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
645 if ( rc ) goto func_leave;
647 /* Delete our ID from the parent's list */
648 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
650 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
651 !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
652 rc = cursor->c_del( cursor, 0 );
657 /* Delete our ID from the tree. With sorted duplicates, this
658 * will leave any child nodes still hanging around. This is OK
659 * for modrdn, which will add our info back in later.
662 BDB_ID2DISK( e->e_id, &nid );
663 rc = cursor->c_get( cursor, &key, &data, DB_SET );
665 rc = cursor->c_del( cursor, 0 );
668 cursor->c_close( cursor );
670 op->o_tmpfree( d, op->o_tmpmemctx );
672 /* Delete IDL cache entries */
673 if ( rc == 0 && bdb->bi_idl_cache_size ) {
675 char *ptr = ((char *)&tmp[1])-1;
677 key.size = sizeof(ID)+1;
678 tmp[1] = eip->bei_id;
679 *ptr = DN_ONE_PREFIX;
680 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
681 if ( eip ->bei_parent ) {
682 *ptr = DN_SUBTREE_PREFIX;
683 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
684 tmp[1] = eip->bei_id;
685 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
687 /* Handle DB with empty suffix */
688 if ( !op->o_bd->be_suffix[0].bv_len && eip ) {
689 tmp[1] = eip->bei_id;
690 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
694 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
707 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
708 DB *db = bdb->bi_dn2id->bdi_db;
713 unsigned char dlen[2];
716 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
718 nrlen = dn_rdnlen( op->o_bd, in );
719 if (!nrlen) nrlen = in->bv_len;
722 key.size = sizeof(ID);
724 key.ulen = sizeof(ID);
725 key.flags = DB_DBT_USERMEM;
726 parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
727 BDB_ID2DISK( parentID, &idp );
730 data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
731 data.ulen = data.size * 3;
732 data.dlen = data.ulen;
733 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
735 rc = db->cursor( db, txn, cursor, bdb->bi_db_opflags );
738 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
739 d->nrdnlen[1] = nrlen & 0xff;
740 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
741 dlen[0] = d->nrdnlen[0];
742 dlen[1] = d->nrdnlen[1];
743 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
747 rc = (*cursor)->c_get( *cursor, &key, &data, DB_GET_BOTH_RANGE );
748 if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
749 strncmp( d->nrdn, in->bv_val, nrlen ))) {
753 ptr = (char *) data.data + data.size - sizeof(ID);
754 BDB_DISK2ID( ptr, &ei->bei_id );
755 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
756 ptr = d->nrdn + nrlen + 1;
757 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
758 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
760 /* How many children does the parent have? */
761 /* FIXME: do we need to lock the parent
762 * entryinfo? Seems safe...
764 (*cursor)->c_count( *cursor, &dkids, 0 );
765 ei->bei_parent->bei_dkids = dkids;
769 op->o_tmpfree( d, op->o_tmpmemctx );
771 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: get failed: %s (%d)\n",
772 db_strerror( rc ), rc, 0 );
774 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: got id=0x%lx\n",
788 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
789 DB *db = bdb->bi_dn2id->bdi_db;
798 key.size = sizeof(ID);
800 key.ulen = sizeof(ID);
801 key.flags = DB_DBT_USERMEM;
802 BDB_ID2DISK( ei->bei_id, &nid );
805 data.flags = DB_DBT_USERMEM;
807 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
810 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
811 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
814 rc = cursor->c_get( cursor, &key, &data, DB_SET );
816 if (d->nrdnlen[0] & 0x80) {
820 ptr = (char *) data.data + data.size - sizeof(ID);
821 BDB_DISK2ID( ptr, idp );
822 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
823 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
824 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
826 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
827 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
828 /* How many children does this node have? */
829 cursor->c_count( cursor, &dkids, 0 );
830 ei->bei_dkids = dkids;
833 cursor->c_close( cursor );
834 op->o_tmpfree( d, op->o_tmpmemctx );
844 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
845 DB *db = bdb->bi_dn2id->bdi_db;
853 key.size = sizeof(ID);
855 key.flags = DB_DBT_USERMEM;
856 BDB_ID2DISK( e->e_id, &id );
858 /* IDL cache is in host byte order */
859 if ( bdb->bi_idl_cache_size ) {
860 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
861 if ( rc != LDAP_NO_SUCH_OBJECT ) {
869 data.ulen = sizeof(d);
870 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
871 data.dlen = sizeof(d);
873 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
876 rc = cursor->c_get( cursor, &key, &data, DB_SET );
879 rc = cursor->c_count( cursor, &dkids, 0 );
881 BEI(e)->bei_dkids = dkids;
882 if ( dkids < 2 ) rc = DB_NOTFOUND;
885 cursor->c_close( cursor );
890 * We can't just use bdb_idl_fetch_key because
891 * 1 - our data items are longer than just an entry ID
892 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
894 * We descend the tree recursively, so we define this cookie
895 * to hold our necessary state information. The bdb_dn2idl_internal
896 * function uses this cookie when calling itself.
899 struct dn2id_cookie {
900 struct bdb_info *bdb;
925 EntryInfo *ei = data;
928 bdb_idl_append_one( idl, ei->bei_id );
934 struct dn2id_cookie *cx
937 BDB_IDL_ZERO( cx->tmp );
939 if ( cx->bdb->bi_idl_cache_size ) {
940 char *ptr = ((char *)&cx->id)-1;
943 cx->key.size = sizeof(ID)+1;
944 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
945 ID *ids = cx->depth ? cx->tmp : cx->ids;
947 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
948 if ( cx->rc == LDAP_SUCCESS ) {
950 bdb_idl_append( cx->ids, cx->tmp );
956 *ptr = DN_ONE_PREFIX;
957 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
958 if ( cx->rc == LDAP_SUCCESS ) {
961 if ( cx->rc == DB_NOTFOUND ) {
966 bdb_cache_entryinfo_lock( cx->ei );
968 /* If number of kids in the cache differs from on-disk, load
969 * up all the kids from the database
971 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
973 db_recno_t dkids = cx->ei->bei_dkids;
974 ei.bei_parent = cx->ei;
976 /* Only one thread should load the cache */
977 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
978 bdb_cache_entryinfo_unlock( cx->ei );
979 ldap_pvt_thread_yield();
980 bdb_cache_entryinfo_lock( cx->ei );
981 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
986 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
988 bdb_cache_entryinfo_unlock( cx->ei );
990 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
991 cx->bdb->bi_db_opflags );
995 cx->data.data = &cx->dbuf;
996 cx->data.ulen = sizeof(ID);
997 cx->data.dlen = sizeof(ID);
998 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
1000 /* The first item holds the parent ID. Ignore it. */
1001 cx->key.data = &cx->nid;
1002 cx->key.size = sizeof(ID);
1003 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
1005 cx->dbc->c_close( cx->dbc );
1009 /* If the on-disk count is zero we've never checked it.
1013 cx->dbc->c_count( cx->dbc, &dkids, 0 );
1014 cx->ei->bei_dkids = dkids;
1017 cx->data.data = cx->buf;
1018 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1019 cx->data.flags = DB_DBT_USERMEM;
1022 /* Fetch the rest of the IDs in a loop... */
1023 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1024 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1028 DB_MULTIPLE_INIT( ptr, &cx->data );
1030 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
1033 diskNode *d = (diskNode *)j;
1036 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
1037 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
1038 ei.bei_nrdn.bv_len = nrlen;
1039 /* nrdn/rdn are set in-place.
1040 * hdb_cache_load will copy them as needed
1042 ei.bei_nrdn.bv_val = d->nrdn;
1043 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1044 - ei.bei_nrdn.bv_len;
1045 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1046 bdb_idl_append_one( cx->tmp, ei.bei_id );
1047 hdb_cache_load( cx->bdb, &ei, &ei2 );
1053 cx->rc = cx->dbc->c_close( cx->dbc );
1055 bdb_cache_entryinfo_lock( cx->ei );
1056 cx->ei->bei_state &= ~CACHE_ENTRY_ONELEVEL;
1057 bdb_cache_entryinfo_unlock( cx->ei );
1062 /* The in-memory cache is in sync with the on-disk data.
1063 * do we have any kids?
1067 if ( cx->ei->bei_ckids > 0 ) {
1068 /* Walk the kids tree; order is irrelevant since bdb_idl_sort
1069 * will sort it later.
1071 avl_apply( cx->ei->bei_kids, apply_func,
1072 cx->tmp, -1, AVL_POSTORDER );
1074 bdb_cache_entryinfo_unlock( cx->ei );
1077 if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1078 bdb_idl_sort( cx->tmp, cx->buf );
1079 if ( cx->bdb->bi_idl_cache_max_size && !BDB_IDL_IS_ZERO( cx->tmp )) {
1080 char *ptr = ((char *)&cx->id)-1;
1082 cx->key.size = sizeof(ID)+1;
1083 *ptr = DN_ONE_PREFIX;
1084 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1088 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1089 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1090 bdb_idl_append( cx->ids, cx->tmp );
1092 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1094 EntryInfo *ei = cx->ei;
1096 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1097 cx->op->o_tmpmemctx );
1098 BDB_IDL_CPY( save, cx->tmp );
1102 for ( cx->id = bdb_idl_first( save, &idcurs );
1104 cx->id = bdb_idl_next( save, &idcurs )) {
1107 if ( bdb_cache_find_id( cx->op, cx->txn, cx->id, &cx->ei,
1112 if ( !( ei2->bei_state & CACHE_ENTRY_NO_KIDS )) {
1113 BDB_ID2DISK( cx->id, &cx->nid );
1114 hdb_dn2idl_internal( cx );
1115 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1118 bdb_cache_entryinfo_lock( ei2 );
1120 bdb_cache_entryinfo_unlock( ei2 );
1124 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1126 bdb_cache_entryinfo_lock( ei );
1127 ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1128 bdb_cache_entryinfo_unlock( ei );
1131 /* Make sure caller knows it had kids! */
1136 BDB_IDL_CPY( cx->ids, cx->tmp );
1151 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1152 struct dn2id_cookie cx;
1154 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1155 ndn->bv_val, 0, 0 );
1157 #ifndef BDB_MULTIPLE_SUFFIXES
1158 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1159 ( ei->bei_id == 0 ||
1160 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len )))
1162 BDB_IDL_ALL( bdb, ids );
1168 BDB_ID2DISK( cx.id, &cx.nid );
1171 cx.db = cx.bdb->bi_dn2id->bdi_db;
1172 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1173 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1176 cx.buf = stack + BDB_IDL_UM_SIZE;
1182 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1186 BDB_IDL_ZERO( ids );
1188 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1189 return LDAP_SUCCESS;
1192 cx.key.ulen = sizeof(ID);
1193 cx.key.size = sizeof(ID);
1194 cx.key.flags = DB_DBT_USERMEM;
1198 hdb_dn2idl_internal(&cx);
1199 if ( cx.need_sort ) {
1200 char *ptr = ((char *)&cx.id)-1;
1201 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1202 bdb_idl_sort( cx.ids, cx.tmp );
1204 cx.key.size = sizeof(ID)+1;
1207 if ( cx.bdb->bi_idl_cache_max_size )
1208 bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1211 if ( cx.rc == DB_NOTFOUND )
1212 cx.rc = LDAP_SUCCESS;
1216 #endif /* BDB_HIER */