View Javadoc

1   // BSD License (http://galagosearch.org)
2   
3   package org.galagosearch.tupleflow;
4   
5   import java.io.File;
6   import java.io.IOException;
7   import java.io.RandomAccessFile;
8   
9   /***
10   *
11   * @author trevor
12   */
13  public class FileOrderedReader<T> implements ReaderSource<T> {
14      RandomAccessFile dataStream;
15      ArrayInput stream;
16      TypeReader<T> orderedReader;
17      String filename;
18      Processor<T> processor;
19      Order<T> order;
20  
21      public FileOrderedReader(String filename, int bufferSize, boolean compressed) throws IOException {
22          // set up the input stream and get its length in bytes
23          dataStream = StreamCreator.inputStream(filename);
24          long fileLength = dataStream.length();
25  
26          // now, set up the stream, including a stopper that keeps us from
27          // reading into the XML region (which no longer exists, but BufferedFileDataStream also buffers for us)
28          if (compressed) {
29              stream = new ArrayInput(new VByteInput(new BufferedFileDataStream(dataStream, fileLength)));
30          } else {
31              stream = new ArrayInput(new BufferedFileDataStream(dataStream, fileLength));
32          }
33  
34          String className = stream.readString();
35          String[] orderSpec = stream.readStrings();
36  
37          try {
38              Class typeClass = Class.forName(className);
39              org.galagosearch.tupleflow.Type type = (org.galagosearch.tupleflow.Type) typeClass.
40                      getConstructor().newInstance();
41              order = type.getOrder(orderSpec);
42          } catch (Exception e) {
43              throw (IOException) new IOException(
44                      "Couldn't create an order object for type: " + className).initCause(e);
45          }
46  
47          this.filename = filename;
48          this.processor = null;
49          this.orderedReader = order.orderedReader(stream, bufferSize);
50      }
51  
52      public FileOrderedReader(String filename) throws IOException {
53          this(filename, 1024, true);
54      }
55  
56      /*** Creates a new instance of FileOrderedReader */
57      public FileOrderedReader(String filename, Order<T> order, int bufferSize, boolean compressed) throws IOException {
58          this(filename, bufferSize, compressed);
59  
60          if (order.getOrderedClass() != this.order.getOrderedClass()) {
61              throw (IOException) new IOException("This file, '" + filename + "', contains objects of type " +
62                                                  this.order.getOrderedClass() + "' even though objects of type " +
63                                                  order.getOrderedClass() + "' were expected.");
64          }
65      }
66  
67      public FileOrderedReader(String filename, Order<T> order, int bufferSize) throws IOException {
68          this(filename, order, bufferSize, true);
69      }
70  
71      public FileOrderedReader(String filename, Order<T> order) throws IOException {
72          this(filename, order, 1024, true);
73      }
74  
75      public void setProcessor(Step processor) throws IncompatibleProcessorException {
76          this.orderedReader.setProcessor(processor);
77      }
78  
79      public Class<T> getOutputClass() {
80          return order.getOrderedClass();
81      }
82  
83      public void run() throws IOException {
84          orderedReader.run();
85      }
86  
87      public Order<T> getOrder() {
88          return order;
89      }
90  
91      public TypeReader<T> getOrderedReader() {
92          return orderedReader;
93      }
94  
95      public T read() throws IOException {
96          T result = orderedReader.read();
97  
98          if (result == null) {
99              close();
100         }
101         return result;
102     }
103 
104     public void close() throws IOException {
105         dataStream.close();
106     }
107 }