Skip to content

Apache Lucene open-source search software

LUCENE-2402: Add an explicit method to invoke IndexDeletionPolicy

Shai Ererabd8aebac1ca1
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 9ed38c4e492..17bf8939469 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -219,6 +219,12 @@ API Changes
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
diff --git a/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java b/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java
index aa82f565d25..4cc22a61bdc 100644
--- a/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java
+++ b/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java
@@ -353,6 +353,26 @@ final class IndexFileDeleter {
deletePendingFiles();
}
+ /**
+ * Revisits the {@link IndexDeletionPolicy} by calling its
+ * {@link IndexDeletionPolicy#onCommit(List)} again with the known commits.
+ * This is useful in cases where a deletion policy which holds onto index
+ * commits is used. The application may know that some commits are not held by
+ * the deletion policy anymore and call
+ * {@link IndexWriter#deleteUnusedFiles()}, which will attempt to delete the
+ * unused commits again.
+ */
+ void revisitPolicy() throws IOException {
+ if (infoStream != null) {
+ message("now revisitPolicy");
+ }
+
+ if (commits.size() > 0) {
+ policy.onCommit(commits);
+ deleteCommits();
+ }
+ }
+
public void deletePendingFiles() throws IOException {
if (deletable != null) {
List<String> oldDeletable = deletable;
diff --git a/lucene/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/src/java/org/apache/lucene/index/IndexWriter.java
index 3e71f822c03..8c11465afbb 100644
--- a/lucene/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/lucene/src/java/org/apache/lucene/index/IndexWriter.java
@@ -4961,8 +4961,18 @@ public class IndexWriter implements Closeable {
* 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).
+ *
+ * <p> 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();
+ deleter.revisitPolicy();
}
+
}
diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java
index 8f04ac32758..1d3466f20f4 100644
--- a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java
+++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java
@@ -4915,6 +4915,40 @@ public class TestIndexWriter extends LuceneTestCase {
}
}
+ 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 {