MtasExtendedTermSpans.java.html 6.85 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>
  
  @Override
  public int startPosition() {
<span class="fc" id="L54">    return super.startPosition();</span>
  }

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

  /**
   * Gets the positions.
   *
   * @return the positions
   */
  public int[] getPositions() {
    int[] list;
<span class="nc bnc" id="L91" title="All 2 branches missed.">    if (assumeSinglePosition) {</span>
<span class="nc" id="L92">      list = new int[1];</span>
<span class="nc" id="L93">      list[0] = super.startPosition();</span>
<span class="nc" id="L94">      return list;</span>
    } else {
      try {
<span class="nc" id="L97">        processEncodedPayload();</span>
<span class="nc" id="L98">        list = mtasPosition.getPositions();</span>
<span class="nc bnc" id="L99" title="All 2 branches missed.">        if (list != null) {</span>
<span class="nc" id="L100">          return mtasPosition.getPositions();</span>
        }
<span class="nc" id="L102">      } catch (IOException e) {</span>
        // do nothing
<span class="nc" id="L104">      }</span>
<span class="nc" id="L105">      int start = super.startPosition();</span>
<span class="nc" id="L106">      int end = super.endPosition();</span>
<span class="nc" id="L107">      list = new int[end - start];</span>
<span class="nc bnc" id="L108" title="All 2 branches missed.">      for (int i = start; i &lt; end; i++)</span>
<span class="nc" id="L109">        list[i - start] = i;</span>
<span class="nc" id="L110">      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="L120" title="1 of 2 branches missed.">    if (!readPayload) {</span>
<span class="fc" id="L121">      payloadSpanCollector.reset();</span>
<span class="fc" id="L122">      collect(payloadSpanCollector);</span>
<span class="fc" id="L123">      Collection&lt;byte[]&gt; originalPayloadCollection = payloadSpanCollector</span>
<span class="fc" id="L124">          .getPayloads();</span>
<span class="pc bpc" id="L125" title="1 of 2 branches missed.">      if (originalPayloadCollection.iterator().hasNext()) {</span>
<span class="fc" id="L126">        byte[] payload = originalPayloadCollection.iterator().next();</span>
<span class="pc bpc" id="L127" title="1 of 2 branches missed.">        if (payload == null) {</span>
<span class="nc" id="L128">          throw new IOException(&quot;no payload&quot;);</span>
        }
<span class="fc" id="L130">        MtasPayloadDecoder payloadDecoder = new MtasPayloadDecoder();</span>
<span class="fc" id="L131">        payloadDecoder.init(startPosition(), payload);</span>
<span class="fc" id="L132">        mtasPosition = payloadDecoder.getMtasPosition();</span>
<span class="fc" id="L133">      } else {</span>
<span class="nc" id="L134">        throw new IOException(&quot;no payload&quot;);</span>
      }
    }
<span class="fc" id="L137">  }</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>