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;
31 public class TestOptimizeForever extends LuceneTestCase {
33 // Just counts how many merges are done for optimize
34 private static class MyIndexWriter extends IndexWriter {
36 AtomicInteger optimizeMergeCount = new AtomicInteger();
37 private boolean first;
39 public MyIndexWriter(Directory dir, IndexWriterConfig conf) throws Exception {
44 public void merge(MergePolicy.OneMerge merge) throws CorruptIndexException, IOException {
45 if (merge.optimize && (first || merge.segments.size() == 1)) {
48 System.out.println("TEST: optimized merge");
50 optimizeMergeCount.incrementAndGet();
56 public void test() throws Exception {
57 final Directory d = newDirectory();
58 final MyIndexWriter w = new MyIndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
59 w.setInfoStream(VERBOSE ? System.out : null);
61 // Try to make an index that requires optimizing:
62 w.getConfig().setMaxBufferedDocs(_TestUtil.nextInt(random, 2, 11));
63 final int numStartDocs = atLeast(20);
64 final LineFileDocs docs = new LineFileDocs(random);
65 for(int docIDX=0;docIDX<numStartDocs;docIDX++) {
66 w.addDocument(docs.nextDoc());
68 MergePolicy mp = w.getConfig().getMergePolicy();
69 final int mergeAtOnce = 1+w.segmentInfos.size();
70 if (mp instanceof TieredMergePolicy) {
71 ((TieredMergePolicy) mp).setMaxMergeAtOnce(mergeAtOnce);
72 } else if (mp instanceof LogMergePolicy) {
73 ((LogMergePolicy) mp).setMergeFactor(mergeAtOnce);
81 final AtomicBoolean doStop = new AtomicBoolean();
82 w.getConfig().setMaxBufferedDocs(2);
83 Thread t = new Thread() {
87 while (!doStop.get()) {
88 w.updateDocument(new Term("docid", "" + random.nextInt(numStartDocs)),
90 // Force deletes to apply
91 w.getReader().close();
93 } catch (Throwable t) {
94 throw new RuntimeException(t);
102 assertTrue("optimize count is " + w.optimizeMergeCount.get(), w.optimizeMergeCount.get() <= 1);