diff -r 22517d09657b -r 8645577fd2c5 lucene/src/java/org/apache/lucene/index/IndexWriter.java --- a/lucene/src/java/org/apache/lucene/index/IndexWriter.java Wed Sep 08 10:33:38 2010 +0000 +++ b/lucene/src/java/org/apache/lucene/index/IndexWriter.java Wed Sep 08 06:36:22 2010 -0400 @@ -4542,6 +4542,10 @@ // 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(); } diff -r 22517d09657b -r 8645577fd2c5 lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java --- a/lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java Wed Sep 08 10:33:38 2010 +0000 +++ b/lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java Wed Sep 08 06:36:22 2010 -0400 @@ -110,10 +110,18 @@ 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 diff -r 22517d09657b -r 8645577fd2c5 lucene/src/test/org/apache/lucene/index/TestIndexWriterReader.java --- a/lucene/src/test/org/apache/lucene/index/TestIndexWriterReader.java Wed Sep 08 10:33:38 2010 +0000 +++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriterReader.java Wed Sep 08 06:36:22 2010 -0400 @@ -156,7 +156,7 @@ assertTrue(r1.isCurrent()); writer.commit(); - assertTrue(r1.isCurrent()); + assertFalse(r1.isCurrent()); assertEquals(200, r1.maxDoc()); @@ -725,12 +725,12 @@ 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 -r 22517d09657b -r 8645577fd2c5 lucene/src/test/org/apache/lucene/index/TestIsCurrent.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lucene/src/test/org/apache/lucene/index/TestIsCurrent.java Wed Sep 08 06:36:22 2010 -0400 @@ -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(); + } +}