Index: lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java =================================================================== --- lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java (revision 1496623) +++ lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java (working copy) @@ -2209,4 +2209,27 @@ 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(); + } } Index: lucene/core/src/java/org/apache/lucene/index/IndexWriter.java =================================================================== --- lucene/core/src/java/org/apache/lucene/index/IndexWriter.java (revision 1496623) +++ lucene/core/src/java/org/apache/lucene/index/IndexWriter.java (working copy) @@ -214,7 +214,7 @@ 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 @@ 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 @@ if (infoStream.isEnabled("IW")) { infoStream.message("IW", "commit: wrote segments file \"" + pendingCommit.getSegmentsFileName() + "\""); } + segmentInfos.updateGeneration(pendingCommit); lastCommitChangeCount = pendingCommitChangeCount; - segmentInfos.updateGeneration(pendingCommit); rollbackSegments = pendingCommit.createBackupSegmentInfos(); deleter.checkpoint(pendingCommit, true); } finally {