add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / tasks / CreateIndexTask.java
1 package org.apache.lucene.benchmark.byTask.tasks;
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.benchmark.byTask.PerfRunData;
21 import org.apache.lucene.benchmark.byTask.utils.Config;
22 import org.apache.lucene.index.CorruptIndexException;
23 import org.apache.lucene.index.IndexCommit;
24 import org.apache.lucene.index.IndexDeletionPolicy;
25 import org.apache.lucene.index.IndexWriter;
26 import org.apache.lucene.index.IndexWriterConfig;
27 import org.apache.lucene.index.LogMergePolicy;
28 import org.apache.lucene.index.MergeScheduler;
29 import org.apache.lucene.index.ConcurrentMergeScheduler;
30 import org.apache.lucene.index.MergePolicy;
31 import org.apache.lucene.index.NoDeletionPolicy;
32 import org.apache.lucene.index.NoMergePolicy;
33 import org.apache.lucene.index.NoMergeScheduler;
34 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
35 import org.apache.lucene.index.TieredMergePolicy;
36 import org.apache.lucene.store.LockObtainFailedException;
37 import org.apache.lucene.util.Version;
38
39 import java.io.BufferedOutputStream;
40 import java.io.File;
41 import java.io.FileOutputStream;
42 import java.io.IOException;
43 import java.io.PrintStream;
44
45 /**
46  * Create an index. <br>
47  * Other side effects: index writer object in perfRunData is set. <br>
48  * Relevant properties: <code>merge.factor (default 10),
49  * max.buffered (default no flush), max.field.length (default
50  * 10,000 tokens), max.field.length, compound (default true), ram.flush.mb [default 0],
51  * merge.policy (default org.apache.lucene.index.LogByteSizeMergePolicy),
52  * merge.scheduler (default
53  * org.apache.lucene.index.ConcurrentMergeScheduler),
54  * concurrent.merge.scheduler.max.thread.count and
55  * concurrent.merge.scheduler.max.merge.count (defaults per
56  * ConcurrentMergeScheduler) </code>.
57  * <p>
58  * This task also supports a "writer.info.stream" property with the following
59  * values:
60  * <ul>
61  * <li>SystemOut - sets {@link IndexWriter#setInfoStream(java.io.PrintStream)}
62  * to {@link System#out}.
63  * <li>SystemErr - sets {@link IndexWriter#setInfoStream(java.io.PrintStream)}
64  * to {@link System#err}.
65  * <li>&lt;file_name&gt; - attempts to create a file given that name and sets
66  * {@link IndexWriter#setInfoStream(java.io.PrintStream)} to that file. If this
67  * denotes an invalid file name, or some error occurs, an exception will be
68  * thrown.
69  * </ul>
70  */
71 public class CreateIndexTask extends PerfTask {
72
73   public CreateIndexTask(PerfRunData runData) {
74     super(runData);
75   }
76
77   
78   
79   public static IndexDeletionPolicy getIndexDeletionPolicy(Config config) {
80     String deletionPolicyName = config.get("deletion.policy", "org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy");
81     if (deletionPolicyName.equals(NoDeletionPolicy.class.getName())) {
82       return NoDeletionPolicy.INSTANCE;
83     } else {
84       try {
85         return Class.forName(deletionPolicyName).asSubclass(IndexDeletionPolicy.class).newInstance();
86       } catch (Exception e) {
87         throw new RuntimeException("unable to instantiate class '" + deletionPolicyName + "' as IndexDeletionPolicy", e);
88       }
89     }
90   }
91   
92   @Override
93   public int doLogic() throws IOException {
94     PerfRunData runData = getRunData();
95     Config config = runData.getConfig();
96     runData.setIndexWriter(configureWriter(config, runData, OpenMode.CREATE, null));
97     return 1;
98   }
99   
100   public static IndexWriterConfig createWriterConfig(Config config, PerfRunData runData, OpenMode mode, IndexCommit commit) {
101     Version version = Version.valueOf(config.get("writer.version", Version.LUCENE_31.toString()));
102     IndexWriterConfig iwConf = new IndexWriterConfig(version, runData.getAnalyzer());
103     iwConf.setOpenMode(mode);
104     IndexDeletionPolicy indexDeletionPolicy = getIndexDeletionPolicy(config);
105     iwConf.setIndexDeletionPolicy(indexDeletionPolicy);
106     if(commit != null)
107       iwConf.setIndexCommit(commit);
108     
109
110     final String mergeScheduler = config.get("merge.scheduler",
111                                              "org.apache.lucene.index.ConcurrentMergeScheduler");
112     if (mergeScheduler.equals(NoMergeScheduler.class.getName())) {
113       iwConf.setMergeScheduler(NoMergeScheduler.INSTANCE);
114     } else {
115       try {
116         iwConf.setMergeScheduler(Class.forName(mergeScheduler).asSubclass(MergeScheduler.class).newInstance());
117       } catch (Exception e) {
118         throw new RuntimeException("unable to instantiate class '" + mergeScheduler + "' as merge scheduler", e);
119       }
120       
121       if (mergeScheduler.equals("org.apache.lucene.index.ConcurrentMergeScheduler")) {
122         ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler) iwConf.getMergeScheduler();
123         int v = config.get("concurrent.merge.scheduler.max.thread.count", -1);
124         if (v != -1) {
125           cms.setMaxThreadCount(v);
126         }
127         v = config.get("concurrent.merge.scheduler.max.merge.count", -1);
128         if (v != -1) {
129           cms.setMaxMergeCount(v);
130         }
131       }
132     }
133
134     final String mergePolicy = config.get("merge.policy",
135                                           "org.apache.lucene.index.LogByteSizeMergePolicy");
136     boolean isCompound = config.get("compound", true);
137     if (mergePolicy.equals(NoMergePolicy.class.getName())) {
138       iwConf.setMergePolicy(isCompound ? NoMergePolicy.COMPOUND_FILES : NoMergePolicy.NO_COMPOUND_FILES);
139     } else {
140       try {
141         iwConf.setMergePolicy(Class.forName(mergePolicy).asSubclass(MergePolicy.class).newInstance());
142       } catch (Exception e) {
143         throw new RuntimeException("unable to instantiate class '" + mergePolicy + "' as merge policy", e);
144       }
145       if(iwConf.getMergePolicy() instanceof LogMergePolicy) {
146         LogMergePolicy logMergePolicy = (LogMergePolicy) iwConf.getMergePolicy();
147         logMergePolicy.setUseCompoundFile(isCompound);
148         logMergePolicy.setMergeFactor(config.get("merge.factor",OpenIndexTask.DEFAULT_MERGE_PFACTOR));
149       } else if(iwConf.getMergePolicy() instanceof TieredMergePolicy) {
150         TieredMergePolicy tieredMergePolicy = (TieredMergePolicy) iwConf.getMergePolicy();
151         tieredMergePolicy.setUseCompoundFile(isCompound);
152       }
153     }
154     final double ramBuffer = config.get("ram.flush.mb",OpenIndexTask.DEFAULT_RAM_FLUSH_MB);
155     final int maxBuffered = config.get("max.buffered",OpenIndexTask.DEFAULT_MAX_BUFFERED);
156     if (maxBuffered == IndexWriterConfig.DISABLE_AUTO_FLUSH) {
157       iwConf.setRAMBufferSizeMB(ramBuffer);
158       iwConf.setMaxBufferedDocs(maxBuffered);
159     } else {
160       iwConf.setMaxBufferedDocs(maxBuffered);
161       iwConf.setRAMBufferSizeMB(ramBuffer);
162     }
163     
164     return iwConf;
165   }
166   
167   public static IndexWriter configureWriter(Config config, PerfRunData runData, OpenMode mode, IndexCommit commit) throws CorruptIndexException, LockObtainFailedException, IOException {
168     IndexWriter writer = new IndexWriter(runData.getDirectory(), createWriterConfig(config, runData, mode, commit));
169     writer.setMaxFieldLength(config.get("max.field.length", OpenIndexTask.DEFAULT_MAX_FIELD_LENGTH));
170
171     String infoStreamVal = config.get("writer.info.stream", null);
172     if (infoStreamVal != null) {
173       if (infoStreamVal.equals("SystemOut")) {
174         writer.setInfoStream(System.out);
175       } else if (infoStreamVal.equals("SystemErr")) {
176         writer.setInfoStream(System.err);
177       } else {
178         File f = new File(infoStreamVal).getAbsoluteFile();
179         writer.setInfoStream(new PrintStream(new BufferedOutputStream(new FileOutputStream(f))));
180       }
181     }
182     return writer;
183   }
184 }