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