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
This commit is contained in:
Shai Erera 2011-05-02 14:00:43 +00:00
parent a75e5282c7
commit fda913a790
3 changed files with 55 additions and 7 deletions

View File

@ -280,6 +280,10 @@ API Changes
* LUCENE-2953: In addition to changes in 3.x, PriorityQueue#initialize(int) * LUCENE-2953: In addition to changes in 3.x, PriorityQueue#initialize(int)
function was moved into the ctor. (Uwe Schindler, Yonik Seeley) 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 New features
* LUCENE-2604: Added RegexpQuery support to QueryParser. Regular expressions * LUCENE-2604: Added RegexpQuery support to QueryParser. Regular expressions

View File

@ -1813,10 +1813,13 @@ public class IndexWriter implements Closeable {
return mergingSegments; return mergingSegments;
} }
/** Expert: the {@link MergeScheduler} calls this method /**
* to retrieve the next merge requested by the * Expert: the {@link MergeScheduler} calls this method to retrieve the next
* MergePolicy */ * merge requested by the MergePolicy
synchronized MergePolicy.OneMerge getNextMerge() { *
* @lucene.experimental
*/
public synchronized MergePolicy.OneMerge getNextMerge() {
if (pendingMerges.size() == 0) if (pendingMerges.size() == 0)
return null; return null;
else { else {
@ -2936,9 +2939,10 @@ public class IndexWriter implements Closeable {
/** /**
* Merges the indicated segments, replacing them in the stack with a * Merges the indicated segments, replacing them in the stack with a
* single segment. * single segment.
*
* @lucene.experimental
*/ */
public final void merge(MergePolicy.OneMerge merge)
final void merge(MergePolicy.OneMerge merge)
throws CorruptIndexException, IOException { throws CorruptIndexException, IOException {
boolean success = false; boolean success = false;

View File

@ -19,17 +19,21 @@ package org.apache.lucene;
import java.io.IOException; import java.io.IOException;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.MockDirectoryWrapper; 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.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LogMergePolicy; import org.apache.lucene.index.LogMergePolicy;
import org.apache.lucene.index.MergePolicy; import org.apache.lucene.index.MergePolicy;
import org.apache.lucene.index.ConcurrentMergeScheduler; 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.analysis.MockAnalyzer;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
/** /**
* Holds tests cases to verify external APIs are accessible * Holds tests cases to verify external APIs are accessible
* while not being in org.apache.lucene.index package. * while not being in org.apache.lucene.index package.
@ -106,4 +110,40 @@ public class TestMergeSchedulerExternal extends LuceneTestCase {
assertTrue(excCalled); assertTrue(excCalled);
dir.close(); 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();
}
} }