1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2011-2012 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
20 #include <ac/string.h>
27 #ifdef MDB_TOOL_IDL_CACHING
28 static int mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn );
32 typedef struct mdb_tool_idl_cache_entry {
33 struct mdb_tool_idl_cache_entry *next;
35 } mdb_tool_idl_cache_entry;
37 typedef struct mdb_tool_idl_cache {
39 mdb_tool_idl_cache_entry *head, *tail;
45 #define WAS_FOUND 0x01
46 #define WAS_RANGE 0x02
48 #define MDB_TOOL_IDL_FLUSH(be, txn) mdb_tool_idl_flush(be, txn)
50 #define MDB_TOOL_IDL_FLUSH(be, txn)
51 #endif /* MDB_TOOL_IDL_CACHING */
53 static MDB_txn *txn = NULL, *txi = NULL;
54 static MDB_cursor *cursor = NULL, *idcursor = NULL;
55 static MDB_cursor *mcp = NULL, *mcd = NULL;
56 static MDB_val key, data;
57 static ID previd = NOID;
59 typedef struct dn_id {
64 #define HOLE_SIZE 4096
65 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
66 static unsigned nhmax = HOLE_SIZE;
67 static unsigned nholes;
69 static struct berval *tool_base;
70 static int tool_scope;
71 static Filter *tool_filter;
72 static Entry *tool_next_entry;
74 static ID mdb_tool_ix_id;
75 static Operation *mdb_tool_ix_op;
76 static MDB_txn *mdb_tool_ix_txn;
77 static int mdb_tool_index_tcount, mdb_tool_threads;
78 static IndexRec *mdb_tool_index_rec;
79 static struct mdb_info *mdb_tool_info;
80 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
81 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
82 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
83 static void * mdb_tool_index_task( void *ctx, void *ptr );
85 static int mdb_writes, mdb_writes_per_commit;
88 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
90 int mdb_tool_entry_open(
91 BackendDB *be, int mode )
93 /* In Quick mode, commit once per 1000 entries */
95 if ( slapMode & SLAP_TOOL_QUICK )
96 mdb_writes_per_commit = 1000;
98 mdb_writes_per_commit = 1;
100 /* Set up for threaded slapindex */
101 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
102 if ( !mdb_tool_info ) {
103 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
104 ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
105 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
106 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
107 if ( mdb->mi_nattrs ) {
109 mdb_tool_threads = slap_tool_thread_max - 1;
110 if ( mdb_tool_threads > 1 ) {
111 mdb_tool_index_rec = ch_calloc( mdb->mi_nattrs, sizeof( IndexRec ));
112 mdb_tool_index_tcount = mdb_tool_threads - 1;
113 for (i=1; i<mdb_tool_threads; i++) {
114 int *ptr = ch_malloc( sizeof( int ));
116 ldap_pvt_thread_pool_submit( &connection_pool,
117 mdb_tool_index_task, ptr );
128 int mdb_tool_entry_close(
131 if ( mdb_tool_info ) {
133 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
135 /* There might still be some threads starting */
136 while ( mdb_tool_index_tcount > 0 ) {
137 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
138 &mdb_tool_index_mutex );
141 mdb_tool_index_tcount = mdb_tool_threads - 1;
142 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
144 /* Make sure all threads are stopped */
145 while ( mdb_tool_index_tcount > 0 ) {
146 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
147 &mdb_tool_index_mutex );
149 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
151 mdb_tool_info = NULL;
153 ch_free( mdb_tool_index_rec );
154 mdb_tool_index_tcount = mdb_tool_threads - 1;
158 mdb_cursor_close( idcursor );
162 mdb_cursor_close( cursor );
166 MDB_TOOL_IDL_FLUSH( be, txn );
167 if ( mdb_txn_commit( txn ))
174 fprintf( stderr, "Error, entries missing!\n");
175 for (i=0; i<nholes; i++) {
176 fprintf(stderr, " entry %ld: %s\n",
177 holes[i].id, holes[i].dn.bv_val);
187 mdb_tool_entry_first_x(
197 return mdb_tool_entry_next( be );
200 ID mdb_tool_entry_next(
205 struct mdb_info *mdb;
207 assert( be != NULL );
208 assert( slapMode & SLAP_TOOL_MODE );
210 mdb = (struct mdb_info *) be->be_private;
211 assert( mdb != NULL );
214 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn );
217 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
219 mdb_txn_abort( txn );
225 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
231 previd = *(ID *)key.mv_data;
234 if ( tool_filter || tool_base ) {
235 static Operation op = {0};
236 static Opheader ohdr = {0};
240 op.o_tmpmemctx = NULL;
241 op.o_tmpmfuncs = &ch_mfuncs;
243 if ( tool_next_entry ) {
244 mdb_entry_release( &op, tool_next_entry, 0 );
245 tool_next_entry = NULL;
248 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
249 if ( rc == LDAP_NO_SUCH_OBJECT ) {
253 assert( tool_next_entry != NULL );
255 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
257 mdb_entry_release( &op, tool_next_entry, 0 );
258 tool_next_entry = NULL;
266 ID mdb_tool_dn2id_get(
271 struct mdb_info *mdb;
277 if ( BER_BVISEMPTY(dn) )
280 mdb = (struct mdb_info *) be->be_private;
283 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
284 MDB_RDONLY : 0, &txn );
291 op.o_tmpmemctx = NULL;
292 op.o_tmpmfuncs = &ch_mfuncs;
294 rc = mdb_dn2id( &op, txn, NULL, dn, &id, NULL, NULL );
295 if ( rc == MDB_NOTFOUND )
302 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
308 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
311 assert( be != NULL );
312 assert( slapMode & SLAP_TOOL_MODE );
314 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
315 *ep = tool_next_entry;
316 tool_next_entry = NULL;
320 if ( id != previd ) {
321 key.mv_size = sizeof(ID);
323 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
332 op.o_tmpmemctx = NULL;
333 op.o_tmpmfuncs = &ch_mfuncs;
334 if ( slapMode & SLAP_TOOL_READONLY ) {
335 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
339 mdb_entry_return( &op, e );
344 if ( tool_base != NULL ) {
345 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
346 ch_free( dn.bv_val );
347 ch_free( ndn.bv_val );
348 rc = LDAP_NO_SUCH_OBJECT;
352 rc = mdb_entry_decode( &op, &data, &e );
354 if ( !BER_BVISNULL( &dn )) {
358 e->e_name.bv_val = NULL;
359 e->e_nname.bv_val = NULL;
371 mdb_tool_entry_get( BackendDB *be, ID id )
377 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
378 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
379 (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &txn );
384 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
385 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
387 mdb_txn_abort( txn );
392 (void)mdb_tool_entry_get_int( be, id, &e );
396 static int mdb_tool_next_id(
403 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
404 struct berval dn = e->e_name;
405 struct berval ndn = e->e_nname;
406 struct berval pdn, npdn, nmatched;
410 if (ndn.bv_len == 0) {
415 rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, &nmatched );
416 if ( rc == MDB_NOTFOUND ) {
417 if ( !be_issuffix( op->o_bd, &ndn ) ) {
419 dnParent( &ndn, &npdn );
420 if ( nmatched.bv_len != npdn.bv_len ) {
421 dnParent( &dn, &pdn );
424 rc = mdb_tool_next_id( op, tid, e, text, 1 );
430 /* If parent didn't exist, it was created just now
431 * and its ID is now in e->e_id. Make sure the current
432 * entry gets added under the new parent ID.
434 if ( eid != e->e_id ) {
441 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
443 snprintf( text->bv_val, text->bv_len,
444 "next_id failed: %s (%d)",
445 mdb_strerror(rc), rc );
446 Debug( LDAP_DEBUG_ANY,
447 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
450 rc = mdb_dn2id_add( op, mcp, mcd, pid, e );
452 snprintf( text->bv_val, text->bv_len,
453 "dn2id_add failed: %s (%d)",
454 mdb_strerror(rc), rc );
455 Debug( LDAP_DEBUG_ANY,
456 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
459 if ( nholes == nhmax - 1 ) {
460 if ( holes == hbuf ) {
461 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
462 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
464 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
468 ber_dupbv( &holes[nholes].dn, &ndn );
469 holes[nholes++].id = e->e_id;
470 key.mv_size = sizeof(ID);
471 key.mv_data = &e->e_id;
474 rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
475 if ( rc == MDB_KEYEXIST )
478 snprintf( text->bv_val, text->bv_len,
479 "dummy id2entry add failed: %s (%d)",
480 mdb_strerror(rc), rc );
481 Debug( LDAP_DEBUG_ANY,
482 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
485 } else if ( !hole ) {
490 for ( i=0; i<nholes; i++) {
491 if ( holes[i].id == e->e_id ) {
492 free(holes[i].dn.bv_val);
493 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
497 } else if ( holes[i].id > e->e_id ) {
511 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
513 if ( !mdb->mi_nattrs )
516 if ( mdb_tool_threads > 1 ) {
521 ir = mdb_tool_index_rec;
522 for (i=0; i<mdb->mi_nattrs; i++)
523 ir[i].ir_attrs = NULL;
525 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
526 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
527 &a->a_desc->ad_tags, ir );
531 for (i=0; i<mdb->mi_nattrs; i++) {
534 rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
535 &ir[i].ir_ai->ai_cursor );
539 mdb_tool_ix_id = e->e_id;
541 mdb_tool_ix_txn = txn;
542 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
543 /* Wait for all threads to be ready */
544 while ( mdb_tool_index_tcount ) {
545 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
546 &mdb_tool_index_mutex );
549 for ( i=1; i<mdb_tool_threads; i++ )
550 mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
551 mdb_tool_index_tcount = mdb_tool_threads - 1;
552 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
553 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
555 rc = mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
558 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
559 for ( i=1; i<mdb_tool_threads; i++ ) {
560 if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
561 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
562 &mdb_tool_index_mutex );
566 if ( mdb_tool_index_rec[i].ir_i ) {
567 rc = mdb_tool_index_rec[i].ir_i;
571 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
575 return mdb_index_entry_add( op, txn, e );
579 ID mdb_tool_entry_put(
582 struct berval *text )
585 struct mdb_info *mdb;
589 assert( be != NULL );
590 assert( slapMode & SLAP_TOOL_MODE );
592 assert( text != NULL );
593 assert( text->bv_val != NULL );
594 assert( text->bv_val[0] == '\0' ); /* overconservative? */
596 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
597 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
599 mdb = (struct mdb_info *) be->be_private;
602 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
604 snprintf( text->bv_val, text->bv_len,
605 "txn_begin failed: %s (%d)",
606 mdb_strerror(rc), rc );
607 Debug( LDAP_DEBUG_ANY,
608 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
609 text->bv_val, 0, 0 );
612 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &idcursor );
614 snprintf( text->bv_val, text->bv_len,
615 "cursor_open failed: %s (%d)",
616 mdb_strerror(rc), rc );
617 Debug( LDAP_DEBUG_ANY,
618 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
619 text->bv_val, 0, 0 );
622 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcp );
624 snprintf( text->bv_val, text->bv_len,
625 "cursor_open failed: %s (%d)",
626 mdb_strerror(rc), rc );
627 Debug( LDAP_DEBUG_ANY,
628 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
629 text->bv_val, 0, 0 );
632 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
634 snprintf( text->bv_val, text->bv_len,
635 "cursor_open failed: %s (%d)",
636 mdb_strerror(rc), rc );
637 Debug( LDAP_DEBUG_ANY,
638 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
639 text->bv_val, 0, 0 );
646 op.o_tmpmemctx = NULL;
647 op.o_tmpmfuncs = &ch_mfuncs;
649 /* add dn2id indices */
650 rc = mdb_tool_next_id( &op, txn, e, text, 0 );
655 rc = mdb_tool_index_add( &op, txn, e );
657 snprintf( text->bv_val, text->bv_len,
658 "index_entry_add failed: err=%d", rc );
659 Debug( LDAP_DEBUG_ANY,
660 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
661 text->bv_val, 0, 0 );
667 rc = mdb_id2entry_add( &op, txn, idcursor, e );
669 snprintf( text->bv_val, text->bv_len,
670 "id2entry_add failed: err=%d", rc );
671 Debug( LDAP_DEBUG_ANY,
672 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
673 text->bv_val, 0, 0 );
680 if ( mdb_writes >= mdb_writes_per_commit ) {
682 MDB_TOOL_IDL_FLUSH( be, txn );
683 rc = mdb_txn_commit( txn );
684 for ( i=0; i<mdb->mi_nattrs; i++ )
685 mdb->mi_attrs[i]->ai_cursor = NULL;
690 snprintf( text->bv_val, text->bv_len,
691 "txn_commit failed: %s (%d)",
692 mdb_strerror(rc), rc );
693 Debug( LDAP_DEBUG_ANY,
694 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
695 text->bv_val, 0, 0 );
702 mdb_txn_abort( txn );
705 for ( i=0; i<mdb->mi_nattrs; i++ )
706 mdb->mi_attrs[i]->ai_cursor = NULL;
708 snprintf( text->bv_val, text->bv_len,
709 "txn_aborted! %s (%d)",
710 rc == LDAP_OTHER ? "Internal error" :
711 mdb_strerror(rc), rc );
712 Debug( LDAP_DEBUG_ANY,
713 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
714 text->bv_val, 0, 0 );
721 int mdb_tool_entry_reindex(
724 AttributeDescription **adv )
726 struct mdb_info *mi = (struct mdb_info *) be->be_private;
732 Debug( LDAP_DEBUG_ARGS,
733 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
735 assert( tool_base == NULL );
736 assert( tool_filter == NULL );
738 /* No indexes configured, nothing to do. Could return an
739 * error here to shortcut things.
745 /* Check for explicit list of attrs to index */
749 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
751 for ( n = 0; adv[n]; n++ ) ;
754 for ( i = 0; i < n; i++ ) {
755 AttributeDescription *ad = adv[i];
756 for ( j = i-1; j>=0; j--) {
757 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
764 for ( i = 0; adv[i]; i++ ) {
765 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
766 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
767 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
768 AttrInfo *ai = mi->mi_attrs[i];
769 mi->mi_attrs[i] = mi->mi_attrs[j];
770 mi->mi_attrs[j] = ai;
774 if ( j == mi->mi_nattrs ) {
775 Debug( LDAP_DEBUG_ANY,
776 LDAP_XSTRING(mdb_tool_entry_reindex)
777 ": no index configured for %s\n",
778 adv[i]->ad_cname.bv_val, 0, 0 );
786 e = mdb_tool_entry_get( be, id );
789 Debug( LDAP_DEBUG_ANY,
790 LDAP_XSTRING(mdb_tool_entry_reindex)
791 ": could not locate id=%ld\n",
797 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
799 Debug( LDAP_DEBUG_ANY,
800 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
801 "txn_begin failed: %s (%d)\n",
802 mdb_strerror(rc), rc, 0 );
807 if ( slapMode & SLAP_TRUNCATE_MODE ) {
809 for ( i=0; i < mi->mi_nattrs; i++ ) {
810 rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
812 Debug( LDAP_DEBUG_ANY,
813 LDAP_XSTRING(mdb_tool_entry_reindex)
814 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
815 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
816 mdb_strerror(rc), rc );
820 slapMode ^= SLAP_TRUNCATE_MODE;
824 * just (re)add them for now
825 * Use truncate mode to empty/reset index databases
828 Debug( LDAP_DEBUG_TRACE,
829 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
834 op.o_tmpmemctx = NULL;
835 op.o_tmpmfuncs = &ch_mfuncs;
837 rc = mdb_tool_index_add( &op, txi, e );
842 if ( mdb_writes >= mdb_writes_per_commit ) {
844 MDB_TOOL_IDL_FLUSH( be, txi );
845 rc = mdb_txn_commit( txi );
847 for ( i=0; i<mi->mi_nattrs; i++ )
848 mi->mi_attrs[i]->ai_cursor = NULL;
850 Debug( LDAP_DEBUG_ANY,
851 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
852 ": txn_commit failed: %s (%d)\n",
853 mdb_strerror(rc), rc, 0 );
862 mdb_txn_abort( txi );
863 for ( i=0; i<mi->mi_nattrs; i++ )
864 mi->mi_attrs[i]->ai_cursor = NULL;
865 Debug( LDAP_DEBUG_ANY,
866 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
867 ": txn_aborted! err=%d\n",
872 mdb_entry_release( &op, e, 0 );
877 ID mdb_tool_entry_modify(
880 struct berval *text )
883 struct mdb_info *mdb;
888 assert( be != NULL );
889 assert( slapMode & SLAP_TOOL_MODE );
891 assert( text != NULL );
892 assert( text->bv_val != NULL );
893 assert( text->bv_val[0] == '\0' ); /* overconservative? */
895 assert ( e->e_id != NOID );
897 Debug( LDAP_DEBUG_TRACE,
898 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
899 (long) e->e_id, e->e_dn, 0 );
901 mdb = (struct mdb_info *) be->be_private;
904 mdb_cursor_close( cursor );
907 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &tid );
909 snprintf( text->bv_val, text->bv_len,
910 "txn_begin failed: %s (%d)",
911 mdb_strerror(rc), rc );
912 Debug( LDAP_DEBUG_ANY,
913 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
914 text->bv_val, 0, 0 );
920 op.o_tmpmemctx = NULL;
921 op.o_tmpmfuncs = &ch_mfuncs;
924 rc = mdb_id2entry_update( &op, tid, NULL, e );
926 snprintf( text->bv_val, text->bv_len,
927 "id2entry_update failed: err=%d", rc );
928 Debug( LDAP_DEBUG_ANY,
929 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
930 text->bv_val, 0, 0 );
936 rc = mdb_txn_commit( tid );
938 snprintf( text->bv_val, text->bv_len,
939 "txn_commit failed: %s (%d)",
940 mdb_strerror(rc), rc );
941 Debug( LDAP_DEBUG_ANY,
942 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
943 "%s\n", text->bv_val, 0, 0 );
948 mdb_txn_abort( tid );
949 snprintf( text->bv_val, text->bv_len,
950 "txn_aborted! %s (%d)",
951 mdb_strerror(rc), rc );
952 Debug( LDAP_DEBUG_ANY,
953 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
954 text->bv_val, 0, 0 );
962 mdb_tool_index_task( void *ctx, void *ptr )
964 int base = *(int *)ptr;
968 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
969 mdb_tool_index_tcount--;
970 if ( !mdb_tool_index_tcount )
971 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
972 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
973 &mdb_tool_index_mutex );
974 if ( slapd_shutdown ) {
975 mdb_tool_index_tcount--;
976 if ( !mdb_tool_index_tcount )
977 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
978 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
981 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
982 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( mdb_tool_ix_op,
984 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
990 #ifdef MDB_TOOL_IDL_CACHING
992 mdb_tool_idl_cmp( const void *v1, const void *v2 )
994 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
997 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
998 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1002 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrInfo *ai, mdb_tool_idl_cache *ic )
1004 mdb_tool_idl_cache_entry *ice;
1005 MDB_val key, data[2];
1009 /* Freshly allocated, ignore it */
1010 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1014 key.mv_data = ic->kstr.bv_val;
1015 key.mv_size = ic->kstr.bv_len;
1017 if ( ic->count > MDB_IDL_DB_SIZE ) {
1018 while ( ic->flags & WAS_FOUND ) {
1019 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1021 /* FIXME: find out why this happens */
1025 if ( ic->flags & WAS_RANGE ) {
1027 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1030 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1032 /* Store range hi */
1033 data[0].mv_data = &ic->last;
1034 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1036 /* Delete old data, replace with range */
1037 ic->first = *(ID *)data[0].mv_data;
1038 mdb_cursor_del( mc, MDB_NODUPDATA );
1042 if ( !(ic->flags & WAS_RANGE)) {
1043 /* range, didn't exist before */
1045 data[0].mv_size = sizeof(ID);
1046 data[0].mv_data = &nid;
1047 rc = mdb_cursor_put( mc, &key, data, 0 );
1049 data[0].mv_data = &ic->first;
1050 rc = mdb_cursor_put( mc, &key, data, 0 );
1052 data[0].mv_data = &ic->last;
1053 rc = mdb_cursor_put( mc, &key, data, 0 );
1064 data[0].mv_size = sizeof(ID);
1067 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1072 end = ic->count & (IDBLOCK-1);
1076 data[1].mv_size = end - i;
1077 data[0].mv_data = &ice->ids[i];
1079 rc = mdb_cursor_put( mc, &key, data, MDB_NODUPDATA|MDB_APPEND|MDB_MULTIPLE );
1081 if ( rc == MDB_KEYEXIST ) {
1090 ic->tail->next = ai->ai_flist;
1091 ai->ai_flist = ic->head;
1094 ic->head = ai->ai_clist;
1100 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai )
1106 mdb_cursor_open( txn, ai->ai_dbi, &mc );
1107 root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1109 rc = mdb_tool_idl_flush_one( mc, ai, root->avl_data );
1112 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1113 mdb_cursor_close( mc );
1119 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1121 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1123 unsigned int i, dbi;
1125 for ( i=0; i < mdb->mi_nattrs; i++ ) {
1126 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1127 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i] );
1128 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1129 mdb->mi_attrs[i]->ai_root = NULL;
1136 int mdb_tool_idl_add(
1138 struct berval *keys,
1142 mdb_tool_idl_cache *ic, itmp;
1143 mdb_tool_idl_cache_entry *ice;
1145 AttrInfo *ai = (AttrInfo *)mc;
1149 for (i=0; keys[i].bv_val; i++) {
1150 itmp.kstr = keys[i];
1151 ic = tavl_find( (Avlnode *)ai->ai_root, &itmp, mdb_tool_idl_cmp );
1153 /* No entry yet, create one */
1159 if ( ai->ai_clist ) {
1161 ai->ai_clist = ic->head;
1163 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1165 ic->kstr.bv_len = itmp.kstr.bv_len;
1166 ic->kstr.bv_val = (char *)(ic+1);
1167 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1168 ic->head = ic->tail = NULL;
1173 tavl_insert( (Avlnode **)&ai->ai_root, ic, mdb_tool_idl_cmp,
1176 /* load existing key count here */
1177 key.mv_size = keys[i].bv_len;
1178 key.mv_data = keys[i].bv_val;
1179 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1181 ic->flags |= WAS_FOUND;
1182 nid = *(ID *)data.mv_data;
1184 ic->count = MDB_IDL_DB_SIZE+1;
1185 ic->flags |= WAS_RANGE;
1189 mdb_cursor_count( mc, &count );
1192 ic->offset = count & (IDBLOCK-1);
1196 /* are we a range already? */
1197 if ( ic->count > MDB_IDL_DB_SIZE ) {
1200 /* Are we at the limit, and converting to a range? */
1201 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1203 ic->tail->next = ai->ai_flist;
1204 ai->ai_flist = ic->head;
1206 ic->head = ic->tail = NULL;
1211 /* No free block, create that too */
1212 lcount = ic->count & (IDBLOCK-1);
1213 if ( !ic->tail || lcount == 0) {
1214 if ( ai->ai_flist ) {
1216 ai->ai_flist = ice->next;
1218 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1224 ic->tail->next = ice;
1228 ice->ids[lcount-1] = 0;
1233 if (!lcount || ice->ids[lcount-1] != id)
1234 ice->ids[lcount] = id;
1240 #endif /* MDB_TOOL_IDL_CACHING */