From c898b44722325904019d86badd2f01f059192220 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Wed, 31 Oct 2012 16:22:53 +0000 Subject: [PATCH] LUCENE-4521: make sure we commit new del file if tryDeleteDocument succeeds but IW has no other pending changes git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1404229 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 6 ++++ .../org/apache/lucene/index/IndexWriter.java | 4 +++ .../lucene/index/TestIndexWriterDelete.java | 29 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index ef90961e77f..9b4c149c4e1 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -104,6 +104,12 @@ Bug Fixes * LUCENE-4511: TermsFilter might return wrong results if a field is not indexed or doesn't exist in the index. (Simon Willnauer) +* LUCENE-4521: IndexWriter.tryDeleteDocument could return true + (successfully deleting the document) but then on IndexWriter + close/commit fail to write the new deletions, if no other changes + happened in the IndexWriter instance. (Ivan Vasilev via Mike + McCandless) + Optimizations * LUCENE-4512: Additional memory savings for CompressingStoredFieldsIndex.MEMORY_CHUNK diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java index 6db8d3087ee..04e61fbb4ea 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java @@ -1317,6 +1317,10 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { checkpoint(); } } + + // Must bump changeCount so if no other changes + // happened, we still commit this change: + changeCount++; } //System.out.println(" yes " + info.info.name + " " + docID); return true; diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterDelete.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterDelete.java index d47b2f4f868..889f401273b 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterDelete.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterDelete.java @@ -39,6 +39,7 @@ import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.store.MockDirectoryWrapper; import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.util.Bits; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util._TestUtil; @@ -1116,4 +1117,32 @@ public class TestIndexWriterDelete extends LuceneTestCase { assertFalse(s.contains("has deletions")); dir.close(); } + + public void testTryDeleteDocument() throws Exception { + + Directory d = newDirectory(); + + IndexWriterConfig iwc = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())); + IndexWriter w = new IndexWriter(d, iwc); + Document doc = new Document(); + w.addDocument(doc); + w.addDocument(doc); + w.addDocument(doc); + w.close(); + + iwc = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())); + iwc.setOpenMode(IndexWriterConfig.OpenMode.APPEND); + w = new IndexWriter(d, iwc); + IndexReader r = DirectoryReader.open(w, false); + assertTrue(w.tryDeleteDocument(r, 1)); + assertTrue(w.tryDeleteDocument(r.leaves().get(0).reader(), 0)); + r.close(); + w.close(); + + r = DirectoryReader.open(d); + assertEquals(2, r.numDeletedDocs()); + assertNotNull(MultiFields.getLiveDocs(r)); + r.close(); + d.close(); + } }