MtasSpanWithinQuery.java.html 21.8 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>MtasSpanWithinQuery.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</a> &gt; <span class="el_source">MtasSpanWithinQuery.java</span></div><h1>MtasSpanWithinQuery.java</h1><pre class="source lang-java linenums">package mtas.search.spans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.spans.SpanWeight;
import org.apache.lucene.search.spans.SpanWithinQuery;
import mtas.search.spans.util.MtasMaximumExpandSpanQuery;
import mtas.search.spans.util.MtasSpanQuery;

/**
 * The Class MtasSpanWithinQuery.
 */
public class MtasSpanWithinQuery extends MtasSpanQuery {

  /** The base query. */
  private SpanWithinQuery baseQuery;

  /** The small query. */
  private MtasSpanQuery smallQuery;

  /** The big query. */
  private MtasSpanQuery bigQuery;

  /** The left boundary big minimum. */
  private int leftBoundaryBigMinimum;

  /** The left boundary big maximum. */
  private int leftBoundaryBigMaximum;

  /** The right boundary big maximum. */
  private int rightBoundaryBigMaximum;

  /** The right boundary big minimum. */
  private int rightBoundaryBigMinimum;

  /** The auto adjust big query. */
  private boolean autoAdjustBigQuery;

  /** The field. */
  String field;

  /**
   * Instantiates a new mtas span within query.
   *
   * @param q1 the q 1
   * @param q2 the q 2
   */

  public MtasSpanWithinQuery(MtasSpanQuery q1, MtasSpanQuery q2) {
<span class="fc" id="L53">    this(q1, q2, 0, 0, 0, 0, true);</span>
<span class="fc" id="L54">  }</span>

