MtasExtendedTermSpans.java.html
6.87 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
<?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>MtasExtendedTermSpans.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">MtasExtendedTermSpans.java</span></div><h1>MtasExtendedTermSpans.java</h1><pre class="source lang-java linenums">package mtas.search.spans.util;
import java.io.IOException;
import java.util.Collection;
import mtas.analysis.token.MtasPosition;
import mtas.codec.payload.MtasPayloadDecoder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Term;
import org.apache.lucene.payloads.PayloadSpanCollector;
import org.apache.lucene.search.spans.TermSpans;
/**
* The Class MtasExtendedTermSpans.
*/
public class MtasExtendedTermSpans extends TermSpans {
/** The Constant log. */
<span class="fc" id="L22"> private static final Log log = LogFactory.getLog(MtasExtendedTermSpans.class);</span>
/** The mtas position. */
<span class="fc" id="L25"> protected MtasPosition mtasPosition = null;</span>
/** The assume single position. */
private boolean assumeSinglePosition;
/** The payload span collector. */
private PayloadSpanCollector payloadSpanCollector;
/**
* Instantiates a new mtas extended term spans.
*
* @param postings the postings
* @param term the term
*/
public MtasExtendedTermSpans(PostingsEnum postings, Term term) {
<span class="nc" id="L40"> this(postings, term, false);</span>
<span class="nc" id="L41"> }</span>
/**
* Instantiates a new mtas extended term spans.
*
* @param postings the postings
* @param term the term
* @param assumeSinglePosition the assume single position
*/
public MtasExtendedTermSpans(PostingsEnum postings, Term term,
boolean assumeSinglePosition) {
<span class="fc" id="L52"> super(null, postings, term, 1);</span>
<span class="fc" id="L53"> payloadSpanCollector = new PayloadSpanCollector();</span>
<span class="fc" id="L54"> this.assumeSinglePosition = assumeSinglePosition;</span>
<span class="fc" id="L55"> }</span>
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.TermSpans#endPosition()
*/
@Override
public int endPosition() {
<span class="fc bfc" id="L64" title="All 2 branches covered."> if (assumeSinglePosition) {</span>
<span class="fc" id="L65"> return super.endPosition();</span>
} else {
<span class="fc" id="L67"> int status = super.endPosition();</span>
<span class="fc bfc" id="L68" title="All 2 branches covered."> if (status >= 0) {</span>
try {
<span class="fc" id="L70"> processEncodedPayload();</span>
<span class="fc bfc" id="L71" title="All 2 branches covered."> return (status != NO_MORE_POSITIONS) ? (mtasPosition.getEnd() + 1)</span>
: NO_MORE_POSITIONS;
<span class="nc" id="L73"> } catch (IOException e) {</span>
<span class="nc" id="L74"> log.debug(e);</span>
<span class="nc" id="L75"> return NO_MORE_POSITIONS;</span>
}
} else {
<span class="fc" id="L78"> return status;</span>
}
}
}
/**
* Gets the positions.
*
* @return the positions
*/
public int[] getPositions() {
int[] list;
<span class="nc bnc" id="L90" title="All 2 branches missed."> if (assumeSinglePosition) {</span>
<span class="nc" id="L91"> list = new int[1];</span>
<span class="nc" id="L92"> list[0] = super.startPosition();</span>
<span class="nc" id="L93"> return list;</span>
} else {
try {
<span class="nc" id="L96"> processEncodedPayload();</span>
<span class="nc" id="L97"> list = mtasPosition.getPositions();</span>
<span class="nc bnc" id="L98" title="All 2 branches missed."> if (list != null) {</span>
<span class="nc" id="L99"> return mtasPosition.getPositions();</span>
}
<span class="nc" id="L101"> } catch (IOException e) {</span>
<span class="nc" id="L102"> log.debug(e);</span>
// do nothing
<span class="nc" id="L104"> }</span>
<span class="nc" id="L105"> int start = super.startPosition();</span>
<span class="nc" id="L106"> int end = super.endPosition();</span>
<span class="nc" id="L107"> list = new int[end - start];</span>
<span class="nc bnc" id="L108" title="All 2 branches missed."> for (int i = start; i < end; i++)</span>
<span class="nc" id="L109"> list[i - start] = i;</span>
<span class="nc" id="L110"> return list;</span>
}
}
/**
* Process encoded payload.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
private void processEncodedPayload() throws IOException {
<span class="pc bpc" id="L120" title="1 of 2 branches missed."> if (!readPayload) {</span>
<span class="fc" id="L121"> payloadSpanCollector.reset();</span>
<span class="fc" id="L122"> collect(payloadSpanCollector);</span>
<span class="fc" id="L123"> Collection<byte[]> originalPayloadCollection = payloadSpanCollector</span>
<span class="fc" id="L124"> .getPayloads();</span>
<span class="pc bpc" id="L125" title="1 of 2 branches missed."> if (originalPayloadCollection.iterator().hasNext()) {</span>
<span class="fc" id="L126"> byte[] payload = originalPayloadCollection.iterator().next();</span>
<span class="pc bpc" id="L127" title="1 of 2 branches missed."> if (payload == null) {</span>
<span class="nc" id="L128"> throw new IOException("no payload");</span>
}
<span class="fc" id="L130"> MtasPayloadDecoder payloadDecoder = new MtasPayloadDecoder();</span>
<span class="fc" id="L131"> payloadDecoder.init(startPosition(), payload);</span>
<span class="fc" id="L132"> mtasPosition = payloadDecoder.getMtasPosition();</span>
<span class="fc" id="L133"> } else {</span>
<span class="nc" id="L134"> throw new IOException("no payload");</span>
}
}
<span class="fc" id="L137"> }</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>