]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
Minor cleanup
[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 = 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         const char      *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, 0, 0 );
206
207         DBTzero( &key );
208         key.size = strlen( dn ) + 2;
209         key.data = ch_malloc( key.size );
210         ((char *)key.data)[0] = DN_BASE_PREFIX;
211         AC_MEMCPY( &((char *)key.data)[1], dn, 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         const char      *in,
239         ID *id,
240         char **matchedDN )
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         const char *dn = in;
247
248         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
249
250         DBTzero( &key );
251         key.size = strlen( dn ) + 2;
252         key.data = ch_malloc( key.size );
253         ((char *)key.data)[0] = DN_BASE_PREFIX;
254
255         /* store the ID */
256         DBTzero( &data );
257         data.data = id;
258         data.ulen = sizeof(ID);
259         data.flags = DB_DBT_USERMEM;
260
261         *matchedDN = NULL;
262
263         while(1) {
264                 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
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                         dn = pdn;
282                         key.size = strlen( dn ) + 2;
283
284                 } else if ( rc == 0 ) {
285                         if( data.size != sizeof( ID ) ) {
286                                 Debug( LDAP_DEBUG_ANY,
287                                         "<= bdb_dn2id_matched: get size mismatch: "
288                                         "expected %ld, got %ld\n",
289                                         (long) sizeof(ID), (long) data.size, 0 );
290                         }
291
292                         if( in != dn ) {
293                                 *matchedDN = (char *) dn;
294                         }
295
296                         Debug( LDAP_DEBUG_TRACE,
297                                 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
298                                 (long) *id, *matchedDN == NULL ? "entry" : "matched", dn );
299                         break;
300
301                 } else {
302                         Debug( LDAP_DEBUG_ANY,
303                                 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
304                                 db_strerror(rc), rc, 0 );
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;
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 = be->be_suffix[0]->bv_len;
582
583         rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
584         for (i=0; rdns[i]; i++);
585         bdb->bi_nrdns = i;
586         charray_free(rdns);
587
588         DBTzero( &key );
589         DBTzero( &data );
590         key.data = (char *)&id;
591         key.ulen = sizeof( id );
592         key.flags = DB_DBT_USERMEM;
593         data.flags = DB_DBT_MALLOC;
594
595         while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
596                 bdb_add_node( id, data.data, bdb );
597         }
598         cursor->c_close( cursor );
599
600         rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
601                 -1, AVL_INORDER );
602
603         return rc;
604 }
605
606 /* This function constructs a full DN for a given id. We really should
607  * be passing idNodes directly, to save some effort...
608  */
609 int bdb_fix_dn(
610         BackendDB *be,
611         ID id,
612         Entry *e
613 )
614 {
615         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
616         idNode *n, *o;
617         int rlen, nrlen;
618         char *ptr, *nptr;
619         
620         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
621         o = bdb_find_id_node(id, bdb->bi_tree);
622         rlen = bdb->bi_sufflen + 1;
623         nrlen = be->be_nsuffix[0]->bv_len + 1;
624         for (n = o; n; n=n->i_parent) {
625                 rlen += n->i_rdn->rdn.bv_len + 1;
626                 nrlen += n->i_rdn->nrdn.bv_len + 1;
627         }
628         e->e_dn = ch_malloc(rlen + nrlen);
629         e->e_ndn = e->e_dn + rlen;
630         ptr = e->e_dn;
631         nptr = e->e_ndn;
632         for (n = o; n; n=n->i_parent) {
633                 ptr = bdb_strcopy(ptr, n->i_rdn->rdn.bv_val);
634                 *ptr++ = ',';
635                 nptr = bdb_strcopy(nptr, n->i_rdn->nrdn.bv_val);
636                 *nptr++ = ',';
637         }
638         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
639
640         ptr--;
641         nptr--;
642         strcpy(ptr, be->be_suffix[0]->bv_val);
643         strcpy(nptr, be->be_nsuffix[0]->bv_val);
644
645         return 0;
646 }
647
648 int
649 bdb_dn2id_add(
650         BackendDB       *be,
651         DB_TXN *txn,
652         char    *pdn,
653         Entry           *e )
654 {
655         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
656         int             rc, rlen, nrlen;
657         DBT             key, data;
658         DB *db = bdb->bi_id2parent->bdi_db;
659         diskNode *d;
660         idNode *n;
661
662         nrlen = dn_rdnlen( be, e->e_ndn );
663         if (nrlen) {
664                 rlen = dn_rdnlen( be, e->e_dn );
665         } else {
666                 rlen = 0;
667         }
668
669         d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
670         d->rdn.bv_len = rlen;
671         d->nrdn.bv_len = nrlen;
672         d->rdn.bv_val = (char *)(d+1);
673         d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
674         strncpy(d->rdn.bv_val, e->e_dn, rlen);
675         d->rdn.bv_val[rlen] = '\0';
676         strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
677         d->nrdn.bv_val[nrlen] = '\0';
678         d->rdn.bv_val -= (long)d;
679         d->nrdn.bv_val -= (long)d;
680
681         if (pdn) {
682                 bdb_dn2id(be, txn, pdn, &d->parent);
683         } else {
684                 d->parent = 0;
685         }
686
687         DBTzero(&key);
688         DBTzero(&data);
689         key.data = &e->e_id;
690         key.size = sizeof(ID);
691         key.flags = DB_DBT_USERMEM;
692
693         data.data = d;
694         data.size = sizeof(diskNode) + rlen + nrlen + 2;
695         data.flags = DB_DBT_USERMEM;
696
697         rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
698
699         if (rc == 0) {
700                 ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
701                 n = bdb_add_node( e->e_id, data.data, bdb);
702                 ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
703
704                 if (d->parent) {
705                         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
706                         bdb_insert_kid(n, bdb->bi_tree);
707                         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
708                 }
709         } else {
710                 free(d);
711         }
712         return rc;
713 }
714
715 int
716 bdb_dn2id_delete(
717         BackendDB       *be,
718         DB_TXN *txn,
719         char    *pdn,
720         Entry   *e )
721 {
722         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
723         int rc;
724         DBT             key;
725         DB *db = bdb->bi_id2parent->bdi_db;
726         idNode *n;
727
728         DBTzero(&key);
729         key.size = sizeof(e->e_id);
730         key.data = &e->e_id;
731
732         rc = db->del( db, txn, &key, 0);
733
734         ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
735         n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
736         if (n) {
737                 if (n->i_parent) {
738                         ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
739                         avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
740                                 (AVL_CMP)node_frdn_cmp);
741                         ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
742                 }
743                 free(n->i_rdn);
744                 ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
745                 free(n);
746         }
747         if (e->e_id == 1)
748                 bdb->bi_troot = NULL;
749         ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
750
751         return rc;
752 }
753
754 int
755 bdb_dn2id_matched(
756         BackendDB       *be,
757         DB_TXN *txn,
758         const char      *in,
759         ID *id,
760         char **matchedDN )
761 {
762         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
763         int             i;
764         char            **rdns;
765         idNode *n, *p;
766
767         if (!bdb->bi_troot)
768                 return DB_NOTFOUND;
769
770         p = bdb->bi_troot;
771         if (be_issuffix(be, in)) {
772                 *id = p->i_id;
773                 return 0;
774         }
775
776         rdns = ldap_explode_dn(in, 0);
777         for (i=0; rdns[i]; i++);
778         i -= bdb->bi_nrdns;
779         if (i < 0)
780                 return -1;
781         n = p;
782         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
783         for (--i; i>=0; i--) {
784                 ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
785                 n = bdb_find_rdn_node(rdns[i], p->i_kids);
786                 ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
787                 if (!n) break;
788                 p = n;
789         }
790         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
791
792         if (n) {
793                 *id = n->i_id;
794         } else if (matchedDN) {
795                 int len = 0, j;
796                 char *ptr;
797                 ++i;
798                 for (j=i; rdns[j]; j++)
799                         len += strlen(rdns[j]) + 1;
800                 ptr = ch_malloc(len);
801                 *matchedDN = ptr;
802                 for (;rdns[i]; i++) {
803                         ptr = bdb_strcopy(ptr, rdns[i]);
804                         *ptr++ = ',';
805                 }
806                 ptr[-1] = '\0';
807         }
808         return n ? 0 : DB_NOTFOUND;
809 }
810
811 int
812 bdb_dn2id(
813         BackendDB       *be,
814         DB_TXN *txn,
815         const char      *dn,
816         ID *id )
817 {
818         return bdb_dn2id_matched(be, txn, dn, id, NULL);
819 }
820
821 int
822 bdb_dn2id_children(
823         BackendDB       *be,
824         DB_TXN *txn,
825         const char *dn )
826 {
827         int             rc;
828         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
829         ID              id;
830         idNode *n;
831
832         rc = bdb_dn2id(be, txn, dn, &id);
833         if (rc != 0)
834                 return rc;
835
836         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
837         n = bdb_find_id_node(id, bdb->bi_tree);
838         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
839
840         if (!n->i_kids)
841                 return DB_NOTFOUND;
842         else
843                 return 0;
844 }
845
846 /* Since we don't store IDLs for onelevel or subtree, we have to construct
847  * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
848  */
849 static int
850 insert_one(
851         idNode *n,
852         ID *ids
853 )
854 {
855         return bdb_idl_insert(ids, n->i_id);
856 }
857
858 static int
859 insert_sub(
860         idNode *n,
861         ID *ids
862 )
863 {
864         int rc;
865
866         rc = bdb_idl_insert(ids, n->i_id);
867         if (rc == 0) {
868                 ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
869                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
870                         AVL_INORDER);
871                 ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
872         }
873         return rc;
874 }
875
876 int
877 bdb_dn2idl(
878         BackendDB       *be,
879         const char      *dn,
880         int prefix,
881         ID *ids )
882 {
883         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
884         int             rc;
885         ID              id;
886         idNode          *n;
887
888         if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn)) {
889                 BDB_IDL_ALL(bdb, ids);
890                 return 0;
891         }
892
893         rc = bdb_dn2id(be, NULL, dn, &id);
894         if (rc) return rc;
895
896         ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
897         n = bdb_find_id_node(id, bdb->bi_tree);
898         ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
899
900         ids[0] = 0;
901         ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
902         if (prefix == DN_ONE_PREFIX) {
903                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
904                         AVL_INORDER);
905         } else {
906                 rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
907                         AVL_INORDER);
908         }
909         ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
910         return rc;
911 }
912 #endif  /* BDB_HIER */