mirror of https://github.com/apache/lucene.git
LUCENE-2046: IndexReader.isCurrent should still return true after writer calls prepareCommit
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@834035 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9db9f77743
commit
7ce48cea93
|
@ -192,6 +192,10 @@ Bug fixes
|
|||
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
|
||||
|
|
|
@ -402,36 +402,14 @@ public final class SegmentInfos extends Vector<SegmentInfo> {
|
|||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1778,4 +1778,24 @@ public class TestIndexReader extends LuceneTestCase
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue