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