add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / index / TestOptimizeForever.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
31 public class TestOptimizeForever extends LuceneTestCase {
32
33   // Just counts how many merges are done for optimize
34   private static class MyIndexWriter extends IndexWriter {
35
36     AtomicInteger optimizeMergeCount = new AtomicInteger();
37     private boolean first;
38
39     public MyIndexWriter(Directory dir, IndexWriterConfig conf) throws Exception {
40       super(dir, conf);
41     }
42
43     @Override
44     public void merge(MergePolicy.OneMerge merge) throws CorruptIndexException, IOException {
45       if (merge.optimize && (first || merge.segments.size() == 1)) {
46         first = false;
47         if (VERBOSE) {
48           System.out.println("TEST: optimized merge");
49         }
50         optimizeMergeCount.incrementAndGet();
51       }
52       super.merge(merge);
53     }
54   }
55
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);
60
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());
67     }
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);
74     } else {
75       // skip test
76       w.close();
77       d.close();
78       return;
79     }
80
81     final AtomicBoolean doStop = new AtomicBoolean();
82     w.getConfig().setMaxBufferedDocs(2);
83     Thread t = new Thread() {
84       @Override
85       public void run() {
86         try {
87           while (!doStop.get()) {
88             w.updateDocument(new Term("docid", "" + random.nextInt(numStartDocs)),
89                              docs.nextDoc());
90             // Force deletes to apply
91             w.getReader().close();
92           }
93         } catch (Throwable t) {
94           throw new RuntimeException(t);
95         }
96       }
97       };
98     t.start();
99     w.optimize();
100     doStop.set(true);
101     t.join();
102     assertTrue("optimize count is " + w.optimizeMergeCount.get(), w.optimizeMergeCount.get() <= 1);
103     w.close();
104     d.close();
105   }
106 }