lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r709456 - in /lucene/java/trunk/src: java/org/apache/lucene/index/DirectoryIndexReader.java test/org/apache/lucene/index/TestIndexReader.java
Date Fri, 31 Oct 2008 16:09:13 GMT
Author: mikemccand
Date: Fri Oct 31 09:09:12 2008
New Revision: 709456

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

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/index/DirectoryIndexReader.java
    lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReader.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/DirectoryIndexReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/DirectoryIndexReader.java?rev=709456&r1=709455&r2=709456&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/DirectoryIndexReader.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/DirectoryIndexReader.java Fri Oct 31
09:09:12 2008
@@ -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/trunk/src/test/org/apache/lucene/index/TestIndexReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReader.java?rev=709456&r1=709455&r2=709456&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReader.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReader.java Fri Oct 31 09:09:12
2008
@@ -1386,4 +1386,44 @@
 
       r3.close();
     }
+
+    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());
+
+        IndexReader r = null;
+        try {
+          r = 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