From c18676ed4fbe8bca981ad2af86f2e4a4e8a5baf5 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Thu, 4 Apr 2013 11:04:38 +0000 Subject: [PATCH] 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 --- lucene/CHANGES.txt | 4 ++++ .../lucene/index/StandardDirectoryReader.java | 6 ++++- .../index/TestDirectoryReaderReopen.java | 24 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index d84ed51b074..327dbbe19ef 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -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 diff --git a/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java b/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java index 2debf3773d2..3fe4f8f423c 100644 --- a/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java +++ b/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java @@ -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 { diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReaderReopen.java b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReaderReopen.java index e80d1bb19a3..bf580776cf4 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReaderReopen.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReaderReopen.java @@ -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 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(); + } }