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

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

import mtas.search.spans.util.MtasSpanQuery;

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

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

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

  /**
   * Instantiates a new mtas span or query.
   *
   * @param initialClauses the initial clauses
   */
  public MtasSpanOrQuery(MtasSpanQuery... initialClauses) {
<span class="fc" id="L31">    super(null, null);</span>
<span class="fc" id="L32">    Integer minimum = null;</span>
<span class="fc" id="L33">    Integer maximum = null;</span>
<span class="fc" id="L34">    clauses = new HashSet&lt;&gt;();</span>
<span class="fc bfc" id="L35" title="All 2 branches covered.">    for (MtasSpanQuery item : initialClauses) {</span>
<span class="pc bpc" id="L36" title="1 of 2 branches missed.">      if (!clauses.contains(item)) {</span>
<span class="fc bfc" id="L37" title="All 2 branches covered.">        if (clauses.isEmpty()) {</span>
<span class="fc" id="L38">          minimum = item.getMinimumWidth();</span>
<span class="fc" id="L39">          maximum = item.getMaximumWidth();</span>
        } else {
<span class="pc bpc" id="L41" title="2 of 4 branches missed.">          if (minimum != null &amp;&amp; item.getMinimumWidth() != null) {</span>
<span class="fc" id="L42">            minimum = Math.min(minimum, item.getMinimumWidth());</span>
          } else {
<span class="nc" id="L44">            minimum = null;</span>
          }
<span class="pc bpc" id="L46" title="2 of 4 branches missed.">          if (maximum != null &amp;&amp; item.getMaximumWidth() != null) {</span>
<span class="fc" id="L47">            maximum = Math.max(maximum, item.getMaximumWidth());</span>
          } else {
<span class="nc" id="L49">            maximum = null;</span>
          }
        }
<span class="fc" id="L52">        clauses.add(item);</span>
      }
    }
<span class="fc" id="L55">    setWidth(minimum, maximum);</span>
<span class="fc" id="L56">    baseQuery = new SpanOrQuery(</span>
<span class="fc" id="L57">        clauses.toArray(new MtasSpanQuery[clauses.size()]));</span>
<span class="fc" id="L58">  }</span>

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

  /*
   * (non-Javadoc)
   * 
   * @see
   * mtas.search.spans.util.MtasSpanQuery#createWeight(org.apache.lucene.search.
   * IndexSearcher, boolean)
   */
  @Override
  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost)
      throws IOException {
<span class="fc" id="L80">    return baseQuery.createWeight(searcher, needsScores, boost);</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 bfc" id="L91" title="All 2 branches covered.">    if (clauses.size() &gt; 1) {</span>
      // rewrite, count MtasSpanMatchAllQuery and check for
      // MtasSpanMatchNoneQuery
<span class="fc" id="L94">      MtasSpanQuery[] newClauses = new MtasSpanQuery[clauses.size()];</span>
<span class="fc" id="L95">      MtasSpanQuery[] oldClauses = clauses</span>
<span class="fc" id="L96">          .toArray(new MtasSpanQuery[clauses.size()]);</span>
<span class="fc" id="L97">      int singlePositionQueries = 0;</span>
<span class="fc" id="L98">      int matchAllSinglePositionQueries = 0;</span>
<span class="fc" id="L99">      int matchNoneQueries = 0;</span>
<span class="fc" id="L100">      boolean actuallyRewritten = false;</span>
<span class="fc bfc" id="L101" title="All 2 branches covered.">      for (int i = 0; i &lt; oldClauses.length; i++) {</span>
<span class="fc" id="L102">        newClauses[i] = oldClauses[i].rewrite(reader);</span>
<span class="fc bfc" id="L103" title="All 2 branches covered.">        actuallyRewritten |= !oldClauses[i].equals(newClauses[i]);</span>
<span class="pc bpc" id="L104" title="1 of 2 branches missed.">        if (newClauses[i] instanceof MtasSpanMatchNoneQuery) {</span>
<span class="nc" id="L105">          matchNoneQueries++;</span>
<span class="pc bpc" id="L106" title="1 of 2 branches missed.">        } else if (newClauses[i].isSinglePositionQuery()) {</span>
<span class="fc" id="L107">          singlePositionQueries++;</span>
<span class="pc bpc" id="L108" title="1 of 2 branches missed.">          if (newClauses[i] instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L109">            matchAllSinglePositionQueries++;</span>
          }
        }
      }
      // filter clauses
<span class="pc bpc" id="L114" title="2 of 4 branches missed.">      if (matchNoneQueries &gt; 0 || matchAllSinglePositionQueries &gt; 0) {</span>
        // compute new number of clauses
<span class="nc" id="L116">        int newNumber = newClauses.length - matchNoneQueries;</span>
<span class="nc bnc" id="L117" title="All 2 branches missed.">        if (matchAllSinglePositionQueries &gt; 0) {</span>
<span class="nc" id="L118">          newNumber -= singlePositionQueries - 1;</span>
        }
<span class="nc" id="L120">        MtasSpanQuery[] newFilteredClauses = new MtasSpanQuery[newNumber];</span>
<span class="nc" id="L121">        int j = 0;</span>
<span class="nc bnc" id="L122" title="All 2 branches missed.">        for (int i = 0; i &lt; newClauses.length; i++) {</span>
<span class="nc bnc" id="L123" title="All 2 branches missed.">          if (!(newClauses[i] instanceof MtasSpanMatchNoneQuery)) {</span>
<span class="nc bnc" id="L124" title="All 4 branches missed.">            if (!newClauses[i].isSinglePositionQuery()</span>
                || matchAllSinglePositionQueries == 0) {
<span class="nc" id="L126">              newFilteredClauses[j] = newClauses[i];</span>
<span class="nc" id="L127">              j++;</span>
<span class="nc bnc" id="L128" title="All 2 branches missed.">            } else if (singlePositionQueries &gt; 0) {</span>
<span class="nc" id="L129">              newFilteredClauses[j] = newClauses[i];</span>
<span class="nc" id="L130">              j++;</span>
<span class="nc" id="L131">              singlePositionQueries = 0; // only match this condition once</span>
            }
          }
        }
<span class="nc" id="L135">        newClauses = newFilteredClauses;</span>
      }
<span class="pc bpc" id="L137" title="1 of 2 branches missed.">      if (newClauses.length == 0) {</span>
<span class="nc" id="L138">        return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);</span>
<span class="pc bpc" id="L139" title="1 of 2 branches missed.">      } else if (newClauses.length == 1) {</span>
<span class="nc" id="L140">        return newClauses[0].rewrite(reader);</span>
<span class="pc bpc" id="L141" title="1 of 4 branches missed.">      } else if (actuallyRewritten || newClauses.length != clauses.size()) {</span>
<span class="fc" id="L142">        return new MtasSpanOrQuery(newClauses).rewrite(reader);</span>
      } else {
<span class="fc" id="L144">        return super.rewrite(reader);</span>
      }
