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 #if 0 /* threaded indexing has no performance advantage */
110 mdb_tool_threads = slap_tool_thread_max - 1;
112 if ( mdb_tool_threads > 1 ) {
113 mdb_tool_index_rec = ch_calloc( mdb->mi_nattrs, sizeof( IndexRec ));
114 mdb_tool_index_tcount = mdb_tool_threads - 1;
115 for (i=1; i<mdb_tool_threads; i++) {
116 int *ptr = ch_malloc( sizeof( int ));
118 ldap_pvt_thread_pool_submit( &connection_pool,
119 mdb_tool_index_task, ptr );
130 int mdb_tool_entry_close(
133 if ( mdb_tool_info ) {
135 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
137 /* There might still be some threads starting */
138 while ( mdb_tool_index_tcount > 0 ) {
139 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
140 &mdb_tool_index_mutex );
143 mdb_tool_index_tcount = mdb_tool_threads - 1;
144 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
146 /* Make sure all threads are stopped */
147 while ( mdb_tool_index_tcount > 0 ) {
148 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
149 &mdb_tool_index_mutex );
151 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
153 mdb_tool_info = NULL;
155 ch_free( mdb_tool_index_rec );
156 mdb_tool_index_tcount = mdb_tool_threads - 1;
160 mdb_cursor_close( idcursor );
164 mdb_cursor_close( cursor );
168 MDB_TOOL_IDL_FLUSH( be, txn );
169 if ( mdb_txn_commit( txn ))
176 fprintf( stderr, "Error, entries missing!\n");
177 for (i=0; i<nholes; i++) {
178 fprintf(stderr, " entry %ld: %s\n",
179 holes[i].id, holes[i].dn.bv_val);
189 mdb_tool_entry_first_x(
199 return mdb_tool_entry_next( be );
202 ID mdb_tool_entry_next(
207 struct mdb_info *mdb;
209 assert( be != NULL );
210 assert( slapMode & SLAP_TOOL_MODE );
212 mdb = (struct mdb_info *) be->be_private;
213 assert( mdb != NULL );
216 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn );
219 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
221 mdb_txn_abort( txn );
227 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
233 previd = *(ID *)key.mv_data;
239 if ( tool_filter || tool_base ) {
240 static Operation op = {0};
241 static Opheader ohdr = {0};
245 op.o_tmpmemctx = NULL;
246 op.o_tmpmfuncs = &ch_mfuncs;
248 if ( tool_next_entry ) {
249 mdb_entry_release( &op, tool_next_entry, 0 );
250 tool_next_entry = NULL;
253 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
254 if ( rc == LDAP_NO_SUCH_OBJECT ) {
258 assert( tool_next_entry != NULL );
260 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
262 mdb_entry_release( &op, tool_next_entry, 0 );
263 tool_next_entry = NULL;
271 ID mdb_tool_dn2id_get(
276 struct mdb_info *mdb;
282 if ( BER_BVISEMPTY(dn) )
285 mdb = (struct mdb_info *) be->be_private;
288 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
289 MDB_RDONLY : 0, &txn );
296 op.o_tmpmemctx = NULL;
297 op.o_tmpmfuncs = &ch_mfuncs;
299 rc = mdb_dn2id( &op, txn, NULL, dn, &id, NULL, NULL );
300 if ( rc == MDB_NOTFOUND )
307 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
313 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
316 assert( be != NULL );
317 assert( slapMode & SLAP_TOOL_MODE );
319 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
320 *ep = tool_next_entry;
321 tool_next_entry = NULL;
325 if ( id != previd ) {
326 key.mv_size = sizeof(ID);
328 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
334 if ( !data.mv_size ) {
335 rc = LDAP_NO_SUCH_OBJECT;
341 op.o_tmpmemctx = NULL;
342 op.o_tmpmfuncs = &ch_mfuncs;
343 if ( slapMode & SLAP_TOOL_READONLY ) {
344 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
348 mdb_entry_return( &op, e );
353 if ( tool_base != NULL ) {
354 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
355 ch_free( dn.bv_val );
356 ch_free( ndn.bv_val );
357 rc = LDAP_NO_SUCH_OBJECT;
362 rc = mdb_entry_decode( &op, &data, &e );
364 if ( !BER_BVISNULL( &dn )) {
368 e->e_name.bv_val = NULL;
369 e->e_nname.bv_val = NULL;
381 mdb_tool_entry_get( BackendDB *be, ID id )
387 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
388 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
389 (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &txn );
394 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
395 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
397 mdb_txn_abort( txn );
402 (void)mdb_tool_entry_get_int( be, id, &e );
406 static int mdb_tool_next_id(
413 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
414 struct berval dn = e->e_name;
415 struct berval ndn = e->e_nname;
416 struct berval pdn, npdn, nmatched;
420 if (ndn.bv_len == 0) {
425 rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, &nmatched );
426 if ( rc == MDB_NOTFOUND ) {
427 if ( !be_issuffix( op->o_bd, &ndn ) ) {
429 dnParent( &ndn, &npdn );
430 if ( nmatched.bv_len != npdn.bv_len ) {
431 dnParent( &dn, &pdn );
434 rc = mdb_tool_next_id( op, tid, e, text, 1 );
440 /* If parent didn't exist, it was created just now
441 * and its ID is now in e->e_id. Make sure the current
442 * entry gets added under the new parent ID.
444 if ( eid != e->e_id ) {
451 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
453 snprintf( text->bv_val, text->bv_len,
454 "next_id failed: %s (%d)",
455 mdb_strerror(rc), rc );
456 Debug( LDAP_DEBUG_ANY,
457 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
460 rc = mdb_dn2id_add( op, mcp, mcd, pid, e );
462 snprintf( text->bv_val, text->bv_len,
463 "dn2id_add failed: %s (%d)",
464 mdb_strerror(rc), rc );
465 Debug( LDAP_DEBUG_ANY,
466 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
469 if ( nholes == nhmax - 1 ) {
470 if ( holes == hbuf ) {
471 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
472 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
474 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
478 ber_dupbv( &holes[nholes].dn, &ndn );
479 holes[nholes++].id = e->e_id;
480 key.mv_size = sizeof(ID);
481 key.mv_data = &e->e_id;
484 rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
485 if ( rc == MDB_KEYEXIST )
488 snprintf( text->bv_val, text->bv_len,
489 "dummy id2entry add failed: %s (%d)",
490 mdb_strerror(rc), rc );
491 Debug( LDAP_DEBUG_ANY,
492 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
495 } else if ( !hole ) {
500 for ( i=0; i<nholes; i++) {
501 if ( holes[i].id == e->e_id ) {
502 free(holes[i].dn.bv_val);
503 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
507 } else if ( holes[i].id > e->e_id ) {
521 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
523 if ( !mdb->mi_nattrs )
526 if ( mdb_tool_threads > 1 ) {
531 ir = mdb_tool_index_rec;
532 for (i=0; i<mdb->mi_nattrs; i++)
533 ir[i].ir_attrs = NULL;
535 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
536 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
537 &a->a_desc->ad_tags, ir );
541 for (i=0; i<mdb->mi_nattrs; i++) {
544 rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
545 &ir[i].ir_ai->ai_cursor );
549 mdb_tool_ix_id = e->e_id;
551 mdb_tool_ix_txn = txn;
552 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
553 /* Wait for all threads to be ready */
554 while ( mdb_tool_index_tcount ) {
555 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
556 &mdb_tool_index_mutex );
559 for ( i=1; i<mdb_tool_threads; i++ )
560 mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
561 mdb_tool_index_tcount = mdb_tool_threads - 1;
562 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
563 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
565 rc = mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
568 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
569 for ( i=1; i<mdb_tool_threads; i++ ) {
570 if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
571 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
572 &mdb_tool_index_mutex );
576 if ( mdb_tool_index_rec[i].ir_i ) {
577 rc = mdb_tool_index_rec[i].ir_i;
581 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
585 return mdb_index_entry_add( op, txn, e );
589 ID mdb_tool_entry_put(
592 struct berval *text )
595 struct mdb_info *mdb;
599 assert( be != NULL );
600 assert( slapMode & SLAP_TOOL_MODE );
602 assert( text != NULL );
603 assert( text->bv_val != NULL );
604 assert( text->bv_val[0] == '\0' ); /* overconservative? */
606 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
607 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
609 mdb = (struct mdb_info *) be->be_private;
612 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
614 snprintf( text->bv_val, text->bv_len,
615 "txn_begin 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_id2entry, &idcursor );
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 if ( !mdb->mi_nextid ) {
634 mdb_next_id( be, idcursor, &dummy );
636 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcp );
638 snprintf( text->bv_val, text->bv_len,
639 "cursor_open failed: %s (%d)",
640 mdb_strerror(rc), rc );
641 Debug( LDAP_DEBUG_ANY,
642 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
643 text->bv_val, 0, 0 );
646 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
648 snprintf( text->bv_val, text->bv_len,
649 "cursor_open failed: %s (%d)",
650 mdb_strerror(rc), rc );
651 Debug( LDAP_DEBUG_ANY,
652 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
653 text->bv_val, 0, 0 );
660 op.o_tmpmemctx = NULL;
661 op.o_tmpmfuncs = &ch_mfuncs;
663 /* add dn2id indices */
664 rc = mdb_tool_next_id( &op, txn, e, text, 0 );
669 rc = mdb_tool_index_add( &op, txn, e );
671 snprintf( text->bv_val, text->bv_len,
672 "index_entry_add failed: err=%d", rc );
673 Debug( LDAP_DEBUG_ANY,
674 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
675 text->bv_val, 0, 0 );
681 rc = mdb_id2entry_add( &op, txn, idcursor, e );
683 snprintf( text->bv_val, text->bv_len,
684 "id2entry_add failed: err=%d", rc );
685 Debug( LDAP_DEBUG_ANY,
686 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
687 text->bv_val, 0, 0 );
694 if ( mdb_writes >= mdb_writes_per_commit ) {
696 MDB_TOOL_IDL_FLUSH( be, txn );
697 rc = mdb_txn_commit( txn );
698 for ( i=0; i<mdb->mi_nattrs; i++ )
699 mdb->mi_attrs[i]->ai_cursor = NULL;
704 snprintf( text->bv_val, text->bv_len,
705 "txn_commit failed: %s (%d)",
706 mdb_strerror(rc), rc );
707 Debug( LDAP_DEBUG_ANY,
708 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
709 text->bv_val, 0, 0 );
716 mdb_txn_abort( txn );
719 for ( i=0; i<mdb->mi_nattrs; i++ )
720 mdb->mi_attrs[i]->ai_cursor = NULL;
722 snprintf( text->bv_val, text->bv_len,
723 "txn_aborted! %s (%d)",
724 rc == LDAP_OTHER ? "Internal error" :
725 mdb_strerror(rc), rc );
726 Debug( LDAP_DEBUG_ANY,
727 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
728 text->bv_val, 0, 0 );
735 int mdb_tool_entry_reindex(
738 AttributeDescription **adv )
740 struct mdb_info *mi = (struct mdb_info *) be->be_private;
746 Debug( LDAP_DEBUG_ARGS,
747 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
749 assert( tool_base == NULL );
750 assert( tool_filter == NULL );
752 /* No indexes configured, nothing to do. Could return an
753 * error here to shortcut things.
759 /* Check for explicit list of attrs to index */
763 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
765 for ( n = 0; adv[n]; n++ ) ;
768 for ( i = 0; i < n; i++ ) {
769 AttributeDescription *ad = adv[i];
770 for ( j = i-1; j>=0; j--) {
771 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
778 for ( i = 0; adv[i]; i++ ) {
779 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
780 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
781 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
782 AttrInfo *ai = mi->mi_attrs[i];
783 mi->mi_attrs[i] = mi->mi_attrs[j];
784 mi->mi_attrs[j] = ai;
788 if ( j == mi->mi_nattrs ) {
789 Debug( LDAP_DEBUG_ANY,
790 LDAP_XSTRING(mdb_tool_entry_reindex)
791 ": no index configured for %s\n",
792 adv[i]->ad_cname.bv_val, 0, 0 );
800 e = mdb_tool_entry_get( be, id );
803 Debug( LDAP_DEBUG_ANY,
804 LDAP_XSTRING(mdb_tool_entry_reindex)
805 ": could not locate id=%ld\n",
811 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
813 Debug( LDAP_DEBUG_ANY,
814 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
815 "txn_begin failed: %s (%d)\n",
816 mdb_strerror(rc), rc, 0 );
821 if ( slapMode & SLAP_TRUNCATE_MODE ) {
823 for ( i=0; i < mi->mi_nattrs; i++ ) {
824 rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
826 Debug( LDAP_DEBUG_ANY,
827 LDAP_XSTRING(mdb_tool_entry_reindex)
828 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
829 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
830 mdb_strerror(rc), rc );
834 slapMode ^= SLAP_TRUNCATE_MODE;
838 * just (re)add them for now
839 * Use truncate mode to empty/reset index databases
842 Debug( LDAP_DEBUG_TRACE,
843 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
848 op.o_tmpmemctx = NULL;
849 op.o_tmpmfuncs = &ch_mfuncs;
851 rc = mdb_tool_index_add( &op, txi, e );
856 if ( mdb_writes >= mdb_writes_per_commit ) {
859 MDB_TOOL_IDL_FLUSH( be, txi );
860 rc = mdb_txn_commit( txi );
862 for ( i=0; i<mi->mi_nattrs; i++ )
863 mi->mi_attrs[i]->ai_cursor = NULL;
865 Debug( LDAP_DEBUG_ANY,
866 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
867 ": txn_commit failed: %s (%d)\n",
868 mdb_strerror(rc), rc, 0 );
871 mdb_cursor_close( cursor );
873 /* Must close the read txn to allow old pages to be reclaimed. */
874 mdb_txn_abort( txn );
875 /* and then reopen it so that tool_entry_next still works. */
876 mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &txn );
877 mdb_cursor_open( txn, mi->mi_id2entry, &cursor );
879 key.mv_size = sizeof(ID);
880 mdb_cursor_get( cursor, &key, NULL, MDB_SET );
886 mdb_cursor_close( cursor );
888 mdb_txn_abort( txi );
889 for ( i=0; i<mi->mi_nattrs; i++ )
890 mi->mi_attrs[i]->ai_cursor = NULL;
891 Debug( LDAP_DEBUG_ANY,
892 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
893 ": txn_aborted! err=%d\n",
898 mdb_entry_release( &op, e, 0 );
903 ID mdb_tool_entry_modify(
906 struct berval *text )
909 struct mdb_info *mdb;
913 assert( be != NULL );
914 assert( slapMode & SLAP_TOOL_MODE );
916 assert( text != NULL );
917 assert( text->bv_val != NULL );
918 assert( text->bv_val[0] == '\0' ); /* overconservative? */
920 assert ( e->e_id != NOID );
922 Debug( LDAP_DEBUG_TRACE,
923 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
924 (long) e->e_id, e->e_dn, 0 );
926 mdb = (struct mdb_info *) be->be_private;
929 mdb_cursor_close( cursor );
933 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
935 snprintf( text->bv_val, text->bv_len,
936 "txn_begin failed: %s (%d)",
937 mdb_strerror(rc), rc );
938 Debug( LDAP_DEBUG_ANY,
939 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
940 text->bv_val, 0, 0 );
947 op.o_tmpmemctx = NULL;
948 op.o_tmpmfuncs = &ch_mfuncs;
951 rc = mdb_id2entry_update( &op, txn, NULL, e );
953 snprintf( text->bv_val, text->bv_len,
954 "id2entry_update failed: err=%d", rc );
955 Debug( LDAP_DEBUG_ANY,
956 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
957 text->bv_val, 0, 0 );
963 rc = mdb_txn_commit( txn );
965 snprintf( text->bv_val, text->bv_len,
966 "txn_commit failed: %s (%d)",
967 mdb_strerror(rc), rc );
968 Debug( LDAP_DEBUG_ANY,
969 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
970 "%s\n", text->bv_val, 0, 0 );
975 mdb_txn_abort( txn );
976 snprintf( text->bv_val, text->bv_len,
977 "txn_aborted! %s (%d)",
978 mdb_strerror(rc), rc );
979 Debug( LDAP_DEBUG_ANY,
980 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
981 text->bv_val, 0, 0 );
991 mdb_tool_index_task( void *ctx, void *ptr )
993 int base = *(int *)ptr;
997 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
998 mdb_tool_index_tcount--;
999 if ( !mdb_tool_index_tcount )
1000 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1001 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
1002 &mdb_tool_index_mutex );
1003 if ( slapd_shutdown ) {
1004 mdb_tool_index_tcount--;
1005 if ( !mdb_tool_index_tcount )
1006 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1007 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1010 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1011 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( mdb_tool_ix_op,
1013 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
1019 #ifdef MDB_TOOL_IDL_CACHING
1021 mdb_tool_idl_cmp( const void *v1, const void *v2 )
1023 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
1026 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
1027 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1031 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrInfo *ai, mdb_tool_idl_cache *ic )
1033 mdb_tool_idl_cache_entry *ice;
1034 MDB_val key, data[2];
1038 /* Freshly allocated, ignore it */
1039 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1043 key.mv_data = ic->kstr.bv_val;
1044 key.mv_size = ic->kstr.bv_len;
1046 if ( ic->count > MDB_IDL_DB_SIZE ) {
1047 while ( ic->flags & WAS_FOUND ) {
1048 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1050 /* FIXME: find out why this happens */
1054 if ( ic->flags & WAS_RANGE ) {
1056 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1059 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1061 /* Store range hi */
1062 data[0].mv_data = &ic->last;
1063 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1065 /* Delete old data, replace with range */
1066 ic->first = *(ID *)data[0].mv_data;
1067 mdb_cursor_del( mc, MDB_NODUPDATA );
1071 if ( !(ic->flags & WAS_RANGE)) {
1072 /* range, didn't exist before */
1074 data[0].mv_size = sizeof(ID);
1075 data[0].mv_data = &nid;
1076 rc = mdb_cursor_put( mc, &key, data, 0 );
1078 data[0].mv_data = &ic->first;
1079 rc = mdb_cursor_put( mc, &key, data, 0 );
1081 data[0].mv_data = &ic->last;
1082 rc = mdb_cursor_put( mc, &key, data, 0 );
1093 data[0].mv_size = sizeof(ID);
1096 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1101 end = ic->count & (IDBLOCK-1);
1105 data[1].mv_size = end - i;
1106 data[0].mv_data = &ice->ids[i];
1108 rc = mdb_cursor_put( mc, &key, data, MDB_NODUPDATA|MDB_APPEND|MDB_MULTIPLE );
1110 if ( rc == MDB_KEYEXIST ) {
1119 ic->tail->next = ai->ai_flist;
1120 ai->ai_flist = ic->head;
1123 ic->head = ai->ai_clist;
1129 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai )
1135 mdb_cursor_open( txn, ai->ai_dbi, &mc );
1136 root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1138 rc = mdb_tool_idl_flush_one( mc, ai, root->avl_data );
1141 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1142 mdb_cursor_close( mc );
1148 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1150 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1152 unsigned int i, dbi;
1154 for ( i=0; i < mdb->mi_nattrs; i++ ) {
1155 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1156 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i] );
1157 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1158 mdb->mi_attrs[i]->ai_root = NULL;
1165 int mdb_tool_idl_add(
1168 struct berval *keys,
1172 mdb_tool_idl_cache *ic, itmp;
1173 mdb_tool_idl_cache_entry *ice;
1175 AttrInfo *ai = (AttrInfo *)mc;
1179 for (i=0; keys[i].bv_val; i++) {
1180 itmp.kstr = keys[i];
1181 ic = tavl_find( (Avlnode *)ai->ai_root, &itmp, mdb_tool_idl_cmp );
1183 /* No entry yet, create one */
1189 if ( ai->ai_clist ) {
1191 ai->ai_clist = ic->head;
1193 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1195 ic->kstr.bv_len = itmp.kstr.bv_len;
1196 ic->kstr.bv_val = (char *)(ic+1);
1197 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1198 ic->head = ic->tail = NULL;
1203 tavl_insert( (Avlnode **)&ai->ai_root, ic, mdb_tool_idl_cmp,
1206 /* load existing key count here */
1207 key.mv_size = keys[i].bv_len;
1208 key.mv_data = keys[i].bv_val;
1209 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1211 ic->flags |= WAS_FOUND;
1212 nid = *(ID *)data.mv_data;
1214 ic->count = MDB_IDL_DB_SIZE+1;
1215 ic->flags |= WAS_RANGE;
1219 mdb_cursor_count( mc, &count );
1222 ic->offset = count & (IDBLOCK-1);
1226 /* are we a range already? */
1227 if ( ic->count > MDB_IDL_DB_SIZE ) {
1230 /* Are we at the limit, and converting to a range? */
1231 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1233 ic->tail->next = ai->ai_flist;
1234 ai->ai_flist = ic->head;
1236 ic->head = ic->tail = NULL;
1241 /* No free block, create that too */
1242 lcount = ic->count & (IDBLOCK-1);
1243 if ( !ic->tail || lcount == 0) {
1244 if ( ai->ai_flist ) {
1246 ai->ai_flist = ice->next;
1248 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1254 ic->tail->next = ice;
1258 ice->ids[lcount-1] = 0;
1263 if (!lcount || ice->ids[lcount-1] != id)
1264 ice->ids[lcount] = id;
1270 #endif /* MDB_TOOL_IDL_CACHING */