Apache Solr open-source search software
LUCENE-2281: add doBeforeFlush; make both do{Before,After}Flush protected
Michael McCandless4eef6adaaf6d
diff --git a/CHANGES.txt b/CHANGES.txtindex 8029bc2f565..959961b34de 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -64,6 +64,10 @@ API Changesfiles are no longer open by IndexReaders. (luocanrao via MikeMcCandless)+* 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 Mike McCandless)+Bug fixes* LUCENE-2119: Don't throw NegativeArraySizeException if you passdiff --git a/src/java/org/apache/lucene/index/IndexWriter.java b/src/java/org/apache/lucene/index/IndexWriter.javaindex dd8eadcb511..d287183185c 100644--- a/src/java/org/apache/lucene/index/IndexWriter.java+++ b/src/java/org/apache/lucene/index/IndexWriter.java@@ -3312,12 +3312,18 @@ public class IndexWriter implements Closeable {}}- // 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.*@@ -3525,6 +3531,8 @@ public class IndexWriter implements Closeable {assert testPoint("startDoFlush");+ doBeforeFlush();+flushCount++;// If we are flushing because too many deletesdiff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.javaindex 5aad8488e5d..8d199655639 100644--- a/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -3174,16 +3174,22 @@ public class TestIndexWriter extends LuceneTestCase {super(dir, a, create, mfl);}- boolean wasCalled;+ boolean afterWasCalled;+ boolean beforeWasCalled;@Overridepublic 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 @@ public class TestIndexWriter extends LuceneTestCase {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);