]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/tools.c
285ad2ed7cd7156a8c1595ef36b408c1e1fc4466
[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, 1, &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, &txn );
260                 if ( rc )
261                         return NOID;
262         }
263
264         op.o_hdr = &ohdr;
265         op.o_bd = be;
266         op.o_tmpmemctx = NULL;
267         op.o_tmpmfuncs = &ch_mfuncs;
268
269         rc = mdb_dn2id( &op, txn, dn, &id, NULL, NULL );
270         if ( rc == MDB_NOTFOUND )
271                 return NOID;
272         
273         return id;
274 }
275
276 static int
277 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
278 {
279         Entry *e = NULL;
280         struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
281         int rc;
282
283         assert( be != NULL );
284         assert( slapMode & SLAP_TOOL_MODE );
285
286         if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
287                 *ep = tool_next_entry;
288                 tool_next_entry = NULL;
289                 return LDAP_SUCCESS;
290         }
291
292         if ( id != previd ) {
293                 key.mv_size = sizeof(ID);
294                 key.mv_data = &id;
295                 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
296                 if ( rc ) {
297                         rc = LDAP_OTHER;
298                         goto done;
299                 }
300         }
301
302         if ( slapMode & SLAP_TOOL_READONLY ) {
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 = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
312                 if ( rc  ) {
313                         rc = LDAP_OTHER;
314                         mdb_entry_return( e );
315                         e = NULL;
316                         goto done;
317                 }
318                 if ( tool_base != NULL ) {
319                         if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
320                                 ch_free( dn.bv_val );
321                                 ch_free( ndn.bv_val );
322                                 rc = LDAP_NO_SUCH_OBJECT;
323                         }
324                 }
325         }
326         /* Get the header */
327         eh.bv.bv_val = data.mv_data;
328         eh.bv.bv_len = data.mv_size;
329
330         rc = entry_header( &eh );
331         if ( rc ) {
332                 rc = LDAP_OTHER;
333                 goto done;
334         }
335         eh.bv.bv_len = eh.nvals * sizeof( struct berval );
336         eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
337         rc = entry_decode( &eh, &e );
338         e->e_id = id;
339         if ( !BER_BVISNULL( &dn )) {
340                 e->e_name = dn;
341                 e->e_nname = ndn;
342         } else {
343                 e->e_name.bv_val = NULL;
344                 e->e_nname.bv_val = NULL;
345         }
346         e->e_bv = eh.bv;
347
348 done:
349         if ( e != NULL ) {
350                 *ep = e;
351         }
352
353         return rc;
354 }
355
356 Entry*
357 mdb_tool_entry_get( BackendDB *be, ID id )
358 {
359         Entry *e = NULL;
360
361         (void)mdb_tool_entry_get_int( be, id, &e );
362         return e;
363 }
364
365 static int mdb_tool_next_id(
366         Operation *op,
367         MDB_txn *tid,
368         Entry *e,
369         struct berval *text,
370         int hole )
371 {
372         struct berval dn = e->e_name;
373         struct berval ndn = e->e_nname;
374         struct berval pdn, npdn, nmatched;
375         ID id, pid = 0;
376         int rc;
377
378         if (ndn.bv_len == 0) {
379                 e->e_id = 0;
380                 return 0;
381         }
382
383         rc = mdb_dn2id( op, tid, &ndn, &id, NULL, &nmatched );
384         if ( rc == MDB_NOTFOUND ) {
385                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
386                         ID eid = e->e_id;
387                         dnParent( &ndn, &npdn );
388                         if ( nmatched.bv_len != npdn.bv_len ) {
389                                 dnParent( &dn, &pdn );
390                                 e->e_name = pdn;
391                                 e->e_nname = npdn;
392                                 rc = mdb_tool_next_id( op, tid, e, text, 1 );
393                                 e->e_name = dn;
394                                 e->e_nname = ndn;
395                                 if ( rc ) {
396                                         return rc;
397                                 }
398                                 /* If parent didn't exist, it was created just now
399                                  * and its ID is now in e->e_id. Make sure the current
400                                  * entry gets added under the new parent ID.
401                                  */
402                                 if ( eid != e->e_id ) {
403                                         pid = e->e_id;
404                                 }
405                         } else {
406                                 pid = id;
407                         }
408                 }
409                 rc = mdb_next_id( op->o_bd, tid, &e->e_id );
410                 if ( rc ) {
411                         snprintf( text->bv_val, text->bv_len,
412                                 "next_id failed: %s (%d)",
413                                 mdb_strerror(rc), rc );
414                 Debug( LDAP_DEBUG_ANY,
415                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
416                         return rc;
417                 }
418                 rc = mdb_dn2id_add( op, tid, pid, e );
419                 if ( rc ) {
420                         snprintf( text->bv_val, text->bv_len, 
421                                 "dn2id_add failed: %s (%d)",
422                                 mdb_strerror(rc), rc );
423                 Debug( LDAP_DEBUG_ANY,
424                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
425                 } else if ( hole ) {
426                         if ( nholes == nhmax - 1 ) {
427                                 if ( holes == hbuf ) {
428                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
429                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
430                                 } else {
431                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
432                                 }
433                                 nhmax *= 2;
434                         }
435                         ber_dupbv( &holes[nholes].dn, &ndn );
436                         holes[nholes++].id = e->e_id;
437                 }
438         } else if ( !hole ) {
439                 unsigned i, j;
440
441                 e->e_id = id;
442
443                 for ( i=0; i<nholes; i++) {
444                         if ( holes[i].id == e->e_id ) {
445                                 free(holes[i].dn.bv_val);
446                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
447                                 holes[j].id = 0;
448                                 nholes--;
449                                 break;
450                         } else if ( holes[i].id > e->e_id ) {
451                                 break;
452                         }
453                 }
454         }
455         return rc;
456 }
457
458 static int
459 mdb_tool_index_add(
460         Operation *op,
461         MDB_txn *txn,
462         Entry *e )
463 {
464         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
465
466         if ( !mdb->mi_nattrs )
467                 return 0;
468
469 #if 0
470         if ( slapMode & SLAP_TOOL_QUICK ) {
471                 IndexRec *ir;
472                 int i, rc;
473                 Attribute *a;
474                 
475                 ir = mdb_tool_index_rec;
476                 memset(ir, 0, mdb->bi_nattrs * sizeof( IndexRec ));
477
478                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
479                         rc = mdb_index_recset( mdb, a, a->a_desc->ad_type, 
480                                 &a->a_desc->ad_tags, ir );
481                         if ( rc )
482                                 return rc;
483                 }
484                 mdb_tool_ix_id = e->e_id;
485                 mdb_tool_ix_op = op;
486                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
487                 /* Wait for all threads to be ready */
488                 while ( mdb_tool_index_tcount ) {
489                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main, 
490                                 &mdb_tool_index_mutex );
491                 }
492                 for ( i=1; i<slap_tool_thread_max; i++ )
493                         mdb_tool_index_threads[i] = LDAP_BUSY;
494                 mdb_tool_index_tcount = slap_tool_thread_max - 1;
495                 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
496                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
497                 rc = mdb_index_recrun( op, mdb, ir, e->e_id, 0 );
498                 if ( rc )
499                         return rc;
500                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
501                 for ( i=1; i<slap_tool_thread_max; i++ ) {
502                         if ( mdb_tool_index_threads[i] == LDAP_BUSY ) {
503                                 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main, 
504                                         &mdb_tool_index_mutex );
505                                 i--;
506                                 continue;
507                         }
508                         if ( mdb_tool_index_threads[i] ) {
509                                 rc = mdb_tool_index_threads[i];
510                                 break;
511                         }
512                 }
513                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
514                 return rc;
515         } else
516 #endif
517         {
518                 return mdb_index_entry_add( op, txn, e );
519         }
520 }
521
522 ID mdb_tool_entry_put(
523         BackendDB *be,
524         Entry *e,
525         struct berval *text )
526 {
527         int rc;
528         struct mdb_info *mdb;
529         Operation op = {0};
530         Opheader ohdr = {0};
531
532         assert( be != NULL );
533         assert( slapMode & SLAP_TOOL_MODE );
534
535         assert( text != NULL );
536         assert( text->bv_val != NULL );
537         assert( text->bv_val[0] == '\0' );      /* overconservative? */
538
539         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
540                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
541
542         mdb = (struct mdb_info *) be->be_private;
543
544         if ( !txn ) {
545         rc = mdb_txn_begin( mdb->mi_dbenv, 0, &txn );
546         if( rc != 0 ) {
547                 snprintf( text->bv_val, text->bv_len,
548                         "txn_begin failed: %s (%d)",
549                         mdb_strerror(rc), rc );
550                 Debug( LDAP_DEBUG_ANY,
551                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
552                          text->bv_val, 0, 0 );
553                 return NOID;
554         }
555         }
556
557         op.o_hdr = &ohdr;
558         op.o_bd = be;
559         op.o_tmpmemctx = NULL;
560         op.o_tmpmfuncs = &ch_mfuncs;
561
562         /* add dn2id indices */
563         rc = mdb_tool_next_id( &op, txn, e, text, 0 );
564         if( rc != 0 ) {
565                 goto done;
566         }
567
568         rc = mdb_tool_index_add( &op, txn, e );
569         if( rc != 0 ) {
570                 snprintf( text->bv_val, text->bv_len,
571                                 "index_entry_add failed: %s (%d)",
572                                 rc == LDAP_OTHER ? "Internal error" :
573                                 mdb_strerror(rc), rc );
574                 Debug( LDAP_DEBUG_ANY,
575                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
576                         text->bv_val, 0, 0 );
577                 goto done;
578         }
579
580
581         /* id2entry index */
582         rc = mdb_id2entry_add( &op, txn, e );
583         if( rc != 0 ) {
584                 snprintf( text->bv_val, text->bv_len,
585                                 "id2entry_add failed: %s (%d)",
586                                 mdb_strerror(rc), rc );
587                 Debug( LDAP_DEBUG_ANY,
588                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
589                         text->bv_val, 0, 0 );
590                 goto done;
591         }
592
593 done:
594         if( rc == 0 ) {
595                 mdb_writes++;
596                 if ( mdb_writes >= mdb_writes_per_commit ) {
597                         rc = mdb_txn_commit( txn );
598                         mdb_writes = 0;
599                         txn = NULL;
600                         if( rc != 0 ) {
601                                 snprintf( text->bv_val, text->bv_len,
602                                                 "txn_commit failed: %s (%d)",
603                                                 mdb_strerror(rc), rc );
604                                 Debug( LDAP_DEBUG_ANY,
605                                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
606                                         text->bv_val, 0, 0 );
607                                 e->e_id = NOID;
608                         }
609                 }
610
611         } else {
612                 mdb_txn_abort( txn );
613                 txn = NULL;
614                 snprintf( text->bv_val, text->bv_len,
615                         "txn_aborted! %s (%d)",
616                         rc == LDAP_OTHER ? "Internal error" :
617                         mdb_strerror(rc), rc );
618                 Debug( LDAP_DEBUG_ANY,
619                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
620                         text->bv_val, 0, 0 );
621                 e->e_id = NOID;
622         }
623
624         return e->e_id;
625 }
626
627 int mdb_tool_entry_reindex(
628         BackendDB *be,
629         ID id,
630         AttributeDescription **adv )
631 {
632         struct mdb_info *mi = (struct mdb_info *) be->be_private;
633         int rc;
634         Entry *e;
635         Operation op = {0};
636         Opheader ohdr = {0};
637
638         Debug( LDAP_DEBUG_ARGS,
639                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
640                 (long) id, 0, 0 );
641         assert( tool_base == NULL );
642         assert( tool_filter == NULL );
643
644         /* No indexes configured, nothing to do. Could return an
645          * error here to shortcut things.
646          */
647         if (!mi->mi_attrs) {
648                 return 0;
649         }
650
651         /* Check for explicit list of attrs to index */
652         if ( adv ) {
653                 int i, j, n;
654
655                 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
656                         /* count */
657                         for ( n = 0; adv[n]; n++ ) ;
658
659                         /* insertion sort */
660                         for ( i = 0; i < n; i++ ) {
661                                 AttributeDescription *ad = adv[i];
662                                 for ( j = i-1; j>=0; j--) {
663                                         if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
664                                         adv[j+1] = adv[j];
665                                 }
666                                 adv[j+1] = ad;
667                         }
668                 }
669
670                 for ( i = 0; adv[i]; i++ ) {
671                         if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
672                                 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
673                                         if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
674                                                 AttrInfo *ai = mi->mi_attrs[i];
675                                                 mi->mi_attrs[i] = mi->mi_attrs[j];
676                                                 mi->mi_attrs[j] = ai;
677                                                 break;
678                                         }
679                                 }
680                                 if ( j == mi->mi_nattrs ) {
681                                         Debug( LDAP_DEBUG_ANY,
682                                                 LDAP_XSTRING(mdb_tool_entry_reindex)
683                                                 ": no index configured for %s\n",
684                                                 adv[i]->ad_cname.bv_val, 0, 0 );
685                                         return -1;
686                                 }
687                         }
688                 }
689                 mi->mi_nattrs = i;
690         }
691
692         e = mdb_tool_entry_get( be, id );
693
694         if( e == NULL ) {
695                 Debug( LDAP_DEBUG_ANY,
696                         LDAP_XSTRING(mdb_tool_entry_reindex)
697                         ": could not locate id=%ld\n",
698                         (long) id, 0, 0 );
699                 return -1;
700         }
701
702         if ( !txi ) {
703                 rc = mdb_txn_begin( mi->mi_dbenv, 0, &txi );
704                 if( rc != 0 ) {
705                         Debug( LDAP_DEBUG_ANY,
706                                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
707                                 "txn_begin failed: %s (%d)\n",
708                                 mdb_strerror(rc), rc, 0 );
709                         goto done;
710                 }
711         }
712         
713         /*
714          * just (re)add them for now
715          * assume that some other routine (not yet implemented)
716          * will zap index databases
717          *
718          */
719
720         Debug( LDAP_DEBUG_TRACE,
721                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
722                 (long) id, e->e_dn, 0 );
723
724         op.o_hdr = &ohdr;
725         op.o_bd = be;
726         op.o_tmpmemctx = NULL;
727         op.o_tmpmfuncs = &ch_mfuncs;
728
729         rc = mdb_tool_index_add( &op, txi, e );
730
731 done:
732         if( rc == 0 ) {
733                 mdb_writes++;
734                 if ( mdb_writes >= mdb_writes_per_commit ) {
735                         rc = mdb_txn_commit( txi );
736                         if( rc != 0 ) {
737                                 Debug( LDAP_DEBUG_ANY,
738                                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
739                                         ": txn_commit failed: %s (%d)\n",
740                                         mdb_strerror(rc), rc, 0 );
741                                 e->e_id = NOID;
742                         }
743                         txi = NULL;
744                 }
745
746         } else {
747                 mdb_txn_abort( txi );
748                 Debug( LDAP_DEBUG_ANY,
749                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
750                         ": txn_aborted! %s (%d)\n",
751                         mdb_strerror(rc), rc, 0 );
752                 e->e_id = NOID;
753                 txi = NULL;
754         }
755         mdb_entry_release( &op, e, 0 );
756
757         return rc;
758 }
759
760 ID mdb_tool_entry_modify(
761         BackendDB *be,
762         Entry *e,
763         struct berval *text )
764 {
765         int rc;
766         struct mdb_info *mdb;
767         MDB_txn *tid;
768         Operation op = {0};
769         Opheader ohdr = {0};
770
771         assert( be != NULL );
772         assert( slapMode & SLAP_TOOL_MODE );
773
774         assert( text != NULL );
775         assert( text->bv_val != NULL );
776         assert( text->bv_val[0] == '\0' );      /* overconservative? */
777
778         assert ( e->e_id != NOID );
779
780         Debug( LDAP_DEBUG_TRACE,
781                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
782                 (long) e->e_id, e->e_dn, 0 );
783
784         mdb = (struct mdb_info *) be->be_private;
785
786         if( cursor ) {
787                 mdb_cursor_close( cursor );
788                 cursor = NULL;
789         }
790         rc = mdb_txn_begin( mdb->mi_dbenv, 0, &tid );
791         if( rc != 0 ) {
792                 snprintf( text->bv_val, text->bv_len,
793                         "txn_begin failed: %s (%d)",
794                         mdb_strerror(rc), rc );
795                 Debug( LDAP_DEBUG_ANY,
796                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
797                          text->bv_val, 0, 0 );
798                 return NOID;
799         }
800
801         op.o_hdr = &ohdr;
802         op.o_bd = be;
803         op.o_tmpmemctx = NULL;
804         op.o_tmpmfuncs = &ch_mfuncs;
805
806         /* id2entry index */
807         rc = mdb_id2entry_update( &op, tid, e );
808         if( rc != 0 ) {
809                 snprintf( text->bv_val, text->bv_len,
810                                 "id2entry_add failed: %s (%d)",
811                                 mdb_strerror(rc), rc );
812                 Debug( LDAP_DEBUG_ANY,
813                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
814                         text->bv_val, 0, 0 );
815                 goto done;
816         }
817
818 done:
819         if( rc == 0 ) {
820                 rc = mdb_txn_commit( tid );
821                 if( rc != 0 ) {
822                         snprintf( text->bv_val, text->bv_len,
823                                         "txn_commit failed: %s (%d)",
824                                         mdb_strerror(rc), rc );
825                         Debug( LDAP_DEBUG_ANY,
826                                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
827                                 "%s\n", text->bv_val, 0, 0 );
828                         e->e_id = NOID;
829                 }
830
831         } else {
832                 mdb_txn_abort( tid );
833                 snprintf( text->bv_val, text->bv_len,
834                         "txn_aborted! %s (%d)",
835                         mdb_strerror(rc), rc );
836                 Debug( LDAP_DEBUG_ANY,
837                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
838                         text->bv_val, 0, 0 );
839                 e->e_id = NOID;
840         }
841
842         return e->e_id;
843 }
844
845 #if 0
846 static void *
847 mdb_tool_index_task( void *ctx, void *ptr )
848 {
849         int base = *(int *)ptr;
850
851         free( ptr );
852         while ( 1 ) {
853                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
854                 mdb_tool_index_tcount--;
855                 if ( !mdb_tool_index_tcount )
856                         ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
857                 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
858                         &mdb_tool_index_mutex );
859                 if ( slapd_shutdown ) {
860                         mdb_tool_index_tcount--;
861                         if ( !mdb_tool_index_tcount )
862                                 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
863                         ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
864                         break;
865                 }
866                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
867
868                 mdb_tool_index_threads[base] = mdb_index_recrun( mdb_tool_ix_op,
869                         mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
870         }
871
872         return NULL;
873 }
874 #endif