<?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>MtasSolrComponentCollection.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">MtasSolrComponentCollection.java</span></div><h1>MtasSolrComponentCollection.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.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.lucene.util.BytesRef; import org.apache.solr.common.params.ModifiableSolrParams; 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.SearchComponent; import org.apache.solr.handler.component.ShardRequest; import org.apache.solr.handler.component.ShardResponse; import org.noggit.JSONParser; import org.noggit.JSONUtil; import mtas.codec.util.CodecComponent.ComponentFields; import mtas.codec.util.CodecComponent.ComponentCollection; import mtas.solr.handler.component.MtasSolrSearchComponent; /** * The Class MtasSolrComponentCollection. */ public class MtasSolrComponentCollection implements MtasSolrComponent<ComponentCollection> { /** The Constant log. */ <span class="fc" id="L37"> private static final Log log = LogFactory</span> <span class="fc" id="L38"> .getLog(MtasSolrComponentCollection.class);</span> /** The Constant PARAM_MTAS_COLLECTION. */ public static final String PARAM_MTAS_COLLECTION = MtasSolrSearchComponent.PARAM_MTAS + ".collection"; /** The Constant NAME_MTAS_COLLECTION_ACTION. */ public static final String NAME_MTAS_COLLECTION_ACTION = "action"; /** The Constant NAME_MTAS_COLLECTION_ID. */ public static final String NAME_MTAS_COLLECTION_ID = "id"; /** The Constant NAME_MTAS_COLLECTION_FIELD. */ public static final String NAME_MTAS_COLLECTION_FIELD = "field"; /** The Constant NAME_MTAS_COLLECTION_POST. */ public static final String NAME_MTAS_COLLECTION_POST = "post"; /** The Constant NAME_MTAS_COLLECTION_URL. */ public static final String NAME_MTAS_COLLECTION_URL = "url"; /** The Constant NAME_MTAS_COLLECTION_COLLECTION. */ public static final String NAME_MTAS_COLLECTION_COLLECTION = "collection"; /** The Constant NAME_MTAS_COLLECTION_KEY. */ public static final String NAME_MTAS_COLLECTION_KEY = "key"; /** The search component. */ private MtasSolrSearchComponent searchComponent; /** * Instantiates a new mtas solr component collection. * * @param searchComponent the search component */ <span class="fc" id="L73"> public MtasSolrComponentCollection(MtasSolrSearchComponent searchComponent) {</span> <span class="fc" id="L74"> this.searchComponent = searchComponent;</span> <span class="fc" id="L75"> }</span> /* * (non-Javadoc) * * @see * mtas.solr.handler.component.util.MtasSolrComponent#prepare(org.apache.solr. * handler.component.ResponseBuilder, * mtas.codec.util.CodecComponent.ComponentFields) */ public void prepare(ResponseBuilder rb, ComponentFields mtasFields) throws IOException { // System.out.println( // "collection: " + System.nanoTime() + " - " + // Thread.currentThread().getId() // + " - " + rb.req.getParams().getBool("isShard", false) + " PREPARE " // + rb.stage + " " + rb.req.getParamString()); <span class="fc" id="L92"> Set<String> ids = MtasSolrResultUtil</span> <span class="fc" id="L93"> .getIdsFromParameters(rb.req.getParams(), PARAM_MTAS_COLLECTION);</span> <span class="pc bpc" id="L94" title="1 of 2 branches missed."> if (!ids.isEmpty()) {</span> <span class="fc" id="L95"> int tmpCounter = 0;</span> <span class="fc" id="L96"> String[] keys = new String[ids.size()];</span> <span class="fc" id="L97"> String[] actions = new String[ids.size()];</span> <span class="fc" id="L98"> String[] fields = new String[ids.size()];</span> <span class="fc" id="L99"> String[] collectionIds = new String[ids.size()];</span> <span class="fc" id="L100"> String[] posts = new String[ids.size()];</span> <span class="fc" id="L101"> String[] urls = new String[ids.size()];</span> <span class="fc" id="L102"> String[] collections = new String[ids.size()];</span> <span class="fc bfc" id="L103" title="All 2 branches covered."> for (String id : ids) {</span> <span class="fc" id="L104"> actions[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_COLLECTION + "."</span> + id + "." + NAME_MTAS_COLLECTION_ACTION, null); <span class="fc" id="L106"> keys[tmpCounter] = rb.req.getParams().get(</span> PARAM_MTAS_COLLECTION + "." + id + "." + NAME_MTAS_COLLECTION_KEY, <span class="fc" id="L108"> String.valueOf(tmpCounter)).trim();</span> <span class="fc" id="L109"> fields[tmpCounter] = rb.req.getParams().get(</span> PARAM_MTAS_COLLECTION + "." + id + "." + NAME_MTAS_COLLECTION_FIELD, null); <span class="fc" id="L112"> collectionIds[tmpCounter] = rb.req.getParams().get(</span> PARAM_MTAS_COLLECTION + "." + id + "." + NAME_MTAS_COLLECTION_ID, null); <span class="fc" id="L115"> posts[tmpCounter] = rb.req.getParams().get(</span> PARAM_MTAS_COLLECTION + "." + id + "." + NAME_MTAS_COLLECTION_POST, null); <span class="fc" id="L118"> urls[tmpCounter] = rb.req.getParams().get(</span> PARAM_MTAS_COLLECTION + "." + id + "." + NAME_MTAS_COLLECTION_URL, null); <span class="fc" id="L121"> collections[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_COLLECTION</span> + "." + id + "." + NAME_MTAS_COLLECTION_COLLECTION, null); <span class="fc" id="L123"> tmpCounter++;</span> <span class="fc" id="L124"> }</span> <span class="fc" id="L125"> mtasFields.doCollection = true;</span> <span class="fc" id="L126"> MtasSolrResultUtil.compareAndCheck(keys, actions,</span> <span class="fc" id="L127"> NAME_MTAS_COLLECTION_KEY, NAME_MTAS_COLLECTION_ACTION, true);</span> <span class="fc" id="L128"> MtasSolrResultUtil.compareAndCheck(keys, fields, NAME_MTAS_COLLECTION_KEY,</span> <span class="fc" id="L129"> NAME_MTAS_COLLECTION_FIELD, false);</span> <span class="fc" id="L130"> MtasSolrResultUtil.compareAndCheck(keys, collectionIds,</span> <span class="fc" id="L131"> NAME_MTAS_COLLECTION_KEY, NAME_MTAS_COLLECTION_ID, false);</span> <span class="fc" id="L132"> MtasSolrResultUtil.compareAndCheck(keys, posts, NAME_MTAS_COLLECTION_KEY,</span> <span class="fc" id="L133"> NAME_MTAS_COLLECTION_POST, false);</span> <span class="fc" id="L134"> MtasSolrResultUtil.compareAndCheck(keys, urls, NAME_MTAS_COLLECTION_KEY,</span> <span class="fc" id="L135"> NAME_MTAS_COLLECTION_URL, false);</span> <span class="fc" id="L136"> MtasSolrResultUtil.compareAndCheck(keys, collections,</span> <span class="fc" id="L137"> NAME_MTAS_COLLECTION_KEY, NAME_MTAS_COLLECTION_COLLECTION, false);</span> <span class="fc bfc" id="L138" title="All 2 branches covered."> for (int i = 0; i < actions.length; i++) {</span> <span class="pc bpc" id="L139" title="1 of 2 branches missed."> if (actions[i] != null) {</span> ComponentCollection componentCollection; <span class="pc bpc" id="L141" title="19 of 34 branches missed."> switch (actions[i]) {</span> case ComponentCollection.ACTION_LIST: <span class="fc" id="L143"> componentCollection = new ComponentCollection(keys[i],</span> ComponentCollection.ACTION_LIST); <span class="fc" id="L145"> componentCollection.setListVariables();</span> <span class="fc" id="L146"> mtasFields.collection.add(componentCollection);</span> <span class="fc" id="L147"> break;</span> case ComponentCollection.ACTION_CHECK: <span class="pc bpc" id="L149" title="1 of 2 branches missed."> if (collectionIds[i] != null) {</span> <span class="fc" id="L150"> componentCollection = new ComponentCollection(keys[i],</span> ComponentCollection.ACTION_CHECK); <span class="fc" id="L152"> componentCollection.setCheckVariables(collectionIds[i]);</span> <span class="fc" id="L153"> mtasFields.collection.add(componentCollection);</span> } else { <span class="nc" id="L155"> throw new IOException(</span> "no id defined for collection (" + actions[i] + ")"); } break; case ComponentCollection.ACTION_GET: <span class="nc bnc" id="L160" title="All 2 branches missed."> if (collectionIds[i] != null) {</span> <span class="nc" id="L161"> componentCollection = new ComponentCollection(keys[i],</span> ComponentCollection.ACTION_GET); <span class="nc" id="L163"> componentCollection.setGetVariables(collectionIds[i]);</span> <span class="nc" id="L164"> mtasFields.collection.add(componentCollection);</span> } else { <span class="nc" id="L166"> throw new IOException(</span> "no id defined for collection (" + actions[i] + ")"); } break; case ComponentCollection.ACTION_CREATE: <span class="pc bpc" id="L171" title="1 of 2 branches missed."> if (fields[i] != null) {</span> <span class="fc" id="L172"> Set<String> fieldList = new HashSet<>(</span> <span class="fc" id="L173"> Arrays.asList(fields[i].split(",")));</span> <span class="fc" id="L174"> componentCollection = new ComponentCollection(keys[i],</span> ComponentCollection.ACTION_CREATE); <span class="fc" id="L176"> componentCollection.setCreateVariables(collectionIds[i],</span> fieldList); <span class="fc" id="L178"> mtasFields.doCollection = true;</span> <span class="fc" id="L179"> mtasFields.collection.add(componentCollection);</span> <span class="fc" id="L180"> rb.setNeedDocSet(true);</span> <span class="fc" id="L181"> } else {</span> <span class="nc" id="L182"> throw new IOException(</span> "no field defined for collection (" + actions[i] + ")"); } break; case ComponentCollection.ACTION_POST: <span class="pc bpc" id="L187" title="1 of 2 branches missed."> if (posts[i] != null) {</span> <span class="fc" id="L188"> componentCollection = new ComponentCollection(keys[i],</span> ComponentCollection.ACTION_POST); <span class="fc" id="L190"> componentCollection.setPostVariables(collectionIds[i],</span> <span class="fc" id="L191"> stringToStringValues(posts[i]));</span> <span class="fc" id="L192"> mtasFields.collection.add(componentCollection);</span> } else { <span class="nc" id="L194"> throw new IOException(</span> "no post defined for collection (" + actions[i] + ")"); } break; case ComponentCollection.ACTION_IMPORT: <span class="nc bnc" id="L199" title="All 4 branches missed."> if (urls[i] != null && collections[i] != null) {</span> <span class="nc" id="L200"> componentCollection = new ComponentCollection(keys[i],</span> ComponentCollection.ACTION_IMPORT); <span class="nc" id="L202"> componentCollection.setImportVariables(collectionIds[i], urls[i],</span> collections[i]); <span class="nc" id="L204"> mtasFields.collection.add(componentCollection);</span> } else { <span class="nc" id="L206"> throw new IOException(</span> "no post defined for collection (" + actions[i] + ")"); } break; case ComponentCollection.ACTION_DELETE: <span class="pc bpc" id="L211" title="1 of 2 branches missed."> if (collectionIds[i] != null) {</span> <span class="fc" id="L212"> componentCollection = new ComponentCollection(keys[i],</span> ComponentCollection.ACTION_DELETE); <span class="fc" id="L214"> componentCollection.setDeleteVariables(collectionIds[i]);</span> <span class="fc" id="L215"> mtasFields.collection.add(componentCollection);</span> } else { <span class="nc" id="L217"> throw new IOException(</span> "no id defined for collection (" + actions[i] + ")"); } break; case ComponentCollection.ACTION_EMPTY: <span class="nc" id="L222"> componentCollection = new ComponentCollection(keys[i],</span> ComponentCollection.ACTION_EMPTY); <span class="nc" id="L224"> mtasFields.collection.add(componentCollection);</span> <span class="nc" id="L225"> break;</span> default: <span class="nc" id="L227"> throw new IOException(</span> "unrecognized action '" + actions[i] + "' for collection"); } <span class="fc" id="L230"> } else {</span> <span class="nc" id="L231"> throw new IOException("no action defined for collection");</span> } } } <span class="fc" id="L235"> }</span> /* * (non-Javadoc) * * @see * mtas.solr.handler.component.util.MtasSolrComponent#modifyRequest(org.apache * .solr.handler.component.ResponseBuilder, * org.apache.solr.handler.component.SearchComponent, * org.apache.solr.handler.component.ShardRequest) */ public void modifyRequest(ResponseBuilder rb, SearchComponent who, ShardRequest sreq) { // System.out.println( // "collection: " + System.nanoTime() + " - " + // Thread.currentThread().getId() // + " - " + rb.req.getParams().getBool("isShard", false) // + " MODIFYREQUEST " + rb.stage + " " + rb.req.getParamString()); <span class="pc bpc" id="L253" title="1 of 2 branches missed."> if (sreq.params.getBool(MtasSolrSearchComponent.PARAM_MTAS, false)</span> <span class="pc bpc" id="L254" title="1 of 2 branches missed."> && sreq.params.getBool(PARAM_MTAS_COLLECTION, false)) {</span> <span class="fc bfc" id="L255" title="All 2 branches covered."> if ((sreq.purpose & ShardRequest.PURPOSE_GET_TOP_IDS) != 0) {</span> // do nothing } else { // remove for other requests <span class="fc" id="L259"> Set<String> keys = MtasSolrResultUtil</span> <span class="fc" id="L260"> .getIdsFromParameters(rb.req.getParams(), PARAM_MTAS_COLLECTION);</span> <span class="fc" id="L261"> sreq.params.remove(PARAM_MTAS_COLLECTION);</span> <span class="fc bfc" id="L262" title="All 2 branches covered."> for (String key : keys) {</span> <span class="fc" id="L263"> sreq.params.remove(PARAM_MTAS_COLLECTION + "." + key + "."</span> + NAME_MTAS_COLLECTION_ACTION); <span class="fc" id="L265"> sreq.params.remove(PARAM_MTAS_COLLECTION + "." + key + "."</span> + NAME_MTAS_COLLECTION_ID); <span class="fc" id="L267"> sreq.params.remove(PARAM_MTAS_COLLECTION + "." + key + "."</span> + NAME_MTAS_COLLECTION_FIELD); <span class="fc" id="L269"> sreq.params.remove(PARAM_MTAS_COLLECTION + "." + key + "."</span> + NAME_MTAS_COLLECTION_POST); <span class="fc" id="L271"> sreq.params.remove(PARAM_MTAS_COLLECTION + "." + key + "."</span> + NAME_MTAS_COLLECTION_KEY); <span class="fc" id="L273"> sreq.params.remove(PARAM_MTAS_COLLECTION + "." + key + "."</span> + NAME_MTAS_COLLECTION_URL); <span class="fc" id="L275"> sreq.params.remove(PARAM_MTAS_COLLECTION + "." + key + "."</span> + NAME_MTAS_COLLECTION_COLLECTION); <span class="fc" id="L277"> }</span> } } <span class="fc" id="L280"> }</span> /* * (non-Javadoc) * * @see * mtas.solr.handler.component.util.MtasSolrComponent#create(mtas.codec.util. * CodecComponent.BasicComponent, java.lang.Boolean) */ public SimpleOrderedMap<Object> create( ComponentCollection componentCollection, Boolean encode) throws IOException { <span class="fc" id="L292"> MtasSolrCollectionResult data = createMtasSolrCollectionResult(</span> <span class="fc bfc" id="L293" title="All 2 branches covered."> componentCollection, encode ? false : true);</span> // Create response <span class="fc" id="L295"> SimpleOrderedMap<Object> mtasCollectionResponse = new SimpleOrderedMap<>();</span> <span class="fc" id="L296"> mtasCollectionResponse.add("key", componentCollection.key);</span> <span class="fc bfc" id="L297" title="All 2 branches covered."> if (encode) {</span> <span class="fc" id="L298"> mtasCollectionResponse.add("_encoded_data",</span> <span class="fc" id="L299"> MtasSolrResultUtil.encode(data));</span> } else { <span class="fc" id="L301"> mtasCollectionResponse.add("data", data);</span> <span class="fc" id="L302"> MtasSolrResultUtil.rewrite(mtasCollectionResponse, searchComponent);</span> } <span class="fc" id="L304"> return mtasCollectionResponse;</span> } /** * Creates the mtas solr collection result. * * @param componentCollection the component collection * @param storeIfRelevant the store if relevant * @return the mtas solr collection result * @throws IOException Signals that an I/O exception has occurred. */ /* * (non-Javadoc) * * @see * mtas.solr.handler.component.util.MtasSolrComponent#create(mtas.codec.util. * CodecComponent.BasicComponent, java.lang.Boolean) */ private MtasSolrCollectionResult createMtasSolrCollectionResult( ComponentCollection componentCollection, boolean storeIfRelevant) throws IOException { // System.out.println("collection: " + System.nanoTime() + " - " // + Thread.currentThread().getId() + " - " + " CREATE "); <span class="pc bpc" id="L327" title="1 of 2 branches missed."> if (componentCollection != null) {</span> // Create response <span class="fc" id="L329"> MtasSolrCollectionResult data = new MtasSolrCollectionResult(</span> componentCollection); <span class="fc" id="L331"> if (componentCollection.action()</span> <span class="fc bfc" id="L332" title="All 2 branches covered."> .equals(ComponentCollection.ACTION_CREATE)) {</span> <span class="pc bpc" id="L333" title="1 of 4 branches missed."> if (storeIfRelevant && componentCollection.version == null) {</span> <span class="fc" id="L334"> componentCollection.version = searchComponent.getCollectionCache()</span> <span class="fc" id="L335"> .create(componentCollection.id,</span> <span class="fc" id="L336"> componentCollection.values().size(),</span> <span class="fc" id="L337"> componentCollection.values());</span> } <span class="fc" id="L339"> data.setCreate(searchComponent.getCollectionCache().now(),</span> <span class="fc" id="L340"> searchComponent.getCollectionCache().check(componentCollection.id));</span> <span class="fc" id="L341"> } else if (componentCollection.action()</span> <span class="fc bfc" id="L342" title="All 2 branches covered."> .equals(ComponentCollection.ACTION_LIST)) {</span> // retrieve and add list to result <span class="fc" id="L344"> data.setList(searchComponent.getCollectionCache().now(),</span> <span class="fc" id="L345"> searchComponent.getCollectionCache().list());</span> <span class="fc" id="L346"> } else if (componentCollection.action()</span> <span class="fc bfc" id="L347" title="All 2 branches covered."> .equals(ComponentCollection.ACTION_CHECK)) {</span> // retrieve and add status to result <span class="fc" id="L349"> data.setCheck(searchComponent.getCollectionCache().now(),</span> <span class="fc" id="L350"> searchComponent.getCollectionCache().check(componentCollection.id));</span> <span class="fc" id="L351"> } else if (componentCollection.action()</span> <span class="pc bpc" id="L352" title="1 of 2 branches missed."> .equals(ComponentCollection.ACTION_GET)) {</span> // retrieve and add status to result <span class="nc" id="L354"> HashSet<String> values = searchComponent.getCollectionCache()</span> <span class="nc" id="L355"> .getDataById(componentCollection.id);</span> <span class="nc bnc" id="L356" title="All 2 branches missed."> if (values != null) {</span> <span class="nc" id="L357"> data.setGet(searchComponent.getCollectionCache().now(),</span> <span class="nc" id="L358"> searchComponent.getCollectionCache()</span> <span class="nc" id="L359"> .check(componentCollection.id),</span> values); } <span class="pc" id="L362"> } else if (componentCollection.action()</span> <span class="pc bpc" id="L363" title="1 of 2 branches missed."> .equals(ComponentCollection.ACTION_EMPTY)) {</span> // empty <span class="nc" id="L365"> searchComponent.getCollectionCache().empty();</span> <span class="fc" id="L366"> } else if (componentCollection.action()</span> <span class="fc bfc" id="L367" title="All 2 branches covered."> .equals(ComponentCollection.ACTION_POST)) {</span> // store if not already stored <span class="pc bpc" id="L369" title="1 of 2 branches missed."> if (componentCollection.version == null) {</span> <span class="fc" id="L370"> componentCollection.version = searchComponent.getCollectionCache()</span> <span class="fc" id="L371"> .create(componentCollection.id,</span> <span class="fc" id="L372"> componentCollection.values().size(),</span> <span class="fc" id="L373"> componentCollection.values());</span> } // add status to result <span class="fc" id="L376"> data.setPost(searchComponent.getCollectionCache().now(),</span> <span class="fc" id="L377"> searchComponent.getCollectionCache().check(componentCollection.id));</span> <span class="fc" id="L378"> } else if (componentCollection.action()</span> <span class="pc bpc" id="L379" title="1 of 2 branches missed."> .equals(ComponentCollection.ACTION_IMPORT)) {</span> // import if not already stored <span class="nc bnc" id="L381" title="All 2 branches missed."> if (componentCollection.version == null) {</span> <span class="nc" id="L382"> componentCollection.version = searchComponent.getCollectionCache()</span> <span class="nc" id="L383"> .create(componentCollection.id,</span> <span class="nc" id="L384"> componentCollection.values().size(),</span> <span class="nc" id="L385"> componentCollection.values());</span> } // add status to result <span class="nc" id="L388"> data.setImport(searchComponent.getCollectionCache().now(),</span> <span class="nc" id="L389"> searchComponent.getCollectionCache().check(componentCollection.id));</span> <span class="fc" id="L390"> } else if (componentCollection.action()</span> <span class="pc bpc" id="L391" title="1 of 2 branches missed."> .equals(ComponentCollection.ACTION_DELETE)) {</span> <span class="fc" id="L392"> searchComponent.getCollectionCache().deleteById(componentCollection.id);</span> } <span class="fc" id="L394"> return data;</span> } else { <span class="nc" id="L396"> throw new IOException("no componentCollection available");</span> } } /* * (non-Javadoc) * * @see * mtas.solr.handler.component.util.MtasSolrComponent#finishStage(org.apache. * solr.handler.component.ResponseBuilder) */ public void finishStage(ResponseBuilder rb) { // System.out.println( // "collection: " + System.nanoTime() + " - " + // Thread.currentThread().getId() // + " - " + rb.req.getParams().getBool("isShard", false) // + " FINISHSTAGE " + rb.stage + " " + rb.req.getParamString()); <span class="pc bpc" id="L413" title="1 of 2 branches missed."> if (rb.req.getParams().getBool(MtasSolrSearchComponent.PARAM_MTAS, false)) {</span> <span class="fc bfc" id="L414" title="All 4 branches covered."> if (rb.stage >= ResponseBuilder.STAGE_EXECUTE_QUERY</span> && rb.stage < ResponseBuilder.STAGE_GET_FIELDS) { <span class="fc" id="L416"> ComponentFields mtasFields = getMtasFields(rb);</span> <span class="pc bpc" id="L417" title="1 of 2 branches missed."> if (mtasFields.doCollection) {</span> <span class="fc bfc" id="L418" title="All 2 branches covered."> if (rb.stage == ResponseBuilder.STAGE_EXECUTE_QUERY) {</span> // mtas response <span class="fc" id="L420"> NamedList<Object> mtasResponse = null;</span> try { <span class="fc" id="L422"> mtasResponse = (NamedList<Object>) rb.rsp.getValues().get("mtas");</span> <span class="nc" id="L423"> } catch (ClassCastException e) {</span> <span class="nc" id="L424"> log.debug(e);</span> <span class="nc" id="L425"> mtasResponse = null;</span> <span class="fc" id="L426"> }</span> <span class="pc bpc" id="L427" title="1 of 2 branches missed."> if (mtasResponse == null) {</span> <span class="nc" id="L428"> mtasResponse = new SimpleOrderedMap<>();</span> <span class="nc" id="L429"> rb.rsp.add("mtas", mtasResponse);</span> } ArrayList<Object> mtasCollectionResponses; <span class="pc bpc" id="L432" title="1 of 2 branches missed."> if (mtasResponse.get("collection") != null</span> <span class="nc bnc" id="L433" title="All 2 branches missed."> && mtasResponse.get("collection") instanceof ArrayList) {</span> <span class="nc" id="L434"> mtasCollectionResponses = (ArrayList<Object>) mtasResponse</span> <span class="nc" id="L435"> .get("collection");</span> } else { <span class="fc" id="L437"> mtasCollectionResponses = new ArrayList<>();</span> <span class="fc" id="L438"> mtasResponse.add("collection", mtasCollectionResponses);</span> } MtasSolrCollectionResult collectionResult; <span class="fc bfc" id="L441" title="All 2 branches covered."> for (ComponentCollection componentCollection : mtasFields.collection) {</span> try { <span class="fc" id="L443"> collectionResult = createMtasSolrCollectionResult(</span> componentCollection, false); // Create response <span class="fc" id="L446"> SimpleOrderedMap<Object> mtasCollectionResponse = new SimpleOrderedMap<>();</span> <span class="fc" id="L447"> mtasCollectionResponse.add("key", componentCollection.key);</span> <span class="fc" id="L448"> mtasCollectionResponse.add("data", collectionResult);</span> <span class="fc" id="L449"> mtasCollectionResponses.add(mtasCollectionResponse);</span> <span class="nc" id="L450"> } catch (IOException e) {</span> <span class="nc" id="L451"> log.debug(e);</span> <span class="fc" id="L452"> }</span> <span class="fc" id="L453"> }</span> } // decode shard responses <span class="fc bfc" id="L456" title="All 2 branches covered."> for (ShardRequest sreq : rb.finished) {</span> <span class="pc bpc" id="L457" title="1 of 2 branches missed."> if (sreq.params.getBool(MtasSolrSearchComponent.PARAM_MTAS, false)</span> <span class="pc bpc" id="L458" title="1 of 2 branches missed."> && sreq.params.getBool(PARAM_MTAS_COLLECTION, false)) {</span> <span class="fc bfc" id="L459" title="All 2 branches covered."> for (ShardResponse shardResponse : sreq.responses) {</span> <span class="fc" id="L460"> NamedList<Object> solrShardResponse = shardResponse</span> <span class="fc" id="L461"> .getSolrResponse().getResponse();</span> try { <span class="fc" id="L463"> ArrayList<SimpleOrderedMap<Object>> data = (ArrayList<SimpleOrderedMap<Object>>) solrShardResponse</span> <span class="fc" id="L464"> .findRecursive("mtas", "collection");</span> <span class="pc bpc" id="L465" title="1 of 2 branches missed."> if (data != null) {</span> <span class="fc" id="L466"> MtasSolrResultUtil.decode(data);</span> <span class="fc bfc" id="L467" title="All 2 branches covered."> if (rb.stage > ResponseBuilder.STAGE_EXECUTE_QUERY) {</span> <span class="fc" id="L468"> ArrayList<SimpleOrderedMap<Object>> filteredData = new ArrayList<>();</span> <span class="fc bfc" id="L469" title="All 2 branches covered."> for (SimpleOrderedMap<Object> dataItem : data) {</span> <span class="pc bpc" id="L470" title="1 of 2 branches missed."> if (dataItem.get("data") != null && dataItem</span> <span class="pc bpc" id="L471" title="1 of 2 branches missed."> .get("data") instanceof MtasSolrCollectionResult) {</span> <span class="fc" id="L472"> MtasSolrCollectionResult collectionResult = (MtasSolrCollectionResult) dataItem</span> <span class="fc" id="L473"> .get("data");</span> <span class="fc bfc" id="L474" title="All 2 branches covered."> if (rb.stage <= MtasSolrSearchComponent.STAGE_COLLECTION_INIT) {</span> <span class="fc" id="L475"> if (!collectionResult.action()</span> <span class="pc bpc" id="L476" title="1 of 2 branches missed."> .equals(ComponentCollection.ACTION_CREATE)</span> <span class="fc" id="L477"> && !collectionResult.action()</span> <span class="pc bpc" id="L478" title="1 of 2 branches missed."> .equals(ComponentCollection.ACTION_LIST)</span> <span class="fc" id="L479"> && !collectionResult.action()</span> <span class="pc bpc" id="L480" title="1 of 2 branches missed."> .equals(ComponentCollection.ACTION_CHECK)) {</span> <span class="fc" id="L481"> filteredData.add(dataItem);</span> } <span class="pc bpc" id="L483" title="1 of 2 branches missed."> } else if (rb.stage <= MtasSolrSearchComponent.STAGE_COLLECTION_FINISH) {</span> <span class="fc" id="L484"> if (!collectionResult.action()</span> <span class="pc bpc" id="L485" title="1 of 2 branches missed."> .equals(ComponentCollection.ACTION_POST)</span> <span class="nc" id="L486"> && !collectionResult.action()</span> <span class="nc bnc" id="L487" title="All 2 branches missed."> .equals(ComponentCollection.ACTION_IMPORT)</span> <span class="nc" id="L488"> && !collectionResult.action()</span> <span class="nc bnc" id="L489" title="All 2 branches missed."> .equals(ComponentCollection.ACTION_CHECK)) {</span> <span class="nc" id="L490"> filteredData.add(dataItem);</span> } } <span class="fc" id="L493"> } else {</span> <span class="nc" id="L494"> filteredData.add(dataItem);</span> } <span class="fc" id="L496"> }</span> <span class="fc" id="L497"> data.clear();</span> <span class="fc" id="L498"> data.addAll(filteredData);</span> } } <span class="nc" id="L501"> } catch (ClassCastException e) {</span> <span class="nc" id="L502"> log.debug(e);</span> // shouldn't happen <span class="fc" id="L504"> }</span> <span class="fc" id="L505"> }</span> } <span class="fc" id="L508"> }</span> } } } <span class="fc" id="L512"> }</span> /* * (non-Javadoc) * * @see * mtas.solr.handler.component.util.MtasSolrComponent#distributedProcess(org. * apache.solr.handler.component.ResponseBuilder, * mtas.codec.util.CodecComponent.ComponentFields) */ @SuppressWarnings("unchecked") public void distributedProcess(ResponseBuilder rb, ComponentFields mtasFields) throws IOException { // System.out.println("collection: " + System.nanoTime() + " - " // + Thread.currentThread().getId() + " - " // + rb.req.getParams().getBool("isShard", false) + " DISTRIBUTEDPROCESS " // + rb.stage + " " + rb.req.getParamString()); <span class="fc" id="L529"> NamedList<Object> mtasResponse = null;</span> try { <span class="fc" id="L531"> mtasResponse = (NamedList<Object>) rb.rsp.getValues().get("mtas");</span> <span class="nc" id="L532"> } catch (ClassCastException e) {</span> <span class="nc" id="L533"> log.debug(e);</span> <span class="nc" id="L534"> mtasResponse = null;</span> <span class="fc" id="L535"> }</span> <span class="pc bpc" id="L536" title="1 of 2 branches missed."> if (mtasResponse != null) {</span> <span class="fc bfc" id="L537" title="All 2 branches covered."> if (rb.stage == MtasSolrSearchComponent.STAGE_COLLECTION_INIT) {</span> // build index <span class="fc" id="L539"> Map<String, MtasSolrCollectionResult> index = new HashMap<>();</span> ArrayList<Object> mtasResponseCollection; try { <span class="fc" id="L542"> mtasResponseCollection = (ArrayList<Object>) mtasResponse</span> <span class="fc" id="L543"> .get("collection");</span> <span class="fc bfc" id="L544" title="All 2 branches covered."> for (Object item : mtasResponseCollection) {</span> <span class="pc bpc" id="L545" title="1 of 2 branches missed."> if (item instanceof SimpleOrderedMap) {</span> <span class="fc" id="L546"> SimpleOrderedMap<Object> itemMap = (SimpleOrderedMap<Object>) item;</span> <span class="pc bpc" id="L547" title="1 of 2 branches missed."> if (itemMap.get("data") != null</span> <span class="pc bpc" id="L548" title="1 of 2 branches missed."> && itemMap.get("data") instanceof MtasSolrCollectionResult) {</span> <span class="fc" id="L549"> MtasSolrCollectionResult collectionItem = (MtasSolrCollectionResult) itemMap</span> <span class="fc" id="L550"> .get("data");</span> <span class="fc" id="L551"> index.put(collectionItem.id(), collectionItem);</span> } } <span class="fc" id="L554"> }</span> <span class="nc" id="L555"> } catch (ClassCastException e) {</span> <span class="nc" id="L556"> log.debug(e);</span> <span class="nc" id="L557"> mtasResponse.remove("collection");</span> <span class="fc" id="L558"> }</span> // check and remove previous responses <span class="fc" id="L560"> Map<String, Set<String>> createPostAfterMissingCheckResult = new HashMap<>();</span> <span class="fc bfc" id="L561" title="All 2 branches covered."> for (ShardRequest sreq : rb.finished) {</span> <span class="pc bpc" id="L562" title="1 of 2 branches missed."> if (sreq.params.getBool(MtasSolrSearchComponent.PARAM_MTAS, false)</span> <span class="pc bpc" id="L563" title="1 of 2 branches missed."> && sreq.params.getBool(PARAM_MTAS_COLLECTION, false)) {</span> <span class="fc bfc" id="L564" title="All 2 branches covered."> for (ShardResponse shardResponse : sreq.responses) {</span> <span class="fc" id="L565"> NamedList<Object> solrShardResponse = shardResponse</span> <span class="fc" id="L566"> .getSolrResponse().getResponse();</span> try { <span class="fc" id="L568"> ArrayList<SimpleOrderedMap<Object>> data = (ArrayList<SimpleOrderedMap<Object>>) solrShardResponse</span> <span class="fc" id="L569"> .findRecursive("mtas", "collection");</span> <span class="pc bpc" id="L570" title="1 of 2 branches missed."> if (data != null) {</span> <span class="fc bfc" id="L571" title="All 2 branches covered."> for (SimpleOrderedMap<Object> dataItem : data) {</span> <span class="pc bpc" id="L572" title="1 of 2 branches missed."> if (dataItem.get("data") != null && dataItem</span> <span class="pc bpc" id="L573" title="1 of 2 branches missed."> .get("data") instanceof MtasSolrCollectionResult) {</span> <span class="fc" id="L574"> MtasSolrCollectionResult dataItemResult = (MtasSolrCollectionResult) dataItem</span> <span class="fc" id="L575"> .get("data");</span> <span class="pc bpc" id="L576" title="1 of 2 branches missed."> if (index.containsKey(dataItemResult.id())</span> <span class="fc" id="L577"> && index.get(dataItemResult.id()).action()</span> <span class="fc bfc" id="L578" title="All 2 branches covered."> .equals(ComponentCollection.ACTION_CHECK)) {</span> <span class="fc bfc" id="L579" title="All 2 branches covered."> if (dataItemResult.status == null) {</span> <span class="fc" id="L580"> if (!createPostAfterMissingCheckResult</span> <span class="fc bfc" id="L581" title="All 2 branches covered."> .containsKey(shardResponse.getShard())) {</span> <span class="fc" id="L582"> createPostAfterMissingCheckResult</span> <span class="fc" id="L583"> .put(shardResponse.getShard(), new HashSet<>());</span> } <span class="fc" id="L585"> createPostAfterMissingCheckResult</span> <span class="fc" id="L586"> .get(shardResponse.getShard())</span> <span class="fc" id="L587"> .add(dataItemResult.id());</span> } } } <span class="fc" id="L591"> }</span> <span class="fc" id="L592"> data.clear();</span> } <span class="nc" id="L594"> } catch (ClassCastException e) {</span> <span class="nc" id="L595"> log.debug(e);</span> // shouldn't happen <span class="fc" id="L597"> }</span> <span class="fc" id="L598"> }</span> } <span class="fc" id="L600"> }</span> // construct new requests <span class="fc" id="L602"> HashMap<String, ModifiableSolrParams> requestParamList = new HashMap<>();</span> <span class="fc" id="L603"> int id = 0;</span> <span class="fc bfc" id="L604" title="All 2 branches covered."> for (ComponentCollection componentCollection : mtasFields.collection) {</span> <span class="fc" id="L605"> if (componentCollection.action()</span> <span class="fc bfc" id="L606" title="All 2 branches covered."> .equals(ComponentCollection.ACTION_CHECK)) {</span> <span class="fc bfc" id="L607" title="All 2 branches covered."> for (String shardAddress : rb.shards) {</span> <span class="fc bfc" id="L608" title="All 2 branches covered."> if (createPostAfterMissingCheckResult.containsKey(shardAddress)) {</span> <span class="fc" id="L609"> if (createPostAfterMissingCheckResult.get(shardAddress)</span> <span class="pc bpc" id="L610" title="1 of 2 branches missed."> .contains(componentCollection.id)) {</span> <span class="fc" id="L611"> HashSet<String> values = searchComponent.getCollectionCache()</span> <span class="fc" id="L612"> .getDataById(componentCollection.id);</span> <span class="fc bfc" id="L613" title="All 2 branches covered."> if (values != null) {</span> ModifiableSolrParams paramsNewRequest; <span class="fc bfc" id="L615" title="All 2 branches covered."> if (!requestParamList.containsKey(shardAddress)) {</span> <span class="fc" id="L616"> paramsNewRequest = new ModifiableSolrParams();</span> <span class="fc" id="L617"> requestParamList.put(shardAddress, paramsNewRequest);</span> } else { <span class="fc" id="L619"> paramsNewRequest = requestParamList.get(shardAddress);</span> } <span class="fc" id="L621"> paramsNewRequest.add(</span> PARAM_MTAS_COLLECTION + "." + id + "." + NAME_MTAS_COLLECTION_KEY, componentCollection.key); <span class="fc" id="L625"> paramsNewRequest.add(PARAM_MTAS_COLLECTION + "." + id + "."</span> + NAME_MTAS_COLLECTION_ID, componentCollection.id); <span class="fc" id="L627"> paramsNewRequest.add(</span> PARAM_MTAS_COLLECTION + "." + id + "." + NAME_MTAS_COLLECTION_ACTION, ComponentCollection.ACTION_POST); <span class="fc" id="L631"> paramsNewRequest.add(</span> PARAM_MTAS_COLLECTION + "." + id + "." + NAME_MTAS_COLLECTION_POST, <span class="fc" id="L634"> stringValuesToString(values));</span> <span class="fc" id="L635"> id++;</span> } } } } <span class="fc" id="L640"> } else if (componentCollection.action()</span> <span class="fc bfc" id="L641" title="All 2 branches covered."> .equals(ComponentCollection.ACTION_CREATE)) {</span> <span class="pc bpc" id="L642" title="1 of 2 branches missed."> if (componentCollection.version == null) {</span> <span class="fc" id="L643"> componentCollection.version = searchComponent.getCollectionCache()</span> <span class="fc" id="L644"> .create(componentCollection.id,</span> <span class="fc" id="L645"> componentCollection.values().size(),</span> <span class="fc" id="L646"> componentCollection.values());</span> } <span class="pc bpc" id="L648" title="1 of 2 branches missed."> if (index.containsKey(componentCollection.id)) {</span> <span class="fc" id="L649"> index.get(componentCollection.id).setCreate(</span> <span class="fc" id="L650"> searchComponent.getCollectionCache().now(), searchComponent</span> <span class="fc" id="L651"> .getCollectionCache().check(componentCollection.id));</span> } <span class="fc bfc" id="L653" title="All 2 branches covered."> for (String shardAddress : rb.shards) {</span> ModifiableSolrParams paramsNewRequest; <span class="pc bpc" id="L655" title="1 of 2 branches missed."> if (!requestParamList.containsKey(shardAddress)) {</span> <span class="fc" id="L656"> paramsNewRequest = new ModifiableSolrParams();</span> <span class="fc" id="L657"> requestParamList.put(shardAddress, paramsNewRequest);</span> } else { <span class="nc" id="L659"> paramsNewRequest = requestParamList.get(shardAddress);</span> } <span class="fc" id="L661"> paramsNewRequest.add(PARAM_MTAS_COLLECTION + "." + id + "."</span> + NAME_MTAS_COLLECTION_KEY, componentCollection.key); <span class="fc" id="L663"> paramsNewRequest.add(PARAM_MTAS_COLLECTION + "." + id + "."</span> + NAME_MTAS_COLLECTION_ID, componentCollection.id); <span class="fc" id="L665"> paramsNewRequest.add(</span> PARAM_MTAS_COLLECTION + "." + id + "." + NAME_MTAS_COLLECTION_ACTION, ComponentCollection.ACTION_POST); <span class="fc" id="L669"> paramsNewRequest.add(</span> PARAM_MTAS_COLLECTION + "." + id + "." + NAME_MTAS_COLLECTION_POST, <span class="fc" id="L672"> stringValuesToString(componentCollection.values()));</span> } } <span class="fc" id="L675"> id++;</span> <span class="fc" id="L676"> }</span> // add new requests <span class="fc bfc" id="L678" title="All 2 branches covered."> for (Entry<String, ModifiableSolrParams> entry : requestParamList</span> <span class="fc" id="L679"> .entrySet()) {</span> <span class="fc" id="L680"> ShardRequest newSreq = new ShardRequest();</span> <span class="fc" id="L681"> newSreq.shards = new String[] { entry.getKey() };</span> <span class="fc" id="L682"> newSreq.purpose = ShardRequest.PURPOSE_PRIVATE;</span> <span class="fc" id="L683"> newSreq.params = entry.getValue();</span> <span class="fc" id="L684"> newSreq.params.add("q", "*");</span> <span class="fc" id="L685"> newSreq.params.add("rows", "0");</span> <span class="fc" id="L686"> newSreq.params.add(MtasSolrSearchComponent.PARAM_MTAS,</span> <span class="fc" id="L687"> rb.req.getOriginalParams()</span> <span class="fc" id="L688"> .getParams(MtasSolrSearchComponent.PARAM_MTAS));</span> <span class="fc" id="L689"> newSreq.params.add(PARAM_MTAS_COLLECTION,</span> <span class="fc" id="L690"> rb.req.getOriginalParams().getParams(PARAM_MTAS_COLLECTION));</span> <span class="fc" id="L691"> rb.addRequest(searchComponent, newSreq);</span> <span class="fc" id="L692"> }</span> <span class="pc bpc" id="L693" title="1 of 2 branches missed."> } else if (rb.stage == MtasSolrSearchComponent.STAGE_COLLECTION_FINISH) {</span> // just rewrite ArrayList<Object> mtasResponseCollection; try { <span class="fc" id="L697"> mtasResponseCollection = (ArrayList<Object>) mtasResponse</span> <span class="fc" id="L698"> .get("collection");</span> <span class="pc bpc" id="L699" title="1 of 2 branches missed."> if (mtasResponseCollection != null) {</span> <span class="fc" id="L700"> MtasSolrResultUtil.rewrite(mtasResponseCollection, searchComponent);</span> } <span class="nc" id="L702"> } catch (ClassCastException e) {</span> <span class="nc" id="L703"> log.debug(e);</span> <span class="nc" id="L704"> mtasResponse.remove("collection");</span> <span class="fc" id="L705"> }</span> } } <span class="fc" id="L708"> }</span> /** * Gets the mtas fields. * * @param rb the rb * @return the mtas fields */ private ComponentFields getMtasFields(ResponseBuilder rb) { <span class="fc" id="L717"> return (ComponentFields) rb.req.getContext().get(ComponentFields.class);</span> } /** * String values to string. * * @param stringValues the string values * @return the string */ private static String stringValuesToString(HashSet<String> stringValues) { <span class="fc" id="L727"> return JSONUtil.toJSON(stringValues);</span> } /** * String to string values. * * @param stringValue the string value * @return the hash set * @throws IOException Signals that an I/O exception has occurred. */ private static HashSet<String> stringToStringValues(String stringValue) throws IOException { // should be improved to support escaped characters <span class="fc" id="L740"> HashSet<String> stringValues = new HashSet<>();</span> <span class="fc" id="L741"> JSONParser jsonParser = new JSONParser(stringValue);</span> <span class="fc" id="L742"> int event = jsonParser.nextEvent();</span> <span class="pc bpc" id="L743" title="1 of 2 branches missed."> if (event == JSONParser.ARRAY_START) {</span> <span class="fc bfc" id="L744" title="All 2 branches covered."> while ((event = jsonParser.nextEvent()) != JSONParser.ARRAY_END) {</span> <span class="pc bpc" id="L745" title="1 of 2 branches missed."> if (jsonParser.getLevel() == 1) {</span> <span class="pc bpc" id="L746" title="2 of 4 branches missed."> switch (event) {</span> case JSONParser.STRING: <span class="fc" id="L748"> stringValues.add(jsonParser.getString());</span> <span class="fc" id="L749"> break;</span> case JSONParser.BIGNUMBER: case JSONParser.NUMBER: case JSONParser.LONG: <span class="fc" id="L753"> stringValues.add(jsonParser.getNumberChars().toString());</span> <span class="fc" id="L754"> break;</span> case JSONParser.BOOLEAN: <span class="nc" id="L756"> stringValues.add(Boolean.toString(jsonParser.getBoolean()));</span> <span class="nc" id="L757"> break;</span> default: // do nothing } } } } else { <span class="nc" id="L764"> throw new IOException("unsupported json structure");</span> } <span class="fc" id="L766"> return stringValues;</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>