You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2012/03/15 19:56:12 UTC

svn commit: r1301142 - in /lucene/dev/trunk/lucene: CHANGES.txt core/src/java/org/apache/lucene/index/IndexWriter.java core/src/test/org/apache/lucene/index/TestDirectoryReader.java core/src/test/org/apache/lucene/index/TestIndexWriter.java

Author: mikemccand
Date: Thu Mar 15 18:56:11 2012
New Revision: 1301142

URL: http://svn.apache.org/viewvc?rev=1301142&view=rev
Log:
LUCENE-3872: catch case where app calls close() after prepareCommit() without the matched commit()

Modified:
    lucene/dev/trunk/lucene/CHANGES.txt
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
    lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java
    lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java

Modified: lucene/dev/trunk/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/CHANGES.txt?rev=1301142&r1=1301141&r2=1301142&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/CHANGES.txt (original)
+++ lucene/dev/trunk/lucene/CHANGES.txt Thu Mar 15 18:56:11 2012
@@ -933,6 +933,10 @@ Bug fixes
 * LUCENE-3841: Fix CloseableThreadLocal to also purge stale entries on
   get(); this fixes certain cases where we were holding onto objects
   for dead threads for too long (Matthew Bellew, Mike McCandless)
+
+* LUCENE-3872: IndexWriter.close() now throws IllegalStateException if
+  you call it after calling prepareCommit() without calling commit()
+  first.  (Tim Bogaert via Mike McCandless)
     
 Optimizations
 

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java?rev=1301142&r1=1301141&r2=1301142&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java Thu Mar 15 18:56:11 2012
@@ -834,6 +834,11 @@ public class IndexWriter implements Clos
   private void closeInternal(boolean waitForMerges) throws CorruptIndexException, IOException {
 
     try {
+
+      if (pendingCommit != null) {
+        throw new IllegalStateException("cannot close: prepareCommit was already called with no corresponding call to commit");
+      }
+
       if (infoStream.isEnabled("IW")) {
         infoStream.message("IW", "now flush at close waitForMerges=" + waitForMerges);
       }
@@ -2358,7 +2363,7 @@ public class IndexWriter implements Clos
    *  #rollback()} to revert the commit and undo all changes
    *  done since the writer was opened.</p>
    *
-   *  You can also just call {@link #commit(Map)} directly
+   *  <p>You can also just call {@link #commit(Map)} directly
    *  without prepareCommit first in which case that method
    *  will internally call prepareCommit.
    *

Modified: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java?rev=1301142&r1=1301141&r2=1301142&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java (original)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java Thu Mar 15 18:56:11 2012
@@ -924,6 +924,7 @@ public void testFilesOpenClose() throws 
     writer.addDocument(new Document());
     writer.prepareCommit();
     assertFalse(DirectoryReader.indexExists(dir));
+    writer.commit();
     writer.close();
     assertTrue(DirectoryReader.indexExists(dir));
     dir.close();

Modified: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java?rev=1301142&r1=1301141&r2=1301142&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java (original)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java Thu Mar 15 18:56:11 2012
@@ -1801,4 +1801,54 @@ public class TestIndexWriter extends Luc
     w.close();
     dir.close();
   }
+
+  // LUCENE-3872
+  public void testPrepareCommitThenClose() throws Exception {
+    Directory dir = newDirectory();
+    IndexWriter w = new IndexWriter(dir,
+                                    new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
+
+    w.prepareCommit();
+    try {
+      w.close();
+      fail("should have hit exception");
+    } catch (IllegalStateException ise) {
+      // expected
+    }
+    w.commit();
+    w.close();
+    IndexReader r = IndexReader.open(dir);
+    assertEquals(0, r.maxDoc());
+    r.close();
+    dir.close();
+  }
+
+  // LUCENE-3872
+  public void testPrepareCommitThenRollback() throws Exception {
+    Directory dir = newDirectory();
+    IndexWriter w = new IndexWriter(dir,
+                                    new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
+
+    w.prepareCommit();
+    w.rollback();
+    assertFalse(DirectoryReader.indexExists(dir));
+    dir.close();
+  }
+
+  // LUCENE-3872
+  public void testPrepareCommitThenRollback2() throws Exception {
+    Directory dir = newDirectory();
+    IndexWriter w = new IndexWriter(dir,
+                                    new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
+
+    w.commit();
+    w.addDocument(new Document());
+    w.prepareCommit();
+    w.rollback();
+    assertTrue(DirectoryReader.indexExists(dir));
+    IndexReader r = IndexReader.open(dir);
+    assertEquals(0, r.maxDoc());
+    r.close();
+    dir.close();
+  }
 }