diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 3b96df97b10..932555b0a3d 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -254,6 +254,9 @@ New Features * LUCENE-5063: FieldCache.DEFAULT.get(Ints|Longs) now uses bit-packing to save memory. (Adrien Grand) +* LUCENE-5079: IndexWriter.hasUncommittedChanges() returns true if there are + changes that have not been committed. (yonik, Mike McCandless, Uwe Schindler) + API Changes * LUCENE-5077: Make it easier to use compressed norms. Lucene42NormsFormat takes 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 2cf0fe8f2d9..d891562b1ae 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java @@ -214,7 +214,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { private final Analyzer analyzer; // how to analyze text private volatile long changeCount; // increments every time a change is completed - private long lastCommitChangeCount; // last changeCount that was committed + private volatile long lastCommitChangeCount; // last changeCount that was committed private List rollbackSegments; // list of segmentInfo we will fallback to if the commit fails @@ -2827,6 +2827,11 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { commitInternal(); } + /** Returns true if there are changes that have not been committed */ + public final boolean hasUncommittedChanges() { + return changeCount != lastCommitChangeCount; + } + private final void commitInternal() throws IOException { if (infoStream.isEnabled("IW")) { @@ -2866,8 +2871,8 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { if (infoStream.isEnabled("IW")) { infoStream.message("IW", "commit: wrote segments file \"" + pendingCommit.getSegmentsFileName() + "\""); } - lastCommitChangeCount = pendingCommitChangeCount; segmentInfos.updateGeneration(pendingCommit); + lastCommitChangeCount = pendingCommitChangeCount; rollbackSegments = pendingCommit.createBackupSegmentInfos(); deleter.checkpoint(pendingCommit, true); } finally { 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 95e9fef1382..8caafd6dcba 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -2209,4 +2209,27 @@ public class TestIndexWriter extends LuceneTestCase { dir.close(); } } + + public void testHasUncommittedChanges() throws IOException { + Directory dir = newDirectory(); + IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random()))); + assertTrue(writer.hasUncommittedChanges()); // this will be true because a commit will create an empty index + Document doc = new Document(); + doc.add(newTextField("myfield", "a b c", Field.Store.NO)); + writer.addDocument(doc); + assertTrue(writer.hasUncommittedChanges()); + writer.commit(); + assertFalse(writer.hasUncommittedChanges()); + writer.addDocument(doc); + assertTrue(writer.hasUncommittedChanges()); + writer.close(); + + writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random()))); + assertFalse(writer.hasUncommittedChanges()); + writer.addDocument(doc); + assertTrue(writer.hasUncommittedChanges()); + + writer.close(); + dir.close(); + } }