Coverage Report - org.galagosearch.core.parse.DocumentIndexReader
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentIndexReader
58%
7/12
0%
0/2
0
DocumentIndexReader$Iterator
100%
22/22
100%
2/2
0
 
 1  
 // BSD License (http://www.galagosearch.org/license)
 2  
 
 3  
 package org.galagosearch.core.parse;
 4  
 
 5  
 import java.io.FileNotFoundException;
 6  
 import java.io.IOException;
 7  
 import java.util.HashMap;
 8  
 import org.galagosearch.core.index.IndexReader;
 9  
 import org.galagosearch.tupleflow.DataStream;
 10  
 import org.galagosearch.tupleflow.VByteInput;
 11  
 
 12  
 /**
 13  
  *
 14  
  * @author trevor
 15  
  */
 16  
 public class DocumentIndexReader {
 17  
     IndexReader reader;
 18  
 
 19  12
     public DocumentIndexReader(String fileName) throws FileNotFoundException, IOException {
 20  12
         reader = new IndexReader(fileName);
 21  12
     }
 22  
 
 23  4
     public DocumentIndexReader(IndexReader reader) {
 24  4
         this.reader = reader;
 25  4
     }
 26  
 
 27  
     public void close() throws IOException {
 28  0
         reader.close();
 29  0
     }
 30  
 
 31  
     public Iterator getIterator() throws IOException {
 32  12
         return new Iterator(reader.getIterator());
 33  
     }
 34  
 
 35  
     public Document getDocument(String key) throws IOException {
 36  0
         IndexReader.Iterator iterator = reader.getIterator(key);
 37  0
         if (iterator == null) return null;
 38  0
         return new Iterator(iterator).getDocument();
 39  
     }
 40  
 
 41  
     public class Iterator {
 42  
         IndexReader.Iterator iterator;
 43  
 
 44  12
         Iterator(IndexReader.Iterator iterator) throws IOException {
 45  12
             this.iterator = iterator;
 46  12
         }
 47  
 
 48  
         public void skipTo(byte[] key) throws IOException {
 49  12
             iterator.skipTo(key);
 50  12
         }
 51  
 
 52  
         public String getKey() {
 53  8
             return iterator.getKey();
 54  
         }
 55  
 
 56  
         public boolean isDone() {
 57  16
             return iterator.isDone();
 58  
         }
 59  
 
 60  
         public Document getDocument() throws IOException {
 61  4
             String key = iterator.getKey();
 62  4
             DataStream stream = iterator.getValueStream();
 63  4
             return decodeDocument(key, stream);
 64  
         }
 65  
 
 66  
         public boolean nextDocument() throws IOException {
 67  4
             return iterator.nextKey();
 68  
         }
 69  
 
 70  
         Document decodeDocument(String key, DataStream stream) throws IOException {
 71  4
             VByteInput input = new VByteInput(stream);
 72  4
             Document document = new Document();
 73  
 
 74  
             // The first string is the document text, followed by
 75  
             // key/value metadata pairs.
 76  4
             document.identifier = key;
 77  4
             document.text = input.readString();
 78  4
             document.metadata = new HashMap<String, String>();
 79  
 
 80  12
             while (!stream.isDone()) {
 81  8
                 String mapKey = input.readString();
 82  8
                 String mapValue = input.readString();
 83  
 
 84  8
                 document.metadata.put(mapKey, mapValue);
 85  8
             }
 86  
 
 87  4
             return document;
 88  
         }
 89  
     }
 90  
 }