MtasUpdateRequestProcessorFactory.java.html 32.7 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
<?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>MtasUpdateRequestProcessorFactory.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.update.processor</a> &gt; <span class="el_source">MtasUpdateRequestProcessorFactory.java</span></div><h1>MtasUpdateRequestProcessorFactory.java</h1><pre class="source lang-java linenums">package mtas.solr.update.processor;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.FlagsAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.util.CharFilterFactory;
import org.apache.lucene.util.BytesRef;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrInputField;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.core.SolrResourceLoader;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.update.AddUpdateCommand;
import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.apache.solr.update.processor.UpdateRequestProcessorFactory;

import mtas.analysis.MtasTokenizer;
import mtas.analysis.util.MtasCharFilterFactory;
import mtas.analysis.util.MtasTokenizerFactory;
import mtas.codec.util.CodecUtil;
import mtas.solr.schema.MtasPreAnalyzedField;

/**
 * A factory for creating MtasUpdateRequestProcessor objects.
 */
<span class="fc" id="L47">public class MtasUpdateRequestProcessorFactory</span>
    extends UpdateRequestProcessorFactory {

  /** The Constant log. */
<span class="fc" id="L51">  private static final Log log = LogFactory</span>
<span class="fc" id="L52">      .getLog(MtasUpdateRequestProcessorFactory.class);</span>

  /** The config. */
<span class="fc" id="L55">  private MtasUpdateRequestProcessorConfig config = null;</span>

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.solr.update.processor.UpdateRequestProcessorFactory#init(org.
   * apache.solr.common.util.NamedList)
   */
  @Override
  @SuppressWarnings(&quot;rawtypes&quot;)
  public void init(NamedList args) {
<span class="fc" id="L67">    super.init(args);</span>
<span class="fc" id="L68">  }</span>

  /**
   * Inits the.
   *
   * @param req the req
   * @throws IOException Signals that an I/O exception has occurred.
   */
  @SuppressWarnings(&quot;unchecked&quot;)
  private void init(SolrQueryRequest req) throws IOException {
<span class="fc bfc" id="L78" title="All 2 branches covered.">    if (config == null) {</span>
      // initialise
<span class="fc" id="L80">      config = new MtasUpdateRequestProcessorConfig();</span>
      // required info
<span class="fc" id="L82">      Map&lt;String, FieldType&gt; fieldTypes = req.getSchema().getFieldTypes();</span>
<span class="fc" id="L83">      Map&lt;String, SchemaField&gt; fields = req.getSchema().getFields();</span>
<span class="fc" id="L84">      SolrResourceLoader resourceLoader = req.getCore().getSolrConfig()</span>
<span class="fc" id="L85">          .getResourceLoader();</span>
      // check fieldTypes
      // for (String name : fieldTypes.keySet()) {
<span class="fc bfc" id="L88" title="All 2 branches covered.">      for (Entry&lt;String, FieldType&gt; entry : fieldTypes.entrySet()) {</span>
        // only for MtasPreAnalyzedField
<span class="fc bfc" id="L90" title="All 2 branches covered.">        if (entry.getValue() instanceof MtasPreAnalyzedField) {</span>
<span class="fc" id="L91">          MtasPreAnalyzedField mpaf = (MtasPreAnalyzedField) entry.getValue();</span>
<span class="fc" id="L92">          config.fieldTypeDefaultConfiguration.put(entry.getKey(),</span>
              mpaf.defaultConfiguration);
<span class="fc" id="L94">          config.fieldTypeConfigurationFromField.put(entry.getKey(),</span>
              mpaf.configurationFromField);
<span class="fc" id="L96">          config.fieldTypeNumberOfTokensField.put(entry.getKey(),</span>
              mpaf.setNumberOfTokens);
<span class="fc" id="L98">          config.fieldTypeNumberOfPositionsField.put(entry.getKey(),</span>
              mpaf.setNumberOfPositions);
<span class="fc" id="L100">          config.fieldTypeSizeField.put(entry.getKey(), mpaf.setSize);</span>
<span class="fc" id="L101">          config.fieldTypeErrorField.put(entry.getKey(), mpaf.setError);</span>
<span class="fc" id="L102">          config.fieldTypePrefixField.put(entry.getKey(), mpaf.setPrefix);</span>
<span class="pc bpc" id="L103" title="1 of 2 branches missed.">          if (mpaf.followIndexAnalyzer == null</span>
<span class="pc bpc" id="L104" title="1 of 2 branches missed.">              || !fieldTypes.containsKey(mpaf.followIndexAnalyzer)) {</span>
<span class="nc" id="L105">            throw new IOException(</span>
<span class="nc" id="L106">                entry.getKey() + &quot; can't follow &quot; + mpaf.followIndexAnalyzer);</span>
          } else {
<span class="fc" id="L108">            FieldType fieldType = fieldTypes.get(mpaf.followIndexAnalyzer);</span>
<span class="fc" id="L109">            SimpleOrderedMap&lt;?&gt; analyzer = null;</span>
<span class="fc" id="L110">            Object tmpObj1 = fieldType.getNamedPropertyValues(false)</span>
<span class="fc" id="L111">                .get(FieldType.INDEX_ANALYZER);</span>
<span class="pc bpc" id="L112" title="3 of 4 branches missed.">            if (tmpObj1 != null &amp;&amp; tmpObj1 instanceof SimpleOrderedMap) {</span>
<span class="nc" id="L113">              analyzer = (SimpleOrderedMap&lt;?&gt;) tmpObj1;</span>
            }
<span class="pc bpc" id="L115" title="1 of 2 branches missed.">            if (analyzer == null) {</span>
<span class="fc" id="L116">              Object tmpObj2 = fieldType.getNamedPropertyValues(false)</span>
<span class="fc" id="L117">                  .get(FieldType.ANALYZER);</span>
<span class="pc bpc" id="L118" title="2 of 4 branches missed.">              if (tmpObj2 != null &amp;&amp; tmpObj2 instanceof SimpleOrderedMap) {</span>
<span class="fc" id="L119">                analyzer = (SimpleOrderedMap&lt;?&gt;) tmpObj2;</span>
              }
            }
<span class="pc bpc" id="L122" title="1 of 2 branches missed.">            if (analyzer == null) {</span>
<span class="nc" id="L123">              throw new IOException(&quot;no analyzer&quot;);</span>
            } else {
              // charfilters
<span class="fc" id="L126">              ArrayList&lt;SimpleOrderedMap&lt;Object&gt;&gt; listCharFilters = null;</span>
<span class="fc" id="L127">              SimpleOrderedMap&lt;Object&gt; configTokenizer = null;</span>
              try {
<span class="fc" id="L129">                listCharFilters = (ArrayList&lt;SimpleOrderedMap&lt;Object&gt;&gt;) analyzer</span>
<span class="fc" id="L130">                    .findRecursive(FieldType.CHAR_FILTERS);</span>
                ;
<span class="fc" id="L132">                configTokenizer = (SimpleOrderedMap&lt;Object&gt;) analyzer</span>
<span class="fc" id="L133">                    .findRecursive(FieldType.TOKENIZER);</span>
<span class="nc" id="L134">              } catch (ClassCastException e) {</span>
<span class="nc" id="L135">                throw new IOException(</span>
                    &quot;could not cast charFilters and/or tokenizer from analyzer&quot;,
                    e);
<span class="fc" id="L138">              }</span>
<span class="pc bpc" id="L139" title="2 of 4 branches missed.">              if (listCharFilters != null &amp;&amp; !listCharFilters.isEmpty()) {</span>
<span class="fc" id="L140">                CharFilterFactory[] charFilterFactories = new CharFilterFactory[listCharFilters</span>
<span class="fc" id="L141">                    .size()];</span>
<span class="fc" id="L142">                int number = 0;</span>
<span class="fc bfc" id="L143" title="All 2 branches covered.">                for (SimpleOrderedMap&lt;Object&gt; configCharFilter : listCharFilters) {</span>
<span class="fc" id="L144">                  String className = null;</span>
<span class="fc" id="L145">                  Map&lt;String, String&gt; args = new HashMap&lt;&gt;();</span>
<span class="fc" id="L146">                  Iterator&lt;Map.Entry&lt;String, Object&gt;&gt; it = configCharFilter</span>
<span class="fc" id="L147">                      .iterator();</span>
                  // get className and args
<span class="fc bfc" id="L149" title="All 2 branches covered.">                  while (it.hasNext()) {</span>
<span class="fc" id="L150">                    Map.Entry&lt;String, Object&gt; obj = it.next();</span>
<span class="pc bpc" id="L151" title="1 of 2 branches missed.">                    if (obj.getValue() instanceof String) {</span>
<span class="fc bfc" id="L152" title="All 2 branches covered.">                      if (obj.getKey().equals(FieldType.CLASS_NAME)) {</span>
<span class="fc" id="L153">                        className = (String) obj.getValue();</span>
                      } else {
<span class="fc" id="L155">                        args.put(obj.getKey(), (String) obj.getValue());</span>
                      }
                    }
<span class="fc" id="L158">                  }</span>
<span class="pc bpc" id="L159" title="1 of 2 branches missed.">                  if (className != null) {</span>
                    try {
<span class="fc" id="L161">                      Class&lt;?&gt; cls = Class.forName((String) className);</span>
<span class="pc bpc" id="L162" title="1 of 2 branches missed.">                      if (cls.isAssignableFrom(MtasCharFilterFactory.class)) {</span>
<span class="fc" id="L163">                        Class&lt;?&gt;[] types = { Map.class,</span>
                            SolrResourceLoader.class };
<span class="fc" id="L165">                        Constructor&lt;?&gt; cnstr = cls.getConstructor(types);</span>
<span class="fc" id="L166">                        Object cff = cnstr.newInstance(args, resourceLoader);</span>
<span class="pc bpc" id="L167" title="1 of 2 branches missed.">                        if (cff instanceof MtasCharFilterFactory) {</span>
<span class="fc" id="L168">                          charFilterFactories[number] = (MtasCharFilterFactory) cff;</span>
<span class="fc" id="L169">                          number++;</span>
                        } else {
<span class="nc" id="L171">                          throw new IOException(</span>
                              className + &quot; is no MtasCharFilterFactory&quot;);
                        }
<span class="fc" id="L174">                      } else {</span>
<span class="nc" id="L175">                        Class&lt;?&gt;[] types = { Map.class };</span>
<span class="nc" id="L176">                        Constructor&lt;?&gt; cnstr = cls.getConstructor(types);</span>
<span class="nc" id="L177">                        Object cff = cnstr.newInstance(args);</span>
<span class="nc bnc" id="L178" title="All 2 branches missed.">                        if (cff instanceof CharFilterFactory) {</span>
<span class="nc" id="L179">                          charFilterFactories[number] = (CharFilterFactory) cff;</span>
<span class="nc" id="L180">                          number++;</span>
                        } else {
<span class="nc" id="L182">                          throw new IOException(</span>
                              className + &quot; is no CharFilterFactory&quot;);
                        }
                      }
<span class="nc" id="L186">                    } catch (ClassNotFoundException | InstantiationException</span>
                        | IllegalAccessException | IllegalArgumentException
                        | InvocationTargetException | NoSuchMethodException e) {
<span class="nc" id="L189">                      throw new IOException(e);</span>
<span class="fc" id="L190">                    }</span>
                  } else {
<span class="nc" id="L192">                    throw new IOException(&quot;no className&quot;);</span>
                  }
<span class="fc" id="L194">                }</span>
<span class="fc" id="L195">                config.fieldTypeCharFilterFactories.put(entry.getKey(),</span>
                    charFilterFactories);
<span class="fc" id="L197">              } else {</span>
<span class="nc" id="L198">                config.fieldTypeCharFilterFactories.put(entry.getKey(), null);</span>
              }
<span class="pc bpc" id="L200" title="1 of 2 branches missed.">              if (configTokenizer != null) {</span>
<span class="fc" id="L201">                String className = null;</span>
<span class="fc" id="L202">                Map&lt;String, String&gt; args = new HashMap&lt;&gt;();</span>
<span class="fc" id="L203">                Iterator&lt;Map.Entry&lt;String, Object&gt;&gt; it = configTokenizer</span>
<span class="fc" id="L204">                    .iterator();</span>
                // get className and args
<span class="fc bfc" id="L206" title="All 2 branches covered.">                while (it.hasNext()) {</span>
<span class="fc" id="L207">                  Map.Entry&lt;String, Object&gt; obj = it.next();</span>
<span class="pc bpc" id="L208" title="1 of 2 branches missed.">                  if (obj.getValue() instanceof String) {</span>
<span class="fc bfc" id="L209" title="All 2 branches covered.">                    if (obj.getKey().equals(FieldType.CLASS_NAME)) {</span>
<span class="fc" id="L210">                      className = (String) obj.getValue();</span>
                    } else {
<span class="fc" id="L212">                      args.put(obj.getKey(), (String) obj.getValue());</span>
                    }
                  }
<span class="fc" id="L215">                }</span>
<span class="pc bpc" id="L216" title="1 of 2 branches missed.">                if (className != null) {</span>
                  try {
<span class="fc" id="L218">                    Class&lt;?&gt; cls = Class.forName((String) className);</span>
<span class="fc" id="L219">                    Class&lt;?&gt;[] types = { Map.class, SolrResourceLoader.class };</span>
<span class="fc" id="L220">                    Constructor&lt;?&gt; cnstr = cls.getConstructor(types);</span>
<span class="fc" id="L221">                    Object cff = cnstr.newInstance(args, resourceLoader);</span>
<span class="pc bpc" id="L222" title="1 of 2 branches missed.">                    if (cff instanceof MtasTokenizerFactory) {</span>
<span class="fc" id="L223">                      config.fieldTypeTokenizerFactory.put(entry.getKey(),</span>
                          (MtasTokenizerFactory) cff);
                    } else {
<span class="nc" id="L226">                      throw new IOException(</span>
                          className + &quot; is no MtasTokenizerFactory&quot;);
                    }
<span class="nc" id="L229">                  } catch (ClassNotFoundException | InstantiationException</span>
                      | IllegalAccessException | IllegalArgumentException
                      | InvocationTargetException | NoSuchMethodException e) {
<span class="nc" id="L232">                    throw new IOException(e);</span>
<span class="fc" id="L233">                  }</span>
                } else {
<span class="nc" id="L235">                  throw new IOException(&quot;no className&quot;);</span>
                }
              }

            }
          }
        }
<span class="fc" id="L242">      }</span>
<span class="fc bfc" id="L243" title="All 2 branches covered.">      for (Entry&lt;String, SchemaField&gt; entry : fields.entrySet()) {</span>
<span class="pc bpc" id="L244" title="1 of 2 branches missed.">        if (entry.getValue().getType() != null</span>
            &amp;&amp; config.fieldTypeTokenizerFactory
<span class="fc bfc" id="L246" title="All 2 branches covered.">                .containsKey(entry.getValue().getType().getTypeName())) {</span>
<span class="fc" id="L247">          config.fieldMapping.put(entry.getKey(),</span>
<span class="fc" id="L248">              entry.getValue().getType().getTypeName());</span>
        }
<span class="fc" id="L250">      }</span>
    }
<span class="fc" id="L252">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.solr.update.processor.UpdateRequestProcessorFactory#getInstance(
   * org.apache.solr.request.SolrQueryRequest,
   * org.apache.solr.response.SolrQueryResponse,
   * org.apache.solr.update.processor.UpdateRequestProcessor)
   */
  @Override
  public UpdateRequestProcessor getInstance(SolrQueryRequest req,
      SolrQueryResponse rsp, UpdateRequestProcessor next) {
    try {
<span class="fc" id="L267">      init(req);</span>
<span class="nc" id="L268">    } catch (IOException e) {</span>
<span class="nc" id="L269">      log.error(e);</span>
<span class="fc" id="L270">    }</span>
<span class="fc" id="L271">    return new MtasUpdateRequestProcessor(next, config);</span>
  }

}

