pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.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   public static IndexDeletionPolicy getIndexDeletionPolicy(Config config) {
78     String deletionPolicyName = config.get("deletion.policy", "org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy");
79     if (deletionPolicyName.equals(NoDeletionPolicy.class.getName())) {
80       return NoDeletionPolicy.INSTANCE;
81     } else {
82       try {
83         return Class.forName(deletionPolicyName).asSubclass(IndexDeletionPolicy.class).newInstance();
84       } catch (Exception e) {
85         throw new RuntimeException("unable to instantiate class '" + deletionPolicyName + "' as IndexDeletionPolicy", e);
86       }
87     }
88   }
89   
90   @Override
91   public int doLogic() throws IOException {
92     PerfRunData runData = getRunData();
93     Config config = runData.getConfig();
94     runData.setIndexWriter(configureWriter(config, runData, OpenMode.CREATE, null));
95     return 1;
96   }
97   
98   public static IndexWriterConfig createWriterConfig(Config config, PerfRunData runData, OpenMode mode, IndexCommit commit) {
99     Version version = Version.valueOf(config.get("writer.version", Version.LUCENE_31.toString()));
100     IndexWriterConfig iwConf = new IndexWriterConfig(version, runData.getAnalyzer());
101     iwConf.setOpenMode(mode);
102     IndexDeletionPolicy indexDeletionPolicy = getIndexDeletionPolicy(config);
103     iwConf.setIndexDeletionPolicy(indexDeletionPolicy);
104     if(commit != null)
105       iwConf.setIndexCommit(commit);
106     
107
108     final String mergeScheduler = config.get("merge.scheduler",
109                                              "org.apache.lucene.index.ConcurrentMergeScheduler");
110     if (mergeScheduler.equals(NoMergeScheduler.class.getName())) {
111       iwConf.setMergeScheduler(NoMergeScheduler.INSTANCE);
112     } else {
113       try {
114         iwConf.setMergeScheduler(Class.forName(mergeScheduler).asSubclass(MergeScheduler.class).newInstance());
115       } catch (Exception e) {
116         throw new RuntimeException("unable to instantiate class '" + mergeScheduler + "' as merge scheduler", e);
117       }
118       
119       if (mergeScheduler.equals("org.apache.lucene.index.ConcurrentMergeScheduler")) {
120         ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler) iwConf.getMergeScheduler();
121         int v = config.get("concurrent.merge.scheduler.max.thread.count", -1);
122         if (v != -1) {
123           cms.setMaxThreadCount(v);
124         }
125         v = config.get("concurrent.merge.scheduler.max.merge.count", -1);
126         if (v != -1) {
127           cms.setMaxMergeCount(v);
128         }
129       }
130     }
131
132     final String mergePolicy = config.get("merge.policy",
133                                           "org.apache.lucene.index.LogByteSizeMergePolicy");
134     boolean isCompound = config.get("compound", true);
135     if (mergePolicy.equals(NoMergePolicy.class.getName())) {
136       iwConf.setMergePolicy(isCompound ? NoMergePolicy.COMPOUND_FILES : NoMergePolicy.NO_COMPOUND_FILES);
137     } else {
138       try {
139         iwConf.setMergePolicy(Class.forName(mergePolicy).asSubclass(MergePolicy.class).newInstance());
140       } catch (Exception e) {
141         throw new RuntimeException("unable to instantiate class '" + mergePolicy + "' as merge policy", e);
142       }
143       if(iwConf.getMergePolicy() instanceof LogMergePolicy) {
144         LogMergePolicy logMergePolicy = (LogMergePolicy) iwConf.getMergePolicy();
145         logMergePolicy.setUseCompoundFile(isCompound);
146         logMergePolicy.setMergeFactor(config.get("merge.factor",OpenIndexTask.DEFAULT_MERGE_PFACTOR));
147       } else if(iwConf.getMergePolicy() instanceof TieredMergePolicy) {
148         TieredMergePolicy tieredMergePolicy = (TieredMergePolicy) iwConf.getMergePolicy();
149         tieredMergePolicy.setUseCompoundFile(isCompound);
150       }
151     }
152     final double ramBuffer = config.get("ram.flush.mb",OpenIndexTask.DEFAULT_RAM_FLUSH_MB);
153     final int maxBuffered = config.get("max.buffered",OpenIndexTask.DEFAULT_MAX_BUFFERED);
154     if (maxBuffered == IndexWriterConfig.DISABLE_AUTO_FLUSH) {
155       iwConf.setRAMBufferSizeMB(ramBuffer);
156       iwConf.setMaxBufferedDocs(maxBuffered);
157     } else {
158       iwConf.setMaxBufferedDocs(maxBuffered);
159       iwConf.setRAMBufferSizeMB(ramBuffer);
160     }
161     
162     return iwConf;
163   }
164   
165   public static IndexWriter configureWriter(Config config, PerfRunData runData, OpenMode mode, IndexCommit commit) throws CorruptIndexException, LockObtainFailedException, IOException {
166     IndexWriter writer = new IndexWriter(runData.getDirectory(), createWriterConfig(config, runData, mode, commit));
167     writer.setMaxFieldLength(config.get("max.field.length", OpenIndexTask.DEFAULT_MAX_FIELD_LENGTH));
168
169     String infoStreamVal = config.get("writer.info.stream", null);
170     if (infoStreamVal != null) {
171       if (infoStreamVal.equals("SystemOut")) {
172         writer.setInfoStream(System.out);
173       } else if (infoStreamVal.equals("SystemErr")) {
174         writer.setInfoStream(System.err);
175       } else {
176         File f = new File(infoStreamVal).getAbsoluteFile();
177         writer.setInfoStream(new PrintStream(new BufferedOutputStream(new FileOutputStream(f))));
178       }
179     }
180     return writer;
181   }
182 }