Skip to content

Apache Lucene open-source search software

LUCENE-3899: don't rely upon DirectoryReader.indexExists to determine if we should checkIndex

Robert Muirf3b90132c97f
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java b/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java
index 2be81498420..4b1ca5c554b 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java
@@ -45,6 +45,7 @@ import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.MockDirectoryWrapper;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
@@ -157,7 +158,9 @@ public class TestBackwardsCompatibility extends LuceneTestCase {
}
File oldIndxeDir = _TestUtil.getTempDir(unsupportedNames[i]);
_TestUtil.unzip(getDataFile("unsupported." + unsupportedNames[i] + ".zip"), oldIndxeDir);
- Directory dir = newFSDirectory(oldIndxeDir);
+ MockDirectoryWrapper dir = newFSDirectory(oldIndxeDir);
+ // don't checkindex, these are intentionally not supported
+ dir.setCheckIndexOnClose(false);
IndexReader reader = null;
IndexWriter writer = null;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java
index ad30c345638..2edd985d9f3 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java
@@ -559,7 +559,7 @@ public class MockDirectoryWrapper extends Directory {
}
open = false;
if (checkIndexOnClose) {
- if (DirectoryReader.indexExists(this)) {
+ if (indexPossiblyExists(this)) {
if (LuceneTestCase.VERBOSE) {
System.out.println("\nNOTE: MockDirectoryWrapper: now crash");
}
@@ -595,6 +595,26 @@ public class MockDirectoryWrapper extends Directory {
}
delegate.close();
}
+
+ /** don't rely upon DirectoryReader.fileExists to determine if we should
+ * checkIndex() or not. It might mask real problems, where we silently
+ * don't checkindex at all. instead we look for a segments file.
+ */
+ private boolean indexPossiblyExists(Directory d) throws IOException {
+ String files[];
+ try {
+ files = d.listAll();
+ } catch (IOException ex) {
+ // this means directory doesn't exist, which is ok. return false
+ return false;
+ }
+ for (String f : files) {
+ if (f.startsWith("segments_")) {
+ return true;
+ }
+ }
+ return false;
+ }
synchronized void removeOpenFile(Closeable c, String name) {
Integer v = openFiles.get(name);