PageRenderTime 21ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/org/getopt/luke/IntervalLimitedCollector.java

http://luke.googlecode.com/
Java | 106 lines | 81 code | 15 blank | 10 comment | 6 complexity | ce2a265d55c26fcae951e4a7b1b441fb MD5 | raw file
  1package org.getopt.luke;
  2
  3import java.io.IOException;
  4
  5import org.apache.lucene.index.AtomicReaderContext;
  6import org.apache.lucene.index.IndexReader;
  7import org.apache.lucene.search.Scorer;
  8import org.apache.lucene.search.TimeLimitingCollector;
  9import org.apache.lucene.search.TopDocs;
 10import org.apache.lucene.search.TopScoreDocCollector;
 11import org.apache.lucene.search.TimeLimitingCollector.TimeExceededException;
 12
 13public class IntervalLimitedCollector extends LimitedHitCollector {
 14  private long maxTime;
 15  private long lastDoc = 0;
 16  private TopScoreDocCollector tdc;
 17  private TopDocs topDocs = null;
 18  private TimeLimitingCollector thc;
 19  
 20  
 21  public IntervalLimitedCollector(int maxTime, boolean outOfOrder, boolean shouldScore) {
 22    this.maxTime = maxTime;
 23    this.outOfOrder = outOfOrder;
 24    this.shouldScore = shouldScore;
 25    tdc = TopScoreDocCollector.create(1000, outOfOrder);
 26    thc = new TimeLimitingCollector(tdc, TimeLimitingCollector.getGlobalCounter(), maxTime);
 27  }
 28
 29  /* (non-Javadoc)
 30   * @see org.getopt.luke.LimitedHitCollector#limitSize()
 31   */
 32  @Override
 33  public long limitSize() {
 34    return maxTime;
 35  }
 36
 37  /* (non-Javadoc)
 38   * @see org.getopt.luke.LimitedHitCollector#limitType()
 39   */
 40  @Override
 41  public int limitType() {
 42    return TYPE_TIME;
 43  }
 44
 45  @Override
 46  public int getDocId(int pos) {
 47    if (topDocs == null) {
 48      topDocs = tdc.topDocs();
 49    }
 50    return topDocs.scoreDocs[pos].doc;
 51  }
 52
 53  /* (non-Javadoc)
 54   * @see org.getopt.luke.AccessibleHitCollector#getScore(int)
 55   */
 56  @Override
 57  public float getScore(int pos) {
 58    if (topDocs == null) {
 59      topDocs = tdc.topDocs();
 60    }
 61    return topDocs.scoreDocs[pos].score;
 62  }
 63
 64  @Override
 65  public int getTotalHits() {
 66    return tdc.getTotalHits();
 67  }
 68
 69  @Override
 70  public void collect(int docNum) throws IOException {
 71    try {
 72      thc.collect(docNum);
 73    } catch (TimeExceededException tee) {
 74      // re-throw
 75      throw new LimitedException(TYPE_TIME, maxTime, tee.getTimeElapsed(), tee.getLastDocCollected());
 76    }
 77  }
 78
 79  @Override
 80  public boolean acceptsDocsOutOfOrder() {
 81    return outOfOrder;
 82  }
 83
 84  @Override
 85  public void setNextReader(AtomicReaderContext context) throws IOException {
 86    this.docBase = context.docBase;
 87    thc.setNextReader(context);
 88  }
 89
 90  @Override
 91  public void setScorer(Scorer scorer) throws IOException {
 92    this.scorer = scorer;
 93    if (shouldScore) {
 94      thc.setScorer(scorer);
 95    } else {
 96      thc.setScorer(NoScoringScorer.INSTANCE);
 97    }
 98  }
 99
100  @Override
101  public void reset() {
102    lastDoc = 0;
103    tdc = TopScoreDocCollector.create(1000, outOfOrder);
104    thc = new TimeLimitingCollector(tdc, TimeLimitingCollector.getGlobalCounter(), maxTime);
105  }
106}