PageRenderTime 32ms CodeModel.GetById 26ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/branch-3x/src/org/getopt/luke/IntervalLimitedCollector.java

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