Implement names for "segments" and "deletable" files in Constants. This removes "hardcoded" filenames and makes code cleaner and easier to maintain.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@180349 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bernhard Messer 2005-06-06 17:52:12 +00:00
parent 3f2a20e6f7
commit 0532c41143
5 changed files with 21 additions and 13 deletions

View File

@ -23,6 +23,7 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.Lock;
import org.apache.lucene.util.Constants;
import java.io.File;
import java.io.FileOutputStream;
@ -199,7 +200,7 @@ public abstract class IndexReader {
* @deprecated Replaced by {@link #getCurrentVersion(File)}
* */
public static long lastModified(File directory) throws IOException {
return FSDirectory.fileModified(directory, "segments");
return FSDirectory.fileModified(directory, Constants.INDEX_SEGMENTS_FILENAME);
}
/**
@ -214,7 +215,7 @@ public abstract class IndexReader {
* @deprecated Replaced by {@link #getCurrentVersion(Directory)}
* */
public static long lastModified(Directory directory) throws IOException {
return directory.fileModified("segments");
return directory.fileModified(Constants.INDEX_SEGMENTS_FILENAME);
}
/**
@ -316,7 +317,7 @@ public abstract class IndexReader {
* @return <code>true</code> if an index exists; <code>false</code> otherwise
*/
public static boolean indexExists(String directory) {
return (new File(directory, "segments")).exists();
return (new File(directory, Constants.INDEX_SEGMENTS_FILENAME)).exists();
}
/**
@ -326,7 +327,7 @@ public abstract class IndexReader {
* @return <code>true</code> if an index exists; <code>false</code> otherwise
*/
public static boolean indexExists(File directory) {
return (new File(directory, "segments")).exists();
return (new File(directory, Constants.INDEX_SEGMENTS_FILENAME)).exists();
}
/**
@ -337,7 +338,7 @@ public abstract class IndexReader {
* @throws IOException if there is a problem with accessing the index
*/
public static boolean indexExists(Directory directory) throws IOException {
return directory.fileExists("segments");
return directory.fileExists(Constants.INDEX_SEGMENTS_FILENAME);
}
/** Returns the number of documents in this index. */

View File

@ -28,6 +28,7 @@ import org.apache.lucene.store.Lock;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.util.Constants;
import org.apache.lucene.document.Document;
import org.apache.lucene.analysis.Analyzer;
@ -789,10 +790,10 @@ public class IndexWriter {
private final Vector readDeleteableFiles() throws IOException {
Vector result = new Vector();
if (!directory.fileExists("deletable"))
if (!directory.fileExists(Constants.INDEX_DELETABLE_FILENAME))
return result;
IndexInput input = directory.openInput("deletable");
IndexInput input = directory.openInput(Constants.INDEX_DELETABLE_FILENAME);
try {
for (int i = input.readInt(); i > 0; i--) // read file names
result.addElement(input.readString());
@ -811,6 +812,6 @@ public class IndexWriter {
} finally {
output.close();
}
directory.renameFile("deleteable.new", "deletable");
directory.renameFile("deleteable.new", Constants.INDEX_DELETABLE_FILENAME);
}
}

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.Constants;
final class SegmentInfos extends Vector {
@ -41,7 +42,7 @@ final class SegmentInfos extends Vector {
public final void read(Directory directory) throws IOException {
IndexInput input = directory.openInput("segments");
IndexInput input = directory.openInput(Constants.INDEX_SEGMENTS_FILENAME);
try {
int format = input.readInt();
if(format < 0){ // file contains explicit format info
@ -91,7 +92,7 @@ final class SegmentInfos extends Vector {
}
// install new segment info
directory.renameFile("segments.new", "segments");
directory.renameFile("segments.new", Constants.INDEX_SEGMENTS_FILENAME);
}
/**
@ -107,7 +108,7 @@ final class SegmentInfos extends Vector {
public static long readCurrentVersion(Directory directory)
throws IOException {
IndexInput input = directory.openInput("segments");
IndexInput input = directory.openInput(Constants.INDEX_SEGMENTS_FILENAME);
int format = 0;
long version = 0;
try {

View File

@ -52,8 +52,8 @@ public class FSDirectory extends Directory {
if (name.endsWith("."+IndexReader.FILENAME_EXTENSIONS[i]))
return true;
}
if (name.equals("deletable")) return true;
else if (name.equals("segments")) return true;
if (name.equals(Constants.INDEX_DELETABLE_FILENAME)) return true;
else if (name.equals(Constants.INDEX_SEGMENTS_FILENAME)) return true;
else if (name.matches(".+\\.f\\d+")) return true;
return false;
}

View File

@ -26,6 +26,11 @@ package org.apache.lucene.util;
public final class Constants {
private Constants() {} // can't construct
/** Name of the index segment file */
public static final String INDEX_SEGMENTS_FILENAME = "segments";
/** Name of the index deletable file */
public static final String INDEX_DELETABLE_FILENAME = "deletable";
/** The value of <tt>System.getProperty("java.version")<tt>. **/
public static final String JAVA_VERSION = System.getProperty("java.version");
/** True iff this is Java version 1.1. */