MtasSpanNotSpans.java.html 18.6 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
<?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>MtasSpanNotSpans.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">MtasSpanNotSpans.java</span></div><h1>MtasSpanNotSpans.java</h1><pre class="source lang-java linenums">package mtas.search.spans;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.lucene.search.TwoPhaseIterator;
import org.apache.lucene.search.spans.SpanCollector;
import mtas.search.spans.MtasSpanNotQuery.MtasSpanNotQuerySpans;
import mtas.search.spans.util.MtasSpans;

/**
 * The Class MtasSpanNotSpans.
 */
public class MtasSpanNotSpans extends MtasSpans {

  /** The query. */
  private MtasSpanNotQuery query;

  /** The spans 1. */
  private MtasSpanNotQuerySpans spans1;

  /** The spans 2. */
  private MtasSpanNotQuerySpans spans2;

  /** The called next start position. */
  private boolean calledNextStartPosition;

  /** The last spans 2 start position. */
  private int lastSpans2StartPosition;

  /** The last spans 2 end position. */
  private int lastSpans2EndPosition;

  /** The last spans 2 end positions. */
  private Set&lt;Integer&gt; lastSpans2EndPositions;

  /** The next spans 2 start position. */
  private int nextSpans2StartPosition;

  /** The next spans 2 end position. */
  private int nextSpans2EndPosition;

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

