LUCENE-2402: Add an explicit method to invoke IndexDeletionPolicy

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@936605 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shai Erera 2010-04-22 03:45:01 +00:00
parent 4fdba4441f
commit bd8aebac1c
4 changed files with 71 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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