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.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -291,7 +292,7 @@ public abstract class DirectoryReader extends BaseCompositeReader<AtomicReader>
// IOException allowed to throw there, in case
// segments_N is corrupt
sis.read(dir, fileName);
} catch (FileNotFoundException fnfe) {
} catch (FileNotFoundException | NoSuchFileException fnfe) {
// LUCENE-948: on NFS (and maybe others), if
// you have writers switching back and forth
// 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.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -170,7 +171,7 @@ final class IndexFileDeleter implements Closeable {
SegmentInfos sis = new SegmentInfos();
try {
sis.read(directory, fileName);
} catch (FileNotFoundException e) {
} catch (FileNotFoundException | NoSuchFileException e) {
// LUCENE-948: on NFS (and maybe others), if
// you have writers switching back and forth
// between machines, it's very likely that the

View File

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

View File

@ -21,6 +21,7 @@ import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Closeable;
import java.nio.file.NoSuchFileException;
import java.util.Collection; // for javadocs
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
* following contract:
* <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.
* </ul>
*
* @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
* length.
*/
@ -106,7 +107,9 @@ public abstract class Directory implements Closeable {
* the only Directory implementations that respect this
* parameter are {@link FSDirectory} and {@link
* CompoundFileDirectory}.
*/
* <p>Throws {@link FileNotFoundException} or {@link NoSuchFileException}
* if the file does not exist.
*/
public abstract IndexInput openInput(String name, IOContext context) throws IOException;
/** 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
* single file handle. The underlying file handle is kept open until the
* {@link IndexInputSlicer} is closed.
* <p>Throws {@link FileNotFoundException} or {@link NoSuchFileException}
* if the file does not exist.
*
* @throws IOException
* if an {@link IOException} occurs

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -155,10 +155,8 @@ public class LocalReplicatorTest extends ReplicatorTestCase {
try {
replicator.obtainFile(res.id, res.sourceFiles.keySet().iterator().next(), "madeUpFile");
fail("should have failed obtaining an unrecognized file");
} catch (FileNotFoundException e) {
} catch (FileNotFoundException | NoSuchFileException e) {
// 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.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -385,7 +386,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
if (randomState.nextBoolean()) {
throw new IOException("a random IOException (" + name + ")");
} 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();
}
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
@ -920,7 +921,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
throws IOException {
maybeYield();
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
// output, except for segments.gen and segments_N