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