Apache Solr open-source search software
LUCENE-771:
Michael McCandless9ae738970125
diff --git a/src/java/org/apache/lucene/store/FSDirectory.java b/src/java/org/apache/lucene/store/FSDirectory.javaindex 9279896a68d..d27601d7cd3 100644--- a/src/java/org/apache/lucene/store/FSDirectory.java+++ b/src/java/org/apache/lucene/store/FSDirectory.java@@ -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. */diff --git a/src/java/org/apache/lucene/store/LockFactory.java b/src/java/org/apache/lucene/store/LockFactory.javaindex 079ed4ba4a6..8fd84d4d63f 100755--- a/src/java/org/apache/lucene/store/LockFactory.java+++ b/src/java/org/apache/lucene/store/LockFactory.java@@ -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;}diff --git a/src/java/org/apache/lucene/store/NativeFSLockFactory.java b/src/java/org/apache/lucene/store/NativeFSLockFactory.javaindex 565e535ab4e..afba74cf890 100755--- a/src/java/org/apache/lucene/store/NativeFSLockFactory.java+++ b/src/java/org/apache/lucene/store/NativeFSLockFactory.java@@ -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);+ }}}};diff --git a/src/java/org/apache/lucene/store/NoLockFactory.java b/src/java/org/apache/lucene/store/NoLockFactory.javaindex 5486652824e..2bc9d4b268e 100755--- a/src/java/org/apache/lucene/store/NoLockFactory.java+++ b/src/java/org/apache/lucene/store/NoLockFactory.java@@ -42,7 +42,7 @@ public class NoLockFactory extends LockFactory {return singletonLock;}- public void clearAllLocks() {};+ public void clearLock(String lockName) {};};class NoLock extends Lock {diff --git a/src/java/org/apache/lucene/store/SimpleFSLockFactory.java b/src/java/org/apache/lucene/store/SimpleFSLockFactory.javaindex 8975824c60c..0a7d3e4b1fd 100755--- a/src/java/org/apache/lucene/store/SimpleFSLockFactory.java+++ b/src/java/org/apache/lucene/store/SimpleFSLockFactory.java@@ -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);+ }}}};diff --git a/src/java/org/apache/lucene/store/SingleInstanceLockFactory.java b/src/java/org/apache/lucene/store/SingleInstanceLockFactory.javaindex e2b8351c9e0..69be4f57431 100755--- a/src/java/org/apache/lucene/store/SingleInstanceLockFactory.java+++ b/src/java/org/apache/lucene/store/SingleInstanceLockFactory.java@@ -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);+ }+ }}};diff --git a/src/test/org/apache/lucene/store/TestLockFactory.java b/src/test/org/apache/lucene/store/TestLockFactory.javaindex 9479c8c2504..b775e30993b 100755--- a/src/test/org/apache/lucene/store/TestLockFactory.java+++ b/src/test/org/apache/lucene/store/TestLockFactory.java@@ -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;