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

import java.io.IOException;
import java.util.Collection;

import mtas.analysis.token.MtasPosition;
import mtas.codec.payload.MtasPayloadDecoder;

import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Term;
import org.apache.lucene.payloads.PayloadSpanCollector;
import org.apache.lucene.search.spans.TermSpans;

/**
 * The Class MtasExtendedTermSpans.
 */
public class MtasExtendedTermSpans extends TermSpans implements MtasSpans {

  /** The mtas position. */
<span class="fc" id="L20">  protected MtasPosition mtasPosition = null;</span>

  /** The assume single position. */
  private boolean assumeSinglePosition;

  /** The payload span collector. */
  private PayloadSpanCollector payloadSpanCollector;

  /**
   * Instantiates a new mtas extended term spans.
   *
   * @param postings the postings
   * @param term the term
   */
  public MtasExtendedTermSpans(PostingsEnum postings, Term term) {
<span class="nc" id="L35">    this(postings, term, false);</span>
<span class="nc" id="L36">  }</span>

  /**
   * Instantiates a new mtas extended term spans.
   *
   * @param postings the postings
   * @param term the term
   * @param assumeSinglePosition the assume single position
   */
  public MtasExtendedTermSpans(PostingsEnum postings, Term term,
      boolean assumeSinglePosition) {
<span class="fc" id="L47">    super(null, postings, term, 1);</span>
<span class="fc" id="L48">    payloadSpanCollector = new PayloadSpanCollector();</span>
<span class="fc" id="L49">    this.assumeSinglePosition = assumeSinglePosition;</span>
<span class="fc" id="L50">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.TermSpans#endPosition()
   */
  @Override
  public int endPosition() {
<span class="fc bfc" id="L59" title="All 2 branches covered.">    if (assumeSinglePosition) {</span>
<span class="fc" id="L60">      return super.endPosition();</span>
    } else {
<span class="fc" id="L62">      int status = super.endPosition();</span>
      try {
<span class="fc" id="L64">        processEncodedPayload();</span>
<span class="fc bfc" id="L65" title="All 2 branches covered.">        return (status != NO_MORE_POSITIONS) ? (mtasPosition.getEnd() + 1)</span>
            : NO_MORE_POSITIONS;
<span class="fc" id="L67">      } catch (IOException e) {</span>
<span class="fc" id="L68">        return NO_MORE_POSITIONS;</span>
      }
    }
  }

  /**
   * Gets the positions.
   *
   * @return the positions
   */
  public int[] getPositions() {
    int[] list;
<span class="nc bnc" id="L80" title="All 2 branches missed.">    if (assumeSinglePosition) {</span>
<span class="nc" id="L81">      list = new int[1];</span>
<span class="nc" id="L82">      list[0] = super.startPosition();</span>
<span class="nc" id="L83">      return list;</span>
    } else {
      try {
<span class="nc" id="L86">        processEncodedPayload();</span>
<span class="nc" id="L87">        list = mtasPosition.getPositions();</span>
<span class="nc bnc" id="L88" title="All 2 branches missed.">        if (list != null) {</span>
<span class="nc" id="L89">          return mtasPosition.getPositions();</span>
        }
<span class="nc" id="L91">      } catch (IOException e) {</span>
        // do nothing
<span class="nc" id="L93">      }</span>
<span class="nc" id="L94">      int start = super.startPosition();</span>
<span class="nc" id="L95">      int end = super.endPosition();</span>
<span class="nc" id="L96">      list = new int[end - start];</span>
<span class="nc bnc" id="L97" title="All 2 branches missed.">      for (int i = start; i &lt; end; i++)</span>
<span class="nc" id="L98">        list[i - start] = i;</span>
<span class="nc" id="L99">      return list;</span>
    }
  }

  /**
   * Process encoded payload.
   *
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void processEncodedPayload() throws IOException {
<span class="pc bpc" id="L109" title="1 of 2 branches missed.">    if (!readPayload) {</span>
<span class="fc" id="L110">      payloadSpanCollector.reset();</span>
<span class="fc" id="L111">      collect(payloadSpanCollector);</span>
<span class="fc" id="L112">      Collection&lt;byte[]&gt; originalPayloadCollection = payloadSpanCollector</span>
<span class="fc" id="L113">          .getPayloads();</span>
<span class="fc bfc" id="L114" title="All 2 branches covered.">      if (originalPayloadCollection.iterator().hasNext()) {</span>
<span class="fc" id="L115">        byte[] payload = originalPayloadCollection.iterator().next();</span>
<span class="pc bpc" id="L116" title="1 of 2 branches missed.">        if (payload == null) {</span>
<span class="nc" id="L117">          throw new IOException(&quot;no payload&quot;);</span>
        }
<span class="fc" id="L119">        MtasPayloadDecoder payloadDecoder = new MtasPayloadDecoder();</span>
<span class="fc" id="L120">        payloadDecoder.init(startPosition(), payload);</span>
<span class="fc" id="L121">        mtasPosition = payloadDecoder.getMtasPosition();</span>
<span class="fc" id="L122">      } else {</span>
<span class="fc" id="L123">        throw new IOException(&quot;no payload&quot;);</span>
      }
    }
<span class="fc" id="L126">  }</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>