Apache Solr open-source search software
LUCENE-773: deprecate "create" argument to FSDirectory.getDirectory(*); use IndexWriter's "create" argument instead
Michael McCandless02d6b05cd67e
diff --git a/CHANGES.txt b/CHANGES.txtindex 497f37a14ec..6ec624ac496 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -184,6 +184,11 @@ API Changes15. Added isOptimized() method to IndexReader.(Otis Gospodnetic)++16. LUCENE-773: Deprecate the FSDirectory.getDirectory(*) methods that+ take a boolean "create" argument. Instead you should use+ IndexWriter's "create" argument to create a new index.+ (Mike McCandless)Bug fixesdiff --git a/src/demo/org/apache/lucene/demo/DeleteFiles.java b/src/demo/org/apache/lucene/demo/DeleteFiles.javaindex e24e0b2225f..e8a00b97d11 100644--- a/src/demo/org/apache/lucene/demo/DeleteFiles.java+++ b/src/demo/org/apache/lucene/demo/DeleteFiles.java@@ -37,7 +37,7 @@ public class DeleteFiles {System.exit(1);}try {- Directory directory = FSDirectory.getDirectory("index", false);+ Directory directory = FSDirectory.getDirectory("index");IndexReader reader = IndexReader.open(directory);Term term = new Term("path", args[0]);diff --git a/src/java/org/apache/lucene/index/IndexModifier.java b/src/java/org/apache/lucene/index/IndexModifier.javaindex b97fe8c1e16..9acf6c6063b 100644--- a/src/java/org/apache/lucene/index/IndexModifier.java+++ b/src/java/org/apache/lucene/index/IndexModifier.java@@ -121,7 +121,7 @@ public class IndexModifier {* <code>false</code> to append to the existing index*/public IndexModifier(String dirName, Analyzer analyzer, boolean create) throws IOException {- Directory dir = FSDirectory.getDirectory(dirName, create);+ Directory dir = FSDirectory.getDirectory(dirName);init(dir, analyzer, create);}@@ -134,7 +134,7 @@ public class IndexModifier {* <code>false</code> to append to the existing index*/public IndexModifier(File file, Analyzer analyzer, boolean create) throws IOException {- Directory dir = FSDirectory.getDirectory(file, create);+ Directory dir = FSDirectory.getDirectory(file);init(dir, analyzer, create);}diff --git a/src/java/org/apache/lucene/index/IndexReader.java b/src/java/org/apache/lucene/index/IndexReader.javaindex 4e2092aba51..57805e1d1f9 100644--- a/src/java/org/apache/lucene/index/IndexReader.java+++ b/src/java/org/apache/lucene/index/IndexReader.java@@ -128,13 +128,13 @@ public abstract class IndexReader {/** Returns an IndexReader reading the index in an FSDirectory in the namedpath. */public static IndexReader open(String path) throws IOException {- return open(FSDirectory.getDirectory(path, false), true);+ return open(FSDirectory.getDirectory(path), true);}/** Returns an IndexReader reading the index in an FSDirectory in the namedpath. */public static IndexReader open(File path) throws IOException {- return open(FSDirectory.getDirectory(path, false), true);+ return open(FSDirectory.getDirectory(path), true);}/** Returns an IndexReader reading the index in the given Directory. */@@ -240,7 +240,7 @@ public abstract class IndexReader {* @throws IOException if segments file cannot be read*/public static long getCurrentVersion(File directory) throws IOException {- Directory dir = FSDirectory.getDirectory(directory, false);+ Directory dir = FSDirectory.getDirectory(directory);long version = getCurrentVersion(dir);dir.close();return version;@@ -767,7 +767,7 @@ public abstract class IndexReader {* @throws IOException if there is a problem with accessing the index*/public static boolean isLocked(String directory) throws IOException {- Directory dir = FSDirectory.getDirectory(directory, false);+ Directory dir = FSDirectory.getDirectory(directory);boolean result = isLocked(dir);dir.close();return result;@@ -815,7 +815,7 @@ public abstract class IndexReader {File file = new File(filename);String dirname = file.getAbsoluteFile().getParent();filename = file.getName();- dir = FSDirectory.getDirectory(dirname, false);+ dir = FSDirectory.getDirectory(dirname);cfr = new CompoundFileReader(dir, filename);String [] files = cfr.list();diff --git a/src/java/org/apache/lucene/index/IndexWriter.java b/src/java/org/apache/lucene/index/IndexWriter.javaindex a9793301a28..a5140416e9c 100644--- a/src/java/org/apache/lucene/index/IndexWriter.java+++ b/src/java/org/apache/lucene/index/IndexWriter.java@@ -36,23 +36,27 @@ import java.util.HashSet;/**An IndexWriter creates and maintains an index.- The third argument to the+ <p>The third argument (<code>create</code>) to the<a href="#IndexWriter(org.apache.lucene.store.Directory, org.apache.lucene.analysis.Analyzer, boolean)"><b>constructor</b></a>determines whether a new index is created, or whether an existing index is- opened for the addition of new documents.+ opened for the addition of new documents. Note that you+ can open an index with create=true even while readers are+ using the index. The old readers will continue to search+ the "point in time" snapshot they had opened, and won't+ see the newly created index until they re-open.</p>- In either case, documents are added with the <a+ <p>In either case, documents are added with the <ahref="#addDocument(org.apache.lucene.document.Document)"><b>addDocument</b></a> method.- When finished adding documents, <a href="#close()"><b>close</b></a> should be called.+ When finished adding documents, <a href="#close()"><b>close</b></a> should be called.</p><p>If an index will not have more documents added for a while and optimal searchperformance is desired, then the <a href="#optimize()"><b>optimize</b></a>- method should be called before the index is closed.+ method should be called before the index is closed.</p><p>Opening an IndexWriter creates a lock file for the directory in use. Trying to openanother IndexWriter on the same directory will lead to an IOException. The IOExceptionis also thrown if an IndexReader on the same directory is used to delete documents- from the index.+ from the index.</p>@see IndexModifier IndexModifier supports the important methods of IndexWriter plus deletion*/@@ -313,12 +317,12 @@ public class IndexWriter {private void init(String path, Analyzer a, final boolean create)throws IOException {- init(FSDirectory.getDirectory(path, create, null, false), a, create, true);+ init(FSDirectory.getDirectory(path), a, create, true);}private void init(File path, Analyzer a, final boolean create)throws IOException {- init(FSDirectory.getDirectory(path, create, null, false), a, create, true);+ init(FSDirectory.getDirectory(path), a, create, true);}private void init(Directory d, Analyzer a, final boolean create, boolean closeDir)@@ -327,6 +331,11 @@ public class IndexWriter {directory = d;analyzer = a;+ if (create) {+ // Clear the write lock in case it's leftover:+ directory.getLockFactory().clearLock(IndexWriter.WRITE_LOCK_NAME);+ }+Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);if (!writeLock.obtain(writeLockTimeout)) // obtain write lockthrow new IOException("Index locked for write: " + writeLock);diff --git a/src/java/org/apache/lucene/store/FSDirectory.java b/src/java/org/apache/lucene/store/FSDirectory.javaindex dcb9bff8742..d93f94a3b2c 100644--- a/src/java/org/apache/lucene/store/FSDirectory.java+++ b/src/java/org/apache/lucene/store/FSDirectory.java@@ -28,7 +28,7 @@ import java.util.Hashtable;import org.apache.lucene.index.IndexFileNameFilter;-// Used only for WRITE_LOCK_NAME:+// Used only for WRITE_LOCK_NAME in deprecated create=true case:import org.apache.lucene.index.IndexWriter;/**@@ -39,6 +39,11 @@ import org.apache.lucene.index.IndexWriter;* <code>org.apache.lucene.store.FSDirectoryLockFactoryClass</code> Java system* property, or by calling {@link #setLockFactory} after creating* the Directory.++ * <p>Directories are cached, so that, for a given canonical+ * path, the same FSDirectory instance will always be+ * returned by <code>getDirectory</code>. This permits+ * synchronization on directories.</p>** @see Directory* @author Doug Cutting@@ -89,8 +94,7 @@ public class FSDirectory extends Directory {* etc.) passing in your preferred lock directory. Then,* pass this <code>LockFactory</code> instance to one of* the <code>getDirectory</code> methods that take a- * <code>lockFactory</code> (for example, {@link- * #getDirectory(String, boolean, LockFactory)}).+ * <code>lockFactory</code> (for example, {@link #getDirectory(String, LockFactory)}).*/public static final String LOCK_DIR = System.getProperty("org.apache.lucene.lockDir",System.getProperty("java.io.tmpdir"));@@ -128,73 +132,48 @@ public class FSDirectory extends Directory {private byte[] buffer = null;/** Returns the directory instance for the named location.- *- * <p>Directories are cached, so that, for a given canonical path, the same- * FSDirectory instance will always be returned. This permits- * synchronization on directories.- ** @param path the path to the directory.- * @param create if true, create, or erase any existing contents.* @return the FSDirectory for the named file. */- public static FSDirectory getDirectory(String path, boolean create)+ public static FSDirectory getDirectory(String path)throws IOException {- return getDirectory(new File(path), create, null, true);+ return getDirectory(new File(path), null);}- /** Returns the directory instance for the named location, using the- * provided LockFactory implementation.- *- * <p>Directories are cached, so that, for a given canonical path, the same- * FSDirectory instance will always be returned. This permits- * synchronization on directories.- *+ /** Returns the directory instance for the named location.* @param path the path to the directory.- * @param create if true, create, or erase any existing contents.* @param lockFactory instance of {@link LockFactory} providing the* locking implementation.* @return the FSDirectory for the named file. */- public static FSDirectory getDirectory(String path, boolean create,- LockFactory lockFactory, boolean doRemoveOldFiles)- throws IOException {- return getDirectory(new File(path), create, lockFactory, doRemoveOldFiles);- }-- public static FSDirectory getDirectory(String path, boolean create,- LockFactory lockFactory)+ public static FSDirectory getDirectory(String path, LockFactory lockFactory)throws IOException {- return getDirectory(new File(path), create, lockFactory, true);+ return getDirectory(new File(path), lockFactory);}/** Returns the directory instance for the named location.- *- * <p>Directories are cached, so that, for a given canonical path, the same- * FSDirectory instance will always be returned. This permits- * synchronization on directories.- ** @param file the path to the directory.- * @param create if true, create, or erase any existing contents.* @return the FSDirectory for the named file. */- public static FSDirectory getDirectory(File file, boolean create, boolean doRemoveOldFiles)+ public static FSDirectory getDirectory(File file)throws IOException {- return getDirectory(file, create, null, doRemoveOldFiles);+ return getDirectory(file, null);}- /** Returns the directory instance for the named location, using the- * provided LockFactory implementation.- *- * <p>Directories are cached, so that, for a given canonical path, the same- * FSDirectory instance will always be returned. This permits- * synchronization on directories.- *+ /** Returns the directory instance for the named location.* @param file the path to the directory.- * @param create if true, create, or erase any existing contents.- * @param lockFactory instance of {@link LockFactory} providing the+ * @param lockFactory instance of {@link LockFactory} providing the* locking implementation.* @return the FSDirectory for the named file. */- public static FSDirectory getDirectory(File file, boolean create,- LockFactory lockFactory, boolean doRemoveOldFiles)- throws IOException {+ public static FSDirectory getDirectory(File file, LockFactory lockFactory)+ throws IOException+ {file = new File(file.getCanonicalPath());++ if (file.exists() && !file.isDirectory())+ throw new IOException(file + " not a directory");++ if (!file.exists())+ if (!file.mkdirs())+ throw new IOException("Cannot create directory: " + file);+FSDirectory dir;synchronized (DIRECTORIES) {dir = (FSDirectory)DIRECTORIES.get(file);@@ -204,19 +183,14 @@ public class FSDirectory extends Directory {} catch (Exception e) {throw new RuntimeException("cannot load FSDirectory class: " + e.toString(), e);}- dir.init(file, create, lockFactory, doRemoveOldFiles);+ dir.init(file, lockFactory);DIRECTORIES.put(file, dir);} else {-// Catch the case where a Directory is pulled from the cache, but has a// different LockFactory instance.if (lockFactory != null && lockFactory != dir.getLockFactory()) {throw new IOException("Directory was previously created with a different LockFactory instance; please pass null as the lockFactory instance and use setLockFactory to change it");}-- if (create) {- dir.create(doRemoveOldFiles);- }}}synchronized (dir) {@@ -225,16 +199,54 @@ public class FSDirectory extends Directory {return dir;}- public static FSDirectory getDirectory(File file, boolean create,- LockFactory lockFactory)++ /** Returns the directory instance for the named location.+ *+ * @deprecated Use IndexWriter's create flag, instead, to+ * create a new index.+ *+ * @param path the path to the directory.+ * @param create if true, create, or erase any existing contents.+ * @return the FSDirectory for the named file. */+ public static FSDirectory getDirectory(String path, boolean create)+ throws IOException {+ return getDirectory(new File(path), create);+ }++ /** Returns the directory instance for the named location.+ *+ * @deprecated Use IndexWriter's create flag, instead, to+ * create a new index.+ *+ * @param file the path to the directory.+ * @param create if true, create, or erase any existing contents.+ * @return the FSDirectory for the named file. */+ public static FSDirectory getDirectory(File file, boolean create)throws IOException{- return getDirectory(file, create, lockFactory, true);+ FSDirectory dir = getDirectory(file, null);++ // This is now deprecated (creation should only be done+ // by IndexWriter):+ if (create) {+ dir.create();+ }++ return dir;}- public static FSDirectory getDirectory(File file, boolean create)- throws IOException {- return getDirectory(file, create, true);+ private void create() throws IOException {+ if (directory.exists()) {+ String[] files = directory.list(IndexFileNameFilter.getFilter()); // clear old files+ if (files == null)+ throw new IOException("Cannot read directory " + directory.getAbsolutePath());+ for (int i = 0; i < files.length; i++) {+ File file = new File(directory, files[i]);+ if (!file.delete())+ throw new IOException("Cannot delete " + file);+ }+ }+ lockFactory.clearLock(IndexWriter.WRITE_LOCK_NAME);}private File directory = null;@@ -242,24 +254,15 @@ public class FSDirectory extends Directory {protected FSDirectory() {}; // permit subclassing- private void init(File path, boolean create, boolean doRemoveOldFiles) throws IOException {- directory = path;-- if (create) {- create(doRemoveOldFiles);- }-- if (!directory.isDirectory())- throw new IOException(path + " not a directory");- }-- private void init(File path, boolean create, LockFactory lockFactory, boolean doRemoveOldFiles) throws IOException {+ private void init(File path, LockFactory lockFactory) throws IOException {// Set up lockFactory with cascaded defaults: if an instance was passed in,// use that; else if locks are disabled, use NoLockFactory; else if the// system property org.apache.lucene.store.FSDirectoryLockFactoryClass is set,// instantiate that; else, use SimpleFSLockFactory:+ directory = path;+boolean doClearLockID = false;if (lockFactory == null) {@@ -297,43 +300,13 @@ public class FSDirectory extends Directory {}}- // Must initialize directory here because setLockFactory uses it- // (when the LockFactory calls getLockID). But we want to create- // the lockFactory before calling init() because init() needs to- // use the lockFactory to clear old locks. So this breaks- // chicken/egg:- directory = path;-setLockFactory(lockFactory);+if (doClearLockID) {// Clear the prefix because write.lock will be// stored in our directory:lockFactory.setLockPrefix(null);}-- init(path, create, doRemoveOldFiles);- }-- private synchronized void create(boolean doRemoveOldFiles) throws IOException {- if (!directory.exists())- if (!directory.mkdirs())- throw new IOException("Cannot create directory: " + directory);-- if (!directory.isDirectory())- throw new IOException(directory + " not a directory");-- if (doRemoveOldFiles) {- String[] files = directory.list(IndexFileNameFilter.getFilter()); // clear old files- if (files == null)- throw new IOException("Cannot read directory " + directory.getAbsolutePath());- for (int i = 0; i < files.length; i++) {- File file = new File(directory, files[i]);- if (!file.delete())- throw new IOException("Cannot delete " + file);- }- }-- lockFactory.clearLock(IndexWriter.WRITE_LOCK_NAME);}/** Returns an array of strings, one for each Lucene index file in the directory. */@@ -445,6 +418,7 @@ public class FSDirectory extends Directory {/** Creates a new, empty file in the directory with the given name.Returns a stream writing this file. */public IndexOutput createOutput(String name) throws IOException {+File file = new File(directory, name);if (file.exists() && !file.delete()) // delete existing, if anythrow new IOException("Cannot overwrite: " + file);diff --git a/src/java/org/apache/lucene/store/RAMDirectory.java b/src/java/org/apache/lucene/store/RAMDirectory.javaindex 502ca300566..ef957cb1358 100644--- a/src/java/org/apache/lucene/store/RAMDirectory.java+++ b/src/java/org/apache/lucene/store/RAMDirectory.java@@ -107,7 +107,7 @@ public class RAMDirectory extends Directory implements Serializable {* @see #RAMDirectory(Directory)*/public RAMDirectory(File dir) throws IOException {- this(FSDirectory.getDirectory(dir, false), true);+ this(FSDirectory.getDirectory(dir), true);}/**@@ -118,7 +118,7 @@ public class RAMDirectory extends Directory implements Serializable {* @see #RAMDirectory(Directory)*/public RAMDirectory(String dir) throws IOException {- this(FSDirectory.getDirectory(dir, false), true);+ this(FSDirectory.getDirectory(dir), true);}/** Returns an array of strings, one for each file in the directory. */diff --git a/src/test/org/apache/lucene/StoreTest.java b/src/test/org/apache/lucene/StoreTest.javaindex abac12f5693..e4d3393ef42 100644--- a/src/test/org/apache/lucene/StoreTest.java+++ b/src/test/org/apache/lucene/StoreTest.java@@ -22,6 +22,7 @@ import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.IndexOutput;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.store.RAMDirectory;+import org.apache.lucene.util._TestUtil;import java.util.Date;import java.util.Random;@@ -46,8 +47,11 @@ class StoreTest {Directory store;if (ram)store = new RAMDirectory();- else- store = FSDirectory.getDirectory("test.store", true);+ else {+ String dirName = "test.store";+ _TestUtil.rmDir(dirName);+ store = FSDirectory.getDirectory(dirName);+ }final int LENGTH_MASK = 0xFFF;@@ -84,7 +88,7 @@ class StoreTest {start = new Date();if (!ram)- store = FSDirectory.getDirectory("test.store", false);+ store = FSDirectory.getDirectory("test.store");for (i = 0; i < count; i++) {String name = i + ".dat";diff --git a/src/test/org/apache/lucene/ThreadSafetyTest.java b/src/test/org/apache/lucene/ThreadSafetyTest.javaindex c6731a60837..c9fade56da2 100644--- a/src/test/org/apache/lucene/ThreadSafetyTest.java+++ b/src/test/org/apache/lucene/ThreadSafetyTest.java@@ -139,7 +139,7 @@ class ThreadSafetyTest {File indexDir = new File("index");if (! indexDir.exists()) indexDir.mkdirs();- IndexReader.unlock(FSDirectory.getDirectory(indexDir, false));+ IndexReader.unlock(FSDirectory.getDirectory(indexDir));if (!readOnly) {IndexWriter writer = new IndexWriter(indexDir, ANALYZER, !add);diff --git a/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java b/src/test/org/apache/lucene/index/TestBackwardsCompatibility.javaindex 94b830723bf..c2acac13898 100644--- a/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java+++ b/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java@@ -119,7 +119,7 @@ public class TestBackwardsCompatibility extends TestCase//QueryParser parser = new QueryParser("contents", new WhitespaceAnalyzer());//Query query = parser.parse("handle:1");- Directory dir = FSDirectory.getDirectory(dirName, false);+ Directory dir = FSDirectory.getDirectory(dirName);IndexSearcher searcher = new IndexSearcher(dir);Hits hits = searcher.search(new TermQuery(new Term("content", "aaa")));@@ -137,7 +137,7 @@ public class TestBackwardsCompatibility extends TestCase* setNorm, and search */public void changeIndexWithAdds(String dirName) throws IOException {- Directory dir = FSDirectory.getDirectory(dirName, false);+ Directory dir = FSDirectory.getDirectory(dirName);// open writerIndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false);@@ -194,7 +194,7 @@ public class TestBackwardsCompatibility extends TestCase* setNorm, and search */public void changeIndexNoAdds(String dirName) throws IOException {- Directory dir = FSDirectory.getDirectory(dirName, false);+ Directory dir = FSDirectory.getDirectory(dirName);// make sure searching sees right # hitsIndexSearcher searcher = new IndexSearcher(dir);@@ -238,7 +238,7 @@ public class TestBackwardsCompatibility extends TestCasepublic void createIndex(String dirName, boolean doCFS) throws IOException {- Directory dir = FSDirectory.getDirectory(dirName, true);+ Directory dir = FSDirectory.getDirectory(dirName);IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);writer.setUseCompoundFile(doCFS);@@ -265,7 +265,7 @@ public class TestBackwardsCompatibility extends TestCasepublic void testExactFileNames() throws IOException {String outputDir = "lucene.backwardscompat0.index";- Directory dir = FSDirectory.getDirectory(outputDir, true);+ Directory dir = FSDirectory.getDirectory(outputDir);IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);for(int i=0;i<35;i++) {addDoc(writer, i);diff --git a/src/test/org/apache/lucene/index/TestCompoundFile.java b/src/test/org/apache/lucene/index/TestCompoundFile.javaindex 22cc59d79be..2203befde7b 100644--- a/src/test/org/apache/lucene/index/TestCompoundFile.java+++ b/src/test/org/apache/lucene/index/TestCompoundFile.java@@ -28,6 +28,7 @@ import org.apache.lucene.store.Directory;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.store._TestHelper;+import org.apache.lucene.util._TestUtil;/**@@ -58,8 +59,9 @@ public class TestCompoundFile extends TestCasepublic void setUp() throws IOException {- //dir = new RAMDirectory();- dir = FSDirectory.getDirectory(new File(System.getProperty("tempDir"), "testIndex"), true);+ File file = new File(System.getProperty("tempDir"), "testIndex");+ _TestUtil.rmDir(file);+ dir = FSDirectory.getDirectory(file);}diff --git a/src/test/org/apache/lucene/index/TestFieldsReader.java b/src/test/org/apache/lucene/index/TestFieldsReader.javaindex bc37c185901..efdb2211002 100644--- a/src/test/org/apache/lucene/index/TestFieldsReader.java+++ b/src/test/org/apache/lucene/index/TestFieldsReader.java@@ -36,6 +36,7 @@ import org.apache.lucene.document.SetBasedFieldSelector;import org.apache.lucene.search.Similarity;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.store.RAMDirectory;+import org.apache.lucene.util._TestUtil;public class TestFieldsReader extends TestCase {private RAMDirectory dir = new RAMDirectory();@@ -167,7 +168,8 @@ public class TestFieldsReader extends TestCase {String userName = System.getProperty("user.name");String path = tmpIODir + File.separator + "lazyDir" + userName;File file = new File(path);- FSDirectory tmpDir = FSDirectory.getDirectory(file, true);+ _TestUtil.rmDir(file);+ FSDirectory tmpDir = FSDirectory.getDirectory(file);assertTrue(tmpDir != null);DocumentWriter writer = new DocumentWriter(tmpDir, new WhitespaceAnalyzer(),Similarity.getDefault(), 50);diff --git a/src/test/org/apache/lucene/index/TestIndexModifier.java b/src/test/org/apache/lucene/index/TestIndexModifier.javaindex 95b45a7e061..a16874a02de 100644--- a/src/test/org/apache/lucene/index/TestIndexModifier.java+++ b/src/test/org/apache/lucene/index/TestIndexModifier.java@@ -146,7 +146,7 @@ public class TestIndexModifier extends TestCase {if (tempDir == null)throw new IOException("java.io.tmpdir undefined, cannot run test");File indexDir = new File(tempDir, "lucenetestindex");- Directory rd = FSDirectory.getDirectory(indexDir, create);+ Directory rd = FSDirectory.getDirectory(indexDir);IndexThread.id = 0;IndexThread.idStack.clear();IndexModifier index = new IndexModifier(rd, new StandardAnalyzer(), create);diff --git a/src/test/org/apache/lucene/index/TestIndexReader.java b/src/test/org/apache/lucene/index/TestIndexReader.javaindex e2cfc50ff2f..5c4ef655929 100644--- a/src/test/org/apache/lucene/index/TestIndexReader.java+++ b/src/test/org/apache/lucene/index/TestIndexReader.java@@ -33,6 +33,7 @@ import org.apache.lucene.document.Field;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Hits;import org.apache.lucene.search.TermQuery;+import org.apache.lucene.util._TestUtil;import java.util.Collection;import java.util.Arrays;@@ -253,7 +254,7 @@ public class TestIndexReader extends TestCasethrow new IOException("tempDir undefined, cannot run test");File indexDir = new File(tempDir, "lucenetestnormwriter");- Directory dir = FSDirectory.getDirectory(indexDir, true);+ Directory dir = FSDirectory.getDirectory(indexDir);IndexWriter writer = null;IndexReader reader = null;Term searchTerm = new Term("content", "aaa");@@ -320,7 +321,7 @@ public class TestIndexReader extends TestCaseprivate void deleteReaderWriterConflict(boolean optimize) throws IOException{//Directory dir = new RAMDirectory();- Directory dir = getDirectory(true);+ Directory dir = getDirectory();Term searchTerm = new Term("content", "aaa");Term searchTerm2 = new Term("content", "bbb");@@ -400,21 +401,23 @@ public class TestIndexReader extends TestCasereader.close();}- private Directory getDirectory(boolean create) throws IOException {- return FSDirectory.getDirectory(new File(System.getProperty("tempDir"), "testIndex"), create);+ private Directory getDirectory() throws IOException {+ return FSDirectory.getDirectory(new File(System.getProperty("tempDir"), "testIndex"));}public void testFilesOpenClose() throws IOException{// Create initial data set- Directory dir = getDirectory(true);+ File dirFile = new File(System.getProperty("tempDir"), "testIndex");+ Directory dir = getDirectory();IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);addDoc(writer, "test");writer.close();dir.close();// Try to erase the data - this ensures that the writer closed all files- dir = getDirectory(true);+ _TestUtil.rmDir(dirFile);+ dir = getDirectory();// Now create the data set again, just as beforewriter = new IndexWriter(dir, new WhitespaceAnalyzer(), true);@@ -423,13 +426,14 @@ public class TestIndexReader extends TestCasedir.close();// Now open existing directory and test that reader closes all files- dir = getDirectory(false);+ dir = getDirectory();IndexReader reader1 = IndexReader.open(dir);reader1.close();dir.close();- // The following will fail if reader did not close all files- dir = getDirectory(true);+ // The following will fail if reader did not close+ // all files+ _TestUtil.rmDir(dirFile);}public void testLastModified() throws IOException {@@ -833,7 +837,7 @@ public class TestIndexReader extends TestCaseprivate void deleteReaderReaderConflict(boolean optimize) throws IOException{- Directory dir = getDirectory(true);+ Directory dir = getDirectory();Term searchTerm1 = new Term("content", "aaa");Term searchTerm2 = new Term("content", "bbb");diff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.javaindex 344db3410c7..a69d56c503f 100644--- a/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -447,7 +447,7 @@ public class TestIndexWriter extends TestCaseFile indexDir = new File(tempDir, "lucenetestindexwriter");try {- Directory dir = FSDirectory.getDirectory(indexDir, true);+ Directory dir = FSDirectory.getDirectory(indexDir);// add one document & close writerIndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);diff --git a/src/test/org/apache/lucene/index/TestNorms.java b/src/test/org/apache/lucene/index/TestNorms.javaindex 916dcc8c153..64fd4923251 100755--- a/src/test/org/apache/lucene/index/TestNorms.java+++ b/src/test/org/apache/lucene/index/TestNorms.java@@ -65,7 +65,7 @@ public class TestNorms extends TestCase {// test with a single index: index1File indexDir1 = new File(tempDir, "lucenetestindex1");- Directory dir1 = FSDirectory.getDirectory(indexDir1, true);+ Directory dir1 = FSDirectory.getDirectory(indexDir1);norms = new ArrayList();modifiedNorms = new ArrayList();@@ -83,14 +83,14 @@ public class TestNorms extends TestCase {numDocNorms = 0;File indexDir2 = new File(tempDir, "lucenetestindex2");- Directory dir2 = FSDirectory.getDirectory(indexDir2, true);+ Directory dir2 = FSDirectory.getDirectory(indexDir2);createIndex(dir2);doTestNorms(dir2);// add index1 and index2 to a third index: index3File indexDir3 = new File(tempDir, "lucenetestindex3");- Directory dir3 = FSDirectory.getDirectory(indexDir3, true);+ Directory dir3 = FSDirectory.getDirectory(indexDir3);createIndex(dir3);IndexWriter iw = new IndexWriter(dir3,anlzr,false);diff --git a/src/test/org/apache/lucene/index/TestStressIndexing.java b/src/test/org/apache/lucene/index/TestStressIndexing.javaindex 959abae8126..7150bb4636b 100644--- a/src/test/org/apache/lucene/index/TestStressIndexing.java+++ b/src/test/org/apache/lucene/index/TestStressIndexing.java@@ -157,7 +157,7 @@ public class TestStressIndexing extends TestCase {// Second in an FSDirectory:String tempDir = System.getProperty("java.io.tmpdir");File dirPath = new File(tempDir, "lucene.test.stress");- directory = FSDirectory.getDirectory(dirPath, true);+ directory = FSDirectory.getDirectory(dirPath);runStressTest(directory);directory.close();rmDir(dirPath);diff --git a/src/test/org/apache/lucene/index/store/TestRAMDirectory.java b/src/test/org/apache/lucene/index/store/TestRAMDirectory.javaindex 3e692a12128..147aad9819b 100644--- a/src/test/org/apache/lucene/index/store/TestRAMDirectory.java+++ b/src/test/org/apache/lucene/index/store/TestRAMDirectory.java@@ -71,7 +71,7 @@ public class TestRAMDirectory extends TestCase {public void testRAMDirectory () throws IOException {- Directory dir = FSDirectory.getDirectory(indexDir, false);+ Directory dir = FSDirectory.getDirectory(indexDir);MockRAMDirectory ramDir = new MockRAMDirectory(dir);// close the underlaying directorydiff --git a/src/test/org/apache/lucene/store/TestLockFactory.java b/src/test/org/apache/lucene/store/TestLockFactory.javaindex b775e30993b..c96f1947164 100755--- a/src/test/org/apache/lucene/store/TestLockFactory.java+++ b/src/test/org/apache/lucene/store/TestLockFactory.java@@ -253,11 +253,11 @@ public class TestLockFactory extends TestCase {String indexDirName = "index.TestLockFactory5";LockFactory lf = new SingleInstanceLockFactory();- FSDirectory fs1 = FSDirectory.getDirectory(indexDirName, true, lf);+ FSDirectory fs1 = FSDirectory.getDirectory(indexDirName, lf);// Different lock factory instance should hit IOException:try {- FSDirectory fs2 = FSDirectory.getDirectory(indexDirName, true, new SingleInstanceLockFactory());+ FSDirectory fs2 = FSDirectory.getDirectory(indexDirName, new SingleInstanceLockFactory());fail("Should have hit an IOException because LockFactory instances differ");} catch (IOException e) {}@@ -266,7 +266,7 @@ public class TestLockFactory extends TestCase {// Same lock factory instance should not:try {- fs2 = FSDirectory.getDirectory(indexDirName, true, lf);+ fs2 = FSDirectory.getDirectory(indexDirName, lf);} catch (IOException e) {e.printStackTrace(System.out);fail("Should not have hit an IOException because LockFactory instances are the same");@@ -296,7 +296,7 @@ public class TestLockFactory extends TestCase {}public void _testStressLocks(LockFactory lockFactory, String indexDirName) throws IOException {- FSDirectory fs1 = FSDirectory.getDirectory(indexDirName, true, lockFactory);+ FSDirectory fs1 = FSDirectory.getDirectory(indexDirName, lockFactory);// First create a 1 doc index:IndexWriter w = new IndexWriter(fs1, new WhitespaceAnalyzer(), true);@@ -350,8 +350,8 @@ public class TestLockFactory extends TestCase {public void testNativeFSLockFactoryPrefix() throws IOException {// Make sure we get identical instances:- Directory dir1 = FSDirectory.getDirectory("TestLockFactory.8", true, new NativeFSLockFactory("TestLockFactory.8"));- Directory dir2 = FSDirectory.getDirectory("TestLockFactory.9", true, new NativeFSLockFactory("TestLockFactory.9"));+ Directory dir1 = FSDirectory.getDirectory("TestLockFactory.8", new NativeFSLockFactory("TestLockFactory.8"));+ Directory dir2 = FSDirectory.getDirectory("TestLockFactory.9", new NativeFSLockFactory("TestLockFactory.9"));String prefix1 = dir1.getLockFactory().getLockPrefix();String prefix2 = dir2.getLockFactory().getLockPrefix();@@ -366,8 +366,8 @@ public class TestLockFactory extends TestCase {// write.lock is stored in index):public void testDefaultFSLockFactoryPrefix() throws IOException {- // Make sure we get identical instances:- Directory dir = FSDirectory.getDirectory("TestLockFactory.10", true);+ // Make sure we get null prefix:+ Directory dir = FSDirectory.getDirectory("TestLockFactory.10");String prefix = dir.getLockFactory().getLockPrefix();diff --git a/src/test/org/apache/lucene/store/TestWindowsMMap.java b/src/test/org/apache/lucene/store/TestWindowsMMap.javaindex 1aad333cf3f..cb6ac9c9a15 100644--- a/src/test/org/apache/lucene/store/TestWindowsMMap.java+++ b/src/test/org/apache/lucene/store/TestWindowsMMap.java@@ -47,7 +47,7 @@ public class TestWindowsMMap extends TestCase {public void testMmapIndex() throws Exception {FSDirectory storeDirectory;- storeDirectory = FSDirectory.getDirectory(storePathname, true);+ storeDirectory = FSDirectory.getDirectory(storePathname);// plan to add a set of useful stopwords, consider changing some of the// interior filters.diff --git a/src/test/org/apache/lucene/util/_TestUtil.java b/src/test/org/apache/lucene/util/_TestUtil.javanew file mode 100644index 00000000000..ad81f87b148--- /dev/null+++ b/src/test/org/apache/lucene/util/_TestUtil.java@@ -0,0 +1,23 @@++package org.apache.lucene.util;+import java.io.File;+import java.io.IOException;++public class _TestUtil {++ public static void rmDir(File dir) throws IOException {+ if (dir.exists()) {+ File[] files = dir.listFiles();+ for (int i = 0; i < files.length; i++) {+ if (!files[i].delete()) {+ throw new IOException("could not delete " + files[i]);+ }+ }+ dir.delete();+ }+ }++ public static void rmDir(String dir) throws IOException {+ rmDir(new File(dir));+ }+}