LUCENE-1976: fix again for 3.1 (fix was temporarily lost in flex merge)

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@931314 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2010-04-06 20:38:14 +00:00
parent d40dacc805
commit 29ff915048
3 changed files with 26 additions and 4 deletions

View File

@ -86,7 +86,7 @@ public class TestIndexWriterReader extends LuceneTestCase {
// get a reader
IndexReader r1 = writer.getReader();
//assertTrue(r1.isCurrent());
assertTrue(r1.isCurrent());
String id10 = r1.document(10).getField("id").stringValue();
@ -94,7 +94,7 @@ public class TestIndexWriterReader extends LuceneTestCase {
newDoc.removeField("id");
newDoc.add(new Field("id", Integer.toString(8000), Store.YES, Index.NOT_ANALYZED));
writer.updateDocument(new Term("id", id10), newDoc);
//assertFalse(r1.isCurrent());
assertFalse(r1.isCurrent());
IndexReader r2 = writer.getReader();
assertTrue(r2.isCurrent());
@ -157,7 +157,7 @@ public class TestIndexWriterReader extends LuceneTestCase {
IndexReader r0 = writer.getReader();
assertTrue(r0.isCurrent());
writer.addIndexesNoOptimize(new Directory[] { dir2 });
//assertFalse(r0.isCurrent());
assertFalse(r0.isCurrent());
r0.close();
IndexReader r1 = writer.getReader();

View File

@ -56,6 +56,7 @@ class DirectoryReader extends IndexReader implements Cloneable {
private IndexDeletionPolicy deletionPolicy;
private Lock writeLock;
private SegmentInfos segmentInfos;
private SegmentInfos segmentInfosStart;
private boolean stale;
private final int termInfosIndexDivisor;
@ -148,6 +149,7 @@ class DirectoryReader extends IndexReader implements Cloneable {
this.directory = writer.getDirectory();
this.readOnly = true;
segmentInfos = infos;
segmentInfosStart = (SegmentInfos) infos.clone();
this.termInfosIndexDivisor = termInfosIndexDivisor;
if (codecs == null) {
this.codecs = CodecProvider.getDefault();
@ -930,7 +932,12 @@ class DirectoryReader extends IndexReader implements Cloneable {
@Override
public boolean isCurrent() throws CorruptIndexException, IOException {
ensureOpen();
return SegmentInfos.readCurrentVersion(directory, codecs) == segmentInfos.getVersion();
if (writer == null || writer.isClosed()) {
// we loaded SegmentInfos from the directory
return SegmentInfos.readCurrentVersion(directory, codecs) == segmentInfos.getVersion();
} else {
return writer.nrtIsCurrent(segmentInfosStart);
}
}
@Override

View File

@ -84,6 +84,7 @@ public class TestIndexWriterReader extends LuceneTestCase {
// get a reader
IndexReader r1 = writer.getReader();
assertTrue(r1.isCurrent());
String id10 = r1.document(10).getField("id").stringValue();
@ -91,15 +92,20 @@ public class TestIndexWriterReader extends LuceneTestCase {
newDoc.removeField("id");
newDoc.add(new Field("id", Integer.toString(8000), Store.YES, Index.NOT_ANALYZED));
writer.updateDocument(new Term("id", id10), newDoc);
assertFalse(r1.isCurrent());
IndexReader r2 = writer.getReader();
assertTrue(r2.isCurrent());
assertEquals(0, count(new Term("id", id10), r2));
assertEquals(1, count(new Term("id", Integer.toString(8000)), r2));
r1.close();
writer.close();
assertTrue(r2.isCurrent());
IndexReader r3 = IndexReader.open(dir1, true);
assertTrue(r3.isCurrent());
assertTrue(r2.isCurrent());
assertEquals(0, count(new Term("id", id10), r3));
assertEquals(1, count(new Term("id", Integer.toString(8000)), r3));
@ -143,9 +149,18 @@ public class TestIndexWriterReader extends LuceneTestCase {
createIndexNoClose(!optimize, "index2", writer2);
writer2.close();
IndexReader r0 = writer.getReader();
assertTrue(r0.isCurrent());
writer.addIndexesNoOptimize(new Directory[] { dir2 });
assertFalse(r0.isCurrent());
r0.close();
IndexReader r1 = writer.getReader();
assertTrue(r1.isCurrent());
writer.commit();
assertTrue(r1.isCurrent());
assertEquals(200, r1.maxDoc());
int index2df = r1.docFreq(new Term("indexname", "index2"));