MtasSolrMtasResult.java.html 22.7 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="../.resources/report.css" type="text/css"/><link rel="shortcut icon" href="../.resources/report.gif" type="image/gif"/><title>MtasSolrMtasResult.java</title><link rel="stylesheet" href="../.resources/prettify.css" type="text/css"/><script type="text/javascript" src="../.resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../.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.solr.handler.component.util</a> &gt; <span class="el_source">MtasSolrMtasResult.java</span></div><h1>MtasSolrMtasResult.java</h1><pre class="source lang-java linenums">package mtas.solr.handler.component.util;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import mtas.codec.util.DataCollector;
import mtas.codec.util.collector.MtasDataCollector;
import mtas.codec.util.collector.MtasDataCollectorResult;
import mtas.codec.util.collector.MtasDataItem;

/**
 * The Class MtasSolrResult.
 */
public class MtasSolrMtasResult implements Serializable {

  /** The Constant serialVersionUID. */
  private static final long serialVersionUID = 1L;

  /** The stats type. */
  public String dataType;
  public String statsType;

  /** The stats items. */
  public Set&lt;String&gt; statsItems;

  /** The sort direction. */
  public String sortType;
  public String sortDirection;

  /** The number. */
  public Integer start;
  public Integer number;

  /** The data collector. */
<span class="fc" id="L41">  public MtasDataCollector&lt;?, ?&gt; dataCollector = null;</span>

  /** The function data. */
  public Map&lt;MtasDataCollector&lt;?, ?&gt;, HashMap&lt;String, MtasSolrMtasResult&gt;&gt; functionData;

  /** The sub stats type. */
  private String[] subDataType;
  private String[] subStatsType;

  /** The sub stats items. */
  private Set&lt;String&gt;[] subStatsItems;

  /** The sub sort direction. */
  private String[] subSortType;
  private String[] subSortDirection;

  /** The sub number. */
  private Integer[] subStart;
  private Integer[] subNumber;

  /**
   * Instantiates a new mtas solr result.
   *
   * @param dataCollector
   *          the data collector
   * @param dataType
   *          the data type
   * @param statsType
   *          the stats type
   * @param statsItems
   *          the stats items
   * @param sortType
   *          the sort type
   * @param sortDirection
   *          the sort direction
   * @param start
   *          the start
   * @param number
   *          the number
   * @param functionData
   *          the function data
   */
  @SuppressWarnings(&quot;unchecked&quot;)
  public MtasSolrMtasResult(MtasDataCollector&lt;?, ?&gt; dataCollector,
      String[] dataType, String[] statsType, Set&lt;String&gt;[] statsItems,
      String[] sortType, String[] sortDirection, Integer[] start,
      Integer[] number,
<span class="fc" id="L88">      Map&lt;MtasDataCollector&lt;?, ?&gt;, HashMap&lt;String, MtasSolrMtasResult&gt;&gt; functionData) {</span>
<span class="fc" id="L89">    this.dataCollector = dataCollector;</span>
<span class="fc" id="L90">    this.functionData = functionData;</span>
<span class="pc bpc" id="L91" title="1 of 2 branches missed.">    this.dataType = (dataType == null) ? null : dataType[0];</span>
<span class="pc bpc" id="L92" title="1 of 2 branches missed.">    this.statsType = (statsType == null) ? null : statsType[0];</span>
<span class="pc bpc" id="L93" title="1 of 2 branches missed.">    this.statsItems = (statsItems == null) ? null : statsItems[0];</span>
<span class="pc bpc" id="L94" title="1 of 2 branches missed.">    this.sortType = (sortType == null) ? null : sortType[0];</span>
<span class="pc bpc" id="L95" title="1 of 2 branches missed.">    this.sortDirection = (sortDirection == null) ? null : sortDirection[0];</span>
<span class="pc bpc" id="L96" title="1 of 2 branches missed.">    this.start = (start == null) ? null : start[0];</span>
<span class="pc bpc" id="L97" title="1 of 2 branches missed.">    this.number = (number == null) ? null : number[0];</span>
<span class="fc" id="L98">    this.subStart = null;</span>
<span class="fc" id="L99">    this.subNumber = null;</span>
<span class="pc bpc" id="L100" title="2 of 4 branches missed.">    if ((dataType != null) &amp;&amp; (dataType.length &gt; 1)) {</span>
<span class="nc" id="L101">      subDataType = new String[dataType.length - 1];</span>
<span class="nc" id="L102">      subStatsType = new String[dataType.length - 1];</span>
<span class="nc" id="L103">      subStatsItems = new TreeSet[dataType.length - 1];</span>
<span class="nc" id="L104">      subSortType = new String[dataType.length - 1];</span>
<span class="nc" id="L105">      subSortDirection = new String[dataType.length - 1];</span>
<span class="nc" id="L106">      System.arraycopy(dataType, 1, subDataType, 0, dataType.length - 1);</span>
<span class="nc" id="L107">      System.arraycopy(statsType, 1, subStatsType, 0, dataType.length - 1);</span>
<span class="nc" id="L108">      System.arraycopy(statsItems, 1, subStatsItems, 0, dataType.length - 1);</span>
<span class="nc" id="L109">      System.arraycopy(sortType, 1, subSortType, 0, dataType.length - 1);</span>
<span class="nc" id="L110">      System.arraycopy(sortDirection, 1, subSortDirection, 0,</span>
          dataType.length - 1);
    } else {
<span class="fc" id="L113">      subDataType = null;</span>
<span class="fc" id="L114">      subStatsType = null;</span>
<span class="fc" id="L115">      subStatsItems = null;</span>
<span class="fc" id="L116">      subSortType = null;</span>
<span class="fc" id="L117">      subSortDirection = null;</span>
    }
<span class="fc" id="L119">  }</span>

