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 764 765 766 767 768
<?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,</span>
              emptyArgs);
<span class="fc bfc" id="L84" title="All 2 branches covered.">        } else if (m.getName().equals(&quot;getIndexInputOffsetList&quot;)) {</span>
<span class="fc" id="L85">          indexInputOffsetList = (HashMap&lt;String, Long&gt;) m.invoke(t,</span>
              emptyArgs);
<span class="fc bfc" id="L87" title="All 2 branches covered.">        } else if (m.getName().equals(&quot;getVersion&quot;)) {</span>
<span class="fc" id="L88">          version = m.invoke(t, emptyArgs);</span>
        }
      }
<span class="pc bpc" id="L91" title="3 of 6 branches missed.">      if (indexInputList == null || indexInputOffsetList == null</span>
          || version == null) {
<span class="nc" id="L93">        throw new IOException(&quot;Reader doesn't provide MtasFieldsProducer&quot;);</span>
      } else {
<span class="fc" id="L95">        return new CodecInfo(indexInputList, indexInputOffsetList, (int) version);</span>
      }
<span class="nc" id="L97">    } catch (IllegalAccessException | InvocationTargetException e) {</span>
<span class="nc" id="L98">      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="L109">    IndexInput inField = indexInputList.get(&quot;field&quot;);</span>
<span class="fc" id="L110">    inField.seek(indexInputOffsetList.get(&quot;field&quot;));</span>
    // store field references in memory
<span class="fc" id="L112">    fieldReferences = new HashMap&lt;String, FieldReferences&gt;();</span>
<span class="fc" id="L113">    boolean doInit = true;</span>
<span class="fc bfc" id="L114" title="All 2 branches covered.">    while (doInit) {</span>
      try {
<span class="fc" id="L116">        String field = inField.readString();</span>
<span class="fc" id="L117">        long refIndexDoc = inField.readVLong();</span>
<span class="fc" id="L118">        long refIndexDocId = inField.readVLong();</span>
<span class="fc" id="L119">        int numberOfDocs = inField.readVInt();</span>
<span class="fc" id="L120">        inField.readVLong(); //refTerm</span>
<span class="fc" id="L121">        inField.readVInt(); //numberOfTerms</span>
<span class="fc" id="L122">        long refPrefix = inField.readVLong();</span>
<span class="fc" id="L123">        int numberOfPrefixes = inField.readVInt();</span>
<span class="fc" id="L124">        fieldReferences.put(field,</span>
            new FieldReferences(refIndexDoc, refIndexDocId, numberOfDocs,
                refPrefix, numberOfPrefixes));
<span class="fc" id="L127">      } catch (IOException e) {</span>
<span class="fc" id="L128">        log.debug(e); </span>
<span class="fc" id="L129">        doInit = false;</span>
<span class="fc" id="L130">      }</span>
    }
    // prefixReferences
<span class="fc" id="L133">    prefixReferences = new HashMap&lt;String, LinkedHashMap&lt;String, Long&gt;&gt;();</span>
<span class="fc" id="L134">  }</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="L150">      IndexDoc doc = getDoc(field, docId);</span>
<span class="nc" id="L151">      IndexInput inObjectId = indexInputList.get(&quot;indexObjectId&quot;);</span>
<span class="nc" id="L152">      IndexInput inObject = indexInputList.get(&quot;object&quot;);</span>
<span class="nc" id="L153">      IndexInput inTerm = indexInputList.get(&quot;term&quot;);</span>
<span class="nc bnc" id="L154" title="All 2 branches missed.">      if (doc.storageFlags == MtasCodecPostingsFormat.MTAS_STORAGE_BYTE) {</span>
<span class="nc" id="L155">        inObjectId.seek(doc.fpIndexObjectId + (mtasId * 1L));</span>
<span class="nc" id="L156">        objectRefApproxCorrection = Long.valueOf(inObjectId.readByte());</span>
<span class="nc bnc" id="L157" title="All 2 branches missed.">      } else if (doc.storageFlags == MtasCodecPostingsFormat.MTAS_STORAGE_SHORT) {</span>
<span class="nc" id="L158">        inObjectId.seek(doc.fpIndexObjectId + (mtasId * 2L));</span>
<span class="nc" id="L159">        objectRefApproxCorrection = Long.valueOf(inObjectId.readShort());</span>
<span class="nc bnc" id="L160" title="All 2 branches missed.">      } else if (doc.storageFlags == MtasCodecPostingsFormat.MTAS_STORAGE_INTEGER) {</span>
<span class="nc" id="L161">        inObjectId.seek(doc.fpIndexObjectId + (mtasId * 4L));</span>
<span class="nc" id="L162">        objectRefApproxCorrection = Long.valueOf(inObjectId.readInt());</span>
      } else {
<span class="nc" id="L164">        inObjectId.seek(doc.fpIndexObjectId + (mtasId * 8L));</span>
<span class="nc" id="L165">        objectRefApproxCorrection = Long.valueOf(inObjectId.readLong());</span>
      }