class MtasUpdateRequestProcessor extends UpdateRequestProcessor {

  /** The log. */
<span class="fc" id="L279">  private static Log log = LogFactory.getLog(MtasUpdateRequestProcessor.class);</span>

  private MtasUpdateRequestProcessorConfig config;

  public MtasUpdateRequestProcessor(UpdateRequestProcessor next,
      MtasUpdateRequestProcessorConfig config) {
<span class="fc" id="L285">    super(next);</span>
<span class="fc" id="L286">    this.config = config;</span>
<span class="fc" id="L287">  }</span>

  @Override
  public void processAdd(AddUpdateCommand cmd) throws IOException {
<span class="pc bpc" id="L291" title="2 of 4 branches missed.">    if (config != null &amp;&amp; config.fieldMapping.size() &gt; 0) {</span>
      // get document
<span class="fc" id="L293">      SolrInputDocument doc = cmd.getSolrInputDocument();</span>
      // loop over configurations
<span class="fc bfc" id="L295" title="All 2 branches covered.">      for (String field : config.fieldMapping.keySet()) {</span>
<span class="fc" id="L296">        SolrInputField originalValue = doc.get(field);</span>
<span class="fc" id="L297">        String fieldType = config.fieldMapping.get(field);</span>
<span class="fc" id="L298">        CharFilterFactory[] charFilterFactories = config.fieldTypeCharFilterFactories</span>
<span class="fc" id="L299">            .get(fieldType);</span>
<span class="fc" id="L300">        MtasTokenizerFactory tokenizerFactory = config.fieldTypeTokenizerFactory</span>
<span class="fc" id="L301">            .get(config.fieldMapping.get(field));</span>
        MtasUpdateRequestProcessorSizeReader sizeReader;
<span class="fc bfc" id="L303" title="All 2 branches covered.">        if (originalValue != null</span>
<span class="pc bpc" id="L304" title="1 of 2 branches missed.">            &amp;&amp; originalValue.getValue() instanceof String) {</span>
<span class="fc" id="L305">          MtasUpdateRequestProcessorResultWriter result = null;</span>
          try {
<span class="fc" id="L307">            String storedValue = (String) originalValue.getValue();</span>
            // create reader
<span class="fc" id="L309">            Reader reader = new StringReader(storedValue);</span>
            // configuration
<span class="fc" id="L311">            String configuration = config.fieldTypeDefaultConfiguration</span>
<span class="fc" id="L312">                .get(fieldType);</span>
<span class="pc bpc" id="L313" title="1 of 2 branches missed.">            if (config.fieldTypeConfigurationFromField.get(fieldType) != null) {</span>
<span class="fc" id="L314">              Object obj = doc.getFieldValue(</span>
<span class="fc" id="L315">                  config.fieldTypeConfigurationFromField.get(fieldType));</span>
<span class="pc bpc" id="L316" title="1 of 2 branches missed.">              if (obj != null) {</span>
<span class="fc" id="L317">                configuration = obj.toString();</span>
              }
            }
            // charFilterFactories
<span class="pc bpc" id="L321" title="1 of 2 branches missed.">            if (charFilterFactories != null) {</span>
<span class="fc bfc" id="L322" title="All 2 branches covered.">              for (CharFilterFactory charFilterFactory : charFilterFactories) {</span>
<span class="pc bpc" id="L323" title="1 of 2 branches missed.">                if (charFilterFactory instanceof MtasCharFilterFactory) {</span>
<span class="fc" id="L324">                  reader = ((MtasCharFilterFactory) charFilterFactory)</span>
<span class="fc" id="L325">                      .create(reader, configuration);</span>
                } else {
<span class="nc" id="L327">                  reader = charFilterFactory.create(reader);</span>
                }
<span class="pc bpc" id="L329" title="1 of 2 branches missed.">                if (reader == null) {</span>
<span class="nc" id="L330">                  throw new IOException(</span>
<span class="nc" id="L331">                      &quot;charFilter &quot; + charFilterFactory.getClass().getName()</span>
                          + &quot; returns null&quot;);
                }
              }
            }

<span class="fc" id="L337">            sizeReader = new MtasUpdateRequestProcessorSizeReader(reader);</span>

            // tokenizerFactory
<span class="fc" id="L340">            result = new MtasUpdateRequestProcessorResultWriter(storedValue);</span>
<span class="fc" id="L341">            int numberOfPositions = 0;</span>
<span class="fc" id="L342">            int numberOfTokens = 0;</span>
<span class="fc" id="L343">            Set&lt;String&gt; prefixes = new HashSet&lt;&gt;();</span>
            try {
<span class="fc" id="L345">              MtasTokenizer tokenizer = tokenizerFactory.create(configuration);</span>
<span class="fc" id="L346">              tokenizer.setReader(sizeReader);</span>
<span class="fc" id="L347">              tokenizer.reset();</span>
              // attributes
<span class="fc" id="L349">              CharTermAttribute termAttribute = tokenizer</span>
<span class="fc" id="L350">                  .getAttribute(CharTermAttribute.class);</span>
<span class="fc" id="L351">              OffsetAttribute offsetAttribute = tokenizer</span>
<span class="fc" id="L352">                  .getAttribute(OffsetAttribute.class);</span>
<span class="fc" id="L353">              PositionIncrementAttribute positionIncrementAttribute = tokenizer</span>
<span class="fc" id="L354">                  .getAttribute(PositionIncrementAttribute.class);</span>
<span class="fc" id="L355">              PayloadAttribute payloadAttribute = tokenizer</span>
<span class="fc" id="L356">                  .getAttribute(PayloadAttribute.class);</span>
<span class="fc" id="L357">              FlagsAttribute flagsAttribute = tokenizer</span>
<span class="fc" id="L358">                  .getAttribute(FlagsAttribute.class);</span>

<span class="fc bfc" id="L360" title="All 2 branches covered.">              while (tokenizer.incrementToken()) {</span>
<span class="fc" id="L361">                String term = null;</span>
<span class="fc" id="L362">                Integer offsetStart = null;</span>
<span class="fc" id="L363">                Integer offsetEnd = null;</span>
<span class="fc" id="L364">                Integer posIncr = null;</span>
<span class="fc" id="L365">                Integer flags = null;</span>
<span class="fc" id="L366">                BytesRef payload = null;</span>
<span class="pc bpc" id="L367" title="1 of 2 branches missed.">                if (termAttribute != null) {</span>
<span class="fc" id="L368">                  term = termAttribute.toString();</span>
<span class="fc" id="L369">                  prefixes.add(CodecUtil.termPrefix(term));</span>
                }
<span class="pc bpc" id="L371" title="1 of 2 branches missed.">                if (offsetAttribute != null) {</span>
<span class="fc" id="L372">                  offsetStart = offsetAttribute.startOffset();</span>
<span class="fc" id="L373">                  offsetEnd = offsetAttribute.endOffset();</span>
                }
<span class="pc bpc" id="L375" title="1 of 2 branches missed.">                if (positionIncrementAttribute != null) {</span>
<span class="fc" id="L376">                  posIncr = positionIncrementAttribute.getPositionIncrement();</span>
                } else {
<span class="nc" id="L378">                  posIncr = 0;</span>
                }
<span class="pc bpc" id="L380" title="1 of 2 branches missed.">                if (payloadAttribute != null) {</span>
<span class="fc" id="L381">                  payload = payloadAttribute.getPayload();</span>
                }
<span class="pc bpc" id="L383" title="1 of 2 branches missed.">                if (flagsAttribute != null) {</span>
<span class="nc" id="L384">                  flags = flagsAttribute.getFlags();</span>
                }
<span class="fc" id="L386">                numberOfTokens++;</span>
<span class="fc" id="L387">                numberOfPositions += posIncr;</span>
<span class="fc" id="L388">                result.addItem(term, offsetStart, offsetEnd, posIncr, payload,</span>
                    flags);
                // System.out.print(term+&quot; &quot;);
<span class="fc" id="L391">              }</span>

              // update field
<span class="fc" id="L394">              doc.remove(field);</span>
<span class="pc bpc" id="L395" title="1 of 2 branches missed.">              if (result.getTokenNumber() &gt; 0) {</span>
<span class="fc" id="L396">                doc.addField(field, result.getFileName());</span>
              }
            } finally {
<span class="pc" id="L399">              result.close();</span>
<span class="fc" id="L400">            }</span>
            // update size
<span class="fc" id="L402">            setFields(doc, config.fieldTypeSizeField.get(fieldType),</span>
<span class="fc" id="L403">                sizeReader.getTotalReadSize());</span>
            // update numberOfPositions
<span class="fc" id="L405">            setFields(doc,</span>
<span class="fc" id="L406">                config.fieldTypeNumberOfPositionsField.get(fieldType),</span>
<span class="fc" id="L407">                numberOfPositions);</span>
            // update numberOfTokens
<span class="fc" id="L409">            setFields(doc, config.fieldTypeNumberOfTokensField.get(fieldType),</span>
<span class="fc" id="L410">                numberOfTokens);</span>
            // update prefixes
<span class="fc" id="L412">            setFields(doc, config.fieldTypePrefixField.get(fieldType),</span>
                prefixes);
<span class="nc" id="L414">          } catch (IOException e) {</span>
<span class="nc" id="L415">            log.info(e);</span>
            // update error
<span class="nc" id="L417">            doc.addField(config.fieldTypeErrorField.get(fieldType),</span>
<span class="nc" id="L418">                e.getMessage());</span>
            // update size
<span class="nc" id="L420">            setFields(doc, config.fieldTypeSizeField.get(fieldType), 0);</span>
            // update numberOfPositions
<span class="nc" id="L422">            setFields(doc,</span>
<span class="nc" id="L423">                config.fieldTypeNumberOfPositionsField.get(fieldType), 0);</span>
            // update numberOfTokens
<span class="nc" id="L425">            setFields(doc, config.fieldTypeNumberOfTokensField.get(fieldType),</span>
<span class="nc" id="L426">                0);</span>
            // update prefixes
<span class="nc" id="L428">            removeFields(doc, config.fieldTypePrefixField.get(fieldType));</span>
<span class="nc bnc" id="L429" title="All 2 branches missed.">            if (result != null) {</span>
<span class="nc" id="L430">              result.forceCloseAndDelete();</span>
<span class="nc" id="L431">              doc.remove(field);</span>
            }
<span class="fc" id="L433">          }</span>
        }
<span class="fc" id="L435">      }</span>

    }
    // pass it up the chain
<span class="fc" id="L439">    super.processAdd(cmd);</span>
<span class="fc" id="L440">  }</span>

