LUCENE-773: fixed NPE caused by IndexWriter assuming LockFactory was non-null

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@499089 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2007-01-23 17:33:11 +00:00
parent 3b13126bf5
commit d8979209bc
4 changed files with 61 additions and 2 deletions

View File

@ -193,6 +193,10 @@ API Changes
17. LUCENE-780: Add a static Directory.copy() method to copy files 17. LUCENE-780: Add a static Directory.copy() method to copy files
from one Directory to another. (Jiri Kuhn via Mike McCandless) from one Directory to another. (Jiri Kuhn via Mike McCandless)
18. LUCENE-773: Added Directory.clearLock(String name) to forcefully
remove an old lock. The default implementation is to ask the
lockFactory (if non null) to clear the lock. (Mike McCandless)
Bug fixes Bug fixes
1. Fixed the web application demo (built with "ant war-demo") which 1. Fixed the web application demo (built with "ant war-demo") which

View File

@ -333,7 +333,7 @@ public class IndexWriter {
if (create) { if (create) {
// Clear the write lock in case it's leftover: // Clear the write lock in case it's leftover:
directory.getLockFactory().clearLock(IndexWriter.WRITE_LOCK_NAME); directory.clearLock(IndexWriter.WRITE_LOCK_NAME);
} }
Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME); Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);

View File

@ -88,6 +88,17 @@ public abstract class Directory {
public Lock makeLock(String name) { public Lock makeLock(String name) {
return lockFactory.makeLock(name); return lockFactory.makeLock(name);
} }
/**
* Attempt to clear (forcefully unlock and remove) the
* specified lock. Only call this at a time when you are
* certain this lock is no longer in use.
* @param lockName name of the lock to be cleared.
*/
public void clearLock(String name) throws IOException {
if (lockFactory != null) {
lockFactory.clearLock(name);
}
}
/** Closes the store. */ /** Closes the store. */
public abstract void close() public abstract void close()
@ -106,8 +117,12 @@ public abstract class Directory {
this.lockFactory = lockFactory; this.lockFactory = lockFactory;
lockFactory.setLockPrefix(this.getLockID()); lockFactory.setLockPrefix(this.getLockID());
} }
/** /**
* Get the LockFactory that this Directory instance is using for its locking implementation. * Get the LockFactory that this Directory instance is
* using for its locking implementation. Note that this
* may be null for Directory implementations that provide
* their own locking implementation.
*/ */
public LockFactory getLockFactory() { public LockFactory getLockFactory() {
return this.lockFactory; return this.lockFactory;

View File

@ -21,6 +21,9 @@ import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput; import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.MockRAMDirectory; import org.apache.lucene.store.MockRAMDirectory;
import org.apache.lucene.store.LockFactory;
import org.apache.lucene.store.Lock;
import org.apache.lucene.store.SingleInstanceLockFactory;
/** /**
* @author goller * @author goller
@ -690,6 +693,43 @@ public class TestIndexWriter extends TestCase
} }
} }
// Make sure that a Directory implementation that does
// not use LockFactory at all (ie overrides makeLock and
// implements its own private locking) works OK. This
// was raised on java-dev as loss of backwards
// compatibility.
public void testNullLockFactory() throws IOException {
final class MyRAMDirectory extends RAMDirectory {
private LockFactory myLockFactory;
MyRAMDirectory() {
lockFactory = null;
myLockFactory = new SingleInstanceLockFactory();
}
public Lock makeLock(String name) {
return myLockFactory.makeLock(name);
}
}
Directory dir = new MyRAMDirectory();
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
for (int i = 0; i < 100; i++) {
addDoc(writer);
}
writer.close();
IndexReader reader = IndexReader.open(dir);
Term searchTerm = new Term("content", "aaa");
IndexSearcher searcher = new IndexSearcher(dir);
Hits hits = searcher.search(new TermQuery(searchTerm));
assertEquals("did not get right number of hits", 100, hits.length());
writer.close();
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
writer.close();
dir.close();
}
private void rmDir(File dir) { private void rmDir(File dir) {
File[] files = dir.listFiles(); File[] files = dir.listFiles();
if (files != null) { if (files != null) {