]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Don't bother setting up threads if there are no indexed attrs (unlikely...)
[openldap] / servers / slapd / back-bdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2006 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/errno.h>
22
23 #define AVL_INTERNAL
24 #include "back-bdb.h"
25 #include "idl.h"
26
27 static DBC *cursor = NULL;
28 static DBT key, data;
29 static EntryHeader eh;
30 static int eoff;
31
32 typedef struct dn_id {
33         ID id;
34         struct berval dn;
35 } dn_id;
36
37 #define HOLE_SIZE       4096
38 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
39 static unsigned nhmax = HOLE_SIZE;
40 static unsigned nholes;
41
42 static int index_nattrs;
43
44 #ifdef BDB_TOOL_IDL_CACHING
45 #define bdb_tool_idl_cmp                BDB_SYMBOL(tool_idl_cmp)
46 #define bdb_tool_idl_flush_one          BDB_SYMBOL(tool_idl_flush_one)
47 #define bdb_tool_idl_flush              BDB_SYMBOL(tool_idl_flush)
48
49 static int bdb_tool_idl_flush( BackendDB *be );
50
51 #define IDBLOCK 1024
52
53 typedef struct bdb_tool_idl_cache_entry {
54         struct bdb_tool_idl_cache_entry *next;
55         ID ids[IDBLOCK];
56 } bdb_tool_idl_cache_entry;
57  
58 typedef struct bdb_tool_idl_cache {
59         struct berval kstr;
60         bdb_tool_idl_cache_entry *head, *tail;
61         ID first, last;
62         int count;
63 } bdb_tool_idl_cache;
64
65 static bdb_tool_idl_cache_entry *bdb_tool_idl_free_list;
66 #endif  /* BDB_TOOL_IDL_CACHING */
67
68 static ID bdb_tool_ix_id;
69 static Operation *bdb_tool_ix_op;
70 static int *bdb_tool_index_threads, bdb_tool_index_tcount;
71 static void *bdb_tool_index_rec;
72 static struct bdb_info *bdb_tool_info;
73 static ldap_pvt_thread_mutex_t bdb_tool_index_mutex;
74 static ldap_pvt_thread_cond_t bdb_tool_index_cond_main;
75 static ldap_pvt_thread_cond_t bdb_tool_index_cond_work;
76
77 static void * bdb_tool_index_task( void *ctx, void *ptr );
78
79 int bdb_tool_entry_open(
80         BackendDB *be, int mode )
81 {
82         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
83
84         /* initialize key and data thangs */
85         DBTzero( &key );
86         DBTzero( &data );
87         key.flags = DB_DBT_REALLOC;
88         data.flags = DB_DBT_USERMEM;
89
90         if (cursor == NULL) {
91                 int rc = bdb->bi_id2entry->bdi_db->cursor(
92                         bdb->bi_id2entry->bdi_db, NULL, &cursor,
93                         bdb->bi_db_opflags );
94                 if( rc != 0 ) {
95                         return -1;
96                 }
97         }
98
99         /* Set up for threaded slapindex */
100         if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK
101                 && bdb->bi_nattrs ) {
102                 if ( !bdb_tool_info ) {
103                         int i;
104                         ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
105                         ldap_pvt_thread_cond_init( &bdb_tool_index_cond_main );
106                         ldap_pvt_thread_cond_init( &bdb_tool_index_cond_work );
107                         bdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
108                         bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
109                         bdb_tool_index_tcount = slap_tool_thread_max - 1;
110                         for (i=1; i<slap_tool_thread_max; i++) {
111                                 int *ptr = ch_malloc( sizeof( int ));
112                                 *ptr = i;
113                                 ldap_pvt_thread_pool_submit( &connection_pool,
114                                         bdb_tool_index_task, ptr );
115                         }
116                 }
117                 bdb_tool_info = bdb;
118         }
119
120         return 0;
121 }
122
123 int bdb_tool_entry_close(
124         BackendDB *be )
125 {
126         if ( bdb_tool_info ) {
127                 slapd_shutdown = 1;
128                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
129                 bdb_tool_index_tcount = slap_tool_thread_max - 1;
130                 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
131                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
132         }
133
134         if( key.data ) {
135                 ch_free( key.data );
136                 key.data = NULL;
137         }
138         if( eh.bv.bv_val ) {
139                 ch_free( eh.bv.bv_val );
140                 eh.bv.bv_val = NULL;
141         }
142
143         if( cursor ) {
144                 cursor->c_close( cursor );
145                 cursor = NULL;
146         }
147
148 #ifdef BDB_TOOL_IDL_CACHING
149         bdb_tool_idl_flush( be );
150 #endif
151
152         if( nholes ) {
153                 unsigned i;
154                 fprintf( stderr, "Error, entries missing!\n");
155                 for (i=0; i<nholes; i++) {
156                         fprintf(stderr, "  entry %ld: %s\n",
157                                 holes[i].id, holes[i].dn.bv_val);
158                 }
159                 return -1;
160         }
161                         
162         return 0;
163 }
164
165 ID bdb_tool_entry_next(
166         BackendDB *be )
167 {
168         int rc;
169         ID id;
170         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
171         char buf[16], *dptr;
172
173         assert( be != NULL );
174         assert( slapMode & SLAP_TOOL_MODE );
175         assert( bdb != NULL );
176         
177         /* Get the header */
178         data.ulen = data.dlen = sizeof( buf );
179         data.data = buf;
180         data.flags |= DB_DBT_PARTIAL;
181         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
182
183         if( rc ) {
184                 /* If we're doing linear indexing and there are more attrs to
185                  * index, and we're at the end of the database, start over.
186                  */
187                 if ( index_nattrs && rc == DB_NOTFOUND ) {
188                         /* optional - do a checkpoint here? */
189                         bdb_attr_info_free( bdb->bi_attrs[0] );
190                         bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
191                         index_nattrs--;
192                         rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
193                         if ( rc ) {
194                                 return NOID;
195                         }
196                 } else {
197                         return NOID;
198                 }
199         }
200
201         dptr = eh.bv.bv_val;
202         eh.bv.bv_val = buf;
203         eh.bv.bv_len = data.size;
204         rc = entry_header( &eh );
205         eoff = eh.data - eh.bv.bv_val;
206         eh.bv.bv_val = dptr;
207         if( rc ) {
208                 return NOID;
209         }
210
211         BDB_DISK2ID( key.data, &id );
212         return id;
213 }
214
215 ID bdb_tool_dn2id_get(
216         Backend *be,
217         struct berval *dn
218 )
219 {
220         Operation op = {0};
221         Opheader ohdr = {0};
222         EntryInfo *ei = NULL;
223         int rc;
224
225         if ( BER_BVISEMPTY(dn) )
226                 return 0;
227
228         op.o_hdr = &ohdr;
229         op.o_bd = be;
230         op.o_tmpmemctx = NULL;
231         op.o_tmpmfuncs = &ch_mfuncs;
232
233         rc = bdb_cache_find_ndn( &op, NULL, dn, &ei );
234         if ( ei ) bdb_cache_entryinfo_unlock( ei );
235         if ( rc == DB_NOTFOUND )
236                 return NOID;
237         
238         return ei->bei_id;
239 }
240
241 int bdb_tool_id2entry_get(
242         Backend *be,
243         ID id,
244         Entry **e
245 )
246 {
247         int rc = bdb_id2entry( be, NULL, 0, id, e );
248
249         if ( rc == DB_NOTFOUND && id == 0 ) {
250                 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
251                 struct berval gluebv = BER_BVC("glue");
252                 dummy->e_name.bv_val = ch_strdup( "" );
253                 dummy->e_nname.bv_val = ch_strdup( "" );
254                 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
255                 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
256                         &gluebv, NULL );
257                 *e = dummy;
258                 rc = LDAP_SUCCESS;
259         }
260         return rc;
261 }
262
263 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
264 {
265         int rc, off;
266         Entry *e = NULL;
267         char *dptr;
268
269         assert( be != NULL );
270         assert( slapMode & SLAP_TOOL_MODE );
271
272         /* Get the size */
273         data.flags ^= DB_DBT_PARTIAL;
274         data.ulen = 0;
275     rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
276         if ( rc != DB_BUFFER_SMALL ) goto leave;
277
278         /* Allocate a block and retrieve the data */
279         eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
280         eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len );
281         eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
282         data.data = eh.data;
283         data.ulen = data.size;
284
285         /* Skip past already parsed nattr/nvals */
286         eh.data += eoff;
287
288     rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
289         if ( rc ) goto leave;
290
291 #ifdef SLAP_ZONE_ALLOC
292         /* FIXME: will add ctx later */
293         rc = entry_decode( &eh, &e, NULL );
294 #else
295         rc = entry_decode( &eh, &e );
296 #endif
297
298         if( rc == LDAP_SUCCESS ) {
299                 e->e_id = id;
300 #ifdef BDB_HIER
301                 if ( slapMode & SLAP_TOOL_READONLY ) {
302                         EntryInfo *ei = NULL;
303                         Operation op = {0};
304                         Opheader ohdr = {0};
305
306                         op.o_hdr = &ohdr;
307                         op.o_bd = be;
308                         op.o_tmpmemctx = NULL;
309                         op.o_tmpmfuncs = &ch_mfuncs;
310
311                         rc = bdb_cache_find_parent( &op, NULL, cursor->locker, id, &ei );
312                         if ( rc == LDAP_SUCCESS ) {
313                                 bdb_cache_entryinfo_unlock( ei );
314                                 e->e_private = ei;
315                                 ei->bei_e = e;
316                                 bdb_fix_dn( e, 0 );
317                                 ei->bei_e = NULL;
318                                 e->e_private = NULL;
319                         }
320                 }
321 #endif
322         }
323 leave:
324         return e;
325 }
326
327 static int bdb_tool_next_id(
328         Operation *op,
329         DB_TXN *tid,
330         Entry *e,
331         struct berval *text,
332         int hole )
333 {
334         struct berval dn = e->e_name;
335         struct berval ndn = e->e_nname;
336         struct berval pdn, npdn;
337         EntryInfo *ei = NULL, eidummy;
338         int rc;
339
340         if (ndn.bv_len == 0) {
341                 e->e_id = 0;
342                 return 0;
343         }
344
345         rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
346         if ( ei ) bdb_cache_entryinfo_unlock( ei );
347         if ( rc == DB_NOTFOUND ) {
348                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
349                         ID eid = e->e_id;
350                         dnParent( &dn, &pdn );
351                         dnParent( &ndn, &npdn );
352                         e->e_name = pdn;
353                         e->e_nname = npdn;
354                         rc = bdb_tool_next_id( op, tid, e, text, 1 );
355                         e->e_name = dn;
356                         e->e_nname = ndn;
357                         if ( rc ) {
358                                 return rc;
359                         }
360                         /* If parent didn't exist, it was created just now
361                          * and its ID is now in e->e_id. Make sure the current
362                          * entry gets added under the new parent ID.
363                          */
364                         if ( eid != e->e_id ) {
365                                 eidummy.bei_id = e->e_id;
366                                 ei = &eidummy;
367                         }
368                 }
369                 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
370                 if ( rc ) {
371                         snprintf( text->bv_val, text->bv_len,
372                                 "next_id failed: %s (%d)",
373                                 db_strerror(rc), rc );
374                 Debug( LDAP_DEBUG_ANY,
375                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
376                         return rc;
377                 }
378                 rc = bdb_dn2id_add( op, tid, ei, e );
379                 if ( rc ) {
380                         snprintf( text->bv_val, text->bv_len, 
381                                 "dn2id_add failed: %s (%d)",
382                                 db_strerror(rc), rc );
383                 Debug( LDAP_DEBUG_ANY,
384                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
385                 } else if ( hole ) {
386                         if ( nholes == nhmax - 1 ) {
387                                 if ( holes == hbuf ) {
388                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
389                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
390                                 } else {
391                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
392                                 }
393                                 nhmax *= 2;
394                         }
395                         ber_dupbv( &holes[nholes].dn, &ndn );
396                         holes[nholes++].id = e->e_id;
397                 }
398         } else if ( !hole ) {
399                 unsigned i;
400
401                 e->e_id = ei->bei_id;
402
403                 for ( i=0; i<nholes; i++) {
404                         if ( holes[i].id == e->e_id ) {
405                                 int j;
406                                 free(holes[i].dn.bv_val);
407                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
408                                 holes[j].id = 0;
409                                 nholes--;
410                                 break;
411                         } else if ( holes[i].id > e->e_id ) {
412                                 break;
413                         }
414                 }
415         }
416         return rc;
417 }
418
419 static int
420 bdb_tool_index_add(
421         Operation *op,
422         DB_TXN *txn,
423         Entry *e )
424 {
425         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
426
427         if ( !bdb->bi_nattrs )
428                 return 0;
429
430         if ( slapMode & SLAP_TOOL_QUICK ) {
431                 IndexRec *ir;
432                 int i, rc;
433                 Attribute *a;
434                 
435                 ir = bdb_tool_index_rec;
436                 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
437
438                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
439                         rc = bdb_index_recset( bdb, a, a->a_desc->ad_type, 
440                                 &a->a_desc->ad_tags, ir );
441                         if ( rc )
442                                 return rc;
443                 }
444                 bdb_tool_ix_id = e->e_id;
445                 bdb_tool_ix_op = op;
446                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
447                 /* Wait for all threads to be ready */
448                 while ( bdb_tool_index_tcount ) {
449                         ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main, 
450                                 &bdb_tool_index_mutex );
451                 }
452                 for ( i=1; i<slap_tool_thread_max; i++ )
453                         bdb_tool_index_threads[i] = LDAP_BUSY;
454                 bdb_tool_index_tcount = slap_tool_thread_max - 1;
455                 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
456                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
457                 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
458                 if ( rc )
459                         return rc;
460                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
461                 for ( i=1; i<slap_tool_thread_max; i++ ) {
462                         if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
463                                 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main, 
464                                         &bdb_tool_index_mutex );
465                                 i--;
466                                 continue;
467                         }
468                         if ( bdb_tool_index_threads[i] ) {
469                                 rc = bdb_tool_index_threads[i];
470                                 break;
471                         }
472                 }
473                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
474                 return rc;
475         } else {
476                 return bdb_index_entry_add( op, txn, e );
477         }
478 }
479
480 ID bdb_tool_entry_put(
481         BackendDB *be,
482         Entry *e,
483         struct berval *text )
484 {
485         int rc;
486         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
487         DB_TXN *tid = NULL;
488         Operation op = {0};
489         Opheader ohdr = {0};
490
491         assert( be != NULL );
492         assert( slapMode & SLAP_TOOL_MODE );
493
494         assert( text != NULL );
495         assert( text->bv_val != NULL );
496         assert( text->bv_val[0] == '\0' );      /* overconservative? */
497
498         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
499                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
500
501         if (! (slapMode & SLAP_TOOL_QUICK)) {
502         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
503                 bdb->bi_db_opflags );
504         if( rc != 0 ) {
505                 snprintf( text->bv_val, text->bv_len,
506                         "txn_begin failed: %s (%d)",
507                         db_strerror(rc), rc );
508                 Debug( LDAP_DEBUG_ANY,
509                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
510                          text->bv_val, 0, 0 );
511                 return NOID;
512         }
513         }
514
515         op.o_hdr = &ohdr;
516         op.o_bd = be;
517         op.o_tmpmemctx = NULL;
518         op.o_tmpmfuncs = &ch_mfuncs;
519
520         /* add dn2id indices */
521         rc = bdb_tool_next_id( &op, tid, e, text, 0 );
522         if( rc != 0 ) {
523                 goto done;
524         }
525
526         if ( !bdb->bi_linear_index )
527                 rc = bdb_tool_index_add( &op, tid, e );
528         if( rc != 0 ) {
529                 snprintf( text->bv_val, text->bv_len,
530                                 "index_entry_add failed: %s (%d)",
531                                 db_strerror(rc), rc );
532                 Debug( LDAP_DEBUG_ANY,
533                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
534                         text->bv_val, 0, 0 );
535                 goto done;
536         }
537
538         /* id2entry index */
539         rc = bdb_id2entry_add( be, tid, e );
540         if( rc != 0 ) {
541                 snprintf( text->bv_val, text->bv_len,
542                                 "id2entry_add failed: %s (%d)",
543                                 db_strerror(rc), rc );
544                 Debug( LDAP_DEBUG_ANY,
545                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
546                         text->bv_val, 0, 0 );
547                 goto done;
548         }
549
550 done:
551         if( rc == 0 ) {
552                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
553                 rc = TXN_COMMIT( tid, 0 );
554                 if( rc != 0 ) {
555                         snprintf( text->bv_val, text->bv_len,
556                                         "txn_commit failed: %s (%d)",
557                                         db_strerror(rc), rc );
558                         Debug( LDAP_DEBUG_ANY,
559                                 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
560                                 text->bv_val, 0, 0 );
561                         e->e_id = NOID;
562                 }
563                 }
564
565         } else {
566                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
567                 TXN_ABORT( tid );
568                 snprintf( text->bv_val, text->bv_len,
569                         "txn_aborted! %s (%d)",
570                         db_strerror(rc), rc );
571                 Debug( LDAP_DEBUG_ANY,
572                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
573                         text->bv_val, 0, 0 );
574                 }
575                 e->e_id = NOID;
576         }
577
578         return e->e_id;
579 }
580
581 int bdb_tool_entry_reindex(
582         BackendDB *be,
583         ID id )
584 {
585         struct bdb_info *bi = (struct bdb_info *) be->be_private;
586         int rc;
587         Entry *e;
588         DB_TXN *tid = NULL;
589         Operation op = {0};
590         Opheader ohdr = {0};
591
592         Debug( LDAP_DEBUG_ARGS,
593                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
594                 (long) id, 0, 0 );
595
596         /* No indexes configured, nothing to do. Could return an
597          * error here to shortcut things.
598          */
599         if (!bi->bi_attrs) {
600                 return 0;
601         }
602
603         /* Get the first attribute to index */
604         if (bi->bi_linear_index && !index_nattrs) {
605                 index_nattrs = bi->bi_nattrs - 1;
606                 bi->bi_nattrs = 1;
607         }
608
609         e = bdb_tool_entry_get( be, id );
610
611         if( e == NULL ) {
612                 Debug( LDAP_DEBUG_ANY,
613                         LDAP_XSTRING(bdb_tool_entry_reindex)
614                         ": could not locate id=%ld\n",
615                         (long) id, 0, 0 );
616                 return -1;
617         }
618
619         if (! (slapMode & SLAP_TOOL_QUICK)) {
620         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
621         if( rc != 0 ) {
622                 Debug( LDAP_DEBUG_ANY,
623                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
624                         "txn_begin failed: %s (%d)\n",
625                         db_strerror(rc), rc, 0 );
626                 goto done;
627         }
628         }
629         
630         /*
631          * just (re)add them for now
632          * assume that some other routine (not yet implemented)
633          * will zap index databases
634          *
635          */
636
637         Debug( LDAP_DEBUG_TRACE,
638                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
639                 (long) id, e->e_dn, 0 );
640
641         op.o_hdr = &ohdr;
642         op.o_bd = be;
643         op.o_tmpmemctx = NULL;
644         op.o_tmpmfuncs = &ch_mfuncs;
645
646         rc = bdb_tool_index_add( &op, tid, e );
647
648 done:
649         if( rc == 0 ) {
650                 if (! (slapMode & SLAP_TOOL_QUICK)) {
651                 rc = TXN_COMMIT( tid, 0 );
652                 if( rc != 0 ) {
653                         Debug( LDAP_DEBUG_ANY,
654                                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
655                                 ": txn_commit failed: %s (%d)\n",
656                                 db_strerror(rc), rc, 0 );
657                         e->e_id = NOID;
658                 }
659                 }
660
661         } else {
662                 if (! (slapMode & SLAP_TOOL_QUICK)) {
663                 TXN_ABORT( tid );
664                 Debug( LDAP_DEBUG_ANY,
665                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
666                         ": txn_aborted! %s (%d)\n",
667                         db_strerror(rc), rc, 0 );
668                 }
669                 e->e_id = NOID;
670         }
671         bdb_entry_release( &op, e, 0 );
672
673         return rc;
674 }
675
676 ID bdb_tool_entry_modify(
677         BackendDB *be,
678         Entry *e,
679         struct berval *text )
680 {
681         int rc;
682         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
683         DB_TXN *tid = NULL;
684         Operation op = {0};
685         Opheader ohdr = {0};
686
687         assert( be != NULL );
688         assert( slapMode & SLAP_TOOL_MODE );
689
690         assert( text != NULL );
691         assert( text->bv_val != NULL );
692         assert( text->bv_val[0] == '\0' );      /* overconservative? */
693
694         assert ( e->e_id != NOID );
695
696         Debug( LDAP_DEBUG_TRACE,
697                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
698                 (long) e->e_id, e->e_dn, 0 );
699
700         if (! (slapMode & SLAP_TOOL_QUICK)) {
701         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
702                 bdb->bi_db_opflags );
703         if( rc != 0 ) {
704                 snprintf( text->bv_val, text->bv_len,
705                         "txn_begin failed: %s (%d)",
706                         db_strerror(rc), rc );
707                 Debug( LDAP_DEBUG_ANY,
708                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
709                          text->bv_val, 0, 0 );
710                 return NOID;
711         }
712         }
713
714         op.o_hdr = &ohdr;
715         op.o_bd = be;
716         op.o_tmpmemctx = NULL;
717         op.o_tmpmfuncs = &ch_mfuncs;
718
719         /* id2entry index */
720         rc = bdb_id2entry_update( be, tid, e );
721         if( rc != 0 ) {
722                 snprintf( text->bv_val, text->bv_len,
723                                 "id2entry_add failed: %s (%d)",
724                                 db_strerror(rc), rc );
725                 Debug( LDAP_DEBUG_ANY,
726                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
727                         text->bv_val, 0, 0 );
728                 goto done;
729         }
730
731 done:
732         if( rc == 0 ) {
733                 if (! (slapMode & SLAP_TOOL_QUICK)) {
734                 rc = TXN_COMMIT( tid, 0 );
735                 if( rc != 0 ) {
736                         snprintf( text->bv_val, text->bv_len,
737                                         "txn_commit failed: %s (%d)",
738                                         db_strerror(rc), rc );
739                         Debug( LDAP_DEBUG_ANY,
740                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
741                                 "%s\n", text->bv_val, 0, 0 );
742                         e->e_id = NOID;
743                 }
744                 }
745
746         } else {
747                 if (! (slapMode & SLAP_TOOL_QUICK)) {
748                 TXN_ABORT( tid );
749                 snprintf( text->bv_val, text->bv_len,
750                         "txn_aborted! %s (%d)",
751                         db_strerror(rc), rc );
752                 Debug( LDAP_DEBUG_ANY,
753                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
754                         text->bv_val, 0, 0 );
755                 }
756                 e->e_id = NOID;
757         }
758
759         return e->e_id;
760 }
761
762 #ifdef BDB_TOOL_IDL_CACHING
763 static int
764 bdb_tool_idl_cmp( const void *v1, const void *v2 )
765 {
766         const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
767         int rc;
768
769         if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
770         return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
771 }
772
773 static int
774 bdb_tool_idl_flush_one( void *v1, void *arg )
775 {
776         bdb_tool_idl_cache *ic = v1;
777         DB *db = arg;
778         struct bdb_info *bdb = bdb_tool_info;
779         bdb_tool_idl_cache_entry *ice;
780         DBC *curs;
781         DBT key, data;
782         int i, rc;
783         ID id, nid;
784
785         /* Freshly allocated, ignore it */
786         if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
787                 return 0;
788         }
789
790         rc = db->cursor( db, NULL, &curs, 0 );
791         if ( rc )
792                 return -1;
793
794         DBTzero( &key );
795         DBTzero( &data );
796
797         bv2DBT( &ic->kstr, &key );
798
799         data.size = data.ulen = sizeof( ID );
800         data.flags = DB_DBT_USERMEM;
801         data.data = &nid;
802
803         rc = curs->c_get( curs, &key, &data, DB_SET );
804         /* If key already exists and we're writing a range... */
805         if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
806                 /* If it's not currently a range, must delete old info */
807                 if ( nid ) {
808                         /* Skip lo */
809                         while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
810                                 curs->c_del( curs, 0 );
811
812                         nid = 0;
813                         /* Store range marker */
814                         curs->c_put( curs, &key, &data, DB_KEYFIRST );
815                 } else {
816                         
817                         /* Skip lo */
818                         rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
819
820                         /* Get hi */
821                         rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
822
823                         /* Delete hi */
824                         curs->c_del( curs, 0 );
825                 }
826                 BDB_ID2DISK( ic->last, &nid );
827                 curs->c_put( curs, &key, &data, DB_KEYLAST );
828                 rc = 0;
829         } else if ( rc && rc != DB_NOTFOUND ) {
830                 rc = -1;
831         } else if ( ic->count > BDB_IDL_DB_SIZE ) {
832                 /* range, didn't exist before */
833                 nid = 0;
834                 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
835                 if ( rc == 0 ) {
836                         BDB_ID2DISK( ic->first, &nid );
837                         rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
838                         if ( rc == 0 ) {
839                                 BDB_ID2DISK( ic->last, &nid );
840                                 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
841                         }
842                 }
843                 if ( rc ) {
844                         rc = -1;
845                 }
846         } else {
847                 int n;
848
849                 /* Just a normal write */
850                 rc = 0;
851                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
852                         int end;
853                         if ( ice->next ) {
854                                 end = IDBLOCK;
855                         } else {
856                                 end = ic->count & (IDBLOCK-1);
857                                 if ( !end )
858                                         end = IDBLOCK;
859                         }
860                         for ( i=0; i<end; i++ ) {
861                                 if ( !ice->ids[i] ) continue;
862                                 BDB_ID2DISK( ice->ids[i], &nid );
863                                 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
864                                 if ( rc ) {
865                                         if ( rc == DB_KEYEXIST ) {
866                                                 rc = 0;
867                                                 continue;
868                                         }
869                                         rc = -1;
870                                         break;
871                                 }
872                         }
873                         if ( rc ) {
874                                 rc = -1;
875                                 break;
876                         }
877                 }
878                 if ( ic->head ) {
879                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
880                         ic->tail->next = bdb_tool_idl_free_list;
881                         bdb_tool_idl_free_list = ic->head;
882                         bdb->bi_idl_cache_size -= n;
883                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
884                 }
885         }
886         if ( ic != db->app_private ) {
887                 ch_free( ic );
888         } else {
889                 ic->head = ic->tail = NULL;
890         }
891         curs->c_close( curs );
892         return rc;
893 }
894
895 static int
896 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
897 {
898         Avlnode *root = db->app_private;
899         int rc;
900
901         db->app_private = ic;
902         rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
903         avl_free( root, NULL );
904         db->app_private = NULL;
905         if ( rc != -1 )
906                 rc = 0;
907         return rc;
908 }
909
910 static int
911 bdb_tool_idl_flush( BackendDB *be )
912 {
913         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
914         DB *db;
915         Avlnode *root;
916         int i, rc = 0;
917
918         for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
919                 db = bdb->bi_databases[i]->bdi_db;
920                 if ( !db->app_private ) continue;
921                 rc = bdb_tool_idl_flush_db( db, NULL );
922                 if ( rc )
923                         break;
924         }
925         if ( !rc ) {
926                 bdb->bi_idl_cache_size = 0;
927         }
928         return rc;
929 }
930
931 int bdb_tool_idl_add(
932         BackendDB *be,
933         DB *db,
934         DB_TXN *txn,
935         DBT *key,
936         ID id )
937 {
938         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
939         bdb_tool_idl_cache *ic, itmp;
940         bdb_tool_idl_cache_entry *ice;
941         int rc;
942
943         if ( !bdb->bi_idl_cache_max_size )
944                 return bdb_idl_insert_key( be, db, txn, key, id );
945
946         DBT2bv( key, &itmp.kstr );
947
948         ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
949
950         /* No entry yet, create one */
951         if ( !ic ) {
952                 DBC *curs;
953                 DBT data;
954                 ID nid;
955                 int rc;
956
957                 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
958                 ic->kstr.bv_len = itmp.kstr.bv_len;
959                 ic->kstr.bv_val = (char *)(ic+1);
960                 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
961                 ic->head = ic->tail = NULL;
962                 ic->last = 0;
963                 ic->count = 0;
964                 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
965                         avl_dup_error );
966
967                 /* load existing key count here */
968                 rc = db->cursor( db, NULL, &curs, 0 );
969                 if ( rc ) return rc;
970
971                 data.ulen = sizeof( ID );
972                 data.flags = DB_DBT_USERMEM;
973                 data.data = &nid;
974                 rc = curs->c_get( curs, key, &data, DB_SET );
975                 if ( rc == 0 ) {
976                         if ( nid == 0 ) {
977                                 ic->count = BDB_IDL_DB_SIZE+1;
978                         } else {
979                                 db_recno_t count;
980
981                                 curs->c_count( curs, &count, 0 );
982                                 ic->count = count;
983                                 BDB_DISK2ID( &nid, &ic->first );
984                         }
985                 }
986                 curs->c_close( curs );
987         }
988         /* are we a range already? */
989         if ( ic->count > BDB_IDL_DB_SIZE ) {
990                 ic->last = id;
991                 return 0;
992         /* Are we at the limit, and converting to a range? */
993         } else if ( ic->count == BDB_IDL_DB_SIZE ) {
994                 int n;
995                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
996                         /* counting */ ;
997                 if ( n ) {
998                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
999                         ic->tail->next = bdb_tool_idl_free_list;
1000                         bdb_tool_idl_free_list = ic->head;
1001                         bdb->bi_idl_cache_size -= n;
1002                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1003                 }
1004                 ic->head = ic->tail = NULL;
1005                 ic->last = id;
1006                 ic->count++;
1007                 return 0;
1008         }
1009         /* No free block, create that too */
1010         if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1011                 ice = NULL;
1012                 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1013                 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1014                         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1015                         rc = bdb_tool_idl_flush_db( db, ic );
1016                         if ( rc )
1017                                 return rc;
1018                         avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1019                                 avl_dup_error );
1020                         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1021                 }
1022                 bdb->bi_idl_cache_size++;
1023                 if ( bdb_tool_idl_free_list ) {
1024                         ice = bdb_tool_idl_free_list;
1025                         bdb_tool_idl_free_list = ice->next;
1026                 }
1027                 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1028                 if ( !ice ) {
1029                         ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1030                 }
1031                 memset( ice, 0, sizeof( *ice ));
1032                 if ( !ic->head ) {
1033                         ic->head = ice;
1034                 } else {
1035                         ic->tail->next = ice;
1036                 }
1037                 ic->tail = ice;
1038                 if ( !ic->count )
1039                         ic->first = id;
1040         }
1041         ice = ic->tail;
1042         ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1043         ic->count++;
1044
1045         return 0;
1046 }
1047 #endif
1048
1049 static void *
1050 bdb_tool_index_task( void *ctx, void *ptr )
1051 {
1052         int base = *(int *)ptr;
1053
1054         free( ptr );
1055         while ( 1 ) {
1056                 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1057                 bdb_tool_index_tcount--;
1058                 if ( !bdb_tool_index_tcount )
1059                         ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1060                 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1061                         &bdb_tool_index_mutex );
1062                 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1063                 if ( slapd_shutdown )
1064                         break;
1065
1066                 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1067                         bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );
1068         }
1069
1070         return NULL;
1071 }