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

import java.io.IOException;

import mtas.analysis.token.MtasTokenString;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.FieldsConsumer;
import org.apache.lucene.codecs.FieldsProducer;
import org.apache.lucene.codecs.PostingsFormat;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SegmentWriteState;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.util.BytesRef;

/**
 * The Class MtasCodecPostingsFormat.
 */
public class MtasCodecPostingsFormat extends PostingsFormat {

  /** The Constant log. */
<span class="fc" id="L24">  private static final Log log = LogFactory</span>
<span class="fc" id="L25">      .getLog(MtasCodecPostingsFormat.class);</span>

  /** The Constant VERSION_START. */
  public static final int VERSION_START = 3;

  /** The Constant VERSION_CURRENT. */
  public static final int VERSION_CURRENT = 3;

  /** The Constant MTAS_OBJECT_HAS_PARENT. */
  static final int MTAS_OBJECT_HAS_PARENT = 1;

  /** The Constant MTAS_OBJECT_HAS_POSITION_RANGE. */
  static final int MTAS_OBJECT_HAS_POSITION_RANGE = 2;

  /** The Constant MTAS_OBJECT_HAS_POSITION_SET. */
  static final int MTAS_OBJECT_HAS_POSITION_SET = 4;

  /** The Constant MTAS_OBJECT_HAS_OFFSET. */
  static final int MTAS_OBJECT_HAS_OFFSET = 8;

  /** The Constant MTAS_OBJECT_HAS_REALOFFSET. */
  static final int MTAS_OBJECT_HAS_REALOFFSET = 16;

  /** The Constant MTAS_OBJECT_HAS_PAYLOAD. */
  static final int MTAS_OBJECT_HAS_PAYLOAD = 32;

  /** The Constant MTAS_STORAGE_BYTE. */
  public static final int MTAS_STORAGE_BYTE = 0;

  /** The Constant MTAS_STORAGE_SHORT. */
  public static final int MTAS_STORAGE_SHORT = 1;

  /** The Constant MTAS_STORAGE_INTEGER. */
  public static final int MTAS_STORAGE_INTEGER = 2;

  /** The Constant MTAS_STORAGE_LONG. */
  public static final int MTAS_STORAGE_LONG = 3;

  /** The Constant MTAS_TMP_FIELD_EXTENSION. */
  public static final String MTAS_TMP_FIELD_EXTENSION = &quot;mtas.field.temporary&quot;;

  /** The Constant MTAS_TMP_OBJECT_EXTENSION. */
  public static final String MTAS_TMP_OBJECT_EXTENSION = &quot;mtas.object.temporary&quot;;

  /** The Constant MTAS_TMP_DOCS_EXTENSION. */
  public static final String MTAS_TMP_DOCS_EXTENSION = &quot;mtas.docs.temporary&quot;;

  /** The Constant MTAS_TMP_DOC_EXTENSION. */
  public static final String MTAS_TMP_DOC_EXTENSION = &quot;mtas.doc.temporary&quot;;

  /** The Constant MTAS_TMP_DOCS_CHAINED_EXTENSION. */
  public static final String MTAS_TMP_DOCS_CHAINED_EXTENSION = &quot;mtas.docs.chained.temporary&quot;;

  /** The Constant MTAS_FIELDINFO_ATTRIBUTE_PREFIX_SINGLE_POSITION. */
  public static final String MTAS_FIELDINFO_ATTRIBUTE_PREFIX_SINGLE_POSITION = &quot;mtas.prefix.single.position&quot;;

  /** The Constant MTAS_FIELDINFO_ATTRIBUTE_PREFIX_MULTIPLE_POSITION. */
  public static final String MTAS_FIELDINFO_ATTRIBUTE_PREFIX_MULTIPLE_POSITION = &quot;mtas.prefix.multiple.position&quot;;

  /** The Constant MTAS_FIELDINFO_ATTRIBUTE_PREFIX_SET_POSITION. */
  public static final String MTAS_FIELDINFO_ATTRIBUTE_PREFIX_SET_POSITION = &quot;mtas.prefix.set.position&quot;;

