MtasIgnoreItem.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
<?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>MtasIgnoreItem.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> > <a href="index.source.html" class="el_package">mtas.search.spans.util</a> > <span class="el_source">MtasIgnoreItem.java</span></div><h1>MtasIgnoreItem.java</h1><pre class="source lang-java linenums">package mtas.search.spans.util;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.search.spans.Spans;
/**
* The Class MtasIgnoreItem.
*/
public class MtasIgnoreItem {
/** The Constant log. */
<span class="fc" id="L18"> private static final Log log = LogFactory.getLog(MtasIgnoreItem.class);</span>
/** The Constant DEFAULT_MAXIMUM_IGNORE_LENGTH. */
public static final int DEFAULT_MAXIMUM_IGNORE_LENGTH = 10;
/** The ignore spans. */
Spans ignoreSpans;
/** The current doc id. */
int currentDocId;
/** The current position. */
int currentPosition;
/** The minimum position. */
int minimumPosition;
/** The maximum ignore length. */
int maximumIgnoreLength;
/** The base start position list. */
HashMap<Integer, HashSet<Integer>> baseStartPositionList;
/** The base end position list. */
HashMap<Integer, HashSet<Integer>> baseEndPositionList;
/** The full end position list. */
HashMap<Integer, HashSet<Integer>> fullEndPositionList;
/** The min base start position. */
HashMap<Integer, Integer> minBaseStartPosition;
/** The max base end position. */
HashMap<Integer, Integer> maxBaseEndPosition;
/** The min full start position. */
HashMap<Integer, Integer> minFullStartPosition;
/** The max full end position. */
HashMap<Integer, Integer> maxFullEndPosition;
/**
* Instantiates a new mtas ignore item.
*
* @param ignoreSpans the ignore spans
* @param maximumIgnoreLength the maximum ignore length
*/
<span class="fc" id="L65"> public MtasIgnoreItem(Spans ignoreSpans, Integer maximumIgnoreLength) {</span>
<span class="fc" id="L66"> this.ignoreSpans = ignoreSpans;</span>
<span class="fc" id="L67"> currentDocId = -1;</span>
<span class="fc" id="L68"> currentPosition = -1;</span>
<span class="fc" id="L69"> minimumPosition = -1;</span>
<span class="fc" id="L70"> baseStartPositionList = new HashMap<>();</span>
<span class="fc" id="L71"> baseEndPositionList = new HashMap<>();</span>
<span class="fc" id="L72"> fullEndPositionList = new HashMap<>();</span>
<span class="fc" id="L73"> minBaseStartPosition = new HashMap<>();</span>
<span class="fc" id="L74"> maxBaseEndPosition = new HashMap<>();</span>
<span class="fc" id="L75"> minFullStartPosition = new HashMap<>();</span>
<span class="fc" id="L76"> maxFullEndPosition = new HashMap<>();</span>
<span class="fc bfc" id="L77" title="All 2 branches covered."> if (maximumIgnoreLength == null) {</span>
<span class="fc" id="L78"> this.maximumIgnoreLength = DEFAULT_MAXIMUM_IGNORE_LENGTH;</span>
} else {
<span class="fc" id="L80"> this.maximumIgnoreLength = maximumIgnoreLength;</span>
}
<span class="fc" id="L82"> }</span>
/**
* Advance to doc.
*
* @param docId the doc id
* @return true, if successful
* @throws IOException Signals that an I/O exception has occurred.
*/
public boolean advanceToDoc(int docId) throws IOException {
<span class="pc bpc" id="L92" title="1 of 4 branches missed."> if (ignoreSpans == null || currentDocId == Spans.NO_MORE_DOCS) {</span>
<span class="fc" id="L93"> return false;</span>
<span class="pc bpc" id="L94" title="1 of 2 branches missed."> } else if (currentDocId == docId) {</span>
<span class="nc" id="L95"> return true;</span>
} else {
<span class="fc" id="L97"> baseEndPositionList.clear();</span>
<span class="fc" id="L98"> fullEndPositionList.clear();</span>
<span class="fc" id="L99"> maxBaseEndPosition.clear();</span>
<span class="fc" id="L100"> minFullStartPosition.clear();</span>
<span class="pc bpc" id="L101" title="1 of 2 branches missed."> if (currentDocId < docId) {</span>
<span class="fc" id="L102"> currentDocId = ignoreSpans.advance(docId);</span>
<span class="fc" id="L103"> currentPosition = -1;</span>
<span class="fc" id="L104"> minimumPosition = -1;</span>
}
<span class="pc bpc" id="L106" title="1 of 2 branches missed."> return currentDocId == docId;</span>
}
}
/**
* Gets the min start position.
*
* @param docId the doc id
* @param position the position
* @return the min start position
* @throws IOException Signals that an I/O exception has occurred.
*/
public int getMinStartPosition(int docId, int position) throws IOException {
<span class="pc bpc" id="L119" title="1 of 4 branches missed."> if (ignoreSpans != null && docId == currentDocId) {</span>
<span class="pc bpc" id="L120" title="1 of 2 branches missed."> if (position < minimumPosition) {</span>
<span class="nc" id="L121"> throw new IOException(</span>
"Unexpected position, should be >= " + minimumPosition + "!");
} else {
<span class="fc" id="L124"> computeFullStartPositionMinimum(position);</span>
<span class="pc bpc" id="L125" title="1 of 2 branches missed."> if (minFullStartPosition.containsKey(position)) {</span>
<span class="fc" id="L126"> return minFullStartPosition.get(position);</span>
} else {
<span class="nc" id="L128"> return 0;</span>
}
}
} else {
<span class="fc" id="L132"> return 0;</span>
}
}
/**
* Gets the max end position.
*
* @param docId the doc id
* @param position the position
* @return the max end position
* @throws IOException Signals that an I/O exception has occurred.
*/
public int getMaxEndPosition(int docId, int position) throws IOException {
<span class="nc bnc" id="L145" title="All 4 branches missed."> if (ignoreSpans != null && docId == currentDocId) {</span>
<span class="nc bnc" id="L146" title="All 2 branches missed."> if (position < minimumPosition) {</span>
<span class="nc" id="L147"> throw new IOException(</span>
"Unexpected position, should be >= " + minimumPosition + "!");
}
<span class="nc" id="L150"> computeFullEndPositionList(position);</span>
<span class="nc bnc" id="L151" title="All 2 branches missed."> if (maxFullEndPosition.containsKey(position)) {</span>
<span class="nc" id="L152"> return maxFullEndPosition.get(position);</span>
} else {
<span class="nc" id="L154"> return 0;</span>
}
} else {
<span class="nc" id="L157"> return 0;</span>
}
}
/**
* Gets the full end position list.
*
* @param docId the doc id
* @param position the position
* @return the full end position list
* @throws IOException Signals that an I/O exception has occurred.
*/
public Set<Integer> getFullEndPositionList(int docId, int position)
throws IOException {
<span class="pc bpc" id="L171" title="1 of 4 branches missed."> if (ignoreSpans != null && docId == currentDocId) {</span>
<span class="pc bpc" id="L172" title="1 of 2 branches missed."> if (position < minimumPosition) {</span>
<span class="nc" id="L173"> throw new IOException(</span>
"Unexpected startPosition, should be >= " + minimumPosition + "!");
} else {
<span class="fc" id="L176"> computeFullEndPositionList(position);</span>
<span class="fc" id="L177"> return fullEndPositionList.get(position);</span>
}
} else {
<span class="fc" id="L180"> return null;</span>
}
}
/**
* Compute full start position minimum.
*
* @param position the position
* @throws IOException Signals that an I/O exception has occurred.
*/
private void computeFullStartPositionMinimum(int position)
throws IOException {
<span class="pc bpc" id="L192" title="2 of 4 branches missed."> if (ignoreSpans != null && !minFullStartPosition.containsKey(position)) {</span>
<span class="fc" id="L193"> HashSet<Integer> list = baseStartPositionList.get(position);</span>
<span class="fc" id="L194"> HashSet<Integer> newList = new HashSet<>();</span>
<span class="fc" id="L195"> int minimumStartPosition = position;</span>
<span class="pc bpc" id="L196" title="3 of 4 branches missed."> while (list != null && !list.isEmpty()) {</span>
<span class="nc" id="L197"> newList.clear();</span>
<span class="nc bnc" id="L198" title="All 2 branches missed."> for (int startPosition : list) {</span>
<span class="nc bnc" id="L199" title="All 2 branches missed."> if (minFullStartPosition.containsKey(startPosition)) {</span>
<span class="nc" id="L200"> minimumStartPosition = Math.min(minimumStartPosition,</span>
<span class="nc" id="L201"> minFullStartPosition.get(startPosition));</span>
<span class="nc bnc" id="L202" title="All 2 branches missed."> } else if (baseStartPositionList.containsKey(startPosition)) {</span>
<span class="nc" id="L203"> newList.addAll(baseStartPositionList.get(startPosition));</span>
} else {
<span class="nc bnc" id="L205" title="All 2 branches missed."> if (startPosition < minimumStartPosition) {</span>
<span class="nc" id="L206"> minimumStartPosition = startPosition;</span>
}
}
<span class="nc" id="L209"> }</span>
<span class="nc" id="L210"> list.clear();</span>
<span class="nc" id="L211"> list.addAll(newList);</span>
}
<span class="fc" id="L213"> minFullStartPosition.put(position, minimumStartPosition);</span>
}
<span class="fc" id="L215"> }</span>
/**
* Compute full end position list.
*
* @param position the position
* @throws IOException Signals that an I/O exception has occurred.
*/
private void computeFullEndPositionList(int position) throws IOException {
<span class="pc bpc" id="L224" title="2 of 4 branches missed."> if (ignoreSpans != null && !fullEndPositionList.containsKey(position)) {</span>
// initial fill
<span class="fc" id="L226"> moveTo(position);</span>
<span class="fc" id="L227"> HashSet<Integer> list = baseEndPositionList.get(position);</span>
<span class="pc bpc" id="L228" title="1 of 4 branches missed."> if (list != null && !list.isEmpty()) {</span>
<span class="fc" id="L229"> int maxEndPosition = maxBaseEndPosition.get(position);</span>
<span class="fc" id="L230"> HashSet<Integer> checkList = new HashSet<>();</span>
<span class="fc" id="L231"> HashSet<Integer> subCheckList = new HashSet<>();</span>
<span class="fc" id="L232"> checkList.addAll(list);</span>
<span class="fc" id="L233"> int depth = 1;</span>
<span class="fc bfc" id="L234" title="All 2 branches covered."> while (!checkList.isEmpty()) {</span>
<span class="pc bpc" id="L235" title="1 of 2 branches missed."> if (depth > maximumIgnoreLength) {</span>
<span class="nc" id="L236"> checkList.clear();</span>
<span class="nc" id="L237"> subCheckList.clear();</span>
<span class="nc" id="L238"> throw new IOException("too many successive ignores, maximum is "</span>
+ maximumIgnoreLength);
} else {
<span class="fc bfc" id="L241" title="All 2 branches covered."> for (Integer checkItem : checkList) {</span>
<span class="pc bpc" id="L242" title="1 of 2 branches missed."> if (fullEndPositionList.get(checkItem) != null) {</span>
<span class="nc" id="L243"> list.addAll(fullEndPositionList.get(checkItem));</span>
<span class="nc" id="L244"> maxEndPosition = Math.max(maxEndPosition,</span>
<span class="nc" id="L245"> maxFullEndPosition.get(checkItem));</span>
} else {
<span class="fc" id="L247"> moveTo(checkItem);</span>
<span class="fc bfc" id="L248" title="All 2 branches covered."> if (baseEndPositionList.containsKey(checkItem)) {</span>
<span class="fc" id="L249"> list.addAll(baseEndPositionList.get(checkItem));</span>
<span class="fc" id="L250"> maxEndPosition = Math.max(maxEndPosition,</span>
<span class="fc" id="L251"> maxBaseEndPosition.get(checkItem));</span>
<span class="fc" id="L252"> subCheckList.addAll(baseEndPositionList.get(checkItem));</span>
} else {
// ready for checkItem
}
}
<span class="fc" id="L257"> }</span>
<span class="fc" id="L258"> checkList.clear();</span>
<span class="fc" id="L259"> checkList.addAll(subCheckList);</span>
<span class="fc" id="L260"> subCheckList.clear();</span>
<span class="fc" id="L261"> depth++;</span>
}
}
<span class="fc" id="L264"> fullEndPositionList.put(position, list);</span>
<span class="fc" id="L265"> maxFullEndPosition.put(position, (maxEndPosition - position));</span>
<span class="fc" id="L266"> } else {</span>
<span class="fc" id="L267"> fullEndPositionList.put(position, null);</span>
<span class="fc" id="L268"> maxFullEndPosition.put(position, 0);</span>
}
}
<span class="fc" id="L271"> }</span>
/**
* Move to.
*
* @param position the position
*/
private void moveTo(int position) {
<span class="fc bfc" id="L279" title="All 2 branches covered."> while (position >= currentPosition) {</span>
try {
<span class="fc" id="L281"> currentPosition = ignoreSpans.nextStartPosition();</span>
<span class="fc bfc" id="L282" title="All 4 branches covered."> if (currentPosition != Spans.NO_MORE_POSITIONS</span>
&& currentPosition >= minimumPosition) {
<span class="pc bpc" id="L284" title="1 of 2 branches missed."> if (!baseEndPositionList.containsKey(currentPosition)) {</span>
<span class="fc" id="L285"> baseEndPositionList.put(currentPosition, new HashSet<Integer>());</span>
<span class="fc" id="L286"> maxBaseEndPosition.put(currentPosition, currentPosition);</span>
} else {
<span class="nc" id="L288"> maxBaseEndPosition.put(currentPosition,</span>
<span class="nc" id="L289"> Math.max(maxBaseEndPosition.get(currentPosition),</span>
<span class="nc" id="L290"> ignoreSpans.endPosition()));</span>
}
<span class="fc bfc" id="L292" title="All 2 branches covered."> if (!baseStartPositionList.containsKey(ignoreSpans.endPosition())) {</span>
<span class="fc" id="L293"> baseStartPositionList.put(ignoreSpans.endPosition(),</span>
new HashSet<Integer>());
<span class="fc" id="L295"> minBaseStartPosition.put(ignoreSpans.endPosition(),</span>
<span class="fc" id="L296"> ignoreSpans.endPosition());</span>
} else {
<span class="fc" id="L298"> minBaseStartPosition.put(ignoreSpans.endPosition(),</span>
<span class="fc" id="L299"> Math.min(minBaseStartPosition.get(ignoreSpans.endPosition()),</span>
currentPosition));
}
<span class="fc" id="L302"> baseStartPositionList.get(ignoreSpans.endPosition())</span>
<span class="fc" id="L303"> .add(currentPosition);</span>
<span class="fc" id="L304"> baseEndPositionList.get(currentPosition)</span>
<span class="fc" id="L305"> .add(ignoreSpans.endPosition());</span>
}
<span class="nc" id="L307"> } catch (IOException e) {</span>
<span class="nc" id="L308"> log.debug(e);</span>
<span class="nc" id="L309"> currentPosition = Spans.NO_MORE_POSITIONS;</span>
<span class="nc" id="L310"> break;</span>
<span class="fc" id="L311"> }</span>
}
<span class="fc" id="L313"> }</span>
/**
* Removes the before.
*
* @param docId the doc id
* @param position the position
*/
public void removeBefore(int docId, int position) {
<span class="pc bpc" id="L322" title="1 of 4 branches missed."> if (ignoreSpans != null && docId == currentDocId) {</span>
<span class="fc" id="L323"> baseStartPositionList.entrySet()</span>
<span class="fc bfc" id="L324" title="All 2 branches covered."> .removeIf(entry -> entry.getKey() < position);</span>
<span class="fc" id="L325"> baseEndPositionList.entrySet()</span>
<span class="fc bfc" id="L326" title="All 2 branches covered."> .removeIf(entry -> entry.getKey() < position);</span>
<span class="fc" id="L327"> fullEndPositionList.entrySet()</span>
<span class="fc bfc" id="L328" title="All 2 branches covered."> .removeIf(entry -> entry.getKey() < position);</span>
<span class="fc" id="L329"> minBaseStartPosition.entrySet()</span>
<span class="fc bfc" id="L330" title="All 2 branches covered."> .removeIf(entry -> entry.getKey() < position);</span>
<span class="fc" id="L331"> maxBaseEndPosition.entrySet()</span>
<span class="fc bfc" id="L332" title="All 2 branches covered."> .removeIf(entry -> entry.getKey() < position);</span>
<span class="fc" id="L333"> minFullStartPosition.entrySet()</span>
<span class="fc bfc" id="L334" title="All 2 branches covered."> .removeIf(entry -> entry.getKey() < position);</span>
<span class="fc" id="L335"> maxFullEndPosition.entrySet()</span>
<span class="fc bfc" id="L336" title="All 2 branches covered."> .removeIf(entry -> entry.getKey() < position);</span>
<span class="fc bfc" id="L337" title="All 2 branches covered."> if (minimumPosition < position) {</span>
<span class="fc" id="L338"> minimumPosition = position;</span>
}
<span class="fc bfc" id="L340" title="All 2 branches covered."> if (currentPosition < position) {</span>
<span class="fc" id="L341"> currentPosition = position;</span>
}
}
<span class="fc" id="L344"> }</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>