lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r745794 - in /lucene/java/branches/lucene_2_4: ./ CHANGES.txt src/java/org/apache/lucene/index/DirectoryIndexReader.java src/test/org/apache/lucene/index/TestIndexReader.java
Date Thu, 19 Feb 2009 09:47:15 GMT
Author: mikemccand
Date: Thu Feb 19 09:47:15 2009
New Revision: 745794

URL: http://svn.apache.org/viewvc?rev=745794&view=rev
Log:
LUCENE-1430 (on 2.4 branch): don't throw false AlreadyClosedException when IndexReader.open
hits IOException on first try opening index

Modified:
    lucene/java/branches/lucene_2_4/   (props changed)
    lucene/java/branches/lucene_2_4/CHANGES.txt
    lucene/java/branches/lucene_2_4/src/java/org/apache/lucene/index/DirectoryIndexReader.java
    lucene/java/branches/lucene_2_4/src/test/org/apache/lucene/index/TestIndexReader.java

Propchange: lucene/java/branches/lucene_2_4/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Feb 19 09:47:15 2009
@@ -1 +1 @@
-/lucene/java/trunk:708549
+/lucene/java/trunk:708549,709456

Modified: lucene/java/branches/lucene_2_4/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/branches/lucene_2_4/CHANGES.txt?rev=745794&r1=745793&r2=745794&view=diff
==============================================================================
--- lucene/java/branches/lucene_2_4/CHANGES.txt (original)
+++ lucene/java/branches/lucene_2_4/CHANGES.txt Thu Feb 19 09:47:15 2009
@@ -19,6 +19,10 @@
    pending deletions, it could lead to later false AssertionError
    during IndexReader.open.  (Mike McCandless)
 
+4. LUCENE-1430: Fix false AlreadyClosedException from IndexReader.open
+   (masking an actual IOException) that takes String or File path.
+   (Mike McCandless)
+
 ======================= Release 2.4.0 2008-10-06 =======================
 
 Changes in backwards compatibility policy

Modified: lucene/java/branches/lucene_2_4/src/java/org/apache/lucene/index/DirectoryIndexReader.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/lucene_2_4/src/java/org/apache/lucene/index/DirectoryIndexReader.java?rev=745794&r1=745793&r2=745794&view=diff
==============================================================================
--- lucene/java/branches/lucene_2_4/src/java/org/apache/lucene/index/DirectoryIndexReader.java
(original)
+++ lucene/java/branches/lucene_2_4/src/java/org/apache/lucene/index/DirectoryIndexReader.java
Thu Feb 19 09:47:15 2009
@@ -96,26 +96,46 @@
         DirectoryIndexReader reader;
 
         if (infos.size() == 1) {          // index is optimized
-          reader = SegmentReader.get(readOnly, infos, infos.info(0), closeDirectory);
+          reader = SegmentReader.get(readOnly, infos, infos.info(0), false);
         } else if (readOnly) {
-          reader = new ReadOnlyMultiSegmentReader(directory, infos, closeDirectory);
+          reader = new ReadOnlyMultiSegmentReader(directory, infos, false);
         } else {
-          reader = new MultiSegmentReader(directory, infos, closeDirectory, false);
+          reader = new MultiSegmentReader(directory, infos, false, false);
         }
         reader.setDeletionPolicy(deletionPolicy);
+        reader.closeDirectory = closeDirectory;
         return reader;
       }
     };
 
-    if (commit == null)
-      return (DirectoryIndexReader) finder.run();
-    else {
-      if (directory != commit.getDirectory())
-        throw new IOException("the specified commit does not match the specified Directory");
-      // This can & will directly throw IOException if the
-      // specified commit point has been deleted:
-      return (DirectoryIndexReader) finder.doBody(commit.getSegmentsFileName());
+    DirectoryIndexReader reader = null;
+    try {
+      if (commit == null)
+        reader = (DirectoryIndexReader) finder.run();
+      else {
+        if (directory != commit.getDirectory())
+          throw new IOException("the specified commit does not match the specified Directory");
+        // This can & will directly throw IOException if the
+        // specified commit point has been deleted:
+        reader = (DirectoryIndexReader) finder.doBody(commit.getSegmentsFileName());
+      }
+    } finally {
+      // We passed false above for closeDirectory so that
+      // the directory would not be closed before we were
+      // done retrying, so at this point if we truly failed
+      // to open a reader, which means an exception is being
+      // thrown, then close the directory now:
+      if (reader == null && closeDirectory) {
+        try {
+          directory.close();
+        } catch (IOException ioe) {
+          // suppress, so we keep throwing original failure
+          // from opening the reader
+        }
+      }
     }
+
+    return reader;
   }
 
   public final synchronized IndexReader reopen() throws CorruptIndexException, IOException
{

Modified: lucene/java/branches/lucene_2_4/src/test/org/apache/lucene/index/TestIndexReader.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/lucene_2_4/src/test/org/apache/lucene/index/TestIndexReader.java?rev=745794&r1=745793&r2=745794&view=diff
==============================================================================
--- lucene/java/branches/lucene_2_4/src/test/org/apache/lucene/index/TestIndexReader.java
(original)
+++ lucene/java/branches/lucene_2_4/src/test/org/apache/lucene/index/TestIndexReader.java
Thu Feb 19 09:47:15 2009
@@ -1503,4 +1503,43 @@
     doc.add(new Field("id", id, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
     return doc;
   }
+
+  public void testFalseDirectoryAlreadyClosed() throws Throwable {
+
+    String tempDir = System.getProperty("java.io.tmpdir");
+    if (tempDir == null)
+      throw new RuntimeException("java.io.tmpdir undefined");
+    File indexDir = new File(tempDir, "lucenetestdiralreadyclosed");
+
+    try {
+      FSDirectory dir = FSDirectory.getDirectory(indexDir);
+      IndexWriter w = new IndexWriter(indexDir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
+      w.setUseCompoundFile(false);
+      Document doc = new Document();
+      w.addDocument(doc);
+      w.close();
+      assertTrue(new File(indexDir, "_0.fnm").delete());
+
+      try {
+        IndexReader.open(indexDir);
+        fail("did not hit expected exception");
+      } catch (AlreadyClosedException ace) {
+        fail("should not have hit AlreadyClosedException");
+      } catch (FileNotFoundException ioe) {
+        // expected
+      }
+
+      // Make sure we really did close the dir inside IndexReader.open
+      dir.close();
+
+      try {
+        dir.fileExists("hi");
+        fail("did not hit expected exception");
+      } catch (AlreadyClosedException ace) {
+        // expected
+      }
+    } finally {
+      _TestUtil.rmDir(indexDir);
+    }
+  }
 }



Mime
View raw message