  /** The Constant MTAS_FIELDINFO_ATTRIBUTE_PREFIX_INTERSECTION. */
  public static final String MTAS_FIELDINFO_ATTRIBUTE_PREFIX_INTERSECTION = &quot;mtas.prefix.intersection&quot;;

  /** The Constant MTAS_OBJECT_EXTENSION. */
  public static final String MTAS_OBJECT_EXTENSION = &quot;mtas.object&quot;;

  /** The Constant MTAS_TERM_EXTENSION. */
  public static final String MTAS_TERM_EXTENSION = &quot;mtas.term&quot;;

  /** The Constant MTAS_FIELD_EXTENSION. */
  public static final String MTAS_FIELD_EXTENSION = &quot;mtas.field&quot;;

  /** The Constant MTAS_PREFIX_EXTENSION. */
  public static final String MTAS_PREFIX_EXTENSION = &quot;mtas.prefix&quot;;

  /** The Constant MTAS_DOC_EXTENSION. */
  public static final String MTAS_DOC_EXTENSION = &quot;mtas.doc&quot;;

  /** The Constant MTAS_INDEX_DOC_ID_EXTENSION. */
  public static final String MTAS_INDEX_DOC_ID_EXTENSION = &quot;mtas.index.doc.id&quot;;

  /** The Constant MTAS_INDEX_OBJECT_ID_EXTENSION. */
  public static final String MTAS_INDEX_OBJECT_ID_EXTENSION = &quot;mtas.index.object.id&quot;;

  /** The Constant MTAS_INDEX_OBJECT_POSITION_EXTENSION. */
  public static final String MTAS_INDEX_OBJECT_POSITION_EXTENSION = &quot;mtas.index.object.position&quot;;

  /** The Constant MTAS_INDEX_OBJECT_PARENT_EXTENSION. */
  public static final String MTAS_INDEX_OBJECT_PARENT_EXTENSION = &quot;mtas.index.object.parent&quot;;

  /** The Constant MTAS_INDEX_TERM_PREFIX_POSITION_EXTENSION. */
  public static final String MTAS_INDEX_TERM_PREFIX_POSITION_EXTENSION = &quot;mtas.index.term.prefix.position&quot;;

  /** The delegate codec name. */
<span class="fc" id="L121">  private String delegateCodecName = null;</span>

  /** The delegate postings format. */
<span class="fc" id="L124">  private PostingsFormat delegatePostingsFormat = null;</span>

  /**
   * Instantiates a new mtas codec postings format.
   */
  public MtasCodecPostingsFormat() {
<span class="fc" id="L130">    this(MtasCodec.MTAS_CODEC_NAME);</span>
<span class="fc" id="L131">  }</span>

  /**
   * Instantiates a new mtas codec postings format.
   *
   * @param delegate the delegate
   */
  public MtasCodecPostingsFormat(PostingsFormat delegate) {
<span class="fc" id="L139">    super(MtasCodec.MTAS_CODEC_NAME);</span>
<span class="fc" id="L140">    delegateCodecName = delegate.getName();</span>
<span class="fc" id="L141">    delegatePostingsFormat = delegate;</span>
    // preload to prevent NoClassDefFoundErrors
    try {
<span class="fc" id="L144">      Class.forName(&quot;mtas.codec.payload.MtasPayloadDecoder&quot;);</span>
<span class="fc" id="L145">      Class.forName(&quot;mtas.codec.payload.MtasBitInputStream&quot;);</span>
<span class="fc" id="L146">      Class.forName(&quot;mtas.analysis.token.MtasPosition&quot;);</span>
<span class="fc" id="L147">      Class.forName(&quot;mtas.analysis.token.MtasOffset&quot;);</span>
<span class="fc" id="L148">      Class.forName(&quot;mtas.codec.tree.MtasRBTree&quot;);</span>
<span class="fc" id="L149">      Class.forName(&quot;mtas.codec.MtasTerms&quot;);</span>
<span class="fc" id="L150">      Class.forName(&quot;mtas.codec.util.CodecInfo&quot;);</span>
<span class="fc" id="L151">      Class.forName(&quot;mtas.codec.tree.MtasTreeNodeId&quot;);</span>
<span class="nc" id="L152">    } catch (ClassNotFoundException e) {</span>
<span class="nc" id="L153">      log.error(e);</span>
<span class="fc" id="L154">    }</span>
<span class="fc" id="L155">  }</span>

