CodecInfo.java.html 37.4 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
<?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>CodecInfo.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.codec.util</a> &gt; <span class="el_source">CodecInfo.java</span></div><h1>CodecInfo.java</h1><pre class="source lang-java linenums">package mtas.codec.util;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import mtas.analysis.token.MtasToken;
import mtas.analysis.token.MtasTokenString;
import mtas.codec.MtasCodecPostingsFormat;
import mtas.codec.tree.IntervalRBTree;
import mtas.codec.tree.IntervalTreeNodeData;
import mtas.codec.util.CodecSearchTree.MtasTreeHit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.index.Terms;
import org.apache.lucene.store.IndexInput;

/**
 * The Class CodecInfo.
 */
public class CodecInfo {

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

  /** The index input list. */
  HashMap&lt;String, IndexInput&gt; indexInputList;

  /** The index input offset list. */
  HashMap&lt;String, Long&gt; indexInputOffsetList;

  /** The version. */
  int version;

  /** The field references. */
  private HashMap&lt;String, FieldReferences&gt; fieldReferences;

  /** The prefix references. */
  private HashMap&lt;String, LinkedHashMap&lt;String, Long&gt;&gt; prefixReferences;

  /**
   * Instantiates a new codec info.
   *
   * @param indexInputList the index input list
   * @param indexInputOffsetList the index input offset list
   * @param version the version
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public CodecInfo(HashMap&lt;String, IndexInput&gt; indexInputList,
      HashMap&lt;String, Long&gt; indexInputOffsetList, int version)
<span class="fc" id="L58">      throws IOException {</span>
<span class="fc" id="L59">    this.indexInputList = indexInputList;</span>
<span class="fc" id="L60">    this.indexInputOffsetList = indexInputOffsetList;</span>
<span class="fc" id="L61">    this.version = version;</span>
<span class="fc" id="L62">    init();</span>
<span class="fc" id="L63">  }</span>

  /**
   * Gets the codec info from terms.
   *
   * @param t the t
   * @return the codec info from terms
   * @throws IOException Signals that an I/O exception has occurred.
   */
  @SuppressWarnings(&quot;unchecked&quot;)
  public static CodecInfo getCodecInfoFromTerms(Terms t) throws IOException {
    try {
<span class="fc" id="L75">      HashMap&lt;String, IndexInput&gt; indexInputList = null;</span>
<span class="fc" id="L76">      HashMap&lt;String, Long&gt; indexInputOffsetList = null;</span>
<span class="fc" id="L77">      Object version = null;</span>
<span class="fc" id="L78">      Method[] methods = t.getClass().getMethods();</span>
<span class="fc" id="L79">      Object[] emptyArgs = null;</span>
<span class="fc bfc" id="L80" title="All 2 branches covered.">      for (Method m : methods) {</span>
<span class="fc bfc" id="L81" title="All 2 branches covered.">        if (m.getName().equals(&quot;getIndexInputList&quot;)) {</span>
<span class="fc" id="L82">          indexInputList = (HashMap&lt;String, IndexInput&gt;) m.invoke(t, emptyArgs);</span>
<span class="fc bfc" id="L83" title="All 2 branches covered.">        } else if (m.getName().equals(&quot;getIndexInputOffsetList&quot;)) {</span>
<span class="fc" id="L84">          indexInputOffsetList = (HashMap&lt;String, Long&gt;) m.invoke(t, emptyArgs);</span>
<span class="fc bfc" id="L85" title="All 2 branches covered.">        } else if (m.getName().equals(&quot;getVersion&quot;)) {</span>
<span class="fc" id="L86">          version = m.invoke(t, emptyArgs);          </span>
        }
      }
<span class="pc bpc" id="L89" title="3 of 6 branches missed.">      if (indexInputList == null || indexInputOffsetList == null</span>
          || version == null) {
<span class="nc" id="L91">        throw new IOException(&quot;Reader doesn't provide MtasFieldsProducer&quot;);</span>
      } else {
<span class="fc" id="L93">        return new CodecInfo(indexInputList, indexInputOffsetList,</span>
<span class="fc" id="L94">            (int) version);</span>
      }
<span class="nc" id="L96">    } catch (IllegalAccessException | InvocationTargetException e) {</span>
<span class="nc" id="L97">      throw new IOException(&quot;Can't get codecInfo&quot;, e);</span>
    }
  }

  /**
   * Inits the.
   *
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void init() throws IOException {
    // move to begin
<span class="fc" id="L108">    IndexInput inField = indexInputList.get(&quot;field&quot;);</span>
<span class="fc" id="L109">    inField.seek(indexInputOffsetList.get(&quot;field&quot;));</span>
    // store field references in memory
<span class="fc" id="L111">    fieldReferences = new HashMap&lt;String, FieldReferences&gt;();</span>
<span class="fc" id="L112">    boolean doInit = true;</span>
<span class="fc bfc" id="L113" title="All 2 branches covered.">    while (doInit) {</span>
      try {
<span class="fc" id="L115">        String field = inField.readString();</span>
<span class="fc" id="L116">        long refIndexDoc = inField.readVLong();</span>
<span class="fc" id="L117">        long refIndexDocId = inField.readVLong();</span>
<span class="fc" id="L118">        int numberOfDocs = inField.readVInt();</span>
<span class="fc" id="L119">        inField.readVLong(); // refTerm</span>
<span class="fc" id="L120">        inField.readVInt(); // numberOfTerms</span>
<span class="fc" id="L121">        long refPrefix = inField.readVLong();</span>
<span class="fc" id="L122">        int numberOfPrefixes = inField.readVInt();</span>
<span class="fc" id="L123">        fieldReferences.put(field, new FieldReferences(refIndexDoc,</span>
            refIndexDocId, numberOfDocs, refPrefix, numberOfPrefixes));
<span class="fc" id="L125">      } catch (IOException e) {</span>
<span class="fc" id="L126">        log.debug(e);</span>
<span class="fc" id="L127">        doInit = false;</span>
<span class="fc" id="L128">      }</span>
    }
    // prefixReferences
<span class="fc" id="L131">    prefixReferences = new HashMap&lt;String, LinkedHashMap&lt;String, Long&gt;&gt;();</span>
<span class="fc" id="L132">  }</span>

  /**
   * Gets the object by id.
   *
   * @param field the field
   * @param docId the doc id
   * @param mtasId the mtas id
   * @return the object by id
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public MtasToken getObjectById(String field, int docId, int mtasId)
      throws IOException {
    try {
      Long ref;
      Long objectRefApproxCorrection;
<span class="nc" id="L148">      IndexDoc doc = getDoc(field, docId);</span>
<span class="nc" id="L149">      IndexInput inObjectId = indexInputList.get(&quot;indexObjectId&quot;);</span>
<span class="nc" id="L150">      IndexInput inObject = indexInputList.get(&quot;object&quot;);</span>
<span class="nc" id="L151">      IndexInput inTerm = indexInputList.get(&quot;term&quot;);</span>
<span class="nc bnc" id="L152" title="All 2 branches missed.">      if (doc.storageFlags == MtasCodecPostingsFormat.MTAS_STORAGE_BYTE) {</span>
<span class="nc" id="L153">        inObjectId.seek(doc.fpIndexObjectId + (mtasId * 1L));</span>
<span class="nc" id="L154">        objectRefApproxCorrection = Long.valueOf(inObjectId.readByte());</span>
<span class="nc bnc" id="L155" title="All 2 branches missed.">      } else if (doc.storageFlags == MtasCodecPostingsFormat.MTAS_STORAGE_SHORT) {</span>
<span class="nc" id="L156">        inObjectId.seek(doc.fpIndexObjectId + (mtasId * 2L));</span>
<span class="nc" id="L157">        objectRefApproxCorrection = Long.valueOf(inObjectId.readShort());</span>
<span class="nc bnc" id="L158" title="All 2 branches missed.">      } else if (doc.storageFlags == MtasCodecPostingsFormat.MTAS_STORAGE_INTEGER) {</span>
<span class="nc" id="L159">        inObjectId.seek(doc.fpIndexObjectId + (mtasId * 4L));</span>
<span class="nc" id="L160">        objectRefApproxCorrection = Long.valueOf(inObjectId.readInt());</span>
      } else {
<span class="nc" id="L162">        inObjectId.seek(doc.fpIndexObjectId + (mtasId * 8L));</span>
<span class="nc" id="L163">        objectRefApproxCorrection = Long.valueOf(inObjectId.readLong());</span>
      }
<span class="nc" id="L165">      ref = objectRefApproxCorrection + doc.objectRefApproxOffset</span>
          + (mtasId * (long) doc.objectRefApproxQuotient);
<span class="nc" id="L167">      return MtasCodecPostingsFormat.getToken(inObject, inTerm, ref);</span>
<span class="nc" id="L168">    } catch (Exception e) {</span>
<span class="nc" id="L169">      throw new IOException(e);</span>
    }
  }

  /**
   * Gets the objects by parent id.
   *
   * @param field the field
   * @param docId the doc id
   * @param position the position
   * @return the objects by parent id
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public List&lt;MtasTokenString&gt; getObjectsByParentId(String field, int docId,
      int position) throws IOException {
<span class="nc" id="L184">    IndexDoc doc = getDoc(field, docId);</span>
<span class="nc" id="L185">    IndexInput inIndexObjectParent = indexInputList.get(&quot;indexObjectParent&quot;);</span>
<span class="nc" id="L186">    ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; hits = CodecSearchTree.searchMtasTree(position,</span>
        inIndexObjectParent, doc.fpIndexObjectParent,
        doc.smallestObjectFilepointer);
<span class="nc" id="L189">    return getObjects(hits);</span>
  }

  /**
   * Gets the objects by position.
   *
   * @param field the field
   * @param docId the doc id
   * @param position the position
   * @return the objects by position
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public ArrayList&lt;MtasTokenString&gt; getObjectsByPosition(String field,
      int docId, int position) throws IOException {
<span class="nc" id="L203">    IndexDoc doc = getDoc(field, docId);</span>
<span class="nc" id="L204">    IndexInput inIndexObjectPosition = indexInputList</span>
<span class="nc" id="L205">        .get(&quot;indexObjectPosition&quot;);</span>
<span class="nc" id="L206">    ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; hits = CodecSearchTree.searchMtasTree(position,</span>
        inIndexObjectPosition, doc.fpIndexObjectPosition,
        doc.smallestObjectFilepointer);
<span class="nc" id="L209">    return getObjects(hits);</span>
  }

  // public ArrayList&lt;MtasTokenString&gt; getObjectsByPositions(String field,
  // int docId, int startPosition, int endPosition) throws IOException {
  // IndexDoc doc = getDoc(field, docId);
  // IndexInput inIndexObjectPosition = indexInputList
  // .get(&quot;indexObjectPosition&quot;);
  // ArrayList&lt;TreeHit&lt;?&gt;&gt; hits = CodecSearchTree.searchTree(startPosition,
  // endPosition, inIndexObjectPosition, doc.fpIndexObjectPosition,
  // doc.smallestObjectFilepointer);
  // return getObjects(hits);
  // }

  /**
   * Gets the prefix filtered objects by positions.
   *
   * @param field the field
   * @param docId the doc id
   * @param prefixes the prefixes
   * @param startPosition the start position
   * @param endPosition the end position
   * @return the prefix filtered objects by positions
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public List&lt;MtasTokenString&gt; getPrefixFilteredObjectsByPositions(String field,
      int docId, List&lt;String&gt; prefixes, int startPosition, int endPosition)
      throws IOException {
<span class="nc" id="L237">    IndexDoc doc = getDoc(field, docId);</span>
<span class="nc" id="L238">    IndexInput inIndexObjectPosition = indexInputList</span>
<span class="nc" id="L239">        .get(&quot;indexObjectPosition&quot;);</span>
<span class="nc bnc" id="L240" title="All 2 branches missed.">    if (doc != null) {</span>
<span class="nc" id="L241">      ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; hits = CodecSearchTree.searchMtasTree(</span>
          startPosition, endPosition, inIndexObjectPosition,
          doc.fpIndexObjectPosition, doc.smallestObjectFilepointer);
<span class="nc" id="L244">      return getPrefixFilteredObjects(hits, prefixes);</span>
    } else {
<span class="nc" id="L246">      return new ArrayList&lt;&gt;();</span>
    }
  }

  /**
   * Gets the prefix filtered objects.
   *
   * @param hits the hits
   * @param prefixes the prefixes
   * @return the prefix filtered objects
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private List&lt;MtasTokenString&gt; getPrefixFilteredObjects(
      List&lt;MtasTreeHit&lt;?&gt;&gt; hits, List&lt;String&gt; prefixes) throws IOException {
<span class="nc" id="L260">    ArrayList&lt;MtasTokenString&gt; tokens = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L261">    IndexInput inObject = indexInputList.get(&quot;object&quot;);</span>
<span class="nc" id="L262">    IndexInput inTerm = indexInputList.get(&quot;term&quot;);</span>
<span class="nc bnc" id="L263" title="All 2 branches missed.">    for (MtasTreeHit&lt;?&gt; hit : hits) {</span>
<span class="nc" id="L264">      MtasTokenString token = MtasCodecPostingsFormat.getToken(inObject, inTerm,</span>
<span class="nc" id="L265">          hit.ref);</span>
<span class="nc bnc" id="L266" title="All 2 branches missed.">      if (token != null) {</span>
<span class="nc bnc" id="L267" title="All 2 branches missed.">        if (!prefixes.isEmpty()) {</span>
<span class="nc bnc" id="L268" title="All 2 branches missed.">          if (prefixes.contains(token.getPrefix())) {</span>
<span class="nc" id="L269">            tokens.add(token);</span>
          }
        } else {
<span class="nc" id="L272">          tokens.add(token);</span>
        }
      }
<span class="nc" id="L275">    }</span>
<span class="nc" id="L276">    return tokens;</span>
  }

  /**
   * Gets the positioned terms by prefixes and position.
   *
   * @param field the field
   * @param docId the doc id
   * @param prefixes the prefixes
   * @param position the position
   * @return the positioned terms by prefixes and position
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public List&lt;MtasTreeHit&lt;String&gt;&gt; getPositionedTermsByPrefixesAndPosition(
      String field, int docId, List&lt;String&gt; prefixes, int position)
      throws IOException {
<span class="nc" id="L292">    return getPositionedTermsByPrefixesAndPositionRange(field, docId, prefixes,</span>
        position, position);
  }

  /**
   * Gets the positioned terms by prefixes and position range.
   *
   * @param field the field
   * @param docId the doc id
   * @param prefixes the prefixes
   * @param startPosition the start position
   * @param endPosition the end position
   * @return the positioned terms by prefixes and position range
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public List&lt;MtasTreeHit&lt;String&gt;&gt; getPositionedTermsByPrefixesAndPositionRange(
      String field, int docId, List&lt;String&gt; prefixes, int startPosition,
      int endPosition) throws IOException {
<span class="nc" id="L310">    IndexDoc doc = getDoc(field, docId);</span>
<span class="nc" id="L311">    IndexInput inIndexObjectPosition = indexInputList</span>
<span class="nc" id="L312">        .get(&quot;indexObjectPosition&quot;);</span>
<span class="nc bnc" id="L313" title="All 2 branches missed.">    if (doc != null) {</span>
<span class="nc" id="L314">      ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; hitItems = CodecSearchTree.searchMtasTree(</span>
          startPosition, endPosition, inIndexObjectPosition,
          doc.fpIndexObjectPosition, doc.smallestObjectFilepointer);
<span class="nc" id="L317">      List&lt;MtasTreeHit&lt;String&gt;&gt; hits = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L318">      Map&lt;String, Integer&gt; prefixIds = getPrefixesIds(field, prefixes);</span>
<span class="nc bnc" id="L319" title="All 4 branches missed.">      if (prefixIds != null &amp;&amp; prefixIds.size() &gt; 0) {</span>
<span class="nc" id="L320">        ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; filteredHitItems = new ArrayList&lt;MtasTreeHit&lt;?&gt;&gt;();</span>

<span class="nc bnc" id="L322" title="All 2 branches missed.">        for (MtasTreeHit&lt;?&gt; hitItem : hitItems) {</span>
<span class="nc bnc" id="L323" title="All 2 branches missed.">          if (prefixIds.containsValue(hitItem.additionalId)) {</span>
<span class="nc" id="L324">            filteredHitItems.add(hitItem);</span>
          }
<span class="nc" id="L326">        }</span>
<span class="nc bnc" id="L327" title="All 2 branches missed.">        if (filteredHitItems.size() &gt; 0) {</span>
<span class="nc" id="L328">          ArrayList&lt;MtasTokenString&gt; objects = getObjects(filteredHitItems);</span>
<span class="nc bnc" id="L329" title="All 2 branches missed.">          for (MtasTokenString token : objects) {</span>
<span class="nc" id="L330">            MtasTreeHit&lt;String&gt; hit = new MtasTreeHit&lt;String&gt;(</span>
<span class="nc" id="L331">                token.getPositionStart(), token.getPositionEnd(),</span>
<span class="nc" id="L332">                token.getTokenRef(), 0, 0, token.getValue());</span>
<span class="nc" id="L333">            hits.add(hit);</span>
<span class="nc" id="L334">          }</span>
        }
      }
<span class="nc" id="L337">      return hits;</span>
    } else {
<span class="nc" id="L339">      return new ArrayList&lt;MtasTreeHit&lt;String&gt;&gt;();</span>
    }
  }

  /**
   * Collect terms by prefixes for list of hit positions.
   *
   * @param field the field
   * @param docId the doc id
   * @param prefixes the prefixes
   * @param positionsHits the positions hits
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public void collectTermsByPrefixesForListOfHitPositions(String field,
      int docId, ArrayList&lt;String&gt; prefixes,
      ArrayList&lt;IntervalTreeNodeData&lt;String&gt;&gt; positionsHits)
      throws IOException {
<span class="fc" id="L356">    IndexDoc doc = getDoc(field, docId);</span>
<span class="fc" id="L357">    IndexInput inIndexObjectPosition = indexInputList</span>
<span class="fc" id="L358">        .get(&quot;indexObjectPosition&quot;);</span>
<span class="fc" id="L359">    IndexInput inTerm = indexInputList.get(&quot;term&quot;);</span>
    // create tree interval hits
<span class="fc" id="L361">    IntervalRBTree&lt;String&gt; positionTree = new IntervalRBTree&lt;String&gt;(</span>
        positionsHits);

    // find prefixIds
<span class="fc" id="L365">    Map&lt;String, Integer&gt; prefixIds = getPrefixesIds(field, prefixes);</span>
    // search matching tokens
<span class="pc bpc" id="L367" title="1 of 2 branches missed.">    if (prefixIds != null) {</span>
<span class="fc" id="L368">      CodecSearchTree.searchMtasTreeWithIntervalTree(prefixIds.values(),</span>
          positionTree, inIndexObjectPosition, doc.fpIndexObjectPosition,
          doc.smallestObjectFilepointer);

      // reverse list
<span class="fc" id="L373">      Map&lt;Integer, String&gt; idPrefixes = new HashMap&lt;&gt;();</span>
<span class="fc bfc" id="L374" title="All 2 branches covered.">      for (Entry&lt;String, Integer&gt; entry : prefixIds.entrySet()) {</span>
<span class="fc" id="L375">        idPrefixes.put(entry.getValue(), entry.getKey());</span>
<span class="fc" id="L376">      }</span>
      // term administration
<span class="fc" id="L378">      Map&lt;Long, String&gt; refTerms = new HashMap&lt;&gt;();</span>

<span class="fc bfc" id="L380" title="All 2 branches covered.">      for (IntervalTreeNodeData&lt;String&gt; positionHit : positionsHits) {</span>
<span class="fc bfc" id="L381" title="All 2 branches covered.">        for (MtasTreeHit&lt;String&gt; hit : positionHit.list) {</span>
<span class="pc bpc" id="L382" title="1 of 2 branches missed.">          if (hit.idData == null) {</span>
<span class="fc" id="L383">            hit.idData = idPrefixes.get(hit.additionalId);</span>
<span class="fc bfc" id="L384" title="All 2 branches covered.">            if (!refTerms.containsKey(hit.additionalRef)) {</span>
<span class="fc" id="L385">              refTerms.put(hit.additionalRef,</span>
<span class="fc" id="L386">                  MtasCodecPostingsFormat.getTerm(inTerm, hit.additionalRef));</span>
            }
<span class="fc" id="L388">            hit.refData = refTerms.get(hit.additionalRef);</span>
          }
<span class="fc" id="L390">        }</span>
<span class="fc" id="L391">      }</span>
    }
<span class="fc" id="L393">  }</span>

  /**
   * Gets the objects.
   *
   * @param hits the hits
   * @return the objects
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public ArrayList&lt;MtasTokenString&gt; getObjects(List&lt;MtasTreeHit&lt;?&gt;&gt; hits)
      throws IOException {
<span class="nc" id="L404">    ArrayList&lt;MtasTokenString&gt; tokens = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L405">    IndexInput inObject = indexInputList.get(&quot;object&quot;);</span>
<span class="nc" id="L406">    IndexInput inTerm = indexInputList.get(&quot;term&quot;);</span>
<span class="nc bnc" id="L407" title="All 2 branches missed.">    for (MtasTreeHit&lt;?&gt; hit : hits) {</span>
<span class="nc" id="L408">      MtasTokenString token = MtasCodecPostingsFormat.getToken(inObject, inTerm,</span>
<span class="nc" id="L409">          hit.ref);</span>
<span class="nc bnc" id="L410" title="All 2 branches missed.">      if (token != null) {</span>
<span class="nc" id="L411">        tokens.add(token);</span>
      }
<span class="nc" id="L413">    }</span>
<span class="nc" id="L414">    return tokens;</span>
  }

  /**
   * Gets the terms.
   *
   * @param refs the refs
   * @return the terms
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public ArrayList&lt;MtasTreeHit&lt;String&gt;&gt; getTerms(ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; refs)
      throws IOException {
    try {
<span class="nc" id="L427">      ArrayList&lt;MtasTreeHit&lt;String&gt;&gt; terms = new ArrayList&lt;MtasTreeHit&lt;String&gt;&gt;();</span>
<span class="nc" id="L428">      IndexInput inTerm = indexInputList.get(&quot;term&quot;);</span>
<span class="nc bnc" id="L429" title="All 2 branches missed.">      for (MtasTreeHit&lt;?&gt; hit : refs) {</span>
<span class="nc" id="L430">        inTerm.seek(hit.ref);</span>
<span class="nc" id="L431">        String term = inTerm.readString();</span>
<span class="nc" id="L432">        MtasTreeHit&lt;String&gt; newHit = new MtasTreeHit&lt;String&gt;(hit.startPosition,</span>
            hit.endPosition, hit.ref, hit.additionalId, hit.additionalRef,
            term);
<span class="nc" id="L435">        terms.add(newHit);</span>
<span class="nc" id="L436">      }</span>
<span class="nc" id="L437">      return terms;</span>
<span class="nc" id="L438">    } catch (Exception e) {</span>
<span class="nc" id="L439">      throw new IOException(e);</span>
    }
  }

  /**
   * Gets the prefixes ids.
   *
   * @param field the field
   * @param prefixes the prefixes
   * @return the prefixes ids
   */
  Map&lt;String, Integer&gt; getPrefixesIds(String field, List&lt;String&gt; prefixes) {
<span class="fc" id="L451">    LinkedHashMap&lt;String, Long&gt; refs = getPrefixes(field);</span>
<span class="pc bpc" id="L452" title="1 of 2 branches missed.">    if (refs != null) {</span>
<span class="fc" id="L453">      List&lt;String&gt; list = new ArrayList&lt;&gt;(refs.keySet());</span>
<span class="fc" id="L454">      Map&lt;String, Integer&gt; result = new HashMap&lt;&gt;();</span>
<span class="fc bfc" id="L455" title="All 2 branches covered.">      for (String prefix : prefixes) {</span>
<span class="fc" id="L456">        int id = list.indexOf(prefix);</span>
<span class="pc bpc" id="L457" title="1 of 2 branches missed.">        if (id &gt;= 0) {</span>
<span class="fc" id="L458">          result.put(prefix, id + 1);</span>
        }
<span class="fc" id="L460">      }</span>
<span class="fc" id="L461">      return result;</span>
    } else {
<span class="nc" id="L463">      return null;</span>
    }
  }

  /**
   * Gets the prefixes.
   *
   * @param field the field
   * @return the prefixes
   */
  private LinkedHashMap&lt;String, Long&gt; getPrefixes(String field) {
<span class="pc bpc" id="L474" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L475">      FieldReferences fr = fieldReferences.get(field);</span>
<span class="fc bfc" id="L476" title="All 2 branches covered.">      if (!prefixReferences.containsKey(field)) {</span>
<span class="fc" id="L477">        LinkedHashMap&lt;String, Long&gt; refs = new LinkedHashMap&lt;String, Long&gt;();</span>
        try {
<span class="fc" id="L479">          IndexInput inPrefix = indexInputList.get(&quot;prefix&quot;);</span>
<span class="fc" id="L480">          inPrefix.seek(fr.refPrefix);</span>
<span class="fc bfc" id="L481" title="All 2 branches covered.">          for (int i = 0; i &lt; fr.numberOfPrefixes; i++) {</span>
<span class="fc" id="L482">            Long ref = inPrefix.getFilePointer();</span>
<span class="fc" id="L483">            String prefix = inPrefix.readString();</span>
<span class="fc" id="L484">            refs.put(prefix, ref);</span>
          }
<span class="nc" id="L486">        } catch (Exception e) {</span>
<span class="nc" id="L487">          log.error(e);</span>
<span class="nc" id="L488">          refs.clear();</span>
<span class="fc" id="L489">        }</span>
<span class="fc" id="L490">        prefixReferences.put(field, refs);</span>
<span class="fc" id="L491">        return refs;</span>
      } else {
<span class="fc" id="L493">        return prefixReferences.get(field);</span>
      }
    } else {
<span class="nc" id="L496">      return null;</span>
    }
  }

  /**
   * Gets the doc.
   *
   * @param field the field
   * @param docId the doc id
   * @return the doc
   */
  public IndexDoc getDoc(String field, int docId) {
<span class="pc bpc" id="L508" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L509">      FieldReferences fr = fieldReferences.get(field);</span>
      try {
<span class="fc" id="L511">        IndexInput inIndexDocId = indexInputList.get(&quot;indexDocId&quot;);</span>
<span class="fc" id="L512">        ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; list = CodecSearchTree.searchMtasTree(docId,</span>
            inIndexDocId, fr.refIndexDocId, fr.refIndexDoc);
<span class="pc bpc" id="L514" title="1 of 2 branches missed.">        if (list.size() == 1) {</span>
<span class="fc" id="L515">          return new IndexDoc(list.get(0).ref);</span>
        }
<span class="nc" id="L517">      } catch (IOException e) {</span>
<span class="nc" id="L518">        log.debug(e);</span>
<span class="nc" id="L519">        return null;</span>
<span class="nc" id="L520">      }</span>
    }
<span class="nc" id="L522">    return null;</span>
  }

