MtasSpanRecurrenceSpans.java.html 19 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
<?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>MtasSpanRecurrenceSpans.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.search.spans</a> &gt; <span class="el_source">MtasSpanRecurrenceSpans.java</span></div><h1>MtasSpanRecurrenceSpans.java</h1><pre class="source lang-java linenums">package mtas.search.spans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.search.spans.SpanCollector;
import org.apache.lucene.search.spans.Spans;

import mtas.search.spans.util.MtasIgnoreItem;
import mtas.search.spans.util.MtasSpans;

/**
 * The Class MtasSpanRecurrenceSpans.
 */
<span class="pc bpc" id="L19" title="1 of 2 branches missed.">public class MtasSpanRecurrenceSpans extends MtasSpans {</span>

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

  /** The spans. */
  private Spans spans;

  /** The ignore item. */
  private MtasIgnoreItem ignoreItem;

  /** The minimum recurrence. */
  int minimumRecurrence;

  /** The maximum recurrence. */
  int maximumRecurrence;

  /** The queue spans. */
  List&lt;Match&gt; queueSpans;

  /** The queue matches. */
  List&lt;Match&gt; queueMatches;

  /** The current match. */
  Match currentMatch;

  /** The no more positions. */
  boolean noMorePositions;

  /** The last start position. */
  int lastStartPosition; // startPosition of last retrieved span

  /** The last span. */
  boolean lastSpan; // last span for this document added to queue

  /**
   * Instantiates a new mtas span recurrence spans.
   *
   * @param spans the spans
   * @param minimumRecurrence the minimum recurrence
   * @param maximumRecurrence the maximum recurrence
   * @param ignoreSpans the ignore spans
   * @param maximumIgnoreLength the maximum ignore length
   */
  public MtasSpanRecurrenceSpans(Spans spans, int minimumRecurrence,
<span class="fc" id="L64">      int maximumRecurrence, Spans ignoreSpans, Integer maximumIgnoreLength) {</span>
<span class="pc bpc" id="L65" title="2 of 4 branches missed.">    assert minimumRecurrence &lt;= maximumRecurrence : &quot;minimumRecurrence &gt; maximumRecurrence&quot;;</span>
<span class="pc bpc" id="L66" title="2 of 4 branches missed.">    assert minimumRecurrence &gt; 0 : &quot;minimumRecurrence &lt; 1 not supported&quot;;</span>
<span class="fc" id="L67">    this.spans = spans;</span>
<span class="fc" id="L68">    this.minimumRecurrence = minimumRecurrence;</span>
<span class="fc" id="L69">    this.maximumRecurrence = maximumRecurrence;</span>
<span class="fc" id="L70">    queueSpans = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L71">    queueMatches = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L72">    ignoreItem = new MtasIgnoreItem(ignoreSpans, maximumIgnoreLength);</span>
<span class="fc" id="L73">    resetQueue();</span>
<span class="fc" id="L74">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#nextStartPosition()
   */
  @Override
  public int nextStartPosition() throws IOException {
<span class="fc bfc" id="L83" title="All 2 branches covered.">    if (findMatches()) {</span>
<span class="fc" id="L84">      currentMatch = queueMatches.get(0);</span>
<span class="fc" id="L85">      queueMatches.remove(0);</span>
<span class="fc" id="L86">      noMorePositions = false;</span>
<span class="fc" id="L87">      return currentMatch.startPosition();</span>
    } else {
<span class="fc" id="L89">      currentMatch = null;</span>
<span class="fc" id="L90">      noMorePositions = true;</span>
<span class="fc" id="L91">      return NO_MORE_POSITIONS;</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#startPosition()
   */
  @Override
  public int startPosition() {
<span class="pc bpc" id="L102" title="1 of 2 branches missed.">    if (currentMatch == null) {</span>
<span class="nc bnc" id="L103" title="All 2 branches missed.">      if (noMorePositions) {</span>
<span class="nc" id="L104">        return NO_MORE_POSITIONS;</span>
      } else {
<span class="nc" id="L106">        return -1;</span>
      }
    } else {
<span class="fc" id="L109">      return currentMatch.startPosition();</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#endPosition()
   */
  @Override
  public int endPosition() {
<span class="pc bpc" id="L120" title="1 of 2 branches missed.">    if (currentMatch == null) {</span>
<span class="nc bnc" id="L121" title="All 2 branches missed.">      if (noMorePositions) {</span>
<span class="nc" id="L122">        return NO_MORE_POSITIONS;</span>
      } else {
<span class="nc" id="L124">        return -1;</span>
      }
    } else {
<span class="fc" id="L127">      return currentMatch.endPosition();</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#width()
   */
  @Override
  public int width() {
<span class="nc" id="L138">    return 1;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.search.spans.Spans#collect(org.apache.lucene.search.spans
   * .SpanCollector)
   */
  @Override
  public void collect(SpanCollector collector) throws IOException {
<span class="nc" id="L150">    spans.collect(collector);</span>
<span class="nc" id="L151">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#docID()
   */
  @Override
  public int docID() {
<span class="fc" id="L160">    return spans.docID();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#nextDoc()
   */
  @Override
  public int nextDoc() throws IOException {
<span class="fc" id="L170">    resetQueue();</span>
<span class="fc bfc" id="L171" title="All 2 branches covered.">    return (spans.nextDoc() == NO_MORE_DOCS) ? NO_MORE_DOCS : toMatchDoc();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#advance(int)
   */
  @Override
  public int advance(int target) throws IOException {
<span class="fc" id="L181">    resetQueue();</span>
<span class="pc bpc" id="L182" title="1 of 2 branches missed.">    return (spans.advance(target) == NO_MORE_DOCS) ? NO_MORE_DOCS</span>
<span class="fc" id="L183">        : toMatchDoc();</span>
  }

  /**
   * Reset queue.
   */
  void resetQueue() {
<span class="fc" id="L190">    queueSpans.clear();</span>
<span class="fc" id="L191">    queueMatches.clear();</span>
<span class="fc" id="L192">    lastStartPosition = 0;</span>
<span class="fc" id="L193">    lastSpan = false;</span>
<span class="fc" id="L194">    currentMatch = null;</span>
<span class="fc" id="L195">    noMorePositions = false;</span>
<span class="fc" id="L196">  }</span>

  /**
   * To match doc.
   *
   * @return the int
   * @throws IOException Signals that an I/O exception has occurred.
   */
  int toMatchDoc() throws IOException {
    while (true) {
<span class="pc bpc" id="L206" title="1 of 2 branches missed.">      if (findMatches()) {</span>
<span class="fc" id="L207">        return docID();</span>
      }
<span class="nc" id="L209">      resetQueue();</span>
<span class="nc bnc" id="L210" title="All 2 branches missed.">      if (spans.nextDoc() == NO_MORE_DOCS) {</span>
<span class="nc" id="L211">        return NO_MORE_DOCS;</span>
      }
    }
  }

  /**
   * Collect span.
   *
   * @return true, if successful
   * @throws IOException Signals that an I/O exception has occurred.
   */
  // try to get something in the queue of spans
  private boolean collectSpan() throws IOException {
<span class="fc bfc" id="L224" title="All 2 branches covered.">    if (lastSpan) {</span>
<span class="fc" id="L225">      return false;</span>
<span class="fc bfc" id="L226" title="All 2 branches covered.">    } else if (spans.nextStartPosition() == NO_MORE_POSITIONS) {</span>
<span class="fc" id="L227">      lastSpan = true;</span>
<span class="fc" id="L228">      return false;</span>
    } else {
<span class="fc" id="L230">      queueSpans.add(new Match(spans.startPosition(), spans.endPosition()));</span>
<span class="fc" id="L231">      lastStartPosition = spans.startPosition();</span>
<span class="fc" id="L232">      return true;</span>
    }
  }

  /**
   * Find matches.
   *
   * @return true, if successful
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private boolean findMatches() throws IOException {
    // check for something in queue of matches
<span class="fc bfc" id="L244" title="All 2 branches covered.">    if (!queueMatches.isEmpty()) {</span>
<span class="fc" id="L245">      return true;</span>
    } else {
<span class="fc" id="L247">      ignoreItem.advanceToDoc(spans.docID());</span>
      while (true) {
        // try to get something in queue of spans
<span class="fc bfc" id="L250" title="All 4 branches covered.">        if (queueSpans.isEmpty() &amp;&amp; !collectSpan()) {</span>
<span class="fc" id="L251">          return false;</span>
        }
        // try to get matches with first span in queue
<span class="fc" id="L254">        Match firstMatch = queueSpans.remove(0);</span>
        // create a list of matches with same startPosition as firstMatch
<span class="fc" id="L256">        List&lt;Match&gt; matches = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L257">        matches.add(firstMatch);</span>
        // matches.addAll(expandWithIgnoreItem(spans.docID(), firstMatch));
        // try to collect spans until lastStartPosition not equal to
        // startPosition of firstMatch
<span class="fc bfc" id="L261" title="All 4 branches covered.">        while (!lastSpan &amp;&amp; (lastStartPosition == firstMatch.startPosition())) {</span>
<span class="fc" id="L262">          collectSpan();</span>
        }
<span class="fc bfc" id="L264" title="All 2 branches covered.">        while (!queueSpans.isEmpty() &amp;&amp; (queueSpans.get(0)</span>
<span class="pc bpc" id="L265" title="1 of 2 branches missed.">            .startPosition() == firstMatch.startPosition())) {</span>
<span class="nc" id="L266">          Match additionalMatch = queueSpans.remove(0);</span>
<span class="nc" id="L267">          matches.add(additionalMatch);</span>
<span class="nc" id="L268">          matches.addAll(expandWithIgnoreItem(spans.docID(), additionalMatch));</span>
<span class="nc" id="L269">        }</span>
        // construct all matches for this startPosition
<span class="fc bfc" id="L271" title="All 2 branches covered.">        for (Match match : matches) {</span>
<span class="fc bfc" id="L272" title="All 2 branches covered.">          for (int n = (minimumRecurrence - 1); n &lt;= (maximumRecurrence</span>
<span class="fc" id="L273">              - 1); n++) {</span>
<span class="fc" id="L274">            findMatches(match, n);</span>
          }
<span class="fc" id="L276">        }</span>
        // check for something in queue of matches
<span class="fc bfc" id="L278" title="All 2 branches covered.">        if (!queueMatches.isEmpty()) {</span>
<span class="fc" id="L279">          ignoreItem.removeBefore(spans.docID(),</span>
<span class="fc" id="L280">              queueMatches.get(0).startPosition());</span>
<span class="fc" id="L281">          return true;</span>
        }
<span class="fc" id="L283">      }</span>
    }
  }

  /**
   * Find matches.
   *
   * @param match the match
   * @param n the n
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void findMatches(Match match, int n) throws IOException {
<span class="fc bfc" id="L295" title="All 2 branches covered.">    if (n &gt; 0) {</span>
<span class="fc" id="L296">      int largestMatchingEndPosition = match.endPosition();</span>
<span class="fc" id="L297">      Set&lt;Integer&gt; list = ignoreItem.getFullEndPositionList(spans.docID(),</span>
<span class="fc" id="L298">          match.endPosition());</span>
      // try to find matches with existing queue
<span class="fc bfc" id="L300" title="All 2 branches covered.">      if (!queueSpans.isEmpty()) {</span>
        Match span;
<span class="fc bfc" id="L302" title="All 2 branches covered.">        for (int i = 0; i &lt; queueSpans.size(); i++) {</span>
<span class="fc" id="L303">          span = queueSpans.get(i);</span>
<span class="pc bpc" id="L304" title="1 of 4 branches missed.">          if (match.endPosition() == span.startPosition()</span>
<span class="nc bnc" id="L305" title="All 2 branches missed.">              || (list != null &amp;&amp; list.contains(span.startPosition()))) {</span>
<span class="fc" id="L306">            findMatches(new Match(match.startPosition(), span.endPosition()),</span>
                (n - 1));
<span class="fc" id="L308">            largestMatchingEndPosition = Math.max(largestMatchingEndPosition,</span>
<span class="fc" id="L309">                span.endPosition());</span>
          }
        }
      }
      // extend queue if necessary and possible
<span class="fc bfc" id="L314" title="All 4 branches covered.">      while (!lastSpan &amp;&amp; (largestMatchingEndPosition &gt;= lastStartPosition)) {</span>
<span class="fc bfc" id="L315" title="All 2 branches covered.">        if (spans.nextStartPosition() == NO_MORE_POSITIONS) {</span>
<span class="fc" id="L316">          lastSpan = true;</span>
        } else {
<span class="fc" id="L318">          Match span = new Match(spans.startPosition(), spans.endPosition());</span>
<span class="fc" id="L319">          queueSpans.add(span);</span>
<span class="fc" id="L320">          lastStartPosition = spans.startPosition();</span>
          // check if this provides new match
<span class="pc bpc" id="L322" title="2 of 4 branches missed.">          if (match.endPosition() == span.startPosition()</span>
<span class="nc bnc" id="L323" title="All 2 branches missed.">              || (list != null &amp;&amp; list.contains(span.startPosition()))) {</span>
<span class="nc" id="L324">            findMatches(new Match(match.startPosition(), span.endPosition()),</span>
                (n - 1));
<span class="nc" id="L326">            largestMatchingEndPosition = Math.max(largestMatchingEndPosition,</span>
<span class="nc" id="L327">                span.endPosition());</span>
          }
<span class="fc" id="L329">        }</span>
      }
<span class="fc" id="L331">    } else {</span>
      // only unique spans
<span class="pc bpc" id="L333" title="1 of 2 branches missed.">      if (!queueMatches.contains(match)) {</span>
<span class="fc" id="L334">        queueMatches.add(match);</span>
      }
    }
<span class="fc" id="L337">  }</span>

  /**
   * Expand with ignore item.
   *
   * @param docId the doc id
   * @param match the match
   * @return the list
   */
  private List&lt;Match&gt; expandWithIgnoreItem(int docId, Match match) {
<span class="nc" id="L347">    List&lt;Match&gt; list = new ArrayList&lt;&gt;();</span>
    try {
<span class="nc" id="L349">      Set&lt;Integer&gt; ignoreList = ignoreItem.getFullEndPositionList(docId,</span>
<span class="nc" id="L350">          match.endPosition);</span>
<span class="nc bnc" id="L351" title="All 2 branches missed.">      if (ignoreList != null) {</span>
<span class="nc bnc" id="L352" title="All 2 branches missed.">        for (Integer endPosition : ignoreList) {</span>
<span class="nc" id="L353">          list.add(new Match(match.startPosition, endPosition));</span>
<span class="nc" id="L354">        }</span>
      }
<span class="nc" id="L356">    } catch (IOException e) {</span>
<span class="nc" id="L357">      log.debug(e);</span>
<span class="nc" id="L358">    }</span>
<span class="nc" id="L359">    return list;</span>
  }

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

    /** The start position. */
    private int startPosition;

    /** The end position. */
    private int endPosition;

    /**
     * Instantiates a new match.
     *
     * @param startPosition the start position
     * @param endPosition the end position
     */
<span class="fc" id="L379">    Match(int startPosition, int endPosition) {</span>
<span class="fc" id="L380">      this.startPosition = startPosition;</span>
<span class="fc" id="L381">      this.endPosition = endPosition;</span>
<span class="fc" id="L382">    }</span>

    /**
     * Start position.
     *
     * @return the int
     */
    public int startPosition() {
<span class="fc" id="L390">      return startPosition;</span>
    }

    /**
     * End position.
     *
     * @return the int
     */
    public int endPosition() {
<span class="fc" id="L399">      return endPosition;</span>
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
<span class="pc bpc" id="L409" title="1 of 2 branches missed.">      if (this == obj)</span>
<span class="nc" id="L410">        return true;</span>
<span class="pc bpc" id="L411" title="1 of 2 branches missed.">      if (obj == null)</span>
<span class="nc" id="L412">        return false;</span>
<span class="pc bpc" id="L413" title="1 of 2 branches missed.">      if (getClass() != obj.getClass())</span>
<span class="nc" id="L414">        return false;</span>
<span class="fc" id="L415">      final Match that = (Match) obj;</span>
<span class="pc bpc" id="L416" title="2 of 4 branches missed.">      return startPosition == that.startPosition</span>
          &amp;&amp; endPosition == that.endPosition;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
<span class="nc" id="L427">      int h = this.getClass().getSimpleName().hashCode();</span>
<span class="nc" id="L428">      h = (h * 5) ^ startPosition;</span>
<span class="nc" id="L429">      h = (h * 7) ^ endPosition;</span>
<span class="nc" id="L430">      return h;</span>
    }

  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#cost()
   */
  @Override
  public long cost() {
<span class="pc bpc" id="L442" title="1 of 2 branches missed.">    return (spans == null) ? 0 : spans.cost();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#positionsCost()
   */
  @Override
  public float positionsCost() {
<span class="nc" id="L452">    return 0;</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>