Skip to content

Apache Lucene open-source search software

LUCENE-5289: clarify that hasUncommittedChanges can still be true after commit, due to merges

Michael McCandlesse1d5edea4218
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 b794e0bc08a..96e02e54030 100644
--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
@@ -2880,7 +2880,12 @@ public class IndexWriter implements Closeable, TwoPhaseCommit{
commitInternal();
}
- /** Returns true if there are changes that have not been committed */
+ /** Returns true if there are changes that have not been
+ * committed. Note that if a merge kicked off as a
+ * result of flushing a new segment during {@link
+ * #commit}, or a concurrent merged finished,
+ * this method may return true right after you
+ * had just called {@link #commit}. */
public final boolean hasUncommittedChanges() {
return changeCount != lastCommitChangeCount || docWriter.anyChanges() || bufferedDeletesStream.any();
}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java
index 9bba40dd63c..4f0bc600797 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java
@@ -2249,6 +2249,11 @@ public class TestIndexWriter extends LuceneTestCase {
doc.add(newTextField("myfield", "a b c", Field.Store.NO));
writer.addDocument(doc);
assertTrue(writer.hasUncommittedChanges());
+
+ // Must commit, waitForMerges, commit again, to be
+ // certain that hasUncommittedChanges returns false:
+ writer.commit();
+ writer.waitForMerges();
writer.commit();
assertFalse(writer.hasUncommittedChanges());
writer.addDocument(doc);
@@ -2258,10 +2263,20 @@ public class TestIndexWriter extends LuceneTestCase {
doc.add(newStringField("id", "xyz", Field.Store.YES));
writer.addDocument(doc);
assertTrue(writer.hasUncommittedChanges());
+
+ // Must commit, waitForMerges, commit again, to be
+ // certain that hasUncommittedChanges returns false:
+ writer.commit();
+ writer.waitForMerges();
writer.commit();
assertFalse(writer.hasUncommittedChanges());
writer.deleteDocuments(new Term("id", "xyz"));
assertTrue(writer.hasUncommittedChanges());
+
+ // Must commit, waitForMerges, commit again, to be
+ // certain that hasUncommittedChanges returns false:
+ writer.commit();
+ writer.waitForMerges();
writer.commit();
assertFalse(writer.hasUncommittedChanges());
writer.close();