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
This commit is contained in:
Shai Erera 2010-04-14 08:28:51 +00:00
parent 994189c221
commit 8703572fc2
6 changed files with 33 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;
}

View File

@ -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 &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.
*/
public abstract long fileLength(String name) throws IOException;
/** Creates a new, empty file in the directory with the given name.

View File

@ -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();
}