  /**
   * Instantiates a new mtas codec postings format.
   *
   * @param codecName the codec name
   */
  public MtasCodecPostingsFormat(String codecName) {
<span class="fc" id="L163">    super(codecName);</span>
<span class="fc" id="L164">    delegateCodecName = codecName;</span>
<span class="fc" id="L165">    delegatePostingsFormat = null;</span>
    // preload to prevent NoClassDefFoundErrors
    try {
<span class="fc" id="L168">      Class.forName(&quot;mtas.codec.payload.MtasPayloadDecoder&quot;);</span>
<span class="fc" id="L169">      Class.forName(&quot;mtas.codec.payload.MtasBitInputStream&quot;);</span>
<span class="fc" id="L170">      Class.forName(&quot;mtas.analysis.token.MtasPosition&quot;);</span>
<span class="fc" id="L171">      Class.forName(&quot;mtas.analysis.token.MtasOffset&quot;);</span>
<span class="fc" id="L172">      Class.forName(&quot;mtas.codec.tree.MtasRBTree&quot;);</span>
<span class="fc" id="L173">      Class.forName(&quot;mtas.codec.MtasTerms&quot;);</span>
<span class="fc" id="L174">      Class.forName(&quot;mtas.codec.util.CodecInfo&quot;);</span>
<span class="fc" id="L175">      Class.forName(&quot;mtas.codec.tree.MtasTreeNodeId&quot;);</span>
<span class="nc" id="L176">    } catch (ClassNotFoundException e) {</span>
<span class="nc" id="L177">      log.error(e);</span>
<span class="fc" id="L178">    }</span>
<span class="fc" id="L179">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.codecs.PostingsFormat#fieldsProducer(org.apache.lucene
   * .index.SegmentReadState)
   */
  @Override
  public final FieldsProducer fieldsProducer(SegmentReadState state)
      throws IOException {
<span class="fc" id="L191">    return new MtasFieldsProducer(state, getName());</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.codecs.PostingsFormat#fieldsConsumer(org.apache.lucene
   * .index.SegmentWriteState)
   */
  @Override
  public final FieldsConsumer fieldsConsumer(SegmentWriteState state)
      throws IOException {
<span class="fc bfc" id="L204" title="All 2 branches covered.">    if (delegatePostingsFormat != null) {</span>
<span class="fc" id="L205">      return new MtasFieldsConsumer(</span>
<span class="fc" id="L206">          delegatePostingsFormat.fieldsConsumer(state), state, getName(),</span>
<span class="fc" id="L207">          delegatePostingsFormat.getName());</span>
    } else {
<span class="fc" id="L209">      PostingsFormat pf = Codec.forName(delegateCodecName).postingsFormat();</span>
<span class="fc" id="L210">      return pf.fieldsConsumer(state);</span>
    }
  }

