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;
731 unsigned int nrlen, rlen;
734 key.mv_size = sizeof(ID);
737 rc = mdb_cursor_open( isc->mt, dbi, &isc->mc );
744 rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
750 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
751 rlen = data.mv_size - sizeof(diskNode) - nrlen;
752 isc->nrdns[isc->numrdns].bv_len = nrlen;
753 isc->nrdns[isc->numrdns].bv_val = d->nrdn;
754 isc->rdns[isc->numrdns].bv_len = rlen;
755 isc->rdns[isc->numrdns].bv_val = d->nrdn+nrlen+1;
759 ptr += data.mv_size - sizeof(ID);
760 memcpy( &id, ptr, sizeof(ID) );
761 x = mdb_idl_search( isc->scopes, id );
762 if ( isc->scopes[x] == id ) {
766 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
774 * We can't just use mdb_idl_fetch_key because
775 * 1 - our data items are longer than just an entry ID
776 * 2 - our data items are sorted alphabetically by nrdn, not by ID.
778 * We descend the tree recursively, so we define this cookie
779 * to hold our necessary state information. The mdb_dn2idl_internal
780 * function uses this cookie when calling itself.
783 struct dn2id_cookie {
784 struct mdb_info *mdb;
809 EntryInfo *ei = data;
812 mdb_idl_append_one( idl, ei->bei_id );
818 struct dn2id_cookie *cx
821 MDB_IDL_ZERO( cx->tmp );
823 if ( cx->mdb->bi_idl_cache_size ) {
824 char *ptr = ((char *)&cx->id)-1;
827 cx->key.size = sizeof(ID)+1;
828 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
829 ID *ids = cx->depth ? cx->tmp : cx->ids;
831 cx->rc = mdb_idl_cache_get(cx->mdb, cx->db, &cx->key, ids);
832 if ( cx->rc == LDAP_SUCCESS ) {
834 mdb_idl_append( cx->ids, cx->tmp );
840 *ptr = DN_ONE_PREFIX;
841 cx->rc = mdb_idl_cache_get(cx->mdb, cx->db, &cx->key, cx->tmp);
842 if ( cx->rc == LDAP_SUCCESS ) {
845 if ( cx->rc == DB_NOTFOUND ) {
850 mdb_cache_entryinfo_lock( cx->ei );
852 /* If number of kids in the cache differs from on-disk, load
853 * up all the kids from the database
855 if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
857 db_recno_t dkids = cx->ei->bei_dkids;
858 ei.bei_parent = cx->ei;
860 /* Only one thread should load the cache */
861 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
862 mdb_cache_entryinfo_unlock( cx->ei );
863 ldap_pvt_thread_yield();
864 mdb_cache_entryinfo_lock( cx->ei );
865 if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
870 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
872 mdb_cache_entryinfo_unlock( cx->ei );
874 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
875 cx->mdb->bi_db_opflags );
879 cx->data.data = &cx->dbuf;
880 cx->data.ulen = sizeof(ID);
881 cx->data.dlen = sizeof(ID);
882 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
884 /* The first item holds the parent ID. Ignore it. */
885 cx->key.data = &cx->nid;
886 cx->key.size = sizeof(ID);
887 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
889 cx->dbc->c_close( cx->dbc );
893 /* If the on-disk count is zero we've never checked it.
897 cx->dbc->c_count( cx->dbc, &dkids, 0 );
898 cx->ei->bei_dkids = dkids;
901 cx->data.data = cx->buf;
902 cx->data.ulen = MDB_IDL_UM_SIZE * sizeof(ID);
903 cx->data.flags = DB_DBT_USERMEM;
906 /* Fetch the rest of the IDs in a loop... */
907 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
908 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
912 DB_MULTIPLE_INIT( ptr, &cx->data );
914 DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
917 diskNode *d = (diskNode *)j;
920 MDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
921 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
922 ei.bei_nrdn.bv_len = nrlen;
923 /* nrdn/rdn are set in-place.
924 * mdb_cache_load will copy them as needed
926 ei.bei_nrdn.bv_val = d->nrdn;
927 ei.bei_rdn.bv_len = len - sizeof(diskNode)
928 - ei.bei_nrdn.bv_len;
929 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
930 mdb_idl_append_one( cx->tmp, ei.bei_id );
931 mdb_cache_load( cx->mdb, &ei, &ei2 );
937 cx->rc = cx->dbc->c_close( cx->dbc );
939 mdb_cache_entryinfo_lock( cx->ei );
940 cx->ei->bei_state &= ~CACHE_ENTRY_ONELEVEL;
941 mdb_cache_entryinfo_unlock( cx->ei );
946 /* The in-memory cache is in sync with the on-disk data.
947 * do we have any kids?
951 if ( cx->ei->bei_ckids > 0 ) {
952 /* Walk the kids tree; order is irrelevant since mdb_idl_sort
953 * will sort it later.
955 avl_apply( cx->ei->bei_kids, apply_func,
956 cx->tmp, -1, AVL_POSTORDER );
958 mdb_cache_entryinfo_unlock( cx->ei );
961 if ( !MDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
962 mdb_idl_sort( cx->tmp, cx->buf );
963 if ( cx->mdb->bi_idl_cache_max_size && !MDB_IDL_IS_ZERO( cx->tmp )) {
964 char *ptr = ((char *)&cx->id)-1;
966 cx->key.size = sizeof(ID)+1;
967 *ptr = DN_ONE_PREFIX;
968 mdb_idl_cache_put( cx->mdb, cx->db, &cx->key, cx->tmp, cx->rc );
972 if ( !MDB_IDL_IS_ZERO( cx->tmp )) {
973 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
974 mdb_idl_append( cx->ids, cx->tmp );
976 if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
978 EntryInfo *ei = cx->ei;
980 save = cx->op->o_tmpalloc( MDB_IDL_SIZEOF( cx->tmp ),
981 cx->op->o_tmpmemctx );
982 MDB_IDL_CPY( save, cx->tmp );
986 for ( cx->id = mdb_idl_first( save, &idcurs );
988 cx->id = mdb_idl_next( save, &idcurs )) {
991 if ( mdb_cache_find_id( cx->op, cx->txn, cx->id, &cx->ei,
996 if ( !( ei2->bei_state & CACHE_ENTRY_NO_KIDS )) {
997 MDB_ID2DISK( cx->id, &cx->nid );
998 mdb_dn2idl_internal( cx );
999 if ( !MDB_IDL_IS_ZERO( cx->tmp ))
1002 mdb_cache_entryinfo_lock( ei2 );
1004 mdb_cache_entryinfo_unlock( ei2 );
1008 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1010 mdb_cache_entryinfo_lock( ei );
1011 ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1012 mdb_cache_entryinfo_unlock( ei );
1015 /* Make sure caller knows it had kids! */
1020 MDB_IDL_CPY( cx->ids, cx->tmp );
1035 struct mdb_info *mdb = (struct mdb_info *)op->o_bd->be_private;
1036 struct dn2id_cookie cx;
1038 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2idl(\"%s\")\n",
1039 ndn->bv_val, 0, 0 );
1041 #ifndef MDB_MULTIPLE_SUFFIXES
1042 if ( op->ors_scope != LDAP_SCOPE_ONELEVEL &&
1043 ( ei->bei_id == 0 ||
1044 ( ei->bei_parent->bei_id == 0 && op->o_bd->be_suffix[0].bv_len )))
1046 MDB_IDL_ALL( mdb, ids );
1052 MDB_ID2DISK( cx.id, &cx.nid );
1055 cx.db = cx.mdb->bi_dn2id->bdi_db;
1056 cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1057 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1060 cx.buf = stack + MDB_IDL_UM_SIZE;
1066 if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1070 MDB_IDL_ZERO( ids );
1072 if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1073 return LDAP_SUCCESS;
1076 cx.key.ulen = sizeof(ID);
1077 cx.key.size = sizeof(ID);
1078 cx.key.flags = DB_DBT_USERMEM;
1082 mdb_dn2idl_internal(&cx);
1083 if ( cx.need_sort ) {
1084 char *ptr = ((char *)&cx.id)-1;
1085 if ( !MDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1086 mdb_idl_sort( cx.ids, cx.tmp );
1088 cx.key.size = sizeof(ID)+1;
1091 if ( cx.mdb->bi_idl_cache_max_size )
1092 mdb_idl_cache_put( cx.mdb, cx.db, &cx.key, cx.ids, cx.rc );
1095 if ( cx.rc == DB_NOTFOUND )
1096 cx.rc = LDAP_SUCCESS;