Index: CHANGES.txt =================================================================== --- CHANGES.txt (revision 915338) +++ CHANGES.txt (working copy) @@ -64,6 +64,10 @@ files are no longer open by IndexReaders. (luocanrao via Mike McCandless) +* LUCENE-2281: added doBeforeFlush to IndexWriter to allow extensions to perform + operations before flush starts. Also exposed doAfterFlush as protected instead + of package-private. (Shai Erera via ?) + Bug fixes * LUCENE-2119: Don't throw NegativeArraySizeException if you pass Index: src/java/org/apache/lucene/index/IndexWriter.java =================================================================== --- src/java/org/apache/lucene/index/IndexWriter.java (revision 915338) +++ src/java/org/apache/lucene/index/IndexWriter.java (working copy) @@ -3312,13 +3312,19 @@ } } - // This is called after pending added and deleted - // documents have been flushed to the Directory but before - // the change is committed (new segments_N file written). - void doAfterFlush() - throws IOException { - } + /** + * A hook for extending classes to execute operations after pending added and + * deleted documents have been flushed to the Directory but before the change + * is committed (new segments_N file written). + */ + protected void doAfterFlush() throws IOException {} + /** + * A hook for extending classes to execute operations before pending added and + * deleted documents are flushed to the Directory. + */ + protected void doBeforeFlush() throws IOException {} + /** Expert: prepare for commit. * *

NOTE: if this method hits an OutOfMemoryError @@ -3525,6 +3531,8 @@ assert testPoint("startDoFlush"); + doBeforeFlush(); + flushCount++; // If we are flushing because too many deletes Index: src/test/org/apache/lucene/index/TestIndexWriter.java =================================================================== --- src/test/org/apache/lucene/index/TestIndexWriter.java (revision 915338) +++ src/test/org/apache/lucene/index/TestIndexWriter.java (working copy) @@ -3174,16 +3174,22 @@ super(dir, a, create, mfl); } - boolean wasCalled; + boolean afterWasCalled; + boolean beforeWasCalled; @Override public void doAfterFlush() { - wasCalled = true; + afterWasCalled = true; } + + @Override + protected void doBeforeFlush() throws IOException { + beforeWasCalled = true; + } } // LUCENE-1222 - public void testDoAfterFlush() throws IOException { + public void testDoBeforeAfterFlush() throws IOException { MockRAMDirectory dir = new MockRAMDirectory(); MockIndexWriter3 w = new MockIndexWriter3(dir, new WhitespaceAnalyzer(TEST_VERSION_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); Document doc = new Document(); @@ -3191,11 +3197,14 @@ Field.Index.ANALYZED)); w.addDocument(doc); w.commit(); - assertTrue(w.wasCalled); - w.wasCalled = true; + assertTrue(w.beforeWasCalled); + assertTrue(w.afterWasCalled); + w.beforeWasCalled = false; + w.afterWasCalled = false; w.deleteDocuments(new Term("field", "field")); w.commit(); - assertTrue(w.wasCalled); + assertTrue(w.beforeWasCalled); + assertTrue(w.afterWasCalled); w.close(); IndexReader ir = IndexReader.open(dir, true);