Coverage Report - org.galagosearch.core.index.ExtentIndexReader
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtentIndexReader
57%
12/21
17%
1/6
0
ExtentIndexReader$Iterator
58%
35/60
40%
4/10
0
 
 1  
 // BSD License (http://www.galagosearch.org/license)
 2  
 package org.galagosearch.core.index;
 3  
 
 4  
 import java.io.DataInput;
 5  
 import java.io.FileNotFoundException;
 6  
 import java.io.IOException;
 7  
 import java.util.HashMap;
 8  
 import org.galagosearch.core.retrieval.*;
 9  
 import org.galagosearch.core.retrieval.query.Node;
 10  
 import org.galagosearch.core.retrieval.query.NodeType;
 11  
 import org.galagosearch.core.retrieval.structured.*;
 12  
 import org.galagosearch.core.util.ExtentArray;
 13  
 import org.galagosearch.tupleflow.DataStream;
 14  
 import org.galagosearch.tupleflow.VByteInput;
 15  
 
 16  
 /**
 17  
  *
 18  
  * @author trevor
 19  
  */
 20  12
 public class ExtentIndexReader implements StructuredIndexPartReader {
 21  
     public class Iterator extends ExtentIterator implements IndexIterator {
 22  
         IndexReader.Iterator iterator;
 23  
         DataInput stream;
 24  
         int documentCount;
 25  
         int options;
 26  
         boolean done;
 27  
         int document;
 28  
         ExtentArray extents;
 29  
         int termDocs;
 30  
 
 31  12
         public Iterator(IndexReader.Iterator iterator) throws IOException {
 32  12
             this.iterator = iterator;
 33  12
             this.extents = new ExtentArray();
 34  12
             this.done = false;
 35  12
             loadIndex();
 36  12
         }
 37  
 
 38  
         public void loadIndex() throws IOException {
 39  12
             readHeader(iterator.getValueStream());
 40  12
         }
 41  
 
 42  
         public void reset() throws IOException {
 43  0
             done = false;
 44  
 
 45  0
             document = 0;
 46  0
             extents.reset();
 47  0
             documentCount = 0;
 48  0
             termDocs = 0;
 49  
 
 50  0
             loadIndex();
 51  0
         }
 52  
         
 53  
         public String getRecordString() {
 54  0
             StringBuilder builder = new StringBuilder();
 55  0
             builder.append(iterator.getKey());
 56  0
             builder.append(",");
 57  0
             builder.append(document);
 58  0
             for (int i = 0; i < extents.getPosition(); ++i) {
 59  0
                 builder.append(",(");
 60  0
                 builder.append(extents.getBuffer()[i].begin);
 61  0
                 builder.append(",");
 62  0
                 builder.append(extents.getBuffer()[i].end);
 63  0
                 builder.append(")");
 64  
            }
 65  0
            return builder.toString();
 66  
         }
 67  
 
 68  
         public boolean nextRecord() throws IOException {
 69  0
             nextDocument();
 70  0
             if (!isDone()) return true;
 71  0
             if (iterator.nextKey()) {
 72  0
                 loadIndex();
 73  0
                 return true;
 74  
             } else {
 75  0
                 return false;
 76  
             }
 77  
         }
 78  
 
 79  
         private void readHeader(DataStream compressedStream) throws IOException {
 80  12
             stream = new VByteInput(compressedStream);
 81  
 
 82  12
             options = stream.readInt();
 83  12
             documentCount = stream.readInt();
 84  12
             termDocs = 0;
 85  12
             done = false;
 86  
 
 87  12
             nextDocument();
 88  12
         }
 89  
 
 90  
         public void nextDocument() throws IOException {
 91  32
             extents.reset();
 92  
 
 93  32
             if (termDocs >= documentCount) {
 94  12
                 done = true;
 95  12
                 return;
 96  
             }
 97  
 
 98  20
             termDocs++;
 99  20
             int deltaDocument = stream.readInt();
 100  20
             document += deltaDocument;
 101  
 
 102  20
             int extentCount = stream.readInt();
 103  20
             int begin = 0;
 104  
 
 105  48
             for (int i = 0; i < extentCount; i++) {
 106  28
                 int deltaBegin = stream.readInt();
 107  28
                 int extentLength = stream.readInt();
 108  28
                 long value = stream.readLong();
 109  
 
 110  28
                 begin = deltaBegin + begin;
 111  28
                 int end = begin + extentLength;
 112  
 
 113  28
                 extents.add(document, begin, end, value);
 114  
             }
 115  20
         }
 116  
 
 117  
         public int document() {
 118  20
             return document;
 119  
         }
 120  
 
 121  
         public int count() {
 122  0
             return extents.getPosition();
 123  
         }
 124  
 
 125  
         public ExtentArray extents() {
 126  12
             return extents;
 127  
         }
 128  
 
 129  
         public boolean isDone() {
 130  48
             return done;
 131  
         }
 132  
     }
 133  
     IndexReader reader;
 134  
 
 135  24
     public ExtentIndexReader(IndexReader reader) throws FileNotFoundException, IOException {
 136  24
         this.reader = reader;
 137  24
     }
 138  
 
 139  
     public Iterator getIterator() throws IOException {
 140  0
         return new Iterator(reader.getIterator());
 141  
     }
 142  
 
 143  
     public Iterator getExtents(String term) throws IOException {
 144  12
         IndexReader.Iterator iterator = reader.getIterator(term);
 145  
 
 146  12
         if (iterator != null) {
 147  12
             return new Iterator(iterator);
 148  
         }
 149  0
         return null;
 150  
     }
 151  
 
 152  
     public CountIterator getCounts(String term) throws IOException {
 153  0
         IndexReader.Iterator iterator = reader.getIterator(term);
 154  
 
 155  0
         if (iterator != null) {
 156  0
             return new Iterator(iterator);
 157  
         }
 158  0
         return null;
 159  
     }
 160  
 
 161  
     public void close() throws IOException {
 162  12
         reader.close();
 163  12
     }
 164  
 
 165  
     public HashMap<String, NodeType> getNodeTypes() {
 166  12
         HashMap<String, NodeType> nodeTypes = new HashMap<String, NodeType>();
 167  12
         nodeTypes.put("extents", new NodeType(Iterator.class));
 168  12
         return nodeTypes;
 169  
     }
 170  
 
 171  
     public IndexIterator getIterator(Node node) throws IOException {
 172  0
         if (node.getOperator().equals("extents")) {
 173  0
             return getExtents(node.getDefaultParameter());
 174  
         } else {
 175  0
             throw new UnsupportedOperationException("Node type " + node.getOperator() +
 176  
                                                     " isn't supported.");
 177  
         }
 178  
     }
 179  
 }