| 1 | |
|
| 2 | |
|
| 3 | |
package org.galagosearch.core.index; |
| 4 | |
|
| 5 | |
import java.io.BufferedOutputStream; |
| 6 | |
import java.io.DataOutputStream; |
| 7 | |
import java.io.FileNotFoundException; |
| 8 | |
import java.io.FileOutputStream; |
| 9 | |
import java.io.IOException; |
| 10 | |
import org.galagosearch.core.types.NumberedDocumentData; |
| 11 | |
import org.galagosearch.tupleflow.Counter; |
| 12 | |
import org.galagosearch.tupleflow.InputClass; |
| 13 | |
import org.galagosearch.tupleflow.Processor; |
| 14 | |
import org.galagosearch.tupleflow.TupleFlowParameters; |
| 15 | |
import org.galagosearch.tupleflow.execution.ErrorHandler; |
| 16 | |
import org.galagosearch.tupleflow.execution.Verification; |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
@InputClass(className = "org.galagosearch.core.types.NumberedDocumentData", order = {"+number"}) |
| 26 | 4 | public class DocumentLengthsWriter implements Processor<NumberedDocumentData> { |
| 27 | |
DataOutputStream output; |
| 28 | 12 | int document = 0; |
| 29 | 12 | Counter documentsWritten = null; |
| 30 | |
|
| 31 | |
|
| 32 | 12 | public DocumentLengthsWriter(TupleFlowParameters parameters) throws FileNotFoundException { |
| 33 | 12 | String filename = parameters.getXML().get("filename"); |
| 34 | 12 | output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename))); |
| 35 | 12 | documentsWritten = parameters.getCounter("Documents Written"); |
| 36 | 12 | } |
| 37 | |
|
| 38 | |
public void close() throws IOException { |
| 39 | 12 | output.close(); |
| 40 | 12 | } |
| 41 | |
|
| 42 | |
public void process(NumberedDocumentData object) throws IOException { |
| 43 | 240 | assert document <= object.number : "d: " + document + " o.d:" + object.number; |
| 44 | |
|
| 45 | 240 | while (document < object.number) { |
| 46 | 0 | output.writeInt(0); |
| 47 | 0 | document++; |
| 48 | |
} |
| 49 | |
|
| 50 | 240 | output.writeInt(object.textLength); |
| 51 | 240 | document++; |
| 52 | 240 | if (documentsWritten != null) documentsWritten.increment(); |
| 53 | 240 | } |
| 54 | |
|
| 55 | |
public static void verify(TupleFlowParameters parameters, ErrorHandler handler) { |
| 56 | 20 | if (!parameters.getXML().containsKey("filename")) { |
| 57 | 0 | handler.addError("DocumentLengthsWriter requires an 'filename' parameter."); |
| 58 | 0 | return; |
| 59 | |
} |
| 60 | |
|
| 61 | 20 | String filename = parameters.getXML().get("filename"); |
| 62 | 20 | Verification.requireWriteableFile(filename, handler); |
| 63 | 20 | } |
| 64 | |
} |