  private void removeFields(SolrInputDocument doc, String fieldNames) {
<span class="nc bnc" id="L443" title="All 2 branches missed.">    if (fieldNames != null) {</span>
<span class="nc" id="L444">      String[] tmpFields = fieldNames.split(&quot;,&quot;);</span>
<span class="nc bnc" id="L445" title="All 2 branches missed.">      for (int i = 0; i &lt; tmpFields.length; i++) {</span>
<span class="nc" id="L446">        doc.removeField(tmpFields[i]);</span>
      }
    }
<span class="nc" id="L449">  }</span>

  private void setFields(SolrInputDocument doc, String fieldNames,
      Object value) {
<span class="pc bpc" id="L453" title="1 of 2 branches missed.">    if (fieldNames != null) {</span>
<span class="fc" id="L454">      String[] tmpFields = fieldNames.split(&quot;,&quot;);</span>
<span class="fc bfc" id="L455" title="All 2 branches covered.">      for (int i = 0; i &lt; tmpFields.length; i++) {</span>
<span class="pc bpc" id="L456" title="1 of 2 branches missed.">        if (!tmpFields[i].trim().isEmpty()) {</span>
<span class="fc" id="L457">          doc.addField(tmpFields[i].trim(), value);</span>
        }
      }
    }
<span class="fc" id="L461">  }</span>

}

