LUCENE-4972: DirectoryTaxonomyWriter makes a commit even if no changes were made

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1478638 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shai Erera 2013-05-03 03:52:50 +00:00
parent 30e43c836a
commit e2c54ff680
3 changed files with 69 additions and 6 deletions

View File

@ -86,6 +86,9 @@ Bug Fixes
* LUCENE-4974: CommitIndexTask was broken if no params were set. (Shai Erera)
* LUCENE-4972: DirectoryTaxonomyWriter created empty commits even if no changes
were made. (Shai Erera, Michael McCandless)
Optimizations
* LUCENE-4938: Don't use an unnecessarily large priority queue in IndexSearcher

View File

@ -225,7 +225,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {
}
// no commit data, or no epoch in it means an old taxonomy, so set its epoch to 1, for lack
// of a better value.
indexEpoch = epochStr == null ? 1 : Long.parseLong(epochStr);
indexEpoch = epochStr == null ? 1 : Long.parseLong(epochStr, 16);
}
if (openMode == OpenMode.CREATE) {
@ -354,8 +354,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {
@Override
public synchronized void close() throws IOException {
if (!isClosed) {
indexWriter.setCommitData(combinedCommitData(indexWriter.getCommitData()));
indexWriter.commit();
commit();
doClose();
}
}
@ -616,7 +615,11 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {
@Override
public synchronized void commit() throws IOException {
ensureOpen();
// LUCENE-4972: if we always call setCommitData, we create empty commits
String epochStr = indexWriter.getCommitData().get(INDEX_EPOCH);
if (epochStr == null || Long.parseLong(epochStr, 16) != indexEpoch) {
indexWriter.setCommitData(combinedCommitData(indexWriter.getCommitData()));
}
indexWriter.commit();
}
@ -626,7 +629,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {
if (commitData != null) {
m.putAll(commitData);
}
m.put(INDEX_EPOCH, Long.toString(indexEpoch));
m.put(INDEX_EPOCH, Long.toString(indexEpoch, 16));
return m;
}
@ -647,7 +650,11 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {
@Override
public synchronized void prepareCommit() throws IOException {
ensureOpen();
// LUCENE-4972: if we always call setCommitData, we create empty commits
String epochStr = indexWriter.getCommitData().get(INDEX_EPOCH);
if (epochStr == null || Long.parseLong(epochStr, 16) != indexEpoch) {
indexWriter.setCommitData(combinedCommitData(indexWriter.getCommitData()));
}
indexWriter.prepareCommit();
}

View File

@ -360,4 +360,57 @@ public class TestDirectoryTaxonomyWriter extends FacetTestCase {
dir.close();
}
@Test
public void testCommitNoEmptyCommits() throws Exception {
// LUCENE-4972: DTW used to create empty commits even if no changes were made
Directory dir = newDirectory();
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir);
taxoWriter.addCategory(new CategoryPath("a"));
taxoWriter.commit();
long gen1 = SegmentInfos.getLastCommitGeneration(dir);
taxoWriter.commit();
long gen2 = SegmentInfos.getLastCommitGeneration(dir);
assertEquals("empty commit should not have changed the index", gen1, gen2);
taxoWriter.close();
dir.close();
}
@Test
public void testCloseNoEmptyCommits() throws Exception {
// LUCENE-4972: DTW used to create empty commits even if no changes were made
Directory dir = newDirectory();
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir);
taxoWriter.addCategory(new CategoryPath("a"));
taxoWriter.commit();
long gen1 = SegmentInfos.getLastCommitGeneration(dir);
taxoWriter.close();
long gen2 = SegmentInfos.getLastCommitGeneration(dir);
assertEquals("empty commit should not have changed the index", gen1, gen2);
taxoWriter.close();
dir.close();
}
@Test
public void testPrepareCommitNoEmptyCommits() throws Exception {
// LUCENE-4972: DTW used to create empty commits even if no changes were made
Directory dir = newDirectory();
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir);
taxoWriter.addCategory(new CategoryPath("a"));
taxoWriter.prepareCommit();
taxoWriter.commit();
long gen1 = SegmentInfos.getLastCommitGeneration(dir);
taxoWriter.prepareCommit();
taxoWriter.commit();
long gen2 = SegmentInfos.getLastCommitGeneration(dir);
assertEquals("empty commit should not have changed the index", gen1, gen2);
taxoWriter.close();
dir.close();
}
}