Apache Solr open-source search software
LUCENE-2316: Define clear semantics for Directory.fileLength
Shai Erera8703572fc215
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex bd4d9c96301..00de2cfd877 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -111,6 +111,11 @@ Changes in backwards compatibility policybehavior you can call writer.commit()/close() immediately after you create it.(Shai Erera, Mike McCandless)+* LUCENE-2316: Directory.fileLength contract was clarified - it returns the+ actual file's length if the file exists, and throws FileNotFoundException+ otherwise. Returning length=0 for a non-existent file is no longer allowed. If+ you relied on that, make sure to catch the exception. (Shai Erera)+Changes in runtime behavior* LUCENE-1923: Made IndexReader.toString() produce somethingdiff --git a/lucene/contrib/db/bdb-je/src/java/org/apache/lucene/store/je/JEDirectory.java b/lucene/contrib/db/bdb-je/src/java/org/apache/lucene/store/je/JEDirectory.javaindex 9f96606a624..83c6b98386e 100644--- a/lucene/contrib/db/bdb-je/src/java/org/apache/lucene/store/je/JEDirectory.java+++ b/lucene/contrib/db/bdb-je/src/java/org/apache/lucene/store/je/JEDirectory.java@@ -19,6 +19,7 @@ package org.apache.lucene.store.je;import java.io.ByteArrayInputStream;import java.io.DataInputStream;+import java.io.FileNotFoundException;import java.io.IOException;import java.util.ArrayList;import java.util.Collections;@@ -133,7 +134,7 @@ public class JEDirectory extends Directory {if (file.exists(this))return file.getLength();- throw new IOException("File does not exist: " + name);+ throw new FileNotFoundException(name);}@Overridediff --git a/lucene/contrib/db/bdb/src/java/org/apache/lucene/store/db/DbDirectory.java b/lucene/contrib/db/bdb/src/java/org/apache/lucene/store/db/DbDirectory.javaindex d5d5137383b..74478b1ed09 100644--- a/lucene/contrib/db/bdb/src/java/org/apache/lucene/store/db/DbDirectory.java+++ b/lucene/contrib/db/bdb/src/java/org/apache/lucene/store/db/DbDirectory.java@@ -17,6 +17,7 @@ package org.apache.lucene.store.db;* limitations under the License.*/+import java.io.FileNotFoundException;import java.io.IOException;import java.io.ByteArrayInputStream;import java.io.DataInputStream;@@ -140,15 +141,13 @@ public class DbDirectory extends Directory {}@Override- public long fileLength(String name)- throws IOException- {+ public long fileLength(String name) throws IOException {File file = new File(name);if (file.exists(this))return file.getLength();- throw new IOException("File does not exist: " + name);+ throw new FileNotFoundException(name);}@Overridediff --git a/lucene/src/java/org/apache/lucene/index/CompoundFileReader.java b/lucene/src/java/org/apache/lucene/index/CompoundFileReader.javaindex 21bb9d1a278..c8e54908368 100644--- a/lucene/src/java/org/apache/lucene/index/CompoundFileReader.java+++ b/lucene/src/java/org/apache/lucene/index/CompoundFileReader.java@@ -24,6 +24,7 @@ import org.apache.lucene.store.IndexOutput;import org.apache.lucene.store.Lock;import java.util.HashMap;+import java.io.FileNotFoundException;import java.io.IOException;@@ -183,12 +184,10 @@ public class CompoundFileReader extends Directory {/** Returns the length of a file in the directory.* @throws IOException if the file does not exist */@Override- public long fileLength(String name)- throws IOException- {+ public long fileLength(String name) throws IOException {FileEntry e = entries.get(name);if (e == null)- throw new IOException("File " + name + " does not exist");+ throw new FileNotFoundException(name);return e.length;}diff --git a/lucene/src/java/org/apache/lucene/store/Directory.java b/lucene/src/java/org/apache/lucene/store/Directory.javaindex 0fc2ae3df00..3e4c7e31af1 100644--- a/lucene/src/java/org/apache/lucene/store/Directory.java+++ b/lucene/src/java/org/apache/lucene/store/Directory.java@@ -17,6 +17,7 @@ package org.apache.lucene.store;* limitations under the License.*/+import java.io.FileNotFoundException;import java.io.IOException;import java.io.Closeable;import java.util.Collection; // for javadocs@@ -73,9 +74,20 @@ public abstract class Directory implements Closeable {public abstract void deleteFile(String name)throws IOException;- /** Returns the length of a file in the directory. */- public abstract long fileLength(String name)- throws IOException;+ /**+ * 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>Returns a value ≥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.+ */+ public abstract long fileLength(String name) throws IOException;/** Creates a new, empty file in the directory with the given name.diff --git a/lucene/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/src/java/org/apache/lucene/store/FSDirectory.javaindex 3a0cab41611..7952d67e274 100644--- a/lucene/src/java/org/apache/lucene/store/FSDirectory.java+++ b/lucene/src/java/org/apache/lucene/store/FSDirectory.java@@ -19,6 +19,7 @@ package org.apache.lucene.store;import java.io.File;import java.io.FileInputStream;+import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FilenameFilter;import java.io.IOException;@@ -265,9 +266,12 @@ public abstract class FSDirectory extends Directory {/** Returns the length in bytes of a file in the directory. */@Override- public long fileLength(String name) {+ public long fileLength(String name) throws IOException {ensureOpen();File file = new File(directory, name);+ if (!file.exists()) {+ throw new FileNotFoundException(name);+ }return file.length();}