add --shared
[pylucene.git] / lucene-java-3.4.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     DateFormat df;
47     ParsePosition pos;
48   }
49
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;
55   
56   @Override
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);
64     }
65     inputFiles.clear();
66     collectFiles(dataDir, inputFiles);
67     if (inputFiles.size() == 0) {
68       throw new RuntimeException("No txt files in dataDir: "+dataDir.getAbsolutePath());
69     }
70   }
71
72   private synchronized DateFormatInfo getDateFormatInfo() {
73     DateFormatInfo dfi = dateFormat.get();
74     if (dfi == null) {
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);
80       dateFormat.set(dfi);
81     }
82     return dfi;
83   }
84   
85   private Date parseDate(String dateStr) {
86     DateFormatInfo dfi = getDateFormatInfo();
87     dfi.pos.setIndex(0);
88     dfi.pos.setErrorIndex(-1);
89     return dfi.df.parse(dateStr.trim(), dfi.pos);
90   }
91
92
93   @Override
94   public void close() throws IOException {
95     // TODO implement?
96   }
97   
98   @Override
99   public DocData getNextDocData(DocData docData) throws NoMoreDataException, IOException {
100     File f = null;
101     String name = null;
102     synchronized (this) {
103       if (nextFile >= inputFiles.size()) {
104         // exhausted files, start a new round, unless forever set to false.
105         if (!forever) {
106           throw new NoMoreDataException();
107         }
108         nextFile = 0;
109         iteration++;
110       }
111       f = inputFiles.get(nextFile++);
112       name = f.getCanonicalPath() + "_" + iteration;
113     }
114
115     BufferedReader reader = new BufferedReader(new FileReader(f));
116     try {
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);
123       String line = null;
124       while ((line = reader.readLine()) != null) {
125         bodyBuf.append(line).append(' ');
126       }
127       reader.close();
128       
129       addBytes(f.length());
130       
131       Date date = parseDate(dateStr.trim());
132       
133       docData.clear();
134       docData.setName(name);
135       docData.setBody(bodyBuf.toString());
136       docData.setTitle(title);
137       docData.setDate(date);
138       return docData;
139     } finally {
140       reader.close();
141     }
142   }
143
144   @Override
145   public synchronized void resetInputs() throws IOException {
146     super.resetInputs();
147     nextFile = 0;
148     iteration = 0;
149   }
150
151 }