1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import java.util.concurrent.atomic.AtomicBoolean;
22 import java.util.concurrent.atomic.AtomicInteger;
24 import org.apache.lucene.analysis.MockAnalyzer;
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.util.LineFileDocs;
27 import org.apache.lucene.util.LuceneTestCase;
28 import org.apache.lucene.util._TestUtil;
30 public class TestForceMergeForever extends LuceneTestCase {
32 // Just counts how many merges are done
33 private static class MyIndexWriter extends IndexWriter {
35 AtomicInteger mergeCount = new AtomicInteger();
36 private boolean first;
38 public MyIndexWriter(Directory dir, IndexWriterConfig conf) throws Exception {
43 public void merge(MergePolicy.OneMerge merge) throws CorruptIndexException, IOException {
44 if (merge.maxNumSegments != -1 && (first || merge.segments.size() == 1)) {
47 System.out.println("TEST: maxNumSegments merge");
49 mergeCount.incrementAndGet();
55 public void test() throws Exception {
56 final Directory d = newDirectory();
57 final MyIndexWriter w = new MyIndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
59 // Try to make an index that requires merging:
60 w.getConfig().setMaxBufferedDocs(_TestUtil.nextInt(random, 2, 11));
61 final int numStartDocs = atLeast(20);
62 final LineFileDocs docs = new LineFileDocs(random);
63 for(int docIDX=0;docIDX<numStartDocs;docIDX++) {
64 w.addDocument(docs.nextDoc());
66 MergePolicy mp = w.getConfig().getMergePolicy();
67 final int mergeAtOnce = 1+w.segmentInfos.size();
68 if (mp instanceof TieredMergePolicy) {
69 ((TieredMergePolicy) mp).setMaxMergeAtOnce(mergeAtOnce);
70 } else if (mp instanceof LogMergePolicy) {
71 ((LogMergePolicy) mp).setMergeFactor(mergeAtOnce);
79 final AtomicBoolean doStop = new AtomicBoolean();
80 w.getConfig().setMaxBufferedDocs(2);
81 Thread t = new Thread() {
85 while (!doStop.get()) {
86 w.updateDocument(new Term("docid", "" + random.nextInt(numStartDocs)),
88 // Force deletes to apply
89 w.getReader().close();
91 } catch (Throwable t) {
92 throw new RuntimeException(t);
100 assertTrue("merge count is " + w.mergeCount.get(), w.mergeCount.get() <= 1);