MtasSolrComponentStats.java.html 70.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
<?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>MtasSolrComponentStats.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">MtasSolrComponentStats.java</span></div><h1>MtasSolrComponentStats.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.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
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.SearchComponent;
import org.apache.solr.handler.component.ShardRequest;
import org.apache.solr.handler.component.ShardResponse;

import mtas.codec.util.CodecComponent.ComponentField;
import mtas.codec.util.CodecComponent.ComponentFields;
import mtas.codec.util.CodecComponent.ComponentPosition;
import mtas.codec.util.CodecComponent.ComponentSpan;
import mtas.codec.util.CodecComponent.ComponentStats;
import mtas.codec.util.CodecComponent.ComponentToken;
import mtas.codec.util.CodecComponent.SubComponentFunction;
import mtas.codec.util.collector.MtasDataCollector;
import mtas.parser.function.ParseException;
import mtas.search.spans.util.MtasSpanQuery;
import mtas.solr.handler.component.MtasSolrSearchComponent;

/**
 * The Class MtasSolrComponentStats.
 */
public class MtasSolrComponentStats
    implements MtasSolrComponent&lt;ComponentStats&gt; {

  /** The Constant log. */
<span class="fc" id="L40">  private static final Log log = LogFactory</span>
<span class="fc" id="L41">      .getLog(MtasSolrComponentStats.class);</span>

  /** The search component. */
  MtasSolrSearchComponent searchComponent;

  /** The Constant PARAM_MTAS_STATS. */
  public static final String PARAM_MTAS_STATS = MtasSolrSearchComponent.PARAM_MTAS
      + &quot;.stats&quot;;

  /** The Constant PARAM_MTAS_STATS_POSITIONS. */
  public static final String PARAM_MTAS_STATS_POSITIONS = PARAM_MTAS_STATS
      + &quot;.positions&quot;;

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

  /** The Constant NAME_MTAS_STATS_POSITIONS_KEY. */
  public static final String NAME_MTAS_STATS_POSITIONS_KEY = &quot;key&quot;;

  /** The Constant NAME_MTAS_STATS_POSITIONS_TYPE. */
  public static final String NAME_MTAS_STATS_POSITIONS_TYPE = &quot;type&quot;;

  /** The Constant NAME_MTAS_STATS_POSITIONS_MINIMUM. */
  public static final String NAME_MTAS_STATS_POSITIONS_MINIMUM = &quot;minimum&quot;;

  /** The Constant NAME_MTAS_STATS_POSITIONS_MAXIMUM. */
  public static final String NAME_MTAS_STATS_POSITIONS_MAXIMUM = &quot;maximum&quot;;

  /** The Constant PARAM_MTAS_STATS_TOKENS. */
  public static final String PARAM_MTAS_STATS_TOKENS = PARAM_MTAS_STATS
      + &quot;.tokens&quot;;

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

  /** The Constant NAME_MTAS_STATS_TOKENS_KEY. */
  public static final String NAME_MTAS_STATS_TOKENS_KEY = &quot;key&quot;;

  /** The Constant NAME_MTAS_STATS_TOKENS_TYPE. */
  public static final String NAME_MTAS_STATS_TOKENS_TYPE = &quot;type&quot;;

  /** The Constant NAME_MTAS_STATS_TOKENS_MINIMUM. */
  public static final String NAME_MTAS_STATS_TOKENS_MINIMUM = &quot;minimum&quot;;

  /** The Constant NAME_MTAS_STATS_TOKENS_MAXIMUM. */
  public static final String NAME_MTAS_STATS_TOKENS_MAXIMUM = &quot;maximum&quot;;

  /** The Constant PARAM_MTAS_STATS_SPANS. */
  public static final String PARAM_MTAS_STATS_SPANS = PARAM_MTAS_STATS
      + &quot;.spans&quot;;

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

  /** The Constant NAME_MTAS_STATS_SPANS_QUERY. */
  public static final String NAME_MTAS_STATS_SPANS_QUERY = &quot;query&quot;;

  /** The Constant NAME_MTAS_STATS_SPANS_EXPAND. */
  public static final String NAME_MTAS_STATS_SPANS_EXPAND = &quot;expand&quot;;

  /** The Constant NAME_MTAS_STATS_SPANS_KEY. */
  public static final String NAME_MTAS_STATS_SPANS_KEY = &quot;key&quot;;

  /** The Constant NAME_MTAS_STATS_SPANS_TYPE. */
  public static final String NAME_MTAS_STATS_SPANS_TYPE = &quot;type&quot;;

  /** The Constant NAME_MTAS_STATS_SPANS_MINIMUM. */
  public static final String NAME_MTAS_STATS_SPANS_MINIMUM = &quot;minimum&quot;;

  /** The Constant NAME_MTAS_STATS_SPANS_MAXIMUM. */
  public static final String NAME_MTAS_STATS_SPANS_MAXIMUM = &quot;maximum&quot;;

  /** The Constant NAME_MTAS_STATS_SPANS_FUNCTION. */
  public static final String NAME_MTAS_STATS_SPANS_FUNCTION = &quot;function&quot;;

  /** The Constant SUBNAME_MTAS_STATS_SPANS_FUNCTION_EXPRESSION. */
  public static final String SUBNAME_MTAS_STATS_SPANS_FUNCTION_EXPRESSION = &quot;expression&quot;;

  /** The Constant SUBNAME_MTAS_STATS_SPANS_FUNCTION_KEY. */
  public static final String SUBNAME_MTAS_STATS_SPANS_FUNCTION_KEY = &quot;key&quot;;

  /** The Constant SUBNAME_MTAS_STATS_SPANS_FUNCTION_TYPE. */
  public static final String SUBNAME_MTAS_STATS_SPANS_FUNCTION_TYPE = &quot;type&quot;;

  /** The Constant SUBNAME_MTAS_STATS_SPANS_QUERY_TYPE. */
  public static final String SUBNAME_MTAS_STATS_SPANS_QUERY_TYPE = &quot;type&quot;;

  /** The Constant SUBNAME_MTAS_STATS_SPANS_QUERY_VALUE. */
  public static final String SUBNAME_MTAS_STATS_SPANS_QUERY_VALUE = &quot;value&quot;;

  /** The Constant SUBNAME_MTAS_STATS_SPANS_QUERY_IGNORE. */
  public static final String SUBNAME_MTAS_STATS_SPANS_QUERY_IGNORE = &quot;ignore&quot;;

  /** The Constant SUBNAME_MTAS_STATS_SPANS_QUERY_MAXIMUM_IGNORE_LENGTH. */
  public static final String SUBNAME_MTAS_STATS_SPANS_QUERY_MAXIMUM_IGNORE_LENGTH = &quot;maximumIgnoreLength&quot;;

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

  /** The Constant SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE. */
  public static final String SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE = &quot;variable&quot;;

  /** The Constant SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE_NAME. */
  public static final String SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE_NAME = &quot;name&quot;;

  /** The Constant SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE_VALUE. */
<span class="fc" id="L147">  public static final String SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE_VALUE = &quot;value&quot;;</span>

  /**
   * Instantiates a new mtas solr component stats.
   *
   * @param searchComponent the search component
   */
<span class="fc" id="L154">  public MtasSolrComponentStats(MtasSolrSearchComponent searchComponent) {</span>
<span class="fc" id="L155">    this.searchComponent = searchComponent;</span>
<span class="fc" id="L156">  }</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 {
<span class="fc bfc" id="L168" title="All 2 branches covered.">    if (rb.req.getParams().getBool(PARAM_MTAS_STATS_POSITIONS, false)) {</span>
<span class="fc" id="L169">      preparePositions(rb, mtasFields);</span>
    }
<span class="fc bfc" id="L171" title="All 2 branches covered.">    if (rb.req.getParams().getBool(PARAM_MTAS_STATS_TOKENS, false)) {</span>
<span class="fc" id="L172">      prepareTokens(rb, mtasFields);</span>
    }
<span class="fc bfc" id="L174" title="All 2 branches covered.">    if (rb.req.getParams().getBool(PARAM_MTAS_STATS_SPANS, false)) {</span>
<span class="fc" id="L175">      prepareSpans(rb, mtasFields);</span>
    }
<span class="fc" id="L177">  }</span>

  /**
   * Prepare positions.
   *
   * @param rb the rb
   * @param mtasFields the mtas fields
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void preparePositions(ResponseBuilder rb, ComponentFields mtasFields)
      throws IOException {
<span class="fc" id="L188">    Set&lt;String&gt; ids = MtasSolrResultUtil</span>
<span class="fc" id="L189">        .getIdsFromParameters(rb.req.getParams(), PARAM_MTAS_STATS_POSITIONS);</span>
<span class="pc bpc" id="L190" title="1 of 2 branches missed.">    if (!ids.isEmpty()) {</span>
<span class="fc" id="L191">      int tmpCounter = 0;</span>
<span class="fc" id="L192">      String[] fields = new String[ids.size()];</span>
<span class="fc" id="L193">      String[] keys = new String[ids.size()];</span>
<span class="fc" id="L194">      String[] minima = new String[ids.size()];</span>
<span class="fc" id="L195">      String[] maxima = new String[ids.size()];</span>
<span class="fc" id="L196">      String[] types = new String[ids.size()];</span>
<span class="fc bfc" id="L197" title="All 2 branches covered.">      for (String id : ids) {</span>
<span class="fc" id="L198">        fields[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_POSITIONS</span>
<span class="fc" id="L199">            + &quot;.&quot; + id + &quot;.&quot; + NAME_MTAS_STATS_POSITIONS_FIELD, null);</span>
<span class="fc" id="L200">        keys[tmpCounter] = rb.req.getParams()</span>
<span class="fc" id="L201">            .get(PARAM_MTAS_STATS_POSITIONS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="fc" id="L202">                + NAME_MTAS_STATS_POSITIONS_KEY, String.valueOf(tmpCounter))</span>
<span class="fc" id="L203">            .trim();</span>
<span class="fc" id="L204">        minima[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_POSITIONS</span>
<span class="fc" id="L205">            + &quot;.&quot; + id + &quot;.&quot; + NAME_MTAS_STATS_POSITIONS_MINIMUM, null);</span>
<span class="fc" id="L206">        maxima[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_POSITIONS</span>
<span class="fc" id="L207">            + &quot;.&quot; + id + &quot;.&quot; + NAME_MTAS_STATS_POSITIONS_MAXIMUM, null);</span>
<span class="fc" id="L208">        types[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_POSITIONS</span>
<span class="fc" id="L209">            + &quot;.&quot; + id + &quot;.&quot; + NAME_MTAS_STATS_POSITIONS_TYPE, null);</span>
<span class="fc" id="L210">        tmpCounter++;</span>
      }
<span class="fc" id="L212">      String uniqueKeyField = rb.req.getSchema().getUniqueKeyField().getName();</span>
<span class="fc" id="L213">      mtasFields.doStats = true;</span>
<span class="fc" id="L214">      mtasFields.doStatsPositions = true;</span>
<span class="fc" id="L215">      rb.setNeedDocSet(true);</span>
<span class="fc bfc" id="L216" title="All 2 branches covered.">      for (String field : fields) {</span>
<span class="pc bpc" id="L217" title="2 of 4 branches missed.">        if (field == null || field.isEmpty()) {</span>
<span class="nc" id="L218">          throw new IOException(&quot;no (valid) field in mtas stats positions&quot;);</span>
<span class="pc bpc" id="L219" title="1 of 2 branches missed.">        } else if (!mtasFields.list.containsKey(field)) {</span>
<span class="fc" id="L220">          mtasFields.list.put(field, new ComponentField(uniqueKeyField));</span>
        }
      }
<span class="fc" id="L223">      MtasSolrResultUtil.compareAndCheck(keys, fields,</span>
<span class="fc" id="L224">          NAME_MTAS_STATS_POSITIONS_KEY, NAME_MTAS_STATS_POSITIONS_FIELD, true);</span>
<span class="fc" id="L225">      MtasSolrResultUtil.compareAndCheck(minima, fields,</span>
<span class="fc" id="L226">          NAME_MTAS_STATS_POSITIONS_MINIMUM, NAME_MTAS_STATS_POSITIONS_FIELD,</span>
<span class="fc" id="L227">          false);</span>
<span class="fc" id="L228">      MtasSolrResultUtil.compareAndCheck(maxima, fields,</span>
<span class="fc" id="L229">          NAME_MTAS_STATS_POSITIONS_MAXIMUM, NAME_MTAS_STATS_POSITIONS_FIELD,</span>
<span class="fc" id="L230">          false);</span>
<span class="fc" id="L231">      MtasSolrResultUtil.compareAndCheck(types, fields,</span>
<span class="fc" id="L232">          NAME_MTAS_STATS_POSITIONS_TYPE, NAME_MTAS_STATS_POSITIONS_FIELD,</span>
<span class="fc" id="L233">          false);</span>
<span class="fc bfc" id="L234" title="All 2 branches covered.">      for (int i = 0; i &lt; fields.length; i++) {</span>
<span class="fc" id="L235">        String field = fields[i];</span>
<span class="fc" id="L236">        String key = keys[i];</span>
<span class="pc bpc" id="L237" title="2 of 4 branches missed.">        String type = (types[i] == null) || (types[i].isEmpty()) ? null</span>
<span class="fc" id="L238">            : types[i].trim();</span>
<span class="pc bpc" id="L239" title="3 of 4 branches missed.">        Double minimum = (minima[i] == null) || (minima[i].isEmpty()) ? null</span>
<span class="nc" id="L240">            : Double.parseDouble(minima[i]);</span>
<span class="pc bpc" id="L241" title="3 of 4 branches missed.">        Double maximum = (maxima[i] == null) || (maxima[i].isEmpty()) ? null</span>
<span class="nc" id="L242">            : Double.parseDouble(maxima[i]);</span>
        try {
<span class="fc" id="L244">          mtasFields.list.get(field).statsPositionList</span>
<span class="fc" id="L245">              .add(new ComponentPosition(key, minimum, maximum, type));</span>
<span class="pc" id="L246">        } catch (ParseException e) {</span>
<span class="nc" id="L247">          throw new IOException(e.getMessage());</span>
        }
      }
    }
<span class="fc" id="L251">  }</span>

  /**
   * Prepare tokens.
   *
   * @param rb the rb
   * @param mtasFields the mtas fields
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void prepareTokens(ResponseBuilder rb, ComponentFields mtasFields)
      throws IOException {
<span class="fc" id="L262">    Set&lt;String&gt; ids = MtasSolrResultUtil</span>
<span class="fc" id="L263">        .getIdsFromParameters(rb.req.getParams(), PARAM_MTAS_STATS_TOKENS);</span>
<span class="pc bpc" id="L264" title="1 of 2 branches missed.">    if (!ids.isEmpty()) {</span>
<span class="fc" id="L265">      int tmpCounter = 0;</span>
<span class="fc" id="L266">      String[] fields = new String[ids.size()];</span>
<span class="fc" id="L267">      String[] keys = new String[ids.size()];</span>
<span class="fc" id="L268">      String[] minima = new String[ids.size()];</span>
<span class="fc" id="L269">      String[] maxima = new String[ids.size()];</span>
<span class="fc" id="L270">      String[] types = new String[ids.size()];</span>
<span class="fc bfc" id="L271" title="All 2 branches covered.">      for (String id : ids) {</span>
<span class="fc" id="L272">        fields[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_TOKENS</span>
<span class="fc" id="L273">            + &quot;.&quot; + id + &quot;.&quot; + NAME_MTAS_STATS_TOKENS_FIELD, null);</span>
<span class="fc" id="L274">        keys[tmpCounter] = rb.req.getParams()</span>
<span class="fc" id="L275">            .get(PARAM_MTAS_STATS_TOKENS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="fc" id="L276">                + NAME_MTAS_STATS_TOKENS_KEY, String.valueOf(tmpCounter))</span>
<span class="fc" id="L277">            .trim();</span>
<span class="fc" id="L278">        minima[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_TOKENS</span>
<span class="fc" id="L279">            + &quot;.&quot; + id + &quot;.&quot; + NAME_MTAS_STATS_TOKENS_MINIMUM, null);</span>
<span class="fc" id="L280">        maxima[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_TOKENS</span>
<span class="fc" id="L281">            + &quot;.&quot; + id + &quot;.&quot; + NAME_MTAS_STATS_TOKENS_MAXIMUM, null);</span>
<span class="fc" id="L282">        types[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_TOKENS + &quot;.&quot;</span>
<span class="fc" id="L283">            + id + &quot;.&quot; + NAME_MTAS_STATS_TOKENS_TYPE, null);</span>
<span class="fc" id="L284">        tmpCounter++;</span>
      }
<span class="fc" id="L286">      String uniqueKeyField = rb.req.getSchema().getUniqueKeyField().getName();</span>
<span class="fc" id="L287">      mtasFields.doStats = true;</span>
<span class="fc" id="L288">      mtasFields.doStatsTokens = true;</span>
<span class="fc" id="L289">      rb.setNeedDocSet(true);</span>
<span class="fc bfc" id="L290" title="All 2 branches covered.">      for (String field : fields) {</span>
<span class="pc bpc" id="L291" title="2 of 4 branches missed.">        if (field == null || field.isEmpty()) {</span>
<span class="nc" id="L292">          throw new IOException(&quot;no (valid) field in mtas stats tokens&quot;);</span>
<span class="fc bfc" id="L293" title="All 2 branches covered.">        } else if (!mtasFields.list.containsKey(field)) {</span>
<span class="fc" id="L294">          mtasFields.list.put(field, new ComponentField(uniqueKeyField));</span>
        }
      }
<span class="fc" id="L297">      MtasSolrResultUtil.compareAndCheck(keys, fields,</span>
<span class="fc" id="L298">          NAME_MTAS_STATS_TOKENS_KEY, NAME_MTAS_STATS_TOKENS_FIELD, true);</span>
<span class="fc" id="L299">      MtasSolrResultUtil.compareAndCheck(minima, fields,</span>
<span class="fc" id="L300">          NAME_MTAS_STATS_TOKENS_MINIMUM, NAME_MTAS_STATS_TOKENS_FIELD, false);</span>
<span class="fc" id="L301">      MtasSolrResultUtil.compareAndCheck(maxima, fields,</span>
<span class="fc" id="L302">          NAME_MTAS_STATS_TOKENS_MAXIMUM, NAME_MTAS_STATS_TOKENS_FIELD, false);</span>
<span class="fc" id="L303">      MtasSolrResultUtil.compareAndCheck(types, fields,</span>
<span class="fc" id="L304">          NAME_MTAS_STATS_TOKENS_TYPE, NAME_MTAS_STATS_TOKENS_FIELD, false);</span>
<span class="fc bfc" id="L305" title="All 2 branches covered.">      for (int i = 0; i &lt; fields.length; i++) {</span>
<span class="fc" id="L306">        String field = fields[i];</span>
<span class="fc" id="L307">        String key = keys[i];</span>
<span class="pc bpc" id="L308" title="2 of 4 branches missed.">        String type = (types[i] == null) || (types[i].isEmpty()) ? null</span>
<span class="fc" id="L309">            : types[i].trim();</span>
<span class="pc bpc" id="L310" title="1 of 4 branches missed.">        Double minimum = (minima[i] == null) || (minima[i].isEmpty()) ? null</span>
<span class="fc" id="L311">            : Double.parseDouble(minima[i]);</span>
<span class="pc bpc" id="L312" title="1 of 4 branches missed.">        Double maximum = (maxima[i] == null) || (maxima[i].isEmpty()) ? null</span>
<span class="fc" id="L313">            : Double.parseDouble(maxima[i]);</span>
        try {
<span class="fc" id="L315">          mtasFields.list.get(field).statsTokenList</span>
<span class="fc" id="L316">              .add(new ComponentToken(key, minimum, maximum, type));</span>
<span class="pc" id="L317">        } catch (ParseException e) {</span>
<span class="nc" id="L318">          throw new IOException(e.getMessage());</span>
        }
      }
    }
<span class="fc" id="L322">  }</span>

  /**
   * Prepare spans.
   *
   * @param rb the rb
   * @param mtasFields the mtas fields
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void prepareSpans(ResponseBuilder rb, ComponentFields mtasFields)
      throws IOException {
<span class="fc" id="L333">    SortedSet&lt;String&gt; ids = MtasSolrResultUtil</span>
<span class="fc" id="L334">        .getIdsFromParameters(rb.req.getParams(), PARAM_MTAS_STATS_SPANS);</span>
<span class="pc bpc" id="L335" title="1 of 2 branches missed.">    if (!ids.isEmpty()) {</span>
<span class="fc" id="L336">      int tmpCounter = 0;</span>
<span class="fc" id="L337">      String[] fields = new String[ids.size()];</span>
<span class="fc" id="L338">      String[] keys = new String[ids.size()];</span>
<span class="fc" id="L339">      String[] minima = new String[ids.size()];</span>
<span class="fc" id="L340">      String[] maxima = new String[ids.size()];</span>
<span class="fc" id="L341">      String[] types = new String[ids.size()];</span>
<span class="fc" id="L342">      String[][] functionExpressions = new String[ids.size()][];</span>
<span class="fc" id="L343">      String[][] functionKeys = new String[ids.size()][];</span>
<span class="fc" id="L344">      String[][] functionTypes = new String[ids.size()][];</span>
<span class="fc" id="L345">      String[][] queryTypes = new String[ids.size()][];</span>
<span class="fc" id="L346">      String[][] queryValues = new String[ids.size()][];</span>
<span class="fc" id="L347">      String[][] queryIgnores = new String[ids.size()][];</span>
<span class="fc" id="L348">      String[][] queryMaximumIgnoreLengths = new String[ids.size()][];</span>
<span class="fc" id="L349">      String[][] queryPrefixes = new String[ids.size()][];</span>
<span class="fc" id="L350">      HashMap&lt;String, String[]&gt;[][] queryVariables = new HashMap[ids.size()][];</span>
<span class="fc" id="L351">      Boolean[] expand = new Boolean[ids.size()];</span>
<span class="fc bfc" id="L352" title="All 2 branches covered.">      for (String id : ids) {</span>
<span class="fc" id="L353">        fields[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_SPANS + &quot;.&quot;</span>
<span class="fc" id="L354">            + id + &quot;.&quot; + NAME_MTAS_STATS_SPANS_FIELD, null);</span>
<span class="fc" id="L355">        keys[tmpCounter] = rb.req.getParams().get(</span>
<span class="fc" id="L356">            PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot; + NAME_MTAS_STATS_SPANS_KEY,</span>
<span class="fc" id="L357">            String.valueOf(tmpCounter)).trim();</span>
<span class="fc" id="L358">        minima[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_SPANS + &quot;.&quot;</span>
<span class="fc" id="L359">            + id + &quot;.&quot; + NAME_MTAS_STATS_SPANS_MINIMUM, null);</span>
<span class="fc" id="L360">        maxima[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_SPANS + &quot;.&quot;</span>
<span class="fc" id="L361">            + id + &quot;.&quot; + NAME_MTAS_STATS_SPANS_MAXIMUM, null);</span>
<span class="fc" id="L362">        types[tmpCounter] = rb.req.getParams().get(PARAM_MTAS_STATS_SPANS + &quot;.&quot;</span>
<span class="fc" id="L363">            + id + &quot;.&quot; + NAME_MTAS_STATS_SPANS_TYPE, null);</span>
<span class="fc" id="L364">        Set&lt;String&gt; functionIds = MtasSolrResultUtil</span>
<span class="fc" id="L365">            .getIdsFromParameters(rb.req.getParams(), PARAM_MTAS_STATS_SPANS</span>
<span class="fc" id="L366">                + &quot;.&quot; + id + &quot;.&quot; + NAME_MTAS_STATS_SPANS_FUNCTION);</span>
<span class="fc" id="L367">        functionExpressions[tmpCounter] = new String[functionIds.size()];</span>
<span class="fc" id="L368">        functionKeys[tmpCounter] = new String[functionIds.size()];</span>
<span class="fc" id="L369">        functionTypes[tmpCounter] = new String[functionIds.size()];</span>
<span class="fc" id="L370">        int tmpSubCounter = 0;</span>
<span class="pc bpc" id="L371" title="1 of 2 branches missed.">        for (String functionId : functionIds) {</span>
<span class="nc" id="L372">          functionKeys[tmpCounter][tmpSubCounter] = rb.req.getParams()</span>
<span class="nc" id="L373">              .get(</span>
<span class="nc" id="L374">                  PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="nc" id="L375">                      + NAME_MTAS_STATS_SPANS_FUNCTION + &quot;.&quot; + functionId + &quot;.&quot;</span>
<span class="nc" id="L376">                      + SUBNAME_MTAS_STATS_SPANS_FUNCTION_KEY,</span>
<span class="nc" id="L377">                  String.valueOf(tmpSubCounter))</span>
<span class="nc" id="L378">              .trim();</span>
<span class="nc" id="L379">          functionExpressions[tmpCounter][tmpSubCounter] = rb.req.getParams()</span>
<span class="nc" id="L380">              .get(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="nc" id="L381">                  + NAME_MTAS_STATS_SPANS_FUNCTION + &quot;.&quot; + functionId + &quot;.&quot;</span>
<span class="nc" id="L382">                  + SUBNAME_MTAS_STATS_SPANS_FUNCTION_EXPRESSION, null);</span>
<span class="nc" id="L383">          functionTypes[tmpCounter][tmpSubCounter] = rb.req.getParams()</span>
<span class="nc" id="L384">              .get(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="nc" id="L385">                  + NAME_MTAS_STATS_SPANS_FUNCTION + &quot;.&quot; + functionId + &quot;.&quot;</span>
<span class="nc" id="L386">                  + SUBNAME_MTAS_STATS_SPANS_FUNCTION_TYPE, null);</span>
<span class="nc" id="L387">          tmpSubCounter++;</span>
        }

<span class="fc" id="L390">        Set&lt;String&gt; qIds = MtasSolrResultUtil</span>
<span class="fc" id="L391">            .getIdsFromParameters(rb.req.getParams(), PARAM_MTAS_STATS_SPANS</span>
<span class="fc" id="L392">                + &quot;.&quot; + id + &quot;.&quot; + NAME_MTAS_STATS_SPANS_QUERY);</span>
<span class="pc bpc" id="L393" title="1 of 2 branches missed.">        if (!qIds.isEmpty()) {</span>
<span class="fc" id="L394">          int tmpQCounter = 0;</span>
<span class="fc" id="L395">          queryTypes[tmpCounter] = new String[qIds.size()];</span>
<span class="fc" id="L396">          queryValues[tmpCounter] = new String[qIds.size()];</span>
<span class="fc" id="L397">          queryIgnores[tmpCounter] = new String[qIds.size()];</span>
<span class="fc" id="L398">          queryMaximumIgnoreLengths[tmpCounter] = new String[qIds.size()];</span>
<span class="fc" id="L399">          queryPrefixes[tmpCounter] = new String[qIds.size()];</span>
<span class="fc" id="L400">          queryVariables[tmpCounter] = new HashMap[qIds.size()];</span>
<span class="fc bfc" id="L401" title="All 2 branches covered.">          for (String qId : qIds) {</span>
<span class="fc" id="L402">            queryTypes[tmpCounter][tmpQCounter] = rb.req.getParams()</span>
<span class="fc" id="L403">                .get(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="fc" id="L404">                    + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + qId + &quot;.&quot;</span>
<span class="fc" id="L405">                    + SUBNAME_MTAS_STATS_SPANS_QUERY_TYPE, null);</span>
<span class="fc" id="L406">            queryValues[tmpCounter][tmpQCounter] = rb.req.getParams()</span>
<span class="fc" id="L407">                .get(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="fc" id="L408">                    + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + qId + &quot;.&quot;</span>
<span class="fc" id="L409">                    + SUBNAME_MTAS_STATS_SPANS_QUERY_VALUE, null);</span>
<span class="fc" id="L410">            queryIgnores[tmpCounter][tmpQCounter] = rb.req.getParams()</span>
<span class="fc" id="L411">                .get(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="fc" id="L412">                    + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + qId + &quot;.&quot;</span>
<span class="fc" id="L413">                    + SUBNAME_MTAS_STATS_SPANS_QUERY_IGNORE, null);</span>
<span class="fc" id="L414">            queryMaximumIgnoreLengths[tmpCounter][tmpQCounter] = rb.req</span>
<span class="fc" id="L415">                .getParams().get(</span>
<span class="fc" id="L416">                    PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="fc" id="L417">                        + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + qId + &quot;.&quot;</span>
<span class="fc" id="L418">                        + SUBNAME_MTAS_STATS_SPANS_QUERY_MAXIMUM_IGNORE_LENGTH,</span>
<span class="fc" id="L419">                    null);</span>
<span class="fc" id="L420">            queryPrefixes[tmpCounter][tmpQCounter] = rb.req.getParams()</span>
<span class="fc" id="L421">                .get(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="fc" id="L422">                    + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + qId + &quot;.&quot;</span>
<span class="fc" id="L423">                    + SUBNAME_MTAS_STATS_SPANS_QUERY_PREFIX, null);</span>
<span class="fc" id="L424">            Set&lt;String&gt; vIds = MtasSolrResultUtil.getIdsFromParameters(</span>
<span class="fc" id="L425">                rb.req.getParams(),</span>
<span class="fc" id="L426">                PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="fc" id="L427">                    + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + qId + &quot;.&quot;</span>
<span class="fc" id="L428">                    + SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE);</span>
<span class="fc" id="L429">            queryVariables[tmpCounter][tmpQCounter] = new HashMap&lt;&gt;();</span>
<span class="fc bfc" id="L430" title="All 2 branches covered.">            if (!vIds.isEmpty()) {</span>
<span class="fc" id="L431">              HashMap&lt;String, ArrayList&lt;String&gt;&gt; tmpVariables = new HashMap&lt;&gt;();</span>
<span class="fc bfc" id="L432" title="All 2 branches covered.">              for (String vId : vIds) {</span>
<span class="fc" id="L433">                String name = rb.req.getParams()</span>
<span class="fc" id="L434">                    .get(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="fc" id="L435">                        + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + qId + &quot;.&quot;</span>
<span class="fc" id="L436">                        + SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE + &quot;.&quot; + vId</span>
<span class="fc" id="L437">                        + &quot;.&quot; + SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE_NAME,</span>
<span class="fc" id="L438">                        null);</span>
<span class="pc bpc" id="L439" title="1 of 2 branches missed.">                if (name != null) {</span>
<span class="pc bpc" id="L440" title="1 of 2 branches missed.">                  if (!tmpVariables.containsKey(name)) {</span>
<span class="fc" id="L441">                    tmpVariables.put(name, new ArrayList&lt;String&gt;());</span>
                  }
<span class="fc" id="L443">                  String value = rb.req.getParams()</span>
<span class="fc" id="L444">                      .get(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="fc" id="L445">                          + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + qId + &quot;.&quot;</span>
<span class="fc" id="L446">                          + SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE + &quot;.&quot; + vId</span>
<span class="fc" id="L447">                          + &quot;.&quot; + SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE_VALUE,</span>
<span class="fc" id="L448">                          null);</span>
<span class="pc bpc" id="L449" title="1 of 2 branches missed.">                  if (value != null) {</span>
<span class="fc" id="L450">                    ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L451">                    String[] subList = value.split(&quot;(?&lt;!\\\\),&quot;);</span>
<span class="fc bfc" id="L452" title="All 2 branches covered.">                    for (int i = 0; i &lt; subList.length; i++) {</span>
<span class="fc" id="L453">                      list.add(</span>
<span class="fc" id="L454">                          subList[i].replace(&quot;\\,&quot;, &quot;,&quot;).replace(&quot;\\\\&quot;, &quot;\\&quot;));</span>
                    }
<span class="fc" id="L456">                    tmpVariables.get(name).addAll(list);</span>
                  }
                }
              }
<span class="fc bfc" id="L460" title="All 2 branches covered.">              for (Entry&lt;String, ArrayList&lt;String&gt;&gt; entry : tmpVariables</span>
<span class="fc" id="L461">                  .entrySet()) {</span>
<span class="fc" id="L462">                queryVariables[tmpCounter][tmpQCounter].put(entry.getKey(),</span>
<span class="fc" id="L463">                    entry.getValue()</span>
<span class="fc" id="L464">                        .toArray(new String[entry.getValue().size()]));</span>
              }
            }
<span class="fc" id="L467">            tmpQCounter++;</span>
          }
<span class="fc" id="L469">        } else {</span>
<span class="nc" id="L470">          throw new IOException(&quot;no &quot; + NAME_MTAS_STATS_SPANS_QUERY</span>
<span class="nc" id="L471">              + &quot; for mtas stats span &quot; + id);</span>
        }
<span class="fc" id="L473">        if (rb.req.getParams().getBool(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + id + &quot;.&quot;</span>
<span class="pc bpc" id="L474" title="1 of 2 branches missed.">            + NAME_MTAS_STATS_SPANS_EXPAND, false)) {</span>
<span class="nc" id="L475">          expand[tmpCounter] = true;</span>
<span class="nc" id="L476">        } else {</span>
<span class="fc" id="L477">          expand[tmpCounter] = false;</span>
        }
<span class="fc" id="L479">        tmpCounter++;</span>
      }
<span class="fc" id="L481">      String uniqueKeyField = rb.req.getSchema().getUniqueKeyField().getName();</span>
<span class="fc" id="L482">      mtasFields.doStats = true;</span>
<span class="fc" id="L483">      mtasFields.doStatsSpans = true;</span>
<span class="fc" id="L484">      rb.setNeedDocSet(true);</span>
<span class="fc bfc" id="L485" title="All 2 branches covered.">      for (String field : fields) {</span>
<span class="pc bpc" id="L486" title="2 of 4 branches missed.">        if (field == null || field.isEmpty()) {</span>
<span class="nc" id="L487">          throw new IOException(&quot;no (valid) field in mtas stats spans&quot;);</span>
<span class="fc bfc" id="L488" title="All 2 branches covered.">        } else if (!mtasFields.list.containsKey(field)) {</span>
<span class="fc" id="L489">          mtasFields.list.put(field, new ComponentField(uniqueKeyField));</span>
        }
      }
<span class="fc" id="L492">      MtasSolrResultUtil.compareAndCheck(keys, fields,</span>
<span class="fc" id="L493">          NAME_MTAS_STATS_SPANS_KEY, NAME_MTAS_STATS_SPANS_FIELD, true);</span>
<span class="fc" id="L494">      MtasSolrResultUtil.compareAndCheck(minima, fields,</span>
<span class="fc" id="L495">          NAME_MTAS_STATS_SPANS_MINIMUM, NAME_MTAS_STATS_SPANS_FIELD, false);</span>
<span class="fc" id="L496">      MtasSolrResultUtil.compareAndCheck(maxima, fields,</span>
<span class="fc" id="L497">          NAME_MTAS_STATS_SPANS_MAXIMUM, NAME_MTAS_STATS_SPANS_FIELD, false);</span>
<span class="fc" id="L498">      MtasSolrResultUtil.compareAndCheck(types, fields,</span>
<span class="fc" id="L499">          NAME_MTAS_STATS_SPANS_TYPE, NAME_MTAS_STATS_SPANS_FIELD, false);</span>
<span class="fc" id="L500">      MtasSolrResultUtil.compareAndCheck(types, fields,</span>
<span class="fc" id="L501">          NAME_MTAS_STATS_SPANS_FUNCTION, NAME_MTAS_STATS_SPANS_FIELD, false);</span>

<span class="fc bfc" id="L503" title="All 2 branches covered.">      for (int i = 0; i &lt; fields.length; i++) {</span>
<span class="fc" id="L504">        ComponentField cf = mtasFields.list.get(fields[i]);</span>
<span class="fc" id="L505">        int queryNumber = queryValues[i].length;</span>
<span class="fc" id="L506">        MtasSpanQuery[] ql = new MtasSpanQuery[queryNumber];</span>
<span class="fc bfc" id="L507" title="All 2 branches covered.">        for (int j = 0; j &lt; queryNumber; j++) {</span>
<span class="pc bpc" id="L508" title="1 of 2 branches missed.">          Integer maximumIgnoreLength = (queryMaximumIgnoreLengths[i][j] == null)</span>
<span class="pc" id="L509">              ? null : Integer.parseInt(queryMaximumIgnoreLengths[i][j]);</span>
<span class="fc" id="L510">          MtasSpanQuery q = MtasSolrResultUtil.constructQuery(queryValues[i][j],</span>
<span class="fc" id="L511">              queryTypes[i][j], queryPrefixes[i][j], queryVariables[i][j],</span>
<span class="fc" id="L512">              fields[i], queryIgnores[i][j], maximumIgnoreLength);</span>
          // minimize number of queries
<span class="fc bfc" id="L514" title="All 2 branches covered.">          if (cf.spanQueryList.contains(q)) {</span>
<span class="fc" id="L515">            q = cf.spanQueryList.get(cf.spanQueryList.indexOf(q));</span>
<span class="fc" id="L516">          } else {</span>
<span class="fc" id="L517">            cf.spanQueryList.add(q);</span>
          }
<span class="fc" id="L519">          ql[j] = q;</span>
        }
<span class="pc bpc" id="L521" title="1 of 4 branches missed.">        Double minimum = (minima[i] == null) || (minima[i].isEmpty()) ? null</span>
<span class="fc" id="L522">            : Double.parseDouble(minima[i]);</span>
<span class="pc bpc" id="L523" title="3 of 4 branches missed.">        Double maximum = (maxima[i] == null) || (maxima[i].isEmpty()) ? null</span>
<span class="nc" id="L524">            : Double.parseDouble(maxima[i]);</span>
<span class="pc bpc" id="L525" title="2 of 4 branches missed.">        String key = (keys[i] == null) || (keys[i].isEmpty())</span>
<span class="nc" id="L526">            ? String.valueOf(i) + &quot;:&quot; + fields[i] + &quot;:&quot; + queryNumber</span>
<span class="fc" id="L527">            : keys[i].trim();</span>
<span class="pc bpc" id="L528" title="2 of 4 branches missed.">        String type = (types[i] == null) || (types[i].isEmpty()) ? null</span>
<span class="fc" id="L529">            : types[i].trim();</span>
<span class="fc" id="L530">        String[] functionKey = functionKeys[i];</span>
<span class="fc" id="L531">        String[] functionExpression = functionExpressions[i];</span>
<span class="fc" id="L532">        String[] functionType = functionTypes[i];</span>
        try {
<span class="fc" id="L534">          mtasFields.list.get(fields[i]).statsSpanList</span>
<span class="fc" id="L535">              .add(new ComponentSpan(ql, key, minimum, maximum, type,</span>
<span class="fc" id="L536">                  functionKey, functionExpression, functionType));</span>
<span class="pc" id="L537">        } catch (ParseException e) {</span>
<span class="nc" id="L538">          throw new IOException(e.getMessage());</span>
        }
<span class="pc bpc" id="L540" title="1 of 2 branches missed.">        if (expand[i]) {</span>
<span class="nc" id="L541">          HashMap&lt;String, String[]&gt;[][] expandedQueryVariables = expandedQueryVariables(</span>
<span class="nc" id="L542">              queryVariables[i]);</span>
<span class="nc bnc" id="L543" title="All 2 branches missed.">          for (int e = 0; e &lt; expandedQueryVariables.length; e++) {</span>
<span class="nc" id="L544">            MtasSpanQuery[] eql = new MtasSpanQuery[queryNumber];</span>
<span class="nc bnc" id="L545" title="All 2 branches missed.">            for (int j = 0; j &lt; queryNumber; j++) {</span>
<span class="nc bnc" id="L546" title="All 2 branches missed.">              Integer maximumIgnoreLength = (queryMaximumIgnoreLengths[i][j] == null)</span>
<span class="nc" id="L547">                  ? null : Integer.parseInt(queryMaximumIgnoreLengths[i][j]);</span>
<span class="nc" id="L548">              MtasSpanQuery q = MtasSolrResultUtil.constructQuery(</span>
<span class="nc" id="L549">                  queryValues[i][j], queryTypes[i][j], queryPrefixes[i][j],</span>
<span class="nc" id="L550">                  expandedQueryVariables[e][j], fields[i], queryIgnores[i][j],</span>
<span class="nc" id="L551">                  maximumIgnoreLength);</span>
              // minimize number of queries
<span class="nc bnc" id="L553" title="All 2 branches missed.">              if (cf.spanQueryList.contains(q)) {</span>
<span class="nc" id="L554">                q = cf.spanQueryList.get(cf.spanQueryList.indexOf(q));</span>
<span class="nc" id="L555">              } else {</span>
<span class="nc" id="L556">                cf.spanQueryList.add(q);</span>
              }
<span class="nc" id="L558">              eql[j] = q;</span>
            }
<span class="nc" id="L560">            String newKey = generateKey(key + &quot; (&quot; + e + &quot;)&quot;,</span>
<span class="nc" id="L561">                expandedQueryVariables[e]);</span>
            try {
<span class="nc" id="L563">              mtasFields.list.get(fields[i]).statsSpanList</span>
<span class="nc" id="L564">                  .add(new ComponentSpan(eql, newKey, minimum, maximum, type,</span>
<span class="nc" id="L565">                      functionKey, functionExpression, functionType));</span>
<span class="nc" id="L566">            } catch (ParseException ee) {</span>
<span class="nc" id="L567">              throw new IOException(ee.getMessage());</span>
            }
          }
        }
      }
<span class="fc" id="L572">    } else {</span>
<span class="nc" id="L573">      throw new IOException(&quot;missing parameters stats spans&quot;);</span>
    }
<span class="fc" id="L575">  }</span>

  /**
   * Generate key.
   *
   * @param key the key
   * @param queryVariables the query variables
   * @return the string
   */
  private String generateKey(String key,
      HashMap&lt;String, String[]&gt;[] queryVariables) {
<span class="nc" id="L586">    StringBuilder newKey = new StringBuilder(key);</span>
<span class="nc" id="L587">    newKey.append(&quot; -&quot;);</span>
<span class="nc bnc" id="L588" title="All 2 branches missed.">    for (int q = 0; q &lt; queryVariables.length; q++) {</span>
<span class="nc bnc" id="L589" title="All 4 branches missed.">      if (queryVariables[q] != null &amp;&amp; queryVariables[q].size() &gt; 0) {</span>
<span class="nc bnc" id="L590" title="All 2 branches missed.">        for (String name : queryVariables[q].keySet()) {</span>
<span class="nc" id="L591">          newKey.append(&quot; q&quot; + q + &quot;:$&quot; + name + &quot;=&quot;);</span>
<span class="nc bnc" id="L592" title="All 2 branches missed.">          if (queryVariables[q].get(name) != null</span>
<span class="nc bnc" id="L593" title="All 2 branches missed.">              &amp;&amp; queryVariables[q].get(name).length == 1) {</span>
<span class="nc" id="L594">            newKey.append(&quot;'&quot; + queryVariables[q].get(name)[0]</span>
<span class="nc" id="L595">                .replace(&quot;\\&quot;, &quot;\\\\&quot;).replace(&quot;,&quot;, &quot;\\,&quot;) + &quot;'&quot;);</span>
<span class="nc" id="L596">          } else {</span>
<span class="nc" id="L597">            newKey.append(&quot;-&quot;);</span>
          }
        }
      }
    }
<span class="nc" id="L602">    return newKey.toString();</span>
  }

  /**
   * Expanded query variables.
   *
   * @param queryVariables the query variables
   * @return the hash map[][]
   */
  private HashMap&lt;String, String[]&gt;[][] expandedQueryVariables(
      HashMap&lt;String, String[]&gt;[] queryVariables) {
<span class="nc" id="L613">    HashMap&lt;String, String[]&gt;[][] subResult = new HashMap[queryVariables.length][];</span>
<span class="nc bnc" id="L614" title="All 2 branches missed.">    for (int q = 0; q &lt; queryVariables.length; q++) {</span>
<span class="nc" id="L615">      subResult[q] = expandedQueryVariables(queryVariables[q]);</span>
    }
<span class="nc" id="L617">    ArrayList&lt;HashMap&lt;String, String[]&gt;[]&gt; result = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L618">    generatePermutations(result, 0, subResult);</span>
<span class="nc" id="L619">    return result.toArray(new HashMap[result.size()][]);</span>
  }

  /**
   * Generate permutations.
   *
   * @param result the result
   * @param index the index
   * @param subResult the sub result
   */
  private void generatePermutations(
      ArrayList&lt;HashMap&lt;String, String[]&gt;[]&gt; result, int index,
      HashMap&lt;String, String[]&gt;[][] subResult) {
<span class="nc" id="L632">    int localIndex = index;</span>
<span class="nc" id="L633">    HashMap&lt;String, String[]&gt;[] value = subResult[localIndex];</span>
<span class="nc bnc" id="L634" title="All 2 branches missed.">    if (localIndex == 0) {</span>
<span class="nc bnc" id="L635" title="All 2 branches missed.">      for (int i = 0; i &lt; value.length; i++) {</span>
<span class="nc" id="L636">        HashMap&lt;String, String[]&gt;[] resultItem = new HashMap[subResult.length];</span>
<span class="nc" id="L637">        resultItem[localIndex] = value[i];</span>
<span class="nc" id="L638">        result.add(resultItem);</span>
      }
<span class="nc" id="L640">    } else {</span>
<span class="nc" id="L641">      ArrayList&lt;HashMap&lt;String, String[]&gt;[]&gt; newResult = new ArrayList&lt;&gt;();</span>
<span class="nc bnc" id="L642" title="All 2 branches missed.">      for (int e = 0; e &lt; result.size(); e++) {</span>
<span class="nc bnc" id="L643" title="All 2 branches missed.">        for (int i = 0; i &lt; value.length; i++) {</span>
<span class="nc" id="L644">          HashMap&lt;String, String[]&gt;[] resultItem = result.get(e);</span>
<span class="nc" id="L645">          resultItem[localIndex] = value[i];</span>
<span class="nc" id="L646">          newResult.add(resultItem);</span>
        }
      }
<span class="nc" id="L649">      result.clear();</span>
<span class="nc" id="L650">      result.addAll(newResult);</span>
    }
<span class="nc" id="L652">    localIndex++;</span>
<span class="nc bnc" id="L653" title="All 2 branches missed.">    if (localIndex &lt; subResult.length) {</span>
<span class="nc" id="L654">      generatePermutations(result, localIndex, subResult);</span>
    }
<span class="nc" id="L656">  }</span>

  /**
   * Expanded query variables.
   *
   * @param queryVariables the query variables
   * @return the hash map[]
   */
  private HashMap&lt;String, String[]&gt;[] expandedQueryVariables(
      HashMap&lt;String, String[]&gt; queryVariables) {
<span class="nc" id="L666">    ArrayList&lt;HashMap&lt;String, String[]&gt;&gt; result = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L667">    Set&lt;String&gt; keys = queryVariables.keySet();</span>
<span class="nc" id="L668">    generatePermutationsQueryVariables(result, keys, queryVariables);</span>
<span class="nc" id="L669">    return result.toArray(new HashMap[result.size()]);</span>
  }

  /**
   * Generate permutations query variables.
   *
   * @param result the result
   * @param keys the keys
   * @param queryVariables the query variables
   */
  private void generatePermutationsQueryVariables(
      ArrayList&lt;HashMap&lt;String, String[]&gt;&gt; result, Set&lt;String&gt; keys,
      HashMap&lt;String, String[]&gt; queryVariables) {
<span class="nc bnc" id="L682" title="All 4 branches missed.">    if (keys != null &amp;&amp; !keys.isEmpty()) {</span>
<span class="nc" id="L683">      Set&lt;String&gt; newKeys = new HashSet&lt;&gt;();</span>
<span class="nc" id="L684">      Iterator&lt;String&gt; it = keys.iterator();</span>
<span class="nc" id="L685">      String key = it.next();</span>
<span class="nc" id="L686">      String[] value = queryVariables.get(key);</span>
<span class="nc bnc" id="L687" title="All 2 branches missed.">      if (result.isEmpty()) {</span>
        HashMap&lt;String, String[]&gt; newItem;
<span class="nc bnc" id="L689" title="All 4 branches missed.">        if (value == null || value.length == 0) {</span>
<span class="nc" id="L690">          newItem = new HashMap&lt;&gt;();</span>
<span class="nc" id="L691">          newItem.put(key, value);</span>
<span class="nc" id="L692">          result.add(newItem);</span>
<span class="nc" id="L693">        } else {</span>
<span class="nc bnc" id="L694" title="All 2 branches missed.">          for (int j = 0; j &lt; value.length; j++) {</span>
<span class="nc" id="L695">            newItem = new HashMap&lt;&gt;();</span>
<span class="nc" id="L696">            newItem.put(key, new String[] { value[j] });</span>
<span class="nc" id="L697">            result.add(newItem);</span>
          }
        }
<span class="nc" id="L700">      } else {</span>
<span class="nc" id="L701">        ArrayList&lt;HashMap&lt;String, String[]&gt;&gt; newResult = new ArrayList&lt;&gt;();</span>
<span class="nc bnc" id="L702" title="All 2 branches missed.">        for (int i = 0; i &lt; result.size(); i++) {</span>
          HashMap&lt;String, String[]&gt; newItem;
<span class="nc bnc" id="L704" title="All 4 branches missed.">          if (value == null || value.length == 0) {</span>
<span class="nc" id="L705">            newItem = (HashMap&lt;String, String[]&gt;) result.get(i).clone();</span>
<span class="nc" id="L706">            newItem.put(key, value);</span>
<span class="nc" id="L707">            newResult.add(newItem);</span>
<span class="nc" id="L708">          } else {</span>
<span class="nc bnc" id="L709" title="All 2 branches missed.">            for (int j = 0; j &lt; value.length; j++) {</span>
<span class="nc" id="L710">              newItem = (HashMap&lt;String, String[]&gt;) result.get(i).clone();</span>
<span class="nc" id="L711">              newItem.put(key, new String[] { value[j] });</span>
<span class="nc" id="L712">              newResult.add(newItem);</span>
            }
          }
        }
<span class="nc" id="L716">        result.clear();</span>
<span class="nc" id="L717">        result.addAll(newResult);</span>
      }
<span class="nc bnc" id="L719" title="All 2 branches missed.">      while (it.hasNext()) {</span>
<span class="nc" id="L720">        newKeys.add(it.next());</span>
      }
<span class="nc" id="L722">      generatePermutationsQueryVariables(result, newKeys, queryVariables);</span>
    }
<span class="nc" id="L724">  }</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) {
<span class="fc bfc" id="L737" title="All 2 branches covered.">    if ((sreq.purpose &amp; ShardRequest.PURPOSE_GET_TOP_IDS) != 0) {</span>
      // do nothing
    } else {
      // remove stats for other requests
<span class="fc" id="L741">      sreq.params.remove(PARAM_MTAS_STATS);</span>
<span class="fc" id="L742">      sreq.params.remove(PARAM_MTAS_STATS_POSITIONS);</span>
<span class="fc" id="L743">      Set&lt;String&gt; keys = MtasSolrResultUtil</span>
<span class="fc" id="L744">          .getIdsFromParameters(rb.req.getParams(), PARAM_MTAS_STATS_POSITIONS);</span>
<span class="fc bfc" id="L745" title="All 2 branches covered.">      for (String key : keys) {</span>
<span class="fc" id="L746">        sreq.params.remove(</span>
<span class="fc" id="L747">            PARAM_MTAS_STATS + &quot;.&quot; + key + &quot;.&quot; + NAME_MTAS_STATS_POSITIONS_KEY);</span>
<span class="fc" id="L748">        sreq.params.remove(PARAM_MTAS_STATS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L749">            + NAME_MTAS_STATS_POSITIONS_FIELD);</span>
<span class="fc" id="L750">        sreq.params.remove(PARAM_MTAS_STATS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L751">            + NAME_MTAS_STATS_POSITIONS_TYPE);</span>
<span class="fc" id="L752">        sreq.params.remove(PARAM_MTAS_STATS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L753">            + NAME_MTAS_STATS_POSITIONS_MAXIMUM);</span>
<span class="fc" id="L754">        sreq.params.remove(PARAM_MTAS_STATS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L755">            + NAME_MTAS_STATS_POSITIONS_MINIMUM);</span>
      }
<span class="fc" id="L757">      sreq.params.remove(PARAM_MTAS_STATS_TOKENS);</span>
<span class="fc" id="L758">      keys = MtasSolrResultUtil.getIdsFromParameters(rb.req.getParams(),</span>
<span class="fc" id="L759">          PARAM_MTAS_STATS_TOKENS);</span>
<span class="fc bfc" id="L760" title="All 2 branches covered.">      for (String key : keys) {</span>
<span class="fc" id="L761">        sreq.params.remove(</span>
<span class="fc" id="L762">            PARAM_MTAS_STATS + &quot;.&quot; + key + &quot;.&quot; + NAME_MTAS_STATS_TOKENS_KEY);</span>
<span class="fc" id="L763">        sreq.params.remove(</span>
<span class="fc" id="L764">            PARAM_MTAS_STATS + &quot;.&quot; + key + &quot;.&quot; + NAME_MTAS_STATS_TOKENS_FIELD);</span>
<span class="fc" id="L765">        sreq.params.remove(</span>
<span class="fc" id="L766">            PARAM_MTAS_STATS + &quot;.&quot; + key + &quot;.&quot; + NAME_MTAS_STATS_TOKENS_TYPE);</span>
<span class="fc" id="L767">        sreq.params.remove(PARAM_MTAS_STATS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L768">            + NAME_MTAS_STATS_TOKENS_MAXIMUM);</span>
<span class="fc" id="L769">        sreq.params.remove(PARAM_MTAS_STATS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L770">            + NAME_MTAS_STATS_TOKENS_MINIMUM);</span>
      }
<span class="fc" id="L772">      sreq.params.remove(PARAM_MTAS_STATS_SPANS);</span>
<span class="fc" id="L773">      keys = MtasSolrResultUtil.getIdsFromParameters(rb.req.getParams(),</span>
<span class="fc" id="L774">          PARAM_MTAS_STATS_SPANS);</span>
<span class="fc bfc" id="L775" title="All 2 branches covered.">      for (String key : keys) {</span>
<span class="fc" id="L776">        sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L777">            + NAME_MTAS_STATS_SPANS_KEY);</span>
<span class="fc" id="L778">        sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L779">            + NAME_MTAS_STATS_SPANS_FIELD);</span>
<span class="fc" id="L780">        sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L781">            + NAME_MTAS_STATS_SPANS_TYPE);</span>
<span class="fc" id="L782">        sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L783">            + NAME_MTAS_STATS_SPANS_MAXIMUM);</span>
<span class="fc" id="L784">        sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L785">            + NAME_MTAS_STATS_SPANS_MINIMUM);</span>
<span class="fc" id="L786">        Set&lt;String&gt; subKeys = MtasSolrResultUtil</span>
<span class="fc" id="L787">            .getIdsFromParameters(rb.req.getParams(), PARAM_MTAS_STATS_SPANS</span>
<span class="fc" id="L788">                + &quot;.&quot; + key + &quot;.&quot; + NAME_MTAS_STATS_SPANS_FUNCTION);</span>
<span class="pc bpc" id="L789" title="1 of 2 branches missed.">        for (String subKey : subKeys) {</span>
<span class="nc" id="L790">          sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="nc" id="L791">              + NAME_MTAS_STATS_SPANS_FUNCTION + &quot;.&quot; + subKey + &quot;.&quot;</span>
<span class="nc" id="L792">              + SUBNAME_MTAS_STATS_SPANS_FUNCTION_EXPRESSION);</span>
<span class="nc" id="L793">          sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="nc" id="L794">              + NAME_MTAS_STATS_SPANS_FUNCTION + &quot;.&quot; + subKey + &quot;.&quot;</span>
<span class="nc" id="L795">              + SUBNAME_MTAS_STATS_SPANS_FUNCTION_KEY);</span>
<span class="nc" id="L796">          sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="nc" id="L797">              + NAME_MTAS_STATS_SPANS_FUNCTION + &quot;.&quot; + subKey + &quot;.&quot;</span>
<span class="nc" id="L798">              + SUBNAME_MTAS_STATS_SPANS_FUNCTION_TYPE);</span>
        }
<span class="fc" id="L800">        subKeys = MtasSolrResultUtil.getIdsFromParameters(rb.req.getParams(),</span>
<span class="fc" id="L801">            PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L802">                + NAME_MTAS_STATS_SPANS_QUERY);</span>
<span class="fc bfc" id="L803" title="All 2 branches covered.">        for (String subKey : subKeys) {</span>
<span class="fc" id="L804">          sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L805">              + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + subKey + &quot;.&quot;</span>
<span class="fc" id="L806">              + SUBNAME_MTAS_STATS_SPANS_QUERY_IGNORE);</span>
<span class="fc" id="L807">          sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L808">              + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + subKey + &quot;.&quot;</span>
<span class="fc" id="L809">              + SUBNAME_MTAS_STATS_SPANS_QUERY_MAXIMUM_IGNORE_LENGTH);</span>
<span class="fc" id="L810">          sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L811">              + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + subKey + &quot;.&quot;</span>
<span class="fc" id="L812">              + SUBNAME_MTAS_STATS_SPANS_QUERY_PREFIX);</span>
<span class="fc" id="L813">          sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L814">              + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + subKey + &quot;.&quot;</span>
<span class="fc" id="L815">              + SUBNAME_MTAS_STATS_SPANS_QUERY_TYPE);</span>
<span class="fc" id="L816">          sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L817">              + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + subKey + &quot;.&quot;</span>
<span class="fc" id="L818">              + SUBNAME_MTAS_STATS_SPANS_QUERY_VALUE);</span>
<span class="fc" id="L819">          Set&lt;String&gt; subSubKeys = MtasSolrResultUtil.getIdsFromParameters(</span>
<span class="fc" id="L820">              rb.req.getParams(),</span>
<span class="fc" id="L821">              PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L822">                  + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + subKey + &quot;.&quot;</span>
<span class="fc" id="L823">                  + SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE);</span>
<span class="fc bfc" id="L824" title="All 2 branches covered.">          for (String subSubKey : subSubKeys) {</span>
<span class="fc" id="L825">            sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L826">                + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + subKey + &quot;.&quot;</span>
<span class="fc" id="L827">                + SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE + &quot;.&quot; + subSubKey</span>
<span class="fc" id="L828">                + &quot;.&quot; + SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE_NAME);</span>
<span class="fc" id="L829">            sreq.params.remove(PARAM_MTAS_STATS_SPANS + &quot;.&quot; + key + &quot;.&quot;</span>
<span class="fc" id="L830">                + NAME_MTAS_STATS_SPANS_QUERY + &quot;.&quot; + subKey + &quot;.&quot;</span>
<span class="fc" id="L831">                + SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE + &quot;.&quot; + subSubKey</span>
<span class="fc" id="L832">                + &quot;.&quot; + SUBNAME_MTAS_STATS_SPANS_QUERY_VARIABLE_VALUE);</span>
          }
        }
      }
    }
