MtasFieldsProducer.java.html
13.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
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
<?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>MtasFieldsProducer.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.codec</a> > <span class="el_source">MtasFieldsProducer.java</span></div><h1>MtasFieldsProducer.java</h1><pre class="source lang-java linenums">package mtas.codec;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.codecs.CodecUtil;
import org.apache.lucene.codecs.FieldsProducer;
import org.apache.lucene.codecs.PostingsFormat;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.IndexFormatTooOldException;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.Terms;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.Accountables;
/**
* The Class MtasFieldsProducer.
*/
public class MtasFieldsProducer extends FieldsProducer {
/** The Constant log. */
<span class="fc" id="L34"> private static final Log log = LogFactory.getLog(MtasFieldsProducer.class);</span>
/** The delegate fields producer. */
private FieldsProducer delegateFieldsProducer;
/** The index input list. */
private HashMap<String, IndexInput> indexInputList;
/** The index input offset list. */
private HashMap<String, Long> indexInputOffsetList;
/** The version. */
private int version;
/**
* Instantiates a new mtas fields producer.
*
* @param state the state
* @param name the name
* @throws IOException Signals that an I/O exception has occurred.
*/
public MtasFieldsProducer(SegmentReadState state, String name)
<span class="fc" id="L56"> throws IOException {</span>
<span class="fc" id="L57"> String postingsFormatName = null;</span>
<span class="fc" id="L58"> indexInputList = new HashMap<>();</span>
<span class="fc" id="L59"> indexInputOffsetList = new HashMap<>();</span>
<span class="fc" id="L60"> version = MtasCodecPostingsFormat.VERSION_CURRENT;</span>
<span class="fc" id="L62"> postingsFormatName = addIndexInputToList("object", openMtasFile(state, name,</span>
MtasCodecPostingsFormat.MTAS_OBJECT_EXTENSION), postingsFormatName);
<span class="fc" id="L64"> addIndexInputToList("term",</span>
<span class="fc" id="L65"> openMtasFile(state, name, MtasCodecPostingsFormat.MTAS_TERM_EXTENSION),</span>
postingsFormatName);
<span class="fc" id="L67"> addIndexInputToList("prefix", openMtasFile(state, name,</span>
MtasCodecPostingsFormat.MTAS_PREFIX_EXTENSION), postingsFormatName);
<span class="fc" id="L69"> addIndexInputToList("field",</span>
<span class="fc" id="L70"> openMtasFile(state, name, MtasCodecPostingsFormat.MTAS_FIELD_EXTENSION),</span>
postingsFormatName);
<span class="fc" id="L72"> addIndexInputToList("indexDocId",</span>
<span class="fc" id="L73"> openMtasFile(state, name,</span>
MtasCodecPostingsFormat.MTAS_INDEX_DOC_ID_EXTENSION),
postingsFormatName);
<span class="fc" id="L76"> addIndexInputToList("indexObjectId",</span>
<span class="fc" id="L77"> openMtasFile(state, name,</span>
MtasCodecPostingsFormat.MTAS_INDEX_OBJECT_ID_EXTENSION),
postingsFormatName);
try {
<span class="fc" id="L81"> addIndexInputToList(</span>
<span class="fc" id="L82"> "doc", openMtasFile(state, name,</span>
<span class="fc" id="L83"> MtasCodecPostingsFormat.MTAS_DOC_EXTENSION, version, version),</span>
postingsFormatName);
<span class="fc" id="L85"> addIndexInputToList("indexObjectPosition",</span>
<span class="fc" id="L86"> openMtasFile(state, name,</span>
MtasCodecPostingsFormat.MTAS_INDEX_OBJECT_POSITION_EXTENSION,
<span class="fc" id="L88"> version, version),</span>
postingsFormatName);
<span class="fc" id="L90"> addIndexInputToList("indexObjectParent",</span>
<span class="fc" id="L91"> openMtasFile(state, name,</span>
MtasCodecPostingsFormat.MTAS_INDEX_OBJECT_PARENT_EXTENSION,
<span class="fc" id="L93"> version, version),</span>
postingsFormatName);
<span class="nc" id="L95"> } catch (IndexFormatTooOldException e) {</span>
<span class="nc" id="L96"> log.debug(e);</span>
<span class="nc" id="L97"> throw new IOException(</span>
"This MTAS doesn't support your index version, please upgrade");
<span class="fc" id="L99"> }</span>
// Load the delegate postingsFormatName from this file
<span class="fc" id="L101"> this.delegateFieldsProducer = PostingsFormat.forName(postingsFormatName)</span>
<span class="fc" id="L102"> .fieldsProducer(state);</span>
<span class="fc" id="L103"> }</span>
/**
* Adds the index input to list.
*
* @param name the name
* @param in the in
* @param postingsFormatName the postings format name
* @return the string
* @throws IOException Signals that an I/O exception has occurred.
*/
private String addIndexInputToList(String name, IndexInput in,
String postingsFormatName) throws IOException {
<span class="pc bpc" id="L116" title="1 of 2 branches missed."> if (indexInputList.get(name) != null) {</span>
<span class="nc" id="L117"> indexInputList.get(name).close();</span>
}
<span class="pc bpc" id="L119" title="1 of 2 branches missed."> if (in != null) {</span>
<span class="fc" id="L120"> String localPostingsFormatName = postingsFormatName;</span>
<span class="fc bfc" id="L121" title="All 2 branches covered."> if (localPostingsFormatName == null) {</span>
<span class="fc" id="L122"> localPostingsFormatName = in.readString();</span>
<span class="pc bpc" id="L123" title="1 of 2 branches missed."> } else if (!in.readString().equals(localPostingsFormatName)) {</span>
<span class="nc" id="L124"> throw new IOException("delegate codec " + name + " doesn't equal "</span>
+ localPostingsFormatName);
}
<span class="fc" id="L127"> indexInputList.put(name, in);</span>
<span class="fc" id="L128"> indexInputOffsetList.put(name, in.getFilePointer());</span>
<span class="fc" id="L129"> return localPostingsFormatName;</span>
} else {
<span class="nc" id="L131"> log.debug("no " + name + " registered");</span>
<span class="nc" id="L132"> return null;</span>
}
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.index.Fields#iterator()
*/
@Override
public Iterator<String> iterator() {
<span class="nc" id="L143"> return delegateFieldsProducer.iterator();</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.codecs.FieldsProducer#close()
*/
@Override
public void close() throws IOException {
<span class="fc" id="L153"> delegateFieldsProducer.close();</span>
<span class="fc bfc" id="L154" title="All 2 branches covered."> for (Entry<String, IndexInput> entry : indexInputList.entrySet()) {</span>
<span class="fc" id="L155"> entry.getValue().close();</span>
<span class="fc" id="L156"> }</span>
<span class="fc" id="L157"> }</span>
/*
* (non-Javadoc)
*
* @see org.apache.lucene.index.Fields#terms(java.lang.String)
*/
@Override
public Terms terms(String field) throws IOException {
<span class="fc" id="L166"> return new MtasTerms(delegateFieldsProducer.terms(field), indexInputList,</span>
indexInputOffsetList, version);
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.index.Fields#size()
*/
@Override
public int size() {
<span class="nc" id="L177"> return delegateFieldsProducer.size();</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.util.Accountable#ramBytesUsed()
*/
@Override
public long ramBytesUsed() {
// return BASE_RAM_BYTES_USED + delegateFieldsProducer.ramBytesUsed();
<span class="nc" id="L188"> return 3 * delegateFieldsProducer.ramBytesUsed();</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.util.Accountable#getChildResources()
*/
@Override
public Collection<Accountable> getChildResources() {
<span class="nc" id="L198"> List<Accountable> resources = new ArrayList<>();</span>
<span class="nc bnc" id="L199" title="All 2 branches missed."> if (delegateFieldsProducer != null) {</span>
<span class="nc" id="L200"> resources.add(</span>
<span class="nc" id="L201"> Accountables.namedAccountable("delegate", delegateFieldsProducer));</span>
}
<span class="nc" id="L203"> return Collections.unmodifiableList(resources);</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.codecs.FieldsProducer#checkIntegrity()
*/
@Override
public void checkIntegrity() throws IOException {
<span class="nc" id="L213"> delegateFieldsProducer.checkIntegrity();</span>
<span class="nc" id="L214"> }</span>
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
<span class="nc" id="L223"> return getClass().getSimpleName() + "(delegate=" + delegateFieldsProducer</span>
+ ")";
}
/**
* Open mtas file.
*
* @param state the state
* @param name the name
* @param extension the extension
* @param minimum the minimum
* @param maximum the maximum
* @return the index input
* @throws IOException Signals that an I/O exception has occurred.
*/
private IndexInput openMtasFile(SegmentReadState state, String name,
String extension, Integer minimum, Integer maximum) throws IOException {
<span class="fc" id="L240"> String fileName = IndexFileNames.segmentFileName(state.segmentInfo.name,</span>
state.segmentSuffix, extension);
IndexInput object;
try {
<span class="fc" id="L244"> object = state.directory.openInput(fileName, state.context);</span>
<span class="nc" id="L245"> } catch (FileNotFoundException | NoSuchFileException e) {</span>
<span class="nc" id="L246"> log.debug(e);</span>
// throw new NoSuchFileException(e.getMessage());
<span class="nc" id="L248"> return null;</span>
<span class="fc" id="L249"> }</span>
<span class="fc bfc" id="L250" title="All 2 branches covered."> int minVersion = (minimum == null) ? MtasCodecPostingsFormat.VERSION_START</span>
<span class="fc" id="L251"> : minimum.intValue();</span>
<span class="fc bfc" id="L252" title="All 2 branches covered."> int maxVersion = (maximum == null) ? MtasCodecPostingsFormat.VERSION_CURRENT</span>
<span class="fc" id="L253"> : maximum.intValue();</span>
try {
<span class="fc" id="L255"> CodecUtil.checkIndexHeader(object, name, minVersion, maxVersion,</span>
<span class="fc" id="L256"> state.segmentInfo.getId(), state.segmentSuffix);</span>
<span class="nc" id="L257"> } catch (IndexFormatTooOldException e) {</span>
<span class="nc" id="L258"> object.close();</span>
<span class="nc" id="L259"> log.debug(e);</span>
<span class="nc" id="L260"> throw new IndexFormatTooOldException(e.getMessage(), e.getVersion(),</span>
<span class="nc" id="L261"> e.getMinVersion(), e.getMaxVersion());</span>
<span class="nc" id="L262"> } catch (EOFException e) {</span>
<span class="nc" id="L263"> object.close();</span>
<span class="nc" id="L264"> log.debug(e);</span>
// throw new EOFException(e.getMessage());
<span class="nc" id="L266"> return null;</span>
<span class="fc" id="L267"> }</span>
<span class="fc" id="L268"> return object;</span>
}
/**
* Open mtas file.
*
* @param state the state
* @param name the name
* @param extension the extension
* @return the index input
* @throws IOException Signals that an I/O exception has occurred.
*/
private IndexInput openMtasFile(SegmentReadState state, String name,
String extension) throws IOException {
<span class="fc" id="L282"> return openMtasFile(state, name, extension, null, null);</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>