]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
Moved bdb_strcopy to slap_strcopy
[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         char *pdn,
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;
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         const char      *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, 0, 0 );
206
207         DBTzero( &key );
208         key.size = strlen( dn ) + 2;
209         key.data = ch_malloc( key.size );
210         ((char *)key.data)[0] = DN_BASE_PREFIX;
211         AC_MEMCPY( &((char *)key.data)[1], dn, 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         const char      *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         const char *dn = in;
247
248         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
249
250         DBTzero( &key );
251         key.size = strlen( dn ) + 2;
252         key.data = ch_malloc( key.size );
253         ((char *)key.data)[0] = DN_BASE_PREFIX;
254
255         /* store the ID */
256         DBTzero( &data );
257         data.data = id;
258         data.ulen = sizeof(ID);
259         data.flags = DB_DBT_USERMEM;
260
261         *matchedDN = NULL;
262
263         while(1) {
264                 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
265
266                 *id = NOID;
267
268                 /* fetch it */
269                 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
270
271                 if( rc == DB_NOTFOUND ) {
272                         char *pdn = dn_parent( be, dn );
273
274                         if( pdn == NULL || *pdn == '\0' ) {
275                                 Debug( LDAP_DEBUG_TRACE,
276                                         "<= bdb_dn2id_matched: no match\n",
277                                         0, 0, 0 );
278                                 break;
279                         }
280
281                         dn = pdn;
282                         key.size = strlen( dn ) + 2;
283
284                 } else if ( rc == 0 ) {
285                         if( data.size != sizeof( ID ) ) {
286                                 Debug( LDAP_DEBUG_ANY,
287                                         "<= bdb_dn2id_matched: get size mismatch: "
288                                         "expected %ld, got %ld\n",
289                                         (long) sizeof(ID), (long) data.size, 0 );
290                         }
291
292                         if( in != dn ) {
293                                 *matchedDN = (char *) dn;
294                         }
295
296                         Debug( LDAP_DEBUG_TRACE,
297                                 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
298                                 (long) *id, *matchedDN == NULL ? "entry" : "matched", dn );
299                         break;
300
301                 } else {
302                         Debug( LDAP_DEBUG_ANY,
303                                 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
304                                 db_strerror(rc), rc, 0 );
305                         break;
306                 }
307         }
308
309         ch_free( key.data );
310         return rc;
311 }
312
313 int
314 bdb_dn2id_children(
315         BackendDB       *be,
316         DB_TXN *txn,
317         const char *dn )
318 {
319         int             rc;
320         DBT             key, data;
321         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
322         DB *db = bdb->bi_dn2id->bdi_db;
323         ID              id;
324
325         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
326                 dn, 0, 0 );
327
328         DBTzero( &key );
329         key.size = strlen( dn ) + 2;
330         key.data = ch_malloc( key.size );
331         ((char *)key.data)[0] = DN_ONE_PREFIX;
332         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
333
334         /* we actually could do a empty get... */
335         DBTzero( &data );
336         data.data = &id;
337         data.ulen = sizeof(id);
338         data.flags = DB_DBT_USERMEM;
339         data.doff = 0;
340         data.dlen = sizeof(id);
341
342         rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
343
344         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
345                 dn,
346                 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
347                         db_strerror(rc) ), rc );
348
349         return rc;
350 }
351
352 int
353 bdb_dn2idl(
354         BackendDB       *be,
355         const char      *dn,
356         int prefix,
357         ID *ids )
358 {
359         int             rc;
360         DBT             key;
361         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
362         DB *db = bdb->bi_dn2id->bdi_db;
363
364         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn, 0, 0 );
365
366         if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
367         {
368                 BDB_IDL_ALL(bdb, ids);
369                 return 0;
370         }
371
372         DBTzero( &key );
373         key.size = strlen( dn ) + 2;
374         key.data = ch_malloc( key.size );
375         ((char *)key.data)[0] = prefix;
376         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
377
378         rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
379
380         if( rc != 0 ) {
381                 Debug( LDAP_DEBUG_TRACE,
382                         "<= bdb_dn2idl: get failed: %s (%d)\n",
383                         db_strerror( rc ), rc, 0 );
384
385         } else {
386                 Debug( LDAP_DEBUG_TRACE,
387                         "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
388                         (long) ids[0],
389                         (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
390         }
391
392         ch_free( key.data );
393         return rc;
394 }
395 #else   /* BDB_HIER */
396
397 /* Experimental management routines for a hierarchically structured backend.
398  *
399  * Unsupported! Use at your own risk!
400  *
401  * Instead of a dn2id database, we use an id2parent database. Each entry in
402  * this database is a struct diskNode, containing the ID of the node's parent
403  * and the RDN of the node.
404  */
405 typedef struct diskNode {
406         ID parent;
407         struct berval rdn;
408         struct berval nrdn;
409 } diskNode;
410
411 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
412  * database into memory (into an AVL tree). Next we iterate through each node
413  * of this tree, connecting each child to its parent. The nodes in this AVL
414  * tree are a struct idNode. The immediate (Onelevel) children of a node are
415  * referenced in the i_kids AVL tree. With this arrangement, there is no need
416  * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
417  * the DN of an entry is constructed by walking up the list of i_parent
418  * pointers, so no full DN is stored on disk anywhere. This makes modrdn
419  * extremely efficient, even when operating on a populated subtree.
420  *
421  * The idNode tree is searched directly from the root when performing id to
422  * entry lookups. The tree is traversed using the i_kids subtrees when
423  * performing dn to id lookups.
424  */
425 typedef struct idNode {
426         ID i_id;
427         struct idNode *i_parent;
428         diskNode *i_rdn;
429         Avlnode *i_kids;
430         ldap_pvt_thread_rdwr_t i_kids_rdwr;
431 } idNode;
432
433
434 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
435  * sorted in lexical order. These are the various helper routines used
436  * for the searches and sorts.
437  */
438 static int
439 node_find_cmp(
440         ID id,
441         idNode *n
442 )
443 {
444         return id - n->i_id;
445 }
446
447 static int
448 node_frdn_cmp(
449         char *nrdn,
450         idNode *n
451 )
452 {
453         return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
454 }
455
456 static int
457 node_add_cmp(
458         idNode *a,
459         idNode *b
460 )
461 {
462         return a->i_id - b->i_id;
463 }
464
465 static int
466 node_rdn_cmp(
467         idNode *a,
468         idNode *b
469 )
470 {
471         return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
472 }
473
474 idNode * bdb_find_id_node(
475         ID id,
476         Avlnode *tree
477 )
478 {
479         return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
480 }
481
482 idNode * bdb_find_rdn_node(
483         char *nrdn,
484         Avlnode *tree
485 )
486 {
487         return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
488 }
489
490 /* This function links a node into its parent's i_kids tree. */
491 int bdb_insert_kid(
492         idNode *a,
493         Avlnode *tree
494 )
495 {
496         int rc;
497
498         if (a->i_rdn->parent == 0)
499                 return 0;
500         a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
501         if (!a->i_parent)
502                 return -1;
503         ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
504         rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
505                 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
506         ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
507         return rc;
508 }
509
510 /* This function adds a node into the main AVL tree */
511 idNode *bdb_add_node(
512         ID id,
513         char *d,
514         struct bdb_info *bdb
515 )
516 {
517         idNode *node;
518
519         node = (idNode *)ch_malloc(sizeof(idNode));
520         node->i_id = id;
521         node->i_parent = NULL;
522         node->i_kids = NULL;
523         node->i_rdn = (diskNode *)d;
524         node->i_rdn->rdn.bv_val += (long)d;
525         node->i_rdn->nrdn.bv_val += (long)d;
526         ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
527         avl_insert( &bdb->bi_tree, (caddr_t) node,
528                         (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
529         if (id == 1)
530                 bdb->bi_troot = node;
531         return node;
532 }
533
534 /* This function initializes the trees at startup time. */
535 int bdb_build_tree(
536         Backend *be
537 )
538 {
539         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
540         int i, rc;
541         DBC *cursor;
542         DBT key, data;
543         ID id;
544         idNode *node;
545         char **rdns;
546
547         bdb->bi_tree = NULL;
548
549         rc = bdb->bi_id2parent->bdi_db->cursor(
550                 bdb->bi_id2parent->bdi_db, NULL, &cursor,
551                 bdb->bi_db_opflags );
552         if( rc != 0 ) {
553                 return NOID;
554         }
555
556         /* When be_suffix is turned into struct berval or LDAPDN
557          * life will get a lot easier... Since no DNs live on disk, we
558          * need to operate on the be_suffix to fully qualify our DNs.
559          * We need to know how many components are in the suffix DN,
560          * so we can tell where the suffix ends and our nodes begin.
561          *
562          * Note that this code always uses be_suffix[0], so defining
563          * multiple suffixes for a single backend won't work!
564          */
565         bdb->bi_sufflen = be->be_suffix[0]->bv_len;
566
567         rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
568         for (i=0; rdns[i]; i++);
569         bdb->bi_nrdns = i;
570         charray_free(rdns);
571
572         DBTzero( &key );
573         DBTzero( &data );
574         key.data = (char *)&id;
575         key.ulen = sizeof( id );
576         key.flags = DB_DBT_USERMEM;
577         data.flags = DB_DBT_MALLOC;
578
579         while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
580                 bdb_add_node( id, data.data, bdb );
581         }
582         cursor->c_close( cursor );
583
584         rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
585                 -1, AVL_INORDER );
586
587         return rc;
588 }
589
590 /* This function constructs a full DN for a given id. We really should
591  * be passing idNodes directly, to save some effort...
592  */
593 int bdb_fix_dn(
594         BackendDB *be,
595         ID id,
596         Entry *e
597 )
598 {
599         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
600         idNode *n, *o;
601         int rlen, nrlen;
602         char *ptr, *nptr;
603         
604         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
605         o = bdb_find_id_node(id, bdb->bi_tree);
606         rlen = bdb->bi_sufflen + 1;
607         nrlen = be->be_nsuffix[0]->bv_len + 1;
608         for (n = o; n; n=n->i_parent) {
609                 rlen += n->i_rdn->rdn.bv_len + 1;
610                 nrlen += n->i_rdn->nrdn.bv_len + 1;
611         }
612         e->e_dn = ch_malloc(rlen + nrlen);
613         e->e_ndn = e->e_dn + rlen;
614         ptr = e->e_dn;
615         nptr = e->e_ndn;
616         for (n = o; n; n=n->i_parent) {
617                 ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
618                 *ptr++ = ',';
619                 nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
620                 *nptr++ = ',';
621         }
622         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
623
624         ptr--;
625         nptr--;
626         strcpy(ptr, be->be_suffix[0]->bv_val);
627         strcpy(nptr, be->be_nsuffix[0]->bv_val);
628
629         return 0;
630 }
631
632 int
633 bdb_dn2id_add(
634         BackendDB       *be,
635         DB_TXN *txn,
636         char    *pdn,
637         Entry           *e )
638 {
639         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
640         int             rc, rlen, nrlen;
641         DBT             key, data;
642         DB *db = bdb->bi_id2parent->bdi_db;
643         diskNode *d;
644         idNode *n;
645
646         nrlen = dn_rdnlen( be, e->e_ndn );
647         if (nrlen) {
648                 rlen = dn_rdnlen( be, e->e_dn );
649         } else {
650                 rlen = 0;
651         }
652
653         d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
654         d->rdn.bv_len = rlen;
655         d->nrdn.bv_len = nrlen;
656         d->rdn.bv_val = (char *)(d+1);
657         d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
658         strncpy(d->rdn.bv_val, e->e_dn, rlen);
659         d->rdn.bv_val[rlen] = '\0';
660         strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
661         d->nrdn.bv_val[nrlen] = '\0';
662         d->rdn.bv_val -= (long)d;
663         d->nrdn.bv_val -= (long)d;
664
665         if (pdn) {
666                 bdb_dn2id(be, txn, pdn, &d->parent);
667         } else {
668                 d->parent = 0;
669         }
670
671         DBTzero(&key);
672         DBTzero(&data);
673         key.data = &e->e_id;
674         key.size = sizeof(ID);
675         key.flags = DB_DBT_USERMEM;
676
677         data.data = d;
678         data.size = sizeof(diskNode) + rlen + nrlen + 2;
679         data.flags = DB_DBT_USERMEM;
680
681         rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
682
683         if (rc == 0) {
684                 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
685                 n = bdb_add_node( e->e_id, data.data, bdb);
686                 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
687
688                 if (d->parent) {
689                         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
690                         bdb_insert_kid(n, bdb->bi_tree);
691                         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
692                 }
693         } else {
694                 free(d);
695         }
696         return rc;
697 }
698
699 int
700 bdb_dn2id_delete(
701         BackendDB       *be,
702         DB_TXN *txn,
703         char    *pdn,
704         Entry   *e )
705 {
706         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
707         int rc;
708         DBT             key;
709         DB *db = bdb->bi_id2parent->bdi_db;
710         idNode *n;
711
712         DBTzero(&key);
713         key.size = sizeof(e->e_id);
714         key.data = &e->e_id;
715
716         rc = db->del( db, txn, &key, 0);
717
718         ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
719         n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
720         if (n) {
721                 if (n->i_parent) {
722                         ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
723                         avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
724                                 (AVL_CMP)node_frdn_cmp);
725                         ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
726                 }
727                 free(n->i_rdn);
728                 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
729                 free(n);
730         }
731         if (e->e_id == 1)
732                 bdb->bi_troot = NULL;
733         ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
734
735         return rc;
736 }
737
738 int
739 bdb_dn2id_matched(
740         BackendDB       *be,
741         DB_TXN *txn,
742         const char      *in,
743         ID *id,
744         char **matchedDN )
745 {
746         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
747         int             i;
748         char            **rdns;
749         idNode *n, *p;
750
751         if (!bdb->bi_troot)
752                 return DB_NOTFOUND;
753
754         p = bdb->bi_troot;
755         if (be_issuffix(be, in)) {
756                 *id = p->i_id;
757                 return 0;
758         }
759
760         rdns = ldap_explode_dn(in, 0);
761         for (i=0; rdns[i]; i++);
762         i -= bdb->bi_nrdns;
763         if (i < 0)
764                 return -1;
765         n = p;
766         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
767         for (--i; i>=0; i--) {
768                 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
769                 n = bdb_find_rdn_node(rdns[i], p->i_kids);
770                 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
771                 if (!n) break;
772                 p = n;
773         }
774         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
775
776         if (n) {
777                 *id = n->i_id;
778         } else if (matchedDN) {
779                 int len = 0, j;
780                 char *ptr;
781                 ++i;
782                 for (j=i; rdns[j]; j++)
783                         len += strlen(rdns[j]) + 1;
784                 ptr = ch_malloc(len);
785                 *matchedDN = ptr;
786                 for (;rdns[i]; i++) {
787                         ptr = slap_strcopy(ptr, rdns[i]);
788                         *ptr++ = ',';
789                 }
790                 ptr[-1] = '\0';
791         }
792         return n ? 0 : DB_NOTFOUND;
793 }
794
795 int
796 bdb_dn2id(
797         BackendDB       *be,
798         DB_TXN *txn,
799         const char      *dn,
800         ID *id )
801 {
802         return bdb_dn2id_matched(be, txn, dn, id, NULL);
803 }
804
805 int
806 bdb_dn2id_children(
807         BackendDB       *be,
808         DB_TXN *txn,
809         const char *dn )
810 {
811         int             rc;
812         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
813         ID              id;
814         idNode *n;
815
816         rc = bdb_dn2id(be, txn, dn, &id);
817         if (rc != 0)
818                 return rc;
819
820         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
821         n = bdb_find_id_node(id, bdb->bi_tree);
822         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
823
824         if (!n->i_kids)
825                 return DB_NOTFOUND;
826         else
827                 return 0;
828 }
829
830 /* Since we don't store IDLs for onelevel or subtree, we have to construct
831  * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
832  */
833 static int
834 insert_one(
835         idNode *n,
836         ID *ids
837 )
838 {
839         return bdb_idl_insert(ids, n->i_id);
840 }
841
842 static int
843 insert_sub(
844         idNode *n,
845         ID *ids
846 )
847 {
848         int rc;
849
850         rc = bdb_idl_insert(ids, n->i_id);
851         if (rc == 0) {
852                 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
853                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
854                         AVL_INORDER);
855                 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
856         }
857         return rc;
858 }
859
860 int
861 bdb_dn2idl(
862         BackendDB       *be,
863         const char      *dn,
864         int prefix,
865         ID *ids )
866 {
867         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
868         int             rc;
869         ID              id;
870         idNode          *n;
871
872         if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn)) {
873                 BDB_IDL_ALL(bdb, ids);
874                 return 0;
875         }
876
877         rc = bdb_dn2id(be, NULL, dn, &id);
878         if (rc) return rc;
879
880         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
881         n = bdb_find_id_node(id, bdb->bi_tree);
882         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
883
884         ids[0] = 0;
885         ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
886         if (prefix == DN_ONE_PREFIX) {
887                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
888                         AVL_INORDER);
889         } else {
890                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
891                         AVL_INORDER);
892         }
893         ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
894         return rc;
895 }
896 #endif  /* BDB_HIER */