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