]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/tools.c
Use MDB_APPEND mode where possible
[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 int mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn );
28
29 #define IDBLOCK 1024
30
31 typedef struct mdb_tool_idl_cache_entry {
32         struct mdb_tool_idl_cache_entry *next;
33         ID ids[IDBLOCK];
34 } mdb_tool_idl_cache_entry;
35
36 typedef struct mdb_tool_idl_cache {
37         struct berval kstr;
38         mdb_tool_idl_cache_entry *head, *tail;
39         ID first, last;
40         int count;
41 } mdb_tool_idl_cache;
42
43 static mdb_tool_idl_cache_entry *mdb_tool_idl_free_list;
44 static Avlnode *mdb_tool_roots[MDB_INDICES];
45
46 static MDB_txn *txn = NULL, *txi = NULL;
47 static MDB_cursor *cursor = NULL, *idcursor = NULL;
48 static MDB_val key, data;
49 static ID previd = NOID;
50
51 typedef struct dn_id {
52         ID id;
53         struct berval dn;
54 } dn_id;
55
56 #define HOLE_SIZE       4096
57 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
58 static unsigned nhmax = HOLE_SIZE;
59 static unsigned nholes;
60
61 static struct berval    *tool_base;
62 static int              tool_scope;
63 static Filter           *tool_filter;
64 static Entry            *tool_next_entry;
65
66 #if 0
67 static ID mdb_tool_ix_id;
68 static Operation *mdb_tool_ix_op;
69 static int *mdb_tool_index_threads, mdb_tool_index_tcount;
70 static void *mdb_tool_index_rec;
71 static struct mdb_info *mdb_tool_info;
72 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
73 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
74 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
75 static void * mdb_tool_index_task( void *ctx, void *ptr );
76 #endif
77
78 static int      mdb_writes, mdb_writes_per_commit;
79
80 static int
81 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
82
83 int mdb_tool_entry_open(
84         BackendDB *be, int mode )
85 {
86         /* In Quick mode, commit once per 1000 entries */
87         mdb_writes = 0;
88         if ( slapMode & SLAP_TOOL_QUICK )
89                 mdb_writes_per_commit = 1000;
90         else
91                 mdb_writes_per_commit = 1;
92
93 #if 0
94         /* Set up for threaded slapindex */
95         if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
96                 if ( !mdb_tool_info ) {
97                         ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
98                         ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
99                         ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
100                         if ( mdb->bi_nattrs ) {
101                                 int i;
102                                 mdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
103                                 mdb_tool_index_rec = ch_malloc( mdb->bi_nattrs * sizeof( IndexRec ));
104                                 mdb_tool_index_tcount = slap_tool_thread_max - 1;
105                                 for (i=1; i<slap_tool_thread_max; i++) {
106                                         int *ptr = ch_malloc( sizeof( int ));
107                                         *ptr = i;
108                                         ldap_pvt_thread_pool_submit( &connection_pool,
109                                                 mdb_tool_index_task, ptr );
110                                 }
111                         }
112                         mdb_tool_info = mdb;
113                 }
114         }
115 #endif
116
117         return 0;
118 }
119
120 int mdb_tool_entry_close(
121         BackendDB *be )
122 {
123 #if 0
124         if ( mdb_tool_info ) {
125                 slapd_shutdown = 1;
126                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
127
128                 /* There might still be some threads starting */
129                 while ( mdb_tool_index_tcount ) {
130                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
131                                         &mdb_tool_index_mutex );
132                 }
133
134                 mdb_tool_index_tcount = slap_tool_thread_max - 1;
135                 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
136
137                 /* Make sure all threads are stopped */
138                 while ( mdb_tool_index_tcount ) {
139                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
140                                 &mdb_tool_index_mutex );
141                 }
142                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
143
144                 mdb_tool_info = NULL;
145                 slapd_shutdown = 0;
146                 ch_free( mdb_tool_index_threads );
147                 ch_free( mdb_tool_index_rec );
148                 mdb_tool_index_tcount = slap_tool_thread_max - 1;
149         }
150 #endif
151
152         if( idcursor ) {
153                 mdb_cursor_close( idcursor );
154                 idcursor = NULL;
155         }
156         if( cursor ) {
157                 mdb_cursor_close( cursor );
158                 cursor = NULL;
159         }
160         mdb_tool_idl_flush( be, txn );
161         if( txn ) {
162                 if ( mdb_txn_commit( txn ))
163                         return -1;
164                 txn = NULL;
165         }
166
167         if( nholes ) {
168                 unsigned i;
169                 fprintf( stderr, "Error, entries missing!\n");
170                 for (i=0; i<nholes; i++) {
171                         fprintf(stderr, "  entry %ld: %s\n",
172                                 holes[i].id, holes[i].dn.bv_val);
173                 }
174                 nholes = 0;
175                 return -1;
176         }
177
178         return 0;
179 }
180
181 ID
182 mdb_tool_entry_first_x(
183         BackendDB *be,
184         struct berval *base,
185         int scope,
186         Filter *f )
187 {
188         tool_base = base;
189         tool_scope = scope;
190         tool_filter = f;
191
192         return mdb_tool_entry_next( be );
193 }
194
195 ID mdb_tool_entry_next(
196         BackendDB *be )
197 {
198         int rc;
199         ID id;
200         struct mdb_info *mdb;
201
202         assert( be != NULL );
203         assert( slapMode & SLAP_TOOL_MODE );
204
205         mdb = (struct mdb_info *) be->be_private;
206         assert( mdb != NULL );
207
208         if ( !txn ) {
209                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn );
210                 if ( rc )
211                         return NOID;
212                 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
213                 if ( rc ) {
214                         mdb_txn_abort( txn );
215                         return NOID;
216                 }
217         }
218
219 next:;
220         rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
221
222         if( rc ) {
223                 return NOID;
224         }
225
226         previd = *(ID *)key.mv_data;
227         id = previd;
228
229         if ( tool_filter || tool_base ) {
230                 static Operation op = {0};
231                 static Opheader ohdr = {0};
232
233                 op.o_hdr = &ohdr;
234                 op.o_bd = be;
235                 op.o_tmpmemctx = NULL;
236                 op.o_tmpmfuncs = &ch_mfuncs;
237
238                 if ( tool_next_entry ) {
239                         mdb_entry_release( &op, tool_next_entry, 0 );
240                         tool_next_entry = NULL;
241                 }
242
243                 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
244                 if ( rc == LDAP_NO_SUCH_OBJECT ) {
245                         goto next;
246                 }
247
248                 assert( tool_next_entry != NULL );
249
250                 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
251                 {
252                         mdb_entry_release( &op, tool_next_entry, 0 );
253                         tool_next_entry = NULL;
254                         goto next;
255                 }
256         }
257
258         return id;
259 }
260
261 ID mdb_tool_dn2id_get(
262         Backend *be,
263         struct berval *dn
264 )
265 {
266         struct mdb_info *mdb;
267         Operation op = {0};
268         Opheader ohdr = {0};
269         ID id;
270         int rc;
271
272         if ( BER_BVISEMPTY(dn) )
273                 return 0;
274
275         mdb = (struct mdb_info *) be->be_private;
276
277         if ( !txn ) {
278                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
279                         MDB_RDONLY : 0, &txn );
280                 if ( rc )
281                         return NOID;
282         }
283
284         op.o_hdr = &ohdr;
285         op.o_bd = be;
286         op.o_tmpmemctx = NULL;
287         op.o_tmpmfuncs = &ch_mfuncs;
288
289         rc = mdb_dn2id( &op, txn, dn, &id, NULL, NULL );
290         if ( rc == MDB_NOTFOUND )
291                 return NOID;
292
293         return id;
294 }
295
296 static int
297 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
298 {
299         Operation op = {0};
300         Opheader ohdr = {0};
301
302         Entry *e = NULL;
303         struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
304         int rc;
305
306         assert( be != NULL );
307         assert( slapMode & SLAP_TOOL_MODE );
308
309         if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
310                 *ep = tool_next_entry;
311                 tool_next_entry = NULL;
312                 return LDAP_SUCCESS;
313         }
314
315         if ( id != previd ) {
316                 key.mv_size = sizeof(ID);
317                 key.mv_data = &id;
318                 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
319                 if ( rc ) {
320                         rc = LDAP_OTHER;
321                         goto done;
322                 }
323         }
324
325         op.o_hdr = &ohdr;
326         op.o_bd = be;
327         op.o_tmpmemctx = NULL;
328         op.o_tmpmfuncs = &ch_mfuncs;
329         if ( slapMode & SLAP_TOOL_READONLY ) {
330                 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
331                 if ( rc  ) {
332                         rc = LDAP_OTHER;
333                         mdb_entry_return( &op, e );
334                         e = NULL;
335                         goto done;
336                 }
337                 if ( tool_base != NULL ) {
338                         if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
339                                 ch_free( dn.bv_val );
340                                 ch_free( ndn.bv_val );
341                                 rc = LDAP_NO_SUCH_OBJECT;
342                         }
343                 }
344         }
345         rc = mdb_entry_decode( &op, &data, &e );
346         e->e_id = id;
347         if ( !BER_BVISNULL( &dn )) {
348                 e->e_name = dn;
349                 e->e_nname = ndn;
350         } else {
351                 e->e_name.bv_val = NULL;
352                 e->e_nname.bv_val = NULL;
353         }
354
355 done:
356         if ( e != NULL ) {
357                 *ep = e;
358         }
359
360         return rc;
361 }
362
363 Entry*
364 mdb_tool_entry_get( BackendDB *be, ID id )
365 {
366         Entry *e = NULL;
367
368         (void)mdb_tool_entry_get_int( be, id, &e );
369         return e;
370 }
371
372 static int mdb_tool_next_id(
373         Operation *op,
374         MDB_txn *tid,
375         Entry *e,
376         struct berval *text,
377         int hole )
378 {
379         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
380         struct berval dn = e->e_name;
381         struct berval ndn = e->e_nname;
382         struct berval pdn, npdn, nmatched;
383         ID id, pid = 0;
384         int rc;
385
386         if (ndn.bv_len == 0) {
387                 e->e_id = 0;
388                 return 0;
389         }
390
391         rc = mdb_dn2id( op, tid, &ndn, &id, NULL, &nmatched );
392         if ( rc == MDB_NOTFOUND ) {
393                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
394                         ID eid = e->e_id;
395                         dnParent( &ndn, &npdn );
396                         if ( nmatched.bv_len != npdn.bv_len ) {
397                                 dnParent( &dn, &pdn );
398                                 e->e_name = pdn;
399                                 e->e_nname = npdn;
400                                 rc = mdb_tool_next_id( op, tid, e, text, 1 );
401                                 e->e_name = dn;
402                                 e->e_nname = ndn;
403                                 if ( rc ) {
404                                         return rc;
405                                 }
406                                 /* If parent didn't exist, it was created just now
407                                  * and its ID is now in e->e_id. Make sure the current
408                                  * entry gets added under the new parent ID.
409                                  */
410                                 if ( eid != e->e_id ) {
411                                         pid = e->e_id;
412                                 }
413                         } else {
414                                 pid = id;
415                         }
416                 }
417                 rc = mdb_next_id( op->o_bd, tid, &e->e_id );
418                 if ( rc ) {
419                         snprintf( text->bv_val, text->bv_len,
420                                 "next_id failed: %s (%d)",
421                                 mdb_strerror(rc), rc );
422                 Debug( LDAP_DEBUG_ANY,
423                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
424                         return rc;
425                 }
426                 rc = mdb_dn2id_add( op, tid, pid, e );
427                 if ( rc ) {
428                         snprintf( text->bv_val, text->bv_len,
429                                 "dn2id_add failed: %s (%d)",
430                                 mdb_strerror(rc), rc );
431                         Debug( LDAP_DEBUG_ANY,
432                                 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
433                 } else if ( hole ) {
434                         MDB_val key, data;
435                         if ( nholes == nhmax - 1 ) {
436                                 if ( holes == hbuf ) {
437                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
438                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
439                                 } else {
440                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
441                                 }
442                                 nhmax *= 2;
443                         }
444                         ber_dupbv( &holes[nholes].dn, &ndn );
445                         holes[nholes++].id = e->e_id;
446                         key.mv_size = sizeof(ID);
447                         key.mv_data = &e->e_id;
448                         data.mv_size = 0;
449                         data.mv_data = NULL;
450                         rc = mdb_put( tid, mdb->mi_id2entry, &key, &data, MDB_NOOVERWRITE );
451                         if ( rc == MDB_KEYEXIST )
452                                 rc = 0;
453                         if ( rc ) {
454                                 snprintf( text->bv_val, text->bv_len,
455                                         "dummy id2entry add failed: %s (%d)",
456                                         mdb_strerror(rc), rc );
457                                 Debug( LDAP_DEBUG_ANY,
458                                         "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
459                         }
460                 }
461         } else if ( !hole ) {
462                 unsigned i, j;
463
464                 e->e_id = id;
465
466                 for ( i=0; i<nholes; i++) {
467                         if ( holes[i].id == e->e_id ) {
468                                 free(holes[i].dn.bv_val);
469                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
470                                 holes[j].id = 0;
471                                 nholes--;
472                                 break;
473                         } else if ( holes[i].id > e->e_id ) {
474                                 break;
475                         }
476                 }
477         }
478         return rc;
479 }
480
481 static int
482 mdb_tool_index_add(
483         Operation *op,
484         MDB_txn *txn,
485         Entry *e )
486 {
487         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
488
489         if ( !mdb->mi_nattrs )
490                 return 0;
491
492 #if 0
493         if ( slapMode & SLAP_TOOL_QUICK ) {
494                 IndexRec *ir;
495                 int i, rc;
496                 Attribute *a;
497
498                 ir = mdb_tool_index_rec;
499                 memset(ir, 0, mdb->bi_nattrs * sizeof( IndexRec ));
500
501                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
502                         rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
503                                 &a->a_desc->ad_tags, ir );
504                         if ( rc )
505                                 return rc;
506                 }
507                 mdb_tool_ix_id = e->e_id;
508                 mdb_tool_ix_op = op;
509                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
510                 /* Wait for all threads to be ready */
511                 while ( mdb_tool_index_tcount ) {
512                         ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
513                                 &mdb_tool_index_mutex );
514                 }
515                 for ( i=1; i<slap_tool_thread_max; i++ )
516                         mdb_tool_index_threads[i] = LDAP_BUSY;
517                 mdb_tool_index_tcount = slap_tool_thread_max - 1;
518                 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
519                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
520                 rc = mdb_index_recrun( op, mdb, ir, e->e_id, 0 );
521                 if ( rc )
522                         return rc;
523                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
524                 for ( i=1; i<slap_tool_thread_max; i++ ) {
525                         if ( mdb_tool_index_threads[i] == LDAP_BUSY ) {
526                                 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
527                                         &mdb_tool_index_mutex );
528                                 i--;
529                                 continue;
530                         }
531                         if ( mdb_tool_index_threads[i] ) {
532                                 rc = mdb_tool_index_threads[i];
533                                 break;
534                         }
535                 }
536                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
537                 return rc;
538         } else
539 #endif
540         {
541                 return mdb_index_entry_add( op, txn, e );
542         }
543 }
544
545 ID mdb_tool_entry_put(
546         BackendDB *be,
547         Entry *e,
548         struct berval *text )
549 {
550         int rc;
551         struct mdb_info *mdb;
552         Operation op = {0};
553         Opheader ohdr = {0};
554
555         assert( be != NULL );
556         assert( slapMode & SLAP_TOOL_MODE );
557
558         assert( text != NULL );
559         assert( text->bv_val != NULL );
560         assert( text->bv_val[0] == '\0' );      /* overconservative? */
561
562         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
563                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
564
565         mdb = (struct mdb_info *) be->be_private;
566
567         if ( !txn ) {
568         rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
569         if( rc != 0 ) {
570                 snprintf( text->bv_val, text->bv_len,
571                         "txn_begin failed: %s (%d)",
572                         mdb_strerror(rc), rc );
573                 Debug( LDAP_DEBUG_ANY,
574                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
575                          text->bv_val, 0, 0 );
576                 return NOID;
577         }
578         }
579
580         op.o_hdr = &ohdr;
581         op.o_bd = be;
582         op.o_tmpmemctx = NULL;
583         op.o_tmpmfuncs = &ch_mfuncs;
584
585         /* add dn2id indices */
586         rc = mdb_tool_next_id( &op, txn, e, text, 0 );
587         if( rc != 0 ) {
588                 goto done;
589         }
590
591         rc = mdb_tool_index_add( &op, txn, e );
592         if( rc != 0 ) {
593                 snprintf( text->bv_val, text->bv_len,
594                                 "index_entry_add failed: err=%d", rc );
595                 Debug( LDAP_DEBUG_ANY,
596                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
597                         text->bv_val, 0, 0 );
598                 goto done;
599         }
600
601
602         /* id2entry index */
603         rc = mdb_id2entry_add( &op, txn, e );
604         if( rc != 0 ) {
605                 snprintf( text->bv_val, text->bv_len,
606                                 "id2entry_add failed: err=%d", rc );
607                 Debug( LDAP_DEBUG_ANY,
608                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
609                         text->bv_val, 0, 0 );
610                 goto done;
611         }
612
613 done:
614         if( rc == 0 ) {
615                 mdb_writes++;
616                 if ( mdb_writes >= mdb_writes_per_commit ) {
617                         mdb_tool_idl_flush( be, txn );
618                         rc = mdb_txn_commit( txn );
619                         mdb_writes = 0;
620                         txn = NULL;
621                         if( rc != 0 ) {
622                                 snprintf( text->bv_val, text->bv_len,
623                                                 "txn_commit failed: %s (%d)",
624                                                 mdb_strerror(rc), rc );
625                                 Debug( LDAP_DEBUG_ANY,
626                                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
627                                         text->bv_val, 0, 0 );
628                                 e->e_id = NOID;
629                         }
630                 }
631
632         } else {
633                 mdb_txn_abort( txn );
634                 txn = NULL;
635                 snprintf( text->bv_val, text->bv_len,
636                         "txn_aborted! %s (%d)",
637                         rc == LDAP_OTHER ? "Internal error" :
638                         mdb_strerror(rc), rc );
639                 Debug( LDAP_DEBUG_ANY,
640                         "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
641                         text->bv_val, 0, 0 );
642                 e->e_id = NOID;
643         }
644
645         return e->e_id;
646 }
647
648 int mdb_tool_entry_reindex(
649         BackendDB *be,
650         ID id,
651         AttributeDescription **adv )
652 {
653         struct mdb_info *mi = (struct mdb_info *) be->be_private;
654         int rc;
655         Entry *e;
656         Operation op = {0};
657         Opheader ohdr = {0};
658
659         Debug( LDAP_DEBUG_ARGS,
660                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
661                 (long) id, 0, 0 );
662         assert( tool_base == NULL );
663         assert( tool_filter == NULL );
664
665         /* No indexes configured, nothing to do. Could return an
666          * error here to shortcut things.
667          */
668         if (!mi->mi_attrs) {
669                 return 0;
670         }
671
672         /* Check for explicit list of attrs to index */
673         if ( adv ) {
674                 int i, j, n;
675
676                 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
677                         /* count */
678                         for ( n = 0; adv[n]; n++ ) ;
679
680                         /* insertion sort */
681                         for ( i = 0; i < n; i++ ) {
682                                 AttributeDescription *ad = adv[i];
683                                 for ( j = i-1; j>=0; j--) {
684                                         if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
685                                         adv[j+1] = adv[j];
686                                 }
687                                 adv[j+1] = ad;
688                         }
689                 }
690
691                 for ( i = 0; adv[i]; i++ ) {
692                         if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
693                                 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
694                                         if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
695                                                 AttrInfo *ai = mi->mi_attrs[i];
696                                                 mi->mi_attrs[i] = mi->mi_attrs[j];
697                                                 mi->mi_attrs[j] = ai;
698                                                 break;
699                                         }
700                                 }
701                                 if ( j == mi->mi_nattrs ) {
702                                         Debug( LDAP_DEBUG_ANY,
703                                                 LDAP_XSTRING(mdb_tool_entry_reindex)
704                                                 ": no index configured for %s\n",
705                                                 adv[i]->ad_cname.bv_val, 0, 0 );
706                                         return -1;
707                                 }
708                         }
709                 }
710                 mi->mi_nattrs = i;
711         }
712
713         if ( slapMode & SLAP_TRUNCATE_MODE ) {
714                 int i;
715                 for ( i=0; i < mi->mi_nattrs; i++ ) {
716                         rc = mdb_drop( txn, mi->mi_attrs[i]->ai_dbi, 0 );
717                         if ( rc ) {
718                                 Debug( LDAP_DEBUG_ANY,
719                                         LDAP_XSTRING(mdb_tool_entry_reindex)
720                                         ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
721                                         mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
722                                         mdb_strerror(rc), rc );
723                                 return -1;
724                         }
725                 }
726                 slapMode ^= SLAP_TRUNCATE_MODE;
727         }
728
729         e = mdb_tool_entry_get( be, id );
730
731         if( e == NULL ) {
732                 Debug( LDAP_DEBUG_ANY,
733                         LDAP_XSTRING(mdb_tool_entry_reindex)
734                         ": could not locate id=%ld\n",
735                         (long) id, 0, 0 );
736                 return -1;
737         }
738
739         if ( !txi ) {
740                 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
741                 if( rc != 0 ) {
742                         Debug( LDAP_DEBUG_ANY,
743                                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
744                                 "txn_begin failed: %s (%d)\n",
745                                 mdb_strerror(rc), rc, 0 );
746                         goto done;
747                 }
748         }
749
750         /*
751          * just (re)add them for now
752          * assume that some other routine (not yet implemented)
753          * will zap index databases
754          *
755          */
756
757         Debug( LDAP_DEBUG_TRACE,
758                 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
759                 (long) id, e->e_dn, 0 );
760
761         op.o_hdr = &ohdr;
762         op.o_bd = be;
763         op.o_tmpmemctx = NULL;
764         op.o_tmpmfuncs = &ch_mfuncs;
765
766         rc = mdb_tool_index_add( &op, txi, e );
767
768 done:
769         if( rc == 0 ) {
770                 mdb_writes++;
771                 if ( mdb_writes >= mdb_writes_per_commit ) {
772                         mdb_tool_idl_flush( be, txi );
773                         rc = mdb_txn_commit( txi );
774                         if( rc != 0 ) {
775                                 Debug( LDAP_DEBUG_ANY,
776                                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
777                                         ": txn_commit failed: %s (%d)\n",
778                                         mdb_strerror(rc), rc, 0 );
779                                 e->e_id = NOID;
780                         }
781                         txi = NULL;
782                 }
783
784         } else {
785                 mdb_txn_abort( txi );
786                 Debug( LDAP_DEBUG_ANY,
787                         "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
788                         ": txn_aborted! err=%d\n",
789                         rc, 0, 0 );
790                 e->e_id = NOID;
791                 txi = NULL;
792         }
793         mdb_entry_release( &op, e, 0 );
794
795         return rc;
796 }
797
798 ID mdb_tool_entry_modify(
799         BackendDB *be,
800         Entry *e,
801         struct berval *text )
802 {
803         int rc;
804         struct mdb_info *mdb;
805         MDB_txn *tid;
806         Operation op = {0};
807         Opheader ohdr = {0};
808
809         assert( be != NULL );
810         assert( slapMode & SLAP_TOOL_MODE );
811
812         assert( text != NULL );
813         assert( text->bv_val != NULL );
814         assert( text->bv_val[0] == '\0' );      /* overconservative? */
815
816         assert ( e->e_id != NOID );
817
818         Debug( LDAP_DEBUG_TRACE,
819                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
820                 (long) e->e_id, e->e_dn, 0 );
821
822         mdb = (struct mdb_info *) be->be_private;
823
824         if( cursor ) {
825                 mdb_cursor_close( cursor );
826                 cursor = NULL;
827         }
828         rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &tid );
829         if( rc != 0 ) {
830                 snprintf( text->bv_val, text->bv_len,
831                         "txn_begin failed: %s (%d)",
832                         mdb_strerror(rc), rc );
833                 Debug( LDAP_DEBUG_ANY,
834                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
835                          text->bv_val, 0, 0 );
836                 return NOID;
837         }
838
839         op.o_hdr = &ohdr;
840         op.o_bd = be;
841         op.o_tmpmemctx = NULL;
842         op.o_tmpmfuncs = &ch_mfuncs;
843
844         /* id2entry index */
845         rc = mdb_id2entry_update( &op, tid, e );
846         if( rc != 0 ) {
847                 snprintf( text->bv_val, text->bv_len,
848                                 "id2entry_update failed: err=%d", rc );
849                 Debug( LDAP_DEBUG_ANY,
850                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
851                         text->bv_val, 0, 0 );
852                 goto done;
853         }
854
855 done:
856         if( rc == 0 ) {
857                 rc = mdb_txn_commit( tid );
858                 if( rc != 0 ) {
859                         snprintf( text->bv_val, text->bv_len,
860                                         "txn_commit failed: %s (%d)",
861                                         mdb_strerror(rc), rc );
862                         Debug( LDAP_DEBUG_ANY,
863                                 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
864                                 "%s\n", text->bv_val, 0, 0 );
865                         e->e_id = NOID;
866                 }
867
868         } else {
869                 mdb_txn_abort( tid );
870                 snprintf( text->bv_val, text->bv_len,
871                         "txn_aborted! %s (%d)",
872                         mdb_strerror(rc), rc );
873                 Debug( LDAP_DEBUG_ANY,
874                         "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
875                         text->bv_val, 0, 0 );
876                 e->e_id = NOID;
877         }
878
879         return e->e_id;
880 }
881
882 #if 0
883 static void *
884 mdb_tool_index_task( void *ctx, void *ptr )
885 {
886         int base = *(int *)ptr;
887
888         free( ptr );
889         while ( 1 ) {
890                 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
891                 mdb_tool_index_tcount--;
892                 if ( !mdb_tool_index_tcount )
893                         ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
894                 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
895                         &mdb_tool_index_mutex );
896                 if ( slapd_shutdown ) {
897                         mdb_tool_index_tcount--;
898                         if ( !mdb_tool_index_tcount )
899                                 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
900                         ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
901                         break;
902                 }
903                 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
904
905                 mdb_tool_index_threads[base] = mdb_index_recrun( mdb_tool_ix_op,
906                         mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
907         }
908
909         return NULL;
910 }
911 #endif
912
913 static int
914 mdb_tool_idl_cmp( const void *v1, const void *v2 )
915 {
916         const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
917         int rc;
918
919         if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
920         return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
921 }
922
923 static int
924 mdb_tool_idl_flush_one( MDB_cursor *mc, mdb_tool_idl_cache *ic )
925 {
926         mdb_tool_idl_cache_entry *ice;
927         MDB_val key, data;
928         int i, rc;
929         ID id, nid;
930
931         /* Freshly allocated, ignore it */
932         if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
933                 return 0;
934         }
935
936         key.mv_data = ic->kstr.bv_val;
937         key.mv_size = ic->kstr.bv_len;
938
939         rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
940         /* If key already exists and we're writing a range... */
941         if ( rc == 0 && ic->count > MDB_IDL_DB_SIZE ) {
942                 nid = *(ID *)data.mv_data;
943                 /* If it's not currently a range, must delete old info */
944                 if ( nid ) {
945                         ic->first = nid;
946                         mdb_cursor_del( mc, MDB_NODUPDATA );
947
948                         goto setrange;
949                 } else {
950                         /* Skip lo */
951                         rc = mdb_cursor_get( mc, &key, &data, MDB_NEXT_DUP );
952
953                         /* Get hi */
954                         rc = mdb_cursor_get( mc, &key, &data, MDB_NEXT_DUP );
955
956                         /* Store range hi */
957                         data.mv_data = &ic->last;
958                         rc = mdb_cursor_put( mc, &key, &data, MDB_CURRENT );
959                 }
960                 rc = 0;
961         } else if ( rc && rc != MDB_NOTFOUND ) {
962                 rc = -1;
963         } else if ( ic->count > MDB_IDL_DB_SIZE ) {
964                 /* range, didn't exist before */
965 setrange:
966                 nid = 0;
967                 data.mv_size = sizeof(ID);
968                 data.mv_data = &nid;
969                 rc = mdb_cursor_put( mc, &key, &data, 0 );
970                 if ( rc == 0 ) {
971                         data.mv_data = &ic->first;
972                         rc = mdb_cursor_put( mc, &key, &data, 0 );
973                         if ( rc == 0 ) {
974                                 data.mv_data = &ic->last;
975                                 rc = mdb_cursor_put( mc, &key, &data, 0 );
976                         }
977                 }
978                 if ( rc ) {
979                         rc = -1;
980                 }
981         } else {
982                 int n;
983
984                 data.mv_size = sizeof(ID);
985                 /* Just a normal write */
986                 rc = 0;
987                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
988                         int end;
989                         if ( ice->next ) {
990                                 end = IDBLOCK;
991                         } else {
992                                 end = ic->count & (IDBLOCK-1);
993                                 if ( !end )
994                                         end = IDBLOCK;
995                         }
996                         for ( i=0; i<end; i++ ) {
997                                 if ( !ice->ids[i] ) continue;
998                                 data.mv_data = &ice->ids[i];
999                                 rc = mdb_cursor_put( mc, &key, &data, MDB_NODUPDATA|MDB_APPEND );
1000                                 if ( rc ) {
1001                                         if ( rc == MDB_KEYEXIST ) {
1002                                                 rc = 0;
1003                                                 continue;
1004                                         }
1005                                         rc = -1;
1006                                         break;
1007                                 }
1008                         }
1009                         if ( rc ) {
1010                                 rc = -1;
1011                                 break;
1012                         }
1013                 }
1014                 if ( ic->head ) {
1015                         ic->tail->next = mdb_tool_idl_free_list;
1016                         mdb_tool_idl_free_list = ic->head;
1017                 }
1018         }
1019         ch_free( ic );
1020         return rc;
1021 }
1022
1023 static int
1024 mdb_tool_idl_flush_db( MDB_txn *txn, MDB_dbi dbi, Avlnode *root )
1025 {
1026         MDB_cursor *mc;
1027         int rc;
1028
1029         mdb_cursor_open( txn, dbi, &mc );
1030         root = tavl_end( root, TAVL_DIR_LEFT );
1031         do {
1032                 rc = mdb_tool_idl_flush_one( mc, root->avl_data );
1033                 if ( rc != -1 )
1034                         rc = 0;
1035         } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1036         mdb_cursor_close( mc );
1037
1038         return rc;
1039 }
1040
1041 static int
1042 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1043 {
1044         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1045         int rc = 0;
1046         unsigned int i;
1047
1048         for ( i=MDB_NDB; i < mdb->mi_nattrs+MDB_NDB; i++ ) {
1049                 if ( !mdb_tool_roots[i] ) continue;
1050                 rc = mdb_tool_idl_flush_db( txn, i, mdb_tool_roots[i] );
1051                 tavl_free(mdb_tool_roots[i], NULL);
1052                 mdb_tool_roots[i] = NULL;
1053                 if ( rc )
1054                         break;
1055         }
1056         return rc;
1057 }
1058
1059 int mdb_tool_idl_add(
1060         MDB_cursor *mc,
1061         struct berval *keys,
1062         ID id )
1063 {
1064         MDB_dbi dbi;
1065         mdb_tool_idl_cache *ic, itmp;
1066         mdb_tool_idl_cache_entry *ice;
1067         int i, rc;
1068
1069         dbi = mdb_cursor_dbi(mc);
1070         for (i=0; keys[i].bv_val; i++) {
1071         itmp.kstr = keys[i];
1072         ic = tavl_find( (Avlnode *)mdb_tool_roots[dbi], &itmp, mdb_tool_idl_cmp );
1073
1074         /* No entry yet, create one */
1075         if ( !ic ) {
1076                 MDB_val key, data;
1077                 ID nid;
1078                 int rc;
1079
1080                 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len );
1081                 ic->kstr.bv_len = itmp.kstr.bv_len;
1082                 ic->kstr.bv_val = (char *)(ic+1);
1083                 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1084                 ic->head = ic->tail = NULL;
1085                 ic->last = 0;
1086                 ic->count = 0;
1087                 tavl_insert( (Avlnode **)&mdb_tool_roots[dbi], ic, mdb_tool_idl_cmp,
1088                         avl_dup_error );
1089
1090                 /* load existing key count here */
1091                 key.mv_size = keys[i].bv_len;
1092                 key.mv_data = keys[i].bv_val;
1093                 data.mv_size = sizeof( ID );
1094                 data.mv_data = &nid;
1095                 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1096                 if ( rc == 0 ) {
1097                         if ( nid == 0 ) {
1098                                 ic->count = MDB_IDL_DB_SIZE+1;
1099                         } else {
1100                                 size_t count;
1101
1102                                 mdb_cursor_count( mc, &count );
1103                                 ic->count = count;
1104                                 ic->first = nid;
1105                         }
1106                 }
1107         }
1108         /* are we a range already? */
1109         if ( ic->count > MDB_IDL_DB_SIZE ) {
1110                 ic->last = id;
1111                 continue;
1112         /* Are we at the limit, and converting to a range? */
1113         } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1114                 int n;
1115                 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1116                         /* counting */ ;
1117                 if ( n ) {
1118                         ic->tail->next = mdb_tool_idl_free_list;
1119                         mdb_tool_idl_free_list = ic->head;
1120                 }
1121                 ic->head = ic->tail = NULL;
1122                 ic->last = id;
1123                 ic->count++;
1124                 continue;
1125         }
1126         /* No free block, create that too */
1127         if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1128                 ice = NULL;
1129                 if ( mdb_tool_idl_free_list ) {
1130                         ice = mdb_tool_idl_free_list;
1131                         mdb_tool_idl_free_list = ice->next;
1132                 }
1133                 if ( !ice ) {
1134                         ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1135                 }
1136                 memset( ice, 0, sizeof( *ice ));
1137                 if ( !ic->head ) {
1138                         ic->head = ice;
1139                 } else {
1140                         ic->tail->next = ice;
1141                 }
1142                 ic->tail = ice;
1143                 if ( !ic->count )
1144                         ic->first = id;
1145         }
1146         ice = ic->tail;
1147         ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1148         ic->count++;
1149         }
1150
1151         return 0;
1152 }