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