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.FileReader;
23 import java.io.IOException;
24 import java.text.DateFormat;
25 import java.text.ParsePosition;
26 import java.text.SimpleDateFormat;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.Locale;
31 import org.apache.lucene.benchmark.byTask.utils.Config;
34 * A {@link ContentSource} reading from the Reuters collection.
38 * <li><b>work.dir</b> - path to the root of docs and indexes dirs (default
40 * <li><b>docs.dir</b> - path to the docs dir (default <b>reuters-out</b>).
43 public class ReutersContentSource extends ContentSource {
45 private static final class DateFormatInfo {
50 private ThreadLocal<DateFormatInfo> dateFormat = new ThreadLocal<DateFormatInfo>();
51 private File dataDir = null;
52 private ArrayList<File> inputFiles = new ArrayList<File>();
53 private int nextFile = 0;
54 private int iteration = 0;
57 public void setConfig(Config config) {
58 super.setConfig(config);
59 File workDir = new File(config.get("work.dir", "work"));
60 String d = config.get("docs.dir", "reuters-out");
61 dataDir = new File(d);
62 if (!dataDir.isAbsolute()) {
63 dataDir = new File(workDir, d);
66 collectFiles(dataDir, inputFiles);
67 if (inputFiles.size() == 0) {
68 throw new RuntimeException("No txt files in dataDir: "+dataDir.getAbsolutePath());
72 private synchronized DateFormatInfo getDateFormatInfo() {
73 DateFormatInfo dfi = dateFormat.get();
75 dfi = new DateFormatInfo();
76 // date format: 30-MAR-1987 14:22:36.87
77 dfi.df = new SimpleDateFormat("dd-MMM-yyyy kk:mm:ss.SSS",Locale.US);
78 dfi.df.setLenient(true);
79 dfi.pos = new ParsePosition(0);
85 private Date parseDate(String dateStr) {
86 DateFormatInfo dfi = getDateFormatInfo();
88 dfi.pos.setErrorIndex(-1);
89 return dfi.df.parse(dateStr.trim(), dfi.pos);
94 public void close() throws IOException {
99 public DocData getNextDocData(DocData docData) throws NoMoreDataException, IOException {
102 synchronized (this) {
103 if (nextFile >= inputFiles.size()) {
104 // exhausted files, start a new round, unless forever set to false.
106 throw new NoMoreDataException();
111 f = inputFiles.get(nextFile++);
112 name = f.getCanonicalPath() + "_" + iteration;
115 BufferedReader reader = new BufferedReader(new FileReader(f));
117 // First line is the date, 3rd is the title, rest is body
118 String dateStr = reader.readLine();
119 reader.readLine();// skip an empty line
120 String title = reader.readLine();
121 reader.readLine();// skip an empty line
122 StringBuilder bodyBuf = new StringBuilder(1024);
124 while ((line = reader.readLine()) != null) {
125 bodyBuf.append(line).append(' ');
129 addBytes(f.length());
131 Date date = parseDate(dateStr.trim());
134 docData.setName(name);
135 docData.setBody(bodyBuf.toString());
136 docData.setTitle(title);
137 docData.setDate(date);
145 public synchronized void resetInputs() throws IOException {