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.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
26 import java.text.DateFormat;
27 import java.text.ParsePosition;
28 import java.text.SimpleDateFormat;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.Locale;
33 import org.apache.lucene.benchmark.byTask.feeds.TrecDocParser.ParsePathType;
34 import org.apache.lucene.benchmark.byTask.utils.Config;
35 import org.apache.lucene.benchmark.byTask.utils.StreamUtils;
36 import org.apache.lucene.benchmark.byTask.utils.StringBuilderReader;
37 import org.apache.lucene.util.ThreadInterruptedException;
40 * Implements a {@link ContentSource} over the TREC collection.
42 * Supports the following configuration parameters (on top of
43 * {@link ContentSource}):
45 * <li><b>work.dir</b> - specifies the working directory. Required if "docs.dir"
46 * denotes a relative path (<b>default=work</b>).
47 * <li><b>docs.dir</b> - specifies the directory where the TREC files reside.
48 * Can be set to a relative path if "work.dir" is also specified
49 * (<b>default=trec</b>).
50 * <li><b>trec.doc.parser</b> - specifies the {@link TrecDocParser} class to use for
51 * parsing the TREC documents content (<b>default=TrecGov2Parser</b>).
52 * <li><b>html.parser</b> - specifies the {@link HTMLParser} class to use for
53 * parsing the HTML parts of the TREC documents content (<b>default=DemoHTMLParser</b>).
54 * <li><b>content.source.encoding</b> - if not specified, ISO-8859-1 is used.
55 * <li><b>content.source.excludeIteration</b> - if true, do not append iteration number to docname
58 public class TrecContentSource extends ContentSource {
60 private static final class DateFormatInfo {
66 public static final String DOCNO = "<DOCNO>";
67 public static final String TERMINATING_DOCNO = "</DOCNO>";
68 public static final String DOC = "<DOC>";
69 public static final String TERMINATING_DOC = "</DOC>";
71 /** separator between lines in the byffer */
72 public static final String NEW_LINE = System.getProperty("line.separator");
74 private static final String DATE_FORMATS [] = {
75 "EEE, dd MMM yyyy kk:mm:ss z", // Tue, 09 Dec 2003 22:39:08 GMT
76 "EEE MMM dd kk:mm:ss yyyy z", // Tue Dec 09 16:45:08 2003 EST
77 "EEE, dd-MMM-':'y kk:mm:ss z", // Tue, 09 Dec 2003 22:39:08 GMT
78 "EEE, dd-MMM-yyy kk:mm:ss z", // Tue, 09 Dec 2003 22:39:08 GMT
79 "EEE MMM dd kk:mm:ss yyyy", // Tue Dec 09 16:45:08 2003
80 "dd MMM yyyy", // 1 March 1994
81 "MMM dd, yyyy", // February 3, 1994
83 "hhmm z.z.z. MMM dd, yyyy", // 0901 u.t.c. April 28, 1994
86 private ThreadLocal<DateFormatInfo> dateFormats = new ThreadLocal<DateFormatInfo>();
87 private ThreadLocal<StringBuilderReader> trecDocReader = new ThreadLocal<StringBuilderReader>();
88 private ThreadLocal<StringBuilder> trecDocBuffer = new ThreadLocal<StringBuilder>();
89 private File dataDir = null;
90 private ArrayList<File> inputFiles = new ArrayList<File>();
91 private int nextFile = 0;
92 private int rawDocSize = 0;
94 // Use to synchronize threads on reading from the TREC documents.
95 private Object lock = new Object();
98 BufferedReader reader;
100 HTMLParser htmlParser;
102 private boolean excludeDocnameIteration;
103 private TrecDocParser trecDocParser = new TrecGov2Parser(); // default
104 ParsePathType currPathType; // not private for tests
106 private DateFormatInfo getDateFormatInfo() {
107 DateFormatInfo dfi = dateFormats.get();
109 dfi = new DateFormatInfo();
110 dfi.dfs = new SimpleDateFormat[DATE_FORMATS.length];
111 for (int i = 0; i < dfi.dfs.length; i++) {
112 dfi.dfs[i] = new SimpleDateFormat(DATE_FORMATS[i], Locale.US);
113 dfi.dfs[i].setLenient(true);
115 dfi.pos = new ParsePosition(0);
116 dateFormats.set(dfi);
121 private StringBuilder getDocBuffer() {
122 StringBuilder sb = trecDocBuffer.get();
124 sb = new StringBuilder();
125 trecDocBuffer.set(sb);
130 Reader getTrecDocReader(StringBuilder docBuffer) {
131 StringBuilderReader r = trecDocReader.get();
133 r = new StringBuilderReader(docBuffer);
134 trecDocReader.set(r);
141 HTMLParser getHtmlParser() {
146 * Read until a line starting with the specified <code>lineStart</code>.
147 * @param buf buffer for collecting the data if so specified/
148 * @param lineStart line start to look for, must not be null.
149 * @param collectMatchLine whether to collect the matching line into <code>buffer</code>.
150 * @param collectAll whether to collect all lines into <code>buffer</code>.
151 * @throws IOException
152 * @throws NoMoreDataException
154 private void read(StringBuilder buf, String lineStart,
155 boolean collectMatchLine, boolean collectAll) throws IOException, NoMoreDataException {
158 String line = reader.readLine();
165 rawDocSize += line.length();
167 if (lineStart!=null && line.startsWith(lineStart)) {
168 if (collectMatchLine) {
169 buf.append(sep).append(line);
176 buf.append(sep).append(line);
182 void openNextFile() throws NoMoreDataException, IOException {
186 if (nextFile >= inputFiles.size()) {
187 // exhausted files, start a new round, unless forever set to false.
189 throw new NoMoreDataException();
194 File f = inputFiles.get(nextFile++);
196 System.out.println("opening: " + f + " length: " + f.length());
199 InputStream inputStream = StreamUtils.inputStream(f); // support either gzip, bzip2, or regular text file, by extension
200 reader = new BufferedReader(new InputStreamReader(inputStream, encoding), StreamUtils.BUFFER_SIZE);
201 currPathType = TrecDocParser.pathType(f);
203 } catch (Exception e) {
205 System.out.println("Skipping 'bad' file " + f.getAbsolutePath()+" due to "+e.getMessage());
208 throw new NoMoreDataException();
213 public Date parseDate(String dateStr) {
214 dateStr = dateStr.trim();
215 DateFormatInfo dfi = getDateFormatInfo();
216 for (int i = 0; i < dfi.dfs.length; i++) {
217 DateFormat df = dfi.dfs[i];
219 dfi.pos.setErrorIndex(-1);
220 Date d = df.parse(dateStr, dfi.pos);
226 // do not fail test just because a date could not be parsed
228 System.out.println("failed to parse date (assigning 'now') for: " + dateStr);
234 public void close() throws IOException {
235 if (reader == null) {
241 } catch (IOException e) {
243 System.out.println("failed to close reader !");
244 e.printStackTrace(System.out);
251 public DocData getNextDocData(DocData docData) throws NoMoreDataException, IOException {
253 StringBuilder docBuf = getDocBuffer();
254 ParsePathType parsedPathType;
256 // protect reading from the TREC files by multiple threads. The rest of the
257 // method, i.e., parsing the content and returning the DocData can run unprotected.
258 synchronized (lock) {
259 if (reader == null) {
263 // 1. skip until doc start - required for all TREC formats
265 read(docBuf, DOC, false, false);
267 // save parsedFile for passing trecDataParser after the sync block, in
268 // case another thread will open another file in between.
269 parsedPathType = currPathType;
271 // 2. name - required for all TREC formats
273 read(docBuf, DOCNO, true, false);
274 name = docBuf.substring(DOCNO.length(), docBuf.indexOf(TERMINATING_DOCNO,
275 DOCNO.length())).trim();
277 if (!excludeDocnameIteration) {
278 name = name + "_" + iteration;
281 // 3. read all until end of doc
283 read(docBuf, TERMINATING_DOC, false, true);
286 // count char length of text to be parsed (may be larger than the resulted plain doc body text).
287 addBytes(docBuf.length());
289 // This code segment relies on HtmlParser being thread safe. When we get
290 // here, everything else is already private to that thread, so we're safe.
292 docData = trecDocParser.parse(docData, name, this, docBuf, parsedPathType);
294 } catch (InterruptedException ie) {
295 throw new ThreadInterruptedException(ie);
302 public void resetInputs() throws IOException {
303 synchronized (lock) {
312 public void setConfig(Config config) {
313 super.setConfig(config);
315 File workDir = new File(config.get("work.dir", "work"));
316 String d = config.get("docs.dir", "trec");
317 dataDir = new File(d);
318 if (!dataDir.isAbsolute()) {
319 dataDir = new File(workDir, d);
322 collectFiles(dataDir, inputFiles);
323 if (inputFiles.size() == 0) {
324 throw new IllegalArgumentException("No files in dataDir: " + dataDir);
328 String trecDocParserClassName = config.get("trec.doc.parser", "org.apache.lucene.benchmark.byTask.feeds.TrecGov2Parser");
329 trecDocParser = Class.forName(trecDocParserClassName).asSubclass(TrecDocParser.class).newInstance();
330 } catch (Exception e) {
331 // Should not get here. Throw runtime exception.
332 throw new RuntimeException(e);
336 String htmlParserClassName = config.get("html.parser",
337 "org.apache.lucene.benchmark.byTask.feeds.DemoHTMLParser");
338 htmlParser = Class.forName(htmlParserClassName).asSubclass(HTMLParser.class).newInstance();
339 } catch (Exception e) {
340 // Should not get here. Throw runtime exception.
341 throw new RuntimeException(e);
344 if (encoding == null) {
345 encoding = "ISO-8859-1";
347 // iteration exclusion in doc name
348 excludeDocnameIteration = config.get("content.source.excludeIteration", false);