class MtasUpdateRequestProcessorConfig {

  HashMap&lt;String, CharFilterFactory[]&gt; fieldTypeCharFilterFactories;
  HashMap&lt;String, MtasTokenizerFactory&gt; fieldTypeTokenizerFactory;
  HashMap&lt;String, String&gt; fieldMapping;
  HashMap&lt;String, String&gt; fieldTypeDefaultConfiguration;
  HashMap&lt;String, String&gt; fieldTypeConfigurationFromField;
  HashMap&lt;String, String&gt; fieldTypeNumberOfTokensField;
  HashMap&lt;String, String&gt; fieldTypeNumberOfPositionsField;
  HashMap&lt;String, String&gt; fieldTypeSizeField;
  HashMap&lt;String, String&gt; fieldTypeErrorField;
  HashMap&lt;String, String&gt; fieldTypePrefixField;

<span class="fc" id="L478">  MtasUpdateRequestProcessorConfig() {</span>
<span class="fc" id="L479">    fieldMapping = new HashMap&lt;&gt;();</span>
<span class="fc" id="L480">    fieldTypeCharFilterFactories = new HashMap&lt;&gt;();</span>
<span class="fc" id="L481">    fieldTypeTokenizerFactory = new HashMap&lt;&gt;();</span>
<span class="fc" id="L482">    fieldTypeDefaultConfiguration = new HashMap&lt;&gt;();</span>
<span class="fc" id="L483">    fieldTypeConfigurationFromField = new HashMap&lt;&gt;();</span>
<span class="fc" id="L484">    fieldTypeNumberOfTokensField = new HashMap&lt;&gt;();</span>
<span class="fc" id="L485">    fieldTypeNumberOfPositionsField = new HashMap&lt;&gt;();</span>
<span class="fc" id="L486">    fieldTypeSizeField = new HashMap&lt;&gt;();</span>
<span class="fc" id="L487">    fieldTypeErrorField = new HashMap&lt;&gt;();</span>
<span class="fc" id="L488">    fieldTypePrefixField = new HashMap&lt;&gt;();</span>
<span class="fc" id="L489">  }</span>

}

class MtasUpdateRequestProcessorSizeReader extends Reader {

  Reader reader;
  long totalReadSize;

<span class="fc" id="L498">  public MtasUpdateRequestProcessorSizeReader(Reader reader) {</span>
<span class="fc" id="L499">    this.reader = reader;</span>
<span class="fc" id="L500">    totalReadSize = 0;</span>
<span class="fc" id="L501">  }</span>

  public int read(char[] cbuf, int off, int len) throws IOException {
<span class="fc" id="L504">    int read = reader.read(cbuf, off, len);</span>
<span class="fc" id="L505">    totalReadSize += read;</span>
<span class="fc" id="L506">    return read;</span>
  }

  public void close() throws IOException {
<span class="nc" id="L510">    reader.close();</span>
<span class="nc" id="L511">  }</span>

  public long getTotalReadSize() {
<span class="fc" id="L514">    return totalReadSize;</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>