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 static int mdb_dn2id_upgrade( BackendDB *be );
743 int mdb_tool_entry_reindex(
746 AttributeDescription **adv )
748 struct mdb_info *mi = (struct mdb_info *) be->be_private;
754 Debug( LDAP_DEBUG_ARGS,
755 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
757 assert( tool_base == NULL );
758 assert( tool_filter == NULL );
760 /* Special: do a dn2id upgrade */
761 if ( adv && adv[0] == slap_schema.si_ad_entryDN ) {
762 /* short-circuit tool_entry_next() */
763 mdb_cursor_get( cursor, &key, &data, MDB_LAST );
764 return mdb_dn2id_upgrade( be );
767 /* No indexes configured, nothing to do. Could return an
768 * error here to shortcut things.
774 /* Check for explicit list of attrs to index */
778 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
780 for ( n = 0; adv[n]; n++ ) ;
783 for ( i = 0; i < n; i++ ) {
784 AttributeDescription *ad = adv[i];
785 for ( j = i-1; j>=0; j--) {
786 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
793 for ( i = 0; adv[i]; i++ ) {
794 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
795 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
796 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
797 AttrInfo *ai = mi->mi_attrs[i];
798 mi->mi_attrs[i] = mi->mi_attrs[j];
799 mi->mi_attrs[j] = ai;
803 if ( j == mi->mi_nattrs ) {
804 Debug( LDAP_DEBUG_ANY,
805 LDAP_XSTRING(mdb_tool_entry_reindex)
806 ": no index configured for %s\n",
807 adv[i]->ad_cname.bv_val, 0, 0 );
815 e = mdb_tool_entry_get( be, id );
818 Debug( LDAP_DEBUG_ANY,
819 LDAP_XSTRING(mdb_tool_entry_reindex)
820 ": could not locate id=%ld\n",
826 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
828 Debug( LDAP_DEBUG_ANY,
829 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
830 "txn_begin failed: %s (%d)\n",
831 mdb_strerror(rc), rc, 0 );
836 if ( slapMode & SLAP_TRUNCATE_MODE ) {
838 for ( i=0; i < mi->mi_nattrs; i++ ) {
839 rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
841 Debug( LDAP_DEBUG_ANY,
842 LDAP_XSTRING(mdb_tool_entry_reindex)
843 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
844 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
845 mdb_strerror(rc), rc );
849 slapMode ^= SLAP_TRUNCATE_MODE;
853 * just (re)add them for now
854 * Use truncate mode to empty/reset index databases
857 Debug( LDAP_DEBUG_TRACE,
858 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
863 op.o_tmpmemctx = NULL;
864 op.o_tmpmfuncs = &ch_mfuncs;
866 rc = mdb_tool_index_add( &op, txi, e );
871 if ( mdb_writes >= mdb_writes_per_commit ) {
874 MDB_TOOL_IDL_FLUSH( be, txi );
875 rc = mdb_txn_commit( txi );
877 for ( i=0; i<mi->mi_nattrs; i++ )
878 mi->mi_attrs[i]->ai_cursor = NULL;
880 Debug( LDAP_DEBUG_ANY,
881 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
882 ": txn_commit failed: %s (%d)\n",
883 mdb_strerror(rc), rc, 0 );
886 mdb_cursor_close( cursor );
888 /* Must close the read txn to allow old pages to be reclaimed. */
889 mdb_txn_abort( txn );
890 /* and then reopen it so that tool_entry_next still works. */
891 mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &txn );
892 mdb_cursor_open( txn, mi->mi_id2entry, &cursor );
894 key.mv_size = sizeof(ID);
895 mdb_cursor_get( cursor, &key, NULL, MDB_SET );
901 mdb_cursor_close( cursor );
903 mdb_txn_abort( txi );
904 for ( i=0; i<mi->mi_nattrs; i++ )
905 mi->mi_attrs[i]->ai_cursor = NULL;
906 Debug( LDAP_DEBUG_ANY,
907 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
908 ": txn_aborted! err=%d\n",
913 mdb_entry_release( &op, e, 0 );
918 ID mdb_tool_entry_modify(
921 struct berval *text )
924 struct mdb_info *mdb;
928 assert( be != NULL );
929 assert( slapMode & SLAP_TOOL_MODE );
931 assert( text != NULL );
932 assert( text->bv_val != NULL );
933 assert( text->bv_val[0] == '\0' ); /* overconservative? */
935 assert ( e->e_id != NOID );
937 Debug( LDAP_DEBUG_TRACE,
938 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
939 (long) e->e_id, e->e_dn, 0 );
941 mdb = (struct mdb_info *) be->be_private;
944 mdb_cursor_close( cursor );
948 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
950 snprintf( text->bv_val, text->bv_len,
951 "txn_begin failed: %s (%d)",
952 mdb_strerror(rc), rc );
953 Debug( LDAP_DEBUG_ANY,
954 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
955 text->bv_val, 0, 0 );
962 op.o_tmpmemctx = NULL;
963 op.o_tmpmfuncs = &ch_mfuncs;
966 rc = mdb_id2entry_update( &op, txn, NULL, e );
968 snprintf( text->bv_val, text->bv_len,
969 "id2entry_update failed: err=%d", rc );
970 Debug( LDAP_DEBUG_ANY,
971 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
972 text->bv_val, 0, 0 );
978 rc = mdb_txn_commit( txn );
980 snprintf( text->bv_val, text->bv_len,
981 "txn_commit failed: %s (%d)",
982 mdb_strerror(rc), rc );
983 Debug( LDAP_DEBUG_ANY,
984 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
985 "%s\n", text->bv_val, 0, 0 );
990 mdb_txn_abort( txn );
991 snprintf( text->bv_val, text->bv_len,
992 "txn_aborted! %s (%d)",
993 mdb_strerror(rc), rc );
994 Debug( LDAP_DEBUG_ANY,
995 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
996 text->bv_val, 0, 0 );
1006 mdb_tool_index_task( void *ctx, void *ptr )
1008 int base = *(int *)ptr;
1012 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
1013 mdb_tool_index_tcount--;
1014 if ( !mdb_tool_index_tcount )
1015 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1016 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
1017 &mdb_tool_index_mutex );
1018 if ( slapd_shutdown ) {
1019 mdb_tool_index_tcount--;
1020 if ( !mdb_tool_index_tcount )
1021 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1022 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1025 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1026 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( mdb_tool_ix_op,
1028 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
1034 #ifdef MDB_TOOL_IDL_CACHING
1036 mdb_tool_idl_cmp( const void *v1, const void *v2 )
1038 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
1041 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
1042 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1046 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrInfo *ai, mdb_tool_idl_cache *ic )
1048 mdb_tool_idl_cache_entry *ice;
1049 MDB_val key, data[2];
1053 /* Freshly allocated, ignore it */
1054 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1058 key.mv_data = ic->kstr.bv_val;
1059 key.mv_size = ic->kstr.bv_len;
1061 if ( ic->count > MDB_IDL_DB_SIZE ) {
1062 while ( ic->flags & WAS_FOUND ) {
1063 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1065 /* FIXME: find out why this happens */
1069 if ( ic->flags & WAS_RANGE ) {
1071 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1074 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1076 /* Store range hi */
1077 data[0].mv_data = &ic->last;
1078 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1080 /* Delete old data, replace with range */
1081 ic->first = *(ID *)data[0].mv_data;
1082 mdb_cursor_del( mc, MDB_NODUPDATA );
1086 if ( !(ic->flags & WAS_RANGE)) {
1087 /* range, didn't exist before */
1089 data[0].mv_size = sizeof(ID);
1090 data[0].mv_data = &nid;
1091 rc = mdb_cursor_put( mc, &key, data, 0 );
1093 data[0].mv_data = &ic->first;
1094 rc = mdb_cursor_put( mc, &key, data, 0 );
1096 data[0].mv_data = &ic->last;
1097 rc = mdb_cursor_put( mc, &key, data, 0 );
1108 data[0].mv_size = sizeof(ID);
1111 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1116 end = ic->count & (IDBLOCK-1);
1120 data[1].mv_size = end - i;
1121 data[0].mv_data = &ice->ids[i];
1123 rc = mdb_cursor_put( mc, &key, data, MDB_NODUPDATA|MDB_APPEND|MDB_MULTIPLE );
1125 if ( rc == MDB_KEYEXIST ) {
1134 ic->tail->next = ai->ai_flist;
1135 ai->ai_flist = ic->head;
1138 ic->head = ai->ai_clist;
1144 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai )
1150 mdb_cursor_open( txn, ai->ai_dbi, &mc );
1151 root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1153 rc = mdb_tool_idl_flush_one( mc, ai, root->avl_data );
1156 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1157 mdb_cursor_close( mc );
1163 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1165 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1167 unsigned int i, dbi;
1169 for ( i=0; i < mdb->mi_nattrs; i++ ) {
1170 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1171 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i] );
1172 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1173 mdb->mi_attrs[i]->ai_root = NULL;
1180 int mdb_tool_idl_add(
1183 struct berval *keys,
1187 mdb_tool_idl_cache *ic, itmp;
1188 mdb_tool_idl_cache_entry *ice;
1190 AttrInfo *ai = (AttrInfo *)mc;
1194 for (i=0; keys[i].bv_val; i++) {
1195 itmp.kstr = keys[i];
1196 ic = tavl_find( (Avlnode *)ai->ai_root, &itmp, mdb_tool_idl_cmp );
1198 /* No entry yet, create one */
1204 if ( ai->ai_clist ) {
1206 ai->ai_clist = ic->head;
1208 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1210 ic->kstr.bv_len = itmp.kstr.bv_len;
1211 ic->kstr.bv_val = (char *)(ic+1);
1212 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1213 ic->head = ic->tail = NULL;
1218 tavl_insert( (Avlnode **)&ai->ai_root, ic, mdb_tool_idl_cmp,
1221 /* load existing key count here */
1222 key.mv_size = keys[i].bv_len;
1223 key.mv_data = keys[i].bv_val;
1224 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1226 ic->flags |= WAS_FOUND;
1227 nid = *(ID *)data.mv_data;
1229 ic->count = MDB_IDL_DB_SIZE+1;
1230 ic->flags |= WAS_RANGE;
1234 mdb_cursor_count( mc, &count );
1237 ic->offset = count & (IDBLOCK-1);
1241 /* are we a range already? */
1242 if ( ic->count > MDB_IDL_DB_SIZE ) {
1245 /* Are we at the limit, and converting to a range? */
1246 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1248 ic->tail->next = ai->ai_flist;
1249 ai->ai_flist = ic->head;
1251 ic->head = ic->tail = NULL;
1256 /* No free block, create that too */
1257 lcount = ic->count & (IDBLOCK-1);
1258 if ( !ic->tail || lcount == 0) {
1259 if ( ai->ai_flist ) {
1261 ai->ai_flist = ice->next;
1263 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1269 ic->tail->next = ice;
1273 ice->ids[lcount-1] = 0;
1278 if (!lcount || ice->ids[lcount-1] != id)
1279 ice->ids[lcount] = id;
1285 #endif /* MDB_TOOL_IDL_CACHING */
1287 /* Upgrade from pre 2.4.34 dn2id format */
1289 #include <ac/unistd.h>
1290 #include <lutil_meter.h>
1292 #define STACKSIZ 2048
1294 typedef struct rec {
1301 mdb_dn2id_upgrade( BackendDB *be ) {
1302 struct mdb_info *mi = (struct mdb_info *) be->be_private;
1304 MDB_cursor *mc = NULL;
1307 int rc, writes=0, depth=0;
1308 int enable_meter = 0;
1309 ID id = 0, *num, count = 0;
1311 lutil_meter_t meter;
1313 if (!(mi->mi_flags & MDB_NEED_UPGRADE)) {
1314 Debug( LDAP_DEBUG_ANY, "database %s: No upgrade needed.\n",
1315 be->be_suffix[0].bv_val, 0, 0 );
1322 mdb_stat(mdb_cursor_txn(cursor), mi->mi_dbis[MDB_ID2ENTRY], &st);
1323 if (!st.ms_entries) {
1324 /* Empty DB, nothing to upgrade? */
1328 enable_meter = !lutil_meter_open(&meter,
1329 &lutil_meter_text_display,
1330 &lutil_meter_linear_estimator,
1334 num = ch_malloc(STACKSIZ * (sizeof(ID) + sizeof(rec)));
1335 stack = (rec *)(num + STACKSIZ);
1337 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1339 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin failed, %s (%d)\n",
1340 mdb_strerror(rc), rc, 0 );
1343 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1345 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open failed, %s (%d)\n",
1346 mdb_strerror(rc), rc, 0 );
1350 key.mv_size = sizeof(ID);
1351 /* post-order depth-first update */
1358 stack[depth].id = id;
1359 rc = mdb_cursor_get(mc, &key, &data, MDB_SET);
1361 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get failed, %s (%d)\n",
1362 mdb_strerror(rc), rc, 0 );
1367 rc = mdb_cursor_count(mc, &dkids);
1369 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_count failed, %s (%d)\n",
1370 mdb_strerror(rc), rc, 0 );
1374 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1376 ptr = data.mv_data + data.mv_size - sizeof(ID);
1377 memcpy(&id, ptr, sizeof(ID));
1379 memcpy(stack[depth].rdn, data.mv_data, data.mv_size);
1380 stack[depth].len = data.mv_size;
1385 /* pop: write updated count, advance to next node */
1387 /* update superior counts */
1389 num[depth-1] += num[depth];
1392 id = stack[depth-1].id;
1393 data.mv_data = stack[depth].rdn;
1394 data.mv_size = stack[depth].len;
1395 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1397 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(BOTH) failed, %s (%d)\n",
1398 mdb_strerror(rc), rc, 0 );
1401 data.mv_data = stack[depth].rdn;
1402 ptr = data.mv_data + data.mv_size;
1403 memcpy(ptr, &num[depth], sizeof(ID));
1404 data.mv_size += sizeof(ID);
1405 rc = mdb_cursor_del(mc, 0);
1407 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_del failed, %s (%d)\n",
1408 mdb_strerror(rc), rc, 0 );
1411 rc = mdb_cursor_put(mc, &key, &data, 0);
1413 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_put failed, %s (%d)\n",
1414 mdb_strerror(rc), rc, 0 );
1420 lutil_meter_update(&meter, count, 0);
1425 len = (ptr[0] & 0x7f) << 8 | ptr[1];
1426 printf("ID: %zu, %zu, %.*s\n", stack[depth].id, num[depth], len, ptr+2);
1430 if (writes == 1000) {
1431 mdb_cursor_close(mc);
1432 rc = mdb_txn_commit(mt);
1434 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit failed, %s (%d)\n",
1435 mdb_strerror(rc), rc, 0 );
1438 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1440 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin(2) failed, %s (%d)\n",
1441 mdb_strerror(rc), rc, 0 );
1444 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1446 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open(2) failed, %s (%d)\n",
1447 mdb_strerror(rc), rc, 0 );
1450 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1452 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(2) failed, %s (%d)\n",
1453 mdb_strerror(rc), rc, 0 );
1460 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1470 mdb_cursor_close(mc);
1473 r2 = mdb_txn_commit(mt);
1475 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit(2) failed, %s (%d)\n",
1476 mdb_strerror(r2), r2, 0 );
1483 lutil_meter_update(&meter, count, 1);
1484 lutil_meter_close(&meter);