Index: lucene/CHANGES.txt =================================================================== --- lucene/CHANGES.txt (revision 935454) +++ lucene/CHANGES.txt (working copy) @@ -219,6 +219,12 @@ expressions). (Uwe Schindler, Robert Muir) +* LUCENE-2402: IndexWriter.deleteUnusedFiles now deletes unreferenced commit + points too. If you use an IndexDeletionPolicy which holds onto index commits + (such as SnapshotDeletionPolicy), you can call this method to remove those + commit points when they are not needed anymore (instead of waiting for the + next commit). (Shai Erera) + Bug fixes * LUCENE-2119: Don't throw NegativeArraySizeException if you pass Index: lucene/src/java/org/apache/lucene/index/IndexWriter.java =================================================================== --- lucene/src/java/org/apache/lucene/index/IndexWriter.java (revision 935454) +++ lucene/src/java/org/apache/lucene/index/IndexWriter.java (working copy) @@ -4961,8 +4961,20 @@ * IndexWriter, you'll see the unused files linger. If * that's a problem, call this method to delete them * (once you've closed the open readers that were - * preventing their deletion). */ + * preventing their deletion). + * + *
In addition, you can call this method to delete + * unreferenced index commits. This might be useful if you + * are using an {@link IndexDeletionPolicy} which holds + * onto index commits until some criteria are met, but those + * commits are no longer needed. Otherwise, those commits will + * be deleted the next time commit() is called. + */ public synchronized void deleteUnusedFiles() throws IOException { - deleter.deletePendingFiles(); + synchronized (commitLock) { + deleter.checkpoint(rollbackSegmentInfos, true); +// deleter.checkpoint((SegmentInfos) segmentInfos.clone(), true); + } } + } Index: lucene/src/test/org/apache/lucene/index/TestIndexWriter.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestIndexWriter.java (revision 935454) +++ lucene/src/test/org/apache/lucene/index/TestIndexWriter.java (working copy) @@ -4915,6 +4915,40 @@ } } + public void testDeleteUnsedFiles2() throws Exception { + // Validates that iw.deleteUnusedFiles() also deletes unused index commits + // in case a deletion policy which holds onto commits is used. + Directory dir = new MockRAMDirectory(); + SnapshotDeletionPolicy sdp = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy()); + IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig( + TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)) + .setIndexDeletionPolicy(sdp)); + + // First commit + Document doc = new Document(); + doc.add(new Field("c", "val", Store.YES, Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS)); + writer.addDocument(doc); + writer.commit(); + assertEquals(1, IndexReader.listCommits(dir).size()); + + // Keep that commit + sdp.snapshot(); + + // Second commit - now KeepOnlyLastCommit cannot delete the prev commit. + doc = new Document(); + doc.add(new Field("c", "val", Store.YES, Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS)); + writer.addDocument(doc); + writer.commit(); + assertEquals(2, IndexReader.listCommits(dir).size()); + + // Should delete the unreferenced commit + sdp.release(); + writer.deleteUnusedFiles(); + assertEquals(1, IndexReader.listCommits(dir).size()); + + writer.close(); + } + private static class FlushCountingIndexWriter extends IndexWriter { int flushCount; public FlushCountingIndexWriter(Directory dir, IndexWriterConfig iwc) throws IOException {