LUCENE-978: ensure all streams are closed when we hit exception in constructor

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@567253 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2007-08-18 09:42:21 +00:00
parent 06d4849ca8
commit c1a76d9dbe
4 changed files with 97 additions and 44 deletions

View File

@ -72,6 +72,10 @@ Bug fixes
sharing an index over NFS and using a custom deletion policy (Mike sharing an index over NFS and using a custom deletion policy (Mike
McCandless) McCandless)
11. LUCENE-978: Ensure TermInfosReader, FieldsReader, and FieldsReader
close any streams they had opened if an exception is hit in the
constructor. (Ning Li via Mike McCandless)
New features New features
1. LUCENE-906: Elision filter for French. 1. LUCENE-906: Elision filter for French.

View File

@ -66,10 +66,13 @@ final class FieldsReader {
} }
FieldsReader(Directory d, String segment, FieldInfos fn, int readBufferSize, int docStoreOffset, int size) throws IOException { FieldsReader(Directory d, String segment, FieldInfos fn, int readBufferSize, int docStoreOffset, int size) throws IOException {
boolean success = false;
try {
fieldInfos = fn; fieldInfos = fn;
cloneableFieldsStream = d.openInput(segment + ".fdt", readBufferSize); cloneableFieldsStream = d.openInput(segment + ".fdt", readBufferSize);
fieldsStream = (IndexInput)cloneableFieldsStream.clone(); fieldsStream = (IndexInput) cloneableFieldsStream.clone();
indexStream = d.openInput(segment + ".fdx", readBufferSize); indexStream = d.openInput(segment + ".fdx", readBufferSize);
if (docStoreOffset != -1) { if (docStoreOffset != -1) {
@ -79,11 +82,23 @@ final class FieldsReader {
// Verify the file is long enough to hold all of our // Verify the file is long enough to hold all of our
// docs // docs
assert ((int) (indexStream.length()/8)) >= size + this.docStoreOffset; assert ((int) (indexStream.length() / 8)) >= size + this.docStoreOffset;
} else { } else {
this.docStoreOffset = 0; this.docStoreOffset = 0;
this.size = (int) (indexStream.length() >> 3); this.size = (int) (indexStream.length() >> 3);
} }
success = true;
} finally {
// With lock-less commits, it's entirely possible (and
// fine) to hit a FileNotFound exception above. In
// this case, we want to explicitly close any subset
// of things that were opened so that we don't have to
// wait for a GC to do so.
if (!success) {
close();
}
}
} }
/** /**
@ -103,9 +118,15 @@ final class FieldsReader {
*/ */
final void close() throws IOException { final void close() throws IOException {
if (!closed) { if (!closed) {
if (fieldsStream != null) {
fieldsStream.close(); fieldsStream.close();
}
if (cloneableFieldsStream != null) {
cloneableFieldsStream.close(); cloneableFieldsStream.close();
}
if (indexStream != null) {
indexStream.close(); indexStream.close();
}
IndexInput localFieldsStream = (IndexInput) fieldsStreamTL.get(); IndexInput localFieldsStream = (IndexInput) fieldsStreamTL.get();
if (localFieldsStream != null) { if (localFieldsStream != null) {
localFieldsStream.close(); localFieldsStream.close();

View File

@ -48,17 +48,31 @@ final class TermInfosReader {
TermInfosReader(Directory dir, String seg, FieldInfos fis, int readBufferSize) TermInfosReader(Directory dir, String seg, FieldInfos fis, int readBufferSize)
throws CorruptIndexException, IOException { throws CorruptIndexException, IOException {
boolean success = false;
try {
directory = dir; directory = dir;
segment = seg; segment = seg;
fieldInfos = fis; fieldInfos = fis;
origEnum = new SegmentTermEnum(directory.openInput(segment + ".tis", readBufferSize), origEnum = new SegmentTermEnum(directory.openInput(segment + ".tis",
fieldInfos, false); readBufferSize), fieldInfos, false);
size = origEnum.size; size = origEnum.size;
indexEnum = indexEnum = new SegmentTermEnum(directory.openInput(segment + ".tii",
new SegmentTermEnum(directory.openInput(segment + ".tii", readBufferSize), readBufferSize), fieldInfos, true);
fieldInfos, true);
success = true;
} finally {
// With lock-less commits, it's entirely possible (and
// fine) to hit a FileNotFound exception above. In
// this case, we want to explicitly close any subset
// of things that were opened so that we don't have to
// wait for a GC to do so.
if (!success) {
close();
}
}
} }
public int getSkipInterval() { public int getSkipInterval() {

View File

@ -53,6 +53,9 @@ class TermVectorsReader implements Cloneable {
TermVectorsReader(Directory d, String segment, FieldInfos fieldInfos, int readBufferSize, int docStoreOffset, int size) TermVectorsReader(Directory d, String segment, FieldInfos fieldInfos, int readBufferSize, int docStoreOffset, int size)
throws CorruptIndexException, IOException { throws CorruptIndexException, IOException {
boolean success = false;
try {
if (d.fileExists(segment + TermVectorsWriter.TVX_EXTENSION)) { if (d.fileExists(segment + TermVectorsWriter.TVX_EXTENSION)) {
tvx = d.openInput(segment + TermVectorsWriter.TVX_EXTENSION, readBufferSize); tvx = d.openInput(segment + TermVectorsWriter.TVX_EXTENSION, readBufferSize);
checkValidFormat(tvx); checkValidFormat(tvx);
@ -68,11 +71,22 @@ class TermVectorsReader implements Cloneable {
this.size = size; this.size = size;
// Verify the file is long enough to hold all of our // Verify the file is long enough to hold all of our
// docs // docs
assert ((int) (tvx.length()/8)) >= size + docStoreOffset; assert ((int) (tvx.length() / 8)) >= size + docStoreOffset;
} }
} }
this.fieldInfos = fieldInfos; this.fieldInfos = fieldInfos;
success = true;
} finally {
// With lock-less commits, it's entirely possible (and
// fine) to hit a FileNotFound exception above. In
// this case, we want to explicitly close any subset
// of things that were opened so that we don't have to
// wait for a GC to do so.
if (!success) {
close();
}
}
} }
private int checkValidFormat(IndexInput in) throws CorruptIndexException, IOException private int checkValidFormat(IndexInput in) throws CorruptIndexException, IOException