  /**
   * Instantiates a new mtas span within query.
   *
   * @param q1 the q 1
   * @param q2 the q 2
   * @param leftMinimum the left minimum
   * @param leftMaximum the left maximum
   * @param rightMinimum the right minimum
   * @param rightMaximum the right maximum
   * @param adjustBigQuery the adjust big query
   */
  public MtasSpanWithinQuery(MtasSpanQuery q1, MtasSpanQuery q2,
      int leftMinimum, int leftMaximum, int rightMinimum, int rightMaximum,
      boolean adjustBigQuery) {
<span class="fc" id="L70">    super(null, null);</span>
<span class="fc" id="L71">    bigQuery = q1;</span>
<span class="fc" id="L72">    smallQuery = q2;</span>
<span class="fc" id="L73">    leftBoundaryBigMinimum = leftMinimum;</span>
<span class="fc" id="L74">    leftBoundaryBigMaximum = leftMaximum;</span>
<span class="fc" id="L75">    rightBoundaryBigMinimum = rightMinimum;</span>
<span class="fc" id="L76">    rightBoundaryBigMaximum = rightMaximum;</span>
<span class="fc" id="L77">    autoAdjustBigQuery = adjustBigQuery;</span>
    // recompute width
<span class="fc" id="L79">    Integer minimumWidth = null;</span>
<span class="fc" id="L80">    Integer maximumWidth = null;</span>
<span class="pc bpc" id="L81" title="1 of 2 branches missed.">    if (bigQuery != null) {</span>
<span class="fc" id="L82">      maximumWidth = bigQuery.getMaximumWidth();</span>
<span class="fc bfc" id="L83" title="All 2 branches covered.">      maximumWidth = (maximumWidth != null)</span>
<span class="fc" id="L84">          ? maximumWidth + rightBoundaryBigMaximum + leftBoundaryBigMaximum</span>
          : null;
    }
<span class="pc bpc" id="L87" title="1 of 2 branches missed.">    if (smallQuery != null) {</span>
<span class="fc bfc" id="L88" title="All 4 branches covered.">      if (smallQuery.getMaximumWidth() != null &amp;&amp; (maximumWidth == null</span>
<span class="fc bfc" id="L89" title="All 2 branches covered.">          || smallQuery.getMaximumWidth() &lt; maximumWidth)) {</span>
<span class="fc" id="L90">        maximumWidth = smallQuery.getMaximumWidth();</span>
      }
<span class="fc" id="L92">      minimumWidth = smallQuery.getMinimumWidth();</span>
    }
<span class="fc" id="L94">    setWidth(minimumWidth, maximumWidth);</span>
    // compute field
<span class="pc bpc" id="L96" title="2 of 4 branches missed.">    if (bigQuery != null &amp;&amp; bigQuery.getField() != null) {</span>
<span class="fc" id="L97">      field = bigQuery.getField();</span>
<span class="nc bnc" id="L98" title="All 4 branches missed.">    } else if (smallQuery != null &amp;&amp; smallQuery.getField() != null) {</span>
<span class="nc" id="L99">      field = smallQuery.getField();</span>
    } else {
<span class="nc" id="L101">      field = null;</span>
    }
<span class="pc bpc" id="L103" title="1 of 2 branches missed.">    if (field != null) {</span>
<span class="fc" id="L104">      baseQuery = new SpanWithinQuery(new MtasMaximumExpandSpanQuery(bigQuery,</span>
          leftBoundaryBigMinimum, leftBoundaryBigMaximum,
          rightBoundaryBigMinimum, rightBoundaryBigMaximum), smallQuery);
    } else {
<span class="nc" id="L108">      baseQuery = null;</span>
    }
<span class="fc" id="L110">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see mtas.search.spans.util.MtasSpanQuery#rewrite(org.apache.lucene.index.
   * IndexReader)
   */
  @Override
  public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
<span class="fc" id="L120">    MtasSpanQuery newBigQuery = bigQuery.rewrite(reader);</span>
<span class="fc" id="L121">    MtasSpanQuery newSmallQuery = smallQuery.rewrite(reader);</span>

<span class="pc bpc" id="L123" title="4 of 8 branches missed.">    if (newBigQuery == null || newBigQuery instanceof MtasSpanMatchNoneQuery</span>
        || newSmallQuery == null
        || newSmallQuery instanceof MtasSpanMatchNoneQuery) {
<span class="nc" id="L126">      return new MtasSpanMatchNoneQuery(field);</span>
    }

<span class="pc bpc" id="L129" title="1 of 2 branches missed.">    if (newSmallQuery.getMinimumWidth() != null</span>
<span class="fc bfc" id="L130" title="All 2 branches covered.">        &amp;&amp; newBigQuery.getMaximumWidth() != null</span>
<span class="pc bpc" id="L131" title="1 of 2 branches missed.">        &amp;&amp; newSmallQuery.getMinimumWidth() &gt; (newBigQuery.getMaximumWidth()</span>
            + leftBoundaryBigMaximum + rightBoundaryBigMaximum)) {
<span class="nc" id="L133">      return new MtasSpanMatchNoneQuery(field);</span>
    }

<span class="pc bpc" id="L136" title="1 of 2 branches missed.">    if (autoAdjustBigQuery) {</span>
<span class="pc bpc" id="L137" title="1 of 2 branches missed.">      if (newBigQuery instanceof MtasSpanRecurrenceQuery) {</span>
<span class="nc" id="L138">        MtasSpanRecurrenceQuery recurrenceQuery = (MtasSpanRecurrenceQuery) newBigQuery;</span>
<span class="nc bnc" id="L139" title="All 2 branches missed.">        if (recurrenceQuery.getIgnoreQuery() == null</span>
<span class="nc bnc" id="L140" title="All 2 branches missed.">            &amp;&amp; recurrenceQuery.getQuery() instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L141">          rightBoundaryBigMaximum += leftBoundaryBigMaximum</span>
<span class="nc" id="L142">              + recurrenceQuery.getMaximumRecurrence();</span>
<span class="nc" id="L143">          rightBoundaryBigMinimum += leftBoundaryBigMinimum</span>
<span class="nc" id="L144">              + recurrenceQuery.getMinimumRecurrence();</span>
<span class="nc" id="L145">          leftBoundaryBigMaximum = 0;</span>
<span class="nc" id="L146">          leftBoundaryBigMinimum = 0;</span>
<span class="nc" id="L147">          newBigQuery = new MtasSpanMatchAllQuery(field);</span>
          // System.out.println(&quot;REPLACE WITH &quot; + newBigQuery + &quot; ([&quot;
          // + leftBoundaryMinimum + &quot;,&quot; + leftBoundaryMaximum + &quot;],[&quot;
          // + rightBoundaryMinimum + &quot;,&quot; + rightBoundaryMaximum + &quot;])&quot;);
<span class="nc" id="L151">          return new MtasSpanWithinQuery(newBigQuery, newSmallQuery,</span>
              leftBoundaryBigMinimum, leftBoundaryBigMaximum,
              rightBoundaryBigMinimum, rightBoundaryBigMaximum,
<span class="nc" id="L154">              autoAdjustBigQuery).rewrite(reader);</span>
        }
<span class="pc bpc" id="L156" title="1 of 2 branches missed.">      } else if (newBigQuery instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc bnc" id="L157" title="All 2 branches missed.">        if (leftBoundaryBigMaximum &gt; 0) {</span>
<span class="nc" id="L158">          rightBoundaryBigMaximum += leftBoundaryBigMaximum;</span>
<span class="nc" id="L159">          rightBoundaryBigMinimum += leftBoundaryBigMinimum;</span>
<span class="nc" id="L160">          leftBoundaryBigMaximum = 0;</span>
<span class="nc" id="L161">          leftBoundaryBigMinimum = 0;</span>
          // System.out.println(&quot;REPLACE WITH &quot; + newBigQuery + &quot; ([&quot;
          // + leftBoundaryMinimum + &quot;,&quot; + leftBoundaryMaximum + &quot;],[&quot;
          // + rightBoundaryMinimum + &quot;,&quot; + rightBoundaryMaximum + &quot;])&quot;);
<span class="nc" id="L165">          return new MtasSpanWithinQuery(newBigQuery, newSmallQuery,</span>
              leftBoundaryBigMinimum, leftBoundaryBigMaximum,
              rightBoundaryBigMinimum, rightBoundaryBigMaximum,
<span class="nc" id="L168">              autoAdjustBigQuery).rewrite(reader);</span>
        }
<span class="fc bfc" id="L170" title="All 2 branches covered.">      } else if (newBigQuery instanceof MtasSpanSequenceQuery) {</span>
<span class="fc" id="L171">        MtasSpanSequenceQuery sequenceQuery = (MtasSpanSequenceQuery) newBigQuery;</span>
<span class="pc bpc" id="L172" title="1 of 2 branches missed.">        if (sequenceQuery.getIgnoreQuery() == null) {</span>
<span class="fc" id="L173">          List&lt;MtasSpanSequenceItem&gt; items = sequenceQuery.getItems();</span>
<span class="fc" id="L174">          List&lt;MtasSpanSequenceItem&gt; newItems = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L175">          int newLeftBoundaryMinimum = 0;</span>
<span class="fc" id="L176">          int newLeftBoundaryMaximum = 0;</span>
<span class="fc" id="L177">          int newRightBoundaryMinimum = 0;</span>
<span class="fc" id="L178">          int newRightBoundaryMaximum = 0;</span>
<span class="fc bfc" id="L179" title="All 2 branches covered.">          for (int i = 0; i &lt; items.size(); i++) {</span>
            // first item
<span class="fc bfc" id="L181" title="All 2 branches covered.">            if (i == 0) {</span>
<span class="pc bpc" id="L182" title="1 of 2 branches missed.">              if (items.get(i).getQuery() instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L183">                newLeftBoundaryMaximum++;</span>
<span class="nc bnc" id="L184" title="All 2 branches missed.">                if (!items.get(i).isOptional()) {</span>
<span class="nc" id="L185">                  newLeftBoundaryMinimum++;</span>
                }
<span class="fc" id="L187">              } else if (items.get(i)</span>
<span class="pc bpc" id="L188" title="1 of 2 branches missed.">                  .getQuery() instanceof MtasSpanRecurrenceQuery) {</span>
<span class="nc" id="L189">                MtasSpanRecurrenceQuery msrq = (MtasSpanRecurrenceQuery) items</span>
<span class="nc" id="L190">                    .get(i).getQuery();</span>
<span class="nc bnc" id="L191" title="All 2 branches missed.">                if (msrq.getQuery() instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L192">                  newLeftBoundaryMaximum += msrq.getMaximumRecurrence();</span>
<span class="nc bnc" id="L193" title="All 2 branches missed.">                  if (!items.get(i).isOptional()) {</span>
<span class="nc" id="L194">                    newLeftBoundaryMinimum += msrq.getMinimumRecurrence();</span>
                  }
                } else {
<span class="nc" id="L197">                  newItems.add(items.get(i));</span>
                }
<span class="nc" id="L199">              } else {</span>
<span class="fc" id="L200">                newItems.add(items.get(i));</span>
              }
              // last item
<span class="fc bfc" id="L203" title="All 2 branches covered.">            } else if (i == (items.size() - 1)) {</span>
<span class="pc bpc" id="L204" title="1 of 2 branches missed.">              if (items.get(i).getQuery() instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L205">                newRightBoundaryMaximum++;</span>
<span class="nc bnc" id="L206" title="All 2 branches missed.">                if (!items.get(i).isOptional()) {</span>
<span class="nc" id="L207">                  newRightBoundaryMinimum++;</span>
                }
<span class="fc" id="L209">              } else if (items.get(i)</span>
<span class="pc bpc" id="L210" title="1 of 2 branches missed.">                  .getQuery() instanceof MtasSpanRecurrenceQuery) {</span>
<span class="nc" id="L211">                MtasSpanRecurrenceQuery msrq = (MtasSpanRecurrenceQuery) items</span>
<span class="nc" id="L212">                    .get(i).getQuery();</span>
<span class="nc bnc" id="L213" title="All 2 branches missed.">                if (msrq.getQuery() instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L214">                  newRightBoundaryMaximum += msrq.getMaximumRecurrence();</span>
<span class="nc bnc" id="L215" title="All 2 branches missed.">                  if (!items.get(i).isOptional()) {</span>
<span class="nc" id="L216">                    newRightBoundaryMinimum += msrq.getMinimumRecurrence();</span>
                  }
                } else {
<span class="nc" id="L219">                  newItems.add(items.get(i));</span>
                }
<span class="nc" id="L221">              } else {</span>
<span class="fc" id="L222">                newItems.add(items.get(i));</span>
              }
              // other items
            } else {
<span class="fc" id="L226">              newItems.add(items.get(i));</span>
            }
          }
<span class="fc" id="L229">          leftBoundaryBigMaximum += newLeftBoundaryMaximum;</span>
<span class="fc" id="L230">          leftBoundaryBigMinimum += newLeftBoundaryMinimum;</span>
<span class="fc" id="L231">          rightBoundaryBigMaximum += newRightBoundaryMaximum;</span>
<span class="fc" id="L232">          rightBoundaryBigMinimum += newRightBoundaryMinimum;</span>
<span class="pc bpc" id="L233" title="1 of 2 branches missed.">          if (newItems.isEmpty()) {</span>
<span class="nc" id="L234">            rightBoundaryBigMaximum = Math.max(0,</span>
                rightBoundaryBigMaximum + leftBoundaryBigMaximum - 1);
<span class="nc" id="L236">            rightBoundaryBigMinimum = Math.max(0,</span>
                rightBoundaryBigMinimum + leftBoundaryBigMinimum - 1);
<span class="nc" id="L238">            leftBoundaryBigMaximum = 0;</span>
<span class="nc" id="L239">            leftBoundaryBigMinimum = 0;</span>
<span class="nc" id="L240">            newItems.add(new MtasSpanSequenceItem(</span>
                new MtasSpanMatchAllQuery(field), false));
          }
<span class="pc bpc" id="L243" title="3 of 6 branches missed.">          if (!items.equals(newItems) || newLeftBoundaryMaximum &gt; 0</span>
              || newRightBoundaryMaximum &gt; 0) {
<span class="nc" id="L245">            newBigQuery = (new MtasSpanSequenceQuery(newItems, null, null))</span>
<span class="nc" id="L246">                .rewrite(reader);</span>
            // System.out.println(&quot;REPLACE WITH &quot; + newBigQuery + &quot; ([&quot;
            // + leftBoundaryMinimum + &quot;,&quot; + leftBoundaryMaximum + &quot;],[&quot;
            // + rightBoundaryMinimum + &quot;,&quot; + rightBoundaryMaximum + &quot;])&quot;);
<span class="nc" id="L250">            return new MtasSpanWithinQuery(newBigQuery, newSmallQuery,</span>
                leftBoundaryBigMinimum, leftBoundaryBigMaximum,
                rightBoundaryBigMinimum, rightBoundaryBigMaximum,
<span class="nc" id="L253">                autoAdjustBigQuery).rewrite(reader);</span>
          }
        }
      }
    }