<span class="nc" id="L167">      ref = objectRefApproxCorrection + doc.objectRefApproxOffset</span>
          + (mtasId * (long) doc.objectRefApproxQuotient);
<span class="nc" id="L169">      return MtasCodecPostingsFormat.getToken(inObject, inTerm, ref);</span>
<span class="nc" id="L170">    } catch (Exception e) {</span>
<span class="nc" id="L171">      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="L186">    IndexDoc doc = getDoc(field, docId);</span>
<span class="nc" id="L187">    IndexInput inIndexObjectParent = indexInputList.get(&quot;indexObjectParent&quot;);</span>
<span class="nc" id="L188">    ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; hits = CodecSearchTree.searchMtasTree(position,</span>
        inIndexObjectParent, doc.fpIndexObjectParent,
        doc.smallestObjectFilepointer);
<span class="nc" id="L191">    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="L205">    IndexDoc doc = getDoc(field, docId);</span>
<span class="nc" id="L206">    IndexInput inIndexObjectPosition = indexInputList</span>
<span class="nc" id="L207">        .get(&quot;indexObjectPosition&quot;);</span>
<span class="nc" id="L208">    ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; hits = CodecSearchTree.searchMtasTree(position,</span>
        inIndexObjectPosition, doc.fpIndexObjectPosition,
        doc.smallestObjectFilepointer);
<span class="nc" id="L211">    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="L239">    IndexDoc doc = getDoc(field, docId);</span>
<span class="nc" id="L240">    IndexInput inIndexObjectPosition = indexInputList</span>
<span class="nc" id="L241">        .get(&quot;indexObjectPosition&quot;);</span>
<span class="nc bnc" id="L242" title="All 2 branches missed.">    if (doc != null) {</span>
<span class="nc" id="L243">      ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; hits = CodecSearchTree.searchMtasTree(</span>
          startPosition, endPosition, inIndexObjectPosition,
          doc.fpIndexObjectPosition, doc.smallestObjectFilepointer);
<span class="nc" id="L246">      return getPrefixFilteredObjects(hits, prefixes);</span>
    } else {
<span class="nc" id="L248">      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="L263">    ArrayList&lt;MtasTokenString&gt; tokens = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L264">    IndexInput inObject = indexInputList.get(&quot;object&quot;);</span>
<span class="nc" id="L265">    IndexInput inTerm = indexInputList.get(&quot;term&quot;);</span>
<span class="nc bnc" id="L266" title="All 2 branches missed.">    for (MtasTreeHit&lt;?&gt; hit : hits) {</span>
<span class="nc" id="L267">      MtasTokenString token = MtasCodecPostingsFormat.getToken(inObject,</span>
<span class="nc" id="L268">          inTerm, hit.ref);</span>
<span class="nc bnc" id="L269" title="All 2 branches missed.">      if (token != null) {</span>
<span class="nc bnc" id="L270" title="All 2 branches missed.">        if (!prefixes.isEmpty()) {</span>
<span class="nc bnc" id="L271" title="All 2 branches missed.">          if (prefixes.contains(token.getPrefix())) {</span>
<span class="nc" id="L272">            tokens.add(token);</span>
          }
        } else {
<span class="nc" id="L275">          tokens.add(token);</span>
        }
      }
<span class="nc" id="L278">    }</span>
<span class="nc" id="L279">    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="L295">    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="L313">    IndexDoc doc = getDoc(field, docId);</span>
<span class="nc" id="L314">    IndexInput inIndexObjectPosition = indexInputList</span>
<span class="nc" id="L315">        .get(&quot;indexObjectPosition&quot;);</span>
<span class="nc bnc" id="L316" title="All 2 branches missed.">    if (doc != null) {</span>
<span class="nc" id="L317">      ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; hitItems = CodecSearchTree.searchMtasTree(</span>
          startPosition, endPosition, inIndexObjectPosition,
          doc.fpIndexObjectPosition, doc.smallestObjectFilepointer);
<span class="nc" id="L320">      List&lt;MtasTreeHit&lt;String&gt;&gt; hits = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L321">      Map&lt;String, Integer&gt; prefixIds = getPrefixesIds(field, prefixes);</span>
<span class="nc bnc" id="L322" title="All 4 branches missed.">      if (prefixIds != null &amp;&amp; prefixIds.size() &gt; 0) {</span>
<span class="nc" id="L323">        ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; filteredHitItems = new ArrayList&lt;MtasTreeHit&lt;?&gt;&gt;();</span>
        
<span class="nc bnc" id="L325" title="All 2 branches missed.">        for (MtasTreeHit&lt;?&gt; hitItem : hitItems) {</span>
<span class="nc bnc" id="L326" title="All 2 branches missed.">          if (prefixIds.containsValue(hitItem.additionalId)) {</span>
<span class="nc" id="L327">            filteredHitItems.add(hitItem);</span>
          }
<span class="nc" id="L329">        }</span>
<span class="nc bnc" id="L330" title="All 2 branches missed.">        if (filteredHitItems.size() &gt; 0) {</span>
<span class="nc" id="L331">          ArrayList&lt;MtasTokenString&gt; objects = getObjects(filteredHitItems);</span>
<span class="nc bnc" id="L332" title="All 2 branches missed.">          for (MtasTokenString token : objects) {</span>
<span class="nc" id="L333">            MtasTreeHit&lt;String&gt; hit = new MtasTreeHit&lt;String&gt;(</span>
<span class="nc" id="L334">                token.getPositionStart(), token.getPositionEnd(),</span>
<span class="nc" id="L335">                token.getTokenRef(), 0, 0, token.getValue());</span>
<span class="nc" id="L336">            hits.add(hit);</span>
<span class="nc" id="L337">          }</span>
        }
      }
<span class="nc" id="L340">      return hits;</span>
    } else {
<span class="nc" id="L342">      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="L359">    IndexDoc doc = getDoc(field, docId);</span>
<span class="fc" id="L360">    IndexInput inIndexObjectPosition = indexInputList</span>
<span class="fc" id="L361">        .get(&quot;indexObjectPosition&quot;);</span>
<span class="fc" id="L362">    IndexInput inTerm = indexInputList.get(&quot;term&quot;);</span>
    // create tree interval hits
<span class="fc" id="L364">    IntervalRBTree&lt;String&gt; positionTree = new IntervalRBTree&lt;String&gt;(</span>
        positionsHits);

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

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

<span class="fc bfc" id="L383" title="All 2 branches covered.">      for (IntervalTreeNodeData&lt;String&gt; positionHit : positionsHits) {</span>
<span class="fc bfc" id="L384" title="All 2 branches covered.">        for (MtasTreeHit&lt;String&gt; hit : positionHit.list) {</span>
<span class="pc bpc" id="L385" title="1 of 2 branches missed.">          if (hit.idData == null) {</span>
<span class="fc" id="L386">            hit.idData = idPrefixes.get(hit.additionalId);</span>
<span class="fc bfc" id="L387" title="All 2 branches covered.">            if (!refTerms.containsKey(hit.additionalRef)) {</span>
<span class="fc" id="L388">              refTerms.put(hit.additionalRef,</span>
<span class="fc" id="L389">                  MtasCodecPostingsFormat.getTerm(inTerm, hit.additionalRef));</span>
            }
<span class="fc" id="L391">            hit.refData = refTerms.get(hit.additionalRef);</span>
          }
<span class="fc" id="L393">        }</span>
<span class="fc" id="L394">      }</span>
    }
<span class="fc" id="L396">  }</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="L407">    ArrayList&lt;MtasTokenString&gt; tokens = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L408">    IndexInput inObject = indexInputList.get(&quot;object&quot;);</span>
<span class="nc" id="L409">    IndexInput inTerm = indexInputList.get(&quot;term&quot;);</span>
<span class="nc bnc" id="L410" title="All 2 branches missed.">    for (MtasTreeHit&lt;?&gt; hit : hits) {</span>
<span class="nc" id="L411">      MtasTokenString token = MtasCodecPostingsFormat.getToken(inObject,</span>
<span class="nc" id="L412">          inTerm, hit.ref);</span>
<span class="nc bnc" id="L413" title="All 2 branches missed.">      if (token != null) {</span>
<span class="nc" id="L414">        tokens.add(token);</span>
      }
<span class="nc" id="L416">    }</span>
<span class="nc" id="L417">    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="L430">      ArrayList&lt;MtasTreeHit&lt;String&gt;&gt; terms = new ArrayList&lt;MtasTreeHit&lt;String&gt;&gt;();</span>
<span class="nc" id="L431">      IndexInput inTerm = indexInputList.get(&quot;term&quot;);</span>
<span class="nc bnc" id="L432" title="All 2 branches missed.">      for (MtasTreeHit&lt;?&gt; hit : refs) {</span>
<span class="nc" id="L433">        inTerm.seek(hit.ref);</span>
<span class="nc" id="L434">        String term = inTerm.readString();</span>
<span class="nc" id="L435">        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="L438">        terms.add(newHit);</span>
<span class="nc" id="L439">      }</span>
<span class="nc" id="L440">      return terms;</span>
<span class="nc" id="L441">    } catch (Exception e) {</span>
<span class="nc" id="L442">      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="L455">    LinkedHashMap&lt;String, Long&gt; refs = getPrefixes(field);</span>
<span class="pc bpc" id="L456" title="1 of 2 branches missed.">    if (refs != null) {</span>
<span class="fc" id="L457">      List&lt;String&gt; list = new ArrayList&lt;&gt;(refs.keySet());</span>
<span class="fc" id="L458">      Map&lt;String, Integer&gt; result = new HashMap&lt;&gt;();</span>
<span class="fc bfc" id="L459" title="All 2 branches covered.">      for (String prefix : prefixes) {</span>
<span class="fc" id="L460">        int id = list.indexOf(prefix);</span>
<span class="pc bpc" id="L461" title="1 of 2 branches missed.">        if (id &gt;= 0) {</span>
<span class="fc" id="L462">          result.put(prefix, id + 1);</span>
        }
<span class="fc" id="L464">      }</span>
<span class="fc" id="L465">      return result;</span>
    } else {
<span class="nc" id="L467">      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="L478" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L479">      FieldReferences fr = fieldReferences.get(field);</span>
<span class="pc bpc" id="L480" title="1 of 2 branches missed.">      if (!prefixReferences.containsKey(field)) {</span>
<span class="fc" id="L481">        LinkedHashMap&lt;String, Long&gt; refs = new LinkedHashMap&lt;String, Long&gt;();</span>
        try {
<span class="fc" id="L483">          IndexInput inPrefix = indexInputList.get(&quot;prefix&quot;);</span>
<span class="fc" id="L484">          inPrefix.seek(fr.refPrefix);</span>
<span class="fc bfc" id="L485" title="All 2 branches covered.">          for (int i = 0; i &lt; fr.numberOfPrefixes; i++) {</span>
<span class="fc" id="L486">            Long ref = inPrefix.getFilePointer();</span>
<span class="fc" id="L487">            String prefix = inPrefix.readString();</span>
<span class="fc" id="L488">            refs.put(prefix, ref);</span>
          }
<span class="nc" id="L490">        } catch (Exception e) {</span>
<span class="nc" id="L491">          log.error(e);</span>
<span class="nc" id="L492">          refs.clear();</span>
<span class="fc" id="L493">        }</span>
<span class="fc" id="L494">        prefixReferences.put(field, refs);</span>
<span class="fc" id="L495">        return refs;</span>
      } else {
<span class="nc" id="L497">        return prefixReferences.get(field);</span>
      }
    } else {
<span class="nc" id="L500">      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="L512" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L513">      FieldReferences fr = fieldReferences.get(field);</span>
      try {
<span class="fc" id="L515">        IndexInput inIndexDocId = indexInputList.get(&quot;indexDocId&quot;);</span>
<span class="fc" id="L516">        ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; list = CodecSearchTree.searchMtasTree(docId,</span>
            inIndexDocId, fr.refIndexDocId, fr.refIndexDoc);
<span class="pc bpc" id="L518" title="1 of 2 branches missed.">        if (list.size() == 1) {</span>
<span class="fc" id="L519">          return new IndexDoc(list.get(0).ref);</span>
        }
<span class="nc" id="L521">      } catch (IOException e) {</span>
<span class="nc" id="L522">        log.debug(e);</span>
<span class="nc" id="L523">        return null;</span>
<span class="nc" id="L524">      }</span>
    }
<span class="nc" id="L526">    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="L537" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L538">      FieldReferences fr = fieldReferences.get(field);</span>
      try {
<span class="fc bfc" id="L540" title="All 2 branches covered.">        if (previousDocId &lt; 0) {</span>
<span class="fc" id="L541">          return new IndexDoc(fr.refIndexDoc);</span>
        } else {
<span class="fc" id="L543">          int nextDocId = previousDocId + 1;</span>
<span class="fc" id="L544">          IndexInput inIndexDocId = indexInputList.get(&quot;indexDocId&quot;);</span>
<span class="fc" id="L545">          ArrayList&lt;MtasTreeHit&lt;?&gt;&gt; list = CodecSearchTree.advanceMtasTree(</span>
              nextDocId, inIndexDocId, fr.refIndexDocId, fr.refIndexDoc);
<span class="fc bfc" id="L547" title="All 2 branches covered.">          if (list.size() == 1) {</span>
<span class="fc" id="L548">            IndexInput inDoc = indexInputList.get(&quot;doc&quot;);</span>
<span class="fc" id="L549">            inDoc.seek(list.get(0).ref);</span>
<span class="fc" id="L550">            return new IndexDoc(inDoc.getFilePointer());</span>
          }
        }
<span class="nc" id="L553">      } catch (IOException e) {</span>
<span class="nc" id="L554">        log.debug(e);</span>
<span class="nc" id="L555">        return null;</span>
<span class="fc" id="L556">      }</span>
    }
