1
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 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 public Iterator(IndexReader.Iterator iterator) throws IOException {
32 this.iterator = iterator;
33 this.extents = new ExtentArray();
34 this.done = false;
35 loadIndex();
36 }
37
38 public void loadIndex() throws IOException {
39 readHeader(iterator.getValueStream());
40 }
41
42 public void reset() throws IOException {
43 done = false;
44
45 document = 0;
46 extents.reset();
47 documentCount = 0;
48 termDocs = 0;
49
50 loadIndex();
51 }
52
53 public String getRecordString() {
54 StringBuilder builder = new StringBuilder();
55 builder.append(iterator.getKey());
56 builder.append(",");
57 builder.append(document);
58 for (int i = 0; i < extents.getPosition(); ++i) {
59 builder.append(",(");
60 builder.append(extents.getBuffer()[i].begin);
61 builder.append(",");
62 builder.append(extents.getBuffer()[i].end);
63 builder.append(")");
64 }
65 return builder.toString();
66 }
67
68 public boolean nextRecord() throws IOException {
69 nextDocument();
70 if (!isDone()) return true;
71 if (iterator.nextKey()) {
72 loadIndex();
73 return true;
74 } else {
75 return false;
76 }
77 }
78
79 private void readHeader(DataStream compressedStream) throws IOException {
80 stream = new VByteInput(compressedStream);
81
82 options = stream.readInt();
83 documentCount = stream.readInt();
84 termDocs = 0;
85 done = false;
86
87 nextDocument();
88 }
89
90 public void nextDocument() throws IOException {
91 extents.reset();
92
93 if (termDocs >= documentCount) {
94 done = true;
95 return;
96 }
97
98 termDocs++;
99 int deltaDocument = stream.readInt();
100 document += deltaDocument;
101
102 int extentCount = stream.readInt();
103 int begin = 0;
104
105 for (int i = 0; i < extentCount; i++) {
106 int deltaBegin = stream.readInt();
107 int extentLength = stream.readInt();
108 long value = stream.readLong();
109
110 begin = deltaBegin + begin;
111 int end = begin + extentLength;
112
113 extents.add(document, begin, end, value);
114 }
115 }
116
117 public int document() {
118 return document;
119 }
120
121 public int count() {
122 return extents.getPosition();
123 }
124
125 public ExtentArray extents() {
126 return extents;
127 }
128
129 public boolean isDone() {
130 return done;
131 }
132 }
133 IndexReader reader;
134
135 public ExtentIndexReader(IndexReader reader) throws FileNotFoundException, IOException {
136 this.reader = reader;
137 }
138
139 public Iterator getIterator() throws IOException {
140 return new Iterator(reader.getIterator());
141 }
142
143 public Iterator getExtents(String term) throws IOException {
144 IndexReader.Iterator iterator = reader.getIterator(term);
145
146 if (iterator != null) {
147 return new Iterator(iterator);
148 }
149 return null;
150 }
151
152 public CountIterator getCounts(String term) throws IOException {
153 IndexReader.Iterator iterator = reader.getIterator(term);
154
155 if (iterator != null) {
156 return new Iterator(iterator);
157 }
158 return null;
159 }
160
161 public void close() throws IOException {
162 reader.close();
163 }
164
165 public HashMap<String, NodeType> getNodeTypes() {
166 HashMap<String, NodeType> nodeTypes = new HashMap<String, NodeType>();
167 nodeTypes.put("extents", new NodeType(Iterator.class));
168 return nodeTypes;
169 }
170
171 public IndexIterator getIterator(Node node) throws IOException {
172 if (node.getOperator().equals("extents")) {
173 return getExtents(node.getDefaultParameter());
174 } else {
175 throw new UnsupportedOperationException("Node type " + node.getOperator() +
176 " isn't supported.");
177 }
178 }
179 }