  /**
   * Gets the next doc.
   *
   * @param field the field
   * @param previousDocId the previous doc id
   * @return the next doc
   */
  public IndexDoc getNextDoc(String field, int previousDocId) {
<span class="pc bpc" id="L533" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L534">      FieldReferences fr = fieldReferences.get(field);</span>
      try {
<span class="fc bfc" id="L536" title="All 2 branches covered.">        if (previousDocId &lt; 0) {</span>
<span class="fc" id="L537">          return new IndexDoc(fr.refIndexDoc);</span>
        } else {
<span class="fc" id="L539">          int nextDocId = previousDocId + 1;</span>
<span class="fc" id="L540">          IndexInput inIndexDocId = indexInputList.get(&quot;indexDocId&quot;);</span>
<span class="fc" id="L541">          ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; list = CodecSearchTree.advanceMtasTree(</span>
              nextDocId, inIndexDocId, fr.refIndexDocId, fr.refIndexDoc);
<span class="fc bfc" id="L543" title="All 2 branches covered.">          if (list.size() == 1) {</span>
<span class="fc" id="L544">            IndexInput inDoc = indexInputList.get(&quot;doc&quot;);</span>
<span class="fc" id="L545">            inDoc.seek(list.get(0).ref);</span>
<span class="fc" id="L546">            return new IndexDoc(inDoc.getFilePointer());</span>
          }
        }
<span class="nc" id="L549">      } catch (IOException e) {</span>
<span class="nc" id="L550">        log.debug(e);</span>
<span class="nc" id="L551">        return null;</span>
<span class="fc" id="L552">      }</span>
    }
<span class="fc" id="L554">    return null;</span>
  }

  /**
   * Gets the number of docs.
   *
   * @param field the field
   * @return the number of docs
   */
  public int getNumberOfDocs(String field) {
<span class="nc bnc" id="L564" title="All 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="nc" id="L565">      FieldReferences fr = fieldReferences.get(field);</span>
<span class="nc" id="L566">      return fr.numberOfDocs;</span>
    } else {
<span class="nc" id="L568">      return 0;</span>
    }
  }

  /**
   * Gets the number of positions.
   *
   * @param field the field
   * @param docId the doc id
   * @return the number of positions
   */
  public Integer getNumberOfPositions(String field, int docId) {
<span class="pc bpc" id="L580" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L581">      IndexDoc doc = getDoc(field, docId);</span>
<span class="pc bpc" id="L582" title="1 of 2 branches missed.">      if (doc != null) {</span>
<span class="fc" id="L583">        return 1 + doc.maxPosition - doc.minPosition;</span>
      }
    }
