1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2011-2013 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;
237 if ( tool_filter || tool_base ) {
238 static Operation op = {0};
239 static Opheader ohdr = {0};
243 op.o_tmpmemctx = NULL;
244 op.o_tmpmfuncs = &ch_mfuncs;
246 if ( tool_next_entry ) {
247 mdb_entry_release( &op, tool_next_entry, 0 );
248 tool_next_entry = NULL;
251 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
252 if ( rc == LDAP_NO_SUCH_OBJECT ) {
256 assert( tool_next_entry != NULL );
258 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
260 mdb_entry_release( &op, tool_next_entry, 0 );
261 tool_next_entry = NULL;
269 ID mdb_tool_dn2id_get(
274 struct mdb_info *mdb;
280 if ( BER_BVISEMPTY(dn) )
283 mdb = (struct mdb_info *) be->be_private;
286 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
287 MDB_RDONLY : 0, &txn );
294 op.o_tmpmemctx = NULL;
295 op.o_tmpmfuncs = &ch_mfuncs;
297 rc = mdb_dn2id( &op, txn, NULL, dn, &id, NULL, NULL );
298 if ( rc == MDB_NOTFOUND )
305 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
311 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
314 assert( be != NULL );
315 assert( slapMode & SLAP_TOOL_MODE );
317 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
318 *ep = tool_next_entry;
319 tool_next_entry = NULL;
323 if ( id != previd ) {
324 key.mv_size = sizeof(ID);
326 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
332 if ( !data.mv_size ) {
333 rc = LDAP_NO_SUCH_OBJECT;
339 op.o_tmpmemctx = NULL;
340 op.o_tmpmfuncs = &ch_mfuncs;
341 if ( slapMode & SLAP_TOOL_READONLY ) {
342 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
346 mdb_entry_return( &op, e );
351 if ( tool_base != NULL ) {
352 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
353 ch_free( dn.bv_val );
354 ch_free( ndn.bv_val );
355 rc = LDAP_NO_SUCH_OBJECT;
360 rc = mdb_entry_decode( &op, &data, &e );
362 if ( !BER_BVISNULL( &dn )) {
366 e->e_name.bv_val = NULL;
367 e->e_nname.bv_val = NULL;
379 mdb_tool_entry_get( BackendDB *be, ID id )
385 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
386 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
387 (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &txn );
392 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
393 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
395 mdb_txn_abort( txn );
400 (void)mdb_tool_entry_get_int( be, id, &e );
404 static int mdb_tool_next_id(
411 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
412 struct berval dn = e->e_name;
413 struct berval ndn = e->e_nname;
414 struct berval pdn, npdn, nmatched;
418 if (ndn.bv_len == 0) {
423 rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, &nmatched );
424 if ( rc == MDB_NOTFOUND ) {
425 if ( !be_issuffix( op->o_bd, &ndn ) ) {
427 dnParent( &ndn, &npdn );
428 if ( nmatched.bv_len != npdn.bv_len ) {
429 dnParent( &dn, &pdn );
432 rc = mdb_tool_next_id( op, tid, e, text, 1 );
438 /* If parent didn't exist, it was created just now
439 * and its ID is now in e->e_id. Make sure the current
440 * entry gets added under the new parent ID.
442 if ( eid != e->e_id ) {
449 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
451 snprintf( text->bv_val, text->bv_len,
452 "next_id failed: %s (%d)",
453 mdb_strerror(rc), rc );
454 Debug( LDAP_DEBUG_ANY,
455 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
458 rc = mdb_dn2id_add( op, mcp, mcd, pid, e );
460 snprintf( text->bv_val, text->bv_len,
461 "dn2id_add failed: %s (%d)",
462 mdb_strerror(rc), rc );
463 Debug( LDAP_DEBUG_ANY,
464 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
467 if ( nholes == nhmax - 1 ) {
468 if ( holes == hbuf ) {
469 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
470 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
472 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
476 ber_dupbv( &holes[nholes].dn, &ndn );
477 holes[nholes++].id = e->e_id;
478 key.mv_size = sizeof(ID);
479 key.mv_data = &e->e_id;
482 rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
483 if ( rc == MDB_KEYEXIST )
486 snprintf( text->bv_val, text->bv_len,
487 "dummy id2entry add failed: %s (%d)",
488 mdb_strerror(rc), rc );
489 Debug( LDAP_DEBUG_ANY,
490 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
493 } else if ( !hole ) {
498 for ( i=0; i<nholes; i++) {
499 if ( holes[i].id == e->e_id ) {
500 free(holes[i].dn.bv_val);
501 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
505 } else if ( holes[i].id > e->e_id ) {
519 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
521 if ( !mdb->mi_nattrs )
524 if ( mdb_tool_threads > 1 ) {
529 ir = mdb_tool_index_rec;
530 for (i=0; i<mdb->mi_nattrs; i++)
531 ir[i].ir_attrs = NULL;
533 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
534 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
535 &a->a_desc->ad_tags, ir );
539 for (i=0; i<mdb->mi_nattrs; i++) {
542 rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
543 &ir[i].ir_ai->ai_cursor );
547 mdb_tool_ix_id = e->e_id;
549 mdb_tool_ix_txn = txn;
550 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
551 /* Wait for all threads to be ready */
552 while ( mdb_tool_index_tcount ) {
553 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
554 &mdb_tool_index_mutex );
557 for ( i=1; i<mdb_tool_threads; i++ )
558 mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
559 mdb_tool_index_tcount = mdb_tool_threads - 1;
560 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
561 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
563 rc = mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
566 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
567 for ( i=1; i<mdb_tool_threads; i++ ) {
568 if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
569 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
570 &mdb_tool_index_mutex );
574 if ( mdb_tool_index_rec[i].ir_i ) {
575 rc = mdb_tool_index_rec[i].ir_i;
579 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
583 return mdb_index_entry_add( op, txn, e );
587 ID mdb_tool_entry_put(
590 struct berval *text )
593 struct mdb_info *mdb;
597 assert( be != NULL );
598 assert( slapMode & SLAP_TOOL_MODE );
600 assert( text != NULL );
601 assert( text->bv_val != NULL );
602 assert( text->bv_val[0] == '\0' ); /* overconservative? */
604 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
605 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
607 mdb = (struct mdb_info *) be->be_private;
610 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
612 snprintf( text->bv_val, text->bv_len,
613 "txn_begin failed: %s (%d)",
614 mdb_strerror(rc), rc );
615 Debug( LDAP_DEBUG_ANY,
616 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
617 text->bv_val, 0, 0 );
620 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &idcursor );
622 snprintf( text->bv_val, text->bv_len,
623 "cursor_open 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 );
630 if ( !mdb->mi_nextid ) {
632 mdb_next_id( be, idcursor, &dummy );
634 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcp );
636 snprintf( text->bv_val, text->bv_len,
637 "cursor_open failed: %s (%d)",
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 );
644 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
646 snprintf( text->bv_val, text->bv_len,
647 "cursor_open failed: %s (%d)",
648 mdb_strerror(rc), rc );
649 Debug( LDAP_DEBUG_ANY,
650 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
651 text->bv_val, 0, 0 );
658 op.o_tmpmemctx = NULL;
659 op.o_tmpmfuncs = &ch_mfuncs;
661 /* add dn2id indices */
662 rc = mdb_tool_next_id( &op, txn, e, text, 0 );
667 rc = mdb_tool_index_add( &op, txn, e );
669 snprintf( text->bv_val, text->bv_len,
670 "index_entry_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 );
679 rc = mdb_id2entry_add( &op, txn, idcursor, e );
681 snprintf( text->bv_val, text->bv_len,
682 "id2entry_add failed: err=%d", rc );
683 Debug( LDAP_DEBUG_ANY,
684 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
685 text->bv_val, 0, 0 );
692 if ( mdb_writes >= mdb_writes_per_commit ) {
694 MDB_TOOL_IDL_FLUSH( be, txn );
695 rc = mdb_txn_commit( txn );
696 for ( i=0; i<mdb->mi_nattrs; i++ )
697 mdb->mi_attrs[i]->ai_cursor = NULL;
702 snprintf( text->bv_val, text->bv_len,
703 "txn_commit failed: %s (%d)",
704 mdb_strerror(rc), rc );
705 Debug( LDAP_DEBUG_ANY,
706 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
707 text->bv_val, 0, 0 );
714 mdb_txn_abort( txn );
717 for ( i=0; i<mdb->mi_nattrs; i++ )
718 mdb->mi_attrs[i]->ai_cursor = NULL;
720 snprintf( text->bv_val, text->bv_len,
721 "txn_aborted! %s (%d)",
722 rc == LDAP_OTHER ? "Internal error" :
723 mdb_strerror(rc), rc );
724 Debug( LDAP_DEBUG_ANY,
725 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
726 text->bv_val, 0, 0 );
733 int mdb_tool_entry_reindex(
736 AttributeDescription **adv )
738 struct mdb_info *mi = (struct mdb_info *) be->be_private;
744 Debug( LDAP_DEBUG_ARGS,
745 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
747 assert( tool_base == NULL );
748 assert( tool_filter == NULL );
750 /* No indexes configured, nothing to do. Could return an
751 * error here to shortcut things.
757 /* Check for explicit list of attrs to index */
761 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
763 for ( n = 0; adv[n]; n++ ) ;
766 for ( i = 0; i < n; i++ ) {
767 AttributeDescription *ad = adv[i];
768 for ( j = i-1; j>=0; j--) {
769 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
776 for ( i = 0; adv[i]; i++ ) {
777 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
778 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
779 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
780 AttrInfo *ai = mi->mi_attrs[i];
781 mi->mi_attrs[i] = mi->mi_attrs[j];
782 mi->mi_attrs[j] = ai;
786 if ( j == mi->mi_nattrs ) {
787 Debug( LDAP_DEBUG_ANY,
788 LDAP_XSTRING(mdb_tool_entry_reindex)
789 ": no index configured for %s\n",
790 adv[i]->ad_cname.bv_val, 0, 0 );
798 e = mdb_tool_entry_get( be, id );
801 Debug( LDAP_DEBUG_ANY,
802 LDAP_XSTRING(mdb_tool_entry_reindex)
803 ": could not locate id=%ld\n",
809 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
811 Debug( LDAP_DEBUG_ANY,
812 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
813 "txn_begin failed: %s (%d)\n",
814 mdb_strerror(rc), rc, 0 );
819 if ( slapMode & SLAP_TRUNCATE_MODE ) {
821 for ( i=0; i < mi->mi_nattrs; i++ ) {
822 rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
824 Debug( LDAP_DEBUG_ANY,
825 LDAP_XSTRING(mdb_tool_entry_reindex)
826 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
827 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
828 mdb_strerror(rc), rc );
832 slapMode ^= SLAP_TRUNCATE_MODE;
836 * just (re)add them for now
837 * Use truncate mode to empty/reset index databases
840 Debug( LDAP_DEBUG_TRACE,
841 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
846 op.o_tmpmemctx = NULL;
847 op.o_tmpmfuncs = &ch_mfuncs;
849 rc = mdb_tool_index_add( &op, txi, e );
854 if ( mdb_writes >= mdb_writes_per_commit ) {
857 MDB_TOOL_IDL_FLUSH( be, txi );
858 rc = mdb_txn_commit( txi );
860 for ( i=0; i<mi->mi_nattrs; i++ )
861 mi->mi_attrs[i]->ai_cursor = NULL;
863 Debug( LDAP_DEBUG_ANY,
864 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
865 ": txn_commit failed: %s (%d)\n",
866 mdb_strerror(rc), rc, 0 );
869 mdb_cursor_close( cursor );
871 /* Must close the read txn to allow old pages to be reclaimed. */
872 mdb_txn_abort( txn );
873 /* and then reopen it so that tool_entry_next still works. */
874 mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &txn );
875 mdb_cursor_open( txn, mi->mi_id2entry, &cursor );
877 key.mv_size = sizeof(ID);
878 mdb_cursor_get( cursor, &key, NULL, MDB_SET );
884 mdb_cursor_close( cursor );
886 mdb_txn_abort( txi );
887 for ( i=0; i<mi->mi_nattrs; i++ )
888 mi->mi_attrs[i]->ai_cursor = NULL;
889 Debug( LDAP_DEBUG_ANY,
890 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
891 ": txn_aborted! err=%d\n",
896 mdb_entry_release( &op, e, 0 );
901 ID mdb_tool_entry_modify(
904 struct berval *text )
907 struct mdb_info *mdb;
911 assert( be != NULL );
912 assert( slapMode & SLAP_TOOL_MODE );
914 assert( text != NULL );
915 assert( text->bv_val != NULL );
916 assert( text->bv_val[0] == '\0' ); /* overconservative? */
918 assert ( e->e_id != NOID );
920 Debug( LDAP_DEBUG_TRACE,
921 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
922 (long) e->e_id, e->e_dn, 0 );
924 mdb = (struct mdb_info *) be->be_private;
927 mdb_cursor_close( cursor );
931 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
933 snprintf( text->bv_val, text->bv_len,
934 "txn_begin failed: %s (%d)",
935 mdb_strerror(rc), rc );
936 Debug( LDAP_DEBUG_ANY,
937 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
938 text->bv_val, 0, 0 );
945 op.o_tmpmemctx = NULL;
946 op.o_tmpmfuncs = &ch_mfuncs;
949 rc = mdb_id2entry_update( &op, txn, NULL, e );
951 snprintf( text->bv_val, text->bv_len,
952 "id2entry_update failed: err=%d", rc );
953 Debug( LDAP_DEBUG_ANY,
954 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
955 text->bv_val, 0, 0 );
961 rc = mdb_txn_commit( txn );
963 snprintf( text->bv_val, text->bv_len,
964 "txn_commit failed: %s (%d)",
965 mdb_strerror(rc), rc );
966 Debug( LDAP_DEBUG_ANY,
967 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
968 "%s\n", text->bv_val, 0, 0 );
973 mdb_txn_abort( txn );
974 snprintf( text->bv_val, text->bv_len,
975 "txn_aborted! %s (%d)",
976 mdb_strerror(rc), rc );
977 Debug( LDAP_DEBUG_ANY,
978 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
979 text->bv_val, 0, 0 );
989 mdb_tool_index_task( void *ctx, void *ptr )
991 int base = *(int *)ptr;
995 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
996 mdb_tool_index_tcount--;
997 if ( !mdb_tool_index_tcount )
998 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
999 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
1000 &mdb_tool_index_mutex );
1001 if ( slapd_shutdown ) {
1002 mdb_tool_index_tcount--;
1003 if ( !mdb_tool_index_tcount )
1004 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1005 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1008 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1009 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( mdb_tool_ix_op,
1011 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
1017 #ifdef MDB_TOOL_IDL_CACHING
1019 mdb_tool_idl_cmp( const void *v1, const void *v2 )
1021 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
1024 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
1025 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1029 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrInfo *ai, mdb_tool_idl_cache *ic )
1031 mdb_tool_idl_cache_entry *ice;
1032 MDB_val key, data[2];
1036 /* Freshly allocated, ignore it */
1037 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1041 key.mv_data = ic->kstr.bv_val;
1042 key.mv_size = ic->kstr.bv_len;
1044 if ( ic->count > MDB_IDL_DB_SIZE ) {
1045 while ( ic->flags & WAS_FOUND ) {
1046 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1048 /* FIXME: find out why this happens */
1052 if ( ic->flags & WAS_RANGE ) {
1054 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1057 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1059 /* Store range hi */
1060 data[0].mv_data = &ic->last;
1061 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1063 /* Delete old data, replace with range */
1064 ic->first = *(ID *)data[0].mv_data;
1065 mdb_cursor_del( mc, MDB_NODUPDATA );
1069 if ( !(ic->flags & WAS_RANGE)) {
1070 /* range, didn't exist before */
1072 data[0].mv_size = sizeof(ID);
1073 data[0].mv_data = &nid;
1074 rc = mdb_cursor_put( mc, &key, data, 0 );
1076 data[0].mv_data = &ic->first;
1077 rc = mdb_cursor_put( mc, &key, data, 0 );
1079 data[0].mv_data = &ic->last;
1080 rc = mdb_cursor_put( mc, &key, data, 0 );
1091 data[0].mv_size = sizeof(ID);
1094 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1099 end = ic->count & (IDBLOCK-1);
1103 data[1].mv_size = end - i;
1104 data[0].mv_data = &ice->ids[i];
1106 rc = mdb_cursor_put( mc, &key, data, MDB_NODUPDATA|MDB_APPEND|MDB_MULTIPLE );
1108 if ( rc == MDB_KEYEXIST ) {
1117 ic->tail->next = ai->ai_flist;
1118 ai->ai_flist = ic->head;
1121 ic->head = ai->ai_clist;
1127 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai )
1133 mdb_cursor_open( txn, ai->ai_dbi, &mc );
1134 root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1136 rc = mdb_tool_idl_flush_one( mc, ai, root->avl_data );
1139 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1140 mdb_cursor_close( mc );
1146 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1148 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1150 unsigned int i, dbi;
1152 for ( i=0; i < mdb->mi_nattrs; i++ ) {
1153 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1154 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i] );
1155 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1156 mdb->mi_attrs[i]->ai_root = NULL;
1163 int mdb_tool_idl_add(
1166 struct berval *keys,
1170 mdb_tool_idl_cache *ic, itmp;
1171 mdb_tool_idl_cache_entry *ice;
1173 AttrInfo *ai = (AttrInfo *)mc;
1177 for (i=0; keys[i].bv_val; i++) {
1178 itmp.kstr = keys[i];
1179 ic = tavl_find( (Avlnode *)ai->ai_root, &itmp, mdb_tool_idl_cmp );
1181 /* No entry yet, create one */
1187 if ( ai->ai_clist ) {
1189 ai->ai_clist = ic->head;
1191 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1193 ic->kstr.bv_len = itmp.kstr.bv_len;
1194 ic->kstr.bv_val = (char *)(ic+1);
1195 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1196 ic->head = ic->tail = NULL;
1201 tavl_insert( (Avlnode **)&ai->ai_root, ic, mdb_tool_idl_cmp,
1204 /* load existing key count here */
1205 key.mv_size = keys[i].bv_len;
1206 key.mv_data = keys[i].bv_val;
1207 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1209 ic->flags |= WAS_FOUND;
1210 nid = *(ID *)data.mv_data;
1212 ic->count = MDB_IDL_DB_SIZE+1;
1213 ic->flags |= WAS_RANGE;
1217 mdb_cursor_count( mc, &count );
1220 ic->offset = count & (IDBLOCK-1);
1224 /* are we a range already? */
1225 if ( ic->count > MDB_IDL_DB_SIZE ) {
1228 /* Are we at the limit, and converting to a range? */
1229 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1231 ic->tail->next = ai->ai_flist;
1232 ai->ai_flist = ic->head;
1234 ic->head = ic->tail = NULL;
1239 /* No free block, create that too */
1240 lcount = ic->count & (IDBLOCK-1);
1241 if ( !ic->tail || lcount == 0) {
1242 if ( ai->ai_flist ) {
1244 ai->ai_flist = ice->next;
1246 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1252 ic->tail->next = ice;
1256 ice->ids[lcount-1] = 0;
1261 if (!lcount || ice->ids[lcount-1] != id)
1262 ice->ids[lcount] = id;
1268 #endif /* MDB_TOOL_IDL_CACHING */