MtasSolrComponentPrefix.java.html
15.1 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
<?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>MtasSolrComponentPrefix.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.solr.handler.component.util</a> > <span class="el_source">MtasSolrComponentPrefix.java</span></div><h1>MtasSolrComponentPrefix.java</h1><pre class="source lang-java linenums">package mtas.solr.handler.component.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.SortedSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.handler.component.ResponseBuilder;
import org.apache.solr.handler.component.SearchComponent;
import org.apache.solr.handler.component.ShardRequest;
import org.apache.solr.handler.component.ShardResponse;
import mtas.codec.util.CodecComponent.ComponentField;
import mtas.codec.util.CodecComponent.ComponentFields;
import mtas.codec.util.CodecComponent.ComponentPrefix;
import mtas.solr.handler.component.MtasSolrSearchComponent;
/**
* The Class MtasSolrComponentPrefix.
*/
public class MtasSolrComponentPrefix
implements MtasSolrComponent<ComponentPrefix> {
/** The Constant log. */
<span class="fc" id="L29"> private static final Log log = LogFactory.getLog(MtasSolrComponentPrefix.class);</span>
/** The search component. */
MtasSolrSearchComponent searchComponent;
/** The Constant PARAM_MTAS_PREFIX. */
public static final String PARAM_MTAS_PREFIX = MtasSolrSearchComponent.PARAM_MTAS
+ ".prefix";
/** The Constant NAME_MTAS_PREFIX_FIELD. */
public static final String NAME_MTAS_PREFIX_FIELD = "field";
/** The Constant NAME_MTAS_PREFIX_KEY. */
public static final String NAME_MTAS_PREFIX_KEY = "key";
/**
* Instantiates a new mtas solr component prefix.
*
* @param searchComponent the search component
*/
<span class="fc" id="L49"> public MtasSolrComponentPrefix(MtasSolrSearchComponent searchComponent) {</span>
<span class="fc" id="L50"> this.searchComponent = searchComponent;</span>
<span class="fc" id="L51"> }</span>
/* (non-Javadoc)
* @see mtas.solr.handler.component.util.MtasSolrComponent#prepare(org.apache.solr.handler.component.ResponseBuilder, mtas.codec.util.CodecComponent.ComponentFields)
*/
public void prepare(ResponseBuilder rb, ComponentFields mtasFields)
throws IOException {
<span class="nc" id="L58"> Set<String> ids = MtasSolrResultUtil</span>
<span class="nc" id="L59"> .getIdsFromParameters(rb.req.getParams(), PARAM_MTAS_PREFIX);</span>
<span class="nc bnc" id="L60" title="All 2 branches missed."> if (!ids.isEmpty()) {</span>
<span class="nc" id="L61"> int tmpCounter = 0;</span>
<span class="nc" id="L62"> String[] fields = new String[ids.size()];</span>
<span class="nc" id="L63"> String[] keys = new String[ids.size()];</span>
<span class="nc bnc" id="L64" title="All 2 branches missed."> for (String id : ids) {</span>
<span class="nc" id="L65"> fields[tmpCounter] = rb.req.getParams().get(</span>
PARAM_MTAS_PREFIX + "." + id + "." + NAME_MTAS_PREFIX_FIELD, null);
<span class="nc" id="L67"> keys[tmpCounter] = rb.req.getParams()</span>
<span class="nc" id="L68"> .get(PARAM_MTAS_PREFIX + "." + id + "." + NAME_MTAS_PREFIX_KEY,</span>
<span class="nc" id="L69"> String.valueOf(tmpCounter))</span>
<span class="nc" id="L70"> .trim();</span>
<span class="nc" id="L71"> tmpCounter++;</span>
<span class="nc" id="L72"> }</span>
<span class="nc" id="L73"> String uniqueKeyField = rb.req.getSchema().getUniqueKeyField().getName();</span>
<span class="nc" id="L74"> mtasFields.doPrefix = true;</span>
// init and checks
<span class="nc bnc" id="L76" title="All 2 branches missed."> for (String field : fields) {</span>
<span class="nc bnc" id="L77" title="All 4 branches missed."> if (field == null || field.isEmpty()) {</span>
<span class="nc" id="L78"> throw new IOException("no (valid) field in mtas prefix");</span>
<span class="nc bnc" id="L79" title="All 2 branches missed."> } else if (!mtasFields.list.containsKey(field)) {</span>
<span class="nc" id="L80"> mtasFields.list.put(field, new ComponentField(uniqueKeyField));</span>
}
}
<span class="nc" id="L83"> MtasSolrResultUtil.compareAndCheck(keys, fields, NAME_MTAS_PREFIX_KEY,</span>
<span class="nc" id="L84"> NAME_MTAS_PREFIX_FIELD, true);</span>
<span class="nc bnc" id="L85" title="All 2 branches missed."> for (int i = 0; i < fields.length; i++) {</span>
<span class="nc" id="L86"> String field = fields[i];</span>
<span class="nc bnc" id="L87" title="All 4 branches missed."> String key = ((keys == null) || (keys[i] == null)</span>
<span class="nc bnc" id="L88" title="All 2 branches missed."> || (keys[i].isEmpty())) ? String.valueOf(i) + ":" + field</span>
<span class="nc" id="L89"> : keys[i].trim();</span>
<span class="nc" id="L90"> mtasFields.list.get(field).prefix = new ComponentPrefix(key);</span>
}
}
<span class="nc" id="L93"> }</span>
/* (non-Javadoc)
* @see mtas.solr.handler.component.util.MtasSolrComponent#modifyRequest(org.apache.solr.handler.component.ResponseBuilder, org.apache.solr.handler.component.SearchComponent, org.apache.solr.handler.component.ShardRequest)
*/
public void modifyRequest(ResponseBuilder rb, SearchComponent who,
ShardRequest sreq) {
<span class="nc bnc" id="L100" title="All 2 branches missed."> if (sreq.params.getBool(MtasSolrSearchComponent.PARAM_MTAS, false)) {</span>
<span class="nc bnc" id="L101" title="All 4 branches missed."> if (sreq.params.getBool(PARAM_MTAS_PREFIX, false)</span>
&& (sreq.purpose & ShardRequest.PURPOSE_GET_TOP_IDS) != 0) {
// do nothing
} else {
// remove prefix for other requests
<span class="nc" id="L106"> Set<String> keys = MtasSolrResultUtil</span>
<span class="nc" id="L107"> .getIdsFromParameters(rb.req.getParams(), PARAM_MTAS_PREFIX);</span>
<span class="nc" id="L108"> sreq.params.remove(PARAM_MTAS_PREFIX);</span>
<span class="nc bnc" id="L109" title="All 2 branches missed."> for (String key : keys) {</span>
<span class="nc" id="L110"> sreq.params.remove(</span>
PARAM_MTAS_PREFIX + "." + key + "." + NAME_MTAS_PREFIX_FIELD);
<span class="nc" id="L112"> sreq.params.remove(</span>
PARAM_MTAS_PREFIX + "." + key + "." + NAME_MTAS_PREFIX_KEY);
<span class="nc" id="L114"> }</span>
}
}
<span class="nc" id="L117"> }</span>
/* (non-Javadoc)
* @see mtas.solr.handler.component.util.MtasSolrComponent#create(mtas.codec.util.CodecComponent.BasicComponent, java.lang.Boolean)
*/
public SimpleOrderedMap<Object> create(ComponentPrefix prefix,
Boolean encode) {
<span class="nc" id="L124"> SimpleOrderedMap<Object> mtasPrefixResponse = new SimpleOrderedMap<>();</span>
<span class="nc" id="L125"> mtasPrefixResponse.add("key", prefix.key);</span>
<span class="nc bnc" id="L126" title="All 2 branches missed."> if (encode) {</span>
<span class="nc" id="L127"> mtasPrefixResponse.add("_encoded_singlePosition",</span>
<span class="nc" id="L128"> MtasSolrResultUtil.encode(prefix.singlePositionList));</span>
<span class="nc" id="L129"> mtasPrefixResponse.add("_encoded_multiplePosition",</span>
<span class="nc" id="L130"> MtasSolrResultUtil.encode(prefix.multiplePositionList));</span>
<span class="nc" id="L131"> mtasPrefixResponse.add("_encoded_setPosition",</span>
<span class="nc" id="L132"> MtasSolrResultUtil.encode(prefix.setPositionList));</span>
<span class="nc" id="L133"> mtasPrefixResponse.add("_encoded_intersecting",</span>
<span class="nc" id="L134"> MtasSolrResultUtil.encode(prefix.intersectingList));</span>
} else {
<span class="nc" id="L136"> mtasPrefixResponse.add("singlePosition", prefix.singlePositionList);</span>
<span class="nc" id="L137"> mtasPrefixResponse.add("multiplePosition", prefix.multiplePositionList);</span>
<span class="nc" id="L138"> mtasPrefixResponse.add("setPosition", prefix.setPositionList);</span>
<span class="nc" id="L139"> mtasPrefixResponse.add("intersecting", prefix.intersectingList);</span>
}
<span class="nc" id="L141"> return mtasPrefixResponse;</span>
}
/* (non-Javadoc)
* @see mtas.solr.handler.component.util.MtasSolrComponent#finishStage(org.apache.solr.handler.component.ResponseBuilder)
*/
@SuppressWarnings("unchecked")
public void finishStage(ResponseBuilder rb) {
<span class="nc bnc" id="L149" title="All 6 branches missed."> if (rb.req.getParams().getBool(MtasSolrSearchComponent.PARAM_MTAS, false)</span>
&& rb.stage >= ResponseBuilder.STAGE_EXECUTE_QUERY
&& rb.stage < ResponseBuilder.STAGE_GET_FIELDS) {
<span class="nc bnc" id="L152" title="All 2 branches missed."> for (ShardRequest sreq : rb.finished) {</span>
<span class="nc bnc" id="L153" title="All 2 branches missed."> if (sreq.params.getBool(MtasSolrSearchComponent.PARAM_MTAS, false)</span>
<span class="nc bnc" id="L154" title="All 2 branches missed."> && sreq.params.getBool(PARAM_MTAS_PREFIX, false)) {</span>
<span class="nc bnc" id="L155" title="All 2 branches missed."> for (ShardResponse shardResponse : sreq.responses) {</span>
<span class="nc" id="L156"> NamedList<Object> response = shardResponse.getSolrResponse()</span>
<span class="nc" id="L157"> .getResponse();</span>
try {
<span class="nc" id="L159"> ArrayList<NamedList<Object>> data = (ArrayList<NamedList<Object>>) response</span>
<span class="nc" id="L160"> .findRecursive("mtas", "prefix");</span>
<span class="nc bnc" id="L161" title="All 2 branches missed."> if (data != null) {</span>
<span class="nc" id="L162"> MtasSolrResultUtil.decode(data);</span>
}
<span class="nc" id="L164"> } catch (ClassCastException e) {</span>
<span class="nc" id="L165"> log.debug(e);</span>
// shouldnt happen
<span class="nc" id="L167"> }</span>
<span class="nc" id="L168"> }</span>
}
<span class="nc" id="L170"> }</span>
}
<span class="nc" id="L172"> }</span>
/* (non-Javadoc)
* @see mtas.solr.handler.component.util.MtasSolrComponent#distributedProcess(org.apache.solr.handler.component.ResponseBuilder, mtas.codec.util.CodecComponent.ComponentFields)
*/
@SuppressWarnings("unchecked")
public void distributedProcess(ResponseBuilder rb, ComponentFields mtasFields)
throws IOException {
// rewrite
<span class="nc" id="L181"> NamedList<Object> mtasResponse = null;</span>
try {
<span class="nc" id="L183"> mtasResponse = (NamedList<Object>) rb.rsp.getValues().get("mtas");</span>
<span class="nc" id="L184"> } catch (ClassCastException e) {</span>
<span class="nc" id="L185"> log.debug(e);</span>
<span class="nc" id="L186"> mtasResponse = null;</span>
<span class="nc" id="L187"> }</span>
<span class="nc bnc" id="L188" title="All 2 branches missed."> if (mtasResponse != null) {</span>
NamedList<Object> mtasResponsePrefix;
try {
<span class="nc" id="L191"> mtasResponsePrefix = (NamedList<Object>) mtasResponse.get("prefix");</span>
<span class="nc bnc" id="L192" title="All 2 branches missed."> if (mtasResponsePrefix != null) {</span>
<span class="nc" id="L193"> repairPrefixItems(mtasResponsePrefix);</span>
<span class="nc" id="L194"> MtasSolrResultUtil.rewrite(mtasResponsePrefix);</span>
}
<span class="nc" id="L196"> } catch (ClassCastException e) {</span>
<span class="nc" id="L197"> log.debug(e);</span>
<span class="nc" id="L198"> mtasResponse.remove("prefix");</span>
<span class="nc" id="L199"> }</span>
}
<span class="nc" id="L201"> }</span>
/**
* Repair prefix items.
*
* @param mtasResponse the mtas response
*/
@SuppressWarnings("unchecked")
private void repairPrefixItems(NamedList<Object> mtasResponse) {
// repair prefix lists
try {
<span class="nc" id="L212"> ArrayList<NamedList<?>> list = (ArrayList<NamedList<?>>) mtasResponse</span>
<span class="nc" id="L213"> .findRecursive("prefix");</span>
// MtasSolrResultUtil.rewrite(list);
<span class="nc bnc" id="L215" title="All 2 branches missed."> if (list != null) {</span>
<span class="nc bnc" id="L216" title="All 2 branches missed."> for (NamedList<?> item : list) {</span>
<span class="nc" id="L217"> SortedSet<String> singlePosition = (SortedSet<String>) item</span>
<span class="nc" id="L218"> .get("singlePosition");</span>
<span class="nc" id="L219"> SortedSet<String> multiplePosition = (SortedSet<String>) item</span>
<span class="nc" id="L220"> .get("multiplePosition");</span>
<span class="nc bnc" id="L221" title="All 4 branches missed."> if (singlePosition != null && multiplePosition != null) {</span>
<span class="nc bnc" id="L222" title="All 2 branches missed."> for (String prefix : multiplePosition) {</span>
<span class="nc bnc" id="L223" title="All 2 branches missed."> if (singlePosition.contains(prefix)) {</span>
<span class="nc" id="L224"> singlePosition.remove(prefix);</span>
}
<span class="nc" id="L226"> }</span>
}
<span class="nc" id="L228"> }</span>
}
<span class="nc" id="L230"> } catch (ClassCastException e) {</span>
<span class="nc" id="L231"> log.debug(e);</span>
<span class="nc" id="L232"> }</span>
<span class="nc" id="L233"> }</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>