LUCENE-5047: Handle NoSuchFileException of Java 7 like FileNotFoundException when opeining index files; document this in Directory.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1491992 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2013-06-11 22:26:08 +00:00
parent fc4d7d3550
commit a62bdaa944
11 changed files with 33 additions and 23 deletions

View File

@ -19,6 +19,7 @@ package org.apache.lucene.index;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -291,7 +292,7 @@ public abstract class DirectoryReader extends BaseCompositeReader<AtomicReader>
// IOException allowed to throw there, in case // IOException allowed to throw there, in case
// segments_N is corrupt // segments_N is corrupt
sis.read(dir, fileName); sis.read(dir, fileName);
} catch (FileNotFoundException fnfe) { } catch (FileNotFoundException | NoSuchFileException fnfe) {
// LUCENE-948: on NFS (and maybe others), if // LUCENE-948: on NFS (and maybe others), if
// you have writers switching back and forth // you have writers switching back and forth
// between machines, it's very likely that the // between machines, it's very likely that the

View File

@ -20,6 +20,7 @@ package org.apache.lucene.index;
import java.io.Closeable; import java.io.Closeable;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -170,7 +171,7 @@ final class IndexFileDeleter implements Closeable {
SegmentInfos sis = new SegmentInfos(); SegmentInfos sis = new SegmentInfos();
try { try {
sis.read(directory, fileName); sis.read(directory, fileName);
} catch (FileNotFoundException e) { } catch (FileNotFoundException | NoSuchFileException e) {
// LUCENE-948: on NFS (and maybe others), if // LUCENE-948: on NFS (and maybe others), if
// you have writers switching back and forth // you have writers switching back and forth
// between machines, it's very likely that the // between machines, it's very likely that the

View File

@ -17,7 +17,6 @@ package org.apache.lucene.index;
* limitations under the License. * limitations under the License.
*/ */
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -21,6 +21,7 @@ import java.io.EOFException;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.Closeable; import java.io.Closeable;
import java.nio.file.NoSuchFileException;
import java.util.Collection; // for javadocs import java.util.Collection; // for javadocs
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
@ -70,12 +71,12 @@ public abstract class Directory implements Closeable {
* Returns the length of a file in the directory. This method follows the * Returns the length of a file in the directory. This method follows the
* following contract: * following contract:
* <ul> * <ul>
* <li>Throws {@link FileNotFoundException} if the file does not exist * <li>Throws {@link FileNotFoundException} or {@link NoSuchFileException}
* if the file does not exist.
* <li>Returns a value &ge;0 if the file exists, which specifies its length. * <li>Returns a value &ge;0 if the file exists, which specifies its length.
* </ul> * </ul>
* *
* @param name the name of the file for which to return the length. * @param name the name of the file for which to return the length.
* @throws FileNotFoundException if the file does not exist.
* @throws IOException if there was an IO error while retrieving the file's * @throws IOException if there was an IO error while retrieving the file's
* length. * length.
*/ */
@ -106,7 +107,9 @@ public abstract class Directory implements Closeable {
* the only Directory implementations that respect this * the only Directory implementations that respect this
* parameter are {@link FSDirectory} and {@link * parameter are {@link FSDirectory} and {@link
* CompoundFileDirectory}. * CompoundFileDirectory}.
*/ * <p>Throws {@link FileNotFoundException} or {@link NoSuchFileException}
* if the file does not exist.
*/
public abstract IndexInput openInput(String name, IOContext context) throws IOException; public abstract IndexInput openInput(String name, IOContext context) throws IOException;
/** Construct a {@link Lock}. /** Construct a {@link Lock}.
@ -223,6 +226,8 @@ public abstract class Directory implements Closeable {
* efficiently open one or more sliced {@link IndexInput} instances from a * efficiently open one or more sliced {@link IndexInput} instances from a
* single file handle. The underlying file handle is kept open until the * single file handle. The underlying file handle is kept open until the
* {@link IndexInputSlicer} is closed. * {@link IndexInputSlicer} is closed.
* <p>Throws {@link FileNotFoundException} or {@link NoSuchFileException}
* if the file does not exist.
* *
* @throws IOException * @throws IOException
* if an {@link IOException} occurs * if an {@link IOException} occurs

View File

@ -19,6 +19,7 @@ package org.apache.lucene.index;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -888,7 +889,7 @@ public class TestAddIndexes extends LuceneTestCase {
if (t instanceof AlreadyClosedException || t instanceof MergePolicy.MergeAbortedException || t instanceof NullPointerException) { if (t instanceof AlreadyClosedException || t instanceof MergePolicy.MergeAbortedException || t instanceof NullPointerException) {
report = !didClose; report = !didClose;
} else if (t instanceof FileNotFoundException) { } else if (t instanceof FileNotFoundException || t instanceof NoSuchFileException) {
report = !didClose; report = !didClose;
} else if (t instanceof IOException) { } else if (t instanceof IOException) {
Throwable t2 = t.getCause(); Throwable t2 = t.getCause();

View File

@ -20,6 +20,7 @@ package org.apache.lucene.index;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -428,8 +429,8 @@ void assertTermDocsCount(String msg,
} }
try { try {
DirectoryReader.open(fileDirName); DirectoryReader.open(fileDirName);
fail("opening DirectoryReader on empty directory failed to produce FileNotFoundException"); fail("opening DirectoryReader on empty directory failed to produce FileNotFoundException/NoSuchFileException");
} catch (FileNotFoundException e) { } catch (FileNotFoundException | NoSuchFileException e) {
// GOOD // GOOD
} }
rmDir(fileDirName); rmDir(fileDirName);
@ -470,8 +471,8 @@ public void testFilesOpenClose() throws IOException {
Directory dir = newFSDirectory(dirFile); Directory dir = newFSDirectory(dirFile);
try { try {
DirectoryReader.open(dir); DirectoryReader.open(dir);
fail("expected FileNotFoundException"); fail("expected FileNotFoundException/NoSuchFileException");
} catch (FileNotFoundException e) { } catch (FileNotFoundException | NoSuchFileException e) {
// expected // expected
} }
@ -480,8 +481,8 @@ public void testFilesOpenClose() throws IOException {
// Make sure we still get a CorruptIndexException (not NPE): // Make sure we still get a CorruptIndexException (not NPE):
try { try {
DirectoryReader.open(dir); DirectoryReader.open(dir);
fail("expected FileNotFoundException"); fail("expected FileNotFoundException/NoSuchFileException");
} catch (FileNotFoundException e) { } catch (FileNotFoundException | NoSuchFileException e) {
// expected // expected
} }

View File

@ -21,6 +21,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -1671,7 +1672,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase {
} catch (CorruptIndexException ex) { } catch (CorruptIndexException ex) {
// Exceptions are fine - we are running out of file handlers here // Exceptions are fine - we are running out of file handlers here
continue; continue;
} catch (FileNotFoundException ex) { } catch (FileNotFoundException | NoSuchFileException ex) {
continue; continue;
} }
failure.clearDoFail(); failure.clearDoFail();

View File

@ -19,6 +19,7 @@ package org.apache.lucene.index;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.NoSuchFileException;
import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.index.IndexWriterConfig.OpenMode; import org.apache.lucene.index.IndexWriterConfig.OpenMode;
@ -37,10 +38,10 @@ public class TestIndexWriterLockRelease extends LuceneTestCase {
Directory dir = newFSDirectory(_TestUtil.getTempDir("testLockRelease")); Directory dir = newFSDirectory(_TestUtil.getTempDir("testLockRelease"));
try { try {
new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND)); new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND));
} catch (FileNotFoundException e) { } catch (FileNotFoundException | NoSuchFileException e) {
try { try {
new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND)); new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND));
} catch (FileNotFoundException e1) { } catch (FileNotFoundException | NoSuchFileException e1) {
} }
} finally { } finally {
dir.close(); dir.close();

View File

@ -20,6 +20,7 @@ package org.apache.lucene.store;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.Arrays; import java.util.Arrays;
import org.apache.lucene.store.MockDirectoryWrapper.Throttling; import org.apache.lucene.store.MockDirectoryWrapper.Throttling;
@ -98,7 +99,7 @@ public class TestDirectory extends LuceneTestCase {
try { try {
IndexInput input = dir.openInput(file, newIOContext(random())); IndexInput input = dir.openInput(file, newIOContext(random()));
input.close(); input.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException | NoSuchFileException e) {
// ignore // ignore
} catch (IOException e) { } catch (IOException e) {
if (e.getMessage().contains("still open for writing")) { if (e.getMessage().contains("still open for writing")) {

View File

@ -155,10 +155,8 @@ public class LocalReplicatorTest extends ReplicatorTestCase {
try { try {
replicator.obtainFile(res.id, res.sourceFiles.keySet().iterator().next(), "madeUpFile"); replicator.obtainFile(res.id, res.sourceFiles.keySet().iterator().next(), "madeUpFile");
fail("should have failed obtaining an unrecognized file"); fail("should have failed obtaining an unrecognized file");
} catch (FileNotFoundException e) { } catch (FileNotFoundException | NoSuchFileException e) {
// expected // expected
} catch (NoSuchFileException e) {
// expected (only java 1.7)
} }
} }

View File

@ -20,6 +20,7 @@ package org.apache.lucene.store;
import java.io.Closeable; import java.io.Closeable;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -385,7 +386,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
if (randomState.nextBoolean()) { if (randomState.nextBoolean()) {
throw new IOException("a random IOException (" + name + ")"); throw new IOException("a random IOException (" + name + ")");
} else { } else {
throw new FileNotFoundException("a random IOException (" + name + ")"); throw randomState.nextBoolean() ? new FileNotFoundException("a random IOException (" + name + ")") : new NoSuchFileException("a random IOException (" + name + ")");
} }
} }
} }
@ -544,7 +545,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
maybeThrowDeterministicException(); maybeThrowDeterministicException();
} }
if (!delegate.fileExists(name)) { if (!delegate.fileExists(name)) {
throw new FileNotFoundException(name + " in dir=" + delegate); throw randomState.nextBoolean() ? new FileNotFoundException(name + " in dir=" + delegate) : new NoSuchFileException(name + " in dir=" + delegate);
} }
// cannot open a file for input if it's still open for // cannot open a file for input if it's still open for
@ -920,7 +921,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
throws IOException { throws IOException {
maybeYield(); maybeYield();
if (!delegate.fileExists(name)) { if (!delegate.fileExists(name)) {
throw new FileNotFoundException(name); throw randomState.nextBoolean() ? new FileNotFoundException(name) : new NoSuchFileException(name);
} }
// cannot open a file for input if it's still open for // cannot open a file for input if it's still open for
// output, except for segments.gen and segments_N // output, except for segments.gen and segments_N