<span class="fc" id="L558">    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="L568" title="All 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="nc" id="L569">      FieldReferences fr = fieldReferences.get(field);</span>
<span class="nc" id="L570">      return fr.numberOfDocs;</span>
    } else {
<span class="nc" id="L572">      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="L584" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L585">      IndexDoc doc = getDoc(field, docId);</span>
<span class="pc bpc" id="L586" title="1 of 2 branches missed.">      if (doc != null) {</span>
<span class="fc" id="L587">        return 1 + doc.maxPosition - doc.minPosition;</span>
      }
    }
<span class="nc" id="L590">    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="L603">    HashMap&lt;Integer, Integer&gt; numbers = new HashMap&lt;Integer, Integer&gt;();</span>
<span class="pc bpc" id="L604" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L605">      FieldReferences fr = fieldReferences.get(field);</span>
<span class="fc" id="L606">      IndexInput inIndexDoc = indexInputList.get(&quot;doc&quot;);</span>
<span class="fc" id="L607">      inIndexDoc.seek(fr.refIndexDoc);</span>
      IndexDoc doc;
<span class="fc bfc" id="L609" title="All 2 branches covered.">      for (int i = 0; i &lt; fr.numberOfDocs; i++) {</span>
<span class="fc" id="L610">        doc = new IndexDoc(null);</span>
<span class="fc" id="L611">        numbers.put((doc.docId + docBase),</span>
<span class="fc" id="L612">            (1 + doc.maxPosition - doc.minPosition));</span>
      }
    }
