]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
Fix some debug statements
[openldap] / servers / slapd / back-bdb / dn2id.c
1 /* dn2id.c - routines to deal with the dn2id index */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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         ID *id2 )
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         while(1) {
264                 dn[-1] = DN_BASE_PREFIX;
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                         key.size -= pdn - dn;
282                         dn = pdn;
283                         key.data = pdn - 1;
284
285                 } else if ( rc == 0 ) {
286                         if( data.size != sizeof( ID ) ) {
287                                 Debug( LDAP_DEBUG_ANY,
288                                         "<= bdb_dn2id_matched: get size mismatch: "
289                                         "expected %ld, got %ld\n",
290                                         (long) sizeof(ID), (long) data.size, 0 );
291                         }
292
293                         if( dn != buf+1 ) {
294                                 *id2 = *id;
295                         }
296
297                         Debug( LDAP_DEBUG_TRACE,
298                                 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
299                                 (long) *id, *id2 == 0 ? "entry" : "matched", dn );
300                         break;
301
302                 } else {
303                         Debug( LDAP_DEBUG_ANY,
304                                 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
305                                 db_strerror(rc), rc, 0 );
306                         break;
307                 }
308         }
309
310         ch_free( buf );
311         return rc;
312 }
313
314 int
315 bdb_dn2id_children(
316         BackendDB       *be,
317         DB_TXN *txn,
318         struct berval *dn )
319 {
320         int             rc;
321         DBT             key, data;
322         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
323         DB *db = bdb->bi_dn2id->bdi_db;
324         ID              id;
325
326         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
327                 dn->bv_val, 0, 0 );
328
329         DBTzero( &key );
330         key.size = dn->bv_len + 2;
331         key.data = ch_malloc( key.size );
332         ((char *)key.data)[0] = DN_ONE_PREFIX;
333         AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
334
335         /* we actually could do a empty get... */
336         DBTzero( &data );
337         data.data = &id;
338         data.ulen = sizeof(id);
339         data.flags = DB_DBT_USERMEM;
340         data.doff = 0;
341         data.dlen = sizeof(id);
342
343         rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
344         free( key.data );
345
346         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
347                 dn->bv_val,
348                 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
349                         db_strerror(rc) ), rc );
350
351         return rc;
352 }
353
354 int
355 bdb_dn2idl(
356         BackendDB       *be,
357         struct berval   *dn,
358         int prefix,
359         ID *ids )
360 {
361         int             rc;
362         DBT             key;
363         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
364         DB *db = bdb->bi_dn2id->bdi_db;
365
366         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn->bv_val, 0, 0 );
367
368         if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val))
369         {
370                 BDB_IDL_ALL(bdb, ids);
371                 return 0;
372         }
373
374         DBTzero( &key );
375         key.size = dn->bv_len + 2;
376         key.data = ch_malloc( key.size );
377         ((char *)key.data)[0] = prefix;
378         AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
379
380         rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
381
382         if( rc != 0 ) {
383                 Debug( LDAP_DEBUG_TRACE,
384                         "<= bdb_dn2idl: get failed: %s (%d)\n",
385                         db_strerror( rc ), rc, 0 );
386
387         } else {
388                 Debug( LDAP_DEBUG_TRACE,
389                         "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
390                         (long) ids[0],
391                         (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
392         }
393
394         ch_free( key.data );
395         return rc;
396 }
397 #else   /* BDB_HIER */
398
399 /* Experimental management routines for a hierarchically structured backend.
400  *
401  * Unsupported! Use at your own risk!
402  *
403  * Instead of a dn2id database, we use an id2parent database. Each entry in
404  * this database is a struct diskNode, containing the ID of the node's parent
405  * and the RDN of the node.
406  */
407 typedef struct diskNode {
408         ID parent;
409         struct berval rdn;
410         struct berval nrdn;
411 } diskNode;
412
413 /* In bdb_db_open() we call bdb_build_tree() which reads the entire id2parent
414  * database into memory (into an AVL tree). Next we iterate through each node
415  * of this tree, connecting each child to its parent. The nodes in this AVL
416  * tree are a struct idNode. The immediate (Onelevel) children of a node are
417  * referenced in the i_kids AVL tree. With this arrangement, there is no need
418  * to maintain the DN_ONE_PREFIX or DN_SUBTREE_PREFIX database keys. Note that
419  * the DN of an entry is constructed by walking up the list of i_parent
420  * pointers, so no full DN is stored on disk anywhere. This makes modrdn
421  * extremely efficient, even when operating on a populated subtree.
422  *
423  * The idNode tree is searched directly from the root when performing id to
424  * entry lookups. The tree is traversed using the i_kids subtrees when
425  * performing dn to id lookups.
426  */
427 typedef struct idNode {
428         ID i_id;
429         struct idNode *i_parent;
430         diskNode *i_rdn;
431         Avlnode *i_kids;
432         ldap_pvt_thread_rdwr_t i_kids_rdwr;
433 } idNode;
434
435
436 /* The main AVL tree is sorted in ID order. The i_kids AVL trees are
437  * sorted in lexical order. These are the various helper routines used
438  * for the searches and sorts.
439  */
440 static int
441 node_find_cmp(
442         ID id,
443         idNode *n
444 )
445 {
446         return id - n->i_id;
447 }
448
449 static int
450 node_frdn_cmp(
451         char *nrdn,
452         idNode *n
453 )
454 {
455         return strcmp(nrdn, n->i_rdn->nrdn.bv_val);
456 }
457
458 static int
459 node_add_cmp(
460         idNode *a,
461         idNode *b
462 )
463 {
464         return a->i_id - b->i_id;
465 }
466
467 static int
468 node_rdn_cmp(
469         idNode *a,
470         idNode *b
471 )
472 {
473         return strcmp(a->i_rdn->nrdn.bv_val, b->i_rdn->nrdn.bv_val);
474 }
475
476 idNode * bdb_find_id_node(
477         ID id,
478         Avlnode *tree
479 )
480 {
481         return avl_find(tree, (const void *)id, (AVL_CMP)node_find_cmp);
482 }
483
484 idNode * bdb_find_rdn_node(
485         char *nrdn,
486         Avlnode *tree
487 )
488 {
489         return avl_find(tree, (const void *)nrdn, (AVL_CMP)node_frdn_cmp);
490 }
491
492 /* This function links a node into its parent's i_kids tree. */
493 int bdb_insert_kid(
494         idNode *a,
495         Avlnode *tree
496 )
497 {
498         int rc;
499
500         if (a->i_rdn->parent == 0)
501                 return 0;
502         a->i_parent = bdb_find_id_node(a->i_rdn->parent, tree);
503         if (!a->i_parent)
504                 return -1;
505         ldap_pvt_thread_rdwr_wlock(&a->i_parent->i_kids_rdwr);
506         rc = avl_insert( &a->i_parent->i_kids, (caddr_t) a,
507                 (AVL_CMP)node_rdn_cmp, (AVL_DUP) avl_dup_error );
508         ldap_pvt_thread_rdwr_wunlock(&a->i_parent->i_kids_rdwr);
509         return rc;
510 }
511
512 /* This function adds a node into the main AVL tree */
513 idNode *bdb_add_node(
514         ID id,
515         char *d,
516         struct bdb_info *bdb
517 )
518 {
519         idNode *node;
520
521         node = (idNode *)ch_malloc(sizeof(idNode));
522         node->i_id = id;
523         node->i_parent = NULL;
524         node->i_kids = NULL;
525         node->i_rdn = (diskNode *)d;
526         node->i_rdn->rdn.bv_val += (long)d;
527         node->i_rdn->nrdn.bv_val += (long)d;
528         ldap_pvt_thread_rdwr_init(&node->i_kids_rdwr);
529         avl_insert( &bdb->bi_tree, (caddr_t) node,
530                         (AVL_CMP)node_add_cmp, (AVL_DUP) avl_dup_error );
531         if (id == 1)
532                 bdb->bi_troot = node;
533         return node;
534 }
535
536 /* This function initializes the trees at startup time. */
537 int bdb_build_tree(
538         Backend *be
539 )
540 {
541         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
542         int i, rc;
543         DBC *cursor;
544         DBT key, data;
545         ID id;
546         idNode *node;
547         char **rdns;
548
549         bdb->bi_tree = NULL;
550
551         rc = bdb->bi_id2parent->bdi_db->cursor(
552                 bdb->bi_id2parent->bdi_db, NULL, &cursor,
553                 bdb->bi_db_opflags );
554         if( rc != 0 ) {
555                 return NOID;
556         }
557
558         /* When be_suffix is turned into struct berval or LDAPDN
559          * life will get a lot easier... Since no DNs live on disk, we
560          * need to operate on the be_suffix to fully qualify our DNs.
561          * We need to know how many components are in the suffix DN,
562          * so we can tell where the suffix ends and our nodes begin.
563          *
564          * Note that this code always uses be_suffix[0], so defining
565          * multiple suffixes for a single backend won't work!
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 = be->be_suffix[0]->bv_len + 1;
607         nrlen = be->be_nsuffix[0]->bv_len + 1;
608         for (n = o; n && n->i_parent; 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_name.bv_len = rlen - 1;
613         e->e_nname.bv_len = nrlen - 1;
614         e->e_name.bv_val = ch_malloc(rlen + nrlen);
615         e->e_nname.bv_val = e->e_name.bv_val + rlen;
616         ptr = e->e_name.bv_val;
617         nptr = e->e_nname.bv_val;
618         for (n = o; n && n->i_parent; n=n->i_parent) {
619                 ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
620                 *ptr++ = ',';
621                 nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
622                 *nptr++ = ',';
623         }
624         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
625
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         struct berval   *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_nname );
647         if (nrlen) {
648                 rlen = dn_rdnlen( be, &e->e_name );
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->bv_len) {
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         struct berval   *in,
743         ID *id,
744         ID *id2 )
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->bv_val)) {
756                 *id = p->i_id;
757                 return 0;
758         }
759
760         rdns = ldap_explode_dn(in->bv_val, 0);
761         for (i=0; rdns[i]; i++);
762         i -= bdb->bi_nrdns;
763         if (i < 0) {
764                 charray_free(rdns);
765                 return -1;
766         }
767         n = p;
768         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
769         for (--i; i>=0; i--) {
770                 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
771                 n = bdb_find_rdn_node(rdns[i], p->i_kids);
772                 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
773                 if (!n) break;
774                 p = n;
775         }
776         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
777         charray_free(rdns);
778
779         if (n) {
780                 *id = n->i_id;
781         } else if (id2) {
782                 *id2 = p->i_id;
783         }
784         return n ? 0 : DB_NOTFOUND;
785 }
786
787 int
788 bdb_dn2id(
789         BackendDB       *be,
790         DB_TXN *txn,
791         struct berval   *dn,
792         ID *id )
793 {
794         return bdb_dn2id_matched(be, txn, dn, id, NULL);
795 }
796
797 int
798 bdb_dn2id_children(
799         BackendDB       *be,
800         DB_TXN *txn,
801         struct berval   *dn )
802 {
803         int             rc;
804         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
805         ID              id;
806         idNode *n;
807
808         rc = bdb_dn2id(be, txn, dn, &id);
809         if (rc != 0)
810                 return rc;
811
812         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
813         n = bdb_find_id_node(id, bdb->bi_tree);
814         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
815
816         if (!n->i_kids)
817                 return DB_NOTFOUND;
818         else
819                 return 0;
820 }
821
822 /* Since we don't store IDLs for onelevel or subtree, we have to construct
823  * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
824  */
825 static int
826 insert_one(
827         idNode *n,
828         ID *ids
829 )
830 {
831         return bdb_idl_insert(ids, n->i_id);
832 }
833
834 static int
835 insert_sub(
836         idNode *n,
837         ID *ids
838 )
839 {
840         int rc;
841
842         rc = bdb_idl_insert(ids, n->i_id);
843         if (rc == 0) {
844                 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
845                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
846                         AVL_INORDER);
847                 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
848         }
849         return rc;
850 }
851
852 int
853 bdb_dn2idl(
854         BackendDB       *be,
855         struct berval   *dn,
856         int prefix,
857         ID *ids )
858 {
859         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
860         int             rc;
861         ID              id;
862         idNode          *n;
863
864         if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val)) {
865                 BDB_IDL_ALL(bdb, ids);
866                 return 0;
867         }
868
869         rc = bdb_dn2id(be, NULL, dn, &id);
870         if (rc) return rc;
871
872         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
873         n = bdb_find_id_node(id, bdb->bi_tree);
874         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
875
876         ids[0] = 0;
877         ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
878         if (prefix == DN_ONE_PREFIX) {
879                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
880                         AVL_INORDER);
881         } else {
882                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
883                         AVL_INORDER);
884         }
885         ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
886         return rc;
887 }
888 #endif  /* BDB_HIER */