mirror of https://github.com/apache/lucene.git
LUCENE-1453: Fix IndexReader.open() to close directory, when open() of the underlying DirectoryReader failed.
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@783314 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
cd7436e1e8
commit
e3c3d443a8
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue