LUCENE-5079: IndexWriter.hasUncommittedChanges()

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1497039 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2013-06-26 18:48:52 +00:00
parent bd1246fad6
commit 146d2be826
3 changed files with 33 additions and 2 deletions

View File

@ -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

View File

@ -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<SegmentInfoPerCommit> 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 {

View File

@ -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();
}
}