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 );
169 MDB_TOOL_IDL_FLUSH( be, txn );
170 if (( rc = mdb_txn_commit( txn ))) {
171 Debug( LDAP_DEBUG_ANY,
172 LDAP_XSTRING(mdb_tool_entry_close) ": database %s: "
173 "txn_commit failed: %s (%d)\n",
174 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
182 fprintf( stderr, "Error, entries missing!\n");
183 for (i=0; i<nholes; i++) {
184 fprintf(stderr, " entry %ld: %s\n",
185 holes[i].id, holes[i].dn.bv_val);
195 mdb_tool_entry_first_x(
205 return mdb_tool_entry_next( be );
208 ID mdb_tool_entry_next(
213 struct mdb_info *mdb;
215 assert( be != NULL );
216 assert( slapMode & SLAP_TOOL_MODE );
218 mdb = (struct mdb_info *) be->be_private;
219 assert( mdb != NULL );
222 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn );
225 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
227 mdb_txn_abort( txn );
233 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
239 previd = *(ID *)key.mv_data;
245 if ( tool_filter || tool_base ) {
246 static Operation op = {0};
247 static Opheader ohdr = {0};
251 op.o_tmpmemctx = NULL;
252 op.o_tmpmfuncs = &ch_mfuncs;
254 if ( tool_next_entry ) {
255 mdb_entry_release( &op, tool_next_entry, 0 );
256 tool_next_entry = NULL;
259 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
260 if ( rc == LDAP_NO_SUCH_OBJECT ) {
264 assert( tool_next_entry != NULL );
266 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
268 mdb_entry_release( &op, tool_next_entry, 0 );
269 tool_next_entry = NULL;
277 ID mdb_tool_dn2id_get(
282 struct mdb_info *mdb;
288 if ( BER_BVISEMPTY(dn) )
291 mdb = (struct mdb_info *) be->be_private;
294 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
295 MDB_RDONLY : 0, &txn );
302 op.o_tmpmemctx = NULL;
303 op.o_tmpmfuncs = &ch_mfuncs;
305 rc = mdb_dn2id( &op, txn, NULL, dn, &id, NULL, NULL, NULL );
306 if ( rc == MDB_NOTFOUND )
313 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
319 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
322 assert( be != NULL );
323 assert( slapMode & SLAP_TOOL_MODE );
325 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
326 *ep = tool_next_entry;
327 tool_next_entry = NULL;
331 if ( id != previd ) {
332 key.mv_size = sizeof(ID);
334 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
340 if ( !data.mv_size ) {
341 rc = LDAP_NO_SUCH_OBJECT;
347 op.o_tmpmemctx = NULL;
348 op.o_tmpmfuncs = &ch_mfuncs;
349 if ( slapMode & SLAP_TOOL_READONLY ) {
350 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
354 mdb_entry_return( &op, e );
359 if ( tool_base != NULL ) {
360 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
361 ch_free( dn.bv_val );
362 ch_free( ndn.bv_val );
363 rc = LDAP_NO_SUCH_OBJECT;
368 rc = mdb_entry_decode( &op, &data, &e );
370 if ( !BER_BVISNULL( &dn )) {
374 e->e_name.bv_val = NULL;
375 e->e_nname.bv_val = NULL;
387 mdb_tool_entry_get( BackendDB *be, ID id )
393 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
394 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
395 (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &txn );
400 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
401 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
403 mdb_txn_abort( txn );
408 (void)mdb_tool_entry_get_int( be, id, &e );
412 static int mdb_tool_next_id(
419 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
420 struct berval dn = e->e_name;
421 struct berval ndn = e->e_nname;
422 struct berval pdn, npdn, nmatched;
426 if (ndn.bv_len == 0) {
431 rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, NULL, &nmatched );
432 if ( rc == MDB_NOTFOUND ) {
433 if ( !be_issuffix( op->o_bd, &ndn ) ) {
435 dnParent( &ndn, &npdn );
436 if ( nmatched.bv_len != npdn.bv_len ) {
437 dnParent( &dn, &pdn );
440 rc = mdb_tool_next_id( op, tid, e, text, 1 );
446 /* If parent didn't exist, it was created just now
447 * and its ID is now in e->e_id. Make sure the current
448 * entry gets added under the new parent ID.
450 if ( eid != e->e_id ) {
457 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
459 snprintf( text->bv_val, text->bv_len,
460 "next_id failed: %s (%d)",
461 mdb_strerror(rc), rc );
462 Debug( LDAP_DEBUG_ANY,
463 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
466 rc = mdb_dn2id_add( op, mcp, mcd, pid, 1, e );
468 snprintf( text->bv_val, text->bv_len,
469 "dn2id_add failed: %s (%d)",
470 mdb_strerror(rc), rc );
471 Debug( LDAP_DEBUG_ANY,
472 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
475 if ( nholes == nhmax - 1 ) {
476 if ( holes == hbuf ) {
477 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
478 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
480 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
484 ber_dupbv( &holes[nholes].dn, &ndn );
485 holes[nholes++].id = e->e_id;
486 key.mv_size = sizeof(ID);
487 key.mv_data = &e->e_id;
490 rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
491 if ( rc == MDB_KEYEXIST )
494 snprintf( text->bv_val, text->bv_len,
495 "dummy id2entry add failed: %s (%d)",
496 mdb_strerror(rc), rc );
497 Debug( LDAP_DEBUG_ANY,
498 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
501 } else if ( !hole ) {
506 for ( i=0; i<nholes; i++) {
507 if ( holes[i].id == e->e_id ) {
508 free(holes[i].dn.bv_val);
509 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
513 } else if ( holes[i].id > e->e_id ) {
527 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
529 if ( !mdb->mi_nattrs )
532 if ( mdb_tool_threads > 1 ) {
537 ir = mdb_tool_index_rec;
538 for (i=0; i<mdb->mi_nattrs; i++)
539 ir[i].ir_attrs = NULL;
541 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
542 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
543 &a->a_desc->ad_tags, ir );
547 for (i=0; i<mdb->mi_nattrs; i++) {
550 rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
551 &ir[i].ir_ai->ai_cursor );
555 mdb_tool_ix_id = e->e_id;
557 mdb_tool_ix_txn = txn;
558 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
559 /* Wait for all threads to be ready */
560 while ( mdb_tool_index_tcount ) {
561 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
562 &mdb_tool_index_mutex );
565 for ( i=1; i<mdb_tool_threads; i++ )
566 mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
567 mdb_tool_index_tcount = mdb_tool_threads - 1;
568 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
569 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
571 rc = mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
574 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
575 for ( i=1; i<mdb_tool_threads; i++ ) {
576 if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
577 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
578 &mdb_tool_index_mutex );
582 if ( mdb_tool_index_rec[i].ir_i ) {
583 rc = mdb_tool_index_rec[i].ir_i;
587 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
591 return mdb_index_entry_add( op, txn, e );
595 ID mdb_tool_entry_put(
598 struct berval *text )
601 struct mdb_info *mdb;
605 assert( be != NULL );
606 assert( slapMode & SLAP_TOOL_MODE );
608 assert( text != NULL );
609 assert( text->bv_val != NULL );
610 assert( text->bv_val[0] == '\0' ); /* overconservative? */
612 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
613 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
615 mdb = (struct mdb_info *) be->be_private;
618 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
620 snprintf( text->bv_val, text->bv_len,
621 "txn_begin failed: %s (%d)",
622 mdb_strerror(rc), rc );
623 Debug( LDAP_DEBUG_ANY,
624 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
625 text->bv_val, 0, 0 );
628 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &idcursor );
630 snprintf( text->bv_val, text->bv_len,
631 "cursor_open failed: %s (%d)",
632 mdb_strerror(rc), rc );
633 Debug( LDAP_DEBUG_ANY,
634 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
635 text->bv_val, 0, 0 );
638 if ( !mdb->mi_nextid ) {
640 mdb_next_id( be, idcursor, &dummy );
642 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcp );
644 snprintf( text->bv_val, text->bv_len,
645 "cursor_open failed: %s (%d)",
646 mdb_strerror(rc), rc );
647 Debug( LDAP_DEBUG_ANY,
648 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
649 text->bv_val, 0, 0 );
652 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
654 snprintf( text->bv_val, text->bv_len,
655 "cursor_open failed: %s (%d)",
656 mdb_strerror(rc), rc );
657 Debug( LDAP_DEBUG_ANY,
658 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
659 text->bv_val, 0, 0 );
666 op.o_tmpmemctx = NULL;
667 op.o_tmpmfuncs = &ch_mfuncs;
669 /* add dn2id indices */
670 rc = mdb_tool_next_id( &op, txn, e, text, 0 );
675 rc = mdb_tool_index_add( &op, txn, e );
677 snprintf( text->bv_val, text->bv_len,
678 "index_entry_add failed: err=%d", rc );
679 Debug( LDAP_DEBUG_ANY,
680 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
681 text->bv_val, 0, 0 );
687 rc = mdb_id2entry_add( &op, txn, idcursor, e );
689 snprintf( text->bv_val, text->bv_len,
690 "id2entry_add failed: err=%d", rc );
691 Debug( LDAP_DEBUG_ANY,
692 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
693 text->bv_val, 0, 0 );
700 if ( mdb_writes >= mdb_writes_per_commit ) {
702 MDB_TOOL_IDL_FLUSH( be, txn );
703 rc = mdb_txn_commit( txn );
704 for ( i=0; i<mdb->mi_nattrs; i++ )
705 mdb->mi_attrs[i]->ai_cursor = NULL;
710 snprintf( text->bv_val, text->bv_len,
711 "txn_commit failed: %s (%d)",
712 mdb_strerror(rc), rc );
713 Debug( LDAP_DEBUG_ANY,
714 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
715 text->bv_val, 0, 0 );
722 mdb_txn_abort( txn );
725 for ( i=0; i<mdb->mi_nattrs; i++ )
726 mdb->mi_attrs[i]->ai_cursor = NULL;
728 snprintf( text->bv_val, text->bv_len,
729 "txn_aborted! %s (%d)",
730 rc == LDAP_OTHER ? "Internal error" :
731 mdb_strerror(rc), rc );
732 Debug( LDAP_DEBUG_ANY,
733 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
734 text->bv_val, 0, 0 );
741 int mdb_tool_entry_reindex(
744 AttributeDescription **adv )
746 struct mdb_info *mi = (struct mdb_info *) be->be_private;
752 Debug( LDAP_DEBUG_ARGS,
753 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
755 assert( tool_base == NULL );
756 assert( tool_filter == NULL );
758 /* No indexes configured, nothing to do. Could return an
759 * error here to shortcut things.
765 /* Check for explicit list of attrs to index */
769 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
771 for ( n = 0; adv[n]; n++ ) ;
774 for ( i = 0; i < n; i++ ) {
775 AttributeDescription *ad = adv[i];
776 for ( j = i-1; j>=0; j--) {
777 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
784 for ( i = 0; adv[i]; i++ ) {
785 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
786 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
787 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
788 AttrInfo *ai = mi->mi_attrs[i];
789 mi->mi_attrs[i] = mi->mi_attrs[j];
790 mi->mi_attrs[j] = ai;
794 if ( j == mi->mi_nattrs ) {
795 Debug( LDAP_DEBUG_ANY,
796 LDAP_XSTRING(mdb_tool_entry_reindex)
797 ": no index configured for %s\n",
798 adv[i]->ad_cname.bv_val, 0, 0 );
806 e = mdb_tool_entry_get( be, id );
809 Debug( LDAP_DEBUG_ANY,
810 LDAP_XSTRING(mdb_tool_entry_reindex)
811 ": could not locate id=%ld\n",
817 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
819 Debug( LDAP_DEBUG_ANY,
820 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
821 "txn_begin failed: %s (%d)\n",
822 mdb_strerror(rc), rc, 0 );
827 if ( slapMode & SLAP_TRUNCATE_MODE ) {
829 for ( i=0; i < mi->mi_nattrs; i++ ) {
830 rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
832 Debug( LDAP_DEBUG_ANY,
833 LDAP_XSTRING(mdb_tool_entry_reindex)
834 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
835 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
836 mdb_strerror(rc), rc );
840 slapMode ^= SLAP_TRUNCATE_MODE;
844 * just (re)add them for now
845 * Use truncate mode to empty/reset index databases
848 Debug( LDAP_DEBUG_TRACE,
849 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
854 op.o_tmpmemctx = NULL;
855 op.o_tmpmfuncs = &ch_mfuncs;
857 rc = mdb_tool_index_add( &op, txi, e );
862 if ( mdb_writes >= mdb_writes_per_commit ) {
865 MDB_TOOL_IDL_FLUSH( be, txi );
866 rc = mdb_txn_commit( txi );
868 for ( i=0; i<mi->mi_nattrs; i++ )
869 mi->mi_attrs[i]->ai_cursor = NULL;
871 Debug( LDAP_DEBUG_ANY,
872 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
873 ": txn_commit failed: %s (%d)\n",
874 mdb_strerror(rc), rc, 0 );
877 mdb_cursor_close( cursor );
879 /* Must close the read txn to allow old pages to be reclaimed. */
880 mdb_txn_abort( txn );
881 /* and then reopen it so that tool_entry_next still works. */
882 mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &txn );
883 mdb_cursor_open( txn, mi->mi_id2entry, &cursor );
885 key.mv_size = sizeof(ID);
886 mdb_cursor_get( cursor, &key, NULL, MDB_SET );
892 mdb_cursor_close( cursor );
894 mdb_txn_abort( txi );
895 for ( i=0; i<mi->mi_nattrs; i++ )
896 mi->mi_attrs[i]->ai_cursor = NULL;
897 Debug( LDAP_DEBUG_ANY,
898 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
899 ": txn_aborted! err=%d\n",
904 mdb_entry_release( &op, e, 0 );
909 ID mdb_tool_entry_modify(
912 struct berval *text )
915 struct mdb_info *mdb;
919 assert( be != NULL );
920 assert( slapMode & SLAP_TOOL_MODE );
922 assert( text != NULL );
923 assert( text->bv_val != NULL );
924 assert( text->bv_val[0] == '\0' ); /* overconservative? */
926 assert ( e->e_id != NOID );
928 Debug( LDAP_DEBUG_TRACE,
929 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
930 (long) e->e_id, e->e_dn, 0 );
932 mdb = (struct mdb_info *) be->be_private;
935 mdb_cursor_close( cursor );
939 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
941 snprintf( text->bv_val, text->bv_len,
942 "txn_begin failed: %s (%d)",
943 mdb_strerror(rc), rc );
944 Debug( LDAP_DEBUG_ANY,
945 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
946 text->bv_val, 0, 0 );
953 op.o_tmpmemctx = NULL;
954 op.o_tmpmfuncs = &ch_mfuncs;
957 rc = mdb_id2entry_update( &op, txn, NULL, e );
959 snprintf( text->bv_val, text->bv_len,
960 "id2entry_update failed: err=%d", rc );
961 Debug( LDAP_DEBUG_ANY,
962 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
963 text->bv_val, 0, 0 );
969 rc = mdb_txn_commit( txn );
971 snprintf( text->bv_val, text->bv_len,
972 "txn_commit failed: %s (%d)",
973 mdb_strerror(rc), rc );
974 Debug( LDAP_DEBUG_ANY,
975 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
976 "%s\n", text->bv_val, 0, 0 );
981 mdb_txn_abort( txn );
982 snprintf( text->bv_val, text->bv_len,
983 "txn_aborted! %s (%d)",
984 mdb_strerror(rc), rc );
985 Debug( LDAP_DEBUG_ANY,
986 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
987 text->bv_val, 0, 0 );
997 mdb_tool_index_task( void *ctx, void *ptr )
999 int base = *(int *)ptr;
1003 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
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_cond_wait( &mdb_tool_index_cond_work,
1008 &mdb_tool_index_mutex );
1009 if ( slapd_shutdown ) {
1010 mdb_tool_index_tcount--;
1011 if ( !mdb_tool_index_tcount )
1012 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1013 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1016 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1017 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( mdb_tool_ix_op,
1019 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
1025 #ifdef MDB_TOOL_IDL_CACHING
1027 mdb_tool_idl_cmp( const void *v1, const void *v2 )
1029 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
1032 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
1033 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1037 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrInfo *ai, mdb_tool_idl_cache *ic )
1039 mdb_tool_idl_cache_entry *ice;
1040 MDB_val key, data[2];
1044 /* Freshly allocated, ignore it */
1045 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1049 key.mv_data = ic->kstr.bv_val;
1050 key.mv_size = ic->kstr.bv_len;
1052 if ( ic->count > MDB_IDL_DB_SIZE ) {
1053 while ( ic->flags & WAS_FOUND ) {
1054 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1056 /* FIXME: find out why this happens */
1060 if ( ic->flags & WAS_RANGE ) {
1062 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1065 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1067 /* Store range hi */
1068 data[0].mv_data = &ic->last;
1069 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1071 /* Delete old data, replace with range */
1072 ic->first = *(ID *)data[0].mv_data;
1073 mdb_cursor_del( mc, MDB_NODUPDATA );
1077 if ( !(ic->flags & WAS_RANGE)) {
1078 /* range, didn't exist before */
1080 data[0].mv_size = sizeof(ID);
1081 data[0].mv_data = &nid;
1082 rc = mdb_cursor_put( mc, &key, data, 0 );
1084 data[0].mv_data = &ic->first;
1085 rc = mdb_cursor_put( mc, &key, data, 0 );
1087 data[0].mv_data = &ic->last;
1088 rc = mdb_cursor_put( mc, &key, data, 0 );
1099 data[0].mv_size = sizeof(ID);
1102 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1107 end = ic->count & (IDBLOCK-1);
1111 data[1].mv_size = end - i;
1112 data[0].mv_data = &ice->ids[i];
1114 rc = mdb_cursor_put( mc, &key, data, MDB_NODUPDATA|MDB_APPEND|MDB_MULTIPLE );
1116 if ( rc == MDB_KEYEXIST ) {
1125 ic->tail->next = ai->ai_flist;
1126 ai->ai_flist = ic->head;
1129 ic->head = ai->ai_clist;
1135 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai )
1141 mdb_cursor_open( txn, ai->ai_dbi, &mc );
1142 root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1144 rc = mdb_tool_idl_flush_one( mc, ai, root->avl_data );
1147 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1148 mdb_cursor_close( mc );
1154 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1156 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1158 unsigned int i, dbi;
1160 for ( i=0; i < mdb->mi_nattrs; i++ ) {
1161 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1162 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i] );
1163 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1164 mdb->mi_attrs[i]->ai_root = NULL;
1171 int mdb_tool_idl_add(
1174 struct berval *keys,
1178 mdb_tool_idl_cache *ic, itmp;
1179 mdb_tool_idl_cache_entry *ice;
1181 AttrInfo *ai = (AttrInfo *)mc;
1185 for (i=0; keys[i].bv_val; i++) {
1186 itmp.kstr = keys[i];
1187 ic = tavl_find( (Avlnode *)ai->ai_root, &itmp, mdb_tool_idl_cmp );
1189 /* No entry yet, create one */
1195 if ( ai->ai_clist ) {
1197 ai->ai_clist = ic->head;
1199 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1201 ic->kstr.bv_len = itmp.kstr.bv_len;
1202 ic->kstr.bv_val = (char *)(ic+1);
1203 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1204 ic->head = ic->tail = NULL;
1209 tavl_insert( (Avlnode **)&ai->ai_root, ic, mdb_tool_idl_cmp,
1212 /* load existing key count here */
1213 key.mv_size = keys[i].bv_len;
1214 key.mv_data = keys[i].bv_val;
1215 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1217 ic->flags |= WAS_FOUND;
1218 nid = *(ID *)data.mv_data;
1220 ic->count = MDB_IDL_DB_SIZE+1;
1221 ic->flags |= WAS_RANGE;
1225 mdb_cursor_count( mc, &count );
1228 ic->offset = count & (IDBLOCK-1);
1232 /* are we a range already? */
1233 if ( ic->count > MDB_IDL_DB_SIZE ) {
1236 /* Are we at the limit, and converting to a range? */
1237 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1239 ic->tail->next = ai->ai_flist;
1240 ai->ai_flist = ic->head;
1242 ic->head = ic->tail = NULL;
1247 /* No free block, create that too */
1248 lcount = ic->count & (IDBLOCK-1);
1249 if ( !ic->tail || lcount == 0) {
1250 if ( ai->ai_flist ) {
1252 ai->ai_flist = ice->next;
1254 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1260 ic->tail->next = ice;
1264 ice->ids[lcount-1] = 0;
1269 if (!lcount || ice->ids[lcount-1] != id)
1270 ice->ids[lcount] = id;
1276 #endif /* MDB_TOOL_IDL_CACHING */