pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestForceMergeForever.java
1 package org.apache.lucene.index;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21 import java.util.concurrent.atomic.AtomicBoolean;
22 import java.util.concurrent.atomic.AtomicInteger;
23
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;
29
30 public class TestForceMergeForever extends LuceneTestCase {
31
32   // Just counts how many merges are done
33   private static class MyIndexWriter extends IndexWriter {
34
35     AtomicInteger mergeCount = new AtomicInteger();
36     private boolean first;
37
38     public MyIndexWriter(Directory dir, IndexWriterConfig conf) throws Exception {
39       super(dir, conf);
40     }
41
42     @Override
43     public void merge(MergePolicy.OneMerge merge) throws CorruptIndexException, IOException {
44       if (merge.maxNumSegments != -1 && (first || merge.segments.size() == 1)) {
45         first = false;
46         if (VERBOSE) {
47           System.out.println("TEST: maxNumSegments merge");
48         }
49         mergeCount.incrementAndGet();
50       }
51       super.merge(merge);
52     }
53   }
54
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)));
58
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());
65     }
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);
72     } else {
73       // skip test
74       w.close();
75       d.close();
76       return;
77     }
78
79     final AtomicBoolean doStop = new AtomicBoolean();
80     w.getConfig().setMaxBufferedDocs(2);
81     Thread t = new Thread() {
82       @Override
83       public void run() {
84         try {
85           while (!doStop.get()) {
86             w.updateDocument(new Term("docid", "" + random.nextInt(numStartDocs)),
87                              docs.nextDoc());
88             // Force deletes to apply
89             w.getReader().close();
90           }
91         } catch (Throwable t) {
92           throw new RuntimeException(t);
93         }
94       }
95       };
96     t.start();
97     w.forceMerge(1);
98     doStop.set(true);
99     t.join();
100     assertTrue("merge count is " + w.mergeCount.get(), w.mergeCount.get() <= 1);
101     w.close();
102     d.close();
103   }
104 }