| 1 | |
|
| 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 | |
|
| 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 | 0 | public FileOrderedReader(String filename, int bufferSize, boolean compressed) throws IOException { |
| 22 | |
|
| 23 | 0 | dataStream = StreamCreator.inputStream(filename); |
| 24 | 0 | long fileLength = dataStream.length(); |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | 0 | if (compressed) { |
| 29 | 0 | stream = new ArrayInput(new VByteInput(new BufferedFileDataStream(dataStream, fileLength))); |
| 30 | |
} else { |
| 31 | 0 | stream = new ArrayInput(new BufferedFileDataStream(dataStream, fileLength)); |
| 32 | |
} |
| 33 | |
|
| 34 | 0 | String className = stream.readString(); |
| 35 | 0 | String[] orderSpec = stream.readStrings(); |
| 36 | |
|
| 37 | |
try { |
| 38 | 0 | Class typeClass = Class.forName(className); |
| 39 | 0 | org.galagosearch.tupleflow.Type type = (org.galagosearch.tupleflow.Type) typeClass. |
| 40 | |
getConstructor().newInstance(); |
| 41 | 0 | order = type.getOrder(orderSpec); |
| 42 | 0 | } catch (Exception e) { |
| 43 | 0 | throw (IOException) new IOException( |
| 44 | |
"Couldn't create an order object for type: " + className).initCause(e); |
| 45 | 0 | } |
| 46 | |
|
| 47 | 0 | this.filename = filename; |
| 48 | 0 | this.processor = null; |
| 49 | 0 | this.orderedReader = order.orderedReader(stream, bufferSize); |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
public FileOrderedReader(String filename) throws IOException { |
| 53 | 0 | this(filename, 1024, true); |
| 54 | 0 | } |
| 55 | |
|
| 56 | |
|
| 57 | |
public FileOrderedReader(String filename, Order<T> order, int bufferSize, boolean compressed) throws IOException { |
| 58 | 0 | this(filename, bufferSize, compressed); |
| 59 | |
|
| 60 | 0 | if (order.getOrderedClass() != this.order.getOrderedClass()) { |
| 61 | 0 | 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 | 0 | } |
| 66 | |
|
| 67 | |
public FileOrderedReader(String filename, Order<T> order, int bufferSize) throws IOException { |
| 68 | 0 | this(filename, order, bufferSize, true); |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
public FileOrderedReader(String filename, Order<T> order) throws IOException { |
| 72 | 0 | this(filename, order, 1024, true); |
| 73 | 0 | } |
| 74 | |
|
| 75 | |
public void setProcessor(Step processor) throws IncompatibleProcessorException { |
| 76 | 0 | this.orderedReader.setProcessor(processor); |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
public Class<T> getOutputClass() { |
| 80 | 0 | return order.getOrderedClass(); |
| 81 | |
} |
| 82 | |
|
| 83 | |
public void run() throws IOException { |
| 84 | 0 | orderedReader.run(); |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
public Order<T> getOrder() { |
| 88 | 0 | return order; |
| 89 | |
} |
| 90 | |
|
| 91 | |
public TypeReader<T> getOrderedReader() { |
| 92 | 0 | return orderedReader; |
| 93 | |
} |
| 94 | |
|
| 95 | |
public T read() throws IOException { |
| 96 | 0 | T result = orderedReader.read(); |
| 97 | |
|
| 98 | 0 | if (result == null) { |
| 99 | 0 | close(); |
| 100 | |
} |
| 101 | 0 | return result; |
| 102 | |
} |
| 103 | |
|
| 104 | |
public void close() throws IOException { |
| 105 | 0 | dataStream.close(); |
| 106 | 0 | } |
| 107 | |
} |