]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
Misc cleanup
[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->i_parent; 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->i_parent; 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         strcpy(ptr, be->be_suffix[0]->bv_val);
628         strcpy(nptr, be->be_nsuffix[0]->bv_val);
629
630         return 0;
631 }
632
633 int
634 bdb_dn2id_add(
635         BackendDB       *be,
636         DB_TXN *txn,
637         struct berval   *pdn,
638         Entry           *e )
639 {
640         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
641         int             rc, rlen, nrlen;
642         DBT             key, data;
643         DB *db = bdb->bi_id2parent->bdi_db;
644         diskNode *d;
645         idNode *n;
646
647         nrlen = dn_rdnlen( be, &e->e_nname );
648         if (nrlen) {
649                 rlen = dn_rdnlen( be, &e->e_name );
650         } else {
651                 rlen = 0;
652         }
653
654         d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
655         d->rdn.bv_len = rlen;
656         d->nrdn.bv_len = nrlen;
657         d->rdn.bv_val = (char *)(d+1);
658         d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
659         strncpy(d->rdn.bv_val, e->e_dn, rlen);
660         d->rdn.bv_val[rlen] = '\0';
661         strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
662         d->nrdn.bv_val[nrlen] = '\0';
663         d->rdn.bv_val -= (long)d;
664         d->nrdn.bv_val -= (long)d;
665
666         if (pdn->bv_len) {
667                 bdb_dn2id(be, txn, pdn, &d->parent);
668         } else {
669                 d->parent = 0;
670         }
671
672         DBTzero(&key);
673         DBTzero(&data);
674         key.data = &e->e_id;
675         key.size = sizeof(ID);
676         key.flags = DB_DBT_USERMEM;
677
678         data.data = d;
679         data.size = sizeof(diskNode) + rlen + nrlen + 2;
680         data.flags = DB_DBT_USERMEM;
681
682         rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
683
684         if (rc == 0) {
685                 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
686                 n = bdb_add_node( e->e_id, data.data, bdb);
687                 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
688
689                 if (d->parent) {
690                         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
691                         bdb_insert_kid(n, bdb->bi_tree);
692                         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
693                 }
694         } else {
695                 free(d);
696         }
697         return rc;
698 }
699
700 int
701 bdb_dn2id_delete(
702         BackendDB       *be,
703         DB_TXN *txn,
704         char    *pdn,
705         Entry   *e )
706 {
707         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
708         int rc;
709         DBT             key;
710         DB *db = bdb->bi_id2parent->bdi_db;
711         idNode *n;
712
713         DBTzero(&key);
714         key.size = sizeof(e->e_id);
715         key.data = &e->e_id;
716
717         rc = db->del( db, txn, &key, 0);
718
719         ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
720         n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
721         if (n) {
722                 if (n->i_parent) {
723                         ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
724                         avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
725                                 (AVL_CMP)node_frdn_cmp);
726                         ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
727                 }
728                 free(n->i_rdn);
729                 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
730                 free(n);
731         }
732         if (e->e_id == 1)
733                 bdb->bi_troot = NULL;
734         ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
735
736         return rc;
737 }
738
739 int
740 bdb_dn2id_matched(
741         BackendDB       *be,
742         DB_TXN *txn,
743         struct berval   *in,
744         ID *id,
745         char **matchedDN )
746 {
747         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
748         int             i;
749         char            **rdns;
750         idNode *n, *p;
751
752         if (!bdb->bi_troot)
753                 return DB_NOTFOUND;
754
755         p = bdb->bi_troot;
756         if (be_issuffix(be, in->bv_val)) {
757                 *id = p->i_id;
758                 return 0;
759         }
760
761         rdns = ldap_explode_dn(in->bv_val, 0);
762         for (i=0; rdns[i]; i++);
763         i -= bdb->bi_nrdns;
764         if (i < 0)
765                 return -1;
766         n = p;
767         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
768         for (--i; i>=0; i--) {
769                 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
770                 n = bdb_find_rdn_node(rdns[i], p->i_kids);
771                 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
772                 if (!n) break;
773                 p = n;
774         }
775         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
776
777         if (n) {
778                 *id = n->i_id;
779         } else if (matchedDN) {
780                 int len = 0, j;
781                 char *ptr;
782                 ++i;
783                 for (j=i; rdns[j]; j++)
784                         len += strlen(rdns[j]) + 1;
785                 ptr = ch_malloc(len);
786                 *matchedDN = ptr;
787                 for (;rdns[i]; i++) {
788                         ptr = slap_strcopy(ptr, rdns[i]);
789                         *ptr++ = ',';
790                 }
791                 ptr[-1] = '\0';
792         }
793         return n ? 0 : DB_NOTFOUND;
794 }
795
796 int
797 bdb_dn2id(
798         BackendDB       *be,
799         DB_TXN *txn,
800         struct berval   *dn,
801         ID *id )
802 {
803         return bdb_dn2id_matched(be, txn, dn, id, NULL);
804 }
805
806 int
807 bdb_dn2id_children(
808         BackendDB       *be,
809         DB_TXN *txn,
810         struct berval   *dn )
811 {
812         int             rc;
813         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
814         ID              id;
815         idNode *n;
816
817         rc = bdb_dn2id(be, txn, dn, &id);
818         if (rc != 0)
819                 return rc;
820
821         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
822         n = bdb_find_id_node(id, bdb->bi_tree);
823         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
824
825         if (!n->i_kids)
826                 return DB_NOTFOUND;
827         else
828                 return 0;
829 }
830
831 /* Since we don't store IDLs for onelevel or subtree, we have to construct
832  * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
833  */
834 static int
835 insert_one(
836         idNode *n,
837         ID *ids
838 )
839 {
840         return bdb_idl_insert(ids, n->i_id);
841 }
842
843 static int
844 insert_sub(
845         idNode *n,
846         ID *ids
847 )
848 {
849         int rc;
850
851         rc = bdb_idl_insert(ids, n->i_id);
852         if (rc == 0) {
853                 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
854                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
855                         AVL_INORDER);
856                 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
857         }
858         return rc;
859 }
860
861 int
862 bdb_dn2idl(
863         BackendDB       *be,
864         struct berval   *dn,
865         int prefix,
866         ID *ids )
867 {
868         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
869         int             rc;
870         ID              id;
871         idNode          *n;
872
873         if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val)) {
874                 BDB_IDL_ALL(bdb, ids);
875                 return 0;
876         }
877
878         rc = bdb_dn2id(be, NULL, dn, &id);
879         if (rc) return rc;
880
881         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
882         n = bdb_find_id_node(id, bdb->bi_tree);
883         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
884
885         ids[0] = 0;
886         ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
887         if (prefix == DN_ONE_PREFIX) {
888                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
889                         AVL_INORDER);
890         } else {
891                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
892                         AVL_INORDER);
893         }
894         ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
895         return rc;
896 }
897 #endif  /* BDB_HIER */