<span class="pc bpc" id="L259" title="1 of 4 branches missed.">    if (!newBigQuery.equals(bigQuery) || !newSmallQuery.equals(smallQuery)) {</span>
<span class="fc" id="L260">      return (new MtasSpanWithinQuery(newBigQuery, newSmallQuery,</span>
          leftBoundaryBigMinimum, leftBoundaryBigMaximum,
          rightBoundaryBigMinimum, rightBoundaryBigMaximum, autoAdjustBigQuery))
<span class="fc" id="L263">              .rewrite(reader);</span>
<span class="fc bfc" id="L264" title="All 2 branches covered.">    } else if (newBigQuery.equals(newSmallQuery)) {</span>
<span class="fc" id="L265">      return newBigQuery;</span>
    } else {
<span class="fc" id="L267">      baseQuery = (SpanWithinQuery) baseQuery.rewrite(reader);</span>
<span class="fc" id="L268">      return super.rewrite(reader);</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.SpanQuery#getField()
   */
  @Override
  public String getField() {
<span class="fc" id="L279">    return field;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.search.spans.SpanQuery#createWeight(org.apache.lucene.
   * search.IndexSearcher, boolean)
   */
  @Override
  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost)
      throws IOException {
<span class="fc" id="L292">    return baseQuery.createWeight(searcher, needsScores, boost);</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#toString(java.lang.String)
   */
  @Override
  public String toString(String field) {
<span class="nc" id="L302">    StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L303">    buffer.append(this.getClass().getSimpleName() + &quot;([&quot;);</span>
<span class="nc bnc" id="L304" title="All 2 branches missed.">    if (smallQuery != null) {</span>
<span class="nc" id="L305">      buffer.append(smallQuery.toString(smallQuery.getField()));</span>
    } else {
<span class="nc" id="L307">      buffer.append(&quot;null&quot;);</span>
    }
<span class="nc" id="L309">    buffer.append(&quot;,&quot;);</span>
<span class="nc bnc" id="L310" title="All 2 branches missed.">    if (bigQuery != null) {</span>
<span class="nc" id="L311">      buffer.append(bigQuery.toString(bigQuery.getField()));</span>
    } else {
<span class="nc" id="L313">      buffer.append(&quot;null&quot;);</span>
    }
<span class="nc" id="L315">    buffer.append(</span>
        &quot;],[&quot; + leftBoundaryBigMinimum + &quot;,&quot; + leftBoundaryBigMaximum + &quot;],[&quot;
            + rightBoundaryBigMinimum + &quot;,&quot; + rightBoundaryBigMaximum + &quot;])&quot;);
