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