  /**
   * Instantiates a new mtas solr result.
   *
   * @param dataCollector
   *          the data collector
   * @param dataType
   *          the data type
   * @param statsType
   *          the stats type
   * @param statsItems
   *          the stats items
   * @param functionData
   *          the function data
   */
  @SuppressWarnings(&quot;unchecked&quot;)
  public MtasSolrMtasResult(MtasDataCollector&lt;?, ?&gt; dataCollector, String dataType,
      String statsType, Set&lt;String&gt; statsItems,
      Map&lt;MtasDataCollector&lt;?, ?&gt;, HashMap&lt;String, MtasSolrMtasResult&gt;&gt; functionData) {
<span class="fc" id="L139">    this(dataCollector, new String[] { dataType }, new String[] { statsType },</span>
        new Set[] { statsItems }, new String[] { null },
<span class="fc" id="L141">        new String[] { null }, new Integer[] { 0 }, new Integer[] { 1 },</span>
        functionData);
<span class="fc" id="L143">  }</span>

  /**
   * Merge.
   *
   * @param newItem
   *          the new item
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  void merge(MtasSolrMtasResult newItem) throws IOException {
<span class="fc" id="L154">    HashMap&lt;MtasDataCollector&lt;?, ?&gt;, MtasDataCollector&lt;?, ?&gt;&gt; map = new HashMap&lt;&gt;();</span>
<span class="pc bpc" id="L155" title="1 of 2 branches missed.">    if(newItem.dataCollector.withTotal()) {</span>
<span class="nc" id="L156">      dataCollector.setWithTotal();</span>
    }
<span class="fc" id="L158">    dataCollector.merge(newItem.dataCollector, map, true);</span>
<span class="fc bfc" id="L159" title="All 2 branches covered.">    if (newItem.functionData != null) {</span>
<span class="pc bpc" id="L160" title="1 of 2 branches missed.">      if (functionData == null) {</span>
<span class="nc" id="L161">        functionData = new HashMap&lt;&gt;();</span>
      }
<span class="fc bfc" id="L163" title="All 2 branches covered.">      for (MtasDataCollector&lt;?, ?&gt; keyCollector : newItem.functionData</span>
<span class="fc" id="L164">          .keySet()) {</span>
<span class="pc bpc" id="L165" title="1 of 2 branches missed.">        if (map.containsKey(keyCollector)) {</span>
          // compute mapped key
<span class="fc" id="L167">          MtasDataCollector&lt;?, ?&gt; newKeyCollector = keyCollector;</span>
<span class="fc bfc" id="L168" title="All 2 branches covered.">          while (map.containsKey(newKeyCollector)) {</span>
<span class="fc" id="L169">            newKeyCollector = map.get(keyCollector);</span>
          }
<span class="pc bpc" id="L171" title="1 of 2 branches missed.">          if (functionData.containsKey(newKeyCollector)) {</span>
<span class="fc" id="L172">            HashMap&lt;String, MtasSolrMtasResult&gt; tmpList = functionData</span>
<span class="fc" id="L173">                .get(newKeyCollector);</span>
<span class="pc bpc" id="L174" title="1 of 2 branches missed.">            for (String functionKey : newItem.functionData.get(keyCollector)</span>
<span class="fc" id="L175">                .keySet()) {</span>
<span class="nc bnc" id="L176" title="All 2 branches missed.">              if (tmpList.containsKey(functionKey)) {</span>
<span class="nc" id="L177">                tmpList.get(functionKey).merge(</span>
<span class="nc" id="L178">                    newItem.functionData.get(keyCollector).get(functionKey));</span>
              } else {
<span class="nc" id="L180">                tmpList.put(functionKey,</span>
<span class="nc" id="L181">                    newItem.functionData.get(keyCollector).get(functionKey));</span>
              }
<span class="nc" id="L183">            }</span>
<span class="fc" id="L184">          } else {</span>
<span class="nc" id="L185">            functionData.put(newKeyCollector,</span>
<span class="nc" id="L186">                newItem.functionData.get(keyCollector));</span>
          }
        }
<span class="fc" id="L189">      }</span>
    }
<span class="fc" id="L191">  }</span>

  /**
   * Gets the data.
   *
   * @param showDebugInfo
   *          the show debug info
   * @return the data
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  NamedList&lt;Object&gt; getData(boolean showDebugInfo) throws IOException {
<span class="fc" id="L203">    if (dataCollector.getCollectorType()</span>
<span class="pc bpc" id="L204" title="1 of 2 branches missed.">        .equals(DataCollector.COLLECTOR_TYPE_DATA)) {</span>
<span class="fc" id="L205">      NamedList&lt;Object&gt; mtasResponse = new SimpleOrderedMap&lt;&gt;();</span>
      // functions
<span class="fc" id="L207">      Map&lt;String, NamedList&lt;Object&gt;&gt; functionList = new HashMap&lt;&gt;();</span>
<span class="pc bpc" id="L208" title="1 of 4 branches missed.">      if (functionData != null &amp;&amp; functionData.containsKey(dataCollector)) {</span>
<span class="fc" id="L209">        HashMap&lt;String, MtasSolrMtasResult&gt; functionDataItem = functionData</span>
<span class="fc" id="L210">            .get(dataCollector);</span>
<span class="pc bpc" id="L211" title="1 of 2 branches missed.">        for (Entry &lt;String, MtasSolrMtasResult&gt; entry : functionDataItem.entrySet()) {</span>
<span class="nc" id="L212">          MtasSolrMtasResult functionResult = entry.getValue();</span>
<span class="nc" id="L213">          if (functionResult.dataCollector.getCollectorType()</span>
<span class="nc bnc" id="L214" title="All 2 branches missed.">              .equals(DataCollector.COLLECTOR_TYPE_DATA)) {</span>
<span class="nc" id="L215">            NamedList&lt;Object&gt; functionData = functionResult</span>
<span class="nc" id="L216">                .getData(showDebugInfo);</span>
<span class="nc" id="L217">            functionList.put(entry.getKey(), functionData);</span>
<span class="nc" id="L218">          } else {</span>
<span class="nc" id="L219">            throw new IOException(&quot;unexpected function collectorType &quot;</span>
<span class="nc" id="L220">                + functionResult.dataCollector.getCollectorType());</span>
          }
<span class="nc" id="L222">        }</span>
      }
      // main result
<span class="fc" id="L225">      MtasDataItem&lt;?, ?&gt; dataItem = dataCollector.getResult().getData();</span>
<span class="pc bpc" id="L226" title="1 of 2 branches missed.">      if (dataItem != null) {</span>
<span class="fc" id="L227">        mtasResponse.addAll(dataItem.rewrite(showDebugInfo));</span>
<span class="pc bpc" id="L228" title="1 of 2 branches missed.">        if (functionList.size() &gt; 0) {</span>
<span class="nc" id="L229">          mtasResponse.add(&quot;functions&quot;, functionList);</span>
        }
<span class="pc bpc" id="L231" title="3 of 4 branches missed.">        if ((subDataType != null) &amp;&amp; (dataItem.getSub() != null)) {</span>
<span class="nc" id="L232">          MtasSolrMtasResult css = new MtasSolrMtasResult(dataItem.getSub(), subDataType,</span>
              subStatsType, subStatsItems, subSortType, subSortDirection,
              subStart, subNumber, functionData);
<span class="nc" id="L235">          if (dataItem.getSub().getCollectorType()</span>
<span class="nc bnc" id="L236" title="All 2 branches missed.">              .equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="nc" id="L237">            mtasResponse.add(dataItem.getSub().getCollectorType(),</span>
<span class="nc" id="L238">                css.getNamedList(showDebugInfo));</span>
<span class="nc" id="L239">          } else if (dataItem.getSub().getCollectorType()</span>
<span class="nc bnc" id="L240" title="All 2 branches missed.">              .equals(DataCollector.COLLECTOR_TYPE_DATA)) {</span>
<span class="nc" id="L241">            mtasResponse.add(dataItem.getSub().getCollectorType(),</span>
<span class="nc" id="L242">                css.getData(showDebugInfo));</span>
          }
        }
      }      
<span class="fc" id="L246">      return mtasResponse;</span>
    } else {
<span class="nc" id="L248">      throw new IOException(</span>
          &quot;only allowed for &quot; + DataCollector.COLLECTOR_TYPE_DATA);
    }
  }

  /**
   * Gets the key list.
   *
   * @return the key list
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  public Set&lt;String&gt; getKeyList() throws IOException {
<span class="nc" id="L261">    if (dataCollector.getCollectorType()</span>
<span class="nc bnc" id="L262" title="All 2 branches missed.">        .equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="nc" id="L263">      return dataCollector.getResult().getComparatorList().keySet();</span>
    } else {
<span class="nc" id="L265">      throw new IOException(</span>
          &quot;only allowed for &quot; + DataCollector.COLLECTOR_TYPE_LIST);
    }
  }

  /**
   * Gets the full key list.
   *
   * @return the full key list
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  public Set&lt;String&gt; getFullKeyList() throws IOException {
<span class="nc" id="L278">    if (dataCollector.getCollectorType()</span>
<span class="nc bnc" id="L279" title="All 2 branches missed.">        .equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="nc" id="L280">      return dataCollector.getKeyList();</span>
    } else {
<span class="nc" id="L282">      throw new IOException(</span>
          &quot;only allowed for &quot; + DataCollector.COLLECTOR_TYPE_LIST);
    }
  }

  /**
   * Gets the named list.
   *
   * @param showDebugInfo
   *          the show debug info
   * @return the named list
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  NamedList&lt;Object&gt; getNamedList(boolean showDebugInfo) throws IOException {
<span class="nc" id="L297">    if (dataCollector.getCollectorType()</span>
<span class="nc bnc" id="L298" title="All 2 branches missed.">        .equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="nc" id="L299">      SimpleOrderedMap&lt;Object&gt; mtasResponseList = new SimpleOrderedMap&lt;&gt;();</span>
      // functions
<span class="nc" id="L301">      Map&lt;String, SimpleOrderedMap&lt;Object&gt;&gt; functionList = new HashMap&lt;&gt;();</span>
<span class="nc bnc" id="L302" title="All 4 branches missed.">      if (functionData != null &amp;&amp; functionData.containsKey(dataCollector)) {</span>
<span class="nc" id="L303">        HashMap&lt;String, MtasSolrMtasResult&gt; functionDataItem = functionData</span>
<span class="nc" id="L304">            .get(dataCollector);</span>
<span class="nc bnc" id="L305" title="All 2 branches missed.">        for (Entry&lt;String, MtasSolrMtasResult&gt; entry : functionDataItem.entrySet()) {</span>
<span class="nc" id="L306">          MtasSolrMtasResult functionResult = entry.getValue();</span>
<span class="nc" id="L307">          if (functionResult.dataCollector.getCollectorType()</span>
<span class="nc bnc" id="L308" title="All 2 branches missed.">              .equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="nc" id="L309">            NamedList&lt;Object&gt; functionNamedList = functionResult</span>
<span class="nc" id="L310">                .getNamedList(showDebugInfo);</span>
<span class="nc bnc" id="L311" title="All 2 branches missed.">            for (int i = 0; i &lt; functionNamedList.size(); i++) {</span>
<span class="nc bnc" id="L312" title="All 2 branches missed.">              if (functionList.containsKey(functionNamedList.getName(i))) {</span>
<span class="nc" id="L313">                SimpleOrderedMap&lt;Object&gt; tmpMap = functionList</span>
<span class="nc" id="L314">                    .get(functionNamedList.getName(i));</span>
<span class="nc" id="L315">                tmpMap.add(entry.getKey(), functionNamedList.getVal(i));</span>
<span class="nc" id="L316">              } else {</span>
<span class="nc" id="L317">                SimpleOrderedMap&lt;Object&gt; tmpMap = new SimpleOrderedMap&lt;&gt;();</span>
<span class="nc" id="L318">                tmpMap.add(entry.getKey(), functionNamedList.getVal(i));</span>
<span class="nc" id="L319">                functionList.put(functionNamedList.getName(i), tmpMap);</span>
              }
            }
<span class="nc" id="L322">          } else {</span>
<span class="nc" id="L323">            throw new IOException(&quot;unexpected function collectorType &quot;</span>
<span class="nc" id="L324">                + functionResult.dataCollector.getCollectorType());</span>
          }
<span class="nc" id="L326">        }</span>
      }
      // main result
<span class="nc" id="L329">      Map&lt;String, ?&gt; dataList = dataCollector.getResult().getList();</span>
<span class="nc bnc" id="L330" title="All 2 branches missed.">      for (Entry&lt;String, ?&gt; entry : dataList.entrySet()) {</span>
<span class="nc" id="L331">        SimpleOrderedMap&lt;Object&gt; mtasResponseListItem = new SimpleOrderedMap&lt;&gt;();</span>
<span class="nc" id="L332">        MtasDataItem&lt;?, ?&gt; dataItem = (MtasDataItem&lt;?, ?&gt;) entry.getValue();</span>
<span class="nc" id="L333">        mtasResponseListItem.addAll(dataItem.rewrite(showDebugInfo));</span>
<span class="nc bnc" id="L334" title="All 2 branches missed.">        if (functionList.containsKey(entry.getKey())) {</span>
<span class="nc" id="L335">          mtasResponseListItem.add(&quot;functions&quot;,</span>
<span class="nc" id="L336">              functionList.get(entry.getKey()));</span>
        }
<span class="nc bnc" id="L338" title="All 4 branches missed.">        if ((subDataType != null) &amp;&amp; (dataItem.getSub() != null)) {</span>
<span class="nc" id="L339">          MtasSolrMtasResult css = new MtasSolrMtasResult(dataItem.getSub(),</span>
              subDataType, subStatsType, subStatsItems, subSortType,
              subSortDirection, subStart, subNumber, functionData);
<span class="nc" id="L342">          if (dataItem.getSub().getCollectorType()</span>
<span class="nc bnc" id="L343" title="All 2 branches missed.">              .equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="nc bnc" id="L344" title="All 2 branches missed.">            if (css.dataCollector.withTotal()) {</span>
<span class="nc" id="L345">              mtasResponseListItem.add(</span>
                  DataCollector.COLLECTOR_TYPE_LIST + &quot;Total&quot;,
<span class="nc" id="L347">                  css.dataCollector.getSize());</span>
            }
<span class="nc" id="L349">            mtasResponseListItem.add(DataCollector.COLLECTOR_TYPE_LIST,</span>
<span class="nc" id="L350">                css.getNamedList(showDebugInfo));</span>
<span class="nc" id="L351">          } else if (dataItem.getSub().getCollectorType()</span>
<span class="nc bnc" id="L352" title="All 2 branches missed.">              .equals(DataCollector.COLLECTOR_TYPE_DATA)) {</span>
<span class="nc" id="L353">            mtasResponseListItem.add(DataCollector.COLLECTOR_TYPE_DATA,</span>
<span class="nc" id="L354">                css.getData(showDebugInfo));</span>
          }
        }
<span class="nc" id="L357">        mtasResponseList.add(entry.getKey(), mtasResponseListItem);</span>
<span class="nc" id="L358">      }</span>
<span class="nc" id="L359">      return mtasResponseList;</span>
    } else {
<span class="nc" id="L361">      throw new IOException(</span>
          &quot;only allowed for &quot; + DataCollector.COLLECTOR_TYPE_LIST);
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
<span class="nc" id="L373">    if (dataCollector.getCollectorType()</span>
<span class="nc bnc" id="L374" title="All 2 branches missed.">        .equals(DataCollector.COLLECTOR_TYPE_DATA)) {</span>
<span class="nc" id="L375">      return &quot;MtasSolrResult(data-&quot; + hashCode() + &quot;)&quot;;</span>
    }
<span class="nc" id="L377">    if (dataCollector.getCollectorType()</span>
<span class="nc bnc" id="L378" title="All 2 branches missed.">        .equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="nc" id="L379">      return &quot;MtasSolrResult(list(&quot; + dataCollector.getSize() + &quot;)-&quot;</span>
<span class="nc" id="L380">          + hashCode() + &quot;)&quot;;</span>
    } else {
<span class="nc" id="L382">      return &quot;MtasSolrResult: unknown&quot;;</span>
    }
  }

  /**
   * Gets the result.
   *
   * @return the result
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  public MtasDataCollectorResult getResult() throws IOException {
<span class="nc" id="L394">    return dataCollector.getResult();</span>
  }

}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.5.201505241946</span></div></body></html>