<span class="fc" id="L837">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see
   * mtas.solr.handler.component.util.MtasSolrComponent#create(mtas.codec.util.
   * CodecComponent.BasicComponent, java.lang.Boolean)
   */
  @Override
  public SimpleOrderedMap&lt;Object&gt; create(ComponentStats response,
      Boolean encode) throws IOException {
<span class="fc bfc" id="L849" title="All 2 branches covered.">    if (response instanceof ComponentPosition) {</span>
<span class="fc" id="L850">      return createPosition((ComponentPosition) response, encode);</span>
<span class="fc bfc" id="L851" title="All 2 branches covered.">    } else if (response instanceof ComponentToken) {</span>
<span class="fc" id="L852">      return createToken((ComponentToken) response, encode);</span>
<span class="pc bpc" id="L853" title="1 of 2 branches missed.">    } else if (response instanceof ComponentSpan) {</span>
<span class="fc" id="L854">      return createSpan((ComponentSpan) response, encode);</span>
    } else {
<span class="nc" id="L856">      throw new IOException(&quot;incorrect type &quot; + response.getClass());</span>
    }
  }

  /**
   * Creates the position.
   *
   * @param position the position
   * @param encode the encode
   * @return the simple ordered map
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private SimpleOrderedMap&lt;Object&gt; createPosition(ComponentPosition position,
      Boolean encode) throws IOException {
    // System.out.println(&quot;Create stats position &quot; + position.dataType + &quot; &quot;
    // + position.statsType + &quot; &quot; + position.statsItems + &quot; --- &quot; + encode);
<span class="fc" id="L872">    SimpleOrderedMap&lt;Object&gt; mtasPositionResponse = new SimpleOrderedMap&lt;&gt;();</span>
<span class="fc" id="L873">    mtasPositionResponse.add(&quot;key&quot;, position.key);</span>
<span class="fc" id="L874">    MtasSolrMtasResult data = new MtasSolrMtasResult(position.dataCollector,</span>
<span class="fc" id="L875">        position.dataType, position.statsType, position.statsItems, null);</span>
<span class="fc bfc" id="L876" title="All 2 branches covered.">    if (encode) {</span>
<span class="fc" id="L877">      mtasPositionResponse.add(&quot;_encoded_data&quot;,</span>
<span class="fc" id="L878">          MtasSolrResultUtil.encode(data));</span>
<span class="fc" id="L879">    } else {</span>
<span class="fc" id="L880">      mtasPositionResponse.add(position.dataCollector.getCollectorType(), data);</span>
<span class="fc" id="L881">      MtasSolrResultUtil.rewrite(mtasPositionResponse, searchComponent);</span>
    }
<span class="fc" id="L883">    return mtasPositionResponse;</span>
  }

  /**
   * Creates the token.
   *
   * @param token the token
   * @param encode the encode
   * @return the simple ordered map
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private SimpleOrderedMap&lt;Object&gt; createToken(ComponentToken token,
      Boolean encode) throws IOException {
    // System.out.println(&quot;Create stats position &quot; + position.dataType + &quot; &quot;
    // + position.statsType + &quot; &quot; + position.statsItems + &quot; --- &quot; + encode);
<span class="fc" id="L898">    SimpleOrderedMap&lt;Object&gt; mtasTokenResponse = new SimpleOrderedMap&lt;&gt;();</span>
<span class="fc" id="L899">    mtasTokenResponse.add(&quot;key&quot;, token.key);</span>
<span class="fc" id="L900">    MtasSolrMtasResult data = new MtasSolrMtasResult(token.dataCollector,</span>
<span class="fc" id="L901">        token.dataType, token.statsType, token.statsItems, null);</span>
<span class="fc bfc" id="L902" title="All 2 branches covered.">    if (encode) {</span>
<span class="fc" id="L903">      mtasTokenResponse.add(&quot;_encoded_data&quot;, MtasSolrResultUtil.encode(data));</span>
<span class="fc" id="L904">    } else {</span>
<span class="fc" id="L905">      mtasTokenResponse.add(token.dataCollector.getCollectorType(), data);</span>
<span class="fc" id="L906">      MtasSolrResultUtil.rewrite(mtasTokenResponse, searchComponent);</span>
    }
<span class="fc" id="L908">    return mtasTokenResponse;</span>
  }

  /**
   * Creates the span.
   *
   * @param span the span
   * @param encode the encode
   * @return the simple ordered map
   * @throws IOException Signals that an I/O exception has occurred.
   */
  @SuppressWarnings(&quot;unchecked&quot;)
  private SimpleOrderedMap&lt;Object&gt; createSpan(ComponentSpan span,
      Boolean encode) throws IOException {
    // System.out.println(&quot;Create stats span &quot; + span.dataType + &quot; &quot;
    // + span.statsType + &quot; &quot; + span.statsItems + &quot; --- &quot; + encode);
<span class="fc" id="L924">    SimpleOrderedMap&lt;Object&gt; mtasSpanResponse = new SimpleOrderedMap&lt;&gt;();</span>
<span class="fc" id="L925">    mtasSpanResponse.add(&quot;key&quot;, span.key);</span>
<span class="fc" id="L926">    HashMap&lt;MtasDataCollector&lt;?, ?&gt;, HashMap&lt;String, MtasSolrMtasResult&gt;&gt; functionData = new HashMap&lt;&gt;();</span>
<span class="fc" id="L927">    HashMap&lt;String, MtasSolrMtasResult&gt; functionDataItem = new HashMap&lt;&gt;();</span>
<span class="fc" id="L928">    functionData.put(span.dataCollector, functionDataItem);</span>
<span class="pc bpc" id="L929" title="1 of 2 branches missed.">    if (span.functions != null) {</span>
<span class="pc bpc" id="L930" title="1 of 2 branches missed.">      for (SubComponentFunction function : span.functions) {</span>
<span class="nc" id="L931">        function.dataCollector.close();</span>
<span class="nc" id="L932">        functionDataItem.put(function.key,</span>
<span class="nc" id="L933">            new MtasSolrMtasResult(function.dataCollector,</span>
<span class="nc" id="L934">                new String[] { function.dataType },</span>
<span class="nc" id="L935">                new String[] { function.statsType },</span>
<span class="nc" id="L936">                new SortedSet[] { function.statsItems }, new String[] { null },</span>
<span class="nc" id="L937">                new String[] { null }, new Integer[] { 0 },</span>
<span class="nc" id="L938">                new Integer[] { Integer.MAX_VALUE }, null));</span>
      }
    }
<span class="fc" id="L941">    MtasSolrMtasResult data = new MtasSolrMtasResult(span.dataCollector,</span>
<span class="fc" id="L942">        span.dataType, span.statsType, span.statsItems, functionData);</span>
<span class="fc bfc" id="L943" title="All 2 branches covered.">    if (encode) {</span>
<span class="fc" id="L944">      mtasSpanResponse.add(&quot;_encoded_data&quot;, MtasSolrResultUtil.encode(data));</span>
<span class="fc" id="L945">    } else {</span>
<span class="fc" id="L946">      mtasSpanResponse.add(span.dataCollector.getCollectorType(), data);</span>
<span class="fc" id="L947">      MtasSolrResultUtil.rewrite(mtasSpanResponse, searchComponent);</span>
    }
<span class="fc" id="L949">    return mtasSpanResponse;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * mtas.solr.handler.component.util.MtasSolrComponent#finishStage(org.apache.
   * solr.handler.component.ResponseBuilder)
   */
  @SuppressWarnings(&quot;unchecked&quot;)
  public void finishStage(ResponseBuilder rb) {
<span class="pc bpc" id="L961" title="1 of 2 branches missed.">    if (rb.req.getParams().getBool(MtasSolrSearchComponent.PARAM_MTAS, false)</span>
<span class="fc bfc" id="L962" title="All 2 branches covered.">        &amp;&amp; rb.stage &gt;= ResponseBuilder.STAGE_EXECUTE_QUERY</span>
<span class="fc bfc" id="L963" title="All 2 branches covered.">        &amp;&amp; rb.stage &lt; ResponseBuilder.STAGE_GET_FIELDS) {</span>
<span class="fc bfc" id="L964" title="All 2 branches covered.">      for (ShardRequest sreq : rb.finished) {</span>
<span class="pc bpc" id="L965" title="1 of 2 branches missed.">        if (sreq.params.getBool(MtasSolrSearchComponent.PARAM_MTAS, false)</span>
<span class="pc bpc" id="L966" title="1 of 2 branches missed.">            &amp;&amp; sreq.params.getBool(PARAM_MTAS_STATS, false)) {</span>
<span class="fc bfc" id="L967" title="All 2 branches covered.">          for (ShardResponse shardResponse : sreq.responses) {</span>
<span class="fc" id="L968">            NamedList&lt;Object&gt; response = shardResponse.getSolrResponse()</span>
<span class="fc" id="L969">                .getResponse();</span>
            try {
<span class="pc" id="L971">              ArrayList&lt;NamedList&lt;Object&gt;&gt; data = (ArrayList&lt;NamedList&lt;Object&gt;&gt;) response</span>
<span class="nc" id="L972">                  .findRecursive(&quot;mtas&quot;, &quot;stats&quot;);</span>
<span class="nc bnc" id="L973" title="All 2 branches missed.">              if (data != null) {</span>
<span class="nc" id="L974">                MtasSolrResultUtil.decode(data);</span>
              }
<span class="pc" id="L976">            } catch (ClassCastException e) {</span>
<span class="fc" id="L977">              log.debug(e);</span>
              // shouldnt happen
            }
          }
        }
      }
    }
<span class="fc" id="L984">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see
   * mtas.solr.handler.component.util.MtasSolrComponent#distributedProcess(org.
   * apache.solr.handler.component.ResponseBuilder,
   * mtas.codec.util.CodecComponent.ComponentFields)
   */
  @SuppressWarnings(&quot;unchecked&quot;)
  public void distributedProcess(ResponseBuilder rb, ComponentFields mtasFields)
      throws IOException {
    // rewrite
<span class="fc" id="L998">    NamedList&lt;Object&gt; mtasResponse = null;</span>
    try {
<span class="fc" id="L1000">      mtasResponse = (NamedList&lt;Object&gt;) rb.rsp.getValues().get(&quot;mtas&quot;);</span>
<span class="pc" id="L1001">    } catch (ClassCastException e) {</span>
<span class="nc" id="L1002">      log.debug(e);</span>
<span class="nc" id="L1003">      mtasResponse = null;</span>
    }
<span class="pc bpc" id="L1005" title="1 of 2 branches missed.">    if (mtasResponse != null) {</span>
      NamedList&lt;Object&gt; mtasResponseStats;
      try {
<span class="fc" id="L1008">        mtasResponseStats = (NamedList&lt;Object&gt;) mtasResponse.get(&quot;stats&quot;);</span>
<span class="pc bpc" id="L1009" title="1 of 2 branches missed.">        if (mtasResponseStats != null) {</span>
<span class="fc" id="L1010">          MtasSolrResultUtil.rewrite(mtasResponseStats, searchComponent);</span>
        }
<span class="pc" id="L1012">      } catch (ClassCastException e) {</span>
<span class="nc" id="L1013">        log.debug(e);</span>
<span class="nc" id="L1014">        mtasResponse.remove(&quot;stats&quot;);</span>
      }
    }
<span class="fc" id="L1017">  }</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>