<span class="nc" id="L586">    return null;</span>
  }

  /**
   * Gets the all number of positions.
   *
   * @param field the field
   * @param docBase the doc base
   * @return the all number of positions
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public HashMap&lt;Integer, Integer&gt; getAllNumberOfPositions(String field,
      int docBase) throws IOException {
<span class="fc" id="L599">    HashMap&lt;Integer, Integer&gt; numbers = new HashMap&lt;Integer, Integer&gt;();</span>
<span class="pc bpc" id="L600" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L601">      FieldReferences fr = fieldReferences.get(field);</span>
<span class="fc" id="L602">      IndexInput inIndexDoc = indexInputList.get(&quot;doc&quot;);</span>
<span class="fc" id="L603">      inIndexDoc.seek(fr.refIndexDoc);</span>
      IndexDoc doc;
<span class="fc bfc" id="L605" title="All 2 branches covered.">      for (int i = 0; i &lt; fr.numberOfDocs; i++) {</span>
<span class="fc" id="L606">        doc = new IndexDoc(null);</span>
<span class="fc" id="L607">        numbers.put((doc.docId + docBase),</span>
<span class="fc" id="L608">            (1 + doc.maxPosition - doc.minPosition));</span>
      }
    }
<span class="fc" id="L611">    return numbers;</span>
  }

  /**
   * Gets the number of tokens.
   *
   * @param field the field
   * @param docId the doc id
   * @return the number of tokens
   */
  public Integer getNumberOfTokens(String field, int docId) {
<span class="pc bpc" id="L622" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L623">      IndexDoc doc = getDoc(field, docId);</span>
<span class="pc bpc" id="L624" title="1 of 2 branches missed.">      if (doc != null) {</span>
<span class="fc" id="L625">        return doc.size;</span>
      }
    }
