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