pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test-framework / java / org / apache / lucene / util / LineFileDocs.java
1 package org.apache.lucene.util;
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.Closeable;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.BufferedReader;
25 import java.io.InputStreamReader;
26 import java.io.InputStream;
27 import java.util.concurrent.atomic.AtomicInteger;
28 import java.util.zip.GZIPInputStream;
29 import java.util.Random;
30
31 import org.apache.lucene.document.Document;
32 import org.apache.lucene.document.Field;
33
34 /** Minimal port of contrib/benchmark's LneDocSource +
35  * DocMaker, so tests can enum docs from a line file created
36  * by contrib/benchmark's WriteLineDoc task */
37 public class LineFileDocs implements Closeable {
38
39   private BufferedReader reader;
40   private final static int BUFFER_SIZE = 1 << 16;     // 64K
41   private final AtomicInteger id = new AtomicInteger();
42   private final String path;
43
44   /** If forever is true, we rewind the file at EOF (repeat
45    * the docs over and over) */
46   public LineFileDocs(Random random, String path) throws IOException {
47     this.path = path;
48     open(random);
49   }
50
51   public LineFileDocs(Random random) throws IOException {
52     this(random, LuceneTestCase.TEST_LINE_DOCS_FILE);
53   }
54
55   public synchronized void close() throws IOException {
56     if (reader != null) {
57       reader.close();
58       reader = null;
59     }
60   }
61
62   private synchronized void open(Random random) throws IOException {
63     InputStream is = getClass().getResourceAsStream(path);
64     if (is == null) {
65       // if its not in classpath, we load it as absolute filesystem path (e.g. Hudson's home dir)
66       is = new FileInputStream(path);
67     }
68     File file = new File(path);
69     long size;
70     if (file.exists()) {
71       size = file.length();
72     } else {
73       size = is.available();
74     }
75     if (path.endsWith(".gz")) {
76       is = new GZIPInputStream(is);
77       // guestimate:
78       size *= 2.8;
79     }
80
81     reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), BUFFER_SIZE);
82
83     // Override sizes for currently "known" line files:
84     if (path.equals("europarl.lines.txt.gz")) {
85       size = 15129506L;
86     } else if (path.equals("/home/hudson/lucene-data/enwiki.random.lines.txt.gz")) {
87       size = 3038178822L;
88     }
89
90     // Randomly seek to starting point:
91     if (random != null && size > 3) {
92       final long seekTo = (random.nextLong()&Long.MAX_VALUE) % (size/3);
93       if (LuceneTestCase.VERBOSE) {
94         System.out.println("TEST: LineFileDocs: seek to fp=" + seekTo + " on open");
95       }
96       reader.skip(seekTo);
97       reader.readLine();
98     }
99   }
100
101   public synchronized void reset(Random random) throws IOException {
102     close();
103     open(random);
104     id.set(0);
105   }
106
107   private final static char SEP = '\t';
108
109   private static final class DocState {
110     final Document doc;
111     final Field titleTokenized;
112     final Field title;
113     final Field body;
114     final Field id;
115     final Field date;
116
117     public DocState() {
118       doc = new Document();
119       
120       title = new Field("title", "", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
121       doc.add(title);
122
123       titleTokenized = new Field("titleTokenized", "", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
124       doc.add(titleTokenized);
125
126       body = new Field("body", "", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
127       doc.add(body);
128
129       id = new Field("docid", "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
130       doc.add(id);
131
132       date = new Field("date", "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
133       doc.add(date);
134     }
135   }
136
137   private final ThreadLocal<DocState> threadDocs = new ThreadLocal<DocState>();
138
139   /** Note: Document instance is re-used per-thread */
140   public Document nextDoc() throws IOException {
141     String line;
142     synchronized(this) {
143       line = reader.readLine();
144       if (line == null) {
145         // Always rewind at end:
146         if (LuceneTestCase.VERBOSE) {
147           System.out.println("TEST: LineFileDocs: now rewind file...");
148         }
149         close();
150         open(null);
151         line = reader.readLine();
152       }
153     }
154
155     DocState docState = threadDocs.get();
156     if (docState == null) {
157       docState = new DocState();
158       threadDocs.set(docState);
159     }
160
161     int spot = line.indexOf(SEP);
162     if (spot == -1) {
163       throw new RuntimeException("line: [" + line + "] is in an invalid format !");
164     }
165     int spot2 = line.indexOf(SEP, 1 + spot);
166     if (spot2 == -1) {
167       throw new RuntimeException("line: [" + line + "] is in an invalid format !");
168     }
169
170     docState.body.setValue(line.substring(1+spot2, line.length()));
171     final String title = line.substring(0, spot);
172     docState.title.setValue(title);
173     docState.titleTokenized.setValue(title);
174     docState.date.setValue(line.substring(1+spot, spot2));
175     docState.id.setValue(Integer.toString(id.getAndIncrement()));
176     return docState.doc;
177   }
178 }