<span class="fc bfc" id="L146" title="All 2 branches covered.">    } else if (clauses.size() == 1) {</span>
<span class="fc" id="L147">      return clauses.iterator().next().rewrite(reader);</span>
    } else {
<span class="fc" id="L149">      return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * mtas.search.spans.MtasSpanUniquePositionQuery#toString(java.lang.String)
   */
  @Override
  public String toString(String field) {
<span class="nc" id="L161">    StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L162">    buffer.append(this.getClass().getSimpleName() + &quot;([&quot;);</span>
<span class="nc" id="L163">    Iterator&lt;MtasSpanQuery&gt; i = clauses.iterator();</span>
<span class="nc bnc" id="L164" title="All 2 branches missed.">    while (i.hasNext()) {</span>
<span class="nc" id="L165">      SpanQuery clause = i.next();</span>
<span class="nc" id="L166">      buffer.append(clause.toString(field));</span>
<span class="nc bnc" id="L167" title="All 2 branches missed.">      if (i.hasNext()) {</span>
<span class="nc" id="L168">        buffer.append(&quot;, &quot;);</span>
      }
<span class="nc" id="L170">    }</span>
<span class="nc" id="L171">    buffer.append(&quot;])&quot;);</span>
<span class="nc" id="L172">    return buffer.toString();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see mtas.search.spans.MtasSpanUniquePositionQuery#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
<span class="fc bfc" id="L182" title="All 2 branches covered.">    if (this == obj)</span>
<span class="fc" id="L183">      return true;</span>
<span class="pc bpc" id="L184" title="1 of 2 branches missed.">    if (obj == null)</span>
<span class="nc" id="L185">      return false;</span>
<span class="fc bfc" id="L186" title="All 2 branches covered.">    if (getClass() != obj.getClass())</span>
<span class="fc" id="L187">      return false;</span>
<span class="fc" id="L188">    final MtasSpanOrQuery that = (MtasSpanOrQuery) obj;</span>
<span class="fc" id="L189">    return clauses.equals(that.clauses);</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see mtas.search.spans.MtasSpanUniquePositionQuery#hashCode()
   */
  @Override
  public int hashCode() {
<span class="fc" id="L199">    int h = this.getClass().getSimpleName().hashCode();</span>
<span class="fc" id="L200">    h = (h * 7) ^ clauses.hashCode();</span>
<span class="fc" id="L201">    return h;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see mtas.search.spans.util.MtasSpanQuery#disableTwoPhaseIterator()
   */
  @Override
  public void disableTwoPhaseIterator() {
<span class="fc" id="L211">    super.disableTwoPhaseIterator();</span>
<span class="fc bfc" id="L212" title="All 2 branches covered.">    for (MtasSpanQuery item : clauses) {</span>
<span class="fc" id="L213">      item.disableTwoPhaseIterator();</span>
<span class="fc" id="L214">    }</span>
<span class="fc" id="L215">  }</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>