<span class="fc" id="L615">    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="L626" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L627">      IndexDoc doc = getDoc(field, docId);</span>
<span class="pc bpc" id="L628" title="1 of 2 branches missed.">      if (doc != null) {</span>
<span class="fc" id="L629">        return doc.size;</span>
      }
    }
<span class="nc" id="L632">    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="L645">    HashMap&lt;Integer, Integer&gt; numbers = new HashMap&lt;Integer, Integer&gt;();</span>
<span class="pc bpc" id="L646" title="1 of 2 branches missed.">    if (fieldReferences.containsKey(field)) {</span>
<span class="fc" id="L647">      FieldReferences fr = fieldReferences.get(field);</span>
<span class="fc" id="L648">      IndexInput inIndexDoc = indexInputList.get(&quot;doc&quot;);</span>
<span class="fc" id="L649">      inIndexDoc.seek(fr.refIndexDoc);</span>
      IndexDoc doc;
<span class="fc bfc" id="L651" title="All 2 branches covered.">      for (int i = 0; i &lt; fr.numberOfDocs; i++) {</span>
<span class="fc" id="L652">        doc = new IndexDoc(null);</span>
<span class="fc" id="L653">        numbers.put((doc.docId + docBase), doc.size);</span>
      }
    }
<span class="fc" id="L656">    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="L704">    public IndexDoc(Long ref) throws IOException {</span>
      try {
<span class="fc" id="L706">        IndexInput inIndexDoc = indexInputList.get(&quot;doc&quot;);</span>
<span class="fc bfc" id="L707" title="All 2 branches covered.">        if (ref != null) {</span>
<span class="fc" id="L708">          inIndexDoc.seek(ref);</span>
        }
<span class="fc" id="L710">        docId = inIndexDoc.readVInt(); // docId</span>
<span class="fc" id="L711">        fpIndexObjectId = inIndexDoc.readVLong(); // ref indexObjectId</span>
<span class="fc" id="L712">        fpIndexObjectPosition = inIndexDoc.readVLong(); // ref indexObjectPosition</span>
<span class="fc" id="L713">        fpIndexObjectParent = inIndexDoc.readVLong(); // ref indexObjectParent</span>
<span class="fc" id="L714">        smallestObjectFilepointer = inIndexDoc.readVLong(); // offset</span>
<span class="fc" id="L715">        objectRefApproxQuotient = inIndexDoc.readVInt(); // slope</span>
<span class="fc" id="L716">        objectRefApproxOffset = inIndexDoc.readZLong(); // offset</span>
<span class="fc" id="L717">        storageFlags = inIndexDoc.readByte(); // flag</span>
<span class="fc" id="L718">        size = inIndexDoc.readVInt(); // number of objects</span>
<span class="fc" id="L719">        minPosition = inIndexDoc.readVInt(); // minimum position</span>
<span class="fc" id="L720">        maxPosition = inIndexDoc.readVInt(); // maximum position</span>
<span class="nc" id="L721">      } catch (Exception e) {</span>
<span class="nc" id="L722">        throw new IOException(e);</span>
<span class="fc" id="L723">      }</span>
<span class="fc" id="L724">    }</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,
        int numberOfDocs, long refPrefix,
<span class="fc" id="L758">        int numberOfPrefixes) {</span>
<span class="fc" id="L759">      this.refIndexDoc = refIndexDoc;</span>
<span class="fc" id="L760">      this.refIndexDocId = refIndexDocId;</span>
<span class="fc" id="L761">      this.numberOfDocs = numberOfDocs;</span>
<span class="fc" id="L762">      this.refPrefix = refPrefix;</span>
<span class="fc" id="L763">      this.numberOfPrefixes = numberOfPrefixes;</span>
<span class="fc" id="L764">    }</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>