View Javadoc

1   // BSD License (http://www.galagosearch.org/license)
2   package org.galagosearch.core.index;
3   
4   import java.io.FileNotFoundException;
5   import java.io.IOException;
6   import java.io.OutputStream;
7   import org.galagosearch.core.types.NumberWordProbability;
8   import org.galagosearch.tupleflow.TupleFlowParameters;
9   
10  /***
11   *
12   * @author trevor
13   */
14  public class SparseFloatListWriter implements 
15          NumberWordProbability.NumberWordOrder.ShreddedProcessor {
16      IndexWriter writer;
17      DoubleInvertedList list;
18  
19      public class DoubleInvertedList implements IndexElement {
20          BackedCompressedByteBuffer data = new BackedCompressedByteBuffer();
21          CompressedByteBuffer header = new CompressedByteBuffer();
22          int lastDocument;
23          int documentCount;
24          byte[] word;
25  
26          public DoubleInvertedList(byte[] word) {
27              this.word = word;
28              this.lastDocument = 0;
29              this.documentCount = 0;
30          }
31  
32          public void write(final OutputStream stream) throws IOException {
33              header.write(stream);
34              data.write(stream);
35          }
36  
37          public void addDocument(int document) throws IOException {
38              data.add(document - lastDocument);
39              documentCount++;
40              lastDocument = document;
41          }
42  
43          public void addProbability(double probability) throws IOException {
44              data.addFloat((float) probability);
45          }
46  
47          public byte[] key() {
48              return word;
49          }
50  
51          public long dataLength() {
52              return data.length() + header.length();
53          }
54  
55          public void close() {
56              header.add(documentCount);
57          }
58      }
59  
60      /*** Creates a new instance of DoubleListWriter */
61      public SparseFloatListWriter(TupleFlowParameters parameters) throws FileNotFoundException, IOException {
62          writer = new IndexWriter(parameters);
63          writer.getManifest().add("readerClass", SparseFloatListReader.class.getName());
64          writer.getManifest().add("writerClass", getClass().getName());
65      }
66  
67      public void processWord(byte[] word) throws IOException {
68          if (list != null) {
69              list.close();
70              writer.add(list);
71          }
72  
73          list = new DoubleInvertedList(word);
74      }
75  
76      public void processNumber(int number) throws IOException {
77          list.addDocument(number);
78      }
79  
80      public void processTuple(double probability) throws IOException {
81          list.addProbability(probability);
82      }
83  
84      public void close() throws IOException {
85          if (list != null) {
86              list.close();
87              writer.add(list);
88          }
89  
90          writer.close();
91      }
92  }