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 org.apache.lucene.benchmark.byTask.utils.Config;
22 import java.io.BufferedReader;
24 import java.io.FileFilter;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.text.DateFormat;
28 import java.text.ParsePosition;
29 import java.text.SimpleDateFormat;
30 import java.util.Arrays;
31 import java.util.Date;
32 import java.util.Locale;
33 import java.util.Stack;
36 * A {@link ContentSource} using the Dir collection for its input. Supports
37 * the following configuration parameters (on top of {@link ContentSource}):
39 * <li><b>work.dir</b> - specifies the working directory. Required if "docs.dir"
40 * denotes a relative path (<b>default=work</b>).
41 * <li><b>docs.dir</b> - specifies the directory the Dir collection. Can be set
42 * to a relative path if "work.dir" is also specified (<b>default=dir-out</b>).
45 public class DirContentSource extends ContentSource {
47 private static final class DateFormatInfo {
48 public DateFormatInfo() {}
53 public static class Iterator implements java.util.Iterator<File> {
55 static class Comparator implements java.util.Comparator<File> {
56 public int compare(File _a, File _b) {
57 String a = _a.toString();
58 String b = _b.toString();
59 int diff = a.length() - b.length();
65 } else if (diff < 0) {
72 /* note it's reversed because we're going to push,
73 which reverses again */
74 return b.compareTo(a);
80 Stack<File> stack = new Stack<File>();
82 /* this seems silly ... there must be a better way ...
83 not that this is good, but can it matter? */
85 Comparator c = new Comparator();
87 public Iterator(File f) {
95 if (!(stack.peek()).isDirectory()) {
103 push(f.listFiles(new FileFilter() {
105 public boolean accept(File file) {
106 return file.isDirectory();
109 push(f.listFiles(new FileFilter() {
111 public boolean accept(File file) {
112 return file.getName().endsWith(".txt");
118 void push(File[] files) {
119 Arrays.sort(files, c);
120 for(int i = 0; i < files.length; i++) {
121 // System.err.println("push " + files[i]);
122 stack.push(files[i]);
126 public int getCount(){
130 public boolean hasNext() {
131 return stack.size() > 0;
137 File object = stack.pop();
138 // System.err.println("pop " + object);
143 public void remove() {
144 throw new RuntimeException("cannot");
149 private ThreadLocal<DateFormatInfo> dateFormat = new ThreadLocal<DateFormatInfo>();
150 private File dataDir = null;
151 private int iteration = 0;
152 private Iterator inputFiles = null;
154 // get/initiate a thread-local simple date format (must do so
155 // because SimpleDateFormat is not thread-safe).
156 private DateFormatInfo getDateFormatInfo() {
157 DateFormatInfo dfi = dateFormat.get();
159 dfi = new DateFormatInfo();
160 dfi.pos = new ParsePosition(0);
161 // date format: 30-MAR-1987 14:22:36.87
162 dfi.df = new SimpleDateFormat("dd-MMM-yyyy kk:mm:ss.SSS", Locale.US);
163 dfi.df.setLenient(true);
169 private Date parseDate(String dateStr) {
170 DateFormatInfo dfi = getDateFormatInfo();
172 dfi.pos.setErrorIndex(-1);
173 return dfi.df.parse(dateStr.trim(), dfi.pos);
177 public void close() throws IOException {
182 public DocData getNextDocData(DocData docData) throws NoMoreDataException, IOException {
185 synchronized (this) {
186 if (!inputFiles.hasNext()) {
187 // exhausted files, start a new round, unless forever set to false.
189 throw new NoMoreDataException();
191 inputFiles = new Iterator(dataDir);
194 f = inputFiles.next();
195 // System.err.println(f);
196 name = f.getCanonicalPath()+"_"+iteration;
199 BufferedReader reader = new BufferedReader(new FileReader(f));
201 //First line is the date, 3rd is the title, rest is body
202 String dateStr = reader.readLine();
203 reader.readLine();//skip an empty line
204 String title = reader.readLine();
205 reader.readLine();//skip an empty line
206 StringBuilder bodyBuf = new StringBuilder(1024);
207 while ((line = reader.readLine()) != null) {
208 bodyBuf.append(line).append(' ');
211 addBytes(f.length());
213 Date date = parseDate(dateStr);
216 docData.setName(name);
217 docData.setBody(bodyBuf.toString());
218 docData.setTitle(title);
219 docData.setDate(date);
224 public synchronized void resetInputs() throws IOException {
226 inputFiles = new Iterator(dataDir);
231 public void setConfig(Config config) {
232 super.setConfig(config);
234 File workDir = new File(config.get("work.dir", "work"));
235 String d = config.get("docs.dir", "dir-out");
236 dataDir = new File(d);
237 if (!dataDir.isAbsolute()) {
238 dataDir = new File(workDir, d);
241 inputFiles = new Iterator(dataDir);
243 if (inputFiles == null) {
244 throw new RuntimeException("No txt files in dataDir: " + dataDir.getAbsolutePath());