LUCENE-2812: fix IndexReader.indexExists to return false when segments_N file cannot be read

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1045212 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2010-12-13 16:09:01 +00:00
parent 8535534a0d
commit 625f60388d
2 changed files with 18 additions and 3 deletions

View File

@ -835,13 +835,17 @@ public abstract class IndexReader implements Cloneable,Closeable {
/**
* Returns <code>true</code> if an index exists at the specified directory.
* If the directory does not exist or if there is no index in it.
* @param directory the directory to check for an index
* @return <code>true</code> if an index exists; <code>false</code> otherwise
* @throws IOException if there is a problem with accessing the index
*/
public static boolean indexExists(Directory directory) throws IOException {
return SegmentInfos.getCurrentSegmentGeneration(directory) != -1;
try {
new SegmentInfos().read(directory);
return true;
} catch (IOException ioe) {
return false;
}
}
/** Returns the number of documents in this index. */

View File

@ -1851,4 +1851,15 @@ public class TestIndexReader extends LuceneTestCase
dir.close();
}
// LUCENE-2812
public void testIndexExists() throws Exception {
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
writer.addDocument(new Document());
writer.prepareCommit();
assertFalse(IndexReader.indexExists(dir));
writer.close();
assertTrue(IndexReader.indexExists(dir));
dir.close();
}
}