<?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>MtasSolrResultMerge.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> > <a href="index.source.html" class="el_package">mtas.solr.handler.component.util</a> > <span class="el_source">MtasSolrResultMerge.java</span></div><h1>MtasSolrResultMerge.java</h1><pre class="source lang-java linenums">package mtas.solr.handler.component.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.SortedSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.handler.component.ResponseBuilder;
import org.apache.solr.handler.component.ShardRequest;
import org.apache.solr.handler.component.ShardResponse;
import mtas.solr.handler.component.MtasSolrSearchComponent;
/**
* The Class MtasSolrResultMerge.
*/
<span class="fc" id="L23">public class MtasSolrResultMerge {</span>
/** The Constant log. */
<span class="fc" id="L26"> private static final Log log = LogFactory.getLog(MtasSolrResultMerge.class);</span>
/**
* Merge.
*
* @param rb the rb
*/
@SuppressWarnings("unchecked")
public void merge(ResponseBuilder rb) {
<span class="pc bpc" id="L35" title="1 of 2 branches missed."> if (rb.req.getParams().getBool(MtasSolrSearchComponent.PARAM_MTAS, false)) {</span>
// mtas response
<span class="fc" id="L37"> NamedList<Object> mtasResponse = null;</span>
try {
<span class="fc" id="L39"> mtasResponse = (NamedList<Object>) rb.rsp.getValues().get("mtas");</span>
<span class="nc" id="L40"> } catch (ClassCastException e) {</span>
<span class="nc" id="L41"> log.debug(e);</span>
<span class="nc" id="L42"> mtasResponse = null;</span>
<span class="fc" id="L43"> }</span>
<span class="fc bfc" id="L44" title="All 2 branches covered."> if (mtasResponse == null) {</span>
<span class="fc" id="L45"> mtasResponse = new SimpleOrderedMap<>();</span>
<span class="fc" id="L46"> rb.rsp.add("mtas", mtasResponse);</span>
}
<span class="fc bfc" id="L49" title="All 2 branches covered."> for (ShardRequest sreq : rb.finished) {</span>
<span class="fc bfc" id="L50" title="All 2 branches covered."> if (rb.stage == ResponseBuilder.STAGE_EXECUTE_QUERY) {</span>
// merge stats
<span class="fc" id="L52"> if (rb.req.getParams()</span>
<span class="fc bfc" id="L53" title="All 2 branches covered."> .getBool(MtasSolrComponentStats.PARAM_MTAS_STATS, false)) {</span>
<span class="fc" id="L54"> mergeNamedList(sreq, mtasResponse, "stats", null);</span>
}
// merge group
<span class="fc" id="L57"> if (rb.req.getParams()</span>
<span class="pc bpc" id="L58" title="1 of 2 branches missed."> .getBool(MtasSolrComponentGroup.PARAM_MTAS_GROUP, false)) {</span>
<span class="nc" id="L59"> mergeArrayList(sreq, mtasResponse, "group", null, false);</span>
}
// merge facet
<span class="fc" id="L62"> if (rb.req.getParams()</span>
<span class="pc bpc" id="L63" title="1 of 2 branches missed."> .getBool(MtasSolrComponentFacet.PARAM_MTAS_FACET, false)) {</span>
<span class="nc" id="L64"> mergeArrayList(sreq, mtasResponse, "facet", null, false);</span>
}
// merge collection
<span class="fc bfc" id="L67" title="All 2 branches covered."> if (rb.req.getParams().getBool(</span>
MtasSolrComponentCollection.PARAM_MTAS_COLLECTION, false)) {
<span class="fc" id="L69"> mergeArrayList(sreq, mtasResponse, "collection", null, false);</span>
}
// merge prefix
<span class="fc" id="L72"> if (rb.req.getParams()</span>
<span class="fc bfc" id="L73" title="All 2 branches covered."> .getBool(MtasSolrComponentPrefix.PARAM_MTAS_PREFIX, false)) {</span>
<span class="fc" id="L74"> mergeArrayList(sreq, mtasResponse, "prefix", null, false);</span>
}
<span class="fc bfc" id="L76" title="All 2 branches covered."> } else if (rb.stage == MtasSolrSearchComponent.STAGE_COLLECTION_INIT) {</span>
// merge collection
<span class="pc bpc" id="L78" title="1 of 2 branches missed."> if (rb.req.getParams().getBool(</span>
MtasSolrComponentCollection.PARAM_MTAS_COLLECTION, false)) {
<span class="fc" id="L80"> mergeArrayList(sreq, mtasResponse, "collection", null, false);</span>
}
<span class="fc bfc" id="L82" title="All 2 branches covered."> } else if (rb.stage == MtasSolrSearchComponent.STAGE_TERMVECTOR_MISSING_KEY) {</span>
// merge termvector
<span class="pc bpc" id="L84" title="1 of 2 branches missed."> if (rb.req.getParams().getBool(</span>
MtasSolrComponentTermvector.PARAM_MTAS_TERMVECTOR, false)) {
<span class="fc" id="L86"> mergeArrayList(sreq, mtasResponse, "termvector", null, false);</span>
}
<span class="pc bpc" id="L88" title="1 of 2 branches missed."> } else if (rb.stage == MtasSolrSearchComponent.STAGE_LIST) {</span>
// merge list
<span class="nc bnc" id="L90" title="All 2 branches missed."> if (rb.req.getParams().getBool(MtasSolrComponentList.PARAM_MTAS_LIST,</span>
false)) {
<span class="nc" id="L92"> mergeArrayList(sreq, mtasResponse, "list",</span>
<span class="nc" id="L93"> ShardRequest.PURPOSE_PRIVATE, true);</span>
}
<span class="fc bfc" id="L95" title="All 2 branches covered."> } else if (rb.stage == ResponseBuilder.STAGE_GET_FIELDS) {</span>
// merge document
<span class="fc" id="L97"> if (rb.req.getParams()</span>
<span class="pc bpc" id="L98" title="1 of 2 branches missed."> .getBool(MtasSolrComponentDocument.PARAM_MTAS_DOCUMENT, false)) {</span>
<span class="nc" id="L99"> mergeArrayList(sreq, mtasResponse, "document",</span>
<span class="nc" id="L100"> ShardRequest.PURPOSE_PRIVATE, true);</span>
}
// merge kwic
<span class="pc bpc" id="L103" title="1 of 2 branches missed."> if (rb.req.getParams().getBool(MtasSolrComponentKwic.PARAM_MTAS_KWIC,</span>
false)) {
<span class="nc" id="L105"> mergeArrayList(sreq, mtasResponse, "kwic",</span>
<span class="nc" id="L106"> ShardRequest.PURPOSE_PRIVATE, true);</span>
}
}
<span class="fc" id="L109"> }</span>
}
<span class="fc" id="L111"> }</span>
/**
* Merge named list.
*
* @param sreq the sreq
* @param mtasResponse the mtas response
* @param key the key
* @param preferredPurpose the preferred purpose
*/
@SuppressWarnings("unchecked")
private void mergeNamedList(ShardRequest sreq, NamedList<Object> mtasResponse,
String key, Integer preferredPurpose) {
// create new response for key
NamedList<Object> mtasListResponse;
<span class="fc" id="L126"> Object o = mtasResponse.get(key);</span>
<span class="pc bpc" id="L127" title="1 of 2 branches missed."> if (o instanceof NamedList) {</span>
<span class="nc" id="L128"> mtasListResponse = (NamedList<Object>) o;</span>
} else {
<span class="fc" id="L130"> mtasListResponse = new SimpleOrderedMap<>();</span>
<span class="fc" id="L131"> mtasResponse.removeAll(key);</span>
<span class="fc" id="L132"> mtasResponse.add(key, mtasListResponse);</span>
}
// collect responses for each shard
<span class="fc" id="L135"> HashMap<String, NamedList<Object>> mtasListShardResponses = new HashMap<>();</span>
<span class="fc bfc" id="L136" title="All 2 branches covered."> for (ShardResponse response : sreq.responses) {</span>
// only continue if new shard or preferred purpose
<span class="pc bpc" id="L138" title="3 of 4 branches missed."> if (mtasListShardResponses.containsKey(response.getShard())</span>
&& ((preferredPurpose == null)
<span class="nc bnc" id="L140" title="All 2 branches missed."> || (sreq.purpose != preferredPurpose))) {</span>
<span class="nc" id="L141"> break;</span>
}
// update
try {
<span class="fc" id="L145"> NamedList<Object> result = response.getSolrResponse().getResponse();</span>
<span class="fc" id="L146"> NamedList<Object> data = (NamedList<Object>) result</span>
<span class="fc" id="L147"> .findRecursive("mtas", key);</span>
<span class="pc bpc" id="L148" title="1 of 2 branches missed."> if (data != null) {</span>
<span class="fc" id="L149"> mtasListShardResponses.put(response.getShard(),</span>
<span class="fc" id="L150"> MtasSolrResultUtil.decode(data));</span>
}
<span class="nc" id="L152"> } catch (ClassCastException e) {</span>
<span class="nc" id="L153"> log.debug(e);</span>
<span class="fc" id="L154"> }</span>
<span class="fc" id="L155"> }</span>
try {
<span class="fc bfc" id="L157" title="All 2 branches covered."> for (NamedList<Object> mtasListShardResponse : mtasListShardResponses</span>
<span class="fc" id="L158"> .values()) {</span>
<span class="fc" id="L159"> mergeResponsesNamedList(mtasListResponse, mtasListShardResponse);</span>
<span class="fc" id="L160"> }</span>
<span class="nc" id="L161"> } catch (IOException e) {</span>
<span class="nc" id="L162"> log.error(e);</span>
<span class="fc" id="L163"> }</span>
<span class="fc" id="L164"> }</span>
/**
* Merge array list.
*
* @param sreq the sreq
* @param mtasResponse the mtas response
* @param key the key
* @param preferredPurpose the preferred purpose
* @param mergeAllShardResponses the merge all shard responses
*/
@SuppressWarnings("unchecked")
private void mergeArrayList(ShardRequest sreq, NamedList<Object> mtasResponse,
String key, Integer preferredPurpose, boolean mergeAllShardResponses) {
// create new response for key
ArrayList<Object> mtasListResponse;
<span class="fc" id="L180"> Object o = mtasResponse.get(key);</span>
<span class="fc bfc" id="L181" title="All 2 branches covered."> if (o instanceof ArrayList) {</span>
<span class="fc" id="L182"> mtasListResponse = (ArrayList<Object>) o;</span>
} else {
<span class="fc" id="L184"> mtasListResponse = new ArrayList<>();</span>
<span class="fc" id="L185"> mtasResponse.removeAll(key);</span>
<span class="fc" id="L186"> mtasResponse.add(key, mtasListResponse);</span>
}
// collect responses for each shard
<span class="fc" id="L189"> HashMap<String, ArrayList<Object>> mtasListShardResponses = new HashMap<>();</span>
<span class="fc" id="L190"> ArrayList<ArrayList<Object>> mtasListShardResponsesExtra = new ArrayList<>();</span>
<span class="fc bfc" id="L191" title="All 2 branches covered."> for (ShardResponse response : sreq.responses) {</span>
// only continue if new shard or preferred purpose
<span class="pc bpc" id="L193" title="3 of 4 branches missed."> if (mtasListShardResponses.containsKey(response.getShard())</span>
&& ((preferredPurpose == null)
<span class="nc bnc" id="L195" title="All 2 branches missed."> || (sreq.purpose != preferredPurpose))) {</span>
<span class="nc" id="L196"> break;</span>
}
// update
try {
<span class="fc" id="L200"> NamedList<Object> result = response.getSolrResponse().getResponse();</span>
<span class="fc" id="L201"> ArrayList<Object> data = (ArrayList<Object>) result</span>
<span class="fc" id="L202"> .findRecursive("mtas", key);</span>
<span class="pc bpc" id="L203" title="1 of 2 branches missed."> if (data != null) {</span>
<span class="pc bpc" id="L204" title="1 of 2 branches missed."> if (mtasListShardResponses.containsKey(response.getShardAddress())) {</span>
<span class="nc bnc" id="L205" title="All 2 branches missed."> if (mergeAllShardResponses) {</span>
<span class="nc" id="L206"> mtasListShardResponsesExtra.add(data);</span>
}
} else {
<span class="fc" id="L209"> mtasListShardResponses.put(response.getShardAddress(), data);</span>
}
}
<span class="nc" id="L212"> } catch (ClassCastException e) {</span>
<span class="nc" id="L213"> log.error(e);</span>
<span class="fc" id="L214"> }</span>
<span class="fc" id="L215"> }</span>
try {
<span class="fc bfc" id="L218" title="All 2 branches covered."> for (ArrayList<Object> mtasListShardResponse : mtasListShardResponses</span>
<span class="fc" id="L219"> .values()) {</span>
<span class="fc" id="L220"> mergeResponsesArrayList(mtasListResponse, mtasListShardResponse);</span>
<span class="fc" id="L221"> }</span>
<span class="pc bpc" id="L222" title="1 of 2 branches missed."> for (ArrayList<Object> mtasListShardResponse : mtasListShardResponsesExtra) {</span>
<span class="nc" id="L223"> mergeResponsesArrayList(mtasListResponse, mtasListShardResponse);</span>
<span class="nc" id="L224"> }</span>
<span class="fc" id="L225"> } catch (IOException e) {</span>
<span class="fc" id="L226"> log.error(e);</span>
<span class="fc" id="L227"> }</span>
<span class="fc" id="L228"> }</span>
/**
* Merge responses sorted set.
*
* @param originalList the original list
* @param shardList the shard list
*/
private void mergeResponsesSortedSet(SortedSet<Object> originalList,
SortedSet<Object> shardList) {
<span class="fc bfc" id="L238" title="All 2 branches covered."> for (Object item : shardList) {</span>
<span class="fc" id="L239"> originalList.add(item);</span>
<span class="fc" id="L240"> }</span>
<span class="fc" id="L241"> }</span>
/**
* Merge responses array list.
*
* @param originalList the original list
* @param shardList the shard list
* @throws IOException Signals that an I/O exception has occurred.
*/
@SuppressWarnings("unchecked")
private void mergeResponsesArrayList(ArrayList<Object> originalList,
ArrayList<Object> shardList) throws IOException {
// get keys from original
<span class="fc" id="L254"> HashMap<String, Object> originalKeyList = new HashMap<>();</span>
<span class="fc bfc" id="L255" title="All 2 branches covered."> for (Object item : originalList) {</span>
<span class="pc bpc" id="L256" title="1 of 2 branches missed."> if (item instanceof NamedList<?>) {</span>
<span class="fc" id="L257"> NamedList<Object> itemList = (NamedList<Object>) item;</span>
<span class="fc" id="L258"> Object key = itemList.get("key");</span>
<span class="pc bpc" id="L259" title="2 of 4 branches missed."> if ((key != null) && (key instanceof String)) {</span>
<span class="fc" id="L260"> originalKeyList.put((String) key, item);</span>
}
}
<span class="fc" id="L263"> }</span>
<span class="fc bfc" id="L264" title="All 2 branches covered."> for (Object item : shardList) {</span>
<span class="pc bpc" id="L265" title="1 of 2 branches missed."> if (item instanceof NamedList<?>) {</span>
<span class="fc" id="L266"> NamedList<Object> itemList = (NamedList<Object>) item;</span>
<span class="fc" id="L267"> Object key = itemList.get("key");</span>
// item with key
<span class="pc bpc" id="L269" title="2 of 4 branches missed."> if ((key != null) && (key instanceof String)) {</span>
// merge
<span class="fc bfc" id="L271" title="All 2 branches covered."> if (originalKeyList.containsKey(key)) {</span>
<span class="fc" id="L272"> Object originalItem = originalKeyList.get(key);</span>
<span class="pc bpc" id="L273" title="1 of 2 branches missed."> if (originalItem.getClass().equals(item.getClass())) {</span>
<span class="fc" id="L274"> mergeResponsesNamedList((NamedList<Object>) originalItem,</span>
(NamedList<Object>) item);
} else {
// ignore?
}
// add
<span class="fc" id="L280"> } else {</span>
<span class="fc" id="L281"> Object clonedItem = adjustablePartsCloned(item);</span>
<span class="fc" id="L282"> originalList.add(clonedItem);</span>
<span class="fc" id="L283"> originalKeyList.put((String) key, clonedItem);</span>
<span class="fc" id="L284"> }</span>
} else {
<span class="nc" id="L286"> originalList.add(item);</span>
}
<span class="fc" id="L288"> } else {</span>
<span class="nc" id="L289"> originalList.add(item);</span>
}
<span class="fc" id="L291"> }</span>
<span class="fc" id="L292"> }</span>
/**
* Merge responses named list.
*
* @param mainResponse the main response
* @param shardResponse the shard response
* @throws IOException Signals that an I/O exception has occurred.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private void mergeResponsesNamedList(NamedList<Object> mainResponse,
NamedList<Object> shardResponse) throws IOException {
<span class="fc" id="L304"> Iterator<Entry<String, Object>> it = shardResponse.iterator();</span>
<span class="fc bfc" id="L305" title="All 2 branches covered."> while (it.hasNext()) {</span>
<span class="fc" id="L306"> Entry<String, Object> entry = it.next();</span>
<span class="fc" id="L307"> String name = entry.getKey();</span>
<span class="fc" id="L308"> Object shardValue = entry.getValue();</span>
<span class="fc" id="L309"> int originalId = mainResponse.indexOf(name, 0);</span>
<span class="fc bfc" id="L310" title="All 2 branches covered."> if (originalId < 0) {</span>
<span class="fc" id="L311"> mainResponse.add(name, adjustablePartsCloned(shardValue));</span>
} else {
<span class="fc" id="L313"> Object original = mainResponse.getVal(originalId);</span>
<span class="pc bpc" id="L314" title="1 of 2 branches missed."> if (original == null) {</span>
<span class="nc" id="L315"> original = adjustablePartsCloned(shardValue);</span>
<span class="pc bpc" id="L316" title="1 of 2 branches missed."> } else if (shardValue != null</span>
<span class="pc bpc" id="L317" title="1 of 2 branches missed."> && original.getClass().equals(shardValue.getClass())) {</span>
// merge ArrayList
<span class="fc bfc" id="L319" title="All 2 branches covered."> if (original instanceof ArrayList) {</span>
<span class="fc" id="L320"> ArrayList originalList = (ArrayList) original;</span>
<span class="fc" id="L321"> ArrayList shardList = (ArrayList) shardValue;</span>
<span class="fc" id="L322"> mergeResponsesArrayList(originalList, shardList);</span>
// merge Namedlist
<span class="pc bpc" id="L324" title="1 of 2 branches missed."> } else if (original instanceof NamedList<?>) {</span>
<span class="nc" id="L325"> mergeResponsesNamedList((NamedList<Object>) original,</span>
(NamedList<Object>) shardValue);
// merge SortedSet
<span class="fc bfc" id="L328" title="All 2 branches covered."> } else if (original instanceof SortedSet<?>) {</span>
<span class="fc" id="L329"> mergeResponsesSortedSet((SortedSet<Object>) original,</span>
(SortedSet<Object>) shardValue);
<span class="fc bfc" id="L331" title="All 2 branches covered."> } else if (original instanceof MtasSolrMtasResult) {</span>
<span class="fc" id="L332"> MtasSolrMtasResult originalComponentResult = (MtasSolrMtasResult) original;</span>
<span class="fc" id="L333"> originalComponentResult.merge((MtasSolrMtasResult) shardValue);</span>
<span class="fc bfc" id="L334" title="All 2 branches covered."> } else if (original instanceof MtasSolrCollectionResult) {</span>
<span class="fc" id="L335"> MtasSolrCollectionResult originalComponentResult = (MtasSolrCollectionResult) original;</span>
<span class="fc" id="L336"> originalComponentResult</span>
<span class="fc" id="L337"> .merge((MtasSolrCollectionResult) shardValue);</span>
<span class="pc bpc" id="L338" title="1 of 2 branches missed."> } else if (original instanceof String) {</span>
// ignore?
<span class="nc bnc" id="L340" title="All 2 branches missed."> } else if (original instanceof Integer) {</span>
<span class="nc" id="L341"> original = (Integer) original + ((Integer) shardValue);</span>
<span class="nc bnc" id="L342" title="All 2 branches missed."> } else if (original instanceof Long) {</span>
<span class="nc" id="L343"> original = (Long) original + ((Long) shardValue);</span>
} else {
// ignore?
}
<span class="fc" id="L347"> mainResponse.setVal(originalId, original);</span>
} else {
// ignore?
}
}
<span class="fc" id="L352"> }</span>
<span class="fc" id="L353"> }</span>
/**
* Adjustable parts cloned.
*
* @param original the original
* @return the object
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private Object adjustablePartsCloned(Object original) {
<span class="fc bfc" id="L363" title="All 2 branches covered."> if (original instanceof NamedList) {</span>
<span class="fc" id="L364"> NamedList<Object> newObject = new SimpleOrderedMap();</span>
<span class="fc" id="L365"> NamedList<Object> originalObject = (NamedList<Object>) original;</span>
<span class="fc bfc" id="L366" title="All 2 branches covered."> for (int i = 0; i < originalObject.size(); i++) {</span>
<span class="fc" id="L367"> newObject.add(originalObject.getName(i),</span>
<span class="fc" id="L368"> adjustablePartsCloned(originalObject.getVal(i)));</span>
}
<span class="fc" id="L370"> return newObject;</span>
<span class="fc bfc" id="L371" title="All 2 branches covered."> } else if (original instanceof ArrayList) {</span>
<span class="fc" id="L372"> ArrayList<Object> newObject = new ArrayList<>();</span>
<span class="fc" id="L373"> ArrayList<Object> originalObject = (ArrayList<Object>) original;</span>
<span class="fc bfc" id="L374" title="All 2 branches covered."> for (int i = 0; i < originalObject.size(); i++) {</span>
<span class="fc" id="L375"> newObject.add(adjustablePartsCloned(originalObject.get(i)));</span>
}
<span class="fc" id="L377"> return newObject;</span>
<span class="pc bpc" id="L378" title="1 of 2 branches missed."> } else if (original instanceof Integer) {</span>
<span class="nc" id="L379"> return original;</span>
}
<span class="fc" id="L381"> return original;</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>