<span class="nc" id="L318">    return buffer.toString();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
<span class="fc bfc" id="L328" title="All 2 branches covered.">    if (this == obj)</span>
<span class="fc" id="L329">      return true;</span>
<span class="pc bpc" id="L330" title="1 of 2 branches missed.">    if (obj == null)</span>
<span class="nc" id="L331">      return false;</span>
<span class="pc bpc" id="L332" title="1 of 2 branches missed.">    if (getClass() != obj.getClass())</span>
<span class="nc" id="L333">      return false;</span>
<span class="fc" id="L334">    final MtasSpanWithinQuery that = (MtasSpanWithinQuery) obj;</span>
<span class="pc bpc" id="L335" title="4 of 10 branches missed.">    return baseQuery.equals(that.baseQuery)</span>
        &amp;&amp; leftBoundaryBigMinimum == that.leftBoundaryBigMinimum
        &amp;&amp; leftBoundaryBigMaximum == that.leftBoundaryBigMaximum
        &amp;&amp; rightBoundaryBigMinimum == that.rightBoundaryBigMinimum
        &amp;&amp; rightBoundaryBigMaximum == that.rightBoundaryBigMaximum;
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#hashCode()
   */
  @Override
  public int hashCode() {
<span class="fc" id="L349">    int h = Integer.rotateLeft(classHash(), 1);</span>
<span class="fc" id="L350">    h ^= smallQuery.hashCode();</span>
<span class="fc" id="L351">    h = Integer.rotateLeft(h, 1);</span>
<span class="fc" id="L352">    h ^= bigQuery.hashCode();</span>
<span class="fc" id="L353">    h = Integer.rotateLeft(h, leftBoundaryBigMinimum) + leftBoundaryBigMinimum;</span>
<span class="fc" id="L354">    h ^= 2;</span>
<span class="fc" id="L355">    h = Integer.rotateLeft(h, leftBoundaryBigMaximum) + leftBoundaryBigMaximum;</span>
<span class="fc" id="L356">    h ^= 3;</span>
<span class="fc" id="L357">    h = Integer.rotateLeft(h, rightBoundaryBigMinimum)</span>
        + rightBoundaryBigMinimum;
<span class="fc" id="L359">    h ^= 5;</span>
<span class="fc" id="L360">    h = Integer.rotateLeft(h, rightBoundaryBigMaximum)</span>
        + rightBoundaryBigMaximum;
<span class="fc" id="L362">    return h;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see mtas.search.spans.util.MtasSpanQuery#disableTwoPhaseIterator()
   */
  @Override
  public void disableTwoPhaseIterator() {
<span class="fc" id="L372">    super.disableTwoPhaseIterator();</span>
<span class="fc" id="L373">    bigQuery.disableTwoPhaseIterator();</span>
<span class="fc" id="L374">    smallQuery.disableTwoPhaseIterator();</span>
<span class="fc" id="L375">  }</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>