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