LUCENE-2634: NRT reader's isCurrent should return false if changes were just committed in the writer

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@994979 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2010-09-08 10:44:19 +00:00
parent f4d567eb06
commit d0ae4d8260
5 changed files with 137 additions and 4 deletions

View File

@ -507,6 +507,9 @@ Bug fixes
* LUCENE-2272: Fix explain in PayloadNearQuery and also fix scoring issue (Peter Keegan via Grant Ingersoll)
* LUCENE-2634: isCurrent on an NRT reader was failing to return false
if the writer had just committed (Nikolay Zamosenchuk via Mike McCandless)
New features
* LUCENE-2128: Parallelized fetching document frequencies during weight

View File

@ -4542,6 +4542,10 @@ public class IndexWriter implements Closeable {
// if any structural changes (new segments), we are
// stale
return false;
} else if (infos.getGeneration() != segmentInfos.getGeneration()) {
// if any commit took place since we were opened, we
// are stale
return false;
} else {
return !docWriter.anyChanges();
}

View File

@ -110,10 +110,18 @@ public class RandomIndexWriter implements Closeable {
w.commit();
}
public int numDocs() throws IOException {
return w.numDocs();
}
public int maxDoc() {
return w.maxDoc();
}
public void deleteAll() throws IOException {
w.deleteAll();
}
public IndexReader getReader() throws IOException {
// If we are writing with PreFlexRW, force a full
// IndexReader.open so terms are sorted in codepoint

View File

@ -156,7 +156,7 @@ public class TestIndexWriterReader extends LuceneTestCase {
assertTrue(r1.isCurrent());
writer.commit();
assertTrue(r1.isCurrent());
assertFalse(r1.isCurrent());
assertEquals(200, r1.maxDoc());
@ -725,12 +725,12 @@ public class TestIndexWriterReader extends LuceneTestCase {
final Random r = new Random();
do {
try {
for(int i=0;i<10;i++) {
writer.addDocument(createDocument(10*count+i, "test", 4));
for(int docUpto=0;docUpto<10;docUpto++) {
writer.addDocument(createDocument(10*count+docUpto, "test", 4));
}
count++;
final int limit = count*10;
for(int i=0;i<5;i++) {
for(int delUpto=0;delUpto<5;delUpto++) {
int x = r.nextInt(limit);
writer.deleteDocuments(new Term("field3", "b"+x));
}

View File

@ -0,0 +1,118 @@
package org.apache.lucene.index;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.util.*;
import org.apache.lucene.analysis.*;
import org.apache.lucene.store.*;
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.IOException;
import java.util.Random;
public class TestIsCurrent extends LuceneTestCaseJ4 {
private RandomIndexWriter writer;
private Directory directory;
private Random rand;
@Override
public void setUp() throws Exception {
super.setUp();
rand = newRandom();
// initialize directory
directory = newDirectory(rand);
writer = new RandomIndexWriter(rand, directory);
// write document
Document doc = new Document();
doc.add(new Field("UUID", "1", Store.YES, Index.ANALYZED));
writer.addDocument(doc);
writer.commit();
}
@Override
public void tearDown() throws Exception {
super.tearDown();
writer.close();
directory.close();
}
/**
* Failing testcase showing the trouble
*
* @throws IOException
*/
@Test
public void testDeleteByTermIsCurrent() throws IOException {
// get reader
IndexReader reader = writer.getReader();
// assert index has a document and reader is up2date
assertEquals("One document should be in the index", 1, writer.numDocs());
assertTrue("Document added, reader should be stale ", reader.isCurrent());
// remove document
Term idTerm = new Term("UUID", "1");
writer.deleteDocuments(idTerm);
writer.commit();
// assert document has been deleted (index changed), reader is stale
assertEquals("Document should be removed", 0, writer.numDocs());
assertFalse("Reader should be stale", reader.isCurrent());
reader.close();
}
/**
* Testcase for example to show that writer.deleteAll() is working as expected
*
* @throws IOException
*/
@Test
public void testDeleteAllIsCurrent() throws IOException {
// get reader
IndexReader reader = writer.getReader();
// assert index has a document and reader is up2date
assertEquals("One document should be in the index", 1, writer.numDocs());
assertTrue("Document added, reader should be stale ", reader.isCurrent());
// remove all documents
writer.deleteAll();
writer.commit();
// assert document has been deleted (index changed), reader is stale
assertEquals("Document should be removed", 0, writer.numDocs());
assertFalse("Reader should be stale", reader.isCurrent());
reader.close();
}
}