LUCENE-771:

* API change: change LockFactory.clearAllLocks() to
    LockFactory.clearLock(String lockName) because locks are no longer
    so "global".  (This API is not released yet).  Now FSDirectory
    just clears specifically the write lock when create=true.

  * Fix abstraction violation of LockFactory: FSDirectory now calls
    clearLock() instead of doing File deletes itself (duh!).


git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@495911 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2007-01-13 15:40:34 +00:00
parent db238e787f
commit 9ae7389701
7 changed files with 32 additions and 45 deletions

View File

@ -332,15 +332,7 @@ public class FSDirectory extends Directory {
}
}
if (lockFactory.getLockPrefix() != null) {
lockFactory.clearAllLocks();
} else {
// Lock file is stored in the index, so we just remove
// it ourselves here:
File lockFile = new File(directory, IndexWriter.WRITE_LOCK_NAME);
if (lockFile.exists() && !lockFile.delete())
throw new IOException("Cannot delete " + lockFile);
}
lockFactory.clearLock(IndexWriter.WRITE_LOCK_NAME);
}
/** Returns an array of strings, one for each Lucene index file in the directory. */

View File

@ -56,9 +56,10 @@ public abstract class LockFactory {
public abstract Lock makeLock(String lockName);
/**
* Clear any existing locks. Only call this at a time when you
* are certain the lock files are not in use. {@link FSDirectory}
* calls this when creating a new index.
* 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.
*/
abstract void clearAllLocks() throws IOException;
abstract public void clearLock(String lockName) throws IOException;
}

View File

@ -129,24 +129,19 @@ public class NativeFSLockFactory extends LockFactory {
return new NativeFSLock(lockDir, lockName);
}
protected void clearAllLocks() throws IOException {
public void clearLock(String lockName) throws IOException {
// Note that this isn't strictly required anymore
// because the existence of these files does not mean
// they are locked, but, still do this in case people
// really want to see the files go away:
if (lockDir.exists() && lockPrefix != null) {
String[] files = lockDir.list();
if (files == null)
throw new IOException("Cannot read lock directory " +
lockDir.getAbsolutePath());
String prefix = lockPrefix + "-n-";
for (int i = 0; i < files.length; i++) {
if (files[i].startsWith(prefix)) {
File lockFile = new File(lockDir, files[i]);
if (!lockFile.delete())
throw new IOException("Cannot delete " + lockFile);
}
}
if (lockDir.exists()) {
if (lockPrefix != null) {
lockName = lockPrefix + "-n-" + lockName;
}
File lockFile = new File(lockDir, lockName);
if (lockFile.exists() && !lockFile.delete()) {
throw new IOException("Cannot delete " + lockFile);
}
}
}
};

View File

@ -42,7 +42,7 @@ public class NoLockFactory extends LockFactory {
return singletonLock;
}
public void clearAllLocks() {};
public void clearLock(String lockName) {};
};
class NoLock extends Lock {

View File

@ -68,20 +68,15 @@ public class SimpleFSLockFactory extends LockFactory {
return new SimpleFSLock(lockDir, lockName);
}
protected void clearAllLocks() throws IOException {
if (lockDir.exists() && lockPrefix != null) {
String[] files = lockDir.list();
if (files == null)
throw new IOException("Cannot read lock directory " +
lockDir.getAbsolutePath());
String prefix = lockPrefix + "-";
for (int i = 0; i < files.length; i++) {
if (!files[i].startsWith(prefix))
continue;
File lockFile = new File(lockDir, files[i]);
if (!lockFile.delete())
throw new IOException("Cannot delete " + lockFile);
}
public void clearLock(String lockName) throws IOException {
if (lockDir.exists()) {
if (lockPrefix != null) {
lockName = lockPrefix + "-" + lockName;
}
File lockFile = new File(lockDir, lockName);
if (lockFile.exists() && !lockFile.delete()) {
throw new IOException("Cannot delete " + lockFile);
}
}
}
};

View File

@ -43,8 +43,12 @@ public class SingleInstanceLockFactory extends LockFactory {
return new SingleInstanceLock(locks, lockName);
}
public void clearAllLocks() throws IOException {
locks = new HashSet();
public void clearLock(String lockName) throws IOException {
synchronized(locks) {
if (locks.contains(lockName)) {
locks.remove(lockName);
}
}
}
};

View File

@ -494,7 +494,7 @@ public class TestLockFactory extends TestCase {
return lock;
}
public void clearAllLocks() {}
public void clearLock(String specificLockName) {}
public class MockLock extends Lock {
public int lockAttempts;