1 package org.apache.lucene.search; 2 3 18 19 import java.io.IOException ; 20 21 import org.apache.lucene.index.Term; 22 import org.apache.lucene.util.PriorityQueue; 23 24 29 public class ParallelMultiSearcher extends MultiSearcher { 30 31 private Searchable[] searchables; 32 private int[] starts; 33 34 35 public ParallelMultiSearcher(Searchable[] searchables) throws IOException { 36 super(searchables); 37 this.searchables=searchables; 38 this.starts=getStarts(); 39 } 40 41 44 public int docFreq(Term term) throws IOException { 45 return super.docFreq(term); 46 } 47 48 53 public TopDocs search(Weight weight, Filter filter, int nDocs) 54 throws IOException { 55 HitQueue hq = new HitQueue(nDocs); 56 int totalHits = 0; 57 MultiSearcherThread[] msta = 58 new MultiSearcherThread[searchables.length]; 59 for (int i = 0; i < searchables.length; i++) { msta[i] = 62 new MultiSearcherThread( 63 searchables[i], 64 weight, 65 filter, 66 nDocs, 67 hq, 68 i, 69 starts, 70 "MultiSearcher thread #" + (i + 1)); 71 msta[i].start(); 72 } 73 74 for (int i = 0; i < searchables.length; i++) { 75 try { 76 msta[i].join(); 77 } catch (InterruptedException ie) { 78 ; } 80 IOException ioe = msta[i].getIOException(); 81 if (ioe == null) { 82 totalHits += msta[i].hits(); 83 } else { 84 throw ioe; 86 } 87 } 88 89 ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()]; 90 for (int i = hq.size() - 1; i >= 0; i--) scoreDocs[i] = (ScoreDoc) hq.pop(); 92 93 float maxScore = (totalHits==0) ? Float.NEGATIVE_INFINITY : scoreDocs[0].score; 94 95 return new TopDocs(totalHits, scoreDocs, maxScore); 96 } 97 98 103 public TopFieldDocs search(Weight weight, Filter filter, int nDocs, Sort sort) 104 throws IOException { 105 FieldDocSortedHitQueue hq = new FieldDocSortedHitQueue (null, nDocs); 107 int totalHits = 0; 108 MultiSearcherThread[] msta = new MultiSearcherThread[searchables.length]; 109 for (int i = 0; i < searchables.length; i++) { msta[i] = 112 new MultiSearcherThread( 113 searchables[i], 114 weight, 115 filter, 116 nDocs, 117 hq, 118 sort, 119 i, 120 starts, 121 "MultiSearcher thread #" + (i + 1)); 122 msta[i].start(); 123 } 124 125 float maxScore=Float.NEGATIVE_INFINITY; 126 127 for (int i = 0; i < searchables.length; i++) { 128 try { 129 msta[i].join(); 130 } catch (InterruptedException ie) { 131 ; } 133 IOException ioe = msta[i].getIOException(); 134 if (ioe == null) { 135 totalHits += msta[i].hits(); 136 maxScore=Math.max(maxScore, msta[i].getMaxScore()); 137 } else { 138 throw ioe; 140 } 141 } 142 143 ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()]; 144 for (int i = hq.size() - 1; i >= 0; i--) scoreDocs[i] = (ScoreDoc) hq.pop(); 146 147 return new TopFieldDocs(totalHits, scoreDocs, hq.getFields(), maxScore); 148 } 149 150 166 public void search(Weight weight, Filter filter, final HitCollector results) 167 throws IOException { 168 for (int i = 0; i < searchables.length; i++) { 169 170 final int start = starts[i]; 171 172 searchables[i].search(weight, filter, new HitCollector() { 173 public void collect(int doc, float score) { 174 results.collect(doc + start, score); 175 } 176 }); 177 178 } 179 } 180 181 185 public Query rewrite(Query original) throws IOException { 186 return super.rewrite(original); 187 } 188 189 } 190 191 194 class MultiSearcherThread extends Thread { 195 196 private Searchable searchable; 197 private Weight weight; 198 private Filter filter; 199 private int nDocs; 200 private TopDocs docs; 201 private int i; 202 private PriorityQueue hq; 203 private int[] starts; 204 private IOException ioe; 205 private Sort sort; 206 207 public MultiSearcherThread( 208 Searchable searchable, 209 Weight weight, 210 Filter filter, 211 int nDocs, 212 HitQueue hq, 213 int i, 214 int[] starts, 215 String name) { 216 super(name); 217 this.searchable = searchable; 218 this.weight = weight; 219 this.filter = filter; 220 this.nDocs = nDocs; 221 this.hq = hq; 222 this.i = i; 223 this.starts = starts; 224 } 225 226 public MultiSearcherThread( 227 Searchable searchable, 228 Weight weight, 229 Filter filter, 230 int nDocs, 231 FieldDocSortedHitQueue hq, 232 Sort sort, 233 int i, 234 int[] starts, 235 String name) { 236 super(name); 237 this.searchable = searchable; 238 this.weight = weight; 239 this.filter = filter; 240 this.nDocs = nDocs; 241 this.hq = hq; 242 this.i = i; 243 this.starts = starts; 244 this.sort = sort; 245 } 246 247 public void run() { 248 try { 249 docs = (sort == null) ? searchable.search (weight, filter, nDocs) 250 : searchable.search (weight, filter, nDocs, sort); 251 } 252 catch (IOException ioe) { 254 this.ioe = ioe; 255 } 256 if (ioe == null) { 257 if (sort != null) { 261 ((FieldDocSortedHitQueue)hq).setFields (((TopFieldDocs)docs).fields); 262 } 263 ScoreDoc[] scoreDocs = docs.scoreDocs; 264 for (int j = 0; 265 j < scoreDocs.length; 266 j++) { ScoreDoc scoreDoc = scoreDocs[j]; 268 scoreDoc.doc += starts[i]; synchronized (hq) { 271 if (!hq.insert(scoreDoc)) 272 break; 273 } } 275 } 276 } 277 278 public int hits() { 279 return docs.totalHits; 280 } 281 282 public float getMaxScore() { 283 return docs.getMaxScore(); 284 } 285 286 public IOException getIOException() { 287 return ioe; 288 } 289 290 } 291 | Popular Tags |