Skip to content

Apache Lucene open-source search software

LUCENE-1453: Fix IndexReader.open() to close directory, when open() of the underlying DirectoryReader failed.

Uwe Schindlere3c3d443a8af
diff --git a/src/java/org/apache/lucene/index/IndexReader.java b/src/java/org/apache/lucene/index/IndexReader.java
index cd6e7fba1e0..8dd89d8be49 100644
--- a/src/java/org/apache/lucene/index/IndexReader.java
+++ b/src/java/org/apache/lucene/index/IndexReader.java
@@ -208,7 +208,7 @@ public abstract class IndexReader implements Cloneable {
* Use {@link #open(Directory, boolean)} instead
* @param path the path to the index directory */
public static IndexReader open(String path) throws CorruptIndexException, IOException {
- return new DirectoryOwningReader(open(FSDirectory.getDirectory(path), null, null, false));
+ return open(path, false);
}
/** Returns an IndexReader reading the index in an
@@ -225,7 +225,15 @@ public abstract class IndexReader implements Cloneable {
* Use {@link #open(Directory, boolean)} instead
*/
public static IndexReader open(String path, boolean readOnly) throws CorruptIndexException, IOException {
- return new DirectoryOwningReader(open(FSDirectory.getDirectory(path), null, null, readOnly));
+ final Directory dir = FSDirectory.getDirectory(path);
+ IndexReader r = null;
+ try {
+ r = open(dir, null, null, readOnly);
+ } finally {
+ if (r == null)
+ dir.close();
+ }
+ return new DirectoryOwningReader(r);
}
/** Returns a read/write IndexReader reading the index in an FSDirectory in the named
@@ -237,7 +245,7 @@ public abstract class IndexReader implements Cloneable {
* Use {@link #open(Directory, boolean)} instead
*/
public static IndexReader open(File path) throws CorruptIndexException, IOException {
- return new DirectoryOwningReader(open(FSDirectory.getDirectory(path), null, null, false));
+ return open(path, false);
}
/** Returns an IndexReader reading the index in an
@@ -254,7 +262,15 @@ public abstract class IndexReader implements Cloneable {
* Use {@link #open(Directory, boolean)} instead
*/
public static IndexReader open(File path, boolean readOnly) throws CorruptIndexException, IOException {
- return new DirectoryOwningReader(open(FSDirectory.getDirectory(path), null, null, readOnly));
+ final Directory dir = FSDirectory.getDirectory(path);
+ IndexReader r = null;
+ try {
+ r = open(dir, null, null, readOnly);
+ } finally {
+ if (r == null)
+ dir.close();
+ }
+ return new DirectoryOwningReader(r);
}
/** Returns a read/write IndexReader reading the index in
diff --git a/src/test/org/apache/lucene/index/TestIndexReader.java b/src/test/org/apache/lucene/index/TestIndexReader.java
index daadb71bf6d..8de439662dc 100644
--- a/src/test/org/apache/lucene/index/TestIndexReader.java
+++ b/src/test/org/apache/lucene/index/TestIndexReader.java
@@ -1202,6 +1202,8 @@ public class TestIndexReader extends LuceneTestCase
} catch (FileNotFoundException e) {
// expected
}
+
+ dir.close();
}
private void deleteReaderReaderConflict(boolean optimize) throws IOException
@@ -1543,7 +1545,7 @@ public class TestIndexReader extends LuceneTestCase
File indexDir = new File(tempDir, "lucenetestdiralreadyclosed");
try {
- FSDirectory dir = FSDirectory.open(indexDir);
+ FSDirectory dir = FSDirectory.getDirectory(indexDir);
IndexWriter w = new IndexWriter(indexDir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
w.setUseCompoundFile(false);
Document doc = new Document();
@@ -1631,6 +1633,7 @@ public class TestIndexReader extends LuceneTestCase
} catch (NoSuchDirectoryException nsde) {
// expected
}
+ dir.close();
}
// LUCENE-1509