1 package org.apache.lucene.benchmark.byTask.feeds;
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 java.io.BufferedWriter;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.OutputStreamWriter;
26 import java.util.Properties;
28 import org.apache.commons.compress.compressors.CompressorStreamFactory;
29 import org.apache.lucene.analysis.WhitespaceAnalyzer;
30 import org.apache.lucene.benchmark.BenchmarkTestCase;
31 import org.apache.lucene.benchmark.byTask.PerfRunData;
32 import org.apache.lucene.benchmark.byTask.feeds.LineDocSource.HeaderLineParser;
33 import org.apache.lucene.benchmark.byTask.feeds.LineDocSource.LineParser;
34 import org.apache.lucene.benchmark.byTask.tasks.AddDocTask;
35 import org.apache.lucene.benchmark.byTask.tasks.CloseIndexTask;
36 import org.apache.lucene.benchmark.byTask.tasks.CreateIndexTask;
37 import org.apache.lucene.benchmark.byTask.tasks.TaskSequence;
38 import org.apache.lucene.benchmark.byTask.tasks.WriteLineDocTask;
39 import org.apache.lucene.benchmark.byTask.utils.Config;
40 import org.apache.lucene.index.Term;
41 import org.apache.lucene.search.IndexSearcher;
42 import org.apache.lucene.search.TermQuery;
43 import org.apache.lucene.search.TopDocs;
45 /** Tests the functionality of {@link LineDocSource}. */
46 public class LineDocSourceTest extends BenchmarkTestCase {
48 private static final CompressorStreamFactory csFactory = new CompressorStreamFactory();
50 private void createBZ2LineFile(File file, boolean addHeader) throws Exception {
51 OutputStream out = new FileOutputStream(file);
52 out = csFactory.createCompressorOutputStream("bzip2", out);
53 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
54 writeDocsToFile(writer, addHeader, null);
58 private void writeDocsToFile(BufferedWriter writer, boolean addHeader, Properties otherFields) throws IOException {
60 writer.write(WriteLineDocTask.FIELDS_HEADER_INDICATOR);
61 writer.write(WriteLineDocTask.SEP);
62 writer.write(DocMaker.TITLE_FIELD);
63 writer.write(WriteLineDocTask.SEP);
64 writer.write(DocMaker.DATE_FIELD);
65 writer.write(WriteLineDocTask.SEP);
66 writer.write(DocMaker.BODY_FIELD);
67 if (otherFields!=null) {
68 // additional field names in the header
69 for (Object fn : otherFields.keySet()) {
70 writer.write(WriteLineDocTask.SEP);
71 writer.write(fn.toString());
76 StringBuilder doc = new StringBuilder();
77 doc.append("title").append(WriteLineDocTask.SEP).append("date").append(WriteLineDocTask.SEP).append(DocMaker.BODY_FIELD);
78 if (otherFields!=null) {
79 // additional field values in the doc line
80 for (Object fv : otherFields.values()) {
81 doc.append(WriteLineDocTask.SEP).append(fv.toString());
84 writer.write(doc.toString());
88 private void createRegularLineFile(File file, boolean addHeader) throws Exception {
89 OutputStream out = new FileOutputStream(file);
90 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
91 writeDocsToFile(writer, addHeader, null);
95 private void createRegularLineFileWithMoreFields(File file, String...extraFields) throws Exception {
96 OutputStream out = new FileOutputStream(file);
97 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
98 Properties p = new Properties();
99 for (String f : extraFields) {
102 writeDocsToFile(writer, true, p);
106 private void doIndexAndSearchTest(File file, Class<? extends LineParser> lineParserClass, String storedField) throws Exception {
107 doIndexAndSearchTestWithRepeats(file, lineParserClass, 1, storedField); // no extra repetitions
108 doIndexAndSearchTestWithRepeats(file, lineParserClass, 2, storedField); // 1 extra repetition
109 doIndexAndSearchTestWithRepeats(file, lineParserClass, 4, storedField); // 3 extra repetitions
112 private void doIndexAndSearchTestWithRepeats(File file,
113 Class<? extends LineParser> lineParserClass, int numAdds, String storedField) throws Exception {
115 Properties props = new Properties();
117 // LineDocSource specific settings.
118 props.setProperty("docs.file", file.getAbsolutePath());
119 if (lineParserClass != null) {
120 props.setProperty("line.parser", lineParserClass.getName());
123 // Indexing configuration.
124 props.setProperty("analyzer", WhitespaceAnalyzer.class.getName());
125 props.setProperty("content.source", LineDocSource.class.getName());
126 props.setProperty("directory", "RAMDirectory");
127 props.setProperty("doc.stored", "true");
128 props.setProperty("doc.index.props", "true");
130 // Create PerfRunData
131 Config config = new Config(props);
132 PerfRunData runData = new PerfRunData(config);
134 TaskSequence tasks = new TaskSequence(runData, "testBzip2", null, false);
135 tasks.addTask(new CreateIndexTask(runData));
136 for (int i=0; i<numAdds; i++) {
137 tasks.addTask(new AddDocTask(runData));
139 tasks.addTask(new CloseIndexTask(runData));
142 IndexSearcher searcher = new IndexSearcher(runData.getDirectory(), true);
143 TopDocs td = searcher.search(new TermQuery(new Term("body", "body")), 10);
144 assertEquals(numAdds, td.totalHits);
145 assertNotNull(td.scoreDocs[0]);
147 if (storedField==null) {
148 storedField = DocMaker.BODY_FIELD; // added to all docs and satisfies field-name == value
150 assertEquals("Wrong field value", storedField, searcher.doc(0).get(storedField));
155 /* Tests LineDocSource with a bzip2 input stream. */
156 public void testBZip2() throws Exception {
157 File file = new File(getWorkDir(), "one-line.bz2");
158 createBZ2LineFile(file,true);
159 doIndexAndSearchTest(file, null, null);
162 public void testBZip2NoHeaderLine() throws Exception {
163 File file = new File(getWorkDir(), "one-line.bz2");
164 createBZ2LineFile(file,false);
165 doIndexAndSearchTest(file, null, null);
168 public void testRegularFile() throws Exception {
169 File file = new File(getWorkDir(), "one-line");
170 createRegularLineFile(file,true);
171 doIndexAndSearchTest(file, null, null);
174 public void testRegularFileSpecialHeader() throws Exception {
175 File file = new File(getWorkDir(), "one-line");
176 createRegularLineFile(file,true);
177 doIndexAndSearchTest(file, HeaderLineParser.class, null);
180 public void testRegularFileNoHeaderLine() throws Exception {
181 File file = new File(getWorkDir(), "one-line");
182 createRegularLineFile(file,false);
183 doIndexAndSearchTest(file, null, null);
186 public void testInvalidFormat() throws Exception {
187 String[] testCases = new String[] {
189 "title", // just title
190 "title" + WriteLineDocTask.SEP, // title + SEP
191 "title" + WriteLineDocTask.SEP + "body", // title + SEP + body
192 // note that title + SEP + body + SEP is a valid line, which results in an
196 for (int i = 0; i < testCases.length; i++) {
197 File file = new File(getWorkDir(), "one-line");
198 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
199 writer.write(testCases[i]);
203 doIndexAndSearchTest(file, null, null);
204 fail("Some exception should have been thrown for: [" + testCases[i] + "]");
205 } catch (Exception e) {
211 /** Doc Name is not part of the default header */
212 public void testWithDocsName() throws Exception {
213 File file = new File(getWorkDir(), "one-line");
214 createRegularLineFileWithMoreFields(file, DocMaker.NAME_FIELD);
215 doIndexAndSearchTest(file, null, DocMaker.NAME_FIELD);
218 /** Use fields names that are not defined in Docmaker and so will go to Properties */
219 public void testWithProperties() throws Exception {
220 File file = new File(getWorkDir(), "one-line");
221 String specialField = "mySpecialField";
222 createRegularLineFileWithMoreFields(file, specialField);
223 doIndexAndSearchTest(file, null, specialField);