SOLR-2992: Add support for IndexWriter.prepareCommit()

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1225954 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2011-12-30 22:41:50 +00:00
parent f5d1e967db
commit 52a47d09ba
6 changed files with 87 additions and 8 deletions

View File

@ -197,6 +197,9 @@ New Features
* SOLR-2481: Add support for commitWithin in DataImportHandler (Sami Siren via yonik)
* SOLR-2992: Add support for IndexWriter.prepareCommit() via prepareCommit=true
on update URLs. (yonik)
Optimizations
----------------------

View File

@ -97,16 +97,21 @@ public class RequestHandlerUtils
boolean optimize = params.getBool( UpdateParams.OPTIMIZE, false );
boolean commit = params.getBool( UpdateParams.COMMIT, false );
if( optimize || commit || force ) {
boolean prepareCommit = params.getBool( UpdateParams.PREPARE_COMMIT, false );
if( optimize || commit || prepareCommit || force ) {
CommitUpdateCommand cmd = new CommitUpdateCommand(req, optimize );
cmd.waitSearcher = params.getBool( UpdateParams.WAIT_SEARCHER, cmd.waitSearcher );
cmd.softCommit = params.getBool( UpdateParams.SOFT_COMMIT, cmd.softCommit );
cmd.expungeDeletes = params.getBool( UpdateParams.EXPUNGE_DELETES, cmd.expungeDeletes);
cmd.maxOptimizeSegments = params.getInt(UpdateParams.MAX_OPTIMIZE_SEGMENTS, cmd.maxOptimizeSegments);
cmd.prepareCommit = prepareCommit;
processor.processCommit( cmd );
return true;
}
return false;
}

View File

@ -27,6 +27,7 @@ public class CommitUpdateCommand extends UpdateCommand {
public boolean waitSearcher=true;
public boolean expungeDeletes = false;
public boolean softCommit = false;
public boolean prepareCommit = false;
/**
* During optimize, optimize down to <= this many segments. Must be >= 1
@ -41,10 +42,11 @@ public class CommitUpdateCommand extends UpdateCommand {
}
@Override
public String toString() {
return "commit(optimize="+optimize
return prepareCommit ? "prepareCommit" :
("commit(optimize="+optimize
+",waitSearcher="+waitSearcher
+",expungeDeletes="+expungeDeletes
+",softCommit="+softCommit
+')';
+')');
}
}

View File

@ -286,8 +286,32 @@ public class DirectUpdateHandler2 extends UpdateHandler {
return rc;
}
public void prepareCommit(CommitUpdateCommand cmd) throws IOException {
boolean error=true;
try {
log.info("start "+cmd);
IndexWriter writer = solrCoreState.getIndexWriter(core);
writer.prepareCommit();
log.info("end_prepareCommit");
error=false;
}
finally {
if (error) numErrors.incrementAndGet();
}
}
@Override
public void commit(CommitUpdateCommand cmd) throws IOException {
if (cmd.prepareCommit) {
prepareCommit(cmd);
return;
}
IndexWriter writer = solrCoreState.getIndexWriter(core);
if (cmd.optimize) {
optimizeCommands.incrementAndGet();
@ -374,7 +398,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
addCommands.set(0);
deleteByIdCommands.set(0);
deleteByQueryCommands.set(0);
numErrors.set(error ? 1 : 0);
if (error) numErrors.incrementAndGet();
}
// if we are supposed to wait for the searcher to be registered, then we should do it
@ -445,7 +469,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
deleteByIdCommandsCumulative.get() - deleteByIdCommands.getAndSet( 0 ) );
deleteByQueryCommandsCumulative.set(
deleteByQueryCommandsCumulative.get() - deleteByQueryCommands.getAndSet( 0 ) );
numErrors.set(error ? 1 : 0);
if (error) numErrors.incrementAndGet();
}
}

View File

@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.Directory;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.MapSolrParams;
@ -268,6 +269,47 @@ public class DirectUpdateHandlerTest extends SolrTestCaseJ4 {
sr.close();
}
@Test
public void testPrepareCommit() throws Exception {
SolrQueryRequest sr = req();
IndexReader r = sr.getSearcher().getTopReaderContext().reader;
Directory d = r.directory();
assertU(adoc("id", "1"));
int nFiles = d.listAll().length;
updateJ("", params("prepareCommit","true"));
assertTrue( d.listAll().length > nFiles); // make sure new index files were actually written
assertJQ(req("q", "id:1")
, "/response/numFound==0"
);
updateJ("", params("rollback","true"));
assertU(commit());
assertJQ(req("q", "id:1")
, "/response/numFound==0"
);
assertU(adoc("id","1"));
updateJ("", params("prepareCommit","true"));
assertJQ(req("q", "id:1")
, "/response/numFound==0"
);
assertU(commit());
assertJQ(req("q", "id:1")
, "/response/numFound==1"
);
sr.close();
}
}

View File

@ -36,13 +36,16 @@ public interface UpdateParams
/** Commit everything after the command completes */
public static String COMMIT = "commit";
/** Commit within a certain time period (in ms) */
public static String COMMIT_WITHIN = "commitWithin";
/** Optimize the index and commit everything after the command completes */
public static String OPTIMIZE = "optimize";
/** @expert calls IndexWriter.prepareCommit */
public static String PREPARE_COMMIT = "prepareCommit";
/** Rollback update commands */
public static String ROLLBACK = "rollback";