X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=servers%2Fslapd%2Fback-mdb%2Ftools.c;h=a84de6f4c8f4d43ee75c2d49349f984c049cfcf4;hb=42a24a1d2ffa30a176d25860a2f25c09ee209ee7;hp=7b490a595fc2722d9e104e9580dbcc5a0b8cbd3d;hpb=b3345a21e027a710829fce527d6a6056e737f1df;p=openldap diff --git a/servers/slapd/back-mdb/tools.c b/servers/slapd/back-mdb/tools.c index 7b490a595f..a84de6f4c8 100644 --- a/servers/slapd/back-mdb/tools.c +++ b/servers/slapd/back-mdb/tools.c @@ -2,7 +2,7 @@ /* $OpenLDAP$ */ /* This work is part of OpenLDAP Software . * - * Copyright 2011-2013 The OpenLDAP Foundation. + * Copyright 2011-2014 The OpenLDAP Foundation. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -50,7 +50,9 @@ typedef struct mdb_tool_idl_cache { #define MDB_TOOL_IDL_FLUSH(be, txn) #endif /* MDB_TOOL_IDL_CACHING */ -static MDB_txn *txn = NULL, *txi = NULL; +MDB_txn *mdb_tool_txn = NULL; + +static MDB_txn *txi = NULL; static MDB_cursor *cursor = NULL, *idcursor = NULL; static MDB_cursor *mcp = NULL, *mcd = NULL; static MDB_val key, data; @@ -72,10 +74,11 @@ static Filter *tool_filter; static Entry *tool_next_entry; static ID mdb_tool_ix_id; -static Operation *mdb_tool_ix_op; +static BackendDB *mdb_tool_ix_be; static MDB_txn *mdb_tool_ix_txn; static int mdb_tool_index_tcount, mdb_tool_threads; static IndexRec *mdb_tool_index_rec; +static AttrIxInfo **mdb_tool_axinfo; static struct mdb_info *mdb_tool_info; static ldap_pvt_thread_mutex_t mdb_tool_index_mutex; static ldap_pvt_thread_cond_t mdb_tool_index_cond_main; @@ -84,19 +87,28 @@ static void * mdb_tool_index_task( void *ctx, void *ptr ); static int mdb_writes, mdb_writes_per_commit; +/* Number of ops per commit in Quick mode. + * Batching speeds writes overall, but too large a + * batch will fail with MDB_TXN_FULL. + */ +#ifndef MDB_WRITES_PER_COMMIT +#define MDB_WRITES_PER_COMMIT 500 +#endif + static int mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep ); int mdb_tool_entry_open( BackendDB *be, int mode ) { - /* In Quick mode, commit once per 1000 entries */ + /* In Quick mode, commit once per 500 entries */ mdb_writes = 0; if ( slapMode & SLAP_TOOL_QUICK ) - mdb_writes_per_commit = 1000; + mdb_writes_per_commit = MDB_WRITES_PER_COMMIT; else mdb_writes_per_commit = 1; +#ifdef MDB_TOOL_IDL_CACHING /* threaded indexing has no performance advantage */ /* Set up for threaded slapindex */ if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) { if ( !mdb_tool_info ) { @@ -106,12 +118,16 @@ int mdb_tool_entry_open( ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work ); if ( mdb->mi_nattrs ) { int i; -#if 0 /* threaded indexing has no performance advantage */ mdb_tool_threads = slap_tool_thread_max - 1; -#endif if ( mdb_tool_threads > 1 ) { mdb_tool_index_rec = ch_calloc( mdb->mi_nattrs, sizeof( IndexRec )); + mdb_tool_axinfo = ch_calloc( mdb_tool_threads, sizeof( AttrIxInfo* ) + + sizeof( AttrIxInfo )); + mdb_tool_axinfo[0] = (AttrIxInfo *)(mdb_tool_axinfo + mdb_tool_threads); + for (i=1; iai_clist)) { + mdb_tool_axinfo[i]->ai_clist = ic->head; + free(ic); + } + while ((ice = mdb_tool_axinfo[i]->ai_flist)) { + mdb_tool_axinfo[i]->ai_flist = ice->next; + free(ice); + } + } } +#endif if( idcursor ) { mdb_cursor_close( idcursor ); @@ -164,11 +198,16 @@ int mdb_tool_entry_close( mdb_cursor_close( cursor ); cursor = NULL; } - if( txn ) { - MDB_TOOL_IDL_FLUSH( be, txn ); - if ( mdb_txn_commit( txn )) + if( mdb_tool_txn ) { + int rc; + if (( rc = mdb_txn_commit( mdb_tool_txn ))) { + Debug( LDAP_DEBUG_ANY, + LDAP_XSTRING(mdb_tool_entry_close) ": database %s: " + "txn_commit failed: %s (%d)\n", + be->be_suffix[0].bv_val, mdb_strerror(rc), rc ); return -1; - txn = NULL; + } + mdb_tool_txn = NULL; } if( nholes ) { @@ -212,13 +251,13 @@ ID mdb_tool_entry_next( mdb = (struct mdb_info *) be->be_private; assert( mdb != NULL ); - if ( !txn ) { - rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn ); + if ( !mdb_tool_txn ) { + rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &mdb_tool_txn ); if ( rc ) return NOID; - rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor ); + rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &cursor ); if ( rc ) { - mdb_txn_abort( txn ); + mdb_txn_abort( mdb_tool_txn ); return NOID; } } @@ -284,9 +323,9 @@ ID mdb_tool_dn2id_get( mdb = (struct mdb_info *) be->be_private; - if ( !txn ) { + if ( !mdb_tool_txn ) { rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ? - MDB_RDONLY : 0, &txn ); + MDB_RDONLY : 0, &mdb_tool_txn ); if ( rc ) return NOID; } @@ -296,7 +335,7 @@ ID mdb_tool_dn2id_get( op.o_tmpmemctx = NULL; op.o_tmpmfuncs = &ch_mfuncs; - rc = mdb_dn2id( &op, txn, NULL, dn, &id, NULL, NULL ); + rc = mdb_dn2id( &op, mdb_tool_txn, NULL, dn, &id, NULL, NULL, NULL ); if ( rc == MDB_NOTFOUND ) return NOID; @@ -341,7 +380,7 @@ mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep ) op.o_tmpmemctx = NULL; op.o_tmpmfuncs = &ch_mfuncs; if ( slapMode & SLAP_TOOL_READONLY ) { - rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn ); + rc = mdb_id2name( &op, mdb_tool_txn, &idcursor, id, &dn, &ndn ); if ( rc ) { rc = LDAP_OTHER; if ( e ) { @@ -359,7 +398,7 @@ mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep ) } } } - rc = mdb_entry_decode( &op, &data, &e ); + rc = mdb_entry_decode( &op, mdb_tool_txn, &data, &e ); e->e_id = id; if ( !BER_BVISNULL( &dn )) { e->e_name = dn; @@ -383,19 +422,19 @@ mdb_tool_entry_get( BackendDB *be, ID id ) Entry *e = NULL; int rc; - if ( !txn ) { + if ( !mdb_tool_txn ) { struct mdb_info *mdb = (struct mdb_info *) be->be_private; rc = mdb_txn_begin( mdb->mi_dbenv, NULL, - (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &txn ); + (slapMode & SLAP_TOOL_READONLY) ? MDB_RDONLY : 0, &mdb_tool_txn ); if ( rc ) return NULL; } if ( !cursor ) { struct mdb_info *mdb = (struct mdb_info *) be->be_private; - rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor ); + rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &cursor ); if ( rc ) { - mdb_txn_abort( txn ); - txn = NULL; + mdb_txn_abort( mdb_tool_txn ); + mdb_tool_txn = NULL; return NULL; } } @@ -422,7 +461,7 @@ static int mdb_tool_next_id( return 0; } - rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, &nmatched ); + rc = mdb_dn2id( op, tid, mcp, &ndn, &id, NULL, NULL, &nmatched ); if ( rc == MDB_NOTFOUND ) { if ( !be_issuffix( op->o_bd, &ndn ) ) { ID eid = e->e_id; @@ -457,7 +496,7 @@ static int mdb_tool_next_id( "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 ); return rc; } - rc = mdb_dn2id_add( op, mcp, mcd, pid, e ); + rc = mdb_dn2id_add( op, mcp, mcd, pid, 1, 1, e ); if ( rc ) { snprintf( text->bv_val, text->bv_len, "dn2id_add failed: %s (%d)", @@ -547,7 +586,6 @@ mdb_tool_index_add( return rc; } mdb_tool_ix_id = e->e_id; - mdb_tool_ix_op = op; mdb_tool_ix_txn = txn; ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex ); /* Wait for all threads to be ready */ @@ -559,33 +597,37 @@ mdb_tool_index_add( for ( i=1; ie_id, 0 ); - if ( rc ) - return rc; - ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex ); - for ( i=1; ie_id, 0 ); } else { return mdb_index_entry_add( op, txn, e ); } } +static int +mdb_tool_index_finish() +{ + int i, rc; + ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex ); + for ( i=1; ibe_private; - if ( !txn ) { - rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn ); + if ( !mdb_tool_txn ) { + rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &mdb_tool_txn ); if( rc != 0 ) { snprintf( text->bv_val, text->bv_len, "txn_begin failed: %s (%d)", @@ -619,7 +661,7 @@ ID mdb_tool_entry_put( text->bv_val, 0, 0 ); return NOID; } - rc = mdb_cursor_open( txn, mdb->mi_id2entry, &idcursor ); + rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_id2entry, &idcursor ); if( rc != 0 ) { snprintf( text->bv_val, text->bv_len, "cursor_open failed: %s (%d)", @@ -633,7 +675,7 @@ ID mdb_tool_entry_put( ID dummy; mdb_next_id( be, idcursor, &dummy ); } - rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcp ); + rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_dn2id, &mcp ); if( rc != 0 ) { snprintf( text->bv_val, text->bv_len, "cursor_open failed: %s (%d)", @@ -643,7 +685,7 @@ ID mdb_tool_entry_put( text->bv_val, 0, 0 ); return NOID; } - rc = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd ); + rc = mdb_cursor_open( mdb_tool_txn, mdb->mi_dn2id, &mcd ); if( rc != 0 ) { snprintf( text->bv_val, text->bv_len, "cursor_open failed: %s (%d)", @@ -661,12 +703,15 @@ ID mdb_tool_entry_put( op.o_tmpmfuncs = &ch_mfuncs; /* add dn2id indices */ - rc = mdb_tool_next_id( &op, txn, e, text, 0 ); + rc = mdb_tool_next_id( &op, mdb_tool_txn, e, text, 0 ); if( rc != 0 ) { goto done; } - rc = mdb_tool_index_add( &op, txn, e ); + if ( mdb_tool_threads > 1 ) { + LDAP_SLIST_INSERT_HEAD( &op.o_extra, &mdb_tool_axinfo[0]->ai_oe, oe_next ); + } + rc = mdb_tool_index_add( &op, mdb_tool_txn, e ); if( rc != 0 ) { snprintf( text->bv_val, text->bv_len, "index_entry_add failed: err=%d", rc ); @@ -678,7 +723,7 @@ ID mdb_tool_entry_put( /* id2entry index */ - rc = mdb_id2entry_add( &op, txn, idcursor, e ); + rc = mdb_id2entry_add( &op, mdb_tool_txn, idcursor, e ); if( rc != 0 ) { snprintf( text->bv_val, text->bv_len, "id2entry_add failed: err=%d", rc ); @@ -688,17 +733,20 @@ ID mdb_tool_entry_put( goto done; } + if( mdb->mi_nattrs && mdb_tool_threads > 1 ) + rc = mdb_tool_index_finish(); + done: if( rc == 0 ) { mdb_writes++; if ( mdb_writes >= mdb_writes_per_commit ) { unsigned i; - MDB_TOOL_IDL_FLUSH( be, txn ); - rc = mdb_txn_commit( txn ); + MDB_TOOL_IDL_FLUSH( be, mdb_tool_txn ); + rc = mdb_txn_commit( mdb_tool_txn ); for ( i=0; imi_nattrs; i++ ) mdb->mi_attrs[i]->ai_cursor = NULL; mdb_writes = 0; - txn = NULL; + mdb_tool_txn = NULL; idcursor = NULL; if( rc != 0 ) { snprintf( text->bv_val, text->bv_len, @@ -713,8 +761,8 @@ done: } else { unsigned i; - mdb_txn_abort( txn ); - txn = NULL; + mdb_txn_abort( mdb_tool_txn ); + mdb_tool_txn = NULL; idcursor = NULL; for ( i=0; imi_nattrs; i++ ) mdb->mi_attrs[i]->ai_cursor = NULL; @@ -732,6 +780,8 @@ done: return e->e_id; } +static int mdb_dn2id_upgrade( BackendDB *be ); + int mdb_tool_entry_reindex( BackendDB *be, ID id, @@ -749,6 +799,13 @@ int mdb_tool_entry_reindex( assert( tool_base == NULL ); assert( tool_filter == NULL ); + /* Special: do a dn2id upgrade */ + if ( adv && adv[0] == slap_schema.si_ad_entryDN ) { + /* short-circuit tool_entry_next() */ + mdb_cursor_get( cursor, &key, &data, MDB_LAST ); + return mdb_dn2id_upgrade( be ); + } + /* No indexes configured, nothing to do. Could return an * error here to shortcut things. */ @@ -871,10 +928,10 @@ done: mdb_cursor_close( cursor ); txi = NULL; /* Must close the read txn to allow old pages to be reclaimed. */ - mdb_txn_abort( txn ); + mdb_txn_abort( mdb_tool_txn ); /* and then reopen it so that tool_entry_next still works. */ - mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &txn ); - mdb_cursor_open( txn, mi->mi_id2entry, &cursor ); + mdb_txn_begin( mi->mi_dbenv, NULL, MDB_RDONLY, &mdb_tool_txn ); + mdb_cursor_open( mdb_tool_txn, mi->mi_id2entry, &cursor ); key.mv_data = &id; key.mv_size = sizeof(ID); mdb_cursor_get( cursor, &key, NULL, MDB_SET ); @@ -929,8 +986,8 @@ ID mdb_tool_entry_modify( mdb_cursor_close( cursor ); cursor = NULL; } - if ( !txn ) { - rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn ); + if ( !mdb_tool_txn ) { + rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &mdb_tool_txn ); if( rc != 0 ) { snprintf( text->bv_val, text->bv_len, "txn_begin failed: %s (%d)", @@ -948,7 +1005,7 @@ ID mdb_tool_entry_modify( op.o_tmpmfuncs = &ch_mfuncs; /* id2entry index */ - rc = mdb_id2entry_update( &op, txn, NULL, e ); + rc = mdb_id2entry_update( &op, mdb_tool_txn, NULL, e ); if( rc != 0 ) { snprintf( text->bv_val, text->bv_len, "id2entry_update failed: err=%d", rc ); @@ -960,7 +1017,7 @@ ID mdb_tool_entry_modify( done: if( rc == 0 ) { - rc = mdb_txn_commit( txn ); + rc = mdb_txn_commit( mdb_tool_txn ); if( rc != 0 ) { snprintf( text->bv_val, text->bv_len, "txn_commit failed: %s (%d)", @@ -972,7 +1029,7 @@ done: } } else { - mdb_txn_abort( txn ); + mdb_txn_abort( mdb_tool_txn ); snprintf( text->bv_val, text->bv_len, "txn_aborted! %s (%d)", mdb_strerror(rc), rc ); @@ -981,7 +1038,7 @@ done: text->bv_val, 0, 0 ); e->e_id = NOID; } - txn = NULL; + mdb_tool_txn = NULL; idcursor = NULL; return e->e_id; @@ -991,8 +1048,18 @@ static void * mdb_tool_index_task( void *ctx, void *ptr ) { int base = *(int *)ptr; + Operation op = {0}; + Opheader ohdr = {0}; + AttrIxInfo ai = {0}, *aio; free( ptr ); + op.o_hdr = &ohdr; + op.o_bd = mdb_tool_ix_be; + op.o_tmpmemctx = NULL; + op.o_tmpmfuncs = &ch_mfuncs; + aio = mdb_tool_axinfo[base]; + mdb_tool_axinfo[base] = &ai; + LDAP_SLIST_INSERT_HEAD( &op.o_extra, &ai.ai_oe, oe_next ); while ( 1 ) { ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex ); mdb_tool_index_tcount--; @@ -1004,11 +1071,13 @@ mdb_tool_index_task( void *ctx, void *ptr ) mdb_tool_index_tcount--; if ( !mdb_tool_index_tcount ) ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main ); + *aio = ai; + mdb_tool_axinfo[base] = aio; ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex ); break; } ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex ); - mdb_tool_index_rec[base].ir_i = mdb_index_recrun( mdb_tool_ix_op, + mdb_tool_index_rec[base].ir_i = mdb_index_recrun( &op, mdb_tool_ix_txn, mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base ); } @@ -1028,7 +1097,7 @@ mdb_tool_idl_cmp( const void *v1, const void *v2 ) } static int -mdb_tool_idl_flush_one( MDB_cursor *mc, AttrInfo *ai, mdb_tool_idl_cache *ic ) +mdb_tool_idl_flush_one( MDB_cursor *mc, AttrIxInfo *ai, mdb_tool_idl_cache *ic ) { mdb_tool_idl_cache_entry *ice; MDB_val key, data[2]; @@ -1092,25 +1161,19 @@ mdb_tool_idl_flush_one( MDB_cursor *mc, AttrInfo *ai, mdb_tool_idl_cache *ic ) data[0].mv_size = sizeof(ID); rc = 0; - i = ic->offset; for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) { int end; if ( ice->next ) { end = IDBLOCK; } else { - end = ic->count & (IDBLOCK-1); + end = (ic->count-ic->offset) & (IDBLOCK-1); if ( !end ) end = IDBLOCK; } - data[1].mv_size = end - i; - data[0].mv_data = &ice->ids[i]; - i = 0; - rc = mdb_cursor_put( mc, &key, data, MDB_NODUPDATA|MDB_APPEND|MDB_MULTIPLE ); + data[1].mv_size = end; + data[0].mv_data = ice->ids; + rc = mdb_cursor_put( mc, &key, data, MDB_APPENDDUP|MDB_MULTIPLE ); if ( rc ) { - if ( rc == MDB_KEYEXIST ) { - rc = 0; - continue; - } rc = -1; break; } @@ -1126,7 +1189,7 @@ mdb_tool_idl_flush_one( MDB_cursor *mc, AttrInfo *ai, mdb_tool_idl_cache *ic ) } static int -mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai ) +mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai, AttrIxInfo *ax ) { MDB_cursor *mc; Avlnode *root; @@ -1135,7 +1198,7 @@ mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai ) mdb_cursor_open( txn, ai->ai_dbi, &mc ); root = tavl_end( ai->ai_root, TAVL_DIR_LEFT ); do { - rc = mdb_tool_idl_flush_one( mc, ai, root->avl_data ); + rc = mdb_tool_idl_flush_one( mc, ax, root->avl_data ); if ( rc != -1 ) rc = 0; } while ((root = tavl_next(root, TAVL_DIR_RIGHT))); @@ -1153,7 +1216,7 @@ mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn ) for ( i=0; i < mdb->mi_nattrs; i++ ) { if ( !mdb->mi_attrs[i]->ai_root ) continue; - rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i] ); + rc = mdb_tool_idl_flush_db( txn, mdb->mi_attrs[i], mdb_tool_axinfo[i % mdb_tool_threads] ); tavl_free(mdb->mi_attrs[i]->ai_root, NULL); mdb->mi_attrs[i]->ai_root = NULL; if ( rc ) @@ -1172,7 +1235,8 @@ int mdb_tool_idl_add( mdb_tool_idl_cache *ic, itmp; mdb_tool_idl_cache_entry *ice; int i, rc, lcount; - AttrInfo *ai = (AttrInfo *)mc; + AttrIxInfo *ax = (AttrIxInfo *)mc; + AttrInfo *ai = (AttrInfo *)ax->ai_ai; mc = ai->ai_cursor; dbi = ai->ai_dbi; @@ -1186,9 +1250,9 @@ int mdb_tool_idl_add( ID nid; int rc; - if ( ai->ai_clist ) { - ic = ai->ai_clist; - ai->ai_clist = ic->head; + if ( ax->ai_clist ) { + ic = ax->ai_clist; + ax->ai_clist = ic->head; } else { ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len + 4 ); } @@ -1230,8 +1294,8 @@ int mdb_tool_idl_add( /* Are we at the limit, and converting to a range? */ } else if ( ic->count == MDB_IDL_DB_SIZE ) { if ( ic->head ) { - ic->tail->next = ai->ai_flist; - ai->ai_flist = ic->head; + ic->tail->next = ax->ai_flist; + ax->ai_flist = ic->head; } ic->head = ic->tail = NULL; ic->last = id; @@ -1239,11 +1303,11 @@ int mdb_tool_idl_add( continue; } /* No free block, create that too */ - lcount = ic->count & (IDBLOCK-1); + lcount = (ic->count-ic->offset) & (IDBLOCK-1); if ( !ic->tail || lcount == 0) { - if ( ai->ai_flist ) { - ice = ai->ai_flist; - ai->ai_flist = ice->next; + if ( ax->ai_flist ) { + ice = ax->ai_flist; + ax->ai_flist = ice->next; } else { ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry )); } @@ -1260,11 +1324,214 @@ int mdb_tool_idl_add( ic->first = id; } ice = ic->tail; - if (!lcount || ice->ids[lcount-1] != id) + if (!lcount || ice->ids[lcount-1] != id) { ice->ids[lcount] = id; - ic->count++; + ic->count++; + } } return 0; } #endif /* MDB_TOOL_IDL_CACHING */ + +/* Upgrade from pre 2.4.34 dn2id format */ + +#include +#include + +#define STACKSIZ 2048 + +typedef struct rec { + ID id; + size_t len; + char rdn[512]; +} rec; + +static int +mdb_dn2id_upgrade( BackendDB *be ) { + struct mdb_info *mi = (struct mdb_info *) be->be_private; + MDB_txn *mt; + MDB_cursor *mc = NULL; + MDB_val key, data; + char *ptr; + int rc, writes=0, depth=0; + int enable_meter = 0; + ID id = 0, *num, count = 0; + rec *stack; + lutil_meter_t meter; + + if (!(mi->mi_flags & MDB_NEED_UPGRADE)) { + Debug( LDAP_DEBUG_ANY, "database %s: No upgrade needed.\n", + be->be_suffix[0].bv_val, 0, 0 ); + return 0; + } + + { + MDB_stat st; + + mdb_stat(mdb_cursor_txn(cursor), mi->mi_dbis[MDB_ID2ENTRY], &st); + if (!st.ms_entries) { + /* Empty DB, nothing to upgrade? */ + return 0; + } + if (isatty(2)) + enable_meter = !lutil_meter_open(&meter, + &lutil_meter_text_display, + &lutil_meter_linear_estimator, + st.ms_entries); + } + + num = ch_malloc(STACKSIZ * (sizeof(ID) + sizeof(rec))); + stack = (rec *)(num + STACKSIZ); + + rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt); + if (rc) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin failed, %s (%d)\n", + mdb_strerror(rc), rc, 0 ); + goto leave; + } + rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc); + if (rc) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open failed, %s (%d)\n", + mdb_strerror(rc), rc, 0 ); + goto leave; + } + + key.mv_size = sizeof(ID); + /* post-order depth-first update */ + for(;;) { + size_t dkids; + unsigned char *ptr; + + /* visit */ + key.mv_data = &id; + stack[depth].id = id; + rc = mdb_cursor_get(mc, &key, &data, MDB_SET); + if (rc) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get failed, %s (%d)\n", + mdb_strerror(rc), rc, 0 ); + goto leave; + } + num[depth] = 1; + + rc = mdb_cursor_count(mc, &dkids); + if (rc) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_count failed, %s (%d)\n", + mdb_strerror(rc), rc, 0 ); + goto leave; + } + if (dkids > 1) { + rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP); +down: + ptr = (unsigned char *)data.mv_data + data.mv_size - sizeof(ID); + memcpy(&id, ptr, sizeof(ID)); + depth++; + memcpy(stack[depth].rdn, data.mv_data, data.mv_size); + stack[depth].len = data.mv_size; + continue; + } + + + /* pop: write updated count, advance to next node */ +pop: + /* update superior counts */ + if (depth) + num[depth-1] += num[depth]; + + key.mv_data = &id; + id = stack[depth-1].id; + data.mv_data = stack[depth].rdn; + data.mv_size = stack[depth].len; + rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH); + if (rc) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(BOTH) failed, %s (%d)\n", + mdb_strerror(rc), rc, 0 ); + goto leave; + } + data.mv_data = stack[depth].rdn; + ptr = (unsigned char *)data.mv_data + data.mv_size; + memcpy(ptr, &num[depth], sizeof(ID)); + data.mv_size += sizeof(ID); + rc = mdb_cursor_del(mc, 0); + if (rc) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_del failed, %s (%d)\n", + mdb_strerror(rc), rc, 0 ); + goto leave; + } + rc = mdb_cursor_put(mc, &key, &data, 0); + if (rc) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_put failed, %s (%d)\n", + mdb_strerror(rc), rc, 0 ); + goto leave; + } + count++; +#if 1 + if (enable_meter) + lutil_meter_update(&meter, count, 0); +#else + { + int len; + ptr = data.mv_data; + len = (ptr[0] & 0x7f) << 8 | ptr[1]; + printf("ID: %zu, %zu, %.*s\n", stack[depth].id, num[depth], len, ptr+2); + } +#endif + writes++; + if (writes == 1000) { + mdb_cursor_close(mc); + rc = mdb_txn_commit(mt); + if (rc) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit failed, %s (%d)\n", + mdb_strerror(rc), rc, 0 ); + goto leave; + } + rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt); + if (rc) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin(2) failed, %s (%d)\n", + mdb_strerror(rc), rc, 0 ); + goto leave; + } + rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc); + if (rc) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open(2) failed, %s (%d)\n", + mdb_strerror(rc), rc, 0 ); + goto leave; + } + rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH); + if (rc) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(2) failed, %s (%d)\n", + mdb_strerror(rc), rc, 0 ); + goto leave; + } + writes = 0; + } + depth--; + + rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP); + if (rc == 0) + goto down; + rc = 0; + if (depth) + goto pop; + else + break; + } +leave: + mdb_cursor_close(mc); + if (mt) { + int r2; + r2 = mdb_txn_commit(mt); + if (r2) { + Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit(2) failed, %s (%d)\n", + mdb_strerror(r2), r2, 0 ); + if (!rc) + rc = r2; + } + } + ch_free(num); + if (enable_meter) { + lutil_meter_update(&meter, count, 1); + lutil_meter_close(&meter); + } + return rc; +}