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>
26 /* Management routines for a hierarchically structured database.
28 * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
29 * entry in this database is a struct diskNode, keyed by entryID and with
30 * the data containing the RDN and entryID of the node's children. We use
31 * a B-Tree with sorted duplicates to store all the children of a node under
32 * the same key. Also, the first item under the key contains the entry's own
33 * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
34 * well as top-down. To keep this info first in the list, the high bit of all
35 * subsequent nrdnlen's is always set. This means we can only accomodate
36 * RDNs up to length 32767, but that's fine since full DNs are already
39 * The diskNode is a variable length structure. This definition is not
40 * directly usable for in-memory manipulation.
42 typedef struct diskNode {
43 unsigned char nrdnlen[2];
45 char rdn[1]; /* variable placement */
46 unsigned char entryID[sizeof(ID)]; /* variable placement */
49 /* Sort function for the sorted duplicate data items of a dn2id key.
50 * Sorts based on normalized RDN, in length order.
54 const MDB_val *usrkey,
61 un = (diskNode *)usrkey->mv_data;
62 cn = (diskNode *)curkey->mv_data;
64 /* data is not aligned, cannot compare directly */
65 rc = un->nrdnlen[0] - cn->nrdnlen[0];
67 rc = un->nrdnlen[1] - cn->nrdnlen[1];
70 nrlen = (un->nrdnlen[0] << 8) | un->nrdnlen[1];
71 return strncmp( un->nrdn, cn->nrdn, nrlen );
75 /* This function constructs a full DN for a given entry.
82 int rlen = 0, nrlen = 0;
89 /* count length of all DN components */
90 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
91 rlen += ei->bei_rdn.bv_len + 1;
92 nrlen += ei->bei_nrdn.bv_len + 1;
93 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
96 /* See if the entry DN was invalidated by a subtree rename */
98 if ( BEI(e)->bei_modrdns >= max ) {
101 /* We found a mismatch, tell the caller to lock it */
102 if ( checkit == 1 ) {
105 /* checkit == 2. do the fix. */
106 free( e->e_name.bv_val );
107 free( e->e_nname.bv_val );
110 e->e_name.bv_len = rlen - 1;
111 e->e_nname.bv_len = nrlen - 1;
112 e->e_name.bv_val = ch_malloc(rlen);
113 e->e_nname.bv_val = ch_malloc(nrlen);
114 ptr = e->e_name.bv_val;
115 nptr = e->e_nname.bv_val;
116 for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
117 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
118 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
119 if ( ei->bei_parent ) {
124 BEI(e)->bei_modrdns = max;
125 if ( ptr > e->e_name.bv_val ) ptr[-1] = '\0';
126 if ( nptr > e->e_nname.bv_val ) nptr[-1] = '\0';
132 /* We add two elements to the DN2ID database - a data item under the parent's
133 * entryID containing the child's RDN and entryID, and an item under the
134 * child's entryID containing the parent's entryID.
144 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
151 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_add 0x%lx: \"%s\"\n",
152 e->e_id, e->e_ndn, 0 );
154 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
156 rlen = dn_rdnlen( op->o_bd, &e->e_name );
158 nrlen = e->e_nname.bv_len;
159 rlen = e->e_name.bv_len;
162 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
163 d->nrdnlen[1] = nrlen & 0xff;
164 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
165 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
167 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
169 memcpy( ptr, &e->e_id, sizeof( ID ));
171 key.mv_size = sizeof(ID);
176 /* Need to make dummy root node once. Subsequent attempts
177 * will fail harmlessly.
180 diskNode dummy = {{0, 0}, "", "", ""};
181 data.mv_data = &dummy;
182 data.mv_size = sizeof(diskNode);
184 mdb_cursor_put( mcp, &key, &data, MDB_NODUPDATA );
188 data.mv_size = sizeof(diskNode) + rlen + nrlen;
190 rc = mdb_cursor_put( mcp, &key, &data, MDB_NODUPDATA );
194 memcpy( ptr, &pid, sizeof( ID ));
195 d->nrdnlen[0] ^= 0x80;
197 rc = mdb_cursor_put( mcd, &key, &data, MDB_NODUPDATA|MDB_APPEND );
201 op->o_tmpfree( d, op->o_tmpmemctx );
202 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
207 /* mc must have been set by mdb_dn2id */
216 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_delete 0x%lx\n",
219 /* Delete our ID from the parent's list */
220 rc = mdb_cursor_del( mc, 0 );
222 /* Delete our ID from the tree. With sorted duplicates, this
223 * will leave any child nodes still hanging around. This is OK
224 * for modrdn, which will add our info back in later.
228 key.mv_size = sizeof(ID);
230 rc = mdb_cursor_get( mc, &key, NULL, MDB_SET );
232 rc = mdb_cursor_del( mc, 0 );
235 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_delete 0x%lx: %d\n", id, rc, 0 );
239 /* return last found ID in *id if no match
240 * If mc is provided, it will be left pointing to the RDN's
241 * record under the parent's ID.
250 struct berval *matched,
251 struct berval *nmatched )
253 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
255 MDB_dbi dbi = mdb->mi_dn2id;
260 char dn[SLAP_LDAPDN_MAXLEN];
264 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
267 matched->bv_val = dn + sizeof(dn) - 1;
269 *matched->bv_val-- = '\0';
272 nmatched->bv_len = 0;
273 nmatched->bv_val = 0;
284 if ( op->o_bd->be_nsuffix[0].bv_len ) {
285 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
287 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
289 for ( ptr = tmp.bv_val + tmp.bv_len - 1; ptr >= tmp.bv_val; ptr-- )
290 if (DN_SEPARATOR(*ptr))
293 tmp.bv_len -= ptr - tmp.bv_val;
297 key.mv_size = sizeof(ID);
302 rc = mdb_cursor_open( txn, dbi, &cursor );
310 data.mv_size = sizeof(diskNode) + tmp.bv_len;
311 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
312 d->nrdnlen[1] = tmp.bv_len & 0xff;
313 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
314 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
317 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
318 op->o_tmpfree( d, op->o_tmpmemctx );
321 ptr = (char *) data.mv_data + data.mv_size - sizeof(ID);
322 memcpy( &nid, ptr, sizeof(ID));
324 /* grab the non-normalized RDN */
328 rlen = data.mv_size - sizeof(diskNode) - tmp.bv_len;
329 matched->bv_len += rlen;
330 matched->bv_val -= rlen + 1;
331 ptr = lutil_strcopy( matched->bv_val, d->rdn + tmp.bv_len );
338 nmatched->bv_val = tmp.bv_val;
341 if ( tmp.bv_val > in->bv_val ) {
342 for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
343 !DN_SEPARATOR(*ptr); ptr--) /* empty */;
344 if ( ptr >= in->bv_val ) {
345 if (DN_SEPARATOR(*ptr)) ptr++;
346 tmp.bv_len = tmp.bv_val - ptr - 1;
355 mdb_cursor_close( cursor );
358 if ( matched->bv_len ) {
359 ptr = op->o_tmpalloc( matched->bv_len+1, op->o_tmpmemctx );
360 strcpy( ptr, matched->bv_val );
361 matched->bv_val = ptr;
363 if ( BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) && !nid ) {
364 ber_dupbv( matched, (struct berval *)&slap_empty_bv );
366 matched->bv_val = NULL;
371 if ( nmatched->bv_val ) {
372 nmatched->bv_len = in->bv_len - (nmatched->bv_val - in->bv_val);
374 *nmatched = slap_empty_bv;
379 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: get failed: %s (%d)\n",
380 mdb_strerror( rc ), rc, 0 );
382 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: got id=0x%lx\n",
389 /* return IDs from root to parent of DN */
397 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
399 MDB_dbi dbi = mdb->mi_dn2id;
407 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2sups(\"%s\")\n", in->bv_val, 0, 0 );
415 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
417 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
419 key.mv_size = sizeof(ID);
421 rc = mdb_cursor_open( txn, dbi, &cursor );
428 data.mv_size = sizeof(diskNode) + tmp.bv_len;
429 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
430 d->nrdnlen[1] = tmp.bv_len & 0xff;
431 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
432 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
435 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
436 op->o_tmpfree( d, op->o_tmpmemctx );
438 mdb_cursor_close( cursor );
441 ptr = (char *) data.mv_data + data.mv_size - sizeof(ID);
442 memcpy( &nid, ptr, sizeof(ID));
445 mdb_idl_insert( ids, pid );
447 if ( tmp.bv_val > in->bv_val ) {
448 for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
449 !DN_SEPARATOR(*ptr); ptr--) /* empty */;
450 if ( ptr >= in->bv_val ) {
451 if (DN_SEPARATOR(*ptr)) ptr++;
452 tmp.bv_len = tmp.bv_val - ptr - 1;
462 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2sups: get failed: %s (%d)\n",
463 mdb_strerror( rc ), rc, 0 );
477 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
478 DB *db = mdb->bi_dn2id->bdi_db;
487 key.size = sizeof(ID);
489 key.ulen = sizeof(ID);
490 key.flags = DB_DBT_USERMEM;
491 MDB_ID2DISK( ei->bei_id, &nid );
494 data.flags = DB_DBT_USERMEM;
496 rc = db->cursor( db, txn, &cursor, mdb->bi_db_opflags );
499 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
500 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
503 rc = cursor->c_get( cursor, &key, &data, DB_SET );
505 if (d->nrdnlen[0] & 0x80) {
509 ptr = (char *) data.data + data.size - sizeof(ID);
510 MDB_DISK2ID( ptr, idp );
511 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
512 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
513 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
515 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
516 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
517 /* How many children does this node have? */
518 cursor->c_count( cursor, &dkids, 0 );
519 ei->bei_dkids = dkids;
522 cursor->c_close( cursor );
523 op->o_tmpfree( d, op->o_tmpmemctx );
534 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
535 MDB_dbi dbi = mdb->mi_dn2id;
541 key.mv_size = sizeof(ID);
545 rc = mdb_cursor_open( txn, dbi, &cursor );
548 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
551 rc = mdb_cursor_count( cursor, &dkids );
553 if ( dkids < 2 ) rc = MDB_NOTFOUND;
556 mdb_cursor_close( cursor );
567 struct berval *nname )
569 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
570 MDB_dbi dbi = mdb->mi_dn2id;
574 char dn[SLAP_LDAPDN_MAXLEN], ndn[SLAP_LDAPDN_MAXLEN], *ptr;
578 key.mv_size = sizeof(ID);
581 rc = mdb_cursor_open( txn, dbi, cursp );
591 unsigned int nrlen, rlen;
595 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
598 ptr += data.mv_size - sizeof(ID);
599 memcpy( &id, ptr, sizeof(ID) );
601 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
602 rlen = data.mv_size - sizeof(diskNode) - nrlen;
603 assert( nrlen < 1024 && rlen < 1024 ); /* FIXME: Sanity check */
608 /* copy name and trailing NUL */
609 memcpy( nptr, d->nrdn, nrlen+1 );
610 memcpy( dptr, d->nrdn+nrlen+1, rlen+1 );
615 name->bv_len = dptr - dn;
616 nname->bv_len = nptr - ndn;
617 name->bv_val = op->o_tmpalloc( name->bv_len + 1, op->o_tmpmemctx );
618 nname->bv_val = op->o_tmpalloc( nname->bv_len + 1, op->o_tmpmemctx );
619 memcpy( name->bv_val, dn, name->bv_len );
620 name->bv_val[name->bv_len] = '\0';
621 memcpy( nname->bv_val, ndn, nname->bv_len );
622 nname->bv_val[nname->bv_len] = '\0';
627 /* Find each id in ids that is a child of base and move it to res.
637 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
638 MDB_dbi dbi = mdb->mi_dn2id;
641 ID ida, id, cid, ci0, idc = 0;
645 key.mv_size = sizeof(ID);
649 rc = mdb_cursor_open( txn, dbi, &cursor );
652 ida = mdb_idl_first( ids, &cid );
654 /* Don't bother moving out of ids if it's a range */
655 if (!MDB_IDL_IS_RANGE(ids)) {
660 while (ida != NOID) {
664 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
666 /* not found, move on to next */
675 ptr += data.mv_size - sizeof(ID);
676 memcpy( &id, ptr, sizeof(ID) );
690 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
693 ida = mdb_idl_next( ids, &cid );
695 if (!MDB_IDL_IS_RANGE( ids ))
698 mdb_cursor_close( cursor );
702 /* See if base is a child of any of the scopes
709 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
710 MDB_dbi dbi = mdb->mi_dn2id;
717 unsigned int nrlen, rlen;
720 key.mv_size = sizeof(ID);
723 rc = mdb_cursor_open( isc->mt, dbi, &isc->mc );
731 rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
738 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
739 rlen = data.mv_size - sizeof(diskNode) - nrlen;
740 isc->nrdns[isc->numrdns].bv_len = nrlen;
741 isc->nrdns[isc->numrdns].bv_val = d->nrdn;
742 isc->rdns[isc->numrdns].bv_len = rlen;
743 isc->rdns[isc->numrdns].bv_val = d->nrdn+nrlen+1;
746 if (!rc && id != isc->id) {
749 mdb_id2l_insert( isc->scopes, &id2 );
752 ptr += data.mv_size - sizeof(ID);
753 memcpy( &id, ptr, sizeof(ID) );
754 x = mdb_id2l_search( isc->scopes, id );
755 if ( x <= isc->scopes[0].mid && isc->scopes[x].mid == id ) {
756 if ( !isc->scopes[x].mval.mv_data ) {
760 data = isc->scopes[x].mval;
763 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
771 * We can't just use mdb_idl_fetch_key because
772 * 1 - our data items are longer than just an entry ID
773 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
775 * We descend the tree recursively, so we define this cookie
776 * to hold our necessary state information. The mdb_dn2idl_internal
777 * function uses this cookie when calling itself.
780 struct dn2id_cookie {
781 struct mdb_info *mdb;
806 EntryInfo *ei = data;
809 mdb_idl_append_one( idl, ei->bei_id );
815 struct dn2id_cookie *cx
818 MDB_IDL_ZERO( cx->tmp );
820 if ( cx->mdb->bi_idl_cache_size ) {
821 char *ptr = ((char *)&cx->id)-1;
824 cx->key.size = sizeof(ID)+1;
825 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
826 ID *ids = cx->depth ? cx->tmp : cx->ids;
828 cx->rc = mdb_idl_cache_get(cx->mdb, cx->db, &cx->key, ids);
829 if ( cx->rc == LDAP_SUCCESS ) {
831 mdb_idl_append( cx->ids, cx->tmp );
837 *ptr = DN_ONE_PREFIX;
838 cx->rc = mdb_idl_cache_get(cx->mdb, cx->db, &cx->key, cx->tmp);
839 if ( cx->rc == LDAP_SUCCESS ) {
842 if ( cx->rc == DB_NOTFOUND ) {
847 mdb_cache_entryinfo_lock( cx->ei );
849 /* If number of kids in the cache differs from on-disk, load
850 * up all the kids from the database
852 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
854 db_recno_t dkids = cx->ei->bei_dkids;
855 ei.bei_parent = cx->ei;
857 /* Only one thread should load the cache */
858 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
859 mdb_cache_entryinfo_unlock( cx->ei );
860 ldap_pvt_thread_yield();
861 mdb_cache_entryinfo_lock( cx->ei );
862 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
867 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
869 mdb_cache_entryinfo_unlock( cx->ei );
871 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
872 cx->mdb->bi_db_opflags );
876 cx->data.data = &cx->dbuf;
877 cx->data.ulen = sizeof(ID);
878 cx->data.dlen = sizeof(ID);
879 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
881 /* The first item holds the parent ID. Ignore it. */
882 cx->key.data = &cx->nid;
883 cx->key.size = sizeof(ID);
884 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
886 cx->dbc->c_close( cx->dbc );
890 /* If the on-disk count is zero we've never checked it.
894 cx->dbc->c_count( cx->dbc, &dkids, 0 );
895 cx->ei->bei_dkids = dkids;
898 cx->data.data = cx->buf;
899 cx->data.ulen = MDB_IDL_UM_SIZE * sizeof(ID);
900 cx->data.flags = DB_DBT_USERMEM;
903 /* Fetch the rest of the IDs in a loop... */
904 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
905 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
909 DB_MULTIPLE_INIT( ptr, &cx->data );
911 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
914 diskNode *d = (diskNode *)j;
917 MDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
918 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
919 ei.bei_nrdn.bv_len = nrlen;
920 /* nrdn/rdn are set in-place.
921 * mdb_cache_load will copy them as needed
923 ei.bei_nrdn.bv_val = d->nrdn;
924 ei.bei_rdn.bv_len = len - sizeof(diskNode)
925 - ei.bei_nrdn.bv_len;
926 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
927 mdb_idl_append_one( cx->tmp, ei.bei_id );
928 mdb_cache_load( cx->mdb, &ei, &ei2 );
934 cx->rc = cx->dbc->c_close( cx->dbc );
936 mdb_cache_entryinfo_lock( cx->ei );
937 cx->ei->bei_state &= ~CACHE_ENTRY_ONELEVEL;
938 mdb_cache_entryinfo_unlock( cx->ei );
943 /* The in-memory cache is in sync with the on-disk data.
944 * do we have any kids?
948 if ( cx->ei->bei_ckids > 0 ) {
949 /* Walk the kids tree; order is irrelevant since mdb_idl_sort
950 * will sort it later.
952 avl_apply( cx->ei->bei_kids, apply_func,
953 cx->tmp, -1, AVL_POSTORDER );
955 mdb_cache_entryinfo_unlock( cx->ei );
958 if ( !MDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
959 mdb_idl_sort( cx->tmp, cx->buf );
960 if ( cx->mdb->bi_idl_cache_max_size && !MDB_IDL_IS_ZERO( cx->tmp )) {
961 char *ptr = ((char *)&cx->id)-1;
963 cx->key.size = sizeof(ID)+1;
964 *ptr = DN_ONE_PREFIX;
965 mdb_idl_cache_put( cx->mdb, cx->db, &cx->key, cx->tmp, cx->rc );
969 if ( !MDB_IDL_IS_ZERO( cx->tmp )) {
970 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
971 mdb_idl_append( cx->ids, cx->tmp );
973 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
975 EntryInfo *ei = cx->ei;
977 save = cx->op->o_tmpalloc( MDB_IDL_SIZEOF( cx->tmp ),
978 cx->op->o_tmpmemctx );
979 MDB_IDL_CPY( save, cx->tmp );
983 for ( cx->id = mdb_idl_first( save, &idcurs );
985 cx->id = mdb_idl_next( save, &idcurs )) {
988 if ( mdb_cache_find_id( cx->op, cx->txn, cx->id, &cx->ei,
993 if ( !( ei2->bei_state & CACHE_ENTRY_NO_KIDS )) {
994 MDB_ID2DISK( cx->id, &cx->nid );
995 mdb_dn2idl_internal( cx );
996 if ( !MDB_IDL_IS_ZERO( cx->tmp ))
999 mdb_cache_entryinfo_lock( ei2 );
1001 mdb_cache_entryinfo_unlock( ei2 );
1005 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1007 mdb_cache_entryinfo_lock( ei );
1008 ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1009 mdb_cache_entryinfo_unlock( ei );
1012 /* Make sure caller knows it had kids! */
1017 MDB_IDL_CPY( cx->ids, cx->tmp );
1032 struct mdb_info *mdb = (struct mdb_info *)op->o_bd->be_private;
1033 struct dn2id_cookie cx;
1035 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2idl(\"%s\")\n",
1036 ndn->bv_val, 0, 0 );
1038 #ifndef MDB_MULTIPLE_SUFFIXES
1039 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1040 ( ei->bei_id == 0 ||
1041 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len )))
1043 MDB_IDL_ALL( mdb, ids );
1049 MDB_ID2DISK( cx.id, &cx.nid );
1052 cx.db = cx.mdb->bi_dn2id->bdi_db;
1053 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1054 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1057 cx.buf = stack + MDB_IDL_UM_SIZE;
1063 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1067 MDB_IDL_ZERO( ids );
1069 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1070 return LDAP_SUCCESS;
1073 cx.key.ulen = sizeof(ID);
1074 cx.key.size = sizeof(ID);
1075 cx.key.flags = DB_DBT_USERMEM;
1079 mdb_dn2idl_internal(&cx);
1080 if ( cx.need_sort ) {
1081 char *ptr = ((char *)&cx.id)-1;
1082 if ( !MDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1083 mdb_idl_sort( cx.ids, cx.tmp );
1085 cx.key.size = sizeof(ID)+1;
1088 if ( cx.mdb->bi_idl_cache_max_size )
1089 mdb_idl_cache_put( cx.mdb, cx.db, &cx.key, cx.ids, cx.rc );
1092 if ( cx.rc == DB_NOTFOUND )
1093 cx.rc = LDAP_SUCCESS;