Index: CHANGES.txt =================================================================== --- CHANGES.txt (revision 833886) +++ CHANGES.txt (working copy) @@ -188,6 +188,10 @@ infoStream on IndexWriter and then add an empty document and commit (Shai Erera via Mike McCandless) + * LUCENE-2046: IndexReader should not see the index as changed, after + IndexWriter.prepareCommit has been called but before + IndexWriter.commit is called. (Peter Keegan via Mike McCandless) + API Changes * Un-deprecate search(Weight weight, Filter filter, int n) from Index: src/test/org/apache/lucene/index/TestIndexReader.java =================================================================== --- src/test/org/apache/lucene/index/TestIndexReader.java (revision 833884) +++ src/test/org/apache/lucene/index/TestIndexReader.java (working copy) @@ -1778,4 +1778,24 @@ r2.close(); dir.close(); } + + // LUCENE-2046 + public void testPrepareCommitIsCurrent() throws Throwable { + Directory dir = new MockRAMDirectory(); + IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED); + Document doc = new Document(); + writer.addDocument(doc); + IndexReader r = IndexReader.open(dir, true); + assertTrue(r.isCurrent()); + writer.addDocument(doc); + writer.prepareCommit(); + assertTrue(r.isCurrent()); + IndexReader r2 = r.reopen(); + assertTrue(r == r2); + writer.commit(); + assertFalse(r.isCurrent()); + writer.close(); + r.close(); + dir.close(); + } } Index: src/java/org/apache/lucene/index/SegmentInfos.java =================================================================== --- src/java/org/apache/lucene/index/SegmentInfos.java (revision 833884) +++ src/java/org/apache/lucene/index/SegmentInfos.java (working copy) @@ -402,36 +402,14 @@ public static long readCurrentVersion(Directory directory) throws CorruptIndexException, IOException { - return ((Long) new FindSegmentsFile(directory) { - @Override - protected Object doBody(String segmentFileName) throws CorruptIndexException, IOException { - - IndexInput input = directory.openInput(segmentFileName); - - int format = 0; - long version = 0; - try { - format = input.readInt(); - if(format < 0){ - if (format < CURRENT_FORMAT) - throw new CorruptIndexException("Unknown format version: " + format); - version = input.readLong(); // read version - } - } - finally { - input.close(); - } - - if(format < 0) - return Long.valueOf(version); - - // We cannot be sure about the format of the file. - // Therefore we have to read the whole file and cannot simply seek to the version entry. - SegmentInfos sis = new SegmentInfos(); - sis.read(directory, segmentFileName); - return Long.valueOf(sis.getVersion()); - } - }.run()).longValue(); + // Fully read the segments file: this ensures that it's + // completely written so that if + // IndexWriter.prepareCommit has been called (but not + // yet commit), then the reader will still see itself as + // current: + SegmentInfos sis = new SegmentInfos(); + sis.read(directory); + return sis.version; } /**