pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / feeds / ReutersContentSource.java
1 package org.apache.lucene.benchmark.byTask.feeds;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.BufferedReader;
21 import java.io.File;
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;
30
31 import org.apache.lucene.benchmark.byTask.utils.Config;
32
33 /**
34  * A {@link ContentSource} reading from the Reuters collection.
35  * <p>
36  * Config properties:
37  * <ul>
38  * <li><b>work.dir</b> - path to the root of docs and indexes dirs (default
39  * <b>work</b>).
40  * <li><b>docs.dir</b> - path to the docs dir (default <b>reuters-out</b>).
41  * </ul>
42  */
43 public class ReutersContentSource extends ContentSource {
44
45   private static final class DateFormatInfo {
46     DateFormatInfo() {}
47     DateFormat df;
48     ParsePosition pos;
49   }
50
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;
56   
57   @Override
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);
65     }
66     inputFiles.clear();
67     collectFiles(dataDir, inputFiles);
68     if (inputFiles.size() == 0) {
69       throw new RuntimeException("No txt files in dataDir: "+dataDir.getAbsolutePath());
70     }
71   }
72
73   private synchronized DateFormatInfo getDateFormatInfo() {
74     DateFormatInfo dfi = dateFormat.get();
75     if (dfi == null) {
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);
81       dateFormat.set(dfi);
82     }
83     return dfi;
84   }
85   
86   private Date parseDate(String dateStr) {
87     DateFormatInfo dfi = getDateFormatInfo();
88     dfi.pos.setIndex(0);
89     dfi.pos.setErrorIndex(-1);
90     return dfi.df.parse(dateStr.trim(), dfi.pos);
91   }
92
93
94   @Override
95   public void close() throws IOException {
96     // TODO implement?
97   }
98   
99   @Override
100   public DocData getNextDocData(DocData docData) throws NoMoreDataException, IOException {
101     File f = null;
102     String name = null;
103     synchronized (this) {
104       if (nextFile >= inputFiles.size()) {
105         // exhausted files, start a new round, unless forever set to false.
106         if (!forever) {
107           throw new NoMoreDataException();
108         }
109         nextFile = 0;
110         iteration++;
111       }
112       f = inputFiles.get(nextFile++);
113       name = f.getCanonicalPath() + "_" + iteration;
114     }
115
116     BufferedReader reader = new BufferedReader(new FileReader(f));
117     try {
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);
124       String line = null;
125       while ((line = reader.readLine()) != null) {
126         bodyBuf.append(line).append(' ');
127       }
128       reader.close();
129       
130       addBytes(f.length());
131       
132       Date date = parseDate(dateStr.trim());
133       
134       docData.clear();
135       docData.setName(name);
136       docData.setBody(bodyBuf.toString());
137       docData.setTitle(title);
138       docData.setDate(date);
139       return docData;
140     } finally {
141       reader.close();
142     }
143   }
144
145   @Override
146   public synchronized void resetInputs() throws IOException {
147     super.resetInputs();
148     nextFile = 0;
149     iteration = 0;
150   }
151
152 }