From 406454d30bc3022cc003fc81f7c6392e19b51da6 Mon Sep 17 00:00:00 2001
From: Uwe Schindler
Date: Sun, 7 Jun 2009 21:52:41 +0000
Subject: [PATCH] LUCENE-1672: Deprecate all String/File ctors/opens in
IndexReader/IndexWriter/IndexSearcher
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@782469 13f79535-47bb-0310-9956-ffa450edef68
---
CHANGES.txt | 6 ++
.../lucene/benchmark/byTask/PerfRunData.java | 2 +-
.../quality/utils/QualityQueriesFinder.java | 2 +-
.../standard/StandardBenchmarker.java | 2 +-
.../benchmark/quality/TestQualityRun.java | 2 +-
.../store/instantiated/TestIndicesEquals.java | 2 +-
.../lucene/index/FieldNormModifier.java | 3 +-
.../apache/lucene/misc/IndexMergeTool.java | 2 +-
.../lucene/misc/LengthNormModifier.java | 2 +-
.../org/apache/lucene/wordnet/SynExpand.java | 3 +-
.../org/apache/lucene/wordnet/SynLookup.java | 3 +-
.../org/apache/lucene/demo/DeleteFiles.java | 4 +-
.../org/apache/lucene/index/CheckIndex.java | 3 +-
.../apache/lucene/index/DirectoryReader.java | 16 +++--
.../apache/lucene/index/IndexModifier.java | 8 ++-
.../org/apache/lucene/index/IndexReader.java | 64 +++++++++++++------
.../org/apache/lucene/index/SegmentInfos.java | 16 +----
.../apache/lucene/search/IndexSearcher.java | 3 +-
src/java/overview.html | 2 +-
src/test/org/apache/lucene/TestDemo.java | 2 +-
.../lucene/TestSnapshotDeletionPolicy.java | 2 +-
21 files changed, 91 insertions(+), 58 deletions(-)
diff --git a/CHANGES.txt b/CHANGES.txt
index 411c94f6402..8d313745bbf 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -185,6 +185,12 @@ API Changes
iterator has exhausted. Otherwise it should return the current doc ID.
(Shai Erera via Mike McCandless)
+19. LUCENE-1672: All ctors/opens and other methods using String/File to
+ specify the directory in IndexReader, IndexWriter, and IndexSearcher
+ were deprecated. You should instantiate the Directory manually before
+ and pass it to these classes (LUCENE-1451, LUCENE-1658).
+ (Uwe Schindler)
+
Bug fixes
1. LUCENE-1415: MultiPhraseQuery has incorrect hashCode() and equals()
diff --git a/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/PerfRunData.java b/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/PerfRunData.java
index 178d69c13ac..4611d49099a 100644
--- a/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/PerfRunData.java
+++ b/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/PerfRunData.java
@@ -128,7 +128,7 @@ public class PerfRunData {
FileUtils.fullyDelete(indexDir);
}
indexDir.mkdirs();
- directory = FSDirectory.getDirectory(indexDir);
+ directory = FSDirectory.open(indexDir);
} else {
directory = new RAMDirectory();
}
diff --git a/contrib/benchmark/src/java/org/apache/lucene/benchmark/quality/utils/QualityQueriesFinder.java b/contrib/benchmark/src/java/org/apache/lucene/benchmark/quality/utils/QualityQueriesFinder.java
index c20c9286c21..632d7907295 100755
--- a/contrib/benchmark/src/java/org/apache/lucene/benchmark/quality/utils/QualityQueriesFinder.java
+++ b/contrib/benchmark/src/java/org/apache/lucene/benchmark/quality/utils/QualityQueriesFinder.java
@@ -52,7 +52,7 @@ public class QualityQueriesFinder {
System.err.println("Usage: java QualityQueriesFinder ");
System.exit(1);
}
- QualityQueriesFinder qqf = new QualityQueriesFinder(FSDirectory.getDirectory(new File(args[0])));
+ QualityQueriesFinder qqf = new QualityQueriesFinder(FSDirectory.open(new File(args[0])));
String q[] = qqf.bestQueries("body",20);
for (int i=0; i ");
}
- FSDirectory directory = FSDirectory.getDirectory(args[0], false);
+ FSDirectory directory = FSDirectory.open(new File(args[0]));
IndexSearcher searcher = new IndexSearcher(directory);
String query = args[1];
diff --git a/contrib/wordnet/src/java/org/apache/lucene/wordnet/SynLookup.java b/contrib/wordnet/src/java/org/apache/lucene/wordnet/SynLookup.java
index 255410935cc..509bbfc7fa6 100644
--- a/contrib/wordnet/src/java/org/apache/lucene/wordnet/SynLookup.java
+++ b/contrib/wordnet/src/java/org/apache/lucene/wordnet/SynLookup.java
@@ -19,6 +19,7 @@ package org.apache.lucene.wordnet;
import java.io.IOException;
import java.io.StringReader;
+import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
@@ -51,7 +52,7 @@ public class SynLookup {
"java org.apache.lucene.wordnet.SynLookup ");
}
- FSDirectory directory = FSDirectory.getDirectory(args[0], false);
+ FSDirectory directory = FSDirectory.open(new File(args[0]));
IndexSearcher searcher = new IndexSearcher(directory);
String word = args[1];
diff --git a/src/demo/org/apache/lucene/demo/DeleteFiles.java b/src/demo/org/apache/lucene/demo/DeleteFiles.java
index e8a00b97d11..99593bc7e1c 100644
--- a/src/demo/org/apache/lucene/demo/DeleteFiles.java
+++ b/src/demo/org/apache/lucene/demo/DeleteFiles.java
@@ -17,6 +17,8 @@ package org.apache.lucene.demo;
* limitations under the License.
*/
+import java.io.File;
+
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.index.IndexReader;
@@ -37,7 +39,7 @@ public class DeleteFiles {
System.exit(1);
}
try {
- Directory directory = FSDirectory.getDirectory("index");
+ Directory directory = FSDirectory.open(new File("index"));
IndexReader reader = IndexReader.open(directory);
Term term = new Term("path", args[0]);
diff --git a/src/java/org/apache/lucene/index/CheckIndex.java b/src/java/org/apache/lucene/index/CheckIndex.java
index 2181dbbd13a..0d5ce9e5d85 100644
--- a/src/java/org/apache/lucene/index/CheckIndex.java
+++ b/src/java/org/apache/lucene/index/CheckIndex.java
@@ -26,6 +26,7 @@ import org.apache.lucene.document.Document;
import java.text.NumberFormat;
import java.io.PrintStream;
import java.io.IOException;
+import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -702,7 +703,7 @@ public class CheckIndex {
System.out.println("\nOpening index @ " + indexPath + "\n");
Directory dir = null;
try {
- dir = FSDirectory.getDirectory(indexPath);
+ dir = FSDirectory.open(new File(indexPath));
} catch (Throwable t) {
System.out.println("ERROR: could not open directory \"" + indexPath + "\"; exiting");
t.printStackTrace(System.out);
diff --git a/src/java/org/apache/lucene/index/DirectoryReader.java b/src/java/org/apache/lucene/index/DirectoryReader.java
index 6dcfaabaa31..b3834419c9b 100644
--- a/src/java/org/apache/lucene/index/DirectoryReader.java
+++ b/src/java/org/apache/lucene/index/DirectoryReader.java
@@ -456,12 +456,16 @@ class DirectoryReader extends IndexReader implements Cloneable {
DirectoryReader reader = null;
- // While trying to reopen, we temporarily mark our
- // closeDirectory as false. This way any exceptions hit
- // partway while opening the reader, which is expected
- // eg if writer is committing, won't close our
- // directory. We restore this value below:
- final boolean myCloseDirectory = closeDirectory;
+ /* TODO: Remove this in 3.0 - the directory is then
+ * no longer owned by the IndexReader and must not be
+ * closed.
+ * While trying to reopen, we temporarily mark our
+ * closeDirectory as false. This way any exceptions hit
+ * partway while opening the reader, which is expected
+ * eg if writer is committing, won't close our
+ * directory. We restore this value below:
+ */
+ final boolean myCloseDirectory = closeDirectory; // @deprectated
closeDirectory = false;
try {
diff --git a/src/java/org/apache/lucene/index/IndexModifier.java b/src/java/org/apache/lucene/index/IndexModifier.java
index d420edfddd6..75acaaf769a 100644
--- a/src/java/org/apache/lucene/index/IndexModifier.java
+++ b/src/java/org/apache/lucene/index/IndexModifier.java
@@ -97,7 +97,7 @@ public class IndexModifier {
protected Directory directory = null;
protected Analyzer analyzer = null;
- protected boolean open = false;
+ protected boolean open = false, closeDir = false;
// Lucene defaults:
protected PrintStream infoStream = null;
@@ -138,6 +138,7 @@ public class IndexModifier {
*/
public IndexModifier(String dirName, Analyzer analyzer, boolean create) throws CorruptIndexException, LockObtainFailedException, IOException {
Directory dir = FSDirectory.getDirectory(dirName);
+ this.closeDir = true;
init(dir, analyzer, create);
}
@@ -156,6 +157,7 @@ public class IndexModifier {
*/
public IndexModifier(File file, Analyzer analyzer, boolean create) throws CorruptIndexException, LockObtainFailedException, IOException {
Directory dir = FSDirectory.getDirectory(file);
+ this.closeDir = true;
init(dir, analyzer, create);
}
@@ -578,6 +580,10 @@ public class IndexModifier {
indexReader = null;
}
open = false;
+ if (closeDir) {
+ directory.close();
+ }
+ closeDir = false;
}
}
diff --git a/src/java/org/apache/lucene/index/IndexReader.java b/src/java/org/apache/lucene/index/IndexReader.java
index 86968b91dc7..86e37f2fe62 100644
--- a/src/java/org/apache/lucene/index/IndexReader.java
+++ b/src/java/org/apache/lucene/index/IndexReader.java
@@ -204,7 +204,8 @@ public abstract class IndexReader implements Cloneable {
* path.
* @throws CorruptIndexException if the index is corrupt
* @throws IOException if there is a low-level IO error
- * @deprecated Use {@link #open(Directory, boolean)} instead
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #open(Directory, boolean)} instead
* @param path the path to the index directory */
public static IndexReader open(String path) throws CorruptIndexException, IOException {
return open(FSDirectory.getDirectory(path), true, null, null, false);
@@ -220,7 +221,9 @@ public abstract class IndexReader implements Cloneable {
* @param path the path to the index directory
* @param readOnly true if this should be a readOnly
* reader
- * @deprecated Use {@link #open(Directory, boolean)} instead*/
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #open(Directory, boolean)} instead
+ */
public static IndexReader open(String path, boolean readOnly) throws CorruptIndexException, IOException {
return open(FSDirectory.getDirectory(path), true, null, null, readOnly);
}
@@ -230,7 +233,8 @@ public abstract class IndexReader implements Cloneable {
* @param path the path to the index directory
* @throws CorruptIndexException if the index is corrupt
* @throws IOException if there is a low-level IO error
- * @deprecated Use {@link #open(Directory, boolean)} instead
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #open(Directory, boolean)} instead
*/
public static IndexReader open(File path) throws CorruptIndexException, IOException {
return open(FSDirectory.getDirectory(path), true, null, null, false);
@@ -246,8 +250,9 @@ public abstract class IndexReader implements Cloneable {
* @param path the path to the index directory
* @param readOnly true if this should be a readOnly
* reader
- * @deprecated Use {@link #open(Directory, boolean)}
- * instead */
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #open(Directory, boolean)} instead
+ */
public static IndexReader open(File path, boolean readOnly) throws CorruptIndexException, IOException {
return open(FSDirectory.getDirectory(path), true, null, null, readOnly);
}
@@ -257,7 +262,8 @@ public abstract class IndexReader implements Cloneable {
* @param directory the index directory
* @throws CorruptIndexException if the index is corrupt
* @throws IOException if there is a low-level IO error
- * @deprecated Use {@link #open(Directory, boolean)} instead
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #open(Directory, boolean)} instead
*/
public static IndexReader open(final Directory directory) throws CorruptIndexException, IOException {
return open(directory, false, null, null, false);
@@ -281,7 +287,8 @@ public abstract class IndexReader implements Cloneable {
* {@link IndexCommit}.
* @param commit the commit point to open
* @throws CorruptIndexException if the index is corrupt
- * @deprecated Use {@link #open(IndexCommit, boolean)} instead
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #open(IndexCommit, boolean)} instead
* @throws IOException if there is a low-level IO error
*/
public static IndexReader open(final IndexCommit commit) throws CorruptIndexException, IOException {
@@ -308,8 +315,8 @@ public abstract class IndexReader implements Cloneable {
* @param deletionPolicy a custom deletion policy (only used
* if you use this reader to perform deletes or to set
* norms); see {@link IndexWriter} for details.
- * @deprecated Use {@link #open(Directory,
- * IndexDeletionPolicy, boolean)} instead
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #open(Directory, IndexDeletionPolicy, boolean)} instead
* @throws CorruptIndexException if the index is corrupt
* @throws IOException if there is a low-level IO error
*/
@@ -344,8 +351,8 @@ public abstract class IndexReader implements Cloneable {
* @param deletionPolicy a custom deletion policy (only used
* if you use this reader to perform deletes or to set
* norms); see {@link IndexWriter} for details.
- * @deprecated Use {@link #open(IndexCommit,
- * IndexDeletionPolicy, boolean)} instead
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #open(IndexCommit, IndexDeletionPolicy, boolean)} instead
* @throws CorruptIndexException if the index is corrupt
* @throws IOException if there is a low-level IO error
*/
@@ -500,6 +507,8 @@ public abstract class IndexReader implements Cloneable {
* {@link #isCurrent()} instead.
* @throws CorruptIndexException if the index is corrupt
* @throws IOException if there is a low-level IO error
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #lastModified(Directory)} instead
*/
public static long lastModified(String directory) throws CorruptIndexException, IOException {
return lastModified(new File(directory));
@@ -511,13 +520,16 @@ public abstract class IndexReader implements Cloneable {
* {@link #isCurrent()} instead.
* @throws CorruptIndexException if the index is corrupt
* @throws IOException if there is a low-level IO error
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #lastModified(Directory)} instead
*/
public static long lastModified(File fileDirectory) throws CorruptIndexException, IOException {
- return ((Long) new SegmentInfos.FindSegmentsFile(fileDirectory) {
- public Object doBody(String segmentFileName) {
- return new Long(FSDirectory.fileModified(fileDirectory, segmentFileName));
- }
- }.run()).longValue();
+ Directory dir = FSDirectory.getDirectory(fileDirectory); // use new static method here
+ try {
+ return lastModified(dir);
+ } finally {
+ dir.close();
+ }
}
/**
@@ -544,6 +556,8 @@ public abstract class IndexReader implements Cloneable {
* @return version number.
* @throws CorruptIndexException if the index is corrupt
* @throws IOException if there is a low-level IO error
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #getCurrentVersion(Directory)} instead
*/
public static long getCurrentVersion(String directory) throws CorruptIndexException, IOException {
return getCurrentVersion(new File(directory));
@@ -558,7 +572,8 @@ public abstract class IndexReader implements Cloneable {
* @return version number.
* @throws CorruptIndexException if the index is corrupt
* @throws IOException if there is a low-level IO error
- * @deprecated Use {@link #getCurrentVersion(Directory)} instead
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #getCurrentVersion(Directory)} instead
*/
public static long getCurrentVersion(File directory) throws CorruptIndexException, IOException {
Directory dir = FSDirectory.getDirectory(directory);
@@ -741,6 +756,8 @@ public abstract class IndexReader implements Cloneable {
* false
is returned.
* @param directory the directory to check for an index
* @return true
if an index exists; false
otherwise
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #indexExists(Directory)} instead
*/
public static boolean indexExists(String directory) {
return indexExists(new File(directory));
@@ -751,6 +768,8 @@ public abstract class IndexReader implements Cloneable {
* If the directory does not exist or if there is no index in it.
* @param directory the directory to check for an index
* @return true
if an index exists; false
otherwise
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #indexExists(Directory)} instead
*/
public static boolean indexExists(File directory) {
@@ -1159,7 +1178,8 @@ public abstract class IndexReader implements Cloneable {
* currently locked.
* @param directory the directory to check for a lock
* @throws IOException if there is a low-level IO error
- * @deprecated Please use {@link IndexWriter#isLocked(Directory)} instead
+ * @deprecated This method will be removed in the 3.0 release.
+ * Please use {@link IndexWriter#isLocked(Directory)} instead
*/
public static boolean isLocked(Directory directory) throws IOException {
return
@@ -1171,7 +1191,8 @@ public abstract class IndexReader implements Cloneable {
* currently locked.
* @param directory the directory to check for a lock
* @throws IOException if there is a low-level IO error
- * @deprecated Please use {@link IndexWriter#isLocked(Directory)} instead
+ * @deprecated This method will be removed in the 3.0 release.
+ * Use {@link #isLocked(Directory)} instead
*/
public static boolean isLocked(String directory) throws IOException {
Directory dir = FSDirectory.getDirectory(directory);
@@ -1186,7 +1207,8 @@ public abstract class IndexReader implements Cloneable {
* Caution: this should only be used by failure recovery code,
* when it is known that no other process nor thread is in fact
* currently accessing this index.
- * @deprecated Please use {@link IndexWriter#unlock(Directory)} instead
+ * @deprecated This method will be removed in the 3.0 release.
+ * Please use {@link IndexWriter#unlock(Directory)} instead
*/
public static void unlock(Directory directory) throws IOException {
directory.makeLock(IndexWriter.WRITE_LOCK_NAME).release();
@@ -1236,7 +1258,7 @@ public abstract class IndexReader implements Cloneable {
File file = new File(filename);
String dirname = file.getAbsoluteFile().getParent();
filename = file.getName();
- dir = FSDirectory.getDirectory(dirname);
+ dir = FSDirectory.open(new File(dirname));
cfr = new CompoundFileReader(dir, filename);
String [] files = cfr.list();
diff --git a/src/java/org/apache/lucene/index/SegmentInfos.java b/src/java/org/apache/lucene/index/SegmentInfos.java
index 384f16fa70c..d34ce1c885c 100644
--- a/src/java/org/apache/lucene/index/SegmentInfos.java
+++ b/src/java/org/apache/lucene/index/SegmentInfos.java
@@ -17,7 +17,6 @@ package org.apache.lucene.index;
* limitations under the License.
*/
-import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
@@ -523,13 +522,8 @@ final class SegmentInfos extends Vector {
*/
public abstract static class FindSegmentsFile {
- File fileDirectory;
Directory directory;
- public FindSegmentsFile(File directory) {
- this.fileDirectory = directory;
- }
-
public FindSegmentsFile(Directory directory) {
this.directory = directory;
}
@@ -582,10 +576,7 @@ final class SegmentInfos extends Vector {
long genA = -1;
- if (directory != null)
- files = directory.listAll();
- else
- files = FSDirectory.listAll(fileDirectory);
+ files = directory.listAll();
if (files != null)
genA = getCurrentSegmentGeneration(files);
@@ -732,10 +723,7 @@ final class SegmentInfos extends Vector {
gen-1);
final boolean prevExists;
- if (directory != null)
- prevExists = directory.fileExists(prevSegmentFileName);
- else
- prevExists = new File(fileDirectory, prevSegmentFileName).exists();
+ prevExists = directory.fileExists(prevSegmentFileName);
if (prevExists) {
message("fallback to prior segment file '" + prevSegmentFileName + "'");
diff --git a/src/java/org/apache/lucene/search/IndexSearcher.java b/src/java/org/apache/lucene/search/IndexSearcher.java
index 60e7306effb..56f0351ff84 100644
--- a/src/java/org/apache/lucene/search/IndexSearcher.java
+++ b/src/java/org/apache/lucene/search/IndexSearcher.java
@@ -46,7 +46,7 @@ public class IndexSearcher extends Searcher {
/** Creates a searcher searching the index in the named directory.
* @throws CorruptIndexException if the index is corrupt
* @throws IOException if there is a low-level IO error
- * @deprecated Use {@link #IndexSearcher(String, boolean)} instead
+ * @deprecated Use {@link #IndexSearcher(Directory, boolean)} instead
*/
public IndexSearcher(String path) throws CorruptIndexException, IOException {
this(IndexReader.open(path), true);
@@ -62,6 +62,7 @@ public class IndexSearcher extends Searcher {
* will be opened readOnly
* @throws CorruptIndexException if the index is corrupt
* @throws IOException if there is a low-level IO error
+ * @deprecated Use {@link #IndexSearcher(Directory, boolean)} instead
*/
public IndexSearcher(String path, boolean readOnly) throws CorruptIndexException, IOException {
this(IndexReader.open(path, readOnly), true);
diff --git a/src/java/overview.html b/src/java/overview.html
index 19408dc4953..248c7a9d9f8 100644
--- a/src/java/overview.html
+++ b/src/java/overview.html
@@ -40,7 +40,7 @@ to check if the results are what we expect):
// Store the index in memory:
Directory directory = new RAMDirectory();
// To store an index on disk, use this instead:
- //Directory directory = FSDirectory.getDirectory("/tmp/testindex");
+ //Directory directory = FSDirectory.open("/tmp/testindex");
IndexWriter iwriter = new IndexWriter(directory, analyzer, true,
new IndexWriter.MaxFieldLength(25000));
Document doc = new Document();
diff --git a/src/test/org/apache/lucene/TestDemo.java b/src/test/org/apache/lucene/TestDemo.java
index 1b432efe84f..6609e7347eb 100644
--- a/src/test/org/apache/lucene/TestDemo.java
+++ b/src/test/org/apache/lucene/TestDemo.java
@@ -49,7 +49,7 @@ public class TestDemo extends LuceneTestCase {
// Store the index in memory:
Directory directory = new RAMDirectory();
// To store an index on disk, use this instead:
- //Directory directory = FSDirectory.getDirectory("/tmp/testindex");
+ //Directory directory = FSDirectory.open(new File("/tmp/testindex"));
IndexWriter iwriter = new IndexWriter(directory, analyzer, true,
new IndexWriter.MaxFieldLength(25000));
Document doc = new Document();
diff --git a/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java b/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java
index ee0e40a4e74..0aa58fe6fab 100644
--- a/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java
+++ b/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java
@@ -54,7 +54,7 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase
try {
// Sometimes past test leaves the dir
_TestUtil.rmDir(dir);
- Directory fsDir = FSDirectory.getDirectory(dir);
+ Directory fsDir = FSDirectory.open(dir);
runTest(fsDir);
fsDir.close();
} finally {