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 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;
29
import org.apache.lucene.util.LuceneTestCase;
31
import java.util.Random;
33
public class TestThreadedOptimize extends LuceneTestCase {
35
private static final Analyzer ANALYZER = new MockAnalyzer(random, MockTokenizer.SIMPLE, true);
37
private final static int NUM_THREADS = 3;
38
//private final static int NUM_THREADS = 5;
40
private final static int NUM_ITER = 1;
42
private final static int NUM_ITER2 = 1;
44
private volatile boolean failed;
46
private void setFailed() {
50
public void runTest(Random random, Directory directory) throws Exception {
52
IndexWriter writer = new IndexWriter(
54
newIndexWriterConfig(TEST_VERSION_CURRENT, ANALYZER).
55
setOpenMode(OpenMode.CREATE).
56
setMaxBufferedDocs(2).
57
setMergePolicy(newLogMergePolicy())
60
for(int iter=0;iter<NUM_ITER;iter++) {
61
final int iterFinal = iter;
63
setMergeFactor(writer.getConfig().getMergePolicy(), 100);
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);
72
((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(4);
73
writer.setInfoStream(VERBOSE ? System.out : null);
75
Thread[] threads = new Thread[NUM_THREADS];
77
for(int i=0;i<NUM_THREADS;i++) {
79
final IndexWriter writerFinal = writer;
80
threads[i] = new Thread() {
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);
92
for(int k=0;k<9*(1+iFinal);k++)
93
writerFinal.deleteDocuments(new Term("id", iterFinal + "_" + iFinal + "_" + j + "_" + k));
94
writerFinal.optimize();
96
} catch (Throwable t) {
98
System.out.println(Thread.currentThread().getName() + ": hit exception");
99
t.printStackTrace(System.out);
105
for(int i=0;i<NUM_THREADS;i++)
108
for(int i=0;i<NUM_THREADS;i++)
113
final int expectedDocCount = (int) ((1+iter)*(200+8*NUM_ITER2*(NUM_THREADS/2.0)*(1+NUM_THREADS)));
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());
119
writer = new IndexWriter(directory, newIndexWriterConfig(
120
TEST_VERSION_CURRENT, ANALYZER).setOpenMode(
121
OpenMode.APPEND).setMaxBufferedDocs(2));
123
IndexReader reader = IndexReader.open(directory, true);
124
assertTrue("reader=" + reader, reader.isOptimized());
125
assertEquals(expectedDocCount, reader.numDocs());
131
public void testThreadedOptimize() throws Exception {
132
Directory directory = newDirectory();
133
runTest(random, directory);