From fda913a790ce2f51368a91a2e72ab7fe39755a21 Mon Sep 17 00:00:00 2001 From: Shai Erera Date: Mon, 2 May 2011 14:00:43 +0000 Subject: [PATCH] LUCENE-3061: Open IndexWriter API to allow custom MergeScheduler implementation git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1098576 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 4 ++ .../org/apache/lucene/index/IndexWriter.java | 16 ++++--- .../lucene/TestMergeSchedulerExternal.java | 42 ++++++++++++++++++- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 2a9553bd9ae..83380041f13 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -280,6 +280,10 @@ API Changes * LUCENE-2953: In addition to changes in 3.x, PriorityQueue#initialize(int) function was moved into the ctor. (Uwe Schindler, Yonik Seeley) +* LUCENE-3061: IndexWriter's getNextMerge() and merge(OneMerge) are now public + (though @lucene.experimental), allowing for custom MergeScheduler + implementations. (Shai Erera) + New features * LUCENE-2604: Added RegexpQuery support to QueryParser. Regular expressions diff --git a/lucene/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/src/java/org/apache/lucene/index/IndexWriter.java index 166a6d594dd..826049c997f 100644 --- a/lucene/src/java/org/apache/lucene/index/IndexWriter.java +++ b/lucene/src/java/org/apache/lucene/index/IndexWriter.java @@ -1813,10 +1813,13 @@ public class IndexWriter implements Closeable { return mergingSegments; } - /** Expert: the {@link MergeScheduler} calls this method - * to retrieve the next merge requested by the - * MergePolicy */ - synchronized MergePolicy.OneMerge getNextMerge() { + /** + * Expert: the {@link MergeScheduler} calls this method to retrieve the next + * merge requested by the MergePolicy + * + * @lucene.experimental + */ + public synchronized MergePolicy.OneMerge getNextMerge() { if (pendingMerges.size() == 0) return null; else { @@ -2936,9 +2939,10 @@ public class IndexWriter implements Closeable { /** * Merges the indicated segments, replacing them in the stack with a * single segment. + * + * @lucene.experimental */ - - final void merge(MergePolicy.OneMerge merge) + public final void merge(MergePolicy.OneMerge merge) throws CorruptIndexException, IOException { boolean success = false; diff --git a/lucene/src/test/org/apache/lucene/TestMergeSchedulerExternal.java b/lucene/src/test/org/apache/lucene/TestMergeSchedulerExternal.java index 6d61cc0a15b..e477ae97b67 100644 --- a/lucene/src/test/org/apache/lucene/TestMergeSchedulerExternal.java +++ b/lucene/src/test/org/apache/lucene/TestMergeSchedulerExternal.java @@ -19,17 +19,21 @@ package org.apache.lucene; import java.io.IOException; import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.store.Directory; import org.apache.lucene.store.MockDirectoryWrapper; +import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.LogMergePolicy; import org.apache.lucene.index.MergePolicy; import org.apache.lucene.index.ConcurrentMergeScheduler; +import org.apache.lucene.index.MergeScheduler; +import org.apache.lucene.index.MergePolicy.OneMerge; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; - /** * Holds tests cases to verify external APIs are accessible * while not being in org.apache.lucene.index package. @@ -106,4 +110,40 @@ public class TestMergeSchedulerExternal extends LuceneTestCase { assertTrue(excCalled); dir.close(); } + + private static class ReportingMergeScheduler extends MergeScheduler { + + @Override + public void merge(IndexWriter writer) throws CorruptIndexException, IOException { + OneMerge merge = null; + while ((merge = writer.getNextMerge()) != null) { + if (VERBOSE) { + System.out.println("executing merge " + merge.segString(writer.getDirectory())); + } + writer.merge(merge); + } + } + + @Override + public void close() throws CorruptIndexException, IOException {} + + } + + public void testCustomMergeScheduler() throws Exception { + // we don't really need to execute anything, just to make sure the custom MS + // compiles. But ensure that it can be used as well, e.g., no other hidden + // dependencies or something. Therefore, don't use any random API ! + Directory dir = new RAMDirectory(); + IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null); + conf.setMergeScheduler(new ReportingMergeScheduler()); + IndexWriter writer = new IndexWriter(dir, conf); + writer.addDocument(new Document()); + writer.commit(); // trigger flush + writer.addDocument(new Document()); + writer.commit(); // trigger flush + writer.optimize(); + writer.close(); + dir.close(); + } + }