Skip to content

Apache Lucene open-source search software

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

Uwe Schindlera62bdaa94437
diff --git a/lucene/core/src/java/org/apache/lucene/index/DirectoryReader.java b/lucene/core/src/java/org/apache/lucene/index/DirectoryReader.java
index 788b157c8da..e2372a19298 100644
--- a/lucene/core/src/java/org/apache/lucene/index/DirectoryReader.java
+++ b/lucene/core/src/java/org/apache/lucene/index/DirectoryReader.java
@@ -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
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.java b/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.java
index c6ec5d03e23..9ff0005dc65 100644
--- a/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.java
+++ b/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.java
@@ -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
diff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java
index 61fd7171af4..cdba96b1391 100644
--- a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java
+++ b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java
@@ -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;
diff --git a/lucene/core/src/java/org/apache/lucene/store/Directory.java b/lucene/core/src/java/org/apache/lucene/store/Directory.java
index ca9b3dfd356..540465594a2 100644
--- a/lucene/core/src/java/org/apache/lucene/store/Directory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/Directory.java
@@ -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
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestAddIndexes.java b/lucene/core/src/test/org/apache/lucene/index/TestAddIndexes.java
index 88bbc3a0f0c..26b8e92c62e 100755
--- a/lucene/core/src/test/org/apache/lucene/index/TestAddIndexes.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestAddIndexes.java
@@ -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();
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java
index 9f8890b0b21..0ed240e57a8 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java
@@ -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
}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
index b65070b2f94..3eac5f0f888 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
@@ -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();
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java
index aaa0fb1338e..e6467f10508 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java
@@ -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();
diff --git a/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java
index 540362dfb0d..9e779f23677 100644
--- a/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java
+++ b/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java
@@ -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")) {
diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java b/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java
index c55f9c3af51..ef20967c6a2 100755
--- a/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java
+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java
@@ -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)
}
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java
index aeb80397c15..c193923f7a1 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java
@@ -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