<span class="nc" id="L628">    return null;</span>
  }

  /**
   * Gets the all number of tokens.
   *
   * @param field the field
   * @param docBase the doc base
   * @return the all number of tokens
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public HashMap&lt;Integer, Integer&gt; getAllNumberOfTokens(String field,
      int docBase) throws IOException {
<span class="fc" id="L641">    HashMap&lt;Integer, Integer&gt; numbers = new HashMap&lt;Integer, Integer&gt;();</span>
<span class="pc bpc" id="L642" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L643">      FieldReferences fr = fieldReferences.get(field);</span>
<span class="fc" id="L644">      IndexInput inIndexDoc = indexInputList.get(&quot;doc&quot;);</span>
<span class="fc" id="L645">      inIndexDoc.seek(fr.refIndexDoc);</span>
      IndexDoc doc;
<span class="fc bfc" id="L647" title="All 2 branches covered.">      for (int i = 0; i &lt; fr.numberOfDocs; i++) {</span>
<span class="fc" id="L648">        doc = new IndexDoc(null);</span>
<span class="fc" id="L649">        numbers.put((doc.docId + docBase), doc.size);</span>
      }
    }
<span class="fc" id="L652">    return numbers;</span>
  }

  /**
   * The Class IndexDoc.
   */
  public class IndexDoc {

    /** The doc id. */
    public int docId;

    /** The fp index object id. */
    public long fpIndexObjectId;

    /** The fp index object position. */
    public long fpIndexObjectPosition;

    /** The fp index object parent. */
    public long fpIndexObjectParent;

    /** The smallest object filepointer. */
    public long smallestObjectFilepointer;

    /** The object ref approx offset. */
    public long objectRefApproxOffset;

    /** The object ref approx quotient. */
    public int objectRefApproxQuotient;

    /** The storage flags. */
    public byte storageFlags;

    /** The size. */
    public int size;

    /** The min position. */
    public int minPosition;

    /** The max position. */
    public int maxPosition;

    /**
     * Instantiates a new index doc.
     *
     * @param ref the ref
     * @throws IOException Signals that an I/O exception has occurred.
     */
<span class="fc" id="L699">    public IndexDoc(Long ref) throws IOException {</span>
      try {
<span class="fc" id="L701">        IndexInput inIndexDoc = indexInputList.get(&quot;doc&quot;);</span>
<span class="fc bfc" id="L702" title="All 2 branches covered.">        if (ref != null) {</span>
<span class="fc" id="L703">          inIndexDoc.seek(ref);</span>
        }
<span class="fc" id="L705">        docId = inIndexDoc.readVInt(); // docId</span>
<span class="fc" id="L706">        fpIndexObjectId = inIndexDoc.readVLong(); // ref indexObjectId</span>
<span class="fc" id="L707">        fpIndexObjectPosition = inIndexDoc.readVLong(); // ref</span>
                                                        // indexObjectPosition
<span class="fc" id="L709">        fpIndexObjectParent = inIndexDoc.readVLong(); // ref indexObjectParent</span>
<span class="fc" id="L710">        smallestObjectFilepointer = inIndexDoc.readVLong(); // offset</span>
<span class="fc" id="L711">        objectRefApproxQuotient = inIndexDoc.readVInt(); // slope</span>
<span class="fc" id="L712">        objectRefApproxOffset = inIndexDoc.readZLong(); // offset</span>
<span class="fc" id="L713">        storageFlags = inIndexDoc.readByte(); // flag</span>
<span class="fc" id="L714">        size = inIndexDoc.readVInt(); // number of objects</span>
<span class="fc" id="L715">        minPosition = inIndexDoc.readVInt(); // minimum position</span>
<span class="fc" id="L716">        maxPosition = inIndexDoc.readVInt(); // maximum position</span>
<span class="nc" id="L717">      } catch (Exception e) {</span>
<span class="nc" id="L718">        throw new IOException(e);</span>
<span class="fc" id="L719">      }</span>
<span class="fc" id="L720">    }</span>
  }

  /**
   * The Class FieldReferences.
   */
  private static class FieldReferences {

    /** The ref index doc. */
    public long refIndexDoc;

    /** The ref index doc id. */
    public long refIndexDocId;

    /** The ref prefix. */
    public long refPrefix;

    /** The number of docs. */
    public int numberOfDocs;

    /** The number of prefixes. */
    public int numberOfPrefixes;

    /**
     * Instantiates a new field references.
     *
     * @param refIndexDoc the ref index doc
     * @param refIndexDocId the ref index doc id
     * @param numberOfDocs the number of docs
     * @param refPrefix the ref prefix
     * @param numberOfPrefixes the number of prefixes
     */
    public FieldReferences(long refIndexDoc, long refIndexDocId,
<span class="fc" id="L753">        int numberOfDocs, long refPrefix, int numberOfPrefixes) {</span>
<span class="fc" id="L754">      this.refIndexDoc = refIndexDoc;</span>
<span class="fc" id="L755">      this.refIndexDocId = refIndexDocId;</span>
<span class="fc" id="L756">      this.numberOfDocs = numberOfDocs;</span>
<span class="fc" id="L757">      this.refPrefix = refPrefix;</span>
<span class="fc" id="L758">      this.numberOfPrefixes = numberOfPrefixes;</span>
<span class="fc" id="L759">    }</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>