From 8703572fc2157a36811b04cf4b41af7d12df5a26 Mon Sep 17 00:00:00 2001 From: Shai Erera Date: Wed, 14 Apr 2010 08:28:51 +0000 Subject: [PATCH] LUCENE-2316: Define clear semantics for Directory.fileLength git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@933879 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 5 +++++ .../apache/lucene/store/je/JEDirectory.java | 3 ++- .../apache/lucene/store/db/DbDirectory.java | 7 +++---- .../lucene/index/CompoundFileReader.java | 7 +++---- .../org/apache/lucene/store/Directory.java | 18 +++++++++++++++--- .../org/apache/lucene/store/FSDirectory.java | 6 +++++- 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index bd4d9c96301..00de2cfd877 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -111,6 +111,11 @@ Changes in backwards compatibility policy behavior 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 something diff --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.java index 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); } @Override diff --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.java index 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); } @Override diff --git a/lucene/src/java/org/apache/lucene/index/CompoundFileReader.java b/lucene/src/java/org/apache/lucene/index/CompoundFileReader.java index 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.java index 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: + * + * + * @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.java index 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(); }