]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
Switched to dn_parent1, cleaned up 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-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_parent1( 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_parent1( 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_parent1( 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_parent1( 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_parent1( 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_parent1( 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_parent1( 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         const 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         char            *nrdn = dn_rdn( be, e->e_ndn );
682         char            *rdn;
683         diskNode *d;
684         idNode *n;
685
686         if (nrdn == NULL) {
687                 nrdn = "";
688                 rdn = "";
689         } else {
690                 rdn = dn_rdn( be, e->e_dn );
691         }
692
693         nrlen = strlen(nrdn);
694         rlen = strlen(rdn);
695         d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
696         d->rdn.bv_len = rlen;
697         d->nrdn.bv_len = nrlen;
698         d->rdn.bv_val = (char *)(d+1);
699         d->nrdn.bv_val = bdb_strcopy(d->rdn.bv_val, rdn) + 1;
700         strcpy(d->nrdn.bv_val, nrdn);
701         d->rdn.bv_val -= (long)d;
702         d->nrdn.bv_val -= (long)d;
703
704         if (nrdn[0]) free(nrdn);
705         if (rdn[0]) free(rdn);
706
707         if (pdn) {
708                 bdb_dn2id(be, txn, pdn, &d->parent);
709         } else {
710                 d->parent = 0;
711         }
712
713         DBTzero(&key);
714         DBTzero(&data);
715         key.data = &e->e_id;
716         key.size = sizeof(ID);
717         key.flags = DB_DBT_USERMEM;
718
719         data.data = d;
720         data.size = sizeof(diskNode) + rlen + nrlen + 2;
721         data.flags = DB_DBT_USERMEM;
722
723         rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
724
725         if (rc == 0) {
726                 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
727                 n = bdb_add_node( e->e_id, data.data, bdb);
728                 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
729
730                 if (d->parent) {
731                         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
732                         bdb_insert_kid(n, bdb->bi_tree);
733                         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
734                 }
735         } else {
736                 free(d);
737         }
738         return rc;
739 }
740
741 int
742 bdb_dn2id_delete(
743         BackendDB       *be,
744         DB_TXN *txn,
745         char    *pdn,
746         Entry   *e )
747 {
748         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
749         int rc;
750         DBT             key;
751         DB *db = bdb->bi_id2parent->bdi_db;
752         idNode *n;
753
754         DBTzero(&key);
755         key.size = sizeof(e->e_id);
756         key.data = &e->e_id;
757
758         rc = db->del( db, txn, &key, 0);
759
760         ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
761         n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
762         if (n) {
763                 if (n->i_parent) {
764                         ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
765                         avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
766                                 (AVL_CMP)node_frdn_cmp);
767                         ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
768                 }
769                 free(n->i_rdn);
770                 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
771                 free(n);
772         }
773         if (e->e_id == 1)
774                 bdb->bi_troot = NULL;
775         ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
776
777         return rc;
778 }
779
780 int
781 bdb_dn2id_matched(
782         BackendDB       *be,
783         DB_TXN *txn,
784         const char      *in,
785         ID *id,
786         char **matchedDN )
787 {
788         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
789         int             i;
790         char            **rdns;
791         idNode *n, *p;
792
793         if (!bdb->bi_troot)
794                 return DB_NOTFOUND;
795
796         p = bdb->bi_troot;
797         if (be_issuffix(be, in)) {
798                 *id = p->i_id;
799                 return 0;
800         }
801
802         rdns = ldap_explode_dn(in, 0);
803         for (i=0; rdns[i]; i++);
804         i -= bdb->bi_nrdns;
805         if (i < 0)
806                 return -1;
807         n = p;
808         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
809         for (--i; i>=0; i--) {
810                 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
811                 n = bdb_find_rdn_node(rdns[i], p->i_kids);
812                 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
813                 if (!n) break;
814                 p = n;
815         }
816         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
817
818         if (n) {
819                 *id = n->i_id;
820         } else if (matchedDN) {
821                 int len = 0, j;
822                 char *ptr;
823                 ++i;
824                 for (j=i; rdns[j]; j++)
825                         len += strlen(rdns[j]) + 1;
826                 ptr = ch_malloc(len);
827                 *matchedDN = ptr;
828                 for (;rdns[i]; i++) {
829                         ptr = bdb_strcopy(ptr, rdns[i]);
830                         *ptr++ = ',';
831                 }
832                 ptr[-1] = '\0';
833         }
834         return n ? 0 : DB_NOTFOUND;
835 }
836
837 int
838 bdb_dn2id(
839         BackendDB       *be,
840         DB_TXN *txn,
841         const char      *dn,
842         ID *id )
843 {
844         return bdb_dn2id_matched(be, txn, dn, id, NULL);
845 }
846
847 int
848 bdb_dn2id_children(
849         BackendDB       *be,
850         DB_TXN *txn,
851         const char *dn )
852 {
853         int             rc;
854         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
855         ID              id;
856         idNode *n;
857
858         rc = bdb_dn2id(be, txn, dn, &id);
859         if (rc != 0)
860                 return rc;
861
862         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
863         n = bdb_find_id_node(id, bdb->bi_tree);
864         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
865
866         if (!n->i_kids)
867                 return DB_NOTFOUND;
868         else
869                 return 0;
870 }
871
872 /* Since we don't store IDLs for onelevel or subtree, we have to construct
873  * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
874  */
875 static int
876 insert_one(
877         idNode *n,
878         ID *ids
879 )
880 {
881         return bdb_idl_insert(ids, n->i_id);
882 }
883
884 static int
885 insert_sub(
886         idNode *n,
887         ID *ids
888 )
889 {
890         int rc;
891
892         rc = bdb_idl_insert(ids, n->i_id);
893         if (rc == 0) {
894                 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
895                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
896                         AVL_INORDER);
897                 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
898         }
899         return rc;
900 }
901
902 int
903 bdb_dn2idl(
904         BackendDB       *be,
905         const char      *dn,
906         int prefix,
907         ID *ids )
908 {
909         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
910         int             rc;
911         ID              id;
912         idNode          *n;
913
914         if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn)) {
915                 BDB_IDL_ALL(bdb, ids);
916                 return 0;
917         }
918
919         rc = bdb_dn2id(be, NULL, dn, &id);
920         if (rc) return rc;
921
922         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
923         n = bdb_find_id_node(id, bdb->bi_tree);
924         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
925
926         ids[0] = 0;
927         ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
928         if (prefix == DN_ONE_PREFIX) {
929                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
930                         AVL_INORDER);
931         } else {
932                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
933                         AVL_INORDER);
934         }
935         ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
936         return rc;
937 }
938 #endif  /* BDB_HIER */