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.
143 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
144 MDB_dbi dbi = mdb->mi_dn2id;
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_put( txn, dbi, &key, &data, MDB_NODUPDATA );
188 data.mv_size = sizeof(diskNode) + rlen + nrlen;
190 rc = mdb_put( txn, dbi, &key, &data, MDB_NODUPDATA );
194 memcpy( ptr, &pid, sizeof( ID ));
195 d->nrdnlen[0] ^= 0x80;
197 rc = mdb_put( txn, dbi, &key, &data, MDB_NODUPDATA );
200 op->o_tmpfree( d, op->o_tmpmemctx );
201 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
213 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
214 MDB_dbi dbi = mdb->mi_dn2id;
220 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_delete 0x%lx: \"%s\"\n",
221 e->e_id, e->e_ndn, 0 );
223 key.mv_size = sizeof(ID);
227 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
228 data.mv_size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
230 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
231 d->nrdnlen[1] = nrlen & 0xff;
232 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
233 memcpy( d->nrdn, e->e_nname.bv_val, nrlen );
234 d->nrdn[nrlen] = '\0';
237 /* Delete our ID from the parent's list */
238 rc = mdb_del( txn, dbi, &key, &data );
240 /* Delete our ID from the tree. With sorted duplicates, this
241 * will leave any child nodes still hanging around. This is OK
242 * for modrdn, which will add our info back in later.
246 d->nrdnlen[0] ^= 0x80;
247 rc = mdb_del( txn, dbi, &key, &data );
250 op->o_tmpfree( d, op->o_tmpmemctx );
252 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
256 /* return last found ID in *id if no match */
263 struct berval *matched,
264 struct berval *nmatched )
266 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
268 MDB_dbi dbi = mdb->mi_dn2id;
273 char dn[SLAP_LDAPDN_MAXLEN];
277 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
280 matched->bv_val = dn + sizeof(dn) - 1;
282 *matched->bv_val-- = '\0';
285 nmatched->bv_len = 0;
286 nmatched->bv_val = 0;
297 if ( op->o_bd->be_nsuffix[0].bv_len ) {
298 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
300 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
302 for ( ptr = tmp.bv_val + tmp.bv_len - 1; ptr >= tmp.bv_val; ptr-- )
303 if (DN_SEPARATOR(*ptr))
306 tmp.bv_len -= ptr - tmp.bv_val;
310 key.mv_size = sizeof(ID);
312 rc = mdb_cursor_open( txn, dbi, &cursor );
319 data.mv_size = sizeof(diskNode) + tmp.bv_len;
320 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
321 d->nrdnlen[1] = tmp.bv_len & 0xff;
322 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
323 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
326 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
327 op->o_tmpfree( d, op->o_tmpmemctx );
330 ptr = (char *) data.mv_data + data.mv_size - sizeof(ID);
331 memcpy( &nid, ptr, sizeof(ID));
333 /* grab the non-normalized RDN */
337 rlen = data.mv_size - sizeof(diskNode) - tmp.bv_len;
338 matched->bv_len += rlen;
339 matched->bv_val -= rlen + 1;
340 ptr = lutil_strcopy( matched->bv_val, d->rdn + tmp.bv_len );
347 nmatched->bv_val = tmp.bv_val;
350 if ( tmp.bv_val > in->bv_val ) {
351 for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
352 !DN_SEPARATOR(*ptr); ptr--) /* empty */;
353 if ( ptr >= in->bv_val ) {
354 if (DN_SEPARATOR(*ptr)) ptr++;
355 tmp.bv_len = tmp.bv_val - ptr - 1;
363 mdb_cursor_close( cursor );
366 if ( matched->bv_len ) {
367 ptr = op->o_tmpalloc( matched->bv_len+1, op->o_tmpmemctx );
368 strcpy( ptr, matched->bv_val );
369 matched->bv_val = ptr;
371 if ( BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) && !nid ) {
372 ber_dupbv( matched, (struct berval *)&slap_empty_bv );
374 matched->bv_val = NULL;
379 if ( nmatched->bv_val ) {
380 nmatched->bv_len = in->bv_len - (nmatched->bv_val - in->bv_val);
382 *nmatched = slap_empty_bv;
387 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: get failed: %s (%d)\n",
388 mdb_strerror( rc ), rc, 0 );
390 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: got id=0x%lx\n",
397 /* return IDs from root to parent of DN */
405 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
407 MDB_dbi dbi = mdb->mi_dn2id;
415 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2sups(\"%s\")\n", in->bv_val, 0, 0 );
423 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
425 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
427 key.mv_size = sizeof(ID);
429 rc = mdb_cursor_open( txn, dbi, &cursor );
436 data.mv_size = sizeof(diskNode) + tmp.bv_len;
437 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
438 d->nrdnlen[1] = tmp.bv_len & 0xff;
439 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
440 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
443 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
444 op->o_tmpfree( d, op->o_tmpmemctx );
446 mdb_cursor_close( cursor );
449 ptr = (char *) data.mv_data + data.mv_size - sizeof(ID);
450 memcpy( &nid, ptr, sizeof(ID));
453 mdb_idl_insert( ids, pid );
455 if ( tmp.bv_val > in->bv_val ) {
456 for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
457 !DN_SEPARATOR(*ptr); ptr--) /* empty */;
458 if ( ptr >= in->bv_val ) {
459 if (DN_SEPARATOR(*ptr)) ptr++;
460 tmp.bv_len = tmp.bv_val - ptr - 1;
470 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2sups: get failed: %s (%d)\n",
471 mdb_strerror( rc ), rc, 0 );
485 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
486 DB *db = mdb->bi_dn2id->bdi_db;
495 key.size = sizeof(ID);
497 key.ulen = sizeof(ID);
498 key.flags = DB_DBT_USERMEM;
499 MDB_ID2DISK( ei->bei_id, &nid );
502 data.flags = DB_DBT_USERMEM;
504 rc = db->cursor( db, txn, &cursor, mdb->bi_db_opflags );
507 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
508 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
511 rc = cursor->c_get( cursor, &key, &data, DB_SET );
513 if (d->nrdnlen[0] & 0x80) {
517 ptr = (char *) data.data + data.size - sizeof(ID);
518 MDB_DISK2ID( ptr, idp );
519 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
520 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
521 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
523 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
524 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
525 /* How many children does this node have? */
526 cursor->c_count( cursor, &dkids, 0 );
527 ei->bei_dkids = dkids;
530 cursor->c_close( cursor );
531 op->o_tmpfree( d, op->o_tmpmemctx );
542 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
543 MDB_dbi dbi = mdb->mi_dn2id;
549 key.mv_size = sizeof(ID);
553 rc = mdb_cursor_open( txn, dbi, &cursor );
556 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
559 rc = mdb_cursor_count( cursor, &dkids );
561 if ( dkids < 2 ) rc = MDB_NOTFOUND;
564 mdb_cursor_close( cursor );
575 struct berval *nname )
577 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
578 MDB_dbi dbi = mdb->mi_dn2id;
582 char dn[SLAP_LDAPDN_MAXLEN], ndn[SLAP_LDAPDN_MAXLEN], *ptr;
586 key.mv_size = sizeof(ID);
589 rc = mdb_cursor_open( txn, dbi, cursp );
599 unsigned int nrlen, rlen;
603 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
606 ptr += data.mv_size - sizeof(ID);
607 memcpy( &id, ptr, sizeof(ID) );
609 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
610 rlen = data.mv_size - sizeof(diskNode) - nrlen;
611 assert( nrlen < 1024 && rlen < 1024 ); /* FIXME: Sanity check */
616 /* copy name and trailing NUL */
617 memcpy( nptr, d->nrdn, nrlen+1 );
618 memcpy( dptr, d->nrdn+nrlen+1, rlen+1 );
623 name->bv_len = dptr - dn;
624 nname->bv_len = nptr - ndn;
625 name->bv_val = op->o_tmpalloc( name->bv_len + 1, op->o_tmpmemctx );
626 nname->bv_val = op->o_tmpalloc( nname->bv_len + 1, op->o_tmpmemctx );
627 memcpy( name->bv_val, dn, name->bv_len );
628 name->bv_val[name->bv_len] = '\0';
629 memcpy( nname->bv_val, ndn, nname->bv_len );
630 nname->bv_val[nname->bv_len] = '\0';
635 /* Find each id in ids that is a child of base and move it to res.
645 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
646 MDB_dbi dbi = mdb->mi_dn2id;
649 ID ida, id, cid, ci0, idc = 0;
653 key.mv_size = sizeof(ID);
657 rc = mdb_cursor_open( txn, dbi, &cursor );
660 ida = mdb_idl_first( ids, &cid );
662 /* Don't bother moving out of ids if it's a range */
663 if (!MDB_IDL_IS_RANGE(ids)) {
668 while (ida != NOID) {
672 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
674 /* not found, move on to next */
683 ptr += data.mv_size - sizeof(ID);
684 memcpy( &id, ptr, sizeof(ID) );
698 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
701 ida = mdb_idl_next( ids, &cid );
703 if (!MDB_IDL_IS_RANGE( ids ))
706 mdb_cursor_close( cursor );
710 /* See if base is a child of any of the scopes
720 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
721 MDB_dbi dbi = mdb->mi_dn2id;
729 key.mv_size = sizeof(ID);
732 rc = mdb_cursor_open( txn, dbi, cursp );
740 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
744 ptr += data.mv_size - sizeof(ID);
745 memcpy( &id, ptr, sizeof(ID) );
746 x = mdb_idl_search( scopes, id );
747 if ( scopes[x] == id )
749 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
757 * We can't just use mdb_idl_fetch_key because
758 * 1 - our data items are longer than just an entry ID
759 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
761 * We descend the tree recursively, so we define this cookie
762 * to hold our necessary state information. The mdb_dn2idl_internal
763 * function uses this cookie when calling itself.
766 struct dn2id_cookie {
767 struct mdb_info *mdb;
792 EntryInfo *ei = data;
795 mdb_idl_append_one( idl, ei->bei_id );
801 struct dn2id_cookie *cx
804 MDB_IDL_ZERO( cx->tmp );
806 if ( cx->mdb->bi_idl_cache_size ) {
807 char *ptr = ((char *)&cx->id)-1;
810 cx->key.size = sizeof(ID)+1;
811 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
812 ID *ids = cx->depth ? cx->tmp : cx->ids;
814 cx->rc = mdb_idl_cache_get(cx->mdb, cx->db, &cx->key, ids);
815 if ( cx->rc == LDAP_SUCCESS ) {
817 mdb_idl_append( cx->ids, cx->tmp );
823 *ptr = DN_ONE_PREFIX;
824 cx->rc = mdb_idl_cache_get(cx->mdb, cx->db, &cx->key, cx->tmp);
825 if ( cx->rc == LDAP_SUCCESS ) {
828 if ( cx->rc == DB_NOTFOUND ) {
833 mdb_cache_entryinfo_lock( cx->ei );
835 /* If number of kids in the cache differs from on-disk, load
836 * up all the kids from the database
838 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
840 db_recno_t dkids = cx->ei->bei_dkids;
841 ei.bei_parent = cx->ei;
843 /* Only one thread should load the cache */
844 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
845 mdb_cache_entryinfo_unlock( cx->ei );
846 ldap_pvt_thread_yield();
847 mdb_cache_entryinfo_lock( cx->ei );
848 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
853 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
855 mdb_cache_entryinfo_unlock( cx->ei );
857 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
858 cx->mdb->bi_db_opflags );
862 cx->data.data = &cx->dbuf;
863 cx->data.ulen = sizeof(ID);
864 cx->data.dlen = sizeof(ID);
865 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
867 /* The first item holds the parent ID. Ignore it. */
868 cx->key.data = &cx->nid;
869 cx->key.size = sizeof(ID);
870 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
872 cx->dbc->c_close( cx->dbc );
876 /* If the on-disk count is zero we've never checked it.
880 cx->dbc->c_count( cx->dbc, &dkids, 0 );
881 cx->ei->bei_dkids = dkids;
884 cx->data.data = cx->buf;
885 cx->data.ulen = MDB_IDL_UM_SIZE * sizeof(ID);
886 cx->data.flags = DB_DBT_USERMEM;
889 /* Fetch the rest of the IDs in a loop... */
890 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
891 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
895 DB_MULTIPLE_INIT( ptr, &cx->data );
897 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
900 diskNode *d = (diskNode *)j;
903 MDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
904 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
905 ei.bei_nrdn.bv_len = nrlen;
906 /* nrdn/rdn are set in-place.
907 * mdb_cache_load will copy them as needed
909 ei.bei_nrdn.bv_val = d->nrdn;
910 ei.bei_rdn.bv_len = len - sizeof(diskNode)
911 - ei.bei_nrdn.bv_len;
912 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
913 mdb_idl_append_one( cx->tmp, ei.bei_id );
914 mdb_cache_load( cx->mdb, &ei, &ei2 );
920 cx->rc = cx->dbc->c_close( cx->dbc );
922 mdb_cache_entryinfo_lock( cx->ei );
923 cx->ei->bei_state &= ~CACHE_ENTRY_ONELEVEL;
924 mdb_cache_entryinfo_unlock( cx->ei );
929 /* The in-memory cache is in sync with the on-disk data.
930 * do we have any kids?
934 if ( cx->ei->bei_ckids > 0 ) {
935 /* Walk the kids tree; order is irrelevant since mdb_idl_sort
936 * will sort it later.
938 avl_apply( cx->ei->bei_kids, apply_func,
939 cx->tmp, -1, AVL_POSTORDER );
941 mdb_cache_entryinfo_unlock( cx->ei );
944 if ( !MDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
945 mdb_idl_sort( cx->tmp, cx->buf );
946 if ( cx->mdb->bi_idl_cache_max_size && !MDB_IDL_IS_ZERO( cx->tmp )) {
947 char *ptr = ((char *)&cx->id)-1;
949 cx->key.size = sizeof(ID)+1;
950 *ptr = DN_ONE_PREFIX;
951 mdb_idl_cache_put( cx->mdb, cx->db, &cx->key, cx->tmp, cx->rc );
955 if ( !MDB_IDL_IS_ZERO( cx->tmp )) {
956 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
957 mdb_idl_append( cx->ids, cx->tmp );
959 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
961 EntryInfo *ei = cx->ei;
963 save = cx->op->o_tmpalloc( MDB_IDL_SIZEOF( cx->tmp ),
964 cx->op->o_tmpmemctx );
965 MDB_IDL_CPY( save, cx->tmp );
969 for ( cx->id = mdb_idl_first( save, &idcurs );
971 cx->id = mdb_idl_next( save, &idcurs )) {
974 if ( mdb_cache_find_id( cx->op, cx->txn, cx->id, &cx->ei,
979 if ( !( ei2->bei_state & CACHE_ENTRY_NO_KIDS )) {
980 MDB_ID2DISK( cx->id, &cx->nid );
981 mdb_dn2idl_internal( cx );
982 if ( !MDB_IDL_IS_ZERO( cx->tmp ))
985 mdb_cache_entryinfo_lock( ei2 );
987 mdb_cache_entryinfo_unlock( ei2 );
991 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
993 mdb_cache_entryinfo_lock( ei );
994 ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
995 mdb_cache_entryinfo_unlock( ei );
998 /* Make sure caller knows it had kids! */
1003 MDB_IDL_CPY( cx->ids, cx->tmp );
1018 struct mdb_info *mdb = (struct mdb_info *)op->o_bd->be_private;
1019 struct dn2id_cookie cx;
1021 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2idl(\"%s\")\n",
1022 ndn->bv_val, 0, 0 );
1024 #ifndef MDB_MULTIPLE_SUFFIXES
1025 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1026 ( ei->bei_id == 0 ||
1027 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len )))
1029 MDB_IDL_ALL( mdb, ids );
1035 MDB_ID2DISK( cx.id, &cx.nid );
1038 cx.db = cx.mdb->bi_dn2id->bdi_db;
1039 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1040 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1043 cx.buf = stack + MDB_IDL_UM_SIZE;
1049 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1053 MDB_IDL_ZERO( ids );
1055 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1056 return LDAP_SUCCESS;
1059 cx.key.ulen = sizeof(ID);
1060 cx.key.size = sizeof(ID);
1061 cx.key.flags = DB_DBT_USERMEM;
1065 mdb_dn2idl_internal(&cx);
1066 if ( cx.need_sort ) {
1067 char *ptr = ((char *)&cx.id)-1;
1068 if ( !MDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1069 mdb_idl_sort( cx.ids, cx.tmp );
1071 cx.key.size = sizeof(ID)+1;
1074 if ( cx.mdb->bi_idl_cache_max_size )
1075 mdb_idl_cache_put( cx.mdb, cx.db, &cx.key, cx.ids, cx.rc );
1078 if ( cx.rc == DB_NOTFOUND )
1079 cx.rc = LDAP_SUCCESS;