LUCENE-773: deprecate "create" argument to

FSDirectory.getDirectory(*); use IndexWriter's "create" argument
instead

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@497612 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2007-01-18 22:47:03 +00:00
parent 30083146b4
commit 02d6b05cd6
21 changed files with 195 additions and 172 deletions

View File

@ -184,6 +184,11 @@ API Changes
15. 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 fixes

View File

@ -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]);

View File

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

View File

@ -128,13 +128,13 @@ public abstract class IndexReader {
/** Returns an IndexReader reading the index in an FSDirectory in the named
path. */
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 named
path. */
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();

View File

@ -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 <a
href="#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 search
performance 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 open
another IndexWriter on the same directory will lead to an IOException. The IOException
is 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 lock
throw new IOException("Index locked for write: " + writeLock);

View File

@ -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);
}
/** 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.
*
* @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)
throws IOException {
return getDirectory(new File(path), create, lockFactory, true);
return getDirectory(new File(path), 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 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)
throws IOException {
return getDirectory(file, create, null, doRemoveOldFiles);
}
/** 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.
*
* @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 path the path to the directory.
* @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)
public static FSDirectory getDirectory(String path, LockFactory lockFactory)
throws IOException {
return getDirectory(new File(path), lockFactory);
}
/** Returns the directory instance for the named location.
* @param file the path to the directory.
* @return the FSDirectory for the named file. */
public static FSDirectory getDirectory(File file)
throws IOException {
return getDirectory(file, null);
}
/** Returns the directory instance for the named location.
* @param file the path to the directory.
* @param lockFactory instance of {@link LockFactory} providing the
* locking implementation.
* @return the FSDirectory for the named file. */
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)
throws IOException
{
return getDirectory(file, create, lockFactory, true);
/** 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, true);
throws IOException
{
FSDirectory dir = getDirectory(file, null);
// This is now deprecated (creation should only be done
// by IndexWriter):
if (create) {
dir.create();
}
return dir;
}
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 any
throw new IOException("Cannot overwrite: " + file);

View File

@ -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. */

View File

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

View File

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

View File

@ -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 writer
IndexWriter 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 # hits
IndexSearcher searcher = new IndexSearcher(dir);
@ -238,7 +238,7 @@ public class TestBackwardsCompatibility extends TestCase
public 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 TestCase
public 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);

View File

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

View File

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

View File

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

View File

@ -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 TestCase
throw 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 TestCase
private 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 TestCase
reader.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 before
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
@ -423,13 +426,14 @@ public class TestIndexReader extends TestCase
dir.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 TestCase
private 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");

View File

@ -447,7 +447,7 @@ public class TestIndexWriter extends TestCase
File indexDir = new File(tempDir, "lucenetestindexwriter");
try {
Directory dir = FSDirectory.getDirectory(indexDir, true);
Directory dir = FSDirectory.getDirectory(indexDir);
// add one document & close writer
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);

View File

@ -65,7 +65,7 @@ public class TestNorms extends TestCase {
// test with a single index: index1
File 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: index3
File 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);

View File

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

View File

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

View File

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

View File

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

View File

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