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-2008 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
26 #define bdb_dn2id_lock BDB_SYMBOL(dn2id_lock)
29 bdb_dn2id_lock( struct bdb_info *bdb, struct berval *dn,
30 int rw, DB_TXN *txn, DB_LOCK *lock )
40 db_rw = DB_LOCK_WRITE;
44 lockobj.data = dn->bv_val;
45 lockobj.size = dn->bv_len;
47 rc = LOCK_GET(bdb->bi_dbenv, TXN_ID(txn), DB_LOCK_NOWAIT,
48 &lockobj, db_rw, lock);
60 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
61 DB *db = bdb->bi_dn2id->bdi_db;
66 struct berval ptr, pdn;
68 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add 0x%lx: \"%s\"\n",
69 e->e_id, e->e_ndn, 0 );
70 assert( e->e_id != NOID );
73 key.size = e->e_nname.bv_len + 2;
75 key.flags = DB_DBT_USERMEM;
76 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
78 buf[0] = DN_BASE_PREFIX;
80 ptr.bv_len = e->e_nname.bv_len;
81 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
82 ptr.bv_val[ptr.bv_len] = '\0';
86 data.size = sizeof( nid );
87 BDB_ID2DISK( e->e_id, &nid );
89 /* store it -- don't override */
90 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
92 char buf[ SLAP_TEXT_BUFLEN ];
93 snprintf( buf, sizeof( buf ), "%s => bdb_dn2id_add dn=\"%s\" ID=0x%lx",
94 op->o_log_prefix, e->e_name.bv_val, e->e_id );
95 Debug( LDAP_DEBUG_ANY, "%s: put failed: %s %d\n",
96 buf, db_strerror(rc), rc );
100 #ifndef BDB_MULTIPLE_SUFFIXES
101 if( !be_issuffix( op->o_bd, &ptr ))
104 buf[0] = DN_SUBTREE_PREFIX;
105 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
107 Debug( LDAP_DEBUG_ANY,
108 "=> bdb_dn2id_add 0x%lx: subtree (%s) put failed: %d\n",
109 e->e_id, ptr.bv_val, rc );
113 #ifdef BDB_MULTIPLE_SUFFIXES
114 if( !be_issuffix( op->o_bd, &ptr ))
117 dnParent( &ptr, &pdn );
119 key.size = pdn.bv_len + 2;
121 pdn.bv_val[-1] = DN_ONE_PREFIX;
122 key.data = pdn.bv_val-1;
125 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
128 Debug( LDAP_DEBUG_ANY,
129 "=> bdb_dn2id_add 0x%lx: parent (%s) insert failed: %d\n",
130 e->e_id, ptr.bv_val, rc );
135 #ifndef BDB_MULTIPLE_SUFFIXES
136 while( !be_issuffix( op->o_bd, &ptr ))
141 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
143 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
146 Debug( LDAP_DEBUG_ANY,
147 "=> bdb_dn2id_add 0x%lx: subtree (%s) insert failed: %d\n",
148 e->e_id, ptr.bv_val, rc );
151 #ifdef BDB_MULTIPLE_SUFFIXES
152 if( be_issuffix( op->o_bd, &ptr )) break;
154 dnParent( &ptr, &pdn );
156 key.size = pdn.bv_len + 2;
158 key.data = pdn.bv_val - 1;
164 op->o_tmpfree( buf, op->o_tmpmemctx );
165 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
176 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
177 DB *db = bdb->bi_dn2id->bdi_db;
181 struct berval pdn, ptr;
184 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete 0x%lx: \"%s\"\n",
185 e->e_id, e->e_ndn, 0 );
188 key.size = e->e_nname.bv_len + 2;
189 buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
191 key.flags = DB_DBT_USERMEM;
192 buf[0] = DN_BASE_PREFIX;
194 ptr.bv_len = e->e_nname.bv_len;
195 AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
196 ptr.bv_val[ptr.bv_len] = '\0';
198 /* We hold this lock until the TXN completes */
199 rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, txn, &lock );
203 rc = db->del( db, txn, &key, 0 );
205 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete 0x%lx: delete failed: %s %d\n",
206 e->e_id, db_strerror(rc), rc );
210 #ifndef BDB_MULTIPLE_SUFFIXES
211 if( !be_issuffix( op->o_bd, &ptr ))
214 buf[0] = DN_SUBTREE_PREFIX;
215 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
217 Debug( LDAP_DEBUG_ANY,
218 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
219 e->e_id, ptr.bv_val, rc );
223 #ifdef BDB_MULTIPLE_SUFFIXES
224 if( !be_issuffix( op->o_bd, &ptr ))
227 dnParent( &ptr, &pdn );
229 key.size = pdn.bv_len + 2;
231 pdn.bv_val[-1] = DN_ONE_PREFIX;
232 key.data = pdn.bv_val - 1;
235 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
238 Debug( LDAP_DEBUG_ANY,
239 "=> bdb_dn2id_delete 0x%lx: parent (%s) delete failed: %d\n",
240 e->e_id, ptr.bv_val, rc );
245 #ifndef BDB_MULTIPLE_SUFFIXES
246 while( !be_issuffix( op->o_bd, &ptr ))
251 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
253 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
255 Debug( LDAP_DEBUG_ANY,
256 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
257 e->e_id, ptr.bv_val, rc );
260 #ifdef BDB_MULTIPLE_SUFFIXES
261 if( be_issuffix( op->o_bd, &ptr )) break;
263 dnParent( &ptr, &pdn );
265 key.size = pdn.bv_len + 2;
267 key.data = pdn.bv_val - 1;
273 op->o_tmpfree( buf, op->o_tmpmemctx );
274 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
286 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
287 DB *db = bdb->bi_dn2id->bdi_db;
293 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id(\"%s\")\n", dn->bv_val, 0, 0 );
296 key.size = dn->bv_len + 2;
297 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
298 ((char *)key.data)[0] = DN_BASE_PREFIX;
299 AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
304 data.ulen = sizeof(ID);
305 data.flags = DB_DBT_USERMEM;
307 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
308 if ( rc ) goto func_leave;
310 rc = bdb_dn2id_lock( bdb, dn, 0, txn, lock );
311 if ( rc ) goto nolock;
314 rc = cursor->c_get( cursor, &key, &data, DB_SET );
317 cursor->c_close( cursor );
321 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
322 db_strerror( rc ), rc, 0 );
324 BDB_DISK2ID( &nid, &ei->bei_id );
325 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%lx\n",
328 op->o_tmpfree( key.data, op->o_tmpmemctx );
339 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
340 DB *db = bdb->bi_dn2id->bdi_db;
344 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
345 e->e_nname.bv_val, 0, 0 );
347 key.size = e->e_nname.bv_len + 2;
348 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
349 ((char *)key.data)[0] = DN_ONE_PREFIX;
350 AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
352 if ( bdb->bi_idl_cache_size ) {
353 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
354 if ( rc != LDAP_NO_SUCH_OBJECT ) {
355 op->o_tmpfree( key.data, op->o_tmpmemctx );
359 /* we actually could do a empty get... */
362 data.ulen = sizeof(id);
363 data.flags = DB_DBT_USERMEM;
365 data.dlen = sizeof(id);
367 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
368 op->o_tmpfree( key.data, op->o_tmpmemctx );
370 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
372 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
373 db_strerror(rc) ), rc );
389 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
390 DB *db = bdb->bi_dn2id->bdi_db;
391 int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
392 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
394 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
397 #ifndef BDB_MULTIPLE_SUFFIXES
398 if ( prefix == DN_SUBTREE_PREFIX
399 && ( ei->bei_id == 0 ||
400 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len ))) {
401 BDB_IDL_ALL(bdb, ids);
407 key.size = ndn->bv_len + 2;
409 key.flags = DB_DBT_USERMEM;
410 key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
411 ((char *)key.data)[0] = prefix;
412 AC_MEMCPY( &((char *)key.data)[1], ndn->bv_val, key.size - 1 );
415 rc = bdb_idl_fetch_key( op->o_bd, db, txn, &key, ids, NULL, 0 );
418 Debug( LDAP_DEBUG_TRACE,
419 "<= bdb_dn2idl: get failed: %s (%d)\n",
420 db_strerror( rc ), rc, 0 );
423 Debug( LDAP_DEBUG_TRACE,
424 "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
426 (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
429 op->o_tmpfree( key.data, op->o_tmpmemctx );
434 /* Management routines for a hierarchically structured database.
436 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
437 * entry in this database is a struct diskNode, keyed by entryID and with
438 * the data containing the RDN and entryID of the node's children. We use
439 * a B-Tree with sorted duplicates to store all the children of a node under
440 * the same key. Also, the first item under the key contains the entry's own
441 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
442 * well as top-down. To keep this info first in the list, the high bit of all
443 * subsequent nrdnlen's is always set. This means we can only accomodate
444 * RDNs up to length 32767, but that's fine since full DNs are already
445 * restricted to 8192.
447 * The diskNode is a variable length structure. This definition is not
448 * directly usable for in-memory manipulation.
450 typedef struct diskNode {
451 unsigned char nrdnlen[2];
453 char rdn[1]; /* variable placement */
454 unsigned char entryID[sizeof(ID)]; /* variable placement */
457 /* Sort function for the sorted duplicate data items of a dn2id key.
458 * Sorts based on normalized RDN, in length order.
470 un = (diskNode *)usrkey->data;
471 cn = (diskNode *)curkey->data;
473 /* data is not aligned, cannot compare directly */
474 rc = un->nrdnlen[0] - cn->nrdnlen[0];
476 rc = un->nrdnlen[1] - cn->nrdnlen[1];
479 return strcmp( un->nrdn, cn->nrdn );
482 /* This function constructs a full DN for a given entry.
489 int rlen = 0, nrlen = 0;
496 /* count length of all DN components */
497 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
498 rlen += ei->bei_rdn.bv_len + 1;
499 nrlen += ei->bei_nrdn.bv_len + 1;
500 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
503 /* See if the entry DN was invalidated by a subtree rename */
505 if ( BEI(e)->bei_modrdns >= max ) {
508 /* We found a mismatch, tell the caller to lock it */
509 if ( checkit == 1 ) {
512 /* checkit == 2. do the fix. */
513 free( e->e_name.bv_val );
514 free( e->e_nname.bv_val );
517 e->e_name.bv_len = rlen - 1;
518 e->e_nname.bv_len = nrlen - 1;
519 e->e_name.bv_val = ch_malloc(rlen);
520 e->e_nname.bv_val = ch_malloc(nrlen);
521 ptr = e->e_name.bv_val;
522 nptr = e->e_nname.bv_val;
523 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
524 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
525 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
526 if ( ei->bei_parent ) {
531 BEI(e)->bei_modrdns = max;
538 /* We add two elements to the DN2ID database - a data item under the parent's
539 * entryID containing the child's RDN and entryID, and an item under the
540 * child's entryID containing the parent's entryID.
549 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
550 DB *db = bdb->bi_dn2id->bdi_db;
557 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_add 0x%lx: \"%s\"\n",
558 e->e_id, e->e_ndn, 0 );
560 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
562 rlen = dn_rdnlen( op->o_bd, &e->e_name );
564 nrlen = e->e_nname.bv_len;
565 rlen = e->e_name.bv_len;
568 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
569 d->nrdnlen[1] = nrlen & 0xff;
570 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
571 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
573 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
575 BDB_ID2DISK( e->e_id, ptr );
579 key.size = sizeof(ID);
580 key.flags = DB_DBT_USERMEM;
581 BDB_ID2DISK( eip->bei_id, &nid );
585 /* Need to make dummy root node once. Subsequent attempts
586 * will fail harmlessly.
588 if ( eip->bei_id == 0 ) {
589 diskNode dummy = {{0, 0}, "", "", ""};
591 data.size = sizeof(diskNode);
592 data.flags = DB_DBT_USERMEM;
594 db->put( db, txn, &key, &data, DB_NODUPDATA );
598 data.size = sizeof(diskNode) + rlen + nrlen;
599 data.flags = DB_DBT_USERMEM;
601 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
604 BDB_ID2DISK( e->e_id, &nid );
605 BDB_ID2DISK( eip->bei_id, ptr );
606 d->nrdnlen[0] ^= 0x80;
608 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
611 /* Update all parents' IDL cache entries */
612 if ( rc == 0 && bdb->bi_idl_cache_size ) {
614 char *ptr = ((char *)&tmp[1])-1;
616 key.size = sizeof(ID)+1;
617 tmp[1] = eip->bei_id;
618 *ptr = DN_ONE_PREFIX;
619 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
620 if ( eip->bei_parent ) {
621 *ptr = DN_SUBTREE_PREFIX;
622 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
623 tmp[1] = eip->bei_id;
624 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
629 op->o_tmpfree( d, op->o_tmpmemctx );
630 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
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 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n",
653 e->e_id, e->e_ndn, 0 );
656 key.size = sizeof(ID);
658 key.flags = DB_DBT_USERMEM;
659 BDB_ID2DISK( eip->bei_id, &nid );
662 data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
663 data.ulen = data.size;
664 data.dlen = data.size;
665 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
669 d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
670 d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
671 d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
672 dlen[0] = d->nrdnlen[0];
673 dlen[1] = d->nrdnlen[1];
674 strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
677 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
678 if ( rc ) goto func_leave;
680 /* We hold this lock until the TXN completes */
681 rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, txn, &lock );
682 if ( rc ) goto nolock;
684 /* Delete our ID from the parent's list */
685 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
687 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
688 !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
689 rc = cursor->c_del( cursor, 0 );
694 /* Delete our ID from the tree. With sorted duplicates, this
695 * will leave any child nodes still hanging around. This is OK
696 * for modrdn, which will add our info back in later.
699 BDB_ID2DISK( e->e_id, &nid );
700 rc = cursor->c_get( cursor, &key, &data, DB_SET );
702 rc = cursor->c_del( cursor, 0 );
706 cursor->c_close( cursor );
708 op->o_tmpfree( d, op->o_tmpmemctx );
710 /* Delete IDL cache entries */
711 if ( rc == 0 && bdb->bi_idl_cache_size ) {
713 char *ptr = ((char *)&tmp[1])-1;
715 key.size = sizeof(ID)+1;
716 tmp[1] = eip->bei_id;
717 *ptr = DN_ONE_PREFIX;
718 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
719 if ( eip ->bei_parent ) {
720 *ptr = DN_SUBTREE_PREFIX;
721 for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
722 tmp[1] = eip->bei_id;
723 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
727 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
740 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
741 DB *db = bdb->bi_dn2id->bdi_db;
747 unsigned char dlen[2];
750 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
752 nrlen = dn_rdnlen( op->o_bd, in );
753 if (!nrlen) nrlen = in->bv_len;
756 key.size = sizeof(ID);
758 key.ulen = sizeof(ID);
759 key.flags = DB_DBT_USERMEM;
760 parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
761 BDB_ID2DISK( parentID, &idp );
764 data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
765 data.ulen = data.size * 3;
766 data.dlen = data.ulen;
767 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
769 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
772 d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
773 d->nrdnlen[1] = nrlen & 0xff;
774 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
775 dlen[0] = d->nrdnlen[0];
776 dlen[1] = d->nrdnlen[1];
777 ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
781 rc = bdb_dn2id_lock( bdb, in, 0, txn, lock );
782 if ( rc ) goto func_leave;
784 rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
785 if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
786 strncmp( d->nrdn, in->bv_val, nrlen ))) {
790 ptr = (char *) data.data + data.size - sizeof(ID);
791 BDB_DISK2ID( ptr, &ei->bei_id );
792 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
793 ptr = d->nrdn + nrlen + 1;
794 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
795 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
797 /* How many children does the parent have? */
798 /* FIXME: do we need to lock the parent
799 * entryinfo? Seems safe...
801 cursor->c_count( cursor, &dkids, 0 );
802 ei->bei_parent->bei_dkids = dkids;
807 cursor->c_close( cursor );
808 op->o_tmpfree( d, op->o_tmpmemctx );
810 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: get failed: %s (%d)\n",
811 db_strerror( rc ), rc, 0 );
813 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: got id=0x%lx\n",
827 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
828 DB *db = bdb->bi_dn2id->bdi_db;
837 key.size = sizeof(ID);
839 key.ulen = sizeof(ID);
840 key.flags = DB_DBT_USERMEM;
841 BDB_ID2DISK( ei->bei_id, &nid );
844 data.flags = DB_DBT_USERMEM;
846 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
849 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
850 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
853 rc = cursor->c_get( cursor, &key, &data, DB_SET );
855 if (d->nrdnlen[0] & 0x80) {
859 ptr = (char *) data.data + data.size - sizeof(ID);
860 BDB_DISK2ID( ptr, idp );
861 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
862 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
863 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
865 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
866 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
867 /* How many children does this node have? */
868 cursor->c_count( cursor, &dkids, 0 );
869 ei->bei_dkids = dkids;
872 cursor->c_close( cursor );
873 op->o_tmpfree( d, op->o_tmpmemctx );
883 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
884 DB *db = bdb->bi_dn2id->bdi_db;
892 key.size = sizeof(ID);
894 key.flags = DB_DBT_USERMEM;
895 BDB_ID2DISK( e->e_id, &id );
897 /* IDL cache is in host byte order */
898 if ( bdb->bi_idl_cache_size ) {
899 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
900 if ( rc != LDAP_NO_SUCH_OBJECT ) {
908 data.ulen = sizeof(d);
909 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
910 data.dlen = sizeof(d);
912 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
915 rc = cursor->c_get( cursor, &key, &data, DB_SET );
918 rc = cursor->c_count( cursor, &dkids, 0 );
920 BEI(e)->bei_dkids = dkids;
921 if ( dkids < 2 ) rc = DB_NOTFOUND;
924 cursor->c_close( cursor );
929 * We can't just use bdb_idl_fetch_key because
930 * 1 - our data items are longer than just an entry ID
931 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
933 * We descend the tree recursively, so we define this cookie
934 * to hold our necessary state information. The bdb_dn2idl_internal
935 * function uses this cookie when calling itself.
938 struct dn2id_cookie {
939 struct bdb_info *bdb;
964 EntryInfo *ei = data;
967 bdb_idl_append_one( idl, ei->bei_id );
973 struct dn2id_cookie *cx
976 BDB_IDL_ZERO( cx->tmp );
978 if ( cx->bdb->bi_idl_cache_size ) {
979 char *ptr = ((char *)&cx->id)-1;
982 cx->key.size = sizeof(ID)+1;
983 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
984 ID *ids = cx->depth ? cx->tmp : cx->ids;
986 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
987 if ( cx->rc == LDAP_SUCCESS ) {
989 bdb_idl_append( cx->ids, cx->tmp );
995 *ptr = DN_ONE_PREFIX;
996 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
997 if ( cx->rc == LDAP_SUCCESS ) {
1000 if ( cx->rc == DB_NOTFOUND ) {
1005 bdb_cache_entryinfo_lock( cx->ei );
1007 /* If number of kids in the cache differs from on-disk, load
1008 * up all the kids from the database
1010 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
1012 db_recno_t dkids = cx->ei->bei_dkids;
1013 ei.bei_parent = cx->ei;
1015 /* Only one thread should load the cache */
1016 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
1017 bdb_cache_entryinfo_unlock( cx->ei );
1018 ldap_pvt_thread_yield();
1019 bdb_cache_entryinfo_lock( cx->ei );
1020 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
1025 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
1027 bdb_cache_entryinfo_unlock( cx->ei );
1029 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
1030 cx->bdb->bi_db_opflags );
1034 cx->data.data = &cx->dbuf;
1035 cx->data.ulen = sizeof(ID);
1036 cx->data.dlen = sizeof(ID);
1037 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
1039 /* The first item holds the parent ID. Ignore it. */
1040 cx->key.data = &cx->nid;
1041 cx->key.size = sizeof(ID);
1042 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
1044 cx->dbc->c_close( cx->dbc );
1048 /* If the on-disk count is zero we've never checked it.
1052 cx->dbc->c_count( cx->dbc, &dkids, 0 );
1053 cx->ei->bei_dkids = dkids;
1056 cx->data.data = cx->buf;
1057 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1058 cx->data.flags = DB_DBT_USERMEM;
1061 /* Fetch the rest of the IDs in a loop... */
1062 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1063 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1067 DB_MULTIPLE_INIT( ptr, &cx->data );
1069 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
1072 diskNode *d = (diskNode *)j;
1075 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
1076 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
1077 ei.bei_nrdn.bv_len = nrlen;
1078 /* nrdn/rdn are set in-place.
1079 * hdb_cache_load will copy them as needed
1081 ei.bei_nrdn.bv_val = d->nrdn;
1082 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1083 - ei.bei_nrdn.bv_len;
1084 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1085 bdb_idl_append_one( cx->tmp, ei.bei_id );
1086 hdb_cache_load( cx->bdb, &ei, &ei2 );
1092 cx->rc = cx->dbc->c_close( cx->dbc );
1094 bdb_cache_entryinfo_lock( cx->ei );
1095 cx->ei->bei_state ^= CACHE_ENTRY_ONELEVEL;
1096 bdb_cache_entryinfo_unlock( cx->ei );
1101 /* The in-memory cache is in sync with the on-disk data.
1102 * do we have any kids?
1106 if ( cx->ei->bei_ckids > 0 ) {
1107 /* Walk the kids tree; order is irrelevant since bdb_idl_sort
1108 * will sort it later.
1110 avl_apply( cx->ei->bei_kids, apply_func,
1111 cx->tmp, -1, AVL_POSTORDER );
1113 bdb_cache_entryinfo_unlock( cx->ei );
1116 if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1117 bdb_idl_sort( cx->tmp, cx->buf );
1118 if ( cx->bdb->bi_idl_cache_max_size && !BDB_IDL_IS_ZERO( cx->tmp )) {
1119 char *ptr = ((char *)&cx->id)-1;
1121 cx->key.size = sizeof(ID)+1;
1122 *ptr = DN_ONE_PREFIX;
1123 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1127 if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1128 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1129 bdb_idl_append( cx->ids, cx->tmp );
1131 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1133 EntryInfo *ei = cx->ei;
1135 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1136 cx->op->o_tmpmemctx );
1137 BDB_IDL_CPY( save, cx->tmp );
1141 for ( cx->id = bdb_idl_first( save, &idcurs );
1143 cx->id = bdb_idl_next( save, &idcurs )) {
1144 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
1146 ( cx->ei->bei_state & CACHE_ENTRY_NO_KIDS ))
1149 BDB_ID2DISK( cx->id, &cx->nid );
1150 hdb_dn2idl_internal( cx );
1151 if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1155 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1157 bdb_cache_entryinfo_lock( ei );
1158 ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1159 bdb_cache_entryinfo_unlock( ei );
1162 /* Make sure caller knows it had kids! */
1167 BDB_IDL_CPY( cx->ids, cx->tmp );
1182 struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1183 struct dn2id_cookie cx;
1185 Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1186 ndn->bv_val, 0, 0 );
1188 #ifndef BDB_MULTIPLE_SUFFIXES
1189 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1190 ( ei->bei_id == 0 ||
1191 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len )))
1193 BDB_IDL_ALL( bdb, ids );
1199 BDB_ID2DISK( cx.id, &cx.nid );
1202 cx.db = cx.bdb->bi_dn2id->bdi_db;
1203 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1204 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1207 cx.buf = stack + BDB_IDL_UM_SIZE;
1213 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1217 BDB_IDL_ZERO( ids );
1219 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1220 return LDAP_SUCCESS;
1223 cx.key.ulen = sizeof(ID);
1224 cx.key.size = sizeof(ID);
1225 cx.key.flags = DB_DBT_USERMEM;
1229 hdb_dn2idl_internal(&cx);
1230 if ( cx.need_sort ) {
1231 char *ptr = ((char *)&cx.id)-1;
1232 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1233 bdb_idl_sort( cx.ids, cx.tmp );
1235 cx.key.size = sizeof(ID)+1;
1238 if ( cx.bdb->bi_idl_cache_max_size )
1239 bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1242 if ( cx.rc == DB_NOTFOUND )
1243 cx.rc = LDAP_SUCCESS;
1247 #endif /* BDB_HIER */