MtasSolrCollectionResult.java.html 19.2 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>MtasSolrCollectionResult.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.solr.handler.component.util</a> &gt; <span class="el_source">MtasSolrCollectionResult.java</span></div><h1>MtasSolrCollectionResult.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.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.apache.solr.common.util.SimpleOrderedMap;

import mtas.codec.util.CodecComponent.ComponentCollection;
import mtas.solr.handler.component.MtasSolrSearchComponent;

/**
 * The Class MtasSolrCollectionResult.
 */
public class MtasSolrCollectionResult implements Serializable {

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

  /** The values. */
  private HashSet&lt;String&gt; values;

  /** The id. */
  private String id;

  /** The action. */
  private String action;

  /** The now. */
  private Long now;

  /** The list. */
  private List&lt;SimpleOrderedMap&lt;Object&gt;&gt; list;

  /** The status. */
  public SimpleOrderedMap&lt;Object&gt; status;

  /** The component collection. */
<span class="fc" id="L43">  private transient ComponentCollection componentCollection = null;</span>

  /**
   * Instantiates a new mtas solr collection result.
   *
   * @param componentCollection the component collection
   * @throws IOException Signals that an I/O exception has occurred.
   */
<span class="fc" id="L51">  public MtasSolrCollectionResult(ComponentCollection componentCollection)</span>
      throws IOException {
<span class="fc" id="L53">    this.componentCollection = componentCollection;</span>
<span class="pc bpc" id="L54" title="1 of 2 branches missed.">    if (componentCollection != null) {</span>
<span class="fc" id="L55">      action = componentCollection.action();</span>
<span class="fc" id="L56">      id = null;</span>
<span class="fc" id="L57">      values = null;</span>
<span class="fc" id="L58">      now = null;</span>
<span class="fc" id="L59">      list = null;</span>
<span class="pc bpc" id="L60" title="15 of 25 branches missed.">      switch (action) {</span>
      case ComponentCollection.ACTION_CREATE:
<span class="fc" id="L62">        values = componentCollection.values();</span>
<span class="fc" id="L63">        id = componentCollection.id;</span>
<span class="fc" id="L64">        break;</span>
      case ComponentCollection.ACTION_CHECK:
      case ComponentCollection.ACTION_GET:
      case ComponentCollection.ACTION_DELETE:
<span class="fc" id="L68">        id = componentCollection.id;</span>
<span class="fc" id="L69">        break;</span>
      case ComponentCollection.ACTION_POST:
      case ComponentCollection.ACTION_IMPORT:
<span class="fc" id="L72">        id = componentCollection.id;</span>
<span class="fc" id="L73">        values = componentCollection.values();</span>
<span class="fc" id="L74">        break;</span>
      case ComponentCollection.ACTION_LIST:
      case ComponentCollection.ACTION_EMPTY:
        // do nothing
        break;
      default:
<span class="nc" id="L80">        throw new IOException(&quot;action &quot; + action + &quot; not allowed&quot;);</span>
      }
<span class="nc" id="L82">    } else {</span>
<span class="nc" id="L83">      throw new IOException(&quot;no componentCollection available&quot;);</span>
    }
<span class="fc" id="L85">  }</span>

