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