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