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;
152 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_add 0x%lx: \"%s\"\n",
153 e->e_id, e->e_ndn, 0 );
155 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
157 rlen = dn_rdnlen( op->o_bd, &e->e_name );
159 nrlen = e->e_nname.bv_len;
160 rlen = e->e_name.bv_len;
163 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
164 d->nrdnlen[1] = nrlen & 0xff;
165 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
166 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
168 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
170 memcpy( ptr, &e->e_id, sizeof( ID ));
172 key.mv_size = sizeof(ID);
177 rc = mdb_cursor_open( txn, dbi, &mc );
181 /* Need to make dummy root node once. Subsequent attempts
182 * will fail harmlessly.
185 diskNode dummy = {{0, 0}, "", "", ""};
186 data.mv_data = &dummy;
187 data.mv_size = sizeof(diskNode);
189 mdb_cursor_put( mc, &key, &data, MDB_NODUPDATA );
193 data.mv_size = sizeof(diskNode) + rlen + nrlen;
195 rc = mdb_cursor_put( mc, &key, &data, MDB_NODUPDATA );
199 memcpy( ptr, &pid, sizeof( ID ));
200 d->nrdnlen[0] ^= 0x80;
202 rc = mdb_cursor_put( mc, &key, &data, MDB_NODUPDATA );
204 mdb_cursor_close( mc );
207 op->o_tmpfree( d, op->o_tmpmemctx );
208 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
220 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
221 MDB_dbi dbi = mdb->mi_dn2id;
227 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_delete 0x%lx: \"%s\"\n",
228 e->e_id, e->e_ndn, 0 );
230 key.mv_size = sizeof(ID);
234 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
235 data.mv_size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
237 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
238 d->nrdnlen[1] = nrlen & 0xff;
239 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
240 memcpy( d->nrdn, e->e_nname.bv_val, nrlen );
241 d->nrdn[nrlen] = '\0';
244 /* Delete our ID from the parent's list */
245 rc = mdb_del( txn, dbi, &key, &data );
247 /* Delete our ID from the tree. With sorted duplicates, this
248 * will leave any child nodes still hanging around. This is OK
249 * for modrdn, which will add our info back in later.
253 d->nrdnlen[0] ^= 0x80;
254 rc = mdb_del( txn, dbi, &key, &data );
257 op->o_tmpfree( d, op->o_tmpmemctx );
259 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
263 /* return last found ID in *id if no match */
270 struct berval *matched,
271 struct berval *nmatched )
273 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
275 MDB_dbi dbi = mdb->mi_dn2id;
280 char dn[SLAP_LDAPDN_MAXLEN];
284 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
287 matched->bv_val = dn + sizeof(dn) - 1;
289 *matched->bv_val-- = '\0';
292 nmatched->bv_len = 0;
293 nmatched->bv_val = 0;
304 if ( op->o_bd->be_nsuffix[0].bv_len ) {
305 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
307 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
309 for ( ptr = tmp.bv_val + tmp.bv_len - 1; ptr >= tmp.bv_val; ptr-- )
310 if (DN_SEPARATOR(*ptr))
313 tmp.bv_len -= ptr - tmp.bv_val;
317 key.mv_size = sizeof(ID);
319 rc = mdb_cursor_open( txn, dbi, &cursor );
326 data.mv_size = sizeof(diskNode) + tmp.bv_len;
327 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
328 d->nrdnlen[1] = tmp.bv_len & 0xff;
329 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
330 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
333 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
334 op->o_tmpfree( d, op->o_tmpmemctx );
337 ptr = (char *) data.mv_data + data.mv_size - sizeof(ID);
338 memcpy( &nid, ptr, sizeof(ID));
340 /* grab the non-normalized RDN */
344 rlen = data.mv_size - sizeof(diskNode) - tmp.bv_len;
345 matched->bv_len += rlen;
346 matched->bv_val -= rlen + 1;
347 ptr = lutil_strcopy( matched->bv_val, d->rdn + tmp.bv_len );
354 nmatched->bv_val = tmp.bv_val;
357 if ( tmp.bv_val > in->bv_val ) {
358 for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
359 !DN_SEPARATOR(*ptr); ptr--) /* empty */;
360 if ( ptr >= in->bv_val ) {
361 if (DN_SEPARATOR(*ptr)) ptr++;
362 tmp.bv_len = tmp.bv_val - ptr - 1;
370 mdb_cursor_close( cursor );
373 if ( matched->bv_len ) {
374 ptr = op->o_tmpalloc( matched->bv_len+1, op->o_tmpmemctx );
375 strcpy( ptr, matched->bv_val );
376 matched->bv_val = ptr;
378 if ( BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) && !nid ) {
379 ber_dupbv( matched, (struct berval *)&slap_empty_bv );
381 matched->bv_val = NULL;
386 if ( nmatched->bv_val ) {
387 nmatched->bv_len = in->bv_len - (nmatched->bv_val - in->bv_val);
389 *nmatched = slap_empty_bv;
394 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: get failed: %s (%d)\n",
395 mdb_strerror( rc ), rc, 0 );
397 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: got id=0x%lx\n",
404 /* return IDs from root to parent of DN */
412 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
414 MDB_dbi dbi = mdb->mi_dn2id;
422 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2sups(\"%s\")\n", in->bv_val, 0, 0 );
430 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
432 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
434 key.mv_size = sizeof(ID);
436 rc = mdb_cursor_open( txn, dbi, &cursor );
443 data.mv_size = sizeof(diskNode) + tmp.bv_len;
444 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
445 d->nrdnlen[1] = tmp.bv_len & 0xff;
446 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
447 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
450 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
451 op->o_tmpfree( d, op->o_tmpmemctx );
453 mdb_cursor_close( cursor );
456 ptr = (char *) data.mv_data + data.mv_size - sizeof(ID);
457 memcpy( &nid, ptr, sizeof(ID));
460 mdb_idl_insert( ids, pid );
462 if ( tmp.bv_val > in->bv_val ) {
463 for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
464 !DN_SEPARATOR(*ptr); ptr--) /* empty */;
465 if ( ptr >= in->bv_val ) {
466 if (DN_SEPARATOR(*ptr)) ptr++;
467 tmp.bv_len = tmp.bv_val - ptr - 1;
477 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2sups: get failed: %s (%d)\n",
478 mdb_strerror( rc ), rc, 0 );
492 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
493 DB *db = mdb->bi_dn2id->bdi_db;
502 key.size = sizeof(ID);
504 key.ulen = sizeof(ID);
505 key.flags = DB_DBT_USERMEM;
506 MDB_ID2DISK( ei->bei_id, &nid );
509 data.flags = DB_DBT_USERMEM;
511 rc = db->cursor( db, txn, &cursor, mdb->bi_db_opflags );
514 data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
515 d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
518 rc = cursor->c_get( cursor, &key, &data, DB_SET );
520 if (d->nrdnlen[0] & 0x80) {
524 ptr = (char *) data.data + data.size - sizeof(ID);
525 MDB_DISK2ID( ptr, idp );
526 ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
527 ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
528 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
530 ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
531 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
532 /* How many children does this node have? */
533 cursor->c_count( cursor, &dkids, 0 );
534 ei->bei_dkids = dkids;
537 cursor->c_close( cursor );
538 op->o_tmpfree( d, op->o_tmpmemctx );
549 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
550 MDB_dbi dbi = mdb->mi_dn2id;
556 key.mv_size = sizeof(ID);
560 rc = mdb_cursor_open( txn, dbi, &cursor );
563 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
566 rc = mdb_cursor_count( cursor, &dkids );
568 if ( dkids < 2 ) rc = MDB_NOTFOUND;
571 mdb_cursor_close( cursor );
582 struct berval *nname )
584 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
585 MDB_dbi dbi = mdb->mi_dn2id;
589 char dn[SLAP_LDAPDN_MAXLEN], ndn[SLAP_LDAPDN_MAXLEN], *ptr;
593 key.mv_size = sizeof(ID);
596 rc = mdb_cursor_open( txn, dbi, cursp );
606 unsigned int nrlen, rlen;
610 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
613 ptr += data.mv_size - sizeof(ID);
614 memcpy( &id, ptr, sizeof(ID) );
616 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
617 rlen = data.mv_size - sizeof(diskNode) - nrlen;
618 assert( nrlen < 1024 && rlen < 1024 ); /* FIXME: Sanity check */
623 /* copy name and trailing NUL */
624 memcpy( nptr, d->nrdn, nrlen+1 );
625 memcpy( dptr, d->nrdn+nrlen+1, rlen+1 );
630 name->bv_len = dptr - dn;
631 nname->bv_len = nptr - ndn;
632 name->bv_val = op->o_tmpalloc( name->bv_len + 1, op->o_tmpmemctx );
633 nname->bv_val = op->o_tmpalloc( nname->bv_len + 1, op->o_tmpmemctx );
634 memcpy( name->bv_val, dn, name->bv_len );
635 name->bv_val[name->bv_len] = '\0';
636 memcpy( nname->bv_val, ndn, nname->bv_len );
637 nname->bv_val[nname->bv_len] = '\0';
642 /* Find each id in ids that is a child of base and move it to res.
652 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
653 MDB_dbi dbi = mdb->mi_dn2id;
656 ID ida, id, cid, ci0, idc = 0;
660 key.mv_size = sizeof(ID);
664 rc = mdb_cursor_open( txn, dbi, &cursor );
667 ida = mdb_idl_first( ids, &cid );
669 /* Don't bother moving out of ids if it's a range */
670 if (!MDB_IDL_IS_RANGE(ids)) {
675 while (ida != NOID) {
679 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
681 /* not found, move on to next */
690 ptr += data.mv_size - sizeof(ID);
691 memcpy( &id, ptr, sizeof(ID) );
705 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
708 ida = mdb_idl_next( ids, &cid );
710 if (!MDB_IDL_IS_RANGE( ids ))
713 mdb_cursor_close( cursor );
717 /* See if base is a child of any of the scopes
724 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
725 MDB_dbi dbi = mdb->mi_dn2id;
732 unsigned int nrlen, rlen;
735 key.mv_size = sizeof(ID);
738 rc = mdb_cursor_open( isc->mt, dbi, &isc->mc );
746 rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
753 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
754 rlen = data.mv_size - sizeof(diskNode) - nrlen;
755 isc->nrdns[isc->numrdns].bv_len = nrlen;
756 isc->nrdns[isc->numrdns].bv_val = d->nrdn;
757 isc->rdns[isc->numrdns].bv_len = rlen;
758 isc->rdns[isc->numrdns].bv_val = d->nrdn+nrlen+1;
761 if (!rc && id != isc->id) {
764 mdb_id2l_insert( isc->scopes, &id2 );
767 ptr += data.mv_size - sizeof(ID);
768 memcpy( &id, ptr, sizeof(ID) );
769 x = mdb_id2l_search( isc->scopes, id );
770 if ( x <= isc->scopes[0].mid && isc->scopes[x].mid == id ) {
771 if ( !isc->scopes[x].mval.mv_data ) {
775 data = isc->scopes[x].mval;
778 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
786 * We can't just use mdb_idl_fetch_key because
787 * 1 - our data items are longer than just an entry ID
788 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
790 * We descend the tree recursively, so we define this cookie
791 * to hold our necessary state information. The mdb_dn2idl_internal
792 * function uses this cookie when calling itself.
795 struct dn2id_cookie {
796 struct mdb_info *mdb;
821 EntryInfo *ei = data;
824 mdb_idl_append_one( idl, ei->bei_id );
830 struct dn2id_cookie *cx
833 MDB_IDL_ZERO( cx->tmp );
835 if ( cx->mdb->bi_idl_cache_size ) {
836 char *ptr = ((char *)&cx->id)-1;
839 cx->key.size = sizeof(ID)+1;
840 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
841 ID *ids = cx->depth ? cx->tmp : cx->ids;
843 cx->rc = mdb_idl_cache_get(cx->mdb, cx->db, &cx->key, ids);
844 if ( cx->rc == LDAP_SUCCESS ) {
846 mdb_idl_append( cx->ids, cx->tmp );
852 *ptr = DN_ONE_PREFIX;
853 cx->rc = mdb_idl_cache_get(cx->mdb, cx->db, &cx->key, cx->tmp);
854 if ( cx->rc == LDAP_SUCCESS ) {
857 if ( cx->rc == DB_NOTFOUND ) {
862 mdb_cache_entryinfo_lock( cx->ei );
864 /* If number of kids in the cache differs from on-disk, load
865 * up all the kids from the database
867 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
869 db_recno_t dkids = cx->ei->bei_dkids;
870 ei.bei_parent = cx->ei;
872 /* Only one thread should load the cache */
873 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
874 mdb_cache_entryinfo_unlock( cx->ei );
875 ldap_pvt_thread_yield();
876 mdb_cache_entryinfo_lock( cx->ei );
877 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
882 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
884 mdb_cache_entryinfo_unlock( cx->ei );
886 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
887 cx->mdb->bi_db_opflags );
891 cx->data.data = &cx->dbuf;
892 cx->data.ulen = sizeof(ID);
893 cx->data.dlen = sizeof(ID);
894 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
896 /* The first item holds the parent ID. Ignore it. */
897 cx->key.data = &cx->nid;
898 cx->key.size = sizeof(ID);
899 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
901 cx->dbc->c_close( cx->dbc );
905 /* If the on-disk count is zero we've never checked it.
909 cx->dbc->c_count( cx->dbc, &dkids, 0 );
910 cx->ei->bei_dkids = dkids;
913 cx->data.data = cx->buf;
914 cx->data.ulen = MDB_IDL_UM_SIZE * sizeof(ID);
915 cx->data.flags = DB_DBT_USERMEM;
918 /* Fetch the rest of the IDs in a loop... */
919 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
920 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
924 DB_MULTIPLE_INIT( ptr, &cx->data );
926 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
929 diskNode *d = (diskNode *)j;
932 MDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
933 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
934 ei.bei_nrdn.bv_len = nrlen;
935 /* nrdn/rdn are set in-place.
936 * mdb_cache_load will copy them as needed
938 ei.bei_nrdn.bv_val = d->nrdn;
939 ei.bei_rdn.bv_len = len - sizeof(diskNode)
940 - ei.bei_nrdn.bv_len;
941 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
942 mdb_idl_append_one( cx->tmp, ei.bei_id );
943 mdb_cache_load( cx->mdb, &ei, &ei2 );
949 cx->rc = cx->dbc->c_close( cx->dbc );
951 mdb_cache_entryinfo_lock( cx->ei );
952 cx->ei->bei_state &= ~CACHE_ENTRY_ONELEVEL;
953 mdb_cache_entryinfo_unlock( cx->ei );
958 /* The in-memory cache is in sync with the on-disk data.
959 * do we have any kids?
963 if ( cx->ei->bei_ckids > 0 ) {
964 /* Walk the kids tree; order is irrelevant since mdb_idl_sort
965 * will sort it later.
967 avl_apply( cx->ei->bei_kids, apply_func,
968 cx->tmp, -1, AVL_POSTORDER );
970 mdb_cache_entryinfo_unlock( cx->ei );
973 if ( !MDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
974 mdb_idl_sort( cx->tmp, cx->buf );
975 if ( cx->mdb->bi_idl_cache_max_size && !MDB_IDL_IS_ZERO( cx->tmp )) {
976 char *ptr = ((char *)&cx->id)-1;
978 cx->key.size = sizeof(ID)+1;
979 *ptr = DN_ONE_PREFIX;
980 mdb_idl_cache_put( cx->mdb, cx->db, &cx->key, cx->tmp, cx->rc );
984 if ( !MDB_IDL_IS_ZERO( cx->tmp )) {
985 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
986 mdb_idl_append( cx->ids, cx->tmp );
988 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
990 EntryInfo *ei = cx->ei;
992 save = cx->op->o_tmpalloc( MDB_IDL_SIZEOF( cx->tmp ),
993 cx->op->o_tmpmemctx );
994 MDB_IDL_CPY( save, cx->tmp );
998 for ( cx->id = mdb_idl_first( save, &idcurs );
1000 cx->id = mdb_idl_next( save, &idcurs )) {
1003 if ( mdb_cache_find_id( cx->op, cx->txn, cx->id, &cx->ei,
1008 if ( !( ei2->bei_state & CACHE_ENTRY_NO_KIDS )) {
1009 MDB_ID2DISK( cx->id, &cx->nid );
1010 mdb_dn2idl_internal( cx );
1011 if ( !MDB_IDL_IS_ZERO( cx->tmp ))
1014 mdb_cache_entryinfo_lock( ei2 );
1016 mdb_cache_entryinfo_unlock( ei2 );
1020 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1022 mdb_cache_entryinfo_lock( ei );
1023 ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1024 mdb_cache_entryinfo_unlock( ei );
1027 /* Make sure caller knows it had kids! */
1032 MDB_IDL_CPY( cx->ids, cx->tmp );
1047 struct mdb_info *mdb = (struct mdb_info *)op->o_bd->be_private;
1048 struct dn2id_cookie cx;
1050 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2idl(\"%s\")\n",
1051 ndn->bv_val, 0, 0 );
1053 #ifndef MDB_MULTIPLE_SUFFIXES
1054 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1055 ( ei->bei_id == 0 ||
1056 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len )))
1058 MDB_IDL_ALL( mdb, ids );
1064 MDB_ID2DISK( cx.id, &cx.nid );
1067 cx.db = cx.mdb->bi_dn2id->bdi_db;
1068 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1069 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1072 cx.buf = stack + MDB_IDL_UM_SIZE;
1078 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1082 MDB_IDL_ZERO( ids );
1084 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1085 return LDAP_SUCCESS;
1088 cx.key.ulen = sizeof(ID);
1089 cx.key.size = sizeof(ID);
1090 cx.key.flags = DB_DBT_USERMEM;
1094 mdb_dn2idl_internal(&cx);
1095 if ( cx.need_sort ) {
1096 char *ptr = ((char *)&cx.id)-1;
1097 if ( !MDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1098 mdb_idl_sort( cx.ids, cx.tmp );
1100 cx.key.size = sizeof(ID)+1;
1103 if ( cx.mdb->bi_idl_cache_max_size )
1104 mdb_idl_cache_put( cx.mdb, cx.db, &cx.key, cx.ids, cx.rc );
1107 if ( cx.rc == DB_NOTFOUND )
1108 cx.rc = LDAP_SUCCESS;