]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
a358fe0d668f167aab6ae44d7ec4dc72718895d4
[openldap] / servers / slapd / back-bdb / dn2id.c
1 /* dn2id.c - routines to deal with the dn2id index */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2008 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-bdb.h"
23 #include "idl.h"
24 #include "lutil.h"
25
26 #define bdb_dn2id_lock                                  BDB_SYMBOL(dn2id_lock)
27
28 static int
29 bdb_dn2id_lock( struct bdb_info *bdb, struct berval *dn,
30         int rw, DB_TXN *txn, DB_LOCK *lock )
31 {
32         int       rc;
33         DBT       lockobj;
34         int       db_rw;
35
36         if (!txn)
37                 return 0;
38
39         if (rw)
40                 db_rw = DB_LOCK_WRITE;
41         else
42                 db_rw = DB_LOCK_READ;
43
44         lockobj.data = dn->bv_val;
45         lockobj.size = dn->bv_len;
46
47         rc = LOCK_GET(bdb->bi_dbenv, TXN_ID(txn), DB_LOCK_NOWAIT,
48                                         &lockobj, db_rw, lock);
49         return rc;
50 }
51
52 #ifndef BDB_HIER
53 int
54 bdb_dn2id_add(
55         Operation *op,
56         DB_TXN *txn,
57         EntryInfo *eip,
58         Entry           *e )
59 {
60         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
61         DB *db = bdb->bi_dn2id->bdi_db;
62         int             rc;
63         DBT             key, data;
64         ID              nid;
65         char            *buf;
66         struct berval   ptr, pdn;
67
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 );
71
72         DBTzero( &key );
73         key.size = e->e_nname.bv_len + 2;
74         key.ulen = key.size;
75         key.flags = DB_DBT_USERMEM;
76         buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
77         key.data = buf;
78         buf[0] = DN_BASE_PREFIX;
79         ptr.bv_val = buf + 1;
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';
83
84         DBTzero( &data );
85         data.data = &nid;
86         data.size = sizeof( nid );
87         BDB_ID2DISK( e->e_id, &nid );
88
89         /* store it -- don't override */
90         rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
91         if( rc != 0 ) {
92                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add 0x%lx: put failed: %s %d\n",
93                         e->e_id, db_strerror(rc), rc );
94                 goto done;
95         }
96
97 #ifndef BDB_MULTIPLE_SUFFIXES
98         if( !be_issuffix( op->o_bd, &ptr ))
99 #endif
100         {
101                 buf[0] = DN_SUBTREE_PREFIX;
102                 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
103                 if( rc != 0 ) {
104                         Debug( LDAP_DEBUG_ANY,
105                         "=> bdb_dn2id_add 0x%lx: subtree (%s) put failed: %d\n",
106                         e->e_id, ptr.bv_val, rc );
107                         goto done;
108                 }
109                 
110 #ifdef BDB_MULTIPLE_SUFFIXES
111         if( !be_issuffix( op->o_bd, &ptr ))
112 #endif
113         {
114                 dnParent( &ptr, &pdn );
115         
116                 key.size = pdn.bv_len + 2;
117                 key.ulen = key.size;
118                 pdn.bv_val[-1] = DN_ONE_PREFIX;
119                 key.data = pdn.bv_val-1;
120                 ptr = pdn;
121
122                 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
123
124                 if( rc != 0 ) {
125                         Debug( LDAP_DEBUG_ANY,
126                                 "=> bdb_dn2id_add 0x%lx: parent (%s) insert failed: %d\n",
127                                         e->e_id, ptr.bv_val, rc );
128                         goto done;
129                 }
130         }
131
132 #ifndef BDB_MULTIPLE_SUFFIXES
133         while( !be_issuffix( op->o_bd, &ptr ))
134 #else
135         for (;;)
136 #endif
137         {
138                 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
139
140                 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
141
142                 if( rc != 0 ) {
143                         Debug( LDAP_DEBUG_ANY,
144                                 "=> bdb_dn2id_add 0x%lx: subtree (%s) insert failed: %d\n",
145                                         e->e_id, ptr.bv_val, rc );
146                         break;
147                 }
148 #ifdef BDB_MULTIPLE_SUFFIXES
149                 if( be_issuffix( op->o_bd, &ptr )) break;
150 #endif
151                 dnParent( &ptr, &pdn );
152
153                 key.size = pdn.bv_len + 2;
154                 key.ulen = key.size;
155                 key.data = pdn.bv_val - 1;
156                 ptr = pdn;
157         }
158         }
159
160 done:
161         op->o_tmpfree( buf, op->o_tmpmemctx );
162         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
163         return rc;
164 }
165
166 int
167 bdb_dn2id_delete(
168         Operation *op,
169         DB_TXN *txn,
170         EntryInfo       *eip,
171         Entry           *e )
172 {
173         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
174         DB *db = bdb->bi_dn2id->bdi_db;
175         char            *buf;
176         DBT             key;
177         DB_LOCK lock;
178         struct berval   pdn, ptr;
179         int             rc;
180
181         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete 0x%lx: \"%s\"\n",
182                 e->e_id, e->e_ndn, 0 );
183
184         DBTzero( &key );
185         key.size = e->e_nname.bv_len + 2;
186         buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
187         key.data = buf;
188         key.flags = DB_DBT_USERMEM;
189         buf[0] = DN_BASE_PREFIX;
190         ptr.bv_val = buf+1;
191         ptr.bv_len = e->e_nname.bv_len;
192         AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
193         ptr.bv_val[ptr.bv_len] = '\0';
194
195         /* We hold this lock until the TXN completes */
196         rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, txn, &lock );
197         if ( rc ) goto done;
198
199         /* delete it */
200         rc = db->del( db, txn, &key, 0 );
201         if( rc != 0 ) {
202                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete 0x%lx: delete failed: %s %d\n",
203                         e->e_id, db_strerror(rc), rc );
204                 goto done;
205         }
206
207 #ifndef BDB_MULTIPLE_SUFFIXES
208         if( !be_issuffix( op->o_bd, &ptr ))
209 #endif
210         {
211                 buf[0] = DN_SUBTREE_PREFIX;
212                 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
213                 if( rc != 0 ) {
214                         Debug( LDAP_DEBUG_ANY,
215                         "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
216                         e->e_id, ptr.bv_val, rc );
217                         goto done;
218                 }
219
220 #ifdef BDB_MULTIPLE_SUFFIXES
221         if( !be_issuffix( op->o_bd, &ptr ))
222 #endif
223         {
224                 dnParent( &ptr, &pdn );
225
226                 key.size = pdn.bv_len + 2;
227                 key.ulen = key.size;
228                 pdn.bv_val[-1] = DN_ONE_PREFIX;
229                 key.data = pdn.bv_val - 1;
230                 ptr = pdn;
231
232                 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
233
234                 if( rc != 0 ) {
235                         Debug( LDAP_DEBUG_ANY,
236                                 "=> bdb_dn2id_delete 0x%lx: parent (%s) delete failed: %d\n",
237                                 e->e_id, ptr.bv_val, rc );
238                         goto done;
239                 }
240         }
241
242 #ifndef BDB_MULTIPLE_SUFFIXES
243         while( !be_issuffix( op->o_bd, &ptr ))
244 #else
245         for (;;)
246 #endif
247         {
248                 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
249
250                 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
251                 if( rc != 0 ) {
252                         Debug( LDAP_DEBUG_ANY,
253                                 "=> bdb_dn2id_delete 0x%lx: subtree (%s) delete failed: %d\n",
254                                 e->e_id, ptr.bv_val, rc );
255                         goto done;
256                 }
257 #ifdef BDB_MULTIPLE_SUFFIXES
258                 if( be_issuffix( op->o_bd, &ptr )) break;
259 #endif
260                 dnParent( &ptr, &pdn );
261
262                 key.size = pdn.bv_len + 2;
263                 key.ulen = key.size;
264                 key.data = pdn.bv_val - 1;
265                 ptr = pdn;
266         }
267         }
268
269 done:
270         op->o_tmpfree( buf, op->o_tmpmemctx );
271         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
272         return rc;
273 }
274
275 int
276 bdb_dn2id(
277         Operation *op,
278         struct berval   *dn,
279         EntryInfo *ei,
280         DB_TXN *txn,
281         DB_LOCK *lock )
282 {
283         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
284         DB *db = bdb->bi_dn2id->bdi_db;
285         DBC     *cursor;
286         int             rc;
287         DBT             key, data;
288         ID              nid;
289
290         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id(\"%s\")\n", dn->bv_val, 0, 0 );
291
292         DBTzero( &key );
293         key.size = dn->bv_len + 2;
294         key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
295         ((char *)key.data)[0] = DN_BASE_PREFIX;
296         AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
297
298         /* store the ID */
299         DBTzero( &data );
300         data.data = &nid;
301         data.ulen = sizeof(ID);
302         data.flags = DB_DBT_USERMEM;
303
304         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
305         if ( rc ) goto func_leave;
306
307         rc = bdb_dn2id_lock( bdb, dn, 0, txn, lock );
308         if ( rc ) goto nolock;
309
310         /* fetch it */
311         rc = cursor->c_get( cursor, &key, &data, DB_SET );
312
313 nolock:
314         cursor->c_close( cursor );
315 func_leave:
316
317         if( rc != 0 ) {
318                 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
319                         db_strerror( rc ), rc, 0 );
320         } else {
321                 BDB_DISK2ID( &nid, &ei->bei_id );
322                 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%lx\n",
323                         ei->bei_id, 0, 0 );
324         }
325         op->o_tmpfree( key.data, op->o_tmpmemctx );
326         return rc;
327 }
328
329 int
330 bdb_dn2id_children(
331         Operation *op,
332         DB_TXN *txn,
333         Entry *e )
334 {
335         DBT             key, data;
336         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
337         DB *db = bdb->bi_dn2id->bdi_db;
338         ID              id;
339         int             rc;
340
341         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
342                 e->e_nname.bv_val, 0, 0 );
343         DBTzero( &key );
344         key.size = e->e_nname.bv_len + 2;
345         key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
346         ((char *)key.data)[0] = DN_ONE_PREFIX;
347         AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
348
349         if ( bdb->bi_idl_cache_size ) {
350                 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
351                 if ( rc != LDAP_NO_SUCH_OBJECT ) {
352                         op->o_tmpfree( key.data, op->o_tmpmemctx );
353                         return rc;
354                 }
355         }
356         /* we actually could do a empty get... */
357         DBTzero( &data );
358         data.data = &id;
359         data.ulen = sizeof(id);
360         data.flags = DB_DBT_USERMEM;
361         data.doff = 0;
362         data.dlen = sizeof(id);
363
364         rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
365         op->o_tmpfree( key.data, op->o_tmpmemctx );
366
367         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
368                 e->e_nname.bv_val,
369                 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
370                         db_strerror(rc) ), rc );
371
372         return rc;
373 }
374
375 int
376 bdb_dn2idl(
377         Operation *op,
378         DB_TXN *txn,
379         struct berval *ndn,
380         EntryInfo *ei,
381         ID *ids,
382         ID *stack )
383 {
384         int             rc;
385         DBT             key;
386         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
387         DB *db = bdb->bi_dn2id->bdi_db;
388         int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
389                 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
390
391         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
392                 ndn->bv_val, 0, 0 );
393
394 #ifndef BDB_MULTIPLE_SUFFIXES
395         if ( prefix == DN_SUBTREE_PREFIX
396                 && ( ei->bei_id == 0 || ei->bei_parent->bei_id == 0 )) {
397                 BDB_IDL_ALL(bdb, ids);
398                 return 0;
399         }
400 #endif
401
402         DBTzero( &key );
403         key.size = ndn->bv_len + 2;
404         key.ulen = key.size;
405         key.flags = DB_DBT_USERMEM;
406         key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
407         ((char *)key.data)[0] = prefix;
408         AC_MEMCPY( &((char *)key.data)[1], ndn->bv_val, key.size - 1 );
409
410         BDB_IDL_ZERO( ids );
411         rc = bdb_idl_fetch_key( op->o_bd, db, txn, &key, ids, NULL, 0 );
412
413         if( rc != 0 ) {
414                 Debug( LDAP_DEBUG_TRACE,
415                         "<= bdb_dn2idl: get failed: %s (%d)\n",
416                         db_strerror( rc ), rc, 0 );
417
418         } else {
419                 Debug( LDAP_DEBUG_TRACE,
420                         "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
421                         (long) ids[0],
422                         (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
423         }
424
425         op->o_tmpfree( key.data, op->o_tmpmemctx );
426         return rc;
427 }
428
429 #else   /* BDB_HIER */
430 /* Management routines for a hierarchically structured database.
431  *
432  * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
433  * entry in this database is a struct diskNode, keyed by entryID and with
434  * the data containing the RDN and entryID of the node's children. We use
435  * a B-Tree with sorted duplicates to store all the children of a node under
436  * the same key. Also, the first item under the key contains the entry's own
437  * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
438  * well as top-down. To keep this info first in the list, the high bit of all
439  * subsequent nrdnlen's is always set. This means we can only accomodate
440  * RDNs up to length 32767, but that's fine since full DNs are already
441  * restricted to 8192.
442  *
443  * The diskNode is a variable length structure. This definition is not
444  * directly usable for in-memory manipulation.
445  */
446 typedef struct diskNode {
447         unsigned char nrdnlen[2];
448         char nrdn[1];
449         char rdn[1];                        /* variable placement */
450         unsigned char entryID[sizeof(ID)];  /* variable placement */
451 } diskNode;
452
453 /* Sort function for the sorted duplicate data items of a dn2id key.
454  * Sorts based on normalized RDN, in length order.
455  */
456 int
457 hdb_dup_compare(
458         DB *db, 
459         const DBT *usrkey,
460         const DBT *curkey
461 )
462 {
463         diskNode *un, *cn;
464         int rc;
465
466         un = (diskNode *)usrkey->data;
467         cn = (diskNode *)curkey->data;
468
469         /* data is not aligned, cannot compare directly */
470         rc = un->nrdnlen[0] - cn->nrdnlen[0];
471         if ( rc ) return rc;
472         rc = un->nrdnlen[1] - cn->nrdnlen[1];
473         if ( rc ) return rc;
474
475         return strcmp( un->nrdn, cn->nrdn );
476 }
477
478 /* This function constructs a full DN for a given entry.
479  */
480 int hdb_fix_dn(
481         Entry *e,
482         int checkit )
483 {
484         EntryInfo *ei;
485         int rlen = 0, nrlen = 0;
486         char *ptr, *nptr;
487         int max = 0;
488
489         if ( !e->e_id )
490                 return 0;
491
492         /* count length of all DN components */
493         for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
494                 rlen += ei->bei_rdn.bv_len + 1;
495                 nrlen += ei->bei_nrdn.bv_len + 1;
496                 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
497         }
498
499         /* See if the entry DN was invalidated by a subtree rename */
500         if ( checkit ) {
501                 if ( BEI(e)->bei_modrdns >= max ) {
502                         return 0;
503                 }
504                 /* We found a mismatch, tell the caller to lock it */
505                 if ( checkit == 1 ) {
506                         return 1;
507                 }
508                 /* checkit == 2. do the fix. */
509                 free( e->e_name.bv_val );
510                 free( e->e_nname.bv_val );
511         }
512
513         e->e_name.bv_len = rlen - 1;
514         e->e_nname.bv_len = nrlen - 1;
515         e->e_name.bv_val = ch_malloc(rlen);
516         e->e_nname.bv_val = ch_malloc(nrlen);
517         ptr = e->e_name.bv_val;
518         nptr = e->e_nname.bv_val;
519         for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
520                 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
521                 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
522                 if ( ei->bei_parent ) {
523                         *ptr++ = ',';
524                         *nptr++ = ',';
525                 }
526         }
527         BEI(e)->bei_modrdns = max;
528         ptr[-1] = '\0';
529         nptr[-1] = '\0';
530
531         return 0;
532 }
533
534 /* We add two elements to the DN2ID database - a data item under the parent's
535  * entryID containing the child's RDN and entryID, and an item under the
536  * child's entryID containing the parent's entryID.
537  */
538 int
539 hdb_dn2id_add(
540         Operation       *op,
541         DB_TXN *txn,
542         EntryInfo       *eip,
543         Entry           *e )
544 {
545         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
546         DB *db = bdb->bi_dn2id->bdi_db;
547         DBT             key, data;
548         ID              nid;
549         int             rc, rlen, nrlen;
550         diskNode *d;
551         char *ptr;
552
553         Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_add 0x%lx: \"%s\"\n",
554                 e->e_id, e->e_ndn, 0 );
555
556         nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
557         if (nrlen) {
558                 rlen = dn_rdnlen( op->o_bd, &e->e_name );
559         } else {
560                 nrlen = e->e_nname.bv_len;
561                 rlen = e->e_name.bv_len;
562         }
563
564         d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
565         d->nrdnlen[1] = nrlen & 0xff;
566         d->nrdnlen[0] = (nrlen >> 8) | 0x80;
567         ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
568         *ptr++ = '\0';
569         ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
570         *ptr++ = '\0';
571         BDB_ID2DISK( e->e_id, ptr );
572
573         DBTzero(&key);
574         DBTzero(&data);
575         key.size = sizeof(ID);
576         key.flags = DB_DBT_USERMEM;
577         BDB_ID2DISK( eip->bei_id, &nid );
578
579         key.data = &nid;
580
581         /* Need to make dummy root node once. Subsequent attempts
582          * will fail harmlessly.
583          */
584         if ( eip->bei_id == 0 ) {
585                 diskNode dummy = {{0, 0}, "", "", ""};
586                 data.data = &dummy;
587                 data.size = sizeof(diskNode);
588                 data.flags = DB_DBT_USERMEM;
589
590                 db->put( db, txn, &key, &data, DB_NODUPDATA );
591         }
592
593         data.data = d;
594         data.size = sizeof(diskNode) + rlen + nrlen;
595         data.flags = DB_DBT_USERMEM;
596
597         rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
598
599         if (rc == 0) {
600                 BDB_ID2DISK( e->e_id, &nid );
601                 BDB_ID2DISK( eip->bei_id, ptr );
602                 d->nrdnlen[0] ^= 0x80;
603
604                 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
605         }
606
607         /* Update all parents' IDL cache entries */
608         if ( rc == 0 && bdb->bi_idl_cache_size ) {
609                 ID tmp[2];
610                 char *ptr = ((char *)&tmp[1])-1;
611                 key.data = ptr;
612                 key.size = sizeof(ID)+1;
613                 tmp[1] = eip->bei_id;
614                 *ptr = DN_ONE_PREFIX;
615                 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
616                 if ( eip->bei_parent ) {
617                         *ptr = DN_SUBTREE_PREFIX;
618                         for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
619                                 tmp[1] = eip->bei_id;
620                                 bdb_idl_cache_add_id( bdb, db, &key, e->e_id );
621                         }
622                 }
623         }
624
625         op->o_tmpfree( d, op->o_tmpmemctx );
626         Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
627
628         return rc;
629 }
630
631 int
632 hdb_dn2id_delete(
633         Operation       *op,
634         DB_TXN *txn,
635         EntryInfo       *eip,
636         Entry   *e )
637 {
638         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
639         DB *db = bdb->bi_dn2id->bdi_db;
640         DBT             key, data;
641         DBC     *cursor;
642         diskNode *d;
643         int rc;
644         ID      nid;
645         unsigned char dlen[2];
646         DB_LOCK lock;
647
648         Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n",
649                 e->e_id, e->e_ndn, 0 );
650
651         DBTzero(&key);
652         key.size = sizeof(ID);
653         key.ulen = key.size;
654         key.flags = DB_DBT_USERMEM;
655         BDB_ID2DISK( eip->bei_id, &nid );
656
657         DBTzero(&data);
658         data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
659         data.ulen = data.size;
660         data.dlen = data.size;
661         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
662
663         key.data = &nid;
664
665         d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
666         d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
667         d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
668         dlen[0] = d->nrdnlen[0];
669         dlen[1] = d->nrdnlen[1];
670         strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
671         data.data = d;
672
673         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
674         if ( rc ) goto func_leave;
675
676         /* We hold this lock until the TXN completes */
677         rc = bdb_dn2id_lock( bdb, &e->e_nname, 1, txn, &lock );
678         if ( rc ) goto nolock;
679
680         /* Delete our ID from the parent's list */
681         rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
682         if ( rc == 0 ) {
683                 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
684                         !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
685                         rc = cursor->c_del( cursor, 0 );
686                 else
687                         rc = DB_NOTFOUND;
688         }
689
690         /* Delete our ID from the tree. With sorted duplicates, this
691          * will leave any child nodes still hanging around. This is OK
692          * for modrdn, which will add our info back in later.
693          */
694         if ( rc == 0 ) {
695                 BDB_ID2DISK( e->e_id, &nid );
696                 rc = cursor->c_get( cursor, &key, &data, DB_SET );
697                 if ( rc == 0 )
698                         rc = cursor->c_del( cursor, 0 );
699         }
700
701 nolock:
702         cursor->c_close( cursor );
703 func_leave:
704         op->o_tmpfree( d, op->o_tmpmemctx );
705
706         /* Delete IDL cache entries */
707         if ( rc == 0 && bdb->bi_idl_cache_size ) {
708                 ID tmp[2];
709                 char *ptr = ((char *)&tmp[1])-1;
710                 key.data = ptr;
711                 key.size = sizeof(ID)+1;
712                 tmp[1] = eip->bei_id;
713                 *ptr = DN_ONE_PREFIX;
714                 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
715                 if ( eip ->bei_parent ) {
716                         *ptr = DN_SUBTREE_PREFIX;
717                         for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) {
718                                 tmp[1] = eip->bei_id;
719                                 bdb_idl_cache_del_id( bdb, db, &key, e->e_id );
720                         }
721                 }
722         }
723         Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 );
724         return rc;
725 }
726
727
728 int
729 hdb_dn2id(
730         Operation       *op,
731         struct berval   *in,
732         EntryInfo       *ei,
733         DB_TXN *txn,
734         DB_LOCK *lock )
735 {
736         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
737         DB *db = bdb->bi_dn2id->bdi_db;
738         DBT             key, data;
739         DBC     *cursor;
740         int             rc = 0, nrlen;
741         diskNode *d;
742         char    *ptr;
743         unsigned char dlen[2];
744         ID idp, parentID;
745
746         Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 );
747
748         nrlen = dn_rdnlen( op->o_bd, in );
749         if (!nrlen) nrlen = in->bv_len;
750
751         DBTzero(&key);
752         key.size = sizeof(ID);
753         key.data = &idp;
754         key.ulen = sizeof(ID);
755         key.flags = DB_DBT_USERMEM;
756         parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
757         BDB_ID2DISK( parentID, &idp );
758
759         DBTzero(&data);
760         data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
761         data.ulen = data.size * 3;
762         data.dlen = data.ulen;
763         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
764
765         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
766         if ( rc ) return rc;
767
768         d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
769         d->nrdnlen[1] = nrlen & 0xff;
770         d->nrdnlen[0] = (nrlen >> 8) | 0x80;
771         dlen[0] = d->nrdnlen[0];
772         dlen[1] = d->nrdnlen[1];
773         ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
774         *ptr = '\0';
775         data.data = d;
776
777         rc = bdb_dn2id_lock( bdb, in, 0, txn, lock );
778         if ( rc ) goto func_leave;
779
780         rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
781         if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
782                 strncmp( d->nrdn, in->bv_val, nrlen ))) {
783                 rc = DB_NOTFOUND;
784         }
785         if ( rc == 0 ) {
786                 ptr = (char *) data.data + data.size - sizeof(ID);
787                 BDB_DISK2ID( ptr, &ei->bei_id );
788                 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
789                 ptr = d->nrdn + nrlen + 1;
790                 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
791                 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
792                         db_recno_t dkids;
793                         /* How many children does the parent have? */
794                         /* FIXME: do we need to lock the parent
795                          * entryinfo? Seems safe...
796                          */
797                         cursor->c_count( cursor, &dkids, 0 );
798                         ei->bei_parent->bei_dkids = dkids;
799                 }
800         }
801
802 func_leave:
803         cursor->c_close( cursor );
804         op->o_tmpfree( d, op->o_tmpmemctx );
805         if( rc != 0 ) {
806                 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: get failed: %s (%d)\n",
807                         db_strerror( rc ), rc, 0 );
808         } else {
809                 Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: got id=0x%lx\n",
810                         ei->bei_id, 0, 0 );
811         }
812
813         return rc;
814 }
815
816 int
817 hdb_dn2id_parent(
818         Operation *op,
819         DB_TXN *txn,
820         EntryInfo *ei,
821         ID *idp )
822 {
823         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
824         DB *db = bdb->bi_dn2id->bdi_db;
825         DBT             key, data;
826         DBC     *cursor;
827         int             rc = 0;
828         diskNode *d;
829         char    *ptr;
830         ID      nid;
831
832         DBTzero(&key);
833         key.size = sizeof(ID);
834         key.data = &nid;
835         key.ulen = sizeof(ID);
836         key.flags = DB_DBT_USERMEM;
837         BDB_ID2DISK( ei->bei_id, &nid );
838
839         DBTzero(&data);
840         data.flags = DB_DBT_USERMEM;
841
842         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
843         if ( rc ) return rc;
844
845         data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
846         d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
847         data.data = d;
848
849         rc = cursor->c_get( cursor, &key, &data, DB_SET );
850         if ( rc == 0 ) {
851                 if (d->nrdnlen[0] & 0x80) {
852                         rc = LDAP_OTHER;
853                 } else {
854                         db_recno_t dkids;
855                         ptr = (char *) data.data + data.size - sizeof(ID);
856                         BDB_DISK2ID( ptr, idp );
857                         ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
858                         ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
859                         ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
860                                 ei->bei_nrdn.bv_len;
861                         ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
862                         ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
863                         /* How many children does this node have? */
864                         cursor->c_count( cursor, &dkids, 0 );
865                         ei->bei_dkids = dkids;
866                 }
867         }
868         cursor->c_close( cursor );
869         op->o_tmpfree( d, op->o_tmpmemctx );
870         return rc;
871 }
872
873 int
874 hdb_dn2id_children(
875         Operation *op,
876         DB_TXN *txn,
877         Entry *e )
878 {
879         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
880         DB *db = bdb->bi_dn2id->bdi_db;
881         DBT             key, data;
882         DBC             *cursor;
883         int             rc;
884         ID              id;
885         diskNode d;
886
887         DBTzero(&key);
888         key.size = sizeof(ID);
889         key.data = &e->e_id;
890         key.flags = DB_DBT_USERMEM;
891         BDB_ID2DISK( e->e_id, &id );
892
893         /* IDL cache is in host byte order */
894         if ( bdb->bi_idl_cache_size ) {
895                 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
896                 if ( rc != LDAP_NO_SUCH_OBJECT ) {
897                         return rc;
898                 }
899         }
900
901         key.data = &id;
902         DBTzero(&data);
903         data.data = &d;
904         data.ulen = sizeof(d);
905         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
906         data.dlen = sizeof(d);
907
908         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
909         if ( rc ) return rc;
910
911         rc = cursor->c_get( cursor, &key, &data, DB_SET );
912         if ( rc == 0 ) {
913                 db_recno_t dkids;
914                 rc = cursor->c_count( cursor, &dkids, 0 );
915                 if ( rc == 0 ) {
916                         BEI(e)->bei_dkids = dkids;
917                         if ( dkids < 2 ) rc = DB_NOTFOUND;
918                 }
919         }
920         cursor->c_close( cursor );
921         return rc;
922 }
923
924 /* bdb_dn2idl:
925  * We can't just use bdb_idl_fetch_key because
926  * 1 - our data items are longer than just an entry ID
927  * 2 - our data items are sorted alphabetically by nrdn, not by ID.
928  *
929  * We descend the tree recursively, so we define this cookie
930  * to hold our necessary state information. The bdb_dn2idl_internal
931  * function uses this cookie when calling itself.
932  */
933
934 struct dn2id_cookie {
935         struct bdb_info *bdb;
936         Operation *op;
937         DB_TXN *txn;
938         EntryInfo *ei;
939         ID *ids;
940         ID *tmp;
941         ID *buf;
942         DB *db;
943         DBC *dbc;
944         DBT key;
945         DBT data;
946         ID dbuf;
947         ID id;
948         ID nid;
949         int rc;
950         int depth;
951         char need_sort;
952         char prefix;
953 };
954
955 static int
956 apply_func(
957         void *data,
958         void *arg )
959 {
960         EntryInfo *ei = data;
961         ID *idl = arg;
962
963         bdb_idl_append_one( idl, ei->bei_id );
964         return 0;
965 }
966
967 static int
968 hdb_dn2idl_internal(
969         struct dn2id_cookie *cx
970 )
971 {
972         BDB_IDL_ZERO( cx->tmp );
973
974         if ( cx->bdb->bi_idl_cache_size ) {
975                 char *ptr = ((char *)&cx->id)-1;
976
977                 cx->key.data = ptr;
978                 cx->key.size = sizeof(ID)+1;
979                 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
980                         ID *ids = cx->depth ? cx->tmp : cx->ids;
981                         *ptr = cx->prefix;
982                         cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, ids);
983                         if ( cx->rc == LDAP_SUCCESS ) {
984                                 if ( cx->depth ) {
985                                         bdb_idl_append( cx->ids, cx->tmp );
986                                         cx->need_sort = 1;
987                                 }
988                                 return cx->rc;
989                         }
990                 }
991                 *ptr = DN_ONE_PREFIX;
992                 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
993                 if ( cx->rc == LDAP_SUCCESS ) {
994                         goto gotit;
995                 }
996                 if ( cx->rc == DB_NOTFOUND ) {
997                         return cx->rc;
998                 }
999         }
1000
1001         bdb_cache_entryinfo_lock( cx->ei );
1002
1003         /* If number of kids in the cache differs from on-disk, load
1004          * up all the kids from the database
1005          */
1006         if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
1007                 EntryInfo ei;
1008                 db_recno_t dkids = cx->ei->bei_dkids;
1009                 ei.bei_parent = cx->ei;
1010
1011                 /* Only one thread should load the cache */
1012                 while ( cx->ei->bei_state & CACHE_ENTRY_ONELEVEL ) {
1013                         bdb_cache_entryinfo_unlock( cx->ei );
1014                         ldap_pvt_thread_yield();
1015                         bdb_cache_entryinfo_lock( cx->ei );
1016                         if ( cx->ei->bei_ckids+1 == cx->ei->bei_dkids ) {
1017                                 goto synced;
1018                         }
1019                 }
1020
1021                 cx->ei->bei_state |= CACHE_ENTRY_ONELEVEL;
1022
1023                 bdb_cache_entryinfo_unlock( cx->ei );
1024
1025                 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
1026                         cx->bdb->bi_db_opflags );
1027                 if ( cx->rc )
1028                         goto done_one;
1029
1030                 cx->data.data = &cx->dbuf;
1031                 cx->data.ulen = sizeof(ID);
1032                 cx->data.dlen = sizeof(ID);
1033                 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
1034
1035                 /* The first item holds the parent ID. Ignore it. */
1036                 cx->key.data = &cx->nid;
1037                 cx->key.size = sizeof(ID);
1038                 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
1039                 if ( cx->rc ) {
1040                         cx->dbc->c_close( cx->dbc );
1041                         goto done_one;
1042                 }
1043
1044                 /* If the on-disk count is zero we've never checked it.
1045                  * Count it now.
1046                  */
1047                 if ( !dkids ) {
1048                         cx->dbc->c_count( cx->dbc, &dkids, 0 );
1049                         cx->ei->bei_dkids = dkids;
1050                 }
1051
1052                 cx->data.data = cx->buf;
1053                 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
1054                 cx->data.flags = DB_DBT_USERMEM;
1055
1056                 if ( dkids > 1 ) {
1057                         /* Fetch the rest of the IDs in a loop... */
1058                         while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
1059                                 DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
1060                                 u_int8_t *j;
1061                                 size_t len;
1062                                 void *ptr;
1063                                 DB_MULTIPLE_INIT( ptr, &cx->data );
1064                                 while (ptr) {
1065                                         DB_MULTIPLE_NEXT( ptr, &cx->data, j, len );
1066                                         if (j) {
1067                                                 EntryInfo *ei2;
1068                                                 diskNode *d = (diskNode *)j;
1069                                                 short nrlen;
1070
1071                                                 BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
1072                                                 nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
1073                                                 ei.bei_nrdn.bv_len = nrlen;
1074                                                 /* nrdn/rdn are set in-place.
1075                                                  * hdb_cache_load will copy them as needed
1076                                                  */
1077                                                 ei.bei_nrdn.bv_val = d->nrdn;
1078                                                 ei.bei_rdn.bv_len = len - sizeof(diskNode)
1079                                                         - ei.bei_nrdn.bv_len;
1080                                                 ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
1081                                                 bdb_idl_append_one( cx->tmp, ei.bei_id );
1082                                                 hdb_cache_load( cx->bdb, &ei, &ei2 );
1083                                         }
1084                                 }
1085                         }
1086                 }
1087
1088                 cx->rc = cx->dbc->c_close( cx->dbc );
1089 done_one:
1090                 bdb_cache_entryinfo_lock( cx->ei );
1091                 cx->ei->bei_state ^= CACHE_ENTRY_ONELEVEL;
1092                 bdb_cache_entryinfo_unlock( cx->ei );
1093                 if ( cx->rc )
1094                         return cx->rc;
1095
1096         } else {
1097                 /* The in-memory cache is in sync with the on-disk data.
1098                  * do we have any kids?
1099                  */
1100 synced:
1101                 cx->rc = 0;
1102                 if ( cx->ei->bei_ckids > 0 ) {
1103                         /* Walk the kids tree; order is irrelevant since bdb_idl_sort
1104                          * will sort it later.
1105                          */
1106                         avl_apply( cx->ei->bei_kids, apply_func,
1107                                 cx->tmp, -1, AVL_POSTORDER );
1108                 }
1109                 bdb_cache_entryinfo_unlock( cx->ei );
1110         }
1111
1112         if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
1113                 bdb_idl_sort( cx->tmp, cx->buf );
1114         if ( cx->bdb->bi_idl_cache_max_size && !BDB_IDL_IS_ZERO( cx->tmp )) {
1115                 char *ptr = ((char *)&cx->id)-1;
1116                 cx->key.data = ptr;
1117                 cx->key.size = sizeof(ID)+1;
1118                 *ptr = DN_ONE_PREFIX;
1119                 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
1120         }
1121
1122 gotit:
1123         if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
1124                 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
1125                         bdb_idl_append( cx->ids, cx->tmp );
1126                         cx->need_sort = 1;
1127                         if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
1128                                 ID *save, idcurs;
1129                                 EntryInfo *ei = cx->ei;
1130                                 int nokids = 1;
1131                                 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
1132                                         cx->op->o_tmpmemctx );
1133                                 BDB_IDL_CPY( save, cx->tmp );
1134
1135                                 idcurs = 0;
1136                                 cx->depth++;
1137                                 for ( cx->id = bdb_idl_first( save, &idcurs );
1138                                         cx->id != NOID;
1139                                         cx->id = bdb_idl_next( save, &idcurs )) {
1140                                         cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
1141                                         if ( !cx->ei ||
1142                                                 ( cx->ei->bei_state & CACHE_ENTRY_NO_KIDS ))
1143                                                 continue;
1144
1145                                         BDB_ID2DISK( cx->id, &cx->nid );
1146                                         hdb_dn2idl_internal( cx );
1147                                         if ( !BDB_IDL_IS_ZERO( cx->tmp ))
1148                                                 nokids = 0;
1149                                 }
1150                                 cx->depth--;
1151                                 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
1152                                 if ( nokids ) {
1153                                         bdb_cache_entryinfo_lock( ei );
1154                                         ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
1155                                         bdb_cache_entryinfo_unlock( ei );
1156                                 }
1157                         }
1158                         /* Make sure caller knows it had kids! */
1159                         cx->tmp[0]=1;
1160
1161                         cx->rc = 0;
1162                 } else {
1163                         BDB_IDL_CPY( cx->ids, cx->tmp );
1164                 }
1165         }
1166         return cx->rc;
1167 }
1168
1169 int
1170 hdb_dn2idl(
1171         Operation       *op,
1172         DB_TXN *txn,
1173         struct berval *ndn,
1174         EntryInfo       *ei,
1175         ID *ids,
1176         ID *stack )
1177 {
1178         struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1179         struct dn2id_cookie cx;
1180
1181         Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1182                 ndn->bv_val, 0, 0 );
1183
1184 #ifndef BDB_MULTIPLE_SUFFIXES
1185         if ( op->ors_scope != LDAP_SCOPE_ONELEVEL && 
1186                 ( ei->bei_id == 0 ||
1187                 ei->bei_parent->bei_id == 0 ))
1188         {
1189                 BDB_IDL_ALL( bdb, ids );
1190                 return 0;
1191         }
1192 #endif
1193
1194         cx.id = ei->bei_id;
1195         BDB_ID2DISK( cx.id, &cx.nid );
1196         cx.ei = ei;
1197         cx.bdb = bdb;
1198         cx.db = cx.bdb->bi_dn2id->bdi_db;
1199         cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1200                 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1201         cx.ids = ids;
1202         cx.tmp = stack;
1203         cx.buf = stack + BDB_IDL_UM_SIZE;
1204         cx.op = op;
1205         cx.txn = txn;
1206         cx.need_sort = 0;
1207         cx.depth = 0;
1208
1209         if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1210                 ids[0] = 1;
1211                 ids[1] = cx.id;
1212         } else {
1213                 BDB_IDL_ZERO( ids );
1214         }
1215         if ( cx.ei->bei_state & CACHE_ENTRY_NO_KIDS )
1216                 return LDAP_SUCCESS;
1217
1218         DBTzero(&cx.key);
1219         cx.key.ulen = sizeof(ID);
1220         cx.key.size = sizeof(ID);
1221         cx.key.flags = DB_DBT_USERMEM;
1222
1223         DBTzero(&cx.data);
1224
1225         hdb_dn2idl_internal(&cx);
1226         if ( cx.need_sort ) {
1227                 char *ptr = ((char *)&cx.id)-1;
1228                 if ( !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 ) 
1229                         bdb_idl_sort( cx.ids, cx.tmp );
1230                 cx.key.data = ptr;
1231                 cx.key.size = sizeof(ID)+1;
1232                 *ptr = cx.prefix;
1233                 cx.id = ei->bei_id;
1234                 if ( cx.bdb->bi_idl_cache_max_size )
1235                         bdb_idl_cache_put( cx.bdb, cx.db, &cx.key, cx.ids, cx.rc );
1236         }
1237
1238         if ( cx.rc == DB_NOTFOUND )
1239                 cx.rc = LDAP_SUCCESS;
1240
1241         return cx.rc;
1242 }
1243 #endif  /* BDB_HIER */