add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / index / TestThreadedOptimize.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 org.apache.lucene.analysis.MockAnalyzer;
21 import org.apache.lucene.analysis.Analyzer;
22 import org.apache.lucene.analysis.MockTokenizer;
23 import org.apache.lucene.store.Directory;
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.document.Field;
26 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
27 import org.apache.lucene.util.English;
28
29 import org.apache.lucene.util.LuceneTestCase;
30
31 import java.util.Random;
32
33 public class TestThreadedOptimize extends LuceneTestCase {
34   
35   private static final Analyzer ANALYZER = new MockAnalyzer(random, MockTokenizer.SIMPLE, true);
36
37   private final static int NUM_THREADS = 3;
38   //private final static int NUM_THREADS = 5;
39
40   private final static int NUM_ITER = 1;
41
42   private final static int NUM_ITER2 = 1;
43
44   private volatile boolean failed;
45
46   private void setFailed() {
47     failed = true;
48   }
49
50   public void runTest(Random random, Directory directory) throws Exception {
51
52     IndexWriter writer = new IndexWriter(
53         directory,
54         newIndexWriterConfig(TEST_VERSION_CURRENT, ANALYZER).
55             setOpenMode(OpenMode.CREATE).
56             setMaxBufferedDocs(2).
57             setMergePolicy(newLogMergePolicy())
58     );
59
60     for(int iter=0;iter<NUM_ITER;iter++) {
61       final int iterFinal = iter;
62
63       setMergeFactor(writer.getConfig().getMergePolicy(), 100);
64
65       for(int i=0;i<200;i++) {
66         Document d = new Document();
67         d.add(newField("id", Integer.toString(i), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
68         d.add(newField("contents", English.intToEnglish(i), Field.Store.NO, Field.Index.ANALYZED_NO_NORMS));
69         writer.addDocument(d);
70       }
71
72       ((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(4);
73       writer.setInfoStream(VERBOSE ? System.out : null);
74
75       Thread[] threads = new Thread[NUM_THREADS];
76       
77       for(int i=0;i<NUM_THREADS;i++) {
78         final int iFinal = i;
79         final IndexWriter writerFinal = writer;
80         threads[i] = new Thread() {
81           @Override
82           public void run() {
83             try {
84               for(int j=0;j<NUM_ITER2;j++) {
85                 writerFinal.optimize(false);
86                 for(int k=0;k<17*(1+iFinal);k++) {
87                   Document d = new Document();
88                   d.add(newField("id", iterFinal + "_" + iFinal + "_" + j + "_" + k, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
89                   d.add(newField("contents", English.intToEnglish(iFinal+k), Field.Store.NO, Field.Index.ANALYZED_NO_NORMS));
90                   writerFinal.addDocument(d);
91                 }
92                 for(int k=0;k<9*(1+iFinal);k++)
93                   writerFinal.deleteDocuments(new Term("id", iterFinal + "_" + iFinal + "_" + j + "_" + k));
94                 writerFinal.optimize();
95               }
96             } catch (Throwable t) {
97               setFailed();
98               System.out.println(Thread.currentThread().getName() + ": hit exception");
99               t.printStackTrace(System.out);
100             }
101           }
102         };
103       }
104
105       for(int i=0;i<NUM_THREADS;i++)
106         threads[i].start();
107
108       for(int i=0;i<NUM_THREADS;i++)
109         threads[i].join();
110
111       assertTrue(!failed);
112
113       final int expectedDocCount = (int) ((1+iter)*(200+8*NUM_ITER2*(NUM_THREADS/2.0)*(1+NUM_THREADS)));
114
115       assertEquals("index=" + writer.segString() + " numDocs=" + writer.numDocs() + " maxDoc=" + writer.maxDoc() + " config=" + writer.getConfig(), expectedDocCount, writer.numDocs());
116       assertEquals("index=" + writer.segString() + " numDocs=" + writer.numDocs() + " maxDoc=" + writer.maxDoc() + " config=" + writer.getConfig(), expectedDocCount, writer.maxDoc());
117
118       writer.close();
119       writer = new IndexWriter(directory, newIndexWriterConfig(
120           TEST_VERSION_CURRENT, ANALYZER).setOpenMode(
121           OpenMode.APPEND).setMaxBufferedDocs(2));
122       
123       IndexReader reader = IndexReader.open(directory, true);
124       assertTrue("reader=" + reader, reader.isOptimized());
125       assertEquals(expectedDocCount, reader.numDocs());
126       reader.close();
127     }
128     writer.close();
129   }
130
131   public void testThreadedOptimize() throws Exception {
132     Directory directory = newDirectory();
133     runTest(random, directory);
134     directory.close();
135   }
136 }