  /**
   * Instantiates a new mtas span not spans.
   *
   * @param query the query
   * @param spans1 the spans 1
   * @param spans2 the spans 2
   */
  public MtasSpanNotSpans(MtasSpanNotQuery query, MtasSpanNotQuerySpans spans1,
      MtasSpanNotQuerySpans spans2) {
<span class="fc" id="L56">    super();</span>
<span class="fc" id="L57">    docId = -1;</span>
<span class="fc" id="L58">    this.query = query;</span>
<span class="fc" id="L59">    this.spans1 = spans1;</span>
<span class="fc" id="L60">    this.spans2 = spans2;</span>
<span class="fc" id="L61">    this.lastSpans2EndPositions = new HashSet&lt;&gt;();</span>
<span class="fc" id="L62">    reset();</span>
<span class="fc" id="L63">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#nextStartPosition()
   */
  @Override
  public int nextStartPosition() throws IOException {
    // no document
<span class="pc bpc" id="L73" title="2 of 4 branches missed.">    if (docId == -1 || docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L74">      throw new IOException(&quot;no document&quot;);</span>
      // littleSpans already at start match, because of check for matching
      // document
<span class="fc bfc" id="L77" title="All 2 branches covered.">    } else if (!calledNextStartPosition) {</span>
<span class="fc" id="L78">      calledNextStartPosition = true;</span>
<span class="fc" id="L79">      return spans1.spans.startPosition();</span>
      // compute next match
    } else {
<span class="fc bfc" id="L82" title="All 2 branches covered.">      if (goToNextStartPosition()) {</span>
        // match found
<span class="fc" id="L84">        return spans1.spans.startPosition();</span>
      } else {
        // no more matches: document finished
<span class="fc" id="L87">        return NO_MORE_POSITIONS;</span>
      }
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#startPosition()
   */
  @Override
  public int startPosition() {
<span class="fc bfc" id="L99" title="All 2 branches covered.">    if (calledNextStartPosition) {</span>
<span class="fc" id="L100">      return spans1.spans.startPosition();</span>
    } else {
<span class="fc" id="L102">      return -1;</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#endPosition()
   */
  @Override
  public int endPosition() {
<span class="pc bpc" id="L113" title="1 of 2 branches missed.">    if (calledNextStartPosition) {</span>
<span class="fc" id="L114">      return spans1.spans.endPosition();</span>
    } else {
<span class="nc" id="L116">      return -1;</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#width()
   */
  @Override
  public int width() {
<span class="nc bnc" id="L127" title="All 2 branches missed.">    if (calledNextStartPosition) {</span>
<span class="nc" id="L128">      return spans1.spans.endPosition() - spans1.spans.startPosition();</span>
    } else {
<span class="nc" id="L130">      return 0;</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="L143">    spans1.spans.collect(collector);</span>
<span class="nc" id="L144">    spans2.spans.collect(collector);</span>
<span class="nc" id="L145">  }</span>

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

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

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#nextDoc()
   */
  @Override
  public int nextDoc() throws IOException {
<span class="fc" id="L174">    reset();</span>
<span class="pc bpc" id="L175" title="1 of 2 branches missed.">    while (!goToNextDoc())</span>
<span class="nc" id="L176">      ;</span>
<span class="fc" id="L177">    return docId;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#advance(int)
   */
  @Override
  public int advance(int target) throws IOException {
<span class="nc" id="L187">    reset();</span>
<span class="nc bnc" id="L188" title="All 2 branches missed.">    if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L189">      return docId;</span>
<span class="nc bnc" id="L190" title="All 2 branches missed.">    } else if (target &lt;= docId) {</span>
      // should not happen
<span class="nc" id="L192">      docId = NO_MORE_DOCS;</span>
<span class="nc" id="L193">      return docId;</span>
    } else {
<span class="nc" id="L195">      docId = spans1.spans.advance(target);</span>
<span class="nc bnc" id="L196" title="All 2 branches missed.">      if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L197">        return docId;</span>
      } else {
<span class="nc" id="L199">        int spans2DocId = spans2.spans.docID();</span>
<span class="nc bnc" id="L200" title="All 2 branches missed.">        if (spans2DocId &lt; docId) {</span>
<span class="nc" id="L201">          spans2DocId = spans2.spans.advance(docId);</span>
        }
<span class="nc bnc" id="L203" title="All 2 branches missed.">        if (docId != spans2DocId) {</span>
<span class="nc bnc" id="L204" title="All 2 branches missed.">          return spans1.spans.nextStartPosition() != NO_MORE_POSITIONS ? docId</span>
              : NO_MORE_DOCS;
<span class="nc bnc" id="L206" title="All 2 branches missed.">        } else if (goToNextStartPosition()) {</span>
<span class="nc" id="L207">          return docId;</span>
        } else {
<span class="nc" id="L209">          return nextDoc();</span>
        }
      }
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#asTwoPhaseIterator()
   */
  @Override
  public TwoPhaseIterator asTwoPhaseIterator() {
<span class="pc bpc" id="L222" title="2 of 6 branches missed.">    if (spans1 == null || spans2 == null || !query.twoPhaseIteratorAllowed()) {</span>
<span class="fc" id="L223">      return null;</span>
    } else {

<span class="fc" id="L226">      TwoPhaseIterator twoPhaseIterator1 = spans1.spans.asTwoPhaseIterator();</span>
<span class="fc bfc" id="L227" title="All 2 branches covered.">      if (twoPhaseIterator1 != null) {</span>
<span class="fc" id="L228">        return new TwoPhaseIterator(twoPhaseIterator1.approximation()) {</span>
          @Override
          public boolean matches() throws IOException {
<span class="pc bpc" id="L231" title="2 of 4 branches missed.">            return twoPhaseIterator1.matches() &amp;&amp; twoPhaseCurrentDocMatches();</span>
          }

          @Override
          public float matchCost() {
<span class="fc" id="L236">            return twoPhaseIterator1.matchCost();</span>
          }
        };
      } else {
<span class="fc" id="L240">        return new TwoPhaseIterator(spans1.spans) {</span>
          @Override
          public boolean matches() throws IOException {
<span class="fc" id="L243">            return twoPhaseCurrentDocMatches();</span>
          }

          @Override
          public float matchCost() {
<span class="fc" id="L248">            return spans1.spans.positionsCost();</span>
          }
        };
      }
    }
  }

  /**
   * Two phase current doc matches.
   *
   * @return true, if successful
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private boolean twoPhaseCurrentDocMatches() throws IOException {
<span class="pc bpc" id="L262" title="1 of 2 branches missed.">    if (docId != spans1.spans.docID()) {</span>
<span class="fc" id="L263">      reset();</span>
<span class="fc" id="L264">      docId = spans1.spans.docID();</span>
    }
<span class="pc bpc" id="L266" title="1 of 2 branches missed.">    if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L267">      return false;</span>
    } else {
<span class="fc" id="L269">      int spans2DocId = spans2.spans.docID();</span>
<span class="pc bpc" id="L270" title="1 of 2 branches missed.">      if (spans2DocId &lt; docId) {</span>
<span class="fc" id="L271">        spans2DocId = spans2.spans.advance(docId);</span>
      }
<span class="pc bpc" id="L273" title="1 of 2 branches missed.">      if (docId != spans2DocId) {</span>
<span class="nc bnc" id="L274" title="All 2 branches missed.">        return spans1.spans.nextStartPosition() != NO_MORE_POSITIONS;</span>
      } else {
<span class="fc" id="L276">        return goToNextStartPosition();</span>
      }
    }
  }

  /**
   * Go to next doc.
   *
   * @return true, if successful
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private boolean goToNextDoc() throws IOException {
<span class="pc bpc" id="L288" title="1 of 2 branches missed.">    if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L289">      return true;</span>
    } else {
<span class="fc" id="L291">      docId = spans1.spans.nextDoc();</span>
<span class="fc bfc" id="L292" title="All 2 branches covered.">      if (docId == NO_MORE_DOCS) {</span>
<span class="fc" id="L293">        return true;</span>
      } else {
<span class="fc" id="L295">        int spans2DocId = spans2.spans.docID();</span>
<span class="fc bfc" id="L296" title="All 2 branches covered.">        if (spans2DocId &lt; docId) {</span>
<span class="fc" id="L297">          spans2DocId = spans2.spans.advance(docId);</span>
        }
<span class="fc bfc" id="L299" title="All 2 branches covered.">        if (docId != spans2DocId) {</span>
<span class="pc bpc" id="L300" title="1 of 2 branches missed.">          return spans1.spans.nextStartPosition() != NO_MORE_POSITIONS;</span>
<span class="pc bpc" id="L301" title="1 of 2 branches missed.">        } else if (goToNextStartPosition()) {</span>
<span class="fc" id="L302">          return true;</span>
        } else {
<span class="nc" id="L304">          reset();</span>
<span class="nc" id="L305">          return false;</span>
        }
      }
    }
  }

  /**
   * Go to next start position.
   *
   * @return true, if successful
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private boolean goToNextStartPosition() throws IOException {
    int nextSpans1StartPosition;
    int nextSpans1EndPosition;
<span class="fc" id="L320">    while ((nextSpans1StartPosition = spans1.spans</span>
<span class="fc bfc" id="L321" title="All 2 branches covered.">        .nextStartPosition()) != NO_MORE_POSITIONS) {</span>
<span class="fc bfc" id="L322" title="All 2 branches covered.">      if (spans1.spans.docID() == spans2.spans.docID()) {</span>
        // clean up
<span class="fc bfc" id="L324" title="All 2 branches covered.">        if (nextSpans1StartPosition &gt; lastSpans2StartPosition) {</span>
<span class="fc" id="L325">          lastSpans2StartPosition = -1;</span>
        }
        // fast check
<span class="fc bfc" id="L328" title="All 4 branches covered.">        if (lastSpans2StartPosition == -1</span>
            &amp;&amp; nextSpans1StartPosition &lt; nextSpans2StartPosition) {
<span class="fc" id="L330">          return true;</span>
        }
<span class="fc" id="L332">        nextSpans1EndPosition = spans1.spans.endPosition();</span>
<span class="fc bfc" id="L333" title="All 2 branches covered.">        if (nextSpans1StartPosition == lastSpans2StartPosition) {</span>
          // try to collect all lastSpans2Endpositions, and return true if not
          // contained
<span class="fc bfc" id="L336" title="All 2 branches covered.">          if (collectAndCheckLastSpans(nextSpans1StartPosition,</span>
              nextSpans1EndPosition)) {
<span class="fc" id="L338">            return true;</span>
          } else {
            // continue
          }
        } else {
          // reset, assume lastSpans2StartPosition&lt;nextSpans1StartPosition
<span class="fc" id="L344">          lastSpans2StartPosition = -1;</span>
          // go to correct next
<span class="fc bfc" id="L346" title="All 2 branches covered.">          while (nextSpans2StartPosition &lt; nextSpans1StartPosition) {</span>
<span class="fc" id="L347">            nextSpans2StartPosition = spans2.spans.nextStartPosition();</span>
          }
<span class="fc" id="L349">          nextSpans2EndPosition = spans2.spans.endPosition();</span>
<span class="fc bfc" id="L350" title="All 2 branches covered.">          if (nextSpans1StartPosition == nextSpans2StartPosition) {</span>
            // try to collect all lastSpans2Endpositions, and return true if not
            // contained
<span class="fc bfc" id="L353" title="All 2 branches covered.">            if (collectAndCheckLastSpans(nextSpans1StartPosition,</span>
                nextSpans1EndPosition)) {
<span class="fc" id="L355">              return true;</span>
            } else {
              // continue
            }
          } else {
<span class="fc" id="L360">            return true;</span>
          }
        }
      } else {
<span class="fc" id="L364">        return true;</span>
      }
    }
    // no more positions
<span class="fc" id="L368">    return false;</span>
  }

  /**
   * Collect and check last spans.
   *
   * @param nextSpans1StartPosition the next spans 1 start position
   * @param nextSpans1EndPosition the next spans 1 end position
   * @return true, if successful
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private boolean collectAndCheckLastSpans(int nextSpans1StartPosition,
      int nextSpans1EndPosition) throws IOException {
    // check next
<span class="fc bfc" id="L382" title="All 4 branches covered.">    if (nextSpans1StartPosition == nextSpans2StartPosition</span>
        &amp;&amp; nextSpans1EndPosition == nextSpans2EndPosition) {
<span class="fc" id="L384">      return false;</span>
    }
    // check last
<span class="fc bfc" id="L387" title="All 4 branches covered.">    if (nextSpans1StartPosition == lastSpans2StartPosition</span>
        &amp;&amp; (nextSpans1EndPosition == lastSpans2EndPosition
<span class="fc bfc" id="L389" title="All 2 branches covered.">            || lastSpans2EndPositions.contains(nextSpans1EndPosition))) {</span>
<span class="fc" id="L390">      return false;</span>
    }
    // collect
<span class="fc bfc" id="L393" title="All 2 branches covered.">    if (nextSpans1StartPosition == nextSpans2StartPosition) {</span>
      // reset
<span class="fc bfc" id="L395" title="All 2 branches covered.">      if (nextSpans2StartPosition != lastSpans2StartPosition) {</span>
<span class="fc" id="L396">        lastSpans2StartPosition = nextSpans2StartPosition;</span>
<span class="fc" id="L397">        lastSpans2EndPosition = -1;</span>
<span class="fc" id="L398">        lastSpans2EndPositions.clear();</span>
      }
<span class="fc bfc" id="L400" title="All 2 branches covered.">      while (nextSpans1StartPosition == nextSpans2StartPosition) {</span>
<span class="fc bfc" id="L401" title="All 2 branches covered.">        if (lastSpans2EndPosition &gt; -1) {</span>
<span class="fc" id="L402">          lastSpans2EndPositions.add(lastSpans2EndPosition);</span>
        }
<span class="fc" id="L404">        lastSpans2EndPosition = nextSpans2EndPosition;</span>
<span class="fc" id="L405">        nextSpans2StartPosition = spans2.spans.nextStartPosition();</span>
<span class="fc" id="L406">        nextSpans2EndPosition = spans2.spans.endPosition();</span>
<span class="fc bfc" id="L407" title="All 4 branches covered.">        if (nextSpans1StartPosition == nextSpans2StartPosition</span>
            &amp;&amp; nextSpans1EndPosition == nextSpans2EndPosition) {
<span class="fc" id="L409">          return false;</span>
        }
      }
    }

<span class="fc" id="L414">    return true;</span>
  }

  /**
   * Reset.
   */
  private void reset() {
<span class="fc" id="L421">    calledNextStartPosition = false;</span>
<span class="fc" id="L422">    lastSpans2StartPosition = -1;</span>
<span class="fc" id="L423">    lastSpans2EndPosition = -1;</span>
<span class="fc" id="L424">    lastSpans2EndPositions.clear();</span>
<span class="fc" id="L425">    nextSpans2StartPosition = -1;</span>
<span class="fc" id="L426">    nextSpans2EndPosition = -1;</span>
<span class="fc" id="L427">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#cost()
   */
  @Override
  public long cost() {
<span class="fc" id="L436">    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>