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 {
51 private ThreadLocal<DateFormatInfo> dateFormat = new ThreadLocal<DateFormatInfo>();
52 private File dataDir = null;
53 private ArrayList<File> inputFiles = new ArrayList<File>();
54 private int nextFile = 0;
55 private int iteration = 0;
58 public void setConfig(Config config) {
59 super.setConfig(config);
60 File workDir = new File(config.get("work.dir", "work"));
61 String d = config.get("docs.dir", "reuters-out");
62 dataDir = new File(d);
63 if (!dataDir.isAbsolute()) {
64 dataDir = new File(workDir, d);
67 collectFiles(dataDir, inputFiles);
68 if (inputFiles.size() == 0) {
69 throw new RuntimeException("No txt files in dataDir: "+dataDir.getAbsolutePath());
73 private synchronized DateFormatInfo getDateFormatInfo() {
74 DateFormatInfo dfi = dateFormat.get();
76 dfi = new DateFormatInfo();
77 // date format: 30-MAR-1987 14:22:36.87
78 dfi.df = new SimpleDateFormat("dd-MMM-yyyy kk:mm:ss.SSS",Locale.US);
79 dfi.df.setLenient(true);
80 dfi.pos = new ParsePosition(0);
86 private Date parseDate(String dateStr) {
87 DateFormatInfo dfi = getDateFormatInfo();
89 dfi.pos.setErrorIndex(-1);
90 return dfi.df.parse(dateStr.trim(), dfi.pos);
95 public void close() throws IOException {
100 public DocData getNextDocData(DocData docData) throws NoMoreDataException, IOException {
103 synchronized (this) {
104 if (nextFile >= inputFiles.size()) {
105 // exhausted files, start a new round, unless forever set to false.
107 throw new NoMoreDataException();
112 f = inputFiles.get(nextFile++);
113 name = f.getCanonicalPath() + "_" + iteration;
116 BufferedReader reader = new BufferedReader(new FileReader(f));
118 // First line is the date, 3rd is the title, rest is body
119 String dateStr = reader.readLine();
120 reader.readLine();// skip an empty line
121 String title = reader.readLine();
122 reader.readLine();// skip an empty line
123 StringBuilder bodyBuf = new StringBuilder(1024);
125 while ((line = reader.readLine()) != null) {
126 bodyBuf.append(line).append(' ');
130 addBytes(f.length());
132 Date date = parseDate(dateStr.trim());
135 docData.setName(name);
136 docData.setBody(bodyBuf.toString());
137 docData.setTitle(title);
138 docData.setDate(date);
146 public synchronized void resetInputs() throws IOException {