  /**
   * Gets the token.
   *
   * @param inObject the in object
   * @param inTerm the in term
   * @param ref the ref
   * @return the token
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static MtasTokenString getToken(IndexInput inObject, IndexInput inTerm,
      Long ref) throws IOException {
<span class="fc" id="L225">    MtasTokenString token = null;</span>
    try {
<span class="fc" id="L227">      inObject.seek(ref);</span>
<span class="fc" id="L228">      token = new MtasTokenString(null, &quot;&quot;);</span>
<span class="fc" id="L229">      token.setId(inObject.readVInt());</span>
<span class="fc" id="L230">      token.setTokenRef(ref);</span>
<span class="fc" id="L231">      int objectFlags = inObject.readVInt();</span>
<span class="fc" id="L232">      int[] positions = null;</span>
<span class="fc bfc" id="L233" title="All 2 branches covered.">      if ((objectFlags &amp; MTAS_OBJECT_HAS_PARENT) == MTAS_OBJECT_HAS_PARENT) {</span>
<span class="fc" id="L234">        int parentId = inObject.readVInt();</span>
<span class="fc" id="L235">        token.setParentId(parentId);</span>
      }
<span class="fc bfc" id="L237" title="All 2 branches covered.">      if ((objectFlags</span>
          &amp; MTAS_OBJECT_HAS_POSITION_RANGE) == MTAS_OBJECT_HAS_POSITION_RANGE) {
<span class="fc" id="L239">        int positionStart = inObject.readVInt();</span>
<span class="fc" id="L240">        int positionEnd = positionStart + inObject.readVInt();</span>
<span class="fc" id="L241">        token.addPositionRange(positionStart, positionEnd);</span>
<span class="fc bfc" id="L242" title="All 2 branches covered.">      } else if ((objectFlags</span>
          &amp; MTAS_OBJECT_HAS_POSITION_SET) == MTAS_OBJECT_HAS_POSITION_SET) {
<span class="fc" id="L244">        int size = inObject.readVInt();</span>
<span class="fc" id="L245">        int tmpPrevious = 0;</span>
<span class="fc" id="L246">        positions = new int[size];</span>
<span class="fc bfc" id="L247" title="All 2 branches covered.">        for (int t = 0; t &lt; size; t++) {</span>
<span class="fc" id="L248">          int position = tmpPrevious + inObject.readVInt();</span>
<span class="fc" id="L249">          tmpPrevious = position;</span>
<span class="fc" id="L250">          positions[t] = position;</span>
        }
<span class="fc" id="L252">        token.addPositions(positions);</span>
<span class="fc" id="L253">      } else {</span>
<span class="fc" id="L254">        int position = inObject.readVInt();</span>
<span class="fc" id="L255">        token.addPosition(position);</span>
      }
<span class="pc bpc" id="L257" title="1 of 2 branches missed.">      if ((objectFlags &amp; MTAS_OBJECT_HAS_OFFSET) == MTAS_OBJECT_HAS_OFFSET) {</span>
<span class="nc" id="L258">        int offsetStart = inObject.readVInt();</span>
<span class="nc" id="L259">        int offsetEnd = offsetStart + inObject.readVInt();</span>
<span class="nc" id="L260">        token.setOffset(offsetStart, offsetEnd);</span>
      }
<span class="pc bpc" id="L262" title="1 of 2 branches missed.">      if ((objectFlags</span>
          &amp; MTAS_OBJECT_HAS_REALOFFSET) == MTAS_OBJECT_HAS_REALOFFSET) {
<span class="nc" id="L264">        int realOffsetStart = inObject.readVInt();</span>
<span class="nc" id="L265">        int realOffsetEnd = realOffsetStart + inObject.readVInt();</span>
<span class="nc" id="L266">        token.setRealOffset(realOffsetStart, realOffsetEnd);</span>
      }
<span class="pc bpc" id="L268" title="1 of 2 branches missed.">      if ((objectFlags &amp; MTAS_OBJECT_HAS_PAYLOAD) == MTAS_OBJECT_HAS_PAYLOAD) {</span>
<span class="nc" id="L269">        int length = inObject.readVInt();</span>
<span class="nc" id="L270">        byte[] mtasPayload = new byte[length];</span>
<span class="nc" id="L271">        inObject.readBytes(mtasPayload, 0, length);</span>
<span class="nc" id="L272">        token.setPayload(new BytesRef(mtasPayload));</span>
      }
<span class="fc" id="L274">      Long termRef = inObject.readVLong();</span>
<span class="fc" id="L275">      inTerm.seek(termRef);</span>
<span class="fc" id="L276">      token.setTermRef(termRef);</span>
<span class="fc" id="L277">      token.setValue(inTerm.readString());</span>
<span class="nc" id="L278">    } catch (Exception e) {</span>
<span class="nc" id="L279">      throw new IOException(e);</span>
<span class="fc" id="L280">    }</span>
<span class="fc" id="L281">    return token;</span>
  }

  /**
   * Gets the term.
   *
   * @param inTerm the in term
   * @param ref the ref
   * @return the term
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static String getTerm(IndexInput inTerm, Long ref) throws IOException {
    try {
<span class="fc" id="L294">      inTerm.seek(ref);</span>
<span class="fc" id="L295">      return inTerm.readString();</span>
<span class="nc" id="L296">    } catch (Exception e) {</span>
<span class="nc" id="L297">      throw new IOException(e);</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>