001: package org.apache.lucene.benchmark.byTask.tasks;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.BufferedWriter;
021: import java.io.FileOutputStream;
022: import java.io.OutputStreamWriter;
023:
024: import org.apache.lucene.benchmark.byTask.PerfRunData;
025: import org.apache.lucene.benchmark.byTask.feeds.DocMaker;
026: import org.apache.lucene.benchmark.byTask.feeds.BasicDocMaker;
027: import org.apache.lucene.benchmark.byTask.utils.Config;
028: import org.apache.lucene.document.Document;
029: import org.apache.lucene.document.Field;
030:
031: public class WriteLineDocTask extends PerfTask {
032:
033: /**
034: * Default value for property <code>doc.add.log.step<code> - indicating how often
035: * an "added N docs" message should be logged.
036: */
037: public static final int DEFAULT_WRITELINE_DOC_LOG_STEP = 1000;
038:
039: public WriteLineDocTask(PerfRunData runData) {
040: super (runData);
041: }
042:
043: private int logStep = -1;
044: private int docSize = 0;
045: int count = 0;
046: private BufferedWriter lineFileOut = null;
047: private DocMaker docMaker;
048:
049: public final static String SEP = "\t";
050:
051: /*
052: * (non-Javadoc)
053: * @see PerfTask#setup()
054: */
055: public void setup() throws Exception {
056: super .setup();
057: if (lineFileOut == null) {
058: Config config = getRunData().getConfig();
059: String fileName = config.get("line.file.out", null);
060: if (fileName == null)
061: throw new Exception("line.file.out must be set");
062: lineFileOut = new BufferedWriter(new OutputStreamWriter(
063: new FileOutputStream(fileName), "UTF-8"));
064: }
065: docMaker = getRunData().getDocMaker();
066: }
067:
068: public void tearDown() throws Exception {
069: log(++count);
070: super .tearDown();
071: }
072:
073: public int doLogic() throws Exception {
074: Document doc;
075: if (docSize > 0) {
076: doc = docMaker.makeDocument(docSize);
077: } else {
078: doc = docMaker.makeDocument();
079: }
080:
081: Field f = doc.getField(BasicDocMaker.BODY_FIELD);
082:
083: String body, title, date;
084: if (f != null)
085: body = f.stringValue().replace('\t', ' ');
086: else
087: body = null;
088:
089: f = doc.getField(BasicDocMaker.TITLE_FIELD);
090: if (f != null)
091: title = f.stringValue().replace('\t', ' ');
092: else
093: title = "";
094:
095: f = doc.getField(BasicDocMaker.DATE_FIELD);
096: if (f != null)
097: date = f.stringValue().replace('\t', ' ');
098: else
099: date = "";
100:
101: if (body != null) {
102: lineFileOut.write(title, 0, title.length());
103: lineFileOut.write(SEP);
104: lineFileOut.write(date, 0, date.length());
105: lineFileOut.write(SEP);
106: lineFileOut.write(body, 0, body.length());
107: lineFileOut.newLine();
108: lineFileOut.flush();
109: }
110: return 1;
111: }
112:
113: private void log(int count) {
114: if (logStep < 0) {
115: // init once per instance
116: logStep = getRunData().getConfig().get(
117: "doc.writeline.log.step",
118: DEFAULT_WRITELINE_DOC_LOG_STEP);
119: }
120: if (logStep > 0 && (count % logStep) == 0) {
121: System.out.println("--> "
122: + Thread.currentThread().getName()
123: + " processed (add) " + count + " docs");
124: }
125: }
126:
127: /**
128: * Set the params (docSize only)
129: * @param params docSize, or 0 for no limit.
130: */
131: public void setParams(String params) {
132: super .setParams(params);
133: docSize = (int) Float.parseFloat(params);
134: }
135:
136: /* (non-Javadoc)
137: * @see org.apache.lucene.benchmark.byTask.tasks.PerfTask#supportsParams()
138: */
139: public boolean supportsParams() {
140: return true;
141: }
142: }
|