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