1 package org.apache.lucene.index; 2 3 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Enumeration ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.SortedMap ; 29 import java.util.TreeMap ; 30 31 import org.apache.lucene.document.Document; 32 import org.apache.lucene.document.Field; 33 34 51 public class ParallelReader extends IndexReader { 52 private List readers = new ArrayList (); 53 private SortedMap fieldToReader = new TreeMap (); 54 private List storedFieldReaders = new ArrayList (); 55 56 private int maxDoc; 57 private int numDocs; 58 private boolean hasDeletions; 59 60 61 public ParallelReader() throws IOException { super(null); } 62 63 64 public void add(IndexReader reader) throws IOException { 65 add(reader, false); 66 } 67 68 77 public void add(IndexReader reader, boolean ignoreStoredFields) 78 throws IOException { 79 80 if (readers.size() == 0) { 81 this.maxDoc = reader.maxDoc(); 82 this.numDocs = reader.numDocs(); 83 this.hasDeletions = reader.hasDeletions(); 84 } 85 86 if (reader.maxDoc() != maxDoc) throw new IllegalArgumentException 88 ("All readers must have same maxDoc: "+maxDoc+"!="+reader.maxDoc()); 89 if (reader.numDocs() != numDocs) 90 throw new IllegalArgumentException 91 ("All readers must have same numDocs: "+numDocs+"!="+reader.numDocs()); 92 93 Iterator i = reader.getFieldNames(IndexReader.FieldOption.ALL).iterator(); 94 while (i.hasNext()) { String field = (String )i.next(); 96 if (fieldToReader.get(field) == null) 97 fieldToReader.put(field, reader); 98 } 99 100 if (!ignoreStoredFields) 101 storedFieldReaders.add(reader); readers.add(reader); 103 } 104 105 public int numDocs() { return numDocs; } 106 107 public int maxDoc() { return maxDoc; } 108 109 public boolean hasDeletions() { return hasDeletions; } 110 111 public boolean isDeleted(int n) { 113 if (readers.size() > 0) 114 return ((IndexReader)readers.get(0)).isDeleted(n); 115 return false; 116 } 117 118 protected void doDelete(int n) throws IOException { 120 for (int i = 0; i < readers.size(); i++) { 121 ((IndexReader)readers.get(i)).doDelete(n); 122 } 123 hasDeletions = true; 124 } 125 126 protected void doUndeleteAll() throws IOException { 128 for (int i = 0; i < readers.size(); i++) { 129 ((IndexReader)readers.get(i)).doUndeleteAll(); 130 } 131 hasDeletions = false; 132 } 133 134 public Document document(int n) throws IOException { 136 Document result = new Document(); 137 for (int i = 0; i < storedFieldReaders.size(); i++) { 138 IndexReader reader = (IndexReader)storedFieldReaders.get(i); 139 Enumeration fields = reader.document(n).fields(); 140 while (fields.hasMoreElements()) { 141 result.add((Field)fields.nextElement()); 142 } 143 } 144 return result; 145 } 146 147 public TermFreqVector[] getTermFreqVectors(int n) throws IOException { 149 ArrayList results = new ArrayList (); 150 Iterator i = fieldToReader.entrySet().iterator(); 151 while (i.hasNext()) { 152 Map.Entry e = (Map.Entry )i.next(); 153 IndexReader reader = (IndexReader)e.getKey(); 154 String field = (String )e.getValue(); 155 TermFreqVector vector = reader.getTermFreqVector(n, field); 156 if (vector != null) 157 results.add(vector); 158 } 159 return (TermFreqVector[]) 160 results.toArray(new TermFreqVector[results.size()]); 161 } 162 163 public TermFreqVector getTermFreqVector(int n, String field) 164 throws IOException { 165 return ((IndexReader)fieldToReader.get(field)).getTermFreqVector(n, field); 166 } 167 168 public boolean hasNorms(String field) throws IOException { 169 return ((IndexReader)fieldToReader.get(field)).hasNorms(field); 170 } 171 172 public byte[] norms(String field) throws IOException { 173 return ((IndexReader)fieldToReader.get(field)).norms(field); 174 } 175 176 public void norms(String field, byte[] result, int offset) 177 throws IOException { 178 ((IndexReader)fieldToReader.get(field)).norms(field, result, offset); 179 } 180 181 protected void doSetNorm(int n, String field, byte value) 182 throws IOException { 183 ((IndexReader)fieldToReader.get(field)).doSetNorm(n, field, value); 184 } 185 186 public TermEnum terms() throws IOException { 187 return new ParallelTermEnum(); 188 } 189 190 public TermEnum terms(Term term) throws IOException { 191 return new ParallelTermEnum(term); 192 } 193 194 public int docFreq(Term term) throws IOException { 195 return ((IndexReader)fieldToReader.get(term.field())).docFreq(term); 196 } 197 198 public TermDocs termDocs(Term term) throws IOException { 199 return new ParallelTermDocs(term); 200 } 201 202 public TermDocs termDocs() throws IOException { 203 return new ParallelTermDocs(); 204 } 205 206 public TermPositions termPositions(Term term) throws IOException { 207 return new ParallelTermPositions(term); 208 } 209 210 public TermPositions termPositions() throws IOException { 211 return new ParallelTermPositions(); 212 } 213 214 protected void doCommit() throws IOException { 215 for (int i = 0; i < readers.size(); i++) 216 ((IndexReader)readers.get(i)).commit(); 217 } 218 219 protected synchronized void doClose() throws IOException { 220 for (int i = 0; i < readers.size(); i++) 221 ((IndexReader)readers.get(i)).close(); 222 } 223 224 public Collection getFieldNames() throws IOException { 225 return fieldToReader.keySet(); 226 } 227 228 public Collection getFieldNames(boolean indexed) throws IOException { 229 Set fieldSet = new HashSet (); 230 for (int i = 0; i < readers.size(); i++) { 231 IndexReader reader = ((IndexReader)readers.get(i)); 232 Collection names = reader.getFieldNames(indexed); 233 fieldSet.addAll(names); 234 } 235 return fieldSet; 236 } 237 238 public Collection getIndexedFieldNames (Field.TermVector tvSpec){ 239 Set fieldSet = new HashSet (); 240 for (int i = 0; i < readers.size(); i++) { 241 IndexReader reader = ((IndexReader)readers.get(i)); 242 Collection names = reader.getIndexedFieldNames(tvSpec); 243 fieldSet.addAll(names); 244 } 245 return fieldSet; 246 } 247 248 public Collection getFieldNames (IndexReader.FieldOption fieldNames) { 249 Set fieldSet = new HashSet (); 250 for (int i = 0; i < readers.size(); i++) { 251 IndexReader reader = ((IndexReader)readers.get(i)); 252 Collection names = reader.getFieldNames(fieldNames); 253 fieldSet.addAll(names); 254 } 255 return fieldSet; 256 } 257 258 private class ParallelTermEnum extends TermEnum { 259 private String field; 260 private TermEnum termEnum; 261 262 public ParallelTermEnum() throws IOException { 263 field = (String )fieldToReader.firstKey(); 264 if (field != null) 265 termEnum = ((IndexReader)fieldToReader.get(field)).terms(); 266 } 267 268 public ParallelTermEnum(Term term) throws IOException { 269 field = term.field(); 270 termEnum = ((IndexReader)fieldToReader.get(field)).terms(term); 271 } 272 273 public boolean next() throws IOException { 274 if (field == null) 275 return false; 276 277 boolean next = termEnum.next(); 278 279 if (next && termEnum.term().field() == field) 281 return true; 283 termEnum.close(); 285 field = (String )fieldToReader.tailMap(field).firstKey(); 287 if (field != null) { 288 termEnum = ((IndexReader)fieldToReader.get(field)).terms(); 289 return true; 290 } 291 292 return false; 294 } 295 296 public Term term() { return termEnum.term(); } 297 public int docFreq() { return termEnum.docFreq(); } 298 public void close() throws IOException { termEnum.close(); } 299 300 } 301 302 private class ParallelTermDocs implements TermDocs { 304 protected TermDocs termDocs; 305 306 public ParallelTermDocs() {} 307 public ParallelTermDocs(Term term) throws IOException { seek(term); } 308 309 public int doc() { return termDocs.doc(); } 310 public int freq() { return termDocs.freq(); } 311 312 public void seek(Term term) throws IOException { 313 termDocs = ((IndexReader)fieldToReader.get(term.field())).termDocs(term); 314 } 315 316 public void seek(TermEnum termEnum) throws IOException { 317 seek(termEnum.term()); 318 } 319 320 public boolean next() throws IOException { return termDocs.next(); } 321 322 public int read(final int[] docs, final int[] freqs) throws IOException { 323 return termDocs.read(docs, freqs); 324 } 325 326 public boolean skipTo(int target) throws IOException { 327 return termDocs.skipTo(target); 328 } 329 330 public void close() throws IOException { termDocs.close(); } 331 332 } 333 334 private class ParallelTermPositions 335 extends ParallelTermDocs implements TermPositions { 336 337 public ParallelTermPositions() {} 338 public ParallelTermPositions(Term term) throws IOException { seek(term); } 339 340 public void seek(Term term) throws IOException { 341 termDocs = ((IndexReader)fieldToReader.get(term.field())) 342 .termPositions(term); 343 } 344 345 public int nextPosition() throws IOException { 346 return ((TermPositions)termDocs).nextPosition(); 347 } 348 349 } 350 351 } 352 353 | Popular Tags |