Coverage Report - org.galagosearch.core.index.PositionIndexWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
PositionIndexWriter
89%
42/47
67%
8/12
0
PositionIndexWriter$PositionsList
100%
51/51
75%
3/4
0
 
 1  
 // BSD License (http://www.galagosearch.org/license)
 2  
 package org.galagosearch.core.index;
 3  
 
 4  
 import java.io.DataOutputStream;
 5  
 import java.io.FileNotFoundException;
 6  
 import java.io.IOException;
 7  
 import java.io.OutputStream;
 8  
 import java.util.TreeMap;
 9  
 import org.galagosearch.core.types.NumberWordPosition;
 10  
 import org.galagosearch.tupleflow.InputClass;
 11  
 import org.galagosearch.tupleflow.TupleFlowParameters;
 12  
 import org.galagosearch.tupleflow.Utility;
 13  
 import org.galagosearch.tupleflow.execution.ErrorHandler;
 14  
 import org.galagosearch.tupleflow.execution.Verification;
 15  
 
 16  
 /**
 17  
  *
 18  
  * @author trevor
 19  
  */
 20  
 @InputClass(className = "org.galagosearch.core.types.NumberWordPosition", order = {"+word", "+document", "+position"})
 21  4
 public class PositionIndexWriter implements
 22  
         NumberWordPosition.WordDocumentPositionOrder.ShreddedProcessor {
 23  20
     int blockSize = 32768;
 24  
     byte[] lastWord;
 25  20
     long lastPosition = 0;
 26  20
     long lastDocument = 0;
 27  
     int skipMinimumBinLength;
 28  
     TreeMap<Integer, Integer> skipLengths;
 29  
 
 30  
     public class PositionsList implements IndexElement {
 31  40
         public PositionsList() {
 32  40
             documents = new BackedCompressedByteBuffer();
 33  40
             counts = new BackedCompressedByteBuffer();
 34  40
             positions = new BackedCompressedByteBuffer();
 35  40
             header = new BackedCompressedByteBuffer();
 36  40
         }
 37  
 
 38  
         public void close() throws IOException {
 39  40
             int options = 0;
 40  
 
 41  40
             if (documents.length() > 0) {
 42  40
                 counts.add(positionCount);
 43  
             }
 44  40
             header.add(options);
 45  
 
 46  40
             header.add(documentCount);
 47  40
             header.add(totalPositionCount);
 48  
 
 49  40
             header.add(documents.length());
 50  40
             header.add(counts.length());
 51  40
             header.add(positions.length());
 52  40
         }
 53  
 
 54  
         public long dataLength() {
 55  200
             long listLength = 0;
 56  
 
 57  200
             listLength += header.length();
 58  200
             listLength += counts.length();
 59  200
             listLength += positions.length();
 60  200
             listLength += documents.length();
 61  
 
 62  200
             return listLength;
 63  
         }
 64  
 
 65  
         public void write(final OutputStream output) throws IOException {
 66  40
             header.write(output);
 67  40
             header.clear();
 68  
 
 69  40
             documents.write(output);
 70  40
             documents.clear();
 71  
 
 72  40
             counts.write(output);
 73  40
             counts.clear();
 74  
 
 75  40
             positions.write(output);
 76  40
             positions.clear();
 77  40
         }
 78  
 
 79  
         public byte[] key() {
 80  240
             return word;
 81  
         }
 82  
 
 83  
         public void setWord(byte[] word) {
 84  40
             this.word = word;
 85  40
             this.lastDocument = 0;
 86  40
             this.lastPosition = 0;
 87  40
             this.totalPositionCount = 0;
 88  40
             this.positionCount = 0;
 89  40
         }
 90  
 
 91  
         public void addDocument(long documentID) throws IOException {
 92  
             // add the last document's counts
 93  116
             if (documents.length() > 0) {
 94  76
                 counts.add(positionCount);
 95  
             }
 96  116
             documents.add(documentID - lastDocument);
 97  116
             lastDocument = documentID;
 98  
 
 99  116
             lastPosition = 0;
 100  116
             positionCount = 0;
 101  116
             documentCount++;
 102  116
         }
 103  
 
 104  
         public void addPosition(int position) throws IOException {
 105  176
             positionCount++;
 106  176
             totalPositionCount++;
 107  176
             positions.add(position - lastPosition);
 108  176
             lastPosition = position;
 109  176
         }
 110  
         private long lastDocument;
 111  
         private int lastPosition;
 112  
         private int positionCount;
 113  
         private int documentCount;
 114  
         private int totalPositionCount;
 115  
         public byte[] word;
 116  
         public BackedCompressedByteBuffer header;
 117  
         public BackedCompressedByteBuffer documents;
 118  
         public BackedCompressedByteBuffer counts;
 119  
         public BackedCompressedByteBuffer positions;
 120  
     }
 121  20
     long maximumDocumentCount = 0;
 122  20
     long maximumDocumentNumber = 0;
 123  
     PositionsList invertedList;
 124  
     DataOutputStream output;
 125  
     long filePosition;
 126  
     IndexWriter writer;
 127  20
     long documentCount = 0;
 128  20
     long collectionLength = 0;
 129  
 
 130  
     /**
 131  
      * Creates a new instance of BinnedListWriter
 132  
      */
 133  20
     public PositionIndexWriter(TupleFlowParameters parameters) throws FileNotFoundException, IOException {
 134  20
         writer = new IndexWriter(parameters);
 135  20
         writer.getManifest().add("writerClass", getClass().getName());
 136  20
         writer.getManifest().add("readerClass", PositionIndexReader.class.getName());
 137  20
     }
 138  
 
 139  
     public void processWord(byte[] wordBytes) throws IOException {
 140  40
         if (invertedList != null) {
 141  20
             invertedList.close();
 142  20
             writer.add(invertedList);
 143  20
             invertedList = null;
 144  
         }
 145  
 
 146  40
         resetDocumentCount();
 147  
 
 148  40
         invertedList = new PositionsList();
 149  40
         invertedList.setWord(wordBytes);
 150  
 
 151  40
         assert lastWord == null || 0 != Utility.compare(lastWord, wordBytes) : "Duplicate word";
 152  40
         lastWord = wordBytes;
 153  40
     }
 154  
 
 155  
     public void processDocument(int document) throws IOException {
 156  116
         invertedList.addDocument(document);
 157  116
         documentCount++;
 158  116
         maximumDocumentNumber = Math.max(document, maximumDocumentNumber);
 159  116
         lastDocument = document;
 160  116
     }
 161  
 
 162  
     public void processPosition(int position) throws IOException {
 163  176
         invertedList.addPosition(position);
 164  176
     }
 165  
 
 166  
     public void processTuple() {
 167  
         // does nothing
 168  0
     }
 169  
 
 170  
     private void resetDocumentCount() {
 171  40
         maximumDocumentCount = Math.max(documentCount, maximumDocumentCount);
 172  40
         documentCount = 0;
 173  40
     }
 174  
 
 175  
     public void close() throws IOException {
 176  20
         if (invertedList != null) {
 177  20
             invertedList.close();
 178  20
             writer.add(invertedList);
 179  
         }
 180  
 
 181  20
         writer.close();
 182  20
     }
 183  
 
 184  
     public long documentCount() {
 185  0
         return maximumDocumentNumber;
 186  
     }
 187  
 
 188  
     public long maximumDocumentCount() {
 189  0
         return maximumDocumentCount;
 190  
     }
 191  
 
 192  
     public static void verify(TupleFlowParameters parameters, ErrorHandler handler) {
 193  28
         if (!parameters.getXML().containsKey("filename")) {
 194  0
             handler.addError("PositionsListWriter requires an 'filename' parameter.");
 195  0
             return;
 196  
         }
 197  
 
 198  28
         String index = parameters.getXML().get("filename");
 199  28
         Verification.requireWriteableFile(index, handler);
 200  28
     }
 201  
 }
 202