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

import java.io.IOException;
import java.util.HashSet;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanWeight;

import mtas.search.spans.util.MtasExtendedSpanAndQuery;
import mtas.search.spans.util.MtasSpanQuery;

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

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

  /** The clauses. */
  private HashSet&lt;MtasSpanQuery&gt; clauses;

  /**
   * Instantiates a new mtas span and query.
   *
   * @param initialClauses the initial clauses
   */
  public MtasSpanAndQuery(MtasSpanQuery... initialClauses) {
<span class="fc" id="L30">    super(null, null);</span>
<span class="fc" id="L31">    Integer minimum = null;</span>
<span class="fc" id="L32">    Integer maximum = null;</span>
<span class="fc" id="L33">    clauses = new HashSet&lt;&gt;();</span>
<span class="fc bfc" id="L34" title="All 2 branches covered.">    for (MtasSpanQuery item : initialClauses) {</span>
<span class="pc bpc" id="L35" title="1 of 2 branches missed.">      if (!clauses.contains(item)) {</span>
<span class="fc" id="L36">        clauses.add(item);</span>
<span class="pc bpc" id="L37" title="1 of 2 branches missed.">        if (item.getMinimumWidth() != null) {</span>
<span class="fc bfc" id="L38" title="All 2 branches covered.">          if (minimum != null) {</span>
<span class="fc" id="L39">            minimum = Math.max(minimum, item.getMinimumWidth());</span>
          } else {
<span class="fc" id="L41">            minimum = item.getMinimumWidth();</span>
          }
        }
<span class="pc bpc" id="L44" title="1 of 2 branches missed.">        if (item.getMaximumWidth() != null) {</span>
<span class="fc bfc" id="L45" title="All 2 branches covered.">          if (maximum != null) {</span>
<span class="fc" id="L46">            maximum = Math.max(maximum, item.getMaximumWidth());</span>
          } else {
<span class="fc" id="L48">            maximum = item.getMaximumWidth();</span>
          }
        }
      }
    }
<span class="fc" id="L53">    setWidth(minimum, maximum);</span>
<span class="fc" id="L54">    baseQuery = new MtasExtendedSpanAndQuery(</span>
<span class="fc" id="L55">        clauses.toArray(new MtasSpanQuery[clauses.size()]));</span>
<span class="fc" id="L56">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.SpanQuery#getField()
   */
  @Override
  public String getField() {
<span class="fc" id="L65">    return baseQuery.getField();</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)
      throws IOException {
<span class="fc" id="L78">    return baseQuery.createWeight(searcher, needsScores);</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="pc bpc" id="L89" title="1 of 2 branches missed.">    if (clauses.size() &gt; 1) {</span>
      // rewrite, count MtasSpanMatchAllQuery and check for
      // MtasSpanMatchNoneQuery
<span class="fc" id="L92">      MtasSpanQuery[] newClauses = new MtasSpanQuery[clauses.size()];</span>
<span class="fc" id="L93">      MtasSpanQuery[] oldClauses = clauses</span>
<span class="fc" id="L94">          .toArray(new MtasSpanQuery[clauses.size()]);</span>
<span class="fc" id="L95">      int singlePositionQueries = 0;</span>
<span class="fc" id="L96">      int matchAllSinglePositionQueries = 0;</span>
<span class="fc" id="L97">      boolean actuallyRewritten = false;</span>
<span class="fc bfc" id="L98" title="All 2 branches covered.">      for (int i = 0; i &lt; oldClauses.length; i++) {</span>
<span class="fc" id="L99">        newClauses[i] = oldClauses[i].rewrite(reader);</span>
<span class="fc bfc" id="L100" title="All 2 branches covered.">        actuallyRewritten |= !oldClauses[i].equals(newClauses[i]);</span>
<span class="pc bpc" id="L101" title="1 of 2 branches missed.">        if (newClauses[i] instanceof MtasSpanMatchNoneQuery) {</span>
<span class="nc" id="L102">          return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);</span>
        } else {
<span class="pc bpc" id="L104" title="1 of 2 branches missed.">          if (newClauses[i].isSinglePositionQuery()) {</span>
<span class="fc" id="L105">            singlePositionQueries++;</span>
<span class="pc bpc" id="L106" title="1 of 2 branches missed.">            if (newClauses[i] instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L107">              matchAllSinglePositionQueries++;</span>
            }
          }
        }
      }
      // filter clauses
<span class="pc bpc" id="L113" title="1 of 2 branches missed.">      if (matchAllSinglePositionQueries &gt; 0) {</span>
        // compute new number of clauses
<span class="nc" id="L115">        int newNumber = newClauses.length - matchAllSinglePositionQueries;</span>
<span class="nc bnc" id="L116" title="All 2 branches missed.">        if (matchAllSinglePositionQueries == singlePositionQueries) {</span>
<span class="nc" id="L117">          newNumber++;</span>
        }
<span class="nc" id="L119">        MtasSpanQuery[] newFilteredClauses = new MtasSpanQuery[newNumber];</span>
<span class="nc" id="L120">        int j = 0;</span>
<span class="nc bnc" id="L121" title="All 2 branches missed.">        for (int i = 0; i &lt; newClauses.length; i++) {</span>
<span class="nc bnc" id="L122" title="All 4 branches missed.">          if (!(newClauses[i].isSinglePositionQuery()</span>
              &amp;&amp; (newClauses[i] instanceof MtasSpanMatchAllQuery))) {
<span class="nc" id="L124">            newFilteredClauses[j] = newClauses[i];</span>
<span class="nc" id="L125">            j++;</span>
<span class="nc bnc" id="L126" title="All 2 branches missed.">          } else if (matchAllSinglePositionQueries == singlePositionQueries) {</span>
<span class="nc" id="L127">            newFilteredClauses[j] = newClauses[i];</span>
<span class="nc" id="L128">            j++;</span>
<span class="nc" id="L129">            singlePositionQueries++; // only match this condition once</span>
          }
        }
<span class="nc" id="L132">        newClauses = newFilteredClauses;</span>
      }
<span class="pc bpc" id="L134" title="1 of 2 branches missed.">      if (newClauses.length == 0) {</span>
<span class="nc" id="L135">        return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);</span>
<span class="pc bpc" id="L136" title="1 of 2 branches missed.">      } else if (newClauses.length == 1) {</span>
<span class="nc" id="L137">        return newClauses[0].rewrite(reader);</span>
<span class="pc bpc" id="L138" title="1 of 4 branches missed.">      } else if (actuallyRewritten || newClauses.length != clauses.size()) {</span>
<span class="fc" id="L139">        return new MtasSpanAndQuery(newClauses).rewrite(reader);</span>
      } else {
<span class="fc" id="L141">        return super.rewrite(reader);</span>
      }
<span class="nc bnc" id="L143" title="All 2 branches missed.">    } else if (clauses.size() == 1) {</span>
<span class="nc" id="L144">      return clauses.iterator().next().rewrite(reader);</span>
    } else {
<span class="nc" id="L146">      return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.search.spans.SpanNearQuery#toString(java.lang.String)
   */
  @Override
  public String toString(String field) {
<span class="nc" id="L158">    return baseQuery.toString(field);</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.SpanNearQuery#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
<span class="fc bfc" id="L168" title="All 2 branches covered.">    if (this == obj)</span>
<span class="fc" id="L169">      return true;</span>
<span class="pc bpc" id="L170" title="1 of 2 branches missed.">    if (obj == null)</span>
<span class="nc" id="L171">      return false;</span>
<span class="pc bpc" id="L172" title="1 of 2 branches missed.">    if (getClass() != obj.getClass())</span>
<span class="nc" id="L173">      return false;</span>
<span class="fc" id="L174">    final MtasSpanAndQuery that = (MtasSpanAndQuery) obj;</span>
<span class="fc" id="L175">    return baseQuery.equals(that.baseQuery);</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.SpanNearQuery#hashCode()
   */
  @Override
  public int hashCode() {
<span class="fc" id="L185">    int h = this.getClass().getSimpleName().hashCode();</span>
<span class="fc" id="L186">    h = (h * 7) ^ clauses.hashCode();</span>
<span class="fc" id="L187">    return h;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see mtas.search.spans.util.MtasSpanQuery#disableTwoPhaseIterator()
   */
  @Override
  public void disableTwoPhaseIterator() {
<span class="nc" id="L197">    super.disableTwoPhaseIterator();</span>
<span class="nc bnc" id="L198" title="All 2 branches missed.">    for (MtasSpanQuery item : clauses) {</span>
<span class="nc" id="L199">      item.disableTwoPhaseIterator();</span>
<span class="nc" id="L200">    }</span>
<span class="nc" id="L201">  }</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>