MtasSpanPrecededBySpans.java.html
12.2 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
<?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="../.resources/report.css" type="text/css"/><link rel="shortcut icon" href="../.resources/report.gif" type="image/gif"/><title>MtasSpanPrecededBySpans.java</title><link rel="stylesheet" href="../.resources/prettify.css" type="text/css"/><script type="text/javascript" src="../.resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../.sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> > <a href="index.source.html" class="el_package">mtas.search.spans</a> > <span class="el_source">MtasSpanPrecededBySpans.java</span></div><h1>MtasSpanPrecededBySpans.java</h1><pre class="source lang-java linenums">package mtas.search.spans;
import java.io.IOException;
import java.util.HashSet;
import org.apache.lucene.search.spans.SpanCollector;
import org.apache.lucene.search.spans.Spans;
import mtas.search.spans.MtasSpanPrecededByQuery.MtasSpanPrecededByQuerySpans;
import mtas.search.spans.util.MtasSpans;
public class MtasSpanPrecededBySpans extends Spans implements MtasSpans {
/** The spans2. */
private MtasSpanPrecededByQuerySpans spans1;
private MtasSpanPrecededByQuerySpans spans2;
private int lastSpans2StartPosition;
private int lastSpans2EndPosition;
private int maximumSpans2EndPosition;
private HashSet<Integer> previousSpans2EndPositions;
/** The no more positions. */
private boolean calledNextStartPosition;
private boolean noMorePositions;
/** The doc id. */
private int docId;
public MtasSpanPrecededBySpans(
MtasSpanPrecededByQuery mtasSpanPrecededByQuery,
MtasSpanPrecededByQuerySpans spans1,
MtasSpanPrecededByQuerySpans spans2) {
<span class="fc" id="L34"> super();</span>
<span class="fc" id="L35"> docId = -1;</span>
<span class="fc" id="L36"> this.spans1 = spans1;</span>
<span class="fc" id="L37"> this.spans2 = spans2;</span>
<span class="fc" id="L38"> previousSpans2EndPositions = new HashSet<>();</span>
<span class="fc" id="L39"> }</span>
@Override
public void collect(SpanCollector collector) throws IOException {
<span class="nc" id="L43"> spans1.spans.collect(collector);</span>
<span class="nc" id="L44"> spans2.spans.collect(collector);</span>
<span class="nc" id="L45"> }</span>
@Override
public int endPosition() {
<span class="pc bpc" id="L49" title="2 of 4 branches missed."> return calledNextStartPosition</span>
<span class="pc" id="L50"> ? (noMorePositions ? NO_MORE_POSITIONS : spans1.spans.endPosition())</span>
: -1;
}
@Override
public int nextStartPosition() throws IOException {
// no document
<span class="pc bpc" id="L57" title="2 of 4 branches missed."> if (docId == -1 || docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L58"> throw new IOException("no document");</span>
// finished
<span class="pc bpc" id="L60" title="1 of 2 branches missed."> } else if (noMorePositions) {</span>
<span class="nc" id="L61"> return NO_MORE_POSITIONS;</span>
// littleSpans already at start match, because of check for matching
// document
<span class="fc bfc" id="L64" title="All 2 branches covered."> } else if (!calledNextStartPosition) {</span>
<span class="fc" id="L65"> calledNextStartPosition = true;</span>
<span class="fc" id="L66"> return spans1.spans.startPosition();</span>
// compute next match
} else {
<span class="fc bfc" id="L69" title="All 2 branches covered."> if (goToNextStartPosition()) {</span>
// match found
<span class="fc" id="L71"> return spans1.spans.startPosition();</span>
} else {
// no more matches: document finished
<span class="fc" id="L74"> return NO_MORE_POSITIONS;</span>
}
}
}
@Override
public float positionsCost() {
<span class="nc" id="L81"> return 0;</span>
}
@Override
public int startPosition() {
<span class="pc bpc" id="L86" title="2 of 4 branches missed."> return calledNextStartPosition</span>
<span class="pc" id="L87"> ? (noMorePositions ? NO_MORE_POSITIONS : spans1.spans.startPosition())</span>
: -1;
}
@Override
public int width() {
<span class="nc bnc" id="L93" title="All 4 branches missed."> return calledNextStartPosition ? (noMorePositions ? 0</span>
<span class="nc" id="L94"> : spans1.spans.endPosition() - spans1.spans.startPosition()) : 0;</span>
}
@Override
public int advance(int target) throws IOException {
<span class="nc" id="L99"> reset();</span>
<span class="nc bnc" id="L100" title="All 2 branches missed."> if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L101"> return docId;</span>
<span class="nc bnc" id="L102" title="All 2 branches missed."> } else if (target < docId) {</span>
// should not happen
<span class="nc" id="L104"> docId = NO_MORE_DOCS;</span>
<span class="nc" id="L105"> return docId;</span>
} else {
// advance 1
<span class="nc" id="L108"> int spans1DocId = spans1.spans.docID();</span>
<span class="nc bnc" id="L109" title="All 2 branches missed."> if (spans1DocId < target) {</span>
<span class="nc" id="L110"> spans1DocId = spans1.spans.advance(target);</span>
<span class="nc bnc" id="L111" title="All 2 branches missed."> if (spans1DocId == NO_MORE_DOCS) {</span>
<span class="nc" id="L112"> docId = NO_MORE_DOCS;</span>
<span class="nc" id="L113"> return docId;</span>
}
<span class="nc" id="L115"> target = Math.max(target, spans1DocId);</span>
}
<span class="nc" id="L117"> int spans2DocId = spans2.spans.docID();</span>
// advance 2
<span class="nc bnc" id="L119" title="All 2 branches missed."> if (spans2DocId < target) {</span>
<span class="nc" id="L120"> spans2DocId = spans2.spans.advance(target);</span>
<span class="nc bnc" id="L121" title="All 2 branches missed."> if (spans2DocId == NO_MORE_DOCS) {</span>
<span class="nc" id="L122"> docId = NO_MORE_DOCS;</span>
<span class="nc" id="L123"> return docId;</span>
}
}
// check equal docId, otherwise next
<span class="nc bnc" id="L127" title="All 2 branches missed."> if (spans1DocId == spans2DocId) {</span>
<span class="nc" id="L128"> docId = spans1DocId;</span>
// check match
<span class="nc bnc" id="L130" title="All 2 branches missed."> if (goToNextStartPosition()) {</span>
<span class="nc" id="L131"> return docId;</span>
} else {
<span class="nc" id="L133"> return nextDoc();</span>
}
} else {
<span class="nc" id="L136"> return nextDoc();</span>
}
}
}
@Override
public long cost() {
<span class="nc" id="L143"> return 0;</span>
}
@Override
public int docID() {
<span class="fc" id="L148"> return docId;</span>
}
@Override
public int nextDoc() throws IOException {
<span class="fc" id="L153"> reset();</span>
<span class="pc bpc" id="L154" title="1 of 2 branches missed."> while (!goToNextDoc())</span>
<span class="nc" id="L155"> ;</span>
<span class="fc" id="L156"> return docId;</span>
}
private boolean goToNextDoc() throws IOException {
<span class="pc bpc" id="L160" title="1 of 2 branches missed."> if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L161"> return true;</span>
} else {
<span class="fc" id="L163"> int spans1DocId = spans1.spans.nextDoc();</span>
<span class="fc" id="L164"> int spans2DocId = spans2.spans.docID();</span>
<span class="fc" id="L165"> docId = Math.max(spans1DocId, spans2DocId);</span>
<span class="fc bfc" id="L166" title="All 4 branches covered."> while (spans1DocId != spans2DocId && docId != NO_MORE_DOCS) {</span>
<span class="pc bpc" id="L167" title="1 of 2 branches missed."> if (spans1DocId < spans2DocId) {</span>
<span class="nc" id="L168"> spans1DocId = spans1.spans.advance(spans2DocId);</span>
<span class="nc" id="L169"> docId = spans1DocId;</span>
} else {
<span class="fc" id="L171"> spans2DocId = spans2.spans.advance(spans1DocId);</span>
<span class="fc" id="L172"> docId = spans2DocId;</span>
}
}
<span class="fc bfc" id="L175" title="All 2 branches covered."> if (docId != NO_MORE_DOCS) {</span>
<span class="pc bpc" id="L176" title="1 of 2 branches missed."> if (!goToNextStartPosition()) {</span>
<span class="nc" id="L177"> reset();</span>
<span class="nc" id="L178"> return false;</span>
}
}
<span class="fc" id="L181"> return true;</span>
}
}
private boolean goToNextStartPosition() throws IOException {
int nextSpans1StartPosition;
<span class="fc" id="L187"> while ((nextSpans1StartPosition = spans1.spans</span>
<span class="fc bfc" id="L188" title="All 2 branches covered."> .nextStartPosition()) != NO_MORE_POSITIONS) {</span>
<span class="fc bfc" id="L189" title="All 2 branches covered."> if (nextSpans1StartPosition == lastSpans2EndPosition) {</span>
<span class="fc" id="L190"> return true;</span>
} else {
// clean up
<span class="fc bfc" id="L193" title="All 2 branches covered."> if (maximumSpans2EndPosition < nextSpans1StartPosition) {</span>
<span class="fc" id="L194"> previousSpans2EndPositions.clear();</span>
<span class="fc" id="L195"> maximumSpans2EndPosition = -1;</span>
<span class="fc" id="L196"> } else if (previousSpans2EndPositions</span>
<span class="pc bpc" id="L197" title="1 of 2 branches missed."> .contains(nextSpans1StartPosition)) {</span>
<span class="nc" id="L198"> return true;</span>
}
// try to find match
<span class="fc bfc" id="L201" title="All 2 branches covered."> while (lastSpans2StartPosition < nextSpans1StartPosition) {</span>
<span class="pc bpc" id="L202" title="1 of 2 branches missed."> if (lastSpans2StartPosition != NO_MORE_POSITIONS) {</span>
<span class="fc" id="L203"> lastSpans2StartPosition = spans2.spans.nextStartPosition();</span>
}
<span class="fc bfc" id="L205" title="All 2 branches covered."> if (lastSpans2StartPosition == NO_MORE_POSITIONS) {</span>
<span class="pc bpc" id="L206" title="1 of 2 branches missed."> if (previousSpans2EndPositions.isEmpty()) {</span>
<span class="fc" id="L207"> noMorePositions = true;</span>
<span class="fc" id="L208"> return false;</span>
}
} else {
<span class="fc" id="L211"> lastSpans2EndPosition = spans2.spans.endPosition();</span>
<span class="fc bfc" id="L212" title="All 2 branches covered."> if (lastSpans2EndPosition >= nextSpans1StartPosition) {</span>
<span class="fc" id="L213"> previousSpans2EndPositions.add(lastSpans2EndPosition);</span>
<span class="fc" id="L214"> maximumSpans2EndPosition = Math.max(maximumSpans2EndPosition,</span>
lastSpans2EndPosition);
}
<span class="fc bfc" id="L217" title="All 2 branches covered."> if (nextSpans1StartPosition == lastSpans2EndPosition) {</span>
<span class="fc" id="L218"> return true;</span>
}
}
}
}
}
<span class="fc" id="L224"> return false;</span>
}
private void reset() {
<span class="fc" id="L228"> calledNextStartPosition = false;</span>
<span class="fc" id="L229"> noMorePositions = false;</span>
<span class="fc" id="L230"> lastSpans2StartPosition = -1;</span>
<span class="fc" id="L231"> lastSpans2EndPosition = -1;</span>
<span class="fc" id="L232"> maximumSpans2EndPosition = -1;</span>
<span class="fc" id="L233"> previousSpans2EndPositions.clear();</span>
<span class="fc" id="L234"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.5.201505241946</span></div></body></html>