MtasSpanUniquePositionSpans.java.html 13.7 KB
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasSpanUniquePositionSpans.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> &gt; <a href="index.source.html" class="el_package">mtas.search.spans.util</a> &gt; <span class="el_source">MtasSpanUniquePositionSpans.java</span></div><h1>MtasSpanUniquePositionSpans.java</h1><pre class="source lang-java linenums">package mtas.search.spans.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.search.TwoPhaseIterator;
import org.apache.lucene.search.spans.SpanCollector;
import org.apache.lucene.search.spans.Spans;

/**
 * The Class MtasSpanUniquePositionSpans.
 */
public class MtasSpanUniquePositionSpans extends MtasSpans {

  /** The query. */
  private MtasSpanUniquePositionQuery query;

  /** The spans. */
  private Spans spans;

  /** The queue spans. */
  private List&lt;Match&gt; queueSpans;

  /** The queue matches. */
  private List&lt;Match&gt; queueMatches;

  /** The current match. */
  private Match currentMatch;

  /** The last start position. */
  private int lastStartPosition; // startPosition of last retrieved span

  /** The last span. */
  private boolean lastSpan; // last span for this document added to queue

  /** The no more positions. */
  private boolean noMorePositions;

  /**
   * Instantiates a new mtas span unique position spans.
   *
   * @param query the query
   * @param spans the spans
   */
  public MtasSpanUniquePositionSpans(MtasSpanUniquePositionQuery query,
      Spans spans) {
<span class="fc" id="L48">    super();</span>
<span class="fc" id="L49">    this.query = query;</span>
<span class="fc" id="L50">    this.spans = spans;</span>
<span class="fc" id="L51">    queueSpans = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L52">    queueMatches = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L53">    resetQueue();</span>
<span class="fc" id="L54">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#nextStartPosition()
   */
  @Override
  public int nextStartPosition() throws IOException {
<span class="fc bfc" id="L63" title="All 2 branches covered.">    if (findMatches()) {</span>
<span class="fc" id="L64">      currentMatch = queueMatches.get(0);</span>
<span class="fc" id="L65">      queueMatches.remove(0);</span>
<span class="fc" id="L66">      noMorePositions = false;</span>
<span class="fc" id="L67">      return currentMatch.startPosition();</span>
    } else {
<span class="fc" id="L69">      currentMatch = null;</span>
<span class="fc" id="L70">      noMorePositions = true;</span>
<span class="fc" id="L71">      return NO_MORE_POSITIONS;</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#startPosition()
   */
  @Override
  public int startPosition() {
<span class="pc bpc" id="L82" title="1 of 2 branches missed.">    if (currentMatch == null) {</span>
<span class="fc bfc" id="L83" title="All 2 branches covered.">      if (noMorePositions) {</span>
<span class="fc" id="L84">        return NO_MORE_POSITIONS;</span>
      } else {
<span class="fc" id="L86">        return -1;</span>
      }
    } else {
<span class="nc" id="L89">      return currentMatch.startPosition();</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#endPosition()
   */
  @Override
  public int endPosition() {
<span class="fc bfc" id="L100" title="All 2 branches covered.">    if (currentMatch == null) {</span>
<span class="fc bfc" id="L101" title="All 2 branches covered.">      if (noMorePositions) {</span>
<span class="fc" id="L102">        return NO_MORE_POSITIONS;</span>
      } else {
<span class="fc" id="L104">        return -1;</span>
      }
    } else {
<span class="fc" id="L107">      return currentMatch.endPosition();</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#width()
   */
  @Override
  public int width() {
    // return (currentMatch.endPosition() - currentMatch.startPosition());
<span class="fc" id="L119">    return 1;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.search.spans.Spans#collect(org.apache.lucene.search.spans
   * .SpanCollector)
   */
  @Override
  public void collect(SpanCollector collector) throws IOException {
<span class="nc" id="L131">    spans.collect(collector);</span>
<span class="nc" id="L132">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#docID()
   */
  @Override
  public int docID() {
<span class="fc" id="L141">    return spans.docID();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#nextDoc()
   */
  @Override
  public int nextDoc() throws IOException {
<span class="fc" id="L151">    resetQueue();</span>
<span class="fc" id="L152">    noMorePositions = false;</span>
<span class="fc bfc" id="L153" title="All 2 branches covered.">    return (spans.nextDoc() == NO_MORE_DOCS) ? NO_MORE_DOCS : toMatchDoc();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#advance(int)
   */
  @Override
  public int advance(int target) throws IOException {
<span class="fc" id="L163">    resetQueue();</span>
<span class="fc" id="L164">    noMorePositions = false;</span>
<span class="fc bfc" id="L165" title="All 2 branches covered.">    return (spans.advance(target) == NO_MORE_DOCS) ? NO_MORE_DOCS</span>
<span class="fc" id="L166">        : toMatchDoc();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see mtas.search.spans.util.MtasSpans#asTwoPhaseIterator()
   */
  @Override
  public TwoPhaseIterator asTwoPhaseIterator() {
<span class="pc bpc" id="L176" title="2 of 4 branches missed.">    if (spans == null || !query.twoPhaseIteratorAllowed()) {</span>
<span class="nc" id="L177">      return null;</span>
    } else {
<span class="fc" id="L179">      return spans.asTwoPhaseIterator();</span>
    }
  }

  /**
   * Reset queue.
   */
  void resetQueue() {
<span class="fc" id="L187">    queueSpans.clear();</span>
<span class="fc" id="L188">    queueMatches.clear();</span>
<span class="fc" id="L189">    lastStartPosition = 0;</span>
<span class="fc" id="L190">    lastSpan = false;</span>
<span class="fc" id="L191">    currentMatch = null;</span>
<span class="fc" id="L192">  }</span>

  /**
   * To match doc.
   *
   * @return the int
   * @throws IOException Signals that an I/O exception has occurred.
   */
  int toMatchDoc() throws IOException {
    while (true) {
<span class="pc bpc" id="L202" title="1 of 2 branches missed.">      if (findMatches()) {</span>
<span class="fc" id="L203">        return docID();</span>
      }
<span class="nc bnc" id="L205" title="All 2 branches missed.">      if (spans.nextDoc() == NO_MORE_DOCS) {</span>
<span class="nc" id="L206">        return NO_MORE_DOCS;</span>
      }
    }
  }

  /**
   * Collect span.
   *
   * @return true, if successful
   * @throws IOException Signals that an I/O exception has occurred.
   */
  // try to get something in the queue of spans
  private boolean collectSpan() throws IOException {
<span class="fc bfc" id="L219" title="All 2 branches covered.">    if (lastSpan) {</span>
<span class="fc" id="L220">      return false;</span>
<span class="fc bfc" id="L221" title="All 2 branches covered.">    } else if (spans.nextStartPosition() == NO_MORE_POSITIONS) {</span>
<span class="fc" id="L222">      lastSpan = true;</span>
<span class="fc" id="L223">      return false;</span>
    } else {
<span class="fc" id="L225">      queueSpans.add(new Match(spans.startPosition(), spans.endPosition()));</span>
<span class="fc" id="L226">      lastStartPosition = spans.startPosition();</span>
<span class="fc" id="L227">      return true;</span>
    }
  }

  /**
   * Find matches.
   *
   * @return true, if successful
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private boolean findMatches() throws IOException {
    // check for something in queue of matches
<span class="fc bfc" id="L239" title="All 2 branches covered.">    if (!queueMatches.isEmpty()) {</span>
<span class="fc" id="L240">      return true;</span>
    } else {
      while (true) {
        // try to get something in queue of spans
<span class="fc bfc" id="L244" title="All 4 branches covered.">        if (queueSpans.isEmpty() &amp;&amp; !collectSpan()) {</span>
<span class="fc" id="L245">          return false;</span>
        }
        // try to get matches with first span in queue
<span class="fc" id="L248">        Match firstMatch = queueSpans.get(0);</span>
<span class="fc" id="L249">        queueSpans.remove(0);</span>
        // create a list of matches with same startposition as firstMatch
<span class="fc" id="L251">        List&lt;Match&gt; matches = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L252">        matches.add(firstMatch);</span>
        // try to collect spans until lastStartPosition not equal to
        // startposition of firstMatch
<span class="fc bfc" id="L255" title="All 4 branches covered.">        while (!lastSpan &amp;&amp; (lastStartPosition == firstMatch.startPosition())) {</span>
<span class="fc" id="L256">          collectSpan();</span>
        }
<span class="fc bfc" id="L258" title="All 2 branches covered.">        while (!queueSpans.isEmpty() &amp;&amp; (queueSpans.get(0)</span>
<span class="fc bfc" id="L259" title="All 2 branches covered.">            .startPosition() == firstMatch.startPosition())) {</span>
<span class="fc" id="L260">          matches.add(queueSpans.get(0));</span>
<span class="fc" id="L261">          queueSpans.remove(0);</span>
        }
        // construct all matches for this startposition
<span class="fc bfc" id="L264" title="All 2 branches covered.">        for (Match match : matches) {</span>
          // only unique spans
<span class="fc bfc" id="L266" title="All 2 branches covered.">          if (!queueMatches.contains(match)) {</span>
<span class="fc" id="L267">            queueMatches.add(match);</span>
          }
<span class="fc" id="L269">        }</span>
        // check for something in queue of matches
<span class="pc bpc" id="L271" title="1 of 2 branches missed.">        if (!queueMatches.isEmpty()) {</span>
<span class="fc" id="L272">          return true;</span>
        }
<span class="nc" id="L274">      }</span>
    }
  }

  /**
   * The Class Match.
   */
  private static class Match {

    /** The start position. */
    private int startPosition;

    /** The end position. */
    private int endPosition;

    /**
     * Instantiates a new match.
     *
     * @param startPosition the start position
     * @param endPosition the end position
     */
<span class="fc" id="L295">    Match(int startPosition, int endPosition) {</span>
<span class="fc" id="L296">      this.startPosition = startPosition;</span>
<span class="fc" id="L297">      this.endPosition = endPosition;</span>
<span class="fc" id="L298">    }</span>

    /**
     * Start position.
     *
     * @return the int
     */
    public int startPosition() {
<span class="fc" id="L306">      return startPosition;</span>
    }

    /**
     * End position.
     *
     * @return the int
     */
    public int endPosition() {
<span class="fc" id="L315">      return endPosition;</span>
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
<span class="pc bpc" id="L325" title="1 of 2 branches missed.">      if (this == obj)</span>
<span class="nc" id="L326">        return true;</span>
<span class="pc bpc" id="L327" title="1 of 2 branches missed.">      if (obj == null)</span>
<span class="nc" id="L328">        return false;</span>
<span class="pc bpc" id="L329" title="1 of 2 branches missed.">      if (getClass() != obj.getClass())</span>
<span class="nc" id="L330">        return false;</span>
<span class="fc" id="L331">      final Match that = (Match) obj;</span>
<span class="pc bpc" id="L332" title="1 of 4 branches missed.">      return startPosition == that.startPosition</span>
          &amp;&amp; endPosition == that.endPosition;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
<span class="nc" id="L343">      int h = this.getClass().getSimpleName().hashCode();</span>
<span class="nc" id="L344">      h = (h * 5) ^ startPosition;</span>
<span class="nc" id="L345">      h = (h * 7) ^ endPosition;</span>
<span class="nc" id="L346">      return h;</span>
    }

  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#cost()
   */
  @Override
  public long cost() {
<span class="nc bnc" id="L358" title="All 2 branches missed.">    return (spans == null) ? 0 : spans.cost();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#positionsCost()
   */
  @Override
  public float positionsCost() {
<span class="nc bnc" id="L368" title="All 2 branches missed.">    return (spans == null) ? 0 : spans.positionsCost();</span>
  }

}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>