MtasMaximumExpandSpans.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>MtasMaximumExpandSpans.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">MtasMaximumExpandSpans.java</span></div><h1>MtasMaximumExpandSpans.java</h1><pre class="source lang-java linenums">package mtas.search.spans.util;

import java.io.IOException;

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

import mtas.codec.util.CodecInfo;
import mtas.codec.util.CodecInfo.IndexDoc;

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

  /** The sub spans. */
  Spans subSpans;

  /** The query. */
  MtasMaximumExpandSpanQuery query;

  /** The min position. */
  int minPosition;

  /** The max position. */
  int maxPosition;

  /** The field. */
  String field;

  /** The mtas codec info. */
  CodecInfo mtasCodecInfo;

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

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

  /** The called next start position. */
  private boolean calledNextStartPosition;

  /** The doc id. */
  int docId;

  /**
   * Instantiates a new mtas maximum expand spans.
   *
   * @param query the query
   * @param mtasCodecInfo the mtas codec info
   * @param field the field
   * @param subSpans the sub spans
   */
  public MtasMaximumExpandSpans(MtasMaximumExpandSpanQuery query,
      CodecInfo mtasCodecInfo, String field, Spans subSpans) {
<span class="fc" id="L57">    super();</span>
<span class="fc" id="L58">    this.subSpans = subSpans;</span>
<span class="fc" id="L59">    this.field = field;</span>
<span class="fc" id="L60">    this.mtasCodecInfo = mtasCodecInfo;</span>
<span class="fc" id="L61">    this.query = query;</span>
<span class="fc" id="L62">    docId = -1;</span>
<span class="fc" id="L63">    reset();</span>
<span class="fc" id="L64">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#nextStartPosition()
   */
  @Override
  public int nextStartPosition() throws IOException {
<span class="pc bpc" id="L73" title="2 of 4 branches missed.">    if (docId == -1 || docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L74">      throw new IOException(&quot;no document&quot;);</span>
<span class="fc bfc" id="L75" title="All 2 branches covered.">    } else if (!calledNextStartPosition) {</span>
<span class="fc" id="L76">      calledNextStartPosition = true;</span>
<span class="fc" id="L77">      return startPosition;</span>
      // compute next match
    } else {
<span class="fc bfc" id="L80" title="All 2 branches covered.">      if (goToNextStartPosition()) {</span>
        // match found
<span class="fc" id="L82">        return startPosition;</span>
      } else {
        // no more matches: document finished
<span class="fc" id="L85">        return NO_MORE_POSITIONS;</span>
      }
    }
  }

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

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

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#width()
   */
  @Override
  public int width() {
<span class="nc" id="L117">    return endPosition - startPosition;</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="L128">    subSpans.collect(collector);</span>
<span class="nc" id="L129">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#positionsCost()
   */
  @Override
  public float positionsCost() {
    // return subSpans.positionsCost();
<span class="nc" id="L139">    return 0;</span>
  }

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

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#nextDoc()
   */
  @Override
  public int nextDoc() throws IOException {
<span class="fc" id="L159">    reset();</span>
<span class="pc bpc" id="L160" title="1 of 2 branches missed.">    while (!goToNextDoc())</span>
<span class="nc" id="L161">      ;</span>
<span class="fc" id="L162">    return docId;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#advance(int)
   */
  @Override
  public int advance(int target) throws IOException {
<span class="nc" id="L172">    reset();</span>
<span class="nc bnc" id="L173" title="All 2 branches missed.">    if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L174">      return docId;</span>
<span class="nc bnc" id="L175" title="All 2 branches missed.">    } else if (target &lt;= docId) {</span>
      // should not happen
<span class="nc" id="L177">      docId = NO_MORE_DOCS;</span>
<span class="nc" id="L178">      return docId;</span>
    } else {
<span class="nc" id="L180">      docId = subSpans.advance(target);</span>
<span class="nc bnc" id="L181" title="All 2 branches missed.">      if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L182">        return docId;</span>
      } else {
<span class="nc" id="L184">        IndexDoc doc = mtasCodecInfo.getDoc(field, docId);</span>
<span class="nc bnc" id="L185" title="All 2 branches missed.">        if (doc != null) {</span>
<span class="nc" id="L186">          minPosition = doc.minPosition;</span>
<span class="nc" id="L187">          maxPosition = doc.maxPosition;</span>
        } else {
<span class="nc" id="L189">          minPosition = NO_MORE_POSITIONS;</span>
<span class="nc" id="L190">          maxPosition = NO_MORE_POSITIONS;</span>
        }
<span class="nc bnc" id="L192" title="All 2 branches missed.">        if (goToNextStartPosition()) {</span>
<span class="nc" id="L193">          return docId;</span>
        } else {
<span class="nc" id="L195">          return nextDoc();</span>
        }
      }
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#asTwoPhaseIterator()
   */
  @Override
  public TwoPhaseIterator asTwoPhaseIterator() {
<span class="nc bnc" id="L208" title="All 2 branches missed.">    if (!query.twoPhaseIteratorAllowed()) {</span>
<span class="nc" id="L209">      return null;</span>
    } else {
<span class="nc" id="L211">      TwoPhaseIterator originalTwoPhaseIterator = subSpans.asTwoPhaseIterator();</span>
<span class="nc bnc" id="L212" title="All 2 branches missed.">      if (originalTwoPhaseIterator != null) {</span>
<span class="nc" id="L213">        return new TwoPhaseIterator(originalTwoPhaseIterator.approximation()) {</span>
          @Override
          public boolean matches() throws IOException {
<span class="nc bnc" id="L216" title="All 2 branches missed.">            return originalTwoPhaseIterator.matches()</span>
<span class="nc bnc" id="L217" title="All 2 branches missed.">                &amp;&amp; twoPhaseCurrentDocMatches();</span>
          }

          @Override
          public float matchCost() {
<span class="nc" id="L222">            return originalTwoPhaseIterator.matchCost();</span>
          }
        };
      } else {
<span class="nc" id="L226">        return new TwoPhaseIterator(subSpans) {</span>

          @Override
          public boolean matches() throws IOException {
<span class="nc" id="L230">            return twoPhaseCurrentDocMatches();</span>
          }

          @Override
          public float matchCost() {
<span class="nc" id="L235">            return subSpans.positionsCost();</span>
          }
        };
      }
    }
  }

  /**
   * Two phase current doc matches.
   *
   * @return true, if successful
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private boolean twoPhaseCurrentDocMatches() throws IOException {
<span class="nc bnc" id="L249" title="All 2 branches missed.">    if (docId != subSpans.docID()) {</span>
<span class="nc" id="L250">      reset();</span>
<span class="nc" id="L251">      docId = subSpans.docID();</span>
<span class="nc" id="L252">      IndexDoc doc = mtasCodecInfo.getDoc(field, docId);</span>
<span class="nc bnc" id="L253" title="All 2 branches missed.">      if (doc != null) {</span>
<span class="nc" id="L254">        minPosition = doc.minPosition;</span>
<span class="nc" id="L255">        maxPosition = doc.maxPosition;</span>
      } else {
<span class="nc" id="L257">        minPosition = NO_MORE_POSITIONS;</span>
<span class="nc" id="L258">        maxPosition = NO_MORE_POSITIONS;</span>
      }
    }
<span class="nc bnc" id="L261" title="All 2 branches missed.">    if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L262">      return false;</span>
    } else {
<span class="nc" id="L264">      return goToNextStartPosition();</span>
    }
  }

  /**
   * Go to next doc.
   *
   * @return true, if successful
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private boolean goToNextDoc() throws IOException {
<span class="fc" id="L275">    reset();</span>
<span class="pc bpc" id="L276" title="1 of 2 branches missed.">    if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L277">      minPosition = NO_MORE_POSITIONS;</span>
<span class="nc" id="L278">      maxPosition = NO_MORE_POSITIONS;</span>
<span class="nc" id="L279">      return true;</span>
    } else {
<span class="fc" id="L281">      docId = subSpans.nextDoc();</span>
<span class="fc bfc" id="L282" title="All 2 branches covered.">      if (docId == NO_MORE_DOCS) {</span>
<span class="fc" id="L283">        minPosition = NO_MORE_POSITIONS;</span>
<span class="fc" id="L284">        maxPosition = NO_MORE_POSITIONS;</span>
<span class="fc" id="L285">        return true;</span>
      } else {
<span class="fc" id="L287">        IndexDoc doc = mtasCodecInfo.getDoc(field, docId);</span>
<span class="pc bpc" id="L288" title="1 of 2 branches missed.">        if (doc != null) {</span>
<span class="fc" id="L289">          minPosition = doc.minPosition;</span>
<span class="fc" id="L290">          maxPosition = doc.maxPosition;</span>
        } else {
<span class="nc" id="L292">          minPosition = NO_MORE_POSITIONS;</span>
<span class="nc" id="L293">          maxPosition = NO_MORE_POSITIONS;</span>
        }
<span class="pc bpc" id="L295" title="1 of 2 branches missed.">        if (goToNextStartPosition()) {</span>
<span class="fc" id="L296">          return true;</span>
        } else {
<span class="nc" id="L298">          return false;</span>
        }
      }
    }
  }

  /**
   * Go to next start position.
   *
   * @return true, if successful
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private boolean goToNextStartPosition() throws IOException {
    int basicStartPosition;
    int basicEndPosition;
<span class="pc bpc" id="L313" title="2 of 4 branches missed.">    if (docId == -1 || docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L314">      throw new IOException(&quot;no document&quot;);</span>
    } else {
<span class="fc" id="L316">      while ((basicStartPosition = subSpans</span>
<span class="fc bfc" id="L317" title="All 2 branches covered.">          .nextStartPosition()) != NO_MORE_POSITIONS) {</span>
<span class="fc" id="L318">        basicEndPosition = subSpans.endPosition();</span>
<span class="fc" id="L319">        startPosition = Math.max(minPosition,</span>
            (basicStartPosition - query.maximumLeft));
<span class="fc" id="L321">        endPosition = Math.min(maxPosition + 1,</span>
            (basicEndPosition + query.maximumRight));
<span class="pc bpc" id="L323" title="2 of 4 branches missed.">        if (startPosition &lt;= (basicStartPosition - query.minimumLeft)</span>
            &amp;&amp; endPosition &gt;= (basicEndPosition + query.minimumRight)) {
<span class="fc" id="L325">          return true;</span>
        }
      }
<span class="fc" id="L328">      return false;</span>
    }
  }

  /**
   * Reset.
   */
  private void reset() {
<span class="fc" id="L336">    calledNextStartPosition = false;</span>
<span class="fc" id="L337">    minPosition = 0;</span>
<span class="fc" id="L338">    maxPosition = 0;</span>
<span class="fc" id="L339">    startPosition = -1;</span>
<span class="fc" id="L340">    endPosition = -1;</span>
<span class="fc" id="L341">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#cost()
   */
  @Override
  public long cost() {
<span class="nc bnc" id="L350" title="All 2 branches missed.">    return subSpans != null ? subSpans.cost() : 0;</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>