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 BackendDB *mdb_tool_ix_be;
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 AttrIxInfo **mdb_tool_axinfo;
80 static struct mdb_info *mdb_tool_info;
81 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
82 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
83 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
84 static void * mdb_tool_index_task( void *ctx, void *ptr );
86 static int mdb_writes, mdb_writes_per_commit;
88 /* Number of ops per commit in Quick mode.
89 * Batching speeds writes overall, but too large a
90 * batch will fail with MDB_TXN_FULL.
92 #ifndef MDB_WRITES_PER_COMMIT
93 #define MDB_WRITES_PER_COMMIT 500
97 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
99 int mdb_tool_entry_open(
100 BackendDB *be, int mode )
102 /* In Quick mode, commit once per 500 entries */
104 if ( slapMode & SLAP_TOOL_QUICK )
105 mdb_writes_per_commit = MDB_WRITES_PER_COMMIT;
107 mdb_writes_per_commit = 1;
109 #ifdef MDB_TOOL_IDL_CACHING /* threaded indexing has no performance advantage */
110 /* Set up for threaded slapindex */
111 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
112 if ( !mdb_tool_info ) {
113 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
114 ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
115 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
116 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
117 if ( mdb->mi_nattrs ) {
119 mdb_tool_threads = slap_tool_thread_max - 1;
120 if ( mdb_tool_threads > 1 ) {
121 mdb_tool_index_rec = ch_calloc( mdb->mi_nattrs, sizeof( IndexRec ));
122 mdb_tool_axinfo = ch_calloc( mdb_tool_threads, sizeof( AttrIxInfo* ) +
123 sizeof( AttrIxInfo ));
124 mdb_tool_axinfo[0] = (AttrIxInfo *)(mdb_tool_axinfo + mdb_tool_threads);
125 for (i=1; i<mdb_tool_threads; i++)
126 mdb_tool_axinfo[i] = mdb_tool_axinfo[i-1]+1;
127 mdb_tool_index_tcount = mdb_tool_threads - 1;
129 for (i=1; i<mdb_tool_threads; i++) {
130 int *ptr = ch_malloc( sizeof( int ));
132 ldap_pvt_thread_pool_submit( &connection_pool,
133 mdb_tool_index_task, ptr );
145 int mdb_tool_entry_close(
148 #ifdef MDB_TOOL_IDL_CACHING
149 if ( mdb_tool_info ) {
152 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
154 /* There might still be some threads starting */
155 while ( mdb_tool_index_tcount > 0 ) {
156 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
157 &mdb_tool_index_mutex );
160 mdb_tool_index_tcount = mdb_tool_threads - 1;
161 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
163 /* Make sure all threads are stopped */
164 while ( mdb_tool_index_tcount > 0 ) {
165 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
166 &mdb_tool_index_mutex );
168 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
170 mdb_tool_info = NULL;
172 ch_free( mdb_tool_index_rec );
173 mdb_tool_index_tcount = mdb_tool_threads - 1;
175 MDB_TOOL_IDL_FLUSH( be, txn );
176 for (i=0; i<mdb_tool_threads; i++) {
177 mdb_tool_idl_cache *ic;
178 mdb_tool_idl_cache_entry *ice;
179 while ((ic = mdb_tool_axinfo[i]->ai_clist)) {
180 mdb_tool_axinfo[i]->ai_clist = ic->head;
183 while ((ice = mdb_tool_axinfo[i]->ai_flist)) {
184 mdb_tool_axinfo[i]->ai_flist = ice->next;
192 mdb_cursor_close( idcursor );
196 mdb_cursor_close( cursor );
201 if (( rc = mdb_txn_commit( txn ))) {
202 Debug( LDAP_DEBUG_ANY,
203 LDAP_XSTRING(mdb_tool_entry_close) ": database %s: "
204 "txn_commit failed: %s (%d)\n",
205 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
213 fprintf( stderr, "Error, entries missing!\n");
214 for (i=0; i<nholes; i++) {
215 fprintf(stderr, " entry %ld: %s\n",
216 holes[i].id, holes[i].dn.bv_val);
226 mdb_tool_entry_first_x(
236 return mdb_tool_entry_next( be );
239 ID mdb_tool_entry_next(
244 struct mdb_info *mdb;
246 assert( be != NULL );
247 assert( slapMode & SLAP_TOOL_MODE );
249 mdb = (struct mdb_info *) be->be_private;
250 assert( mdb != NULL );
253 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn );
256 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
258 mdb_txn_abort( txn );
264 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
270 previd = *(ID *)key.mv_data;
276 if ( tool_filter || tool_base ) {
277 static Operation op = {0};
278 static Opheader ohdr = {0};
282 op.o_tmpmemctx = NULL;
283 op.o_tmpmfuncs = &ch_mfuncs;
285 if ( tool_next_entry ) {
286 mdb_entry_release( &op, tool_next_entry, 0 );
287 tool_next_entry = NULL;
290 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
291 if ( rc == LDAP_NO_SUCH_OBJECT ) {
295 assert( tool_next_entry != NULL );
297 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
299 mdb_entry_release( &op, tool_next_entry, 0 );
300 tool_next_entry = NULL;
308 ID mdb_tool_dn2id_get(
313 struct mdb_info *mdb;
319 if ( BER_BVISEMPTY(dn) )
322 mdb = (struct mdb_info *) be->be_private;
325 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
326 MDB_RDONLY : 0, &txn );
333 op.o_tmpmemctx = NULL;
334 op.o_tmpmfuncs = &ch_mfuncs;
336 rc = mdb_dn2id( &op, txn, NULL, dn, &id, NULL, NULL, NULL );
337 if ( rc == MDB_NOTFOUND )
344 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
350 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
353 assert( be != NULL );
354 assert( slapMode & SLAP_TOOL_MODE );
356 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
357 *ep = tool_next_entry;
358 tool_next_entry = NULL;
362 if ( id != previd ) {
363 key.mv_size = sizeof(ID);
365 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
371 if ( !data.mv_size ) {
372 rc = LDAP_NO_SUCH_OBJECT;
378 op.o_tmpmemctx = NULL;
379 op.o_tmpmfuncs = &ch_mfuncs;
380 if ( slapMode & SLAP_TOOL_READONLY ) {
381 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
385 mdb_entry_return( &op, e );
390 if ( tool_base != NULL ) {
391 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
392 ch_free( dn.bv_val );
393 ch_free( ndn.bv_val );
394 rc = LDAP_NO_SUCH_OBJECT;
399 rc = mdb_entry_decode( &op, &data, &e );
401 if ( !BER_BVISNULL( &dn )) {
405 e->e_name.bv_val = NULL;
406 e->e_nname.bv_val = NULL;
418 mdb_tool_entry_get( BackendDB *be, ID id )
424 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
425 rc = mdb_txn_begin( mdb->mi_dbenv, NULL,
426 (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &txn );
431 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
432 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
434 mdb_txn_abort( txn );
439 (void)mdb_tool_entry_get_int( be, id, &e );
443 static int mdb_tool_next_id(
450 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
451 struct berval dn = e->e_name;
452 struct berval ndn = e->e_nname;
453 struct berval pdn, npdn, nmatched;
457 if (ndn.bv_len == 0) {
462 rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, NULL, &nmatched );
463 if ( rc == MDB_NOTFOUND ) {
464 if ( !be_issuffix( op->o_bd, &ndn ) ) {
466 dnParent( &ndn, &npdn );
467 if ( nmatched.bv_len != npdn.bv_len ) {
468 dnParent( &dn, &pdn );
471 rc = mdb_tool_next_id( op, tid, e, text, 1 );
477 /* If parent didn't exist, it was created just now
478 * and its ID is now in e->e_id. Make sure the current
479 * entry gets added under the new parent ID.
481 if ( eid != e->e_id ) {
488 rc = mdb_next_id( op->o_bd, idcursor, &e->e_id );
490 snprintf( text->bv_val, text->bv_len,
491 "next_id failed: %s (%d)",
492 mdb_strerror(rc), rc );
493 Debug( LDAP_DEBUG_ANY,
494 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
497 rc = mdb_dn2id_add( op, mcp, mcd, pid, 1, 1, e );
499 snprintf( text->bv_val, text->bv_len,
500 "dn2id_add failed: %s (%d)",
501 mdb_strerror(rc), rc );
502 Debug( LDAP_DEBUG_ANY,
503 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
506 if ( nholes == nhmax - 1 ) {
507 if ( holes == hbuf ) {
508 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
509 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
511 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
515 ber_dupbv( &holes[nholes].dn, &ndn );
516 holes[nholes++].id = e->e_id;
517 key.mv_size = sizeof(ID);
518 key.mv_data = &e->e_id;
521 rc = mdb_cursor_put( idcursor, &key, &data, MDB_NOOVERWRITE );
522 if ( rc == MDB_KEYEXIST )
525 snprintf( text->bv_val, text->bv_len,
526 "dummy id2entry add failed: %s (%d)",
527 mdb_strerror(rc), rc );
528 Debug( LDAP_DEBUG_ANY,
529 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
532 } else if ( !hole ) {
537 for ( i=0; i<nholes; i++) {
538 if ( holes[i].id == e->e_id ) {
539 free(holes[i].dn.bv_val);
540 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
544 } else if ( holes[i].id > e->e_id ) {
558 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
560 if ( !mdb->mi_nattrs )
563 if ( mdb_tool_threads > 1 ) {
568 ir = mdb_tool_index_rec;
569 for (i=0; i<mdb->mi_nattrs; i++)
570 ir[i].ir_attrs = NULL;
572 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
573 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
574 &a->a_desc->ad_tags, ir );
578 for (i=0; i<mdb->mi_nattrs; i++) {
581 rc = mdb_cursor_open( txn, ir[i].ir_ai->ai_dbi,
582 &ir[i].ir_ai->ai_cursor );
586 mdb_tool_ix_id = e->e_id;
587 mdb_tool_ix_txn = txn;
588 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
589 /* Wait for all threads to be ready */
590 while ( mdb_tool_index_tcount ) {
591 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
592 &mdb_tool_index_mutex );
595 for ( i=1; i<mdb_tool_threads; i++ )
596 mdb_tool_index_rec[i].ir_i = LDAP_BUSY;
597 mdb_tool_index_tcount = mdb_tool_threads - 1;
598 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
599 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
601 return mdb_index_recrun( op, txn, mdb, ir, e->e_id, 0 );
604 return mdb_index_entry_add( op, txn, e );
609 mdb_tool_index_finish()
612 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
613 for ( i=1; i<mdb_tool_threads; i++ ) {
614 if ( mdb_tool_index_rec[i].ir_i == LDAP_BUSY ) {
615 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
616 &mdb_tool_index_mutex );
620 if ( mdb_tool_index_rec[i].ir_i ) {
621 rc = mdb_tool_index_rec[i].ir_i;
625 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
629 ID mdb_tool_entry_put(
632 struct berval *text )
635 struct mdb_info *mdb;
639 assert( be != NULL );
640 assert( slapMode & SLAP_TOOL_MODE );
642 assert( text != NULL );
643 assert( text->bv_val != NULL );
644 assert( text->bv_val[0] == '\0' ); /* overconservative? */
646 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
647 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
649 mdb = (struct mdb_info *) be->be_private;
652 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
654 snprintf( text->bv_val, text->bv_len,
655 "txn_begin 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 );
662 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &idcursor );
664 snprintf( text->bv_val, text->bv_len,
665 "cursor_open failed: %s (%d)",
666 mdb_strerror(rc), rc );
667 Debug( LDAP_DEBUG_ANY,
668 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
669 text->bv_val, 0, 0 );
672 if ( !mdb->mi_nextid ) {
674 mdb_next_id( be, idcursor, &dummy );
676 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcp );
678 snprintf( text->bv_val, text->bv_len,
679 "cursor_open failed: %s (%d)",
680 mdb_strerror(rc), rc );
681 Debug( LDAP_DEBUG_ANY,
682 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
683 text->bv_val, 0, 0 );
686 rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
688 snprintf( text->bv_val, text->bv_len,
689 "cursor_open failed: %s (%d)",
690 mdb_strerror(rc), rc );
691 Debug( LDAP_DEBUG_ANY,
692 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
693 text->bv_val, 0, 0 );
700 op.o_tmpmemctx = NULL;
701 op.o_tmpmfuncs = &ch_mfuncs;
703 /* add dn2id indices */
704 rc = mdb_tool_next_id( &op, txn, e, text, 0 );
709 if ( mdb_tool_threads > 1 ) {
710 LDAP_SLIST_INSERT_HEAD( &op.o_extra, &mdb_tool_axinfo[0]->ai_oe, oe_next );
712 rc = mdb_tool_index_add( &op, txn, e );
714 snprintf( text->bv_val, text->bv_len,
715 "index_entry_add failed: err=%d", rc );
716 Debug( LDAP_DEBUG_ANY,
717 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
718 text->bv_val, 0, 0 );
724 rc = mdb_id2entry_add( &op, txn, idcursor, e );
726 snprintf( text->bv_val, text->bv_len,
727 "id2entry_add failed: err=%d", rc );
728 Debug( LDAP_DEBUG_ANY,
729 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
730 text->bv_val, 0, 0 );
734 if( mdb->mi_nattrs && mdb_tool_threads > 1 )
735 rc = mdb_tool_index_finish();
740 if ( mdb_writes >= mdb_writes_per_commit ) {
742 MDB_TOOL_IDL_FLUSH( be, txn );
743 rc = mdb_txn_commit( txn );
744 for ( i=0; i<mdb->mi_nattrs; i++ )
745 mdb->mi_attrs[i]->ai_cursor = NULL;
750 snprintf( text->bv_val, text->bv_len,
751 "txn_commit failed: %s (%d)",
752 mdb_strerror(rc), rc );
753 Debug( LDAP_DEBUG_ANY,
754 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
755 text->bv_val, 0, 0 );
762 mdb_txn_abort( txn );
765 for ( i=0; i<mdb->mi_nattrs; i++ )
766 mdb->mi_attrs[i]->ai_cursor = NULL;
768 snprintf( text->bv_val, text->bv_len,
769 "txn_aborted! %s (%d)",
770 rc == LDAP_OTHER ? "Internal error" :
771 mdb_strerror(rc), rc );
772 Debug( LDAP_DEBUG_ANY,
773 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
774 text->bv_val, 0, 0 );
781 static int mdb_dn2id_upgrade( BackendDB *be );
783 int mdb_tool_entry_reindex(
786 AttributeDescription **adv )
788 struct mdb_info *mi = (struct mdb_info *) be->be_private;
794 Debug( LDAP_DEBUG_ARGS,
795 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
797 assert( tool_base == NULL );
798 assert( tool_filter == NULL );
800 /* Special: do a dn2id upgrade */
801 if ( adv && adv[0] == slap_schema.si_ad_entryDN ) {
802 /* short-circuit tool_entry_next() */
803 mdb_cursor_get( cursor, &key, &data, MDB_LAST );
804 return mdb_dn2id_upgrade( be );
807 /* No indexes configured, nothing to do. Could return an
808 * error here to shortcut things.
814 /* Check for explicit list of attrs to index */
818 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
820 for ( n = 0; adv[n]; n++ ) ;
823 for ( i = 0; i < n; i++ ) {
824 AttributeDescription *ad = adv[i];
825 for ( j = i-1; j>=0; j--) {
826 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
833 for ( i = 0; adv[i]; i++ ) {
834 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
835 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
836 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
837 AttrInfo *ai = mi->mi_attrs[i];
838 mi->mi_attrs[i] = mi->mi_attrs[j];
839 mi->mi_attrs[j] = ai;
843 if ( j == mi->mi_nattrs ) {
844 Debug( LDAP_DEBUG_ANY,
845 LDAP_XSTRING(mdb_tool_entry_reindex)
846 ": no index configured for %s\n",
847 adv[i]->ad_cname.bv_val, 0, 0 );
855 e = mdb_tool_entry_get( be, id );
858 Debug( LDAP_DEBUG_ANY,
859 LDAP_XSTRING(mdb_tool_entry_reindex)
860 ": could not locate id=%ld\n",
866 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
868 Debug( LDAP_DEBUG_ANY,
869 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
870 "txn_begin failed: %s (%d)\n",
871 mdb_strerror(rc), rc, 0 );
876 if ( slapMode & SLAP_TRUNCATE_MODE ) {
878 for ( i=0; i < mi->mi_nattrs; i++ ) {
879 rc = mdb_drop( txi, mi->mi_attrs[i]->ai_dbi, 0 );
881 Debug( LDAP_DEBUG_ANY,
882 LDAP_XSTRING(mdb_tool_entry_reindex)
883 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
884 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
885 mdb_strerror(rc), rc );
889 slapMode ^= SLAP_TRUNCATE_MODE;
893 * just (re)add them for now
894 * Use truncate mode to empty/reset index databases
897 Debug( LDAP_DEBUG_TRACE,
898 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
903 op.o_tmpmemctx = NULL;
904 op.o_tmpmfuncs = &ch_mfuncs;
906 rc = mdb_tool_index_add( &op, txi, e );
911 if ( mdb_writes >= mdb_writes_per_commit ) {
914 MDB_TOOL_IDL_FLUSH( be, txi );
915 rc = mdb_txn_commit( txi );
917 for ( i=0; i<mi->mi_nattrs; i++ )
918 mi->mi_attrs[i]->ai_cursor = NULL;
920 Debug( LDAP_DEBUG_ANY,
921 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
922 ": txn_commit failed: %s (%d)\n",
923 mdb_strerror(rc), rc, 0 );
926 mdb_cursor_close( cursor );
928 /* Must close the read txn to allow old pages to be reclaimed. */
929 mdb_txn_abort( txn );
930 /* and then reopen it so that tool_entry_next still works. */
931 mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &txn );
932 mdb_cursor_open( txn, mi->mi_id2entry, &cursor );
934 key.mv_size = sizeof(ID);
935 mdb_cursor_get( cursor, &key, NULL, MDB_SET );
941 mdb_cursor_close( cursor );
943 mdb_txn_abort( txi );
944 for ( i=0; i<mi->mi_nattrs; i++ )
945 mi->mi_attrs[i]->ai_cursor = NULL;
946 Debug( LDAP_DEBUG_ANY,
947 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
948 ": txn_aborted! err=%d\n",
953 mdb_entry_release( &op, e, 0 );
958 ID mdb_tool_entry_modify(
961 struct berval *text )
964 struct mdb_info *mdb;
968 assert( be != NULL );
969 assert( slapMode & SLAP_TOOL_MODE );
971 assert( text != NULL );
972 assert( text->bv_val != NULL );
973 assert( text->bv_val[0] == '\0' ); /* overconservative? */
975 assert ( e->e_id != NOID );
977 Debug( LDAP_DEBUG_TRACE,
978 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
979 (long) e->e_id, e->e_dn, 0 );
981 mdb = (struct mdb_info *) be->be_private;
984 mdb_cursor_close( cursor );
988 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
990 snprintf( text->bv_val, text->bv_len,
991 "txn_begin failed: %s (%d)",
992 mdb_strerror(rc), rc );
993 Debug( LDAP_DEBUG_ANY,
994 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
995 text->bv_val, 0, 0 );
1002 op.o_tmpmemctx = NULL;
1003 op.o_tmpmfuncs = &ch_mfuncs;
1005 /* id2entry index */
1006 rc = mdb_id2entry_update( &op, txn, NULL, e );
1008 snprintf( text->bv_val, text->bv_len,
1009 "id2entry_update failed: err=%d", rc );
1010 Debug( LDAP_DEBUG_ANY,
1011 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
1012 text->bv_val, 0, 0 );
1018 rc = mdb_txn_commit( txn );
1020 snprintf( text->bv_val, text->bv_len,
1021 "txn_commit failed: %s (%d)",
1022 mdb_strerror(rc), rc );
1023 Debug( LDAP_DEBUG_ANY,
1024 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
1025 "%s\n", text->bv_val, 0, 0 );
1030 mdb_txn_abort( txn );
1031 snprintf( text->bv_val, text->bv_len,
1032 "txn_aborted! %s (%d)",
1033 mdb_strerror(rc), rc );
1034 Debug( LDAP_DEBUG_ANY,
1035 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
1036 text->bv_val, 0, 0 );
1046 mdb_tool_index_task( void *ctx, void *ptr )
1048 int base = *(int *)ptr;
1050 Opheader ohdr = {0};
1051 AttrIxInfo ai = {0}, *aio;
1055 op.o_bd = mdb_tool_ix_be;
1056 op.o_tmpmemctx = NULL;
1057 op.o_tmpmfuncs = &ch_mfuncs;
1058 aio = mdb_tool_axinfo[base];
1059 mdb_tool_axinfo[base] = &ai;
1060 LDAP_SLIST_INSERT_HEAD( &op.o_extra, &ai.ai_oe, oe_next );
1062 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
1063 mdb_tool_index_tcount--;
1064 if ( !mdb_tool_index_tcount )
1065 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1066 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
1067 &mdb_tool_index_mutex );
1068 if ( slapd_shutdown ) {
1069 mdb_tool_index_tcount--;
1070 if ( !mdb_tool_index_tcount )
1071 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
1073 mdb_tool_axinfo[base] = aio;
1074 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1077 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
1078 mdb_tool_index_rec[base].ir_i = mdb_index_recrun( &op,
1080 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
1086 #ifdef MDB_TOOL_IDL_CACHING
1088 mdb_tool_idl_cmp( const void *v1, const void *v2 )
1090 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
1093 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
1094 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1098 mdb_tool_idl_flush_one( MDB_cursor *mc, AttrIxInfo *ai, mdb_tool_idl_cache *ic )
1100 mdb_tool_idl_cache_entry *ice;
1101 MDB_val key, data[2];
1105 /* Freshly allocated, ignore it */
1106 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
1110 key.mv_data = ic->kstr.bv_val;
1111 key.mv_size = ic->kstr.bv_len;
1113 if ( ic->count > MDB_IDL_DB_SIZE ) {
1114 while ( ic->flags & WAS_FOUND ) {
1115 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
1117 /* FIXME: find out why this happens */
1121 if ( ic->flags & WAS_RANGE ) {
1123 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1126 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
1128 /* Store range hi */
1129 data[0].mv_data = &ic->last;
1130 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
1132 /* Delete old data, replace with range */
1133 ic->first = *(ID *)data[0].mv_data;
1134 mdb_cursor_del( mc, MDB_NODUPDATA );
1138 if ( !(ic->flags & WAS_RANGE)) {
1139 /* range, didn't exist before */
1141 data[0].mv_size = sizeof(ID);
1142 data[0].mv_data = &nid;
1143 rc = mdb_cursor_put( mc, &key, data, 0 );
1145 data[0].mv_data = &ic->first;
1146 rc = mdb_cursor_put( mc, &key, data, 0 );
1148 data[0].mv_data = &ic->last;
1149 rc = mdb_cursor_put( mc, &key, data, 0 );
1160 data[0].mv_size = sizeof(ID);
1162 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1167 end = (ic->count-ic->offset) & (IDBLOCK-1);
1171 data[1].mv_size = end;
1172 data[0].mv_data = ice->ids;
1173 rc = mdb_cursor_put( mc, &key, data, MDB_APPENDDUP|MDB_MULTIPLE );
1180 ic->tail->next = ai->ai_flist;
1181 ai->ai_flist = ic->head;
1184 ic->head = ai->ai_clist;
1190 mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai, AttrIxInfo *ax )
1196 mdb_cursor_open( txn, ai->ai_dbi, &mc );
1197 root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
1199 rc = mdb_tool_idl_flush_one( mc, ax, root->avl_data );
1202 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1203 mdb_cursor_close( mc );
1209 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1211 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1213 unsigned int i, dbi;
1215 for ( i=0; i < mdb->mi_nattrs; i++ ) {
1216 if ( !mdb->mi_attrs[i]->ai_root ) continue;
1217 rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i], mdb_tool_axinfo[i % mdb_tool_threads] );
1218 tavl_free(mdb->mi_attrs[i]->ai_root, NULL);
1219 mdb->mi_attrs[i]->ai_root = NULL;
1226 int mdb_tool_idl_add(
1229 struct berval *keys,
1233 mdb_tool_idl_cache *ic, itmp;
1234 mdb_tool_idl_cache_entry *ice;
1236 AttrIxInfo *ax = (AttrIxInfo *)mc;
1237 AttrInfo *ai = (AttrInfo *)ax->ai_ai;
1241 for (i=0; keys[i].bv_val; i++) {
1242 itmp.kstr = keys[i];
1243 ic = tavl_find( (Avlnode *)ai->ai_root, &itmp, mdb_tool_idl_cmp );
1245 /* No entry yet, create one */
1251 if ( ax->ai_clist ) {
1253 ax->ai_clist = ic->head;
1255 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 );
1257 ic->kstr.bv_len = itmp.kstr.bv_len;
1258 ic->kstr.bv_val = (char *)(ic+1);
1259 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1260 ic->head = ic->tail = NULL;
1265 tavl_insert( (Avlnode **)&ai->ai_root, ic, mdb_tool_idl_cmp,
1268 /* load existing key count here */
1269 key.mv_size = keys[i].bv_len;
1270 key.mv_data = keys[i].bv_val;
1271 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1273 ic->flags |= WAS_FOUND;
1274 nid = *(ID *)data.mv_data;
1276 ic->count = MDB_IDL_DB_SIZE+1;
1277 ic->flags |= WAS_RANGE;
1281 mdb_cursor_count( mc, &count );
1284 ic->offset = count & (IDBLOCK-1);
1288 /* are we a range already? */
1289 if ( ic->count > MDB_IDL_DB_SIZE ) {
1292 /* Are we at the limit, and converting to a range? */
1293 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1295 ic->tail->next = ax->ai_flist;
1296 ax->ai_flist = ic->head;
1298 ic->head = ic->tail = NULL;
1303 /* No free block, create that too */
1304 lcount = (ic->count-ic->offset) & (IDBLOCK-1);
1305 if ( !ic->tail || lcount == 0) {
1306 if ( ax->ai_flist ) {
1308 ax->ai_flist = ice->next;
1310 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1316 ic->tail->next = ice;
1320 ice->ids[lcount-1] = 0;
1325 if (!lcount || ice->ids[lcount-1] != id) {
1326 ice->ids[lcount] = id;
1333 #endif /* MDB_TOOL_IDL_CACHING */
1335 /* Upgrade from pre 2.4.34 dn2id format */
1337 #include <ac/unistd.h>
1338 #include <lutil_meter.h>
1340 #define STACKSIZ 2048
1342 typedef struct rec {
1349 mdb_dn2id_upgrade( BackendDB *be ) {
1350 struct mdb_info *mi = (struct mdb_info *) be->be_private;
1352 MDB_cursor *mc = NULL;
1355 int rc, writes=0, depth=0;
1356 int enable_meter = 0;
1357 ID id = 0, *num, count = 0;
1359 lutil_meter_t meter;
1361 if (!(mi->mi_flags & MDB_NEED_UPGRADE)) {
1362 Debug( LDAP_DEBUG_ANY, "database %s: No upgrade needed.\n",
1363 be->be_suffix[0].bv_val, 0, 0 );
1370 mdb_stat(mdb_cursor_txn(cursor), mi->mi_dbis[MDB_ID2ENTRY], &st);
1371 if (!st.ms_entries) {
1372 /* Empty DB, nothing to upgrade? */
1376 enable_meter = !lutil_meter_open(&meter,
1377 &lutil_meter_text_display,
1378 &lutil_meter_linear_estimator,
1382 num = ch_malloc(STACKSIZ * (sizeof(ID) + sizeof(rec)));
1383 stack = (rec *)(num + STACKSIZ);
1385 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1387 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin failed, %s (%d)\n",
1388 mdb_strerror(rc), rc, 0 );
1391 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1393 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open failed, %s (%d)\n",
1394 mdb_strerror(rc), rc, 0 );
1398 key.mv_size = sizeof(ID);
1399 /* post-order depth-first update */
1406 stack[depth].id = id;
1407 rc = mdb_cursor_get(mc, &key, &data, MDB_SET);
1409 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get failed, %s (%d)\n",
1410 mdb_strerror(rc), rc, 0 );
1415 rc = mdb_cursor_count(mc, &dkids);
1417 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_count failed, %s (%d)\n",
1418 mdb_strerror(rc), rc, 0 );
1422 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1424 ptr = (unsigned char *)data.mv_data + data.mv_size - sizeof(ID);
1425 memcpy(&id, ptr, sizeof(ID));
1427 memcpy(stack[depth].rdn, data.mv_data, data.mv_size);
1428 stack[depth].len = data.mv_size;
1433 /* pop: write updated count, advance to next node */
1435 /* update superior counts */
1437 num[depth-1] += num[depth];
1440 id = stack[depth-1].id;
1441 data.mv_data = stack[depth].rdn;
1442 data.mv_size = stack[depth].len;
1443 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1445 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(BOTH) failed, %s (%d)\n",
1446 mdb_strerror(rc), rc, 0 );
1449 data.mv_data = stack[depth].rdn;
1450 ptr = (unsigned char *)data.mv_data + data.mv_size;
1451 memcpy(ptr, &num[depth], sizeof(ID));
1452 data.mv_size += sizeof(ID);
1453 rc = mdb_cursor_del(mc, 0);
1455 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_del failed, %s (%d)\n",
1456 mdb_strerror(rc), rc, 0 );
1459 rc = mdb_cursor_put(mc, &key, &data, 0);
1461 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_put failed, %s (%d)\n",
1462 mdb_strerror(rc), rc, 0 );
1468 lutil_meter_update(&meter, count, 0);
1473 len = (ptr[0] & 0x7f) << 8 | ptr[1];
1474 printf("ID: %zu, %zu, %.*s\n", stack[depth].id, num[depth], len, ptr+2);
1478 if (writes == 1000) {
1479 mdb_cursor_close(mc);
1480 rc = mdb_txn_commit(mt);
1482 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit failed, %s (%d)\n",
1483 mdb_strerror(rc), rc, 0 );
1486 rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt);
1488 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin(2) failed, %s (%d)\n",
1489 mdb_strerror(rc), rc, 0 );
1492 rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc);
1494 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open(2) failed, %s (%d)\n",
1495 mdb_strerror(rc), rc, 0 );
1498 rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH);
1500 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(2) failed, %s (%d)\n",
1501 mdb_strerror(rc), rc, 0 );
1508 rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP);
1518 mdb_cursor_close(mc);
1521 r2 = mdb_txn_commit(mt);
1523 Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit(2) failed, %s (%d)\n",
1524 mdb_strerror(r2), r2, 0 );
1531 lutil_meter_update(&meter, count, 1);
1532 lutil_meter_close(&meter);