Index: lucene/CHANGES.txt =================================================================== --- lucene/CHANGES.txt (revision 1478508) +++ lucene/CHANGES.txt (working copy) @@ -86,6 +86,9 @@ * 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) + Optimizations * LUCENE-4938: Don't use an unnecessarily large priority queue in IndexSearcher Index: lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java =================================================================== --- lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java (revision 1478508) +++ lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java (working copy) @@ -225,7 +225,7 @@ } // 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 @@ @Override public synchronized void close() throws IOException { if (!isClosed) { - indexWriter.setCommitData(combinedCommitData(indexWriter.getCommitData())); - indexWriter.commit(); + commit(); doClose(); } } @@ -616,7 +615,11 @@ @Override public synchronized void commit() throws IOException { ensureOpen(); - indexWriter.setCommitData(combinedCommitData(indexWriter.getCommitData())); + // 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 @@ 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 @@ @Override public synchronized void prepareCommit() throws IOException { ensureOpen(); - indexWriter.setCommitData(combinedCommitData(indexWriter.getCommitData())); + // 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(); } Index: lucene/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java =================================================================== --- lucene/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java (revision 1478508) +++ lucene/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java (working copy) @@ -359,5 +359,58 @@ taxoWriter.close(); 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(); + } + }