  /**
   * Sets the list.
   *
   * @param now the now
   * @param list the list
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public void setList(long now, List&lt;SimpleOrderedMap&lt;Object&gt;&gt; list)
      throws IOException {
<span class="pc bpc" id="L96" title="1 of 2 branches missed.">    if (action.equals(ComponentCollection.ACTION_LIST)) {</span>
<span class="fc" id="L97">      this.now = now;</span>
<span class="fc" id="L98">      this.list = list;</span>
<span class="fc" id="L99">    } else {</span>
<span class="nc" id="L100">      throw new IOException(&quot;not allowed with action '&quot; + action + &quot;'&quot;);</span>
    }
<span class="fc" id="L102">  }</span>

  /**
   * Sets the check.
   *
   * @param now the now
   * @param status the status
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public void setCheck(long now, SimpleOrderedMap&lt;Object&gt; status)
      throws IOException {
<span class="pc bpc" id="L113" title="1 of 2 branches missed.">    if (action.equals(ComponentCollection.ACTION_CHECK)) {</span>
<span class="fc" id="L114">      this.now = now;</span>
<span class="fc" id="L115">      this.status = status;</span>
<span class="fc" id="L116">    } else {</span>
<span class="nc" id="L117">      throw new IOException(&quot;not allowed with action '&quot; + action + &quot;'&quot;);</span>
    }
<span class="fc" id="L119">  }</span>

  /**
   * Sets the get.
   *
   * @param now the now
   * @param status the status
   * @param stringValues the string values
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public void setGet(long now, SimpleOrderedMap&lt;Object&gt; status,
      HashSet&lt;String&gt; stringValues) throws IOException {
<span class="nc bnc" id="L131" title="All 2 branches missed.">    if (action.equals(ComponentCollection.ACTION_GET)) {</span>
<span class="nc" id="L132">      this.now = now;</span>
<span class="nc" id="L133">      this.status = status;</span>
<span class="nc" id="L134">      this.values = stringValues;</span>
<span class="nc" id="L135">    } else {</span>
<span class="nc" id="L136">      throw new IOException(&quot;not allowed with action '&quot; + action + &quot;'&quot;);</span>
    }
<span class="nc" id="L138">  }</span>

  /**
   * Sets the post.
   *
   * @param now the now
   * @param status the status
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public void setPost(long now, SimpleOrderedMap&lt;Object&gt; status)
      throws IOException {
<span class="pc bpc" id="L149" title="1 of 2 branches missed.">    if (action.equals(ComponentCollection.ACTION_POST)) {</span>
<span class="fc" id="L150">      this.now = now;</span>
<span class="fc" id="L151">      this.status = status;</span>
<span class="fc" id="L152">    } else {</span>
<span class="nc" id="L153">      throw new IOException(&quot;not allowed with action '&quot; + action + &quot;'&quot;);</span>
    }
<span class="fc" id="L155">  }</span>

  /**
   * Sets the import.
   *
   * @param now the now
   * @param status the status
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public void setImport(long now, SimpleOrderedMap&lt;Object&gt; status)
      throws IOException {
<span class="nc bnc" id="L166" title="All 2 branches missed.">    if (action.equals(ComponentCollection.ACTION_IMPORT)) {</span>
<span class="nc" id="L167">      this.now = now;</span>
<span class="nc" id="L168">      this.status = status;</span>
<span class="nc" id="L169">    } else {</span>
<span class="nc" id="L170">      throw new IOException(&quot;not allowed with action '&quot; + action + &quot;'&quot;);</span>
    }
<span class="nc" id="L172">  }</span>

  /**
   * Sets the create.
   *
   * @param now the now
   * @param status the status
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public void setCreate(long now, SimpleOrderedMap&lt;Object&gt; status)
      throws IOException {
<span class="pc bpc" id="L183" title="1 of 2 branches missed.">    if (action.equals(ComponentCollection.ACTION_CREATE)) {</span>
<span class="fc" id="L184">      this.now = now;</span>
<span class="fc" id="L185">      this.status = status;</span>
<span class="fc" id="L186">    } else {</span>
<span class="nc" id="L187">      throw new IOException(&quot;not allowed with action '&quot; + action + &quot;'&quot;);</span>
    }
<span class="fc" id="L189">  }</span>

  /**
   * Id.
   *
   * @return the string
   */
  public String id() {
<span class="fc" id="L197">    return id;</span>
  }

  /**
   * Action.
   *
   * @return the string
   */
  public String action() {
<span class="fc" id="L206">    return action;</span>
  }

  /**
   * Rewrite.
   *
   * @param searchComponent the search component
   * @return the simple ordered map
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public SimpleOrderedMap&lt;Object&gt; rewrite(
      MtasSolrSearchComponent searchComponent) throws IOException {
<span class="fc" id="L218">    SimpleOrderedMap&lt;Object&gt; response = new SimpleOrderedMap&lt;&gt;();</span>
    Iterator&lt;Entry&lt;String, Object&gt;&gt; it;
<span class="pc bpc" id="L220" title="10 of 19 branches missed.">    switch (action) {</span>
    case ComponentCollection.ACTION_LIST:
<span class="fc" id="L222">      response.add(&quot;now&quot;, now);</span>
<span class="fc" id="L223">      response.add(&quot;list&quot;, list);</span>
<span class="fc" id="L224">      break;</span>
    case ComponentCollection.ACTION_CREATE:
    case ComponentCollection.ACTION_POST:
    case ComponentCollection.ACTION_IMPORT:
<span class="pc bpc" id="L228" title="2 of 4 branches missed.">      if (componentCollection != null &amp;&amp; status != null) {</span>
<span class="fc" id="L229">        it = status.iterator();</span>
<span class="fc bfc" id="L230" title="All 2 branches covered.">        while (it.hasNext()) {</span>
<span class="fc" id="L231">          Entry&lt;String, Object&gt; entry = it.next();</span>
<span class="fc" id="L232">          response.add(entry.getKey(), entry.getValue());</span>
        }
      }
<span class="fc" id="L235">      break;</span>
    case ComponentCollection.ACTION_CHECK:
<span class="fc bfc" id="L237" title="All 2 branches covered.">      if (status != null) {</span>
<span class="fc" id="L238">        it = status.iterator();</span>
<span class="fc bfc" id="L239" title="All 2 branches covered.">        while (it.hasNext()) {</span>
<span class="fc" id="L240">          Entry&lt;String, Object&gt; entry = it.next();</span>
<span class="fc" id="L241">          response.add(entry.getKey(), entry.getValue());</span>
        }
      }
<span class="fc" id="L244">      break;</span>
    case ComponentCollection.ACTION_GET:
<span class="nc bnc" id="L246" title="All 2 branches missed.">      if (status != null) {</span>
<span class="nc" id="L247">        it = status.iterator();</span>
<span class="nc bnc" id="L248" title="All 2 branches missed.">        while (it.hasNext()) {</span>
<span class="nc" id="L249">          Entry&lt;String, Object&gt; entry = it.next();</span>
<span class="nc" id="L250">          response.add(entry.getKey(), entry.getValue());</span>
        }
      }
<span class="nc bnc" id="L253" title="All 2 branches missed.">      if (values != null) {</span>
<span class="nc" id="L254">        response.add(&quot;values&quot;, values);</span>
      }
<span class="nc" id="L256">      break;</span>
    default:
      break;
    }
<span class="fc" id="L260">    return response;</span>
  }

  /**
   * Merge.
   *
   * @param newItem the new item
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public void merge(MtasSolrCollectionResult newItem) throws IOException {
<span class="pc bpc" id="L270" title="2 of 4 branches missed.">    if (action != null &amp;&amp; newItem.action != null) {</span>
<span class="fc bfc" id="L271" title="All 2 branches covered.">      if (action.equals(ComponentCollection.ACTION_CREATE)</span>
<span class="fc bfc" id="L272" title="All 2 branches covered.">          &amp;&amp; newItem.action.equals(ComponentCollection.ACTION_CREATE)) {</span>
<span class="fc" id="L273">        values.addAll(newItem.values);</span>
<span class="pc bpc" id="L274" title="3 of 6 branches missed.">        if (id != null &amp;&amp; (newItem.id == null || !newItem.id.equals(id))) {</span>
<span class="nc" id="L275">          id = null;</span>
        }
<span class="pc bfc" id="L277" title="All 2 branches covered.">      } else if (action.equals(ComponentCollection.ACTION_LIST)) {</span>
<span class="pc bpc" id="L278" title="1 of 2 branches missed.">        if (list != null) {</span>
<span class="fc" id="L279">          HashMap&lt;String, SimpleOrderedMap&lt;Object&gt;&gt; index = new HashMap&lt;&gt;();</span>
<span class="fc bfc" id="L280" title="All 2 branches covered.">          for (SimpleOrderedMap&lt;Object&gt; item : list) {</span>
<span class="pc bpc" id="L281" title="2 of 4 branches missed.">            if (item.get(&quot;id&quot;) != null &amp;&amp; item.get(&quot;id&quot;) instanceof String) {</span>
<span class="fc" id="L282">              index.put((String) item.get(&quot;id&quot;), item);</span>
<span class="fc bfc" id="L283" title="All 2 branches covered.">              if (item.get(&quot;shards&quot;) == null</span>
<span class="pc bpc" id="L284" title="1 of 2 branches missed.">                  || !(item.get(&quot;shards&quot;) instanceof List)) {</span>
<span class="fc" id="L285">                item.add(&quot;shards&quot;, new ArrayList&lt;&gt;());</span>
              }
            }
          }
<span class="fc bfc" id="L289" title="All 2 branches covered.">          for (SimpleOrderedMap&lt;Object&gt; item : newItem.list) {</span>
<span class="pc bpc" id="L290" title="2 of 4 branches missed.">            if (item.get(&quot;id&quot;) != null &amp;&amp; item.get(&quot;id&quot;) instanceof String) {</span>
<span class="fc" id="L291">              String id = (String) item.get(&quot;id&quot;);</span>
<span class="pc bpc" id="L292" title="1 of 2 branches missed.">              if (index.containsKey(id)) {</span>
<span class="fc" id="L293">                SimpleOrderedMap&lt;Object&gt; indexItem = index.get(id);</span>
                List&lt;SimpleOrderedMap&lt;Object&gt;&gt; shards;
<span class="pc bpc" id="L295" title="1 of 2 branches missed.">                if (indexItem.get(&quot;shards&quot;) != null</span>
<span class="pc bpc" id="L296" title="1 of 2 branches missed.">                    &amp;&amp; indexItem.get(&quot;shards&quot;) instanceof List) {</span>
<span class="fc" id="L297">                  shards = (List&lt;SimpleOrderedMap&lt;Object&gt;&gt;) indexItem</span>
<span class="fc" id="L298">                      .get(&quot;shards&quot;);</span>
<span class="fc" id="L299">                } else {</span>
<span class="nc" id="L300">                  shards = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L301">                  indexItem.add(&quot;shards&quot;, shards);</span>
                }
<span class="fc" id="L303">                shards.add(item);</span>
              }
            }
          }
        }
<span class="fc bfc" id="L308" title="All 2 branches covered.">      } else if (action.equals(ComponentCollection.ACTION_CHECK)</span>
<span class="fc bfc" id="L309" title="All 2 branches covered.">          || action.equals(ComponentCollection.ACTION_POST)</span>
<span class="pc bpc" id="L310" title="1 of 2 branches missed.">          || action.equals(ComponentCollection.ACTION_IMPORT)</span>
<span class="fc bfc" id="L311" title="All 2 branches covered.">          || action.equals(ComponentCollection.ACTION_CREATE)</span>
<span class="pc bpc" id="L312" title="1 of 2 branches missed.">          || action.equals(ComponentCollection.ACTION_GET)) {</span>
<span class="pc bpc" id="L313" title="1 of 4 branches missed.">        if (status != null &amp;&amp; status.get(&quot;id&quot;) != null</span>
<span class="pc bpc" id="L314" title="1 of 2 branches missed.">            &amp;&amp; status.get(&quot;id&quot;) instanceof String) {</span>
<span class="fc" id="L315">          String id = (String) status.get(&quot;id&quot;);</span>
<span class="pc bpc" id="L316" title="1 of 2 branches missed.">          if (id.equals(newItem.id)) {</span>
            List&lt;SimpleOrderedMap&lt;Object&gt;&gt; shards;
<span class="fc bfc" id="L318" title="All 2 branches covered.">            if (status.get(&quot;shards&quot;) != null</span>
<span class="pc bpc" id="L319" title="1 of 2 branches missed.">                &amp;&amp; status.get(&quot;shards&quot;) instanceof List) {</span>
<span class="fc" id="L320">              shards = (List&lt;SimpleOrderedMap&lt;Object&gt;&gt;) status.get(&quot;shards&quot;);</span>
<span class="fc" id="L321">            } else {</span>
<span class="fc" id="L322">              shards = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L323">              status.add(&quot;shards&quot;, shards);</span>
            }
<span class="fc bfc" id="L325" title="All 2 branches covered.">            if (newItem.status != null) {</span>
<span class="pc bpc" id="L326" title="1 of 2 branches missed.">              if (action.equals(ComponentCollection.ACTION_GET)) {</span>
<span class="nc" id="L327">                newItem.status.add(&quot;values&quot;, newItem.values);</span>
              }
<span class="fc" id="L329">              shards.add(newItem.status);</span>
            }
          }
        }
<span class="fc" id="L333">      } else {</span>
<span class="fc" id="L334">        throw new IOException(&quot;not allowed for action '&quot; + action + &quot;'&quot;);</span>
      }
    }
<span class="fc" id="L337">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
<span class="nc" id="L346">    StringBuilder text = new StringBuilder(&quot;&quot;);</span>
<span class="nc" id="L347">    text.append(MtasSolrCollectionResult.class.getSimpleName() + &quot;[&quot;);</span>
<span class="nc" id="L348">    text.append(action + &quot;, &quot;);</span>
<span class="nc" id="L349">    text.append(id + &quot;, &quot;);</span>
<span class="nc bnc" id="L350" title="All 2 branches missed.">    if (componentCollection != null) {</span>
<span class="nc" id="L351">      text.append(componentCollection.version + &quot;, &quot;);</span>
<span class="nc bnc" id="L352" title="All 2 branches missed.">    } else if (status != null) {</span>
<span class="nc" id="L353">      text.append(status.get(&quot;version&quot;) + &quot;, &quot;);</span>
<span class="nc" id="L354">    } else {</span>
<span class="nc" id="L355">      text.append(&quot;null, &quot;);</span>
    }
<span class="nc bnc" id="L357" title="All 2 branches missed.">    text.append((values != null) ? values.size() : &quot;null&quot;);</span>
<span class="nc" id="L358">    text.append(&quot;]&quot;);</span>
<span class="nc" id="L359">    return text.toString();</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>