]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
Changed dn_rdn/dn_rdnlen to struct berval
[openldap] / servers / slapd / back-bdb / dn2id.c
1 /* dn2id.c - routines to deal with the dn2id index */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "idl.h"
15
16 #ifndef BDB_HIER
17 int
18 bdb_dn2id_add(
19         BackendDB       *be,
20         DB_TXN *txn,
21         struct berval   *pbv,
22         Entry           *e )
23 {
24         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
25         DB *db = bdb->bi_dn2id->bdi_db;
26         int             rc;
27         DBT             key, data;
28         char            *buf, *ptr, *pdn;
29
30         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
31                 e->e_ndn, (long) e->e_id, 0 );
32         assert( e->e_id != NOID );
33
34         DBTzero( &key );
35         key.size = e->e_nname.bv_len + 2;
36         buf = ch_malloc( key.size );
37         key.data = buf;
38         buf[0] = DN_BASE_PREFIX;
39         ptr = buf + 1;
40         AC_MEMCPY( ptr, e->e_ndn, key.size - 1 );
41
42         DBTzero( &data );
43         data.data = (char *) &e->e_id;
44         data.size = sizeof( e->e_id );
45
46         /* store it -- don't override */
47         rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
48         if( rc != 0 ) {
49                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
50                         db_strerror(rc), rc, 0 );
51                 goto done;
52         }
53
54         if( !be_issuffix( be, ptr )) {
55                 buf[0] = DN_SUBTREE_PREFIX;
56                 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
57                 if( rc != 0 ) {
58                         Debug( LDAP_DEBUG_ANY,
59                         "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
60                         ptr, rc, 0 );
61                         goto done;
62                 }
63         }
64
65         pdn = dn_parent( be, ptr );
66
67         if( pdn != NULL ) {
68                 key.size -= pdn - ptr;
69                 pdn[-1] = DN_ONE_PREFIX;
70                 key.data = pdn - 1;
71
72                 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
73
74                 if( rc != 0 ) {
75                         Debug( LDAP_DEBUG_ANY,
76                                 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
77                                         pdn, rc, 0 );
78                         goto done;
79                 }
80         }
81
82         while( pdn != NULL ) {
83                 if ( be_issuffix( be, pdn ))
84                         break;
85                 pdn[-1] = DN_SUBTREE_PREFIX;
86
87                 rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
88
89                 if( rc != 0 ) {
90                         Debug( LDAP_DEBUG_ANY,
91                                 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
92                                         pdn, rc, 0 );
93                         break;
94                 }
95                 ptr = pdn;
96                 pdn = dn_parent( be, pdn );
97                 key.size -= pdn - ptr;
98                 key.data = pdn - 1;
99         }
100
101 done:
102         ch_free( buf );
103         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
104         return rc;
105 }
106
107 int
108 bdb_dn2id_delete(
109         BackendDB       *be,
110         DB_TXN *txn,
111         char    *pdn,
112         Entry           *e )
113 {
114         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
115         DB *db = bdb->bi_dn2id->bdi_db;
116         int             rc;
117         DBT             key;
118         char            *buf, *ptr;
119
120         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
121                 e->e_ndn, e->e_id, 0 );
122
123         DBTzero( &key );
124         key.size = e->e_nname.bv_len + 2;
125         buf = ch_malloc( key.size );
126         key.data = buf;
127         key.flags = DB_DBT_USERMEM;
128         buf[0] = DN_BASE_PREFIX;
129         ptr = buf+1;
130         AC_MEMCPY( ptr, e->e_ndn, key.size - 1 );
131
132         /* delete it */
133         rc = db->del( db, txn, &key, 0 );
134         if( rc != 0 ) {
135                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
136                         db_strerror(rc), rc, 0 );
137                 goto done;
138         }
139
140         if( !be_issuffix( be, ptr )) {
141                 buf[0] = DN_SUBTREE_PREFIX;
142                 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
143                 if( rc != 0 ) {
144                         Debug( LDAP_DEBUG_ANY,
145                         "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
146                         ptr, rc, 0 );
147                         goto done;
148                 }
149         }
150
151         pdn = dn_parent( be, ptr );
152
153         if( pdn != NULL ) {
154                 key.size -= pdn - ptr;
155                 pdn[-1] = DN_ONE_PREFIX;
156                 key.data = pdn - 1;
157
158                 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
159
160                 if( rc != 0 ) {
161                         Debug( LDAP_DEBUG_ANY,
162                                 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
163                                 pdn, rc, 0 );
164                         goto done;
165                 }
166         }
167
168         while( pdn != NULL ) {
169                 if ( be_issuffix( be, pdn ))
170                         break;
171
172                 pdn[-1] = DN_SUBTREE_PREFIX;
173
174                 rc = bdb_idl_delete_key( be, db, txn, &key, e->e_id );
175                 if( rc != 0 ) {
176                         Debug( LDAP_DEBUG_ANY,
177                                 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
178                                 pdn, rc, 0 );
179                         goto done;
180                 }
181                 ptr = pdn;
182                 pdn = dn_parent( be, pdn );
183                 key.size -= pdn - ptr;
184                 key.data = pdn - 1;
185         }
186
187 done:
188         ch_free( buf );
189         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
190         return rc;
191 }
192
193 int
194 bdb_dn2id(
195         BackendDB       *be,
196         DB_TXN *txn,
197         struct berval   *dn,
198         ID *id )
199 {
200         int             rc;
201         DBT             key, data;
202         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
203         DB *db = bdb->bi_dn2id->bdi_db;
204
205         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
206
207         DBTzero( &key );
208         key.size = dn->bv_len + 2;
209         key.data = ch_malloc( key.size );
210         ((char *)key.data)[0] = DN_BASE_PREFIX;
211         AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
212
213         /* store the ID */
214         DBTzero( &data );
215         data.data = id;
216         data.ulen = sizeof(ID);
217         data.flags = DB_DBT_USERMEM;
218
219         /* fetch it */
220         rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
221
222         if( rc != 0 ) {
223                 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
224                         db_strerror( rc ), rc, 0 );
225         } else {
226                 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
227                         *id, 0, 0 );
228         }
229
230         ch_free( key.data );
231         return rc;
232 }
233
234 int
235 bdb_dn2id_matched(
236         BackendDB       *be,
237         DB_TXN *txn,
238         struct berval   *in,
239         ID *id,
240         char **matchedDN )
241 {
242         int             rc;
243         DBT             key, data;
244         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
245         DB *db = bdb->bi_dn2id->bdi_db;
246         char            *buf, *dn;
247
248         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", in->bv_val, 0, 0 );
249
250         DBTzero( &key );
251         key.size = in->bv_len + 2;
252         buf = ch_malloc( key.size );
253         key.data = buf;
254         dn = buf+1;
255         AC_MEMCPY( dn, in->bv_val, key.size - 1 );
256
257         /* store the ID */
258         DBTzero( &data );
259         data.data = id;
260         data.ulen = sizeof(ID);
261         data.flags = DB_DBT_USERMEM;
262
263         *matchedDN = NULL;
264
265         while(1) {
266                 dn[-1] = DN_BASE_PREFIX;
267
268                 *id = NOID;
269
270                 /* fetch it */
271                 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
272
273                 if( rc == DB_NOTFOUND ) {
274                         char *pdn = dn_parent( be, dn );
275
276                         if( pdn == NULL || *pdn == '\0' ) {
277                                 Debug( LDAP_DEBUG_TRACE,
278                                         "<= bdb_dn2id_matched: no match\n",
279                                         0, 0, 0 );
280                                 break;
281                         }
282
283                         key.size -= pdn - dn;
284                         dn = pdn;
285                         key.data = pdn - 1;
286
287                 } else if ( rc == 0 ) {
288                         if( data.size != sizeof( ID ) ) {
289                                 Debug( LDAP_DEBUG_ANY,
290                                         "<= bdb_dn2id_matched: get size mismatch: "
291                                         "expected %ld, got %ld\n",
292                                         (long) sizeof(ID), (long) data.size, 0 );
293                         }
294
295                         if( dn != buf+1 ) {
296                                 *matchedDN = (char *) dn;
297                         }
298
299                         Debug( LDAP_DEBUG_TRACE,
300                                 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
301                                 (long) *id, *matchedDN == NULL ? "entry" : "matched", dn );
302                         break;
303
304                 } else {
305                         Debug( LDAP_DEBUG_ANY,
306                                 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
307                                 db_strerror(rc), rc, 0 );
308                         break;
309                 }
310         }
311
312         ch_free( buf );
313         return rc;
314 }
315
316 int
317 bdb_dn2id_children(
318         BackendDB       *be,
319         DB_TXN *txn,
320         struct berval *dn )
321 {
322         int             rc;
323         DBT             key, data;
324         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
325         DB *db = bdb->bi_dn2id->bdi_db;
326         ID              id;
327
328         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
329                 dn->bv_val, 0, 0 );
330
331         DBTzero( &key );
332         key.size = dn->bv_len + 2;
333         key.data = ch_malloc( key.size );
334         ((char *)key.data)[0] = DN_ONE_PREFIX;
335         AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
336
337         /* we actually could do a empty get... */
338         DBTzero( &data );
339         data.data = &id;
340         data.ulen = sizeof(id);
341         data.flags = DB_DBT_USERMEM;
342         data.doff = 0;
343         data.dlen = sizeof(id);
344
345         rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
346
347         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
348                 dn,
349                 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
350                         db_strerror(rc) ), rc );
351
352         return rc;
353 }
354
355 int
356 bdb_dn2idl(
357         BackendDB       *be,
358         struct berval   *dn,
359         int prefix,
360         ID *ids )
361 {
362         int             rc;
363         DBT             key;
364         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
365         DB *db = bdb->bi_dn2id->bdi_db;
366
367         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
368
369         if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val))
370         {
371                 BDB_IDL_ALL(bdb, ids);
372                 return 0;
373         }
374
375         DBTzero( &key );
376         key.size = dn->bv_len + 2;
377         key.data = ch_malloc( key.size );
378         ((char *)key.data)[0] = prefix;
379         AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
380
381         rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
382
383         if( rc != 0 ) {
384                 Debug( LDAP_DEBUG_TRACE,
385                         "<= bdb_dn2idl: get failed: %s (%d)\n",
386                         db_strerror( rc ), rc, 0 );
387
388         } else {
389                 Debug( LDAP_DEBUG_TRACE,
390                         "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
391                         (long) ids[0],
392                         (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
393         }
394
395         ch_free( key.data );
396         return rc;
397 }
398 #else   /* BDB_HIER */
399
400 /* Experimental management routines for a hierarchically structured backend.
401  *
402  * Unsupported! Use at your own risk!
403  *
404  * Instead of a dn2id database, we use an id2parent database. Each entry in
405  * this database is a struct diskNode, containing the ID of the node's parent
406  * and the RDN of the node.
407  */
408 typedef struct diskNode {
409         ID parent;
410         struct berval rdn;
411         struct berval nrdn;
412 } diskNode;
413
414 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
415  * database into memory (into an AVL tree). Next we iterate through each node
416  * of this tree, connecting each child to its parent. The nodes in this AVL
417  * tree are a struct idNode. The immediate (Onelevel) children of a node are
418  * referenced in the i_kids AVL tree. With this arrangement, there is no need
419  * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
420  * the DN of an entry is constructed by walking up the list of i_parent
421  * pointers, so no full DN is stored on disk anywhere. This makes modrdn
422  * extremely efficient, even when operating on a populated subtree.
423  *
424  * The idNode tree is searched directly from the root when performing id to
425  * entry lookups. The tree is traversed using the i_kids subtrees when
426  * performing dn to id lookups.
427  */
428 typedef struct idNode {
429         ID i_id;
430         struct idNode *i_parent;
431         diskNode *i_rdn;
432         Avlnode *i_kids;
433         ldap_pvt_thread_rdwr_t i_kids_rdwr;
434 } idNode;
435
436
437 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
438  * sorted in lexical order. These are the various helper routines used
439  * for the searches and sorts.
440  */
441 static int
442 node_find_cmp(
443         ID id,
444         idNode *n
445 )
446 {
447         return id - n->i_id;
448 }
449
450 static int
451 node_frdn_cmp(
452         char *nrdn,
453         idNode *n
454 )
455 {
456         return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
457 }
458
459 static int
460 node_add_cmp(
461         idNode *a,
462         idNode *b
463 )
464 {
465         return a->i_id - b->i_id;
466 }
467
468 static int
469 node_rdn_cmp(
470         idNode *a,
471         idNode *b
472 )
473 {
474         return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
475 }
476
477 idNode * bdb_find_id_node(
478         ID id,
479         Avlnode *tree
480 )
481 {
482         return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
483 }
484
485 idNode * bdb_find_rdn_node(
486         char *nrdn,
487         Avlnode *tree
488 )
489 {
490         return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
491 }
492
493 /* This function links a node into its parent's i_kids tree. */
494 int bdb_insert_kid(
495         idNode *a,
496         Avlnode *tree
497 )
498 {
499         int rc;
500
501         if (a->i_rdn->parent == 0)
502                 return 0;
503         a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
504         if (!a->i_parent)
505                 return -1;
506         ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
507         rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
508                 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
509         ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
510         return rc;
511 }
512
513 /* This function adds a node into the main AVL tree */
514 idNode *bdb_add_node(
515         ID id,
516         char *d,
517         struct bdb_info *bdb
518 )
519 {
520         idNode *node;
521
522         node = (idNode *)ch_malloc(sizeof(idNode));
523         node->i_id = id;
524         node->i_parent = NULL;
525         node->i_kids = NULL;
526         node->i_rdn = (diskNode *)d;
527         node->i_rdn->rdn.bv_val += (long)d;
528         node->i_rdn->nrdn.bv_val += (long)d;
529         ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
530         avl_insert( &bdb->bi_tree, (caddr_t) node,
531                         (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
532         if (id == 1)
533                 bdb->bi_troot = node;
534         return node;
535 }
536
537 /* This function initializes the trees at startup time. */
538 int bdb_build_tree(
539         Backend *be
540 )
541 {
542         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
543         int i, rc;
544         DBC *cursor;
545         DBT key, data;
546         ID id;
547         idNode *node;
548         char **rdns;
549
550         bdb->bi_tree = NULL;
551
552         rc = bdb->bi_id2parent->bdi_db->cursor(
553                 bdb->bi_id2parent->bdi_db, NULL, &cursor,
554                 bdb->bi_db_opflags );
555         if( rc != 0 ) {
556                 return NOID;
557         }
558
559         /* When be_suffix is turned into struct berval or LDAPDN
560          * life will get a lot easier... Since no DNs live on disk, we
561          * need to operate on the be_suffix to fully qualify our DNs.
562          * We need to know how many components are in the suffix DN,
563          * so we can tell where the suffix ends and our nodes begin.
564          *
565          * Note that this code always uses be_suffix[0], so defining
566          * multiple suffixes for a single backend won't work!
567          */
568         rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
569         for (i=0; rdns[i]; i++);
570         bdb->bi_nrdns = i;
571         charray_free(rdns);
572
573         DBTzero( &key );
574         DBTzero( &data );
575         key.data = (char *)&id;
576         key.ulen = sizeof( id );
577         key.flags = DB_DBT_USERMEM;
578         data.flags = DB_DBT_MALLOC;
579
580         while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
581                 bdb_add_node( id, data.data, bdb );
582         }
583         cursor->c_close( cursor );
584
585         rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
586                 -1, AVL_INORDER );
587
588         return rc;
589 }
590
591 /* This function constructs a full DN for a given id. We really should
592  * be passing idNodes directly, to save some effort...
593  */
594 int bdb_fix_dn(
595         BackendDB *be,
596         ID id,
597         Entry *e
598 )
599 {
600         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
601         idNode *n, *o;
602         int rlen, nrlen;
603         char *ptr, *nptr;
604         
605         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
606         o = bdb_find_id_node(id, bdb->bi_tree);
607         rlen = be->be_suffix[0]->bv_len + 1;
608         nrlen = be->be_nsuffix[0]->bv_len + 1;
609         for (n = o; n; n=n->i_parent) {
610                 rlen += n->i_rdn->rdn.bv_len + 1;
611                 nrlen += n->i_rdn->nrdn.bv_len + 1;
612         }
613         e->e_name.bv_len = rlen - 1;
614         e->e_nname.bv_len = nrlen - 1;
615         e->e_name.bv_val = ch_malloc(rlen + nrlen);
616         e->e_nname.bv_val = e->e_name.bv_val + rlen;
617         ptr = e->e_name.bv_val;
618         nptr = e->e_nname.bv_val;
619         for (n = o; n; n=n->i_parent) {
620                 ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
621                 *ptr++ = ',';
622                 nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
623                 *nptr++ = ',';
624         }
625         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
626
627         ptr--;
628         nptr--;
629         strcpy(ptr, be->be_suffix[0]->bv_val);
630         strcpy(nptr, be->be_nsuffix[0]->bv_val);
631
632         return 0;
633 }
634
635 int
636 bdb_dn2id_add(
637         BackendDB       *be,
638         DB_TXN *txn,
639         struct berval   *pdn,
640         Entry           *e )
641 {
642         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
643         int             rc, rlen, nrlen;
644         DBT             key, data;
645         DB *db = bdb->bi_id2parent->bdi_db;
646         diskNode *d;
647         idNode *n;
648
649         nrlen = dn_rdnlen( be, &e->e_nname );
650         if (nrlen) {
651                 rlen = dn_rdnlen( be, &e->e_name );
652         } else {
653                 rlen = 0;
654         }
655
656         d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
657         d->rdn.bv_len = rlen;
658         d->nrdn.bv_len = nrlen;
659         d->rdn.bv_val = (char *)(d+1);
660         d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
661         strncpy(d->rdn.bv_val, e->e_dn, rlen);
662         d->rdn.bv_val[rlen] = '\0';
663         strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
664         d->nrdn.bv_val[nrlen] = '\0';
665         d->rdn.bv_val -= (long)d;
666         d->nrdn.bv_val -= (long)d;
667
668         if (pdn) {
669                 bdb_dn2id(be, txn, pdn, &d->parent);
670         } else {
671                 d->parent = 0;
672         }
673
674         DBTzero(&key);
675         DBTzero(&data);
676         key.data = &e->e_id;
677         key.size = sizeof(ID);
678         key.flags = DB_DBT_USERMEM;
679
680         data.data = d;
681         data.size = sizeof(diskNode) + rlen + nrlen + 2;
682         data.flags = DB_DBT_USERMEM;
683
684         rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
685
686         if (rc == 0) {
687                 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
688                 n = bdb_add_node( e->e_id, data.data, bdb);
689                 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
690
691                 if (d->parent) {
692                         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
693                         bdb_insert_kid(n, bdb->bi_tree);
694                         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
695                 }
696         } else {
697                 free(d);
698         }
699         return rc;
700 }
701
702 int
703 bdb_dn2id_delete(
704         BackendDB       *be,
705         DB_TXN *txn,
706         char    *pdn,
707         Entry   *e )
708 {
709         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
710         int rc;
711         DBT             key;
712         DB *db = bdb->bi_id2parent->bdi_db;
713         idNode *n;
714
715         DBTzero(&key);
716         key.size = sizeof(e->e_id);
717         key.data = &e->e_id;
718
719         rc = db->del( db, txn, &key, 0);
720
721         ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
722         n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
723         if (n) {
724                 if (n->i_parent) {
725                         ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
726                         avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
727                                 (AVL_CMP)node_frdn_cmp);
728                         ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
729                 }
730                 free(n->i_rdn);
731                 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
732                 free(n);
733         }
734         if (e->e_id == 1)
735                 bdb->bi_troot = NULL;
736         ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
737
738         return rc;
739 }
740
741 int
742 bdb_dn2id_matched(
743         BackendDB       *be,
744         DB_TXN *txn,
745         struct berval   *in,
746         ID *id,
747         char **matchedDN )
748 {
749         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
750         int             i;
751         char            **rdns;
752         idNode *n, *p;
753
754         if (!bdb->bi_troot)
755                 return DB_NOTFOUND;
756
757         p = bdb->bi_troot;
758         if (be_issuffix(be, in->bv_val)) {
759                 *id = p->i_id;
760                 return 0;
761         }
762
763         rdns = ldap_explode_dn(in->bv_val, 0);
764         for (i=0; rdns[i]; i++);
765         i -= bdb->bi_nrdns;
766         if (i < 0)
767                 return -1;
768         n = p;
769         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
770         for (--i; i>=0; i--) {
771                 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
772                 n = bdb_find_rdn_node(rdns[i], p->i_kids);
773                 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
774                 if (!n) break;
775                 p = n;
776         }
777         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
778
779         if (n) {
780                 *id = n->i_id;
781         } else if (matchedDN) {
782                 int len = 0, j;
783                 char *ptr;
784                 ++i;
785                 for (j=i; rdns[j]; j++)
786                         len += strlen(rdns[j]) + 1;
787                 ptr = ch_malloc(len);
788                 *matchedDN = ptr;
789                 for (;rdns[i]; i++) {
790                         ptr = slap_strcopy(ptr, rdns[i]);
791                         *ptr++ = ',';
792                 }
793                 ptr[-1] = '\0';
794         }
795         return n ? 0 : DB_NOTFOUND;
796 }
797
798 int
799 bdb_dn2id(
800         BackendDB       *be,
801         DB_TXN *txn,
802         struct berval   *dn,
803         ID *id )
804 {
805         return bdb_dn2id_matched(be, txn, dn, id, NULL);
806 }
807
808 int
809 bdb_dn2id_children(
810         BackendDB       *be,
811         DB_TXN *txn,
812         struct berval   *dn )
813 {
814         int             rc;
815         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
816         ID              id;
817         idNode *n;
818
819         rc = bdb_dn2id(be, txn, dn, &id);
820         if (rc != 0)
821                 return rc;
822
823         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
824         n = bdb_find_id_node(id, bdb->bi_tree);
825         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
826
827         if (!n->i_kids)
828                 return DB_NOTFOUND;
829         else
830                 return 0;
831 }
832
833 /* Since we don't store IDLs for onelevel or subtree, we have to construct
834  * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
835  */
836 static int
837 insert_one(
838         idNode *n,
839         ID *ids
840 )
841 {
842         return bdb_idl_insert(ids, n->i_id);
843 }
844
845 static int
846 insert_sub(
847         idNode *n,
848         ID *ids
849 )
850 {
851         int rc;
852
853         rc = bdb_idl_insert(ids, n->i_id);
854         if (rc == 0) {
855                 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
856                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
857                         AVL_INORDER);
858                 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
859         }
860         return rc;
861 }
862
863 int
864 bdb_dn2idl(
865         BackendDB       *be,
866         struct berval   *dn,
867         int prefix,
868         ID *ids )
869 {
870         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
871         int             rc;
872         ID              id;
873         idNode          *n;
874
875         if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val)) {
876                 BDB_IDL_ALL(bdb, ids);
877                 return 0;
878         }
879
880         rc = bdb_dn2id(be, NULL, dn, &id);
881         if (rc) return rc;
882
883         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
884         n = bdb_find_id_node(id, bdb->bi_tree);
885         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
886
887         ids[0] = 0;
888         ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
889         if (prefix == DN_ONE_PREFIX) {
890                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
891                         AVL_INORDER);
892         } else {
893                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
894                         AVL_INORDER);
895         }
896         ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
897         return rc;
898 }
899 #endif  /* BDB_HIER */