Apache Lucene open-source search software
LUCENE-2634: NRT reader's isCurrent should return false if changes were just committed in the writer
Michael McCandlessd0ae4d826019
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex f98221b4ca8..6ba5a524a2d 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -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 weightdiff --git a/lucene/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/src/java/org/apache/lucene/index/IndexWriter.javaindex ff435d715ec..8e0b5d2c70e 100644--- a/lucene/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/src/java/org/apache/lucene/index/IndexWriter.java@@ -4542,6 +4542,10 @@ public class IndexWriter implements Closeable {// if any structural changes (new segments), we are// stalereturn 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();}diff --git a/lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java b/lucene/src/test/org/apache/lucene/index/RandomIndexWriter.javaindex fa3e85513ed..684a5c99dad 100644--- a/lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java+++ b/lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java@@ -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 codepointdiff --git a/lucene/src/test/org/apache/lucene/index/TestIndexWriterReader.java b/lucene/src/test/org/apache/lucene/index/TestIndexWriterReader.javaindex ee8e0f734bf..df8bff67e33 100644--- a/lucene/src/test/org/apache/lucene/index/TestIndexWriterReader.java+++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriterReader.java@@ -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));}diff --git a/lucene/src/test/org/apache/lucene/index/TestIsCurrent.java b/lucene/src/test/org/apache/lucene/index/TestIsCurrent.javanew file mode 100644index 00000000000..52650f4258b--- /dev/null+++ b/lucene/src/test/org/apache/lucene/index/TestIsCurrent.java@@ -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();+ }+}