LUCENE-4898: openIfChange now allows targeting an IndexCommit from an NRT reader

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1464437 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2013-04-04 11:04:38 +00:00
parent 8b573c413d
commit c18676ed4f
3 changed files with 33 additions and 1 deletions

View File

@ -160,6 +160,10 @@ New Features
* LUCENE-4645: Added support for the "Contains" spatial predicate for
RecursivePrefixTreeStrategy. (David Smiley)
* LUCENE-4898: DirectoryReader.openIfChanged now allows opening a reader
on an IndexCommit starting from a near-real-time reader (previously
this would throw IllegalArgumentException). (Mike McCandless)
Optimizations
* LUCENE-4839: SorterTemplate.merge can now be overridden in order to replace

View File

@ -260,7 +260,7 @@ final class StandardDirectoryReader extends DirectoryReader {
private DirectoryReader doOpenFromWriter(IndexCommit commit) throws IOException {
if (commit != null) {
throw new IllegalArgumentException("a reader obtained from IndexWriter.getReader() cannot currently accept a commit");
return doOpenFromCommit(commit);
}
if (writer.nrtIsCurrent(segmentInfos)) {
@ -293,6 +293,10 @@ final class StandardDirectoryReader extends DirectoryReader {
}
}
return doOpenFromCommit(commit);
}
private DirectoryReader doOpenFromCommit(IndexCommit commit) throws IOException {
return (DirectoryReader) new SegmentInfos.FindSegmentsFile(directory) {
@Override
protected Object doBody(String segmentFileName) throws IOException {

View File

@ -594,4 +594,28 @@ public class TestDirectoryReaderReopen extends LuceneTestCase {
r.close();
dir.close();
}
public void testOpenIfChangedNRTToCommit() throws Exception {
Directory dir = newDirectory();
// Can't use RIW because it randomly commits:
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
Document doc = new Document();
doc.add(newStringField("field", "value", Field.Store.NO));
w.addDocument(doc);
w.commit();
List<IndexCommit> commits = DirectoryReader.listCommits(dir);
assertEquals(1, commits.size());
w.addDocument(doc);
DirectoryReader r = DirectoryReader.open(w, true);
assertEquals(2, r.numDocs());
IndexReader r2 = DirectoryReader.openIfChanged(r, commits.get(0));
assertNotNull(r2);
r.close();
assertEquals(1, r2.numDocs());
w.close();
r2.close();
dir.close();
}
}