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