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-2014 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;
154 struct berval pdn, ptr;
157 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete 0x%lx: \"%s\"\n",
158 e->e_id, e->e_ndn, 0 );
161 key.size = e->e_nname.bv_len + 2;
162 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
164 key.flags = DB_DBT_USERMEM;
165 buf[0] = DN_BASE_PREFIX;
167 ptr.bv_len = e->e_nname.bv_len;
168 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
169 ptr.bv_val[ptr.bv_len] = '\0';
172 rc = db->del( db, txn, &key, 0 );
174 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete 0x%lx: delete failed: %s %d\n",
175 e->e_id, db_strerror(rc), rc );
179 #ifndef BDB_MULTIPLE_SUFFIXES
180 if( !be_issuffix( op->o_bd, &ptr ))
183 buf[0] = DN_SUBTREE_PREFIX;
184 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
186 Debug( LDAP_DEBUG_ANY,
187 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
188 e->e_id, ptr.bv_val, rc );
192 #ifdef BDB_MULTIPLE_SUFFIXES
193 if( !be_issuffix( op->o_bd, &ptr ))
196 dnParent( &ptr, &pdn );
198 key.size = pdn.bv_len + 2;
200 pdn.bv_val[-1] = DN_ONE_PREFIX;
201 key.data = pdn.bv_val - 1;
204 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
207 Debug( LDAP_DEBUG_ANY,
208 "=> bdb_dn2id_delete 0x%lx: parent (%s) delete failed: %d\n",
209 e->e_id, ptr.bv_val, rc );
214 #ifndef BDB_MULTIPLE_SUFFIXES
215 while( !be_issuffix( op->o_bd, &ptr ))
220 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
222 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
224 Debug( LDAP_DEBUG_ANY,
225 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
226 e->e_id, ptr.bv_val, rc );
229 #ifdef BDB_MULTIPLE_SUFFIXES
230 if( be_issuffix( op->o_bd, &ptr )) break;
232 dnParent( &ptr, &pdn );
234 key.size = pdn.bv_len + 2;
236 key.data = pdn.bv_val - 1;
242 op->o_tmpfree( buf, op->o_tmpmemctx );
243 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
255 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
256 DB *db = bdb->bi_dn2id->bdi_db;
261 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id(\"%s\")\n", dn->bv_val, 0, 0 );
264 key.size = dn->bv_len + 2;
265 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
266 ((char *)key.data)[0] = DN_BASE_PREFIX;
267 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
272 data.ulen = sizeof(ID);
273 data.flags = DB_DBT_USERMEM;
275 rc = db->cursor( db, txn, cursor, bdb->bi_db_opflags );
279 rc = (*cursor)->c_get( *cursor, &key, &data, DB_SET );
282 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
283 db_strerror( rc ), rc, 0 );
285 BDB_DISK2ID( &nid, &ei->bei_id );
286 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%lx\n",
289 op->o_tmpfree( key.data, op->o_tmpmemctx );
300 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
301 DB *db = bdb->bi_dn2id->bdi_db;
305 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
306 e->e_nname.bv_val, 0, 0 );
308 key.size = e->e_nname.bv_len + 2;
309 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
310 ((char *)key.data)[0] = DN_ONE_PREFIX;
311 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
313 if ( bdb->bi_idl_cache_size ) {
314 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
315 if ( rc != LDAP_NO_SUCH_OBJECT ) {
316 op->o_tmpfree( key.data, op->o_tmpmemctx );
320 /* we actually could do a empty get... */
323 data.ulen = sizeof(id);
324 data.flags = DB_DBT_USERMEM;
326 data.dlen = sizeof(id);
328 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
329 op->o_tmpfree( key.data, op->o_tmpmemctx );
331 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
333 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
334 db_strerror(rc) ), rc );
350 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
351 DB *db = bdb->bi_dn2id->bdi_db;
352 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
353 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
355 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
358 #ifndef BDB_MULTIPLE_SUFFIXES
359 if ( prefix == DN_SUBTREE_PREFIX
360 && ( ei->bei_id == 0 ||
361 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len ))) {
362 BDB_IDL_ALL(bdb, ids);
368 key.size = ndn->bv_len + 2;
370 key.flags = DB_DBT_USERMEM;
371 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
372 ((char *)key.data)[0] = prefix;
373 AC_MEMCPY( &((char *)key.data)[1], ndn->bv_val, key.size - 1 );
376 rc = bdb_idl_fetch_key( op->o_bd, db, txn, &key, ids, NULL, 0 );
379 Debug( LDAP_DEBUG_TRACE,
380 "<= bdb_dn2idl: get failed: %s (%d)\n",
381 db_strerror( rc ), rc, 0 );
384 Debug( LDAP_DEBUG_TRACE,
385 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
387 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
390 op->o_tmpfree( key.data, op->o_tmpmemctx );
395 /* Management routines for a hierarchically structured database.
397 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
398 * entry in this database is a struct diskNode, keyed by entryID and with
399 * the data containing the RDN and entryID of the node's children. We use
400 * a B-Tree with sorted duplicates to store all the children of a node under
401 * the same key. Also, the first item under the key contains the entry's own
402 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
403 * well as top-down. To keep this info first in the list, the high bit of all
404 * subsequent nrdnlen's is always set. This means we can only accomodate
405 * RDNs up to length 32767, but that's fine since full DNs are already
406 * restricted to 8192.
408 * The diskNode is a variable length structure. This definition is not
409 * directly usable for in-memory manipulation.
411 typedef struct diskNode {
412 unsigned char nrdnlen[2];
414 char rdn[1]; /* variable placement */
415 unsigned char entryID[sizeof(ID)]; /* variable placement */
418 /* Sort function for the sorted duplicate data items of a dn2id key.
419 * Sorts based on normalized RDN, in length order.
431 un = (diskNode *)usrkey->data;
432 cn = (diskNode *)curkey->data;
434 /* data is not aligned, cannot compare directly */
435 rc = un->nrdnlen[0] - cn->nrdnlen[0];
437 rc = un->nrdnlen[1] - cn->nrdnlen[1];
440 return strcmp( un->nrdn, cn->nrdn );
443 /* This function constructs a full DN for a given entry.
450 int rlen = 0, nrlen = 0;
457 /* count length of all DN components */
458 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
459 rlen += ei->bei_rdn.bv_len + 1;
460 nrlen += ei->bei_nrdn.bv_len + 1;
461 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
464 /* See if the entry DN was invalidated by a subtree rename */
466 if ( BEI(e)->bei_modrdns >= max ) {
469 /* We found a mismatch, tell the caller to lock it */
470 if ( checkit == 1 ) {
473 /* checkit == 2. do the fix. */
474 free( e->e_name.bv_val );
475 free( e->e_nname.bv_val );
478 e->e_name.bv_len = rlen - 1;
479 e->e_nname.bv_len = nrlen - 1;
480 e->e_name.bv_val = ch_malloc(rlen);
481 e->e_nname.bv_val = ch_malloc(nrlen);
482 ptr = e->e_name.bv_val;
483 nptr = e->e_nname.bv_val;
484 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
485 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
486 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
487 if ( ei->bei_parent ) {
492 BEI(e)->bei_modrdns = max;
493 if ( ptr > e->e_name.bv_val ) ptr[-1] = '\0';
494 if ( nptr > e->e_nname.bv_val ) nptr[-1] = '\0';
499 /* We add two elements to the DN2ID database - a data item under the parent's
500 * entryID containing the child's RDN and entryID, and an item under the
501 * child's entryID containing the parent's entryID.
510 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
511 DB *db = bdb->bi_dn2id->bdi_db;
518 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_add 0x%lx: \"%s\"\n",
519 e->e_id, e->e_ndn, 0 );
521 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
523 rlen = dn_rdnlen( op->o_bd, &e->e_name );
525 nrlen = e->e_nname.bv_len;
526 rlen = e->e_name.bv_len;
529 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
530 d->nrdnlen[1] = nrlen & 0xff;
531 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
532 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
534 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
536 BDB_ID2DISK( e->e_id, ptr );
540 key.size = sizeof(ID);
541 key.flags = DB_DBT_USERMEM;
542 BDB_ID2DISK( eip->bei_id, &nid );
546 /* Need to make dummy root node once. Subsequent attempts
547 * will fail harmlessly.
549 if ( eip->bei_id == 0 ) {
550 diskNode dummy = {{0, 0}, "", "", ""};
552 data.size = sizeof(diskNode);
553 data.flags = DB_DBT_USERMEM;
555 db->put( db, txn, &key, &data, DB_NODUPDATA );
559 data.size = sizeof(diskNode) + rlen + nrlen;
560 data.flags = DB_DBT_USERMEM;
562 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
565 BDB_ID2DISK( e->e_id, &nid );
566 BDB_ID2DISK( eip->bei_id, ptr );
567 d->nrdnlen[0] ^= 0x80;
569 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
572 /* Update all parents' IDL cache entries */
573 if ( rc == 0 && bdb->bi_idl_cache_size ) {
575 char *ptr = ((char *)&tmp[1])-1;
577 key.size = sizeof(ID)+1;
578 tmp[1] = eip->bei_id;
579 *ptr = DN_ONE_PREFIX;
580 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
581 if ( eip->bei_parent ) {
582 *ptr = DN_SUBTREE_PREFIX;
583 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
584 tmp[1] = eip->bei_id;
585 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
587 /* Handle DB with empty suffix */
588 if ( !op->o_bd->be_suffix[0].bv_len && eip ) {
589 tmp[1] = eip->bei_id;
590 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
595 op->o_tmpfree( d, op->o_tmpmemctx );
596 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
608 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
609 DB *db = bdb->bi_dn2id->bdi_db;
615 unsigned char dlen[2];
617 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n",
618 e->e_id, e->e_ndn, 0 );
621 key.size = sizeof(ID);
623 key.flags = DB_DBT_USERMEM;
624 BDB_ID2DISK( eip->bei_id, &nid );
627 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
628 data.ulen = data.size;
629 data.dlen = data.size;
630 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
634 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
635 d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
636 d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
637 dlen[0] = d->nrdnlen[0];
638 dlen[1] = d->nrdnlen[1];
639 memcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val, BEI(e)->bei_nrdn.bv_len+1 );
642 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
643 if ( rc ) goto func_leave;
645 /* Delete our ID from the parent's list */
646 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
648 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
649 !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
650 rc = cursor->c_del( cursor, 0 );
655 /* Delete our ID from the tree. With sorted duplicates, this
656 * will leave any child nodes still hanging around. This is OK
657 * for modrdn, which will add our info back in later.
660 BDB_ID2DISK( e->e_id, &nid );
661 rc = cursor->c_get( cursor, &key, &data, DB_SET );
663 rc = cursor->c_del( cursor, 0 );
666 cursor->c_close( cursor );
668 op->o_tmpfree( d, op->o_tmpmemctx );
670 /* Delete IDL cache entries */
671 if ( rc == 0 && bdb->bi_idl_cache_size ) {
673 char *ptr = ((char *)&tmp[1])-1;
675 key.size = sizeof(ID)+1;
676 tmp[1] = eip->bei_id;
677 *ptr = DN_ONE_PREFIX;
678 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
679 if ( eip ->bei_parent ) {
680 *ptr = DN_SUBTREE_PREFIX;
681 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
682 tmp[1] = eip->bei_id;
683 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
685 /* Handle DB with empty suffix */
686 if ( !op->o_bd->be_suffix[0].bv_len && eip ) {
687 tmp[1] = eip->bei_id;
688 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
692 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
705 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
706 DB *db = bdb->bi_dn2id->bdi_db;
711 unsigned char dlen[2];
714 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
716 nrlen = dn_rdnlen( op->o_bd, in );
717 if (!nrlen) nrlen = in->bv_len;
720 key.size = sizeof(ID);
722 key.ulen = sizeof(ID);
723 key.flags = DB_DBT_USERMEM;
724 parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
725 BDB_ID2DISK( parentID, &idp );
728 data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
729 data.ulen = data.size * 3;
730 data.dlen = data.ulen;
731 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
733 rc = db->cursor( db, txn, cursor, bdb->bi_db_opflags );
736 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
737 d->nrdnlen[1] = nrlen & 0xff;
738 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
739 dlen[0] = d->nrdnlen[0];
740 dlen[1] = d->nrdnlen[1];
741 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
745 rc = (*cursor)->c_get( *cursor, &key, &data, DB_GET_BOTH_RANGE );
746 if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
747 strncmp( d->nrdn, in->bv_val, nrlen ))) {
751 ptr = (char *) data.data + data.size - sizeof(ID);
752 BDB_DISK2ID( ptr, &ei->bei_id );
753 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
754 ptr = d->nrdn + nrlen + 1;
755 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
756 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
758 /* How many children does the parent have? */
759 /* FIXME: do we need to lock the parent
760 * entryinfo? Seems safe...
762 (*cursor)->c_count( *cursor, &dkids, 0 );
763 ei->bei_parent->bei_dkids = dkids;
767 op->o_tmpfree( d, op->o_tmpmemctx );
769 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: get failed: %s (%d)\n",
770 db_strerror( rc ), rc, 0 );
772 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: got id=0x%lx\n",
786 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
787 DB *db = bdb->bi_dn2id->bdi_db;
796 key.size = sizeof(ID);
798 key.ulen = sizeof(ID);
799 key.flags = DB_DBT_USERMEM;
800 BDB_ID2DISK( ei->bei_id, &nid );
803 data.flags = DB_DBT_USERMEM;
805 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
808 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
809 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
812 rc = cursor->c_get( cursor, &key, &data, DB_SET );
814 if (d->nrdnlen[0] & 0x80) {
818 ptr = (char *) data.data + data.size - sizeof(ID);
819 BDB_DISK2ID( ptr, idp );
820 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
821 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
822 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
824 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
825 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
826 /* How many children does this node have? */
827 cursor->c_count( cursor, &dkids, 0 );
828 ei->bei_dkids = dkids;
831 cursor->c_close( cursor );
832 op->o_tmpfree( d, op->o_tmpmemctx );
842 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
843 DB *db = bdb->bi_dn2id->bdi_db;
851 key.size = sizeof(ID);
853 key.flags = DB_DBT_USERMEM;
854 BDB_ID2DISK( e->e_id, &id );
856 /* IDL cache is in host byte order */
857 if ( bdb->bi_idl_cache_size ) {
858 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
859 if ( rc != LDAP_NO_SUCH_OBJECT ) {
867 data.ulen = sizeof(d);
868 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
869 data.dlen = sizeof(d);
871 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
874 rc = cursor->c_get( cursor, &key, &data, DB_SET );
877 rc = cursor->c_count( cursor, &dkids, 0 );
879 BEI(e)->bei_dkids = dkids;
880 if ( dkids < 2 ) rc = DB_NOTFOUND;
883 cursor->c_close( cursor );
888 * We can't just use bdb_idl_fetch_key because
889 * 1 - our data items are longer than just an entry ID
890 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
892 * We descend the tree recursively, so we define this cookie
893 * to hold our necessary state information. The bdb_dn2idl_internal
894 * function uses this cookie when calling itself.
897 struct dn2id_cookie {
898 struct bdb_info *bdb;
923 EntryInfo *ei = data;
926 bdb_idl_append_one( idl, ei->bei_id );
932 struct dn2id_cookie *cx
935 BDB_IDL_ZERO( cx->tmp );
937 if ( cx->bdb->bi_idl_cache_size ) {
938 char *ptr = ((char *)&cx->id)-1;
941 cx->key.size = sizeof(ID)+1;
942 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
943 ID *ids = cx->depth ? cx->tmp : cx->ids;
945 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
946 if ( cx->rc == LDAP_SUCCESS ) {
948 bdb_idl_delete( cx->tmp, cx->id ); /* ITS#6983, drop our own ID */
949 bdb_idl_append( cx->ids, cx->tmp );
955 *ptr = DN_ONE_PREFIX;
956 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
957 if ( cx->rc == LDAP_SUCCESS ) {
960 if ( cx->rc == DB_NOTFOUND ) {
965 bdb_cache_entryinfo_lock( cx->ei );
967 /* If number of kids in the cache differs from on-disk, load
968 * up all the kids from the database
970 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
972 db_recno_t dkids = cx->ei->bei_dkids;
973 ei.bei_parent = cx->ei;
975 /* Only one thread should load the cache */
976 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
977 bdb_cache_entryinfo_unlock( cx->ei );
978 ldap_pvt_thread_yield();
979 bdb_cache_entryinfo_lock( cx->ei );
980 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
985 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
987 bdb_cache_entryinfo_unlock( cx->ei );
989 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
990 cx->bdb->bi_db_opflags );
994 cx->data.data = &cx->dbuf;
995 cx->data.ulen = sizeof(ID);
996 cx->data.dlen = sizeof(ID);
997 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
999 /* The first item holds the parent ID. Ignore it. */
1000 cx->key.data = &cx->nid;
1001 cx->key.size = sizeof(ID);
1002 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
1004 cx->dbc->c_close( cx->dbc );
1008 /* If the on-disk count is zero we've never checked it.
1012 cx->dbc->c_count( cx->dbc, &dkids, 0 );
1013 cx->ei->bei_dkids = dkids;
1016 cx->data.data = cx->buf;
1017 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1018 cx->data.flags = DB_DBT_USERMEM;
1021 /* Fetch the rest of the IDs in a loop... */
1022 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1023 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1027 DB_MULTIPLE_INIT( ptr, &cx->data );
1029 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
1032 diskNode *d = (diskNode *)j;
1035 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
1036 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
1037 ei.bei_nrdn.bv_len = nrlen;
1038 /* nrdn/rdn are set in-place.
1039 * hdb_cache_load will copy them as needed
1041 ei.bei_nrdn.bv_val = d->nrdn;
1042 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1043 - ei.bei_nrdn.bv_len;
1044 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1045 bdb_idl_append_one( cx->tmp, ei.bei_id );
1046 hdb_cache_load( cx->bdb, &ei, &ei2 );
1052 cx->rc = cx->dbc->c_close( cx->dbc );
1054 bdb_cache_entryinfo_lock( cx->ei );
1055 cx->ei->bei_state &= ~CACHE_ENTRY_ONELEVEL;
1056 bdb_cache_entryinfo_unlock( cx->ei );
1061 /* The in-memory cache is in sync with the on-disk data.
1062 * do we have any kids?
1066 if ( cx->ei->bei_ckids > 0 ) {
1067 /* Walk the kids tree; order is irrelevant since bdb_idl_sort
1068 * will sort it later.
1070 avl_apply( cx->ei->bei_kids, apply_func,
1071 cx->tmp, -1, AVL_POSTORDER );
1073 bdb_cache_entryinfo_unlock( cx->ei );
1076 if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1077 bdb_idl_sort( cx->tmp, cx->buf );
1078 if ( cx->bdb->bi_idl_cache_max_size && !BDB_IDL_IS_ZERO( cx->tmp )) {
1079 char *ptr = ((char *)&cx->id)-1;
1081 cx->key.size = sizeof(ID)+1;
1082 *ptr = DN_ONE_PREFIX;
1083 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1087 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1088 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1089 bdb_idl_append( cx->ids, cx->tmp );
1091 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1093 EntryInfo *ei = cx->ei;
1095 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1096 cx->op->o_tmpmemctx );
1097 BDB_IDL_CPY( save, cx->tmp );
1101 for ( cx->id = bdb_idl_first( save, &idcurs );
1103 cx->id = bdb_idl_next( save, &idcurs )) {
1106 if ( bdb_cache_find_id( cx->op, cx->txn, cx->id, &cx->ei,
1111 if ( !( ei2->bei_state & CACHE_ENTRY_NO_KIDS )) {
1112 BDB_ID2DISK( cx->id, &cx->nid );
1113 hdb_dn2idl_internal( cx );
1114 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1117 bdb_cache_entryinfo_lock( ei2 );
1119 bdb_cache_entryinfo_unlock( ei2 );
1123 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1125 bdb_cache_entryinfo_lock( ei );
1126 ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1127 bdb_cache_entryinfo_unlock( ei );
1130 /* Make sure caller knows it had kids! */
1135 BDB_IDL_CPY( cx->ids, cx->tmp );
1150 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1151 struct dn2id_cookie cx;
1153 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1154 ndn->bv_val, 0, 0 );
1156 #ifndef BDB_MULTIPLE_SUFFIXES
1157 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1158 ( ei->bei_id == 0 ||
1159 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len )))
1161 BDB_IDL_ALL( bdb, ids );
1167 BDB_ID2DISK( cx.id, &cx.nid );
1170 cx.db = cx.bdb->bi_dn2id->bdi_db;
1171 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1172 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1175 cx.buf = stack + BDB_IDL_UM_SIZE;
1181 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1185 BDB_IDL_ZERO( ids );
1187 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1188 return LDAP_SUCCESS;
1191 cx.key.ulen = sizeof(ID);
1192 cx.key.size = sizeof(ID);
1193 cx.key.flags = DB_DBT_USERMEM;
1197 hdb_dn2idl_internal(&cx);
1198 if ( cx.need_sort ) {
1199 char *ptr = ((char *)&cx.id)-1;
1200 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1201 bdb_idl_sort( cx.ids, cx.tmp );
1203 cx.key.size = sizeof(ID)+1;
1206 if ( cx.bdb->bi_idl_cache_max_size )
1207 bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1210 if ( cx.rc == DB_NOTFOUND )
1211 cx.rc = LDAP_SUCCESS;
1215 #endif /* BDB_HIER */