]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/tools.c
Merge remote branch 'mdb/mdb.master'
[openldap] / servers / slapd / back-mdb / 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 2011 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-mdb.h"
25 #include "idl.h"
26
27 static MDB_txn *txn = NULL, *txi = NULL;
28 static MDB_cursor *cursor = NULL, *idcursor = NULL;
29 static MDB_val key, data;
30 static EntryHeader eh;
31 static ID previd = NOID;
32
33 typedef struct dn_id {
34         ID id;
35         struct berval dn;
36 } dn_id;
37
38 #define HOLE_SIZE       4096
39 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
40 static unsigned nhmax = HOLE_SIZE;
41 static unsigned nholes;
42
43 static struct berval    *tool_base;
44 static int              tool_scope;
45 static Filter           *tool_filter;
46 static Entry            *tool_next_entry;
47
48 #if 0
49 static ID mdb_tool_ix_id;
50 static Operation *mdb_tool_ix_op;
51 static int *mdb_tool_index_threads, mdb_tool_index_tcount;
52 static void *mdb_tool_index_rec;
53 static struct mdb_info *mdb_tool_info;
54 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
55 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
56 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
57 static void * mdb_tool_index_task( void *ctx, void *ptr );
58 #endif
59
60 static int      mdb_writes, mdb_writes_per_commit;
61
62 static int
63 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
64
65 int mdb_tool_entry_open(
66         BackendDB *be, int mode )
67 {
68         /* In Quick mode, commit once per 1000 entries */
69         mdb_writes = 0;
70         if ( slapMode & SLAP_TOOL_QUICK )
71                 mdb_writes_per_commit = 1000;
72         else
73                 mdb_writes_per_commit = 1;
74
75 #if 0
76         /* Set up for threaded slapindex */
77         if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
78                 if ( !mdb_tool_info ) {
79                         ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
80                         ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
81                         ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
82                         if ( mdb->bi_nattrs ) {
83                                 int i;
84                                 mdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
85                                 mdb_tool_index_rec = ch_malloc( mdb->bi_nattrs * sizeof( IndexRec ));
86                                 mdb_tool_index_tcount = slap_tool_thread_max - 1;
87                                 for (i=1; i<slap_tool_thread_max; i++) {
88                                         int *ptr = ch_malloc( sizeof( int ));
89                                         *ptr = i;
90                                         ldap_pvt_thread_pool_submit( &connection_pool,
91                                                 mdb_tool_index_task, ptr );
92                                 }
93                         }
94                         mdb_tool_info = mdb;
95                 }
96         }
97 #endif
98
99         return 0;
100 }
101
102 int mdb_tool_entry_close(
103         BackendDB *be )
104 {
105 #if 0
106         if ( mdb_tool_info ) {
107                 slapd_shutdown = 1;
108                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
109
110                 /* There might still be some threads starting */
111                 while ( mdb_tool_index_tcount ) {
112                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
113                                         &mdb_tool_index_mutex );
114                 }
115
116                 mdb_tool_index_tcount = slap_tool_thread_max - 1;
117                 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
118
119                 /* Make sure all threads are stopped */
120                 while ( mdb_tool_index_tcount ) {
121                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
122                                 &mdb_tool_index_mutex );
123                 }
124                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
125
126                 mdb_tool_info = NULL;
127                 slapd_shutdown = 0;
128                 ch_free( mdb_tool_index_threads );
129                 ch_free( mdb_tool_index_rec );
130                 mdb_tool_index_tcount = slap_tool_thread_max - 1;
131         }
132 #endif
133
134         if( idcursor ) {
135                 mdb_cursor_close( idcursor );
136                 idcursor = NULL;
137         }
138         if( cursor ) {
139                 mdb_cursor_close( cursor );
140                 cursor = NULL;
141         }
142         if( txn ) {
143                 if ( mdb_txn_commit( txn ))
144                         return -1;
145                 txn = NULL;
146         }
147
148         if( nholes ) {
149                 unsigned i;
150                 fprintf( stderr, "Error, entries missing!\n");
151                 for (i=0; i<nholes; i++) {
152                         fprintf(stderr, "  entry %ld: %s\n",
153                                 holes[i].id, holes[i].dn.bv_val);
154                 }
155                 nholes = 0;
156                 return -1;
157         }
158                         
159         return 0;
160 }
161
162 ID
163 mdb_tool_entry_first_x(
164         BackendDB *be,
165         struct berval *base,
166         int scope,
167         Filter *f )
168 {
169         tool_base = base;
170         tool_scope = scope;
171         tool_filter = f;
172         
173         return mdb_tool_entry_next( be );
174 }
175
176 ID mdb_tool_entry_next(
177         BackendDB *be )
178 {
179         int rc;
180         ID id;
181         struct mdb_info *mdb;
182
183         assert( be != NULL );
184         assert( slapMode & SLAP_TOOL_MODE );
185
186         mdb = (struct mdb_info *) be->be_private;
187         assert( mdb != NULL );
188
189         if ( !txn ) {
190                 rc = mdb_txn_begin( mdb->mi_dbenv, MDB_RDONLY, &txn );
191                 if ( rc )
192                         return NOID;
193                 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
194                 if ( rc ) {
195                         mdb_txn_abort( txn );
196                         return NOID;
197                 }
198         }
199
200 next:;
201         rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
202
203         if( rc ) {
204                 return NOID;
205         }
206
207         previd = *(ID *)key.mv_data;
208         id = previd;
209
210         if ( tool_filter || tool_base ) {
211                 static Operation op = {0};
212                 static Opheader ohdr = {0};
213
214                 op.o_hdr = &ohdr;
215                 op.o_bd = be;
216                 op.o_tmpmemctx = NULL;
217                 op.o_tmpmfuncs = &ch_mfuncs;
218
219                 if ( tool_next_entry ) {
220                         mdb_entry_release( &op, tool_next_entry, 0 );
221                         tool_next_entry = NULL;
222                 }
223
224                 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
225                 if ( rc == LDAP_NO_SUCH_OBJECT ) {
226                         goto next;
227                 }
228
229                 assert( tool_next_entry != NULL );
230
231                 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
232                 {
233                         mdb_entry_release( &op, tool_next_entry, 0 );
234                         tool_next_entry = NULL;
235                         goto next;
236                 }
237         }
238
239         return id;
240 }
241
242 ID mdb_tool_dn2id_get(
243         Backend *be,
244         struct berval *dn
245 )
246 {
247         struct mdb_info *mdb;
248         Operation op = {0};
249         Opheader ohdr = {0};
250         ID id;
251         int rc;
252
253         if ( BER_BVISEMPTY(dn) )
254                 return 0;
255
256         mdb = (struct mdb_info *) be->be_private;
257
258         if ( !txn ) {
259                 rc = mdb_txn_begin( mdb->mi_dbenv, (slapMode & SLAP_TOOL_READONLY) != 0 ?
260                         MDB_RDONLY : 0, &txn );
261                 if ( rc )
262                         return NOID;
263         }
264
265         op.o_hdr = &ohdr;
266         op.o_bd = be;
267         op.o_tmpmemctx = NULL;
268         op.o_tmpmfuncs = &ch_mfuncs;
269
270         rc = mdb_dn2id( &op, txn, dn, &id, NULL, NULL );
271         if ( rc == MDB_NOTFOUND )
272                 return NOID;
273         
274         return id;
275 }
276
277 static int
278 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
279 {
280         Entry *e = NULL;
281         struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
282         int rc;
283
284         assert( be != NULL );
285         assert( slapMode & SLAP_TOOL_MODE );
286
287         if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
288                 *ep = tool_next_entry;
289                 tool_next_entry = NULL;
290                 return LDAP_SUCCESS;
291         }
292
293         if ( id != previd ) {
294                 key.mv_size = sizeof(ID);
295                 key.mv_data = &id;
296                 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
297                 if ( rc ) {
298                         rc = LDAP_OTHER;
299                         goto done;
300                 }
301         }
302
303         if ( slapMode & SLAP_TOOL_READONLY ) {
304                 Operation op = {0};
305                 Opheader ohdr = {0};
306
307                 op.o_hdr = &ohdr;
308                 op.o_bd = be;
309                 op.o_tmpmemctx = NULL;
310                 op.o_tmpmfuncs = &ch_mfuncs;
311
312                 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
313                 if ( rc  ) {
314                         rc = LDAP_OTHER;
315                         mdb_entry_return( e );
316                         e = NULL;
317                         goto done;
318                 }
319                 if ( tool_base != NULL ) {
320                         if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
321                                 ch_free( dn.bv_val );
322                                 ch_free( ndn.bv_val );
323                                 rc = LDAP_NO_SUCH_OBJECT;
324                         }
325                 }
326         }
327         /* Get the header */
328         eh.bv.bv_val = data.mv_data;
329         eh.bv.bv_len = data.mv_size;
330
331         rc = entry_header( &eh );
332         if ( rc ) {
333                 rc = LDAP_OTHER;
334                 goto done;
335         }
336         eh.bv.bv_len = eh.nvals * sizeof( struct berval );
337         eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
338         rc = entry_decode( &eh, &e );
339         e->e_id = id;
340         if ( !BER_BVISNULL( &dn )) {
341                 e->e_name = dn;
342                 e->e_nname = ndn;
343         } else {
344                 e->e_name.bv_val = NULL;
345                 e->e_nname.bv_val = NULL;
346         }
347         e->e_bv = eh.bv;
348
349 done:
350         if ( e != NULL ) {
351                 *ep = e;
352         }
353
354         return rc;
355 }
356
357 Entry*
358 mdb_tool_entry_get( BackendDB *be, ID id )
359 {
360         Entry *e = NULL;
361
362         (void)mdb_tool_entry_get_int( be, id, &e );
363         return e;
364 }
365
366 static int mdb_tool_next_id(
367         Operation *op,
368         MDB_txn *tid,
369         Entry *e,
370         struct berval *text,
371         int hole )
372 {
373         struct berval dn = e->e_name;
374         struct berval ndn = e->e_nname;
375         struct berval pdn, npdn, nmatched;
376         ID id, pid = 0;
377         int rc;
378
379         if (ndn.bv_len == 0) {
380                 e->e_id = 0;
381                 return 0;
382         }
383
384         rc = mdb_dn2id( op, tid, &ndn, &id, NULL, &nmatched );
385         if ( rc == MDB_NOTFOUND ) {
386                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
387                         ID eid = e->e_id;
388                         dnParent( &ndn, &npdn );
389                         if ( nmatched.bv_len != npdn.bv_len ) {
390                                 dnParent( &dn, &pdn );
391                                 e->e_name = pdn;
392                                 e->e_nname = npdn;
393                                 rc = mdb_tool_next_id( op, tid, e, text, 1 );
394                                 e->e_name = dn;
395                                 e->e_nname = ndn;
396                                 if ( rc ) {
397                                         return rc;
398                                 }
399                                 /* If parent didn't exist, it was created just now
400                                  * and its ID is now in e->e_id. Make sure the current
401                                  * entry gets added under the new parent ID.
402                                  */
403                                 if ( eid != e->e_id ) {
404                                         pid = e->e_id;
405                                 }
406                         } else {
407                                 pid = id;
408                         }
409                 }
410                 rc = mdb_next_id( op->o_bd, tid, &e->e_id );
411                 if ( rc ) {
412                         snprintf( text->bv_val, text->bv_len,
413                                 "next_id failed: %s (%d)",
414                                 mdb_strerror(rc), rc );
415                 Debug( LDAP_DEBUG_ANY,
416                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
417                         return rc;
418                 }
419                 rc = mdb_dn2id_add( op, tid, pid, e );
420                 if ( rc ) {
421                         snprintf( text->bv_val, text->bv_len, 
422                                 "dn2id_add failed: %s (%d)",
423                                 mdb_strerror(rc), rc );
424                 Debug( LDAP_DEBUG_ANY,
425                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
426                 } else if ( hole ) {
427                         if ( nholes == nhmax - 1 ) {
428                                 if ( holes == hbuf ) {
429                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
430                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
431                                 } else {
432                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
433                                 }
434                                 nhmax *= 2;
435                         }
436                         ber_dupbv( &holes[nholes].dn, &ndn );
437                         holes[nholes++].id = e->e_id;
438                 }
439         } else if ( !hole ) {
440                 unsigned i, j;
441
442                 e->e_id = id;
443
444                 for ( i=0; i<nholes; i++) {
445                         if ( holes[i].id == e->e_id ) {
446                                 free(holes[i].dn.bv_val);
447                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
448                                 holes[j].id = 0;
449                                 nholes--;
450                                 break;
451                         } else if ( holes[i].id > e->e_id ) {
452                                 break;
453                         }
454                 }
455         }
456         return rc;
457 }
458
459 static int
460 mdb_tool_index_add(
461         Operation *op,
462         MDB_txn *txn,
463         Entry *e )
464 {
465         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
466
467         if ( !mdb->mi_nattrs )
468                 return 0;
469
470 #if 0
471         if ( slapMode & SLAP_TOOL_QUICK ) {
472                 IndexRec *ir;
473                 int i, rc;
474                 Attribute *a;
475                 
476                 ir = mdb_tool_index_rec;
477                 memset(ir, 0, mdb->bi_nattrs * sizeof( IndexRec ));
478
479                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
480                         rc = mdb_index_recset( mdb, a, a->a_desc->ad_type, 
481                                 &a->a_desc->ad_tags, ir );
482                         if ( rc )
483                                 return rc;
484                 }
485                 mdb_tool_ix_id = e->e_id;
486                 mdb_tool_ix_op = op;
487                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
488                 /* Wait for all threads to be ready */
489                 while ( mdb_tool_index_tcount ) {
490                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main, 
491                                 &mdb_tool_index_mutex );
492                 }
493                 for ( i=1; i<slap_tool_thread_max; i++ )
494                         mdb_tool_index_threads[i] = LDAP_BUSY;
495                 mdb_tool_index_tcount = slap_tool_thread_max - 1;
496                 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
497                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
498                 rc = mdb_index_recrun( op, mdb, ir, e->e_id, 0 );
499                 if ( rc )
500                         return rc;
501                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
502                 for ( i=1; i<slap_tool_thread_max; i++ ) {
503                         if ( mdb_tool_index_threads[i] == LDAP_BUSY ) {
504                                 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main, 
505                                         &mdb_tool_index_mutex );
506                                 i--;
507                                 continue;
508                         }
509                         if ( mdb_tool_index_threads[i] ) {
510                                 rc = mdb_tool_index_threads[i];
511                                 break;
512                         }
513                 }
514                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
515                 return rc;
516         } else
517 #endif
518         {
519                 return mdb_index_entry_add( op, txn, e );
520         }
521 }
522
523 ID mdb_tool_entry_put(
524         BackendDB *be,
525         Entry *e,
526         struct berval *text )
527 {
528         int rc;
529         struct mdb_info *mdb;
530         Operation op = {0};
531         Opheader ohdr = {0};
532
533         assert( be != NULL );
534         assert( slapMode & SLAP_TOOL_MODE );
535
536         assert( text != NULL );
537         assert( text->bv_val != NULL );
538         assert( text->bv_val[0] == '\0' );      /* overconservative? */
539
540         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
541                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
542
543         mdb = (struct mdb_info *) be->be_private;
544
545         if ( !txn ) {
546         rc = mdb_txn_begin( mdb->mi_dbenv, 0, &txn );
547         if( rc != 0 ) {
548                 snprintf( text->bv_val, text->bv_len,
549                         "txn_begin failed: %s (%d)",
550                         mdb_strerror(rc), rc );
551                 Debug( LDAP_DEBUG_ANY,
552                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
553                          text->bv_val, 0, 0 );
554                 return NOID;
555         }
556         }
557
558         op.o_hdr = &ohdr;
559         op.o_bd = be;
560         op.o_tmpmemctx = NULL;
561         op.o_tmpmfuncs = &ch_mfuncs;
562
563         /* add dn2id indices */
564         rc = mdb_tool_next_id( &op, txn, e, text, 0 );
565         if( rc != 0 ) {
566                 goto done;
567         }
568
569         rc = mdb_tool_index_add( &op, txn, e );
570         if( rc != 0 ) {
571                 snprintf( text->bv_val, text->bv_len,
572                                 "index_entry_add failed: %s (%d)",
573                                 rc == LDAP_OTHER ? "Internal error" :
574                                 mdb_strerror(rc), rc );
575                 Debug( LDAP_DEBUG_ANY,
576                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
577                         text->bv_val, 0, 0 );
578                 goto done;
579         }
580
581
582         /* id2entry index */
583         rc = mdb_id2entry_add( &op, txn, e );
584         if( rc != 0 ) {
585                 snprintf( text->bv_val, text->bv_len,
586                                 "id2entry_add failed: %s (%d)",
587                                 mdb_strerror(rc), rc );
588                 Debug( LDAP_DEBUG_ANY,
589                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
590                         text->bv_val, 0, 0 );
591                 goto done;
592         }
593
594 done:
595         if( rc == 0 ) {
596                 mdb_writes++;
597                 if ( mdb_writes >= mdb_writes_per_commit ) {
598                         rc = mdb_txn_commit( txn );
599                         mdb_writes = 0;
600                         txn = NULL;
601                         if( rc != 0 ) {
602                                 snprintf( text->bv_val, text->bv_len,
603                                                 "txn_commit failed: %s (%d)",
604                                                 mdb_strerror(rc), rc );
605                                 Debug( LDAP_DEBUG_ANY,
606                                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
607                                         text->bv_val, 0, 0 );
608                                 e->e_id = NOID;
609                         }
610                 }
611
612         } else {
613                 mdb_txn_abort( txn );
614                 txn = NULL;
615                 snprintf( text->bv_val, text->bv_len,
616                         "txn_aborted! %s (%d)",
617                         rc == LDAP_OTHER ? "Internal error" :
618                         mdb_strerror(rc), rc );
619                 Debug( LDAP_DEBUG_ANY,
620                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
621                         text->bv_val, 0, 0 );
622                 e->e_id = NOID;
623         }
624
625         return e->e_id;
626 }
627
628 int mdb_tool_entry_reindex(
629         BackendDB *be,
630         ID id,
631         AttributeDescription **adv )
632 {
633         struct mdb_info *mi = (struct mdb_info *) be->be_private;
634         int rc;
635         Entry *e;
636         Operation op = {0};
637         Opheader ohdr = {0};
638
639         Debug( LDAP_DEBUG_ARGS,
640                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
641                 (long) id, 0, 0 );
642         assert( tool_base == NULL );
643         assert( tool_filter == NULL );
644
645         /* No indexes configured, nothing to do. Could return an
646          * error here to shortcut things.
647          */
648         if (!mi->mi_attrs) {
649                 return 0;
650         }
651
652         /* Check for explicit list of attrs to index */
653         if ( adv ) {
654                 int i, j, n;
655
656                 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
657                         /* count */
658                         for ( n = 0; adv[n]; n++ ) ;
659
660                         /* insertion sort */
661                         for ( i = 0; i < n; i++ ) {
662                                 AttributeDescription *ad = adv[i];
663                                 for ( j = i-1; j>=0; j--) {
664                                         if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
665                                         adv[j+1] = adv[j];
666                                 }
667                                 adv[j+1] = ad;
668                         }
669                 }
670
671                 for ( i = 0; adv[i]; i++ ) {
672                         if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
673                                 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
674                                         if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
675                                                 AttrInfo *ai = mi->mi_attrs[i];
676                                                 mi->mi_attrs[i] = mi->mi_attrs[j];
677                                                 mi->mi_attrs[j] = ai;
678                                                 break;
679                                         }
680                                 }
681                                 if ( j == mi->mi_nattrs ) {
682                                         Debug( LDAP_DEBUG_ANY,
683                                                 LDAP_XSTRING(mdb_tool_entry_reindex)
684                                                 ": no index configured for %s\n",
685                                                 adv[i]->ad_cname.bv_val, 0, 0 );
686                                         return -1;
687                                 }
688                         }
689                 }
690                 mi->mi_nattrs = i;
691         }
692
693         e = mdb_tool_entry_get( be, id );
694
695         if( e == NULL ) {
696                 Debug( LDAP_DEBUG_ANY,
697                         LDAP_XSTRING(mdb_tool_entry_reindex)
698                         ": could not locate id=%ld\n",
699                         (long) id, 0, 0 );
700                 return -1;
701         }
702
703         if ( !txi ) {
704                 rc = mdb_txn_begin( mi->mi_dbenv, 0, &txi );
705                 if( rc != 0 ) {
706                         Debug( LDAP_DEBUG_ANY,
707                                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
708                                 "txn_begin failed: %s (%d)\n",
709                                 mdb_strerror(rc), rc, 0 );
710                         goto done;
711                 }
712         }
713         
714         /*
715          * just (re)add them for now
716          * assume that some other routine (not yet implemented)
717          * will zap index databases
718          *
719          */
720
721         Debug( LDAP_DEBUG_TRACE,
722                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
723                 (long) id, e->e_dn, 0 );
724
725         op.o_hdr = &ohdr;
726         op.o_bd = be;
727         op.o_tmpmemctx = NULL;
728         op.o_tmpmfuncs = &ch_mfuncs;
729
730         rc = mdb_tool_index_add( &op, txi, e );
731
732 done:
733         if( rc == 0 ) {
734                 mdb_writes++;
735                 if ( mdb_writes >= mdb_writes_per_commit ) {
736                         rc = mdb_txn_commit( txi );
737                         if( rc != 0 ) {
738                                 Debug( LDAP_DEBUG_ANY,
739                                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
740                                         ": txn_commit failed: %s (%d)\n",
741                                         mdb_strerror(rc), rc, 0 );
742                                 e->e_id = NOID;
743                         }
744                         txi = NULL;
745                 }
746
747         } else {
748                 mdb_txn_abort( txi );
749                 Debug( LDAP_DEBUG_ANY,
750                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
751                         ": txn_aborted! %s (%d)\n",
752                         mdb_strerror(rc), rc, 0 );
753                 e->e_id = NOID;
754                 txi = NULL;
755         }
756         mdb_entry_release( &op, e, 0 );
757
758         return rc;
759 }
760
761 ID mdb_tool_entry_modify(
762         BackendDB *be,
763         Entry *e,
764         struct berval *text )
765 {
766         int rc;
767         struct mdb_info *mdb;
768         MDB_txn *tid;
769         Operation op = {0};
770         Opheader ohdr = {0};
771
772         assert( be != NULL );
773         assert( slapMode & SLAP_TOOL_MODE );
774
775         assert( text != NULL );
776         assert( text->bv_val != NULL );
777         assert( text->bv_val[0] == '\0' );      /* overconservative? */
778
779         assert ( e->e_id != NOID );
780
781         Debug( LDAP_DEBUG_TRACE,
782                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
783                 (long) e->e_id, e->e_dn, 0 );
784
785         mdb = (struct mdb_info *) be->be_private;
786
787         if( cursor ) {
788                 mdb_cursor_close( cursor );
789                 cursor = NULL;
790         }
791         rc = mdb_txn_begin( mdb->mi_dbenv, 0, &tid );
792         if( rc != 0 ) {
793                 snprintf( text->bv_val, text->bv_len,
794                         "txn_begin failed: %s (%d)",
795                         mdb_strerror(rc), rc );
796                 Debug( LDAP_DEBUG_ANY,
797                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
798                          text->bv_val, 0, 0 );
799                 return NOID;
800         }
801
802         op.o_hdr = &ohdr;
803         op.o_bd = be;
804         op.o_tmpmemctx = NULL;
805         op.o_tmpmfuncs = &ch_mfuncs;
806
807         /* id2entry index */
808         rc = mdb_id2entry_update( &op, tid, e );
809         if( rc != 0 ) {
810                 snprintf( text->bv_val, text->bv_len,
811                                 "id2entry_add failed: %s (%d)",
812                                 mdb_strerror(rc), rc );
813                 Debug( LDAP_DEBUG_ANY,
814                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
815                         text->bv_val, 0, 0 );
816                 goto done;
817         }
818
819 done:
820         if( rc == 0 ) {
821                 rc = mdb_txn_commit( tid );
822                 if( rc != 0 ) {
823                         snprintf( text->bv_val, text->bv_len,
824                                         "txn_commit failed: %s (%d)",
825                                         mdb_strerror(rc), rc );
826                         Debug( LDAP_DEBUG_ANY,
827                                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
828                                 "%s\n", text->bv_val, 0, 0 );
829                         e->e_id = NOID;
830                 }
831
832         } else {
833                 mdb_txn_abort( tid );
834                 snprintf( text->bv_val, text->bv_len,
835                         "txn_aborted! %s (%d)",
836                         mdb_strerror(rc), rc );
837                 Debug( LDAP_DEBUG_ANY,
838                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
839                         text->bv_val, 0, 0 );
840                 e->e_id = NOID;
841         }
842
843         return e->e_id;
844 }
845
846 #if 0
847 static void *
848 mdb_tool_index_task( void *ctx, void *ptr )
849 {
850         int base = *(int *)ptr;
851
852         free( ptr );
853         while ( 1 ) {
854                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
855                 mdb_tool_index_tcount--;
856                 if ( !mdb_tool_index_tcount )
857                         ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
858                 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
859                         &mdb_tool_index_mutex );
860                 if ( slapd_shutdown ) {
861                         mdb_tool_index_tcount--;
862                         if ( !mdb_tool_index_tcount )
863                                 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
864                         ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
865                         break;
866                 }
867                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
868
869                 mdb_tool_index_threads[base] = mdb_index_recrun( mdb_tool_ix_op,
870                         mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
871         }
872
873         return NULL;
874 }
875 #endif