Skip to content

Apache Lucene open-source search software

LUCENE-3185: fix a couple issues with NRTCachingDir

Michael McCandless506f9271911b
diff --git a/lucene/contrib/CHANGES.txt b/lucene/contrib/CHANGES.txt
index e45af1f3640..e7d0a36e5da 100644
--- a/lucene/contrib/CHANGES.txt
+++ b/lucene/contrib/CHANGES.txt
@@ -79,6 +79,12 @@ API Changes
First/SecondPassGroupingCollector. (Martijn van Groningen, Mike
McCandless)
+Bug Fixes
+
+ * LUCENE-3185: Fix bug in NRTCachingDirectory.deleteFile that would
+ always throw exception and sometimes fail to actually delete the
+ file. (Mike McCandless)
+
Build
* LUCENE-3149: Upgrade contrib/icu's ICU jar file to ICU 4.8.
diff --git a/lucene/contrib/misc/src/java/org/apache/lucene/store/NRTCachingDirectory.java b/lucene/contrib/misc/src/java/org/apache/lucene/store/NRTCachingDirectory.java
index 86998a0378f..6cd8ac02cba 100644
--- a/lucene/contrib/misc/src/java/org/apache/lucene/store/NRTCachingDirectory.java
+++ b/lucene/contrib/misc/src/java/org/apache/lucene/store/NRTCachingDirectory.java
@@ -89,10 +89,10 @@ public class NRTCachingDirectory extends Directory {
private final long maxCachedBytes;
private static final boolean VERBOSE = false;
-
+
/**
* We will cache a newly created output if 1) it's a
- * flush or a merge and the estimated size of the merged segmnt is <=
+ * flush or a merge and the estimated size of the merged segment is <=
* maxMergeSizeMB, and 2) the total cached bytes is <=
* maxCachedMB */
public NRTCachingDirectory(Directory delegate, double maxMergeSizeMB, double maxCachedMB) {
@@ -101,6 +101,36 @@ public class NRTCachingDirectory extends Directory {
maxCachedBytes = (long) (maxCachedMB*1024*1024);
}
+ @Override
+ public LockFactory getLockFactory() {
+ return delegate.getLockFactory();
+ }
+
+ @Override
+ public void setLockFactory(LockFactory lf) throws IOException {
+ delegate.setLockFactory(lf);
+ }
+
+ @Override
+ public String getLockID() {
+ return delegate.getLockID();
+ }
+
+ @Override
+ public Lock makeLock(String name) {
+ return delegate.makeLock(name);
+ }
+
+ @Override
+ public void clearLock(String name) throws IOException {
+ delegate.clearLock(name);
+ }
+
+ @Override
+ public String toString() {
+ return "NRTCachingDirectory(" + delegate + "; maxCacheMB=" + (maxCachedBytes/1024/1024.) + " maxMergeSizeMB=" + (maxMergeSizeBytes/1024/1024.) + ")";
+ }
+
@Override
public synchronized String[] listAll() throws IOException {
final Set<String> files = new HashSet<String>();
@@ -108,7 +138,9 @@ public class NRTCachingDirectory extends Directory {
files.add(f);
}
for(String f : delegate.listAll()) {
- assert !files.contains(f);
+ // Cannot do this -- if lucene calls createOutput but
+ // file already exists then this falsely trips:
+ //assert !files.contains(f): "file \"" + f + "\" is in both dirs";
files.add(f);
}
return files.toArray(new String[files.size()]);
@@ -136,12 +168,15 @@ public class NRTCachingDirectory extends Directory {
@Override
public synchronized void deleteFile(String name) throws IOException {
- // Delete from both, in case we are currently uncaching:
if (VERBOSE) {
System.out.println("nrtdir.deleteFile name=" + name);
}
- cache.deleteFile(name);
- delegate.deleteFile(name);
+ if (cache.fileExists(name)) {
+ assert !delegate.fileExists(name);
+ cache.deleteFile(name);
+ } else {
+ delegate.deleteFile(name);
+ }
}
@Override
@@ -207,16 +242,6 @@ public class NRTCachingDirectory extends Directory {
}
}
- @Override
- public Lock makeLock(String name) {
- return delegate.makeLock(name);
- }
-
- @Override
- public void clearLock(String name) throws IOException {
- delegate.clearLock(name);
- }
-
/** Close this directory, which flushes any cached files
* to the delegate and then closes the delegate. */
@Override
diff --git a/lucene/contrib/misc/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java b/lucene/contrib/misc/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java
index 1f682ce6f78..9001d13a4de 100644
--- a/lucene/contrib/misc/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java
+++ b/lucene/contrib/misc/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java
@@ -111,4 +111,12 @@ public class TestNRTCachingDirectory extends LuceneTestCase {
conf.setMergeScheduler(cachedFSDir.getMergeScheduler());
IndexWriter writer = new IndexWriter(cachedFSDir, conf);
}
+
+ public void testDeleteFile() throws Exception {
+ Directory dir = new NRTCachingDirectory(newDirectory(), 2.0, 25.0);
+ dir.createOutput("foo.txt").close();
+ dir.deleteFile("foo.txt");
+ assertEquals(0, dir.listAll().length);
+ dir.close();
+ }
}