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