LUCENE-812: make sure Native/SimpleFSLockFactory can be set via system property

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@512493 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2007-02-27 23:33:31 +00:00
parent 178500b09c
commit 66d4eb336f
5 changed files with 95 additions and 26 deletions

View File

@ -29,6 +29,11 @@ Bug fixes
that there is a single trailing wildcard (and no additional wildcard that there is a single trailing wildcard (and no additional wildcard
or '?' in the query text). (Doron Cohen) or '?' in the query text). (Doron Cohen)
3. LUCENE-812: Add no-argument constructors to NativeFSLockFactory
and SimpleFSLockFactory. This enables all 4 builtin LockFactory
implementations to be specified via the System property
org.apache.lucene.store.FSDirectoryLockFactoryClass. (Mike McCandless)
Optimizations Optimizations
======================= Release 2.1.0 2007-02-14 ======================= ======================= Release 2.1.0 2007-02-14 =======================

View File

@ -291,6 +291,12 @@ public class FSDirectory extends Directory {
} catch (ClassCastException e) { } catch (ClassCastException e) {
throw new IOException("unable to cast LockClass " + lockClassName + " instance to a LockFactory"); throw new IOException("unable to cast LockClass " + lockClassName + " instance to a LockFactory");
} }
if (lockFactory instanceof NativeFSLockFactory) {
((NativeFSLockFactory) lockFactory).setLockDir(path);
} else if (lockFactory instanceof SimpleFSLockFactory) {
((SimpleFSLockFactory) lockFactory).setLockDir(path);
}
} else { } else {
// Our default lock is SimpleFSLockFactory; // Our default lock is SimpleFSLockFactory;
// default lockDir is our index directory: // default lockDir is our index directory:

View File

@ -52,10 +52,6 @@ import java.util.Random;
* is fine because the OS will free the locks held against * is fine because the OS will free the locks held against
* these files even though the files still remain.</p> * these files even though the files still remain.</p>
* *
* <p>Native locks file names have the substring "-n-", which
* you can use to differentiate them from lock files created
* by {@link SimpleFSLockFactory}.</p>
*
* @see LockFactory * @see LockFactory
*/ */
@ -90,6 +86,17 @@ public class NativeFSLockFactory extends LockFactory {
l.release(); l.release();
} }
/**
* Create a NativeFSLockFactory instance, with null (unset)
* lock directory. This is package-private and is only
* used by FSDirectory when creating this LockFactory via
* the System property
* org.apache.lucene.store.FSDirectoryLockFactoryClass.
*/
NativeFSLockFactory() throws IOException {
this((File) null);
}
/** /**
* Create a NativeFSLockFactory instance, storing lock * Create a NativeFSLockFactory instance, storing lock
* files into the specified lockDirName: * files into the specified lockDirName:
@ -107,9 +114,18 @@ public class NativeFSLockFactory extends LockFactory {
* @param lockDir where lock files are created. * @param lockDir where lock files are created.
*/ */
public NativeFSLockFactory(File lockDir) throws IOException { public NativeFSLockFactory(File lockDir) throws IOException {
setLockDir(lockDir);
}
/**
* Set the lock directory. This is package-private and is
* only used externally by FSDirectory when creating this
* LockFactory via the System property
* org.apache.lucene.store.FSDirectoryLockFactoryClass.
*/
void setLockDir(File lockDir) throws IOException {
this.lockDir = lockDir; this.lockDir = lockDir;
if (lockDir != null) {
// Ensure that lockDir exists and is a directory. // Ensure that lockDir exists and is a directory.
if (!lockDir.exists()) { if (!lockDir.exists()) {
if (!lockDir.mkdirs()) if (!lockDir.mkdirs())
@ -122,6 +138,7 @@ public class NativeFSLockFactory extends LockFactory {
acquireTestLock(); acquireTestLock();
} }
}
public synchronized Lock makeLock(String lockName) { public synchronized Lock makeLock(String lockName) {
if (lockPrefix != null) if (lockPrefix != null)

View File

@ -40,12 +40,23 @@ public class SimpleFSLockFactory extends LockFactory {
private File lockDir; private File lockDir;
/**
* Create a SimpleFSLockFactory instance, with null (unset)
* lock directory. This is package-private and is only
* used by FSDirectory when creating this LockFactory via
* the System property
* org.apache.lucene.store.FSDirectoryLockFactoryClass.
*/
SimpleFSLockFactory() throws IOException {
this((File) null);
}
/** /**
* Instantiate using the provided directory (as a File instance). * Instantiate using the provided directory (as a File instance).
* @param lockDir where lock files should be created. * @param lockDir where lock files should be created.
*/ */
public SimpleFSLockFactory(File lockDir) throws IOException { public SimpleFSLockFactory(File lockDir) throws IOException {
init(lockDir); setLockDir(lockDir);
} }
/** /**
@ -54,10 +65,16 @@ public class SimpleFSLockFactory extends LockFactory {
*/ */
public SimpleFSLockFactory(String lockDirName) throws IOException { public SimpleFSLockFactory(String lockDirName) throws IOException {
lockDir = new File(lockDirName); lockDir = new File(lockDirName);
init(lockDir); setLockDir(lockDir);
} }
protected void init(File lockDir) throws IOException { /**
* Set the lock directory. This is package-private and is
* only used externally by FSDirectory when creating this
* LockFactory via the System property
* org.apache.lucene.store.FSDirectoryLockFactoryClass.
*/
void setLockDir(File lockDir) throws IOException {
this.lockDir = lockDir; this.lockDir = lockDir;
} }

View File

@ -197,23 +197,47 @@ public class TestLockFactory extends TestCase {
// Verify: setting custom lock factory class (as system property) works: // Verify: setting custom lock factory class (as system property) works:
// Verify: all 4 builtin LockFactory implementations are
// settable this way
// Verify: FSDirectory does basic locking correctly // Verify: FSDirectory does basic locking correctly
public void testLockClassProperty() throws IOException { public void testLockClassProperty() throws IOException {
String indexDirName = "index.TestLockFactory3"; String indexDirName = "index.TestLockFactory3";
String prpName = "org.apache.lucene.store.FSDirectoryLockFactoryClass";
System.setProperty("org.apache.lucene.store.FSDirectoryLockFactoryClass", try {
"org.apache.lucene.store.NoLockFactory");
// NoLockFactory:
System.setProperty(prpName, "org.apache.lucene.store.NoLockFactory");
IndexWriter writer = new IndexWriter(indexDirName, new WhitespaceAnalyzer(), true); IndexWriter writer = new IndexWriter(indexDirName, new WhitespaceAnalyzer(), true);
assertTrue("FSDirectory did not use correct LockFactory: got " + writer.getDirectory().getLockFactory(), assertTrue("FSDirectory did not use correct LockFactory: got " + writer.getDirectory().getLockFactory(),
NoLockFactory.class.isInstance(writer.getDirectory().getLockFactory())); NoLockFactory.class.isInstance(writer.getDirectory().getLockFactory()));
// Put back to the correct default for subsequent tests:
// System.clearProperty("org.apache.lucene.store.FSDirectoryLockFactoryClass");
System.setProperty("org.apache.lucene.store.FSDirectoryLockFactoryClass", "");
writer.close(); writer.close();
// SingleInstanceLockFactory:
System.setProperty(prpName, "org.apache.lucene.store.SingleInstanceLockFactory");
writer = new IndexWriter(indexDirName, new WhitespaceAnalyzer(), true);
assertTrue("FSDirectory did not use correct LockFactory: got " + writer.getDirectory().getLockFactory(),
SingleInstanceLockFactory.class.isInstance(writer.getDirectory().getLockFactory()));
writer.close();
// NativeFSLockFactory:
System.setProperty(prpName, "org.apache.lucene.store.NativeFSLockFactory");
writer = new IndexWriter(indexDirName, new WhitespaceAnalyzer(), true);
assertTrue("FSDirectory did not use correct LockFactory: got " + writer.getDirectory().getLockFactory(),
NativeFSLockFactory.class.isInstance(writer.getDirectory().getLockFactory()));
writer.close();
// SimpleFSLockFactory:
System.setProperty(prpName, "org.apache.lucene.store.SimpleFSLockFactory");
writer = new IndexWriter(indexDirName, new WhitespaceAnalyzer(), true);
assertTrue("FSDirectory did not use correct LockFactory: got " + writer.getDirectory().getLockFactory(),
SimpleFSLockFactory.class.isInstance(writer.getDirectory().getLockFactory()));
writer.close();
} finally {
// Put back to the correct default for subsequent tests:
System.setProperty("org.apache.lucene.store.FSDirectoryLockFactoryClass", "");
}
// Cleanup // Cleanup
rmDir(indexDirName); rmDir(indexDirName);
} }