1
|
package eu.dnetlib.functionality.index;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringReader;
|
5
|
import java.util.Map;
|
6
|
|
7
|
import eu.dnetlib.functionality.cql.CqlValueTransformerMap;
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
|
11
|
import org.apache.solr.client.solrj.impl.CloudSolrClient;
|
12
|
import org.dom4j.Document;
|
13
|
import org.dom4j.DocumentException;
|
14
|
import org.dom4j.io.SAXReader;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.beans.factory.annotation.Required;
|
17
|
|
18
|
import com.google.common.collect.Maps;
|
19
|
|
20
|
import eu.dnetlib.data.provision.index.rmi.IndexServiceException;
|
21
|
|
22
|
import eu.dnetlib.functionality.index.feed.DocumentMapperFactory;
|
23
|
import eu.dnetlib.functionality.index.model.Any.ValueType;
|
24
|
import eu.dnetlib.functionality.index.query.IndexQueryFactory;
|
25
|
import eu.dnetlib.functionality.index.query.SolrIndexQueryFactory;
|
26
|
import eu.dnetlib.functionality.index.query.SolrIndexQueryResponseFactory;
|
27
|
import eu.dnetlib.functionality.index.solr.cql.SolrTypeBasedCqlValueTransformerMapFactory;
|
28
|
import eu.dnetlib.functionality.index.solr.feed.SolrDocumentMapperFactory;
|
29
|
import eu.dnetlib.functionality.index.utils.IndexConfigFactory;
|
30
|
import eu.dnetlib.functionality.index.utils.MetadataReference;
|
31
|
import eu.dnetlib.functionality.index.utils.RemoteSolrAdministrator;
|
32
|
import eu.dnetlib.functionality.index.utils.ZkUtils;
|
33
|
|
34
|
/**
|
35
|
* The Class SolrIndexServerDAO.
|
36
|
*/
|
37
|
public class SolrIndexServerDAO extends AbstractBackendDescriptor implements IndexServerDAO {
|
38
|
|
39
|
/**
|
40
|
* The log.
|
41
|
*/
|
42
|
private static final Log log = LogFactory.getLog(SolrIndexServerDAO.class); // NOPMD by marko on 11/24/08 5:02 PM
|
43
|
|
44
|
/** The zk utils. */
|
45
|
@Autowired
|
46
|
private ZkUtils zkUtils;
|
47
|
|
48
|
/** The query response factory. */
|
49
|
@Autowired
|
50
|
private SolrIndexQueryResponseFactory queryResponseFactory;
|
51
|
|
52
|
@Autowired
|
53
|
private SolrIndexQueryFactory solrIndexQueryFactory;
|
54
|
|
55
|
/** The solr document mapper factory. */
|
56
|
@Autowired
|
57
|
private SolrDocumentMapperFactory solrDocumentMapperFactory;
|
58
|
|
59
|
/** The solr administrator. */
|
60
|
private RemoteSolrAdministrator solrAdministrator;
|
61
|
|
62
|
/** The solr type based cql value transformer map. */
|
63
|
@Autowired
|
64
|
private SolrTypeBasedCqlValueTransformerMapFactory tMapFactory;
|
65
|
|
66
|
/**
|
67
|
* {@inheritDoc}
|
68
|
*
|
69
|
* @see eu.dnetlib.functionality.index.IndexServerDAO#createIndexCollection(eu.dnetlib.functionality.index.utils.MetadataReference,
|
70
|
* java.lang.String)
|
71
|
*/
|
72
|
@Override
|
73
|
public void createIndexCollection(final MetadataReference mdref, final String fields) throws IndexServiceException {
|
74
|
CloudSolrClient client = null;
|
75
|
try {
|
76
|
client = getClient();
|
77
|
client.connect();
|
78
|
|
79
|
if (!solrAdministrator.indexCollectionExists(mdref.toString(), client)) {
|
80
|
Map<String, String> params = Maps.newHashMap();
|
81
|
|
82
|
final Map<String, String> p = getServiceProperties();
|
83
|
params.put("numShards", p.get("numShards"));
|
84
|
params.put("replicationFactor", p.get("replicationFactor"));
|
85
|
|
86
|
for (IndexConfigFactory.CONFIG_PARAMS param_Name : IndexConfigFactory.CONFIG_PARAMS.values()) {
|
87
|
params.put(param_Name.toString(), p.get(param_Name.toString()));
|
88
|
}
|
89
|
|
90
|
zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdref.toString(), parse(fields), params, false);
|
91
|
solrAdministrator.createSolrIndex(p.get("host"), p.get("port"), mdref.toString(), p.get("numShards"), p.get("replicationFactor"),
|
92
|
mdref.toString(), p.get("maxShardsPerNode"));
|
93
|
}
|
94
|
client.getZkStateReader().close();
|
95
|
|
96
|
} catch (Exception e) {
|
97
|
log.error("Error on creating IndexCollection", e);
|
98
|
throw new IndexServiceException("Error on creating IndexCollection", e);
|
99
|
} finally {
|
100
|
if (client != null) {
|
101
|
shutdown(mdref);
|
102
|
}
|
103
|
}
|
104
|
}
|
105
|
|
106
|
@Override
|
107
|
public void updateIndexCollection(final MetadataReference mdRef, final Document fields) throws IndexServiceException {
|
108
|
CloudSolrClient client = null;
|
109
|
try {
|
110
|
client = getClient();
|
111
|
client.connect();
|
112
|
Map<String, String> params = Maps.newHashMap();
|
113
|
|
114
|
params.put("numShards", getServiceProperties().get("numShards"));
|
115
|
params.put("replicationFactor", getServiceProperties().get("replicationFactor"));
|
116
|
|
117
|
for (IndexConfigFactory.CONFIG_PARAMS param_Name : IndexConfigFactory.CONFIG_PARAMS.values()) {
|
118
|
params.put(param_Name.toString(), getServiceProperties().get(param_Name.toString()));
|
119
|
}
|
120
|
|
121
|
zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdRef.toString(), fields, params, true);
|
122
|
client.getZkStateReader().close();
|
123
|
shutdown(mdRef);
|
124
|
solrAdministrator.reloadCollection(getServiceProperties().get("host"), getServiceProperties().get("port"), mdRef.toString());
|
125
|
|
126
|
} catch (Exception e) {
|
127
|
log.error("Error on updating IndexCollection", e);
|
128
|
throw new IndexServiceException("Error on updating IndexCollection", e);
|
129
|
} finally {
|
130
|
if (client != null) {
|
131
|
shutdown(mdRef);
|
132
|
}
|
133
|
}
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Parses the fields parameter.
|
138
|
*
|
139
|
* @param fields
|
140
|
* the fields
|
141
|
* @return the document
|
142
|
* @throws IndexServiceException
|
143
|
* the index service exception
|
144
|
*/
|
145
|
private Document parse(final String fields) throws IndexServiceException {
|
146
|
try {
|
147
|
return new SAXReader().read(new StringReader(fields));
|
148
|
} catch (DocumentException e) {
|
149
|
throw new IndexServiceException("unable to parse fields: " + fields, e);
|
150
|
}
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* {@inheritDoc}
|
155
|
*
|
156
|
* @see eu.dnetlib.functionality.index.IndexServerDAO#getIndexCollection(eu.dnetlib.functionality.index.utils.MetadataReference)
|
157
|
*/
|
158
|
@Override
|
159
|
public IndexCollection getIndexCollection(final MetadataReference mdref) throws IndexServiceException {
|
160
|
final CloudSolrClient client = getClient(mdref);
|
161
|
return new SolrIndexCollection(client);
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* {@inheritDoc}
|
166
|
*
|
167
|
* @see eu.dnetlib.functionality.index.IndexServerDAO#getSchema(MetadataReference)
|
168
|
*/
|
169
|
@Override
|
170
|
public Map<String, ValueType> getSchema(final MetadataReference mdRef) throws IndexServiceException {
|
171
|
CloudSolrClient client = getClient(mdRef);
|
172
|
Map<String, ValueType> fields = solrAdministrator.getFieldNamesAndTypes(mdRef.toString(), client);
|
173
|
shutdown(mdRef);
|
174
|
return fields;
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* {@inheritDoc}
|
179
|
*
|
180
|
* @see eu.dnetlib.functionality.index.IndexServerDAO#getCqlValueTransformerMap(MetadataReference)
|
181
|
*/
|
182
|
@Override
|
183
|
public CqlValueTransformerMap getCqlValueTransformerMap(final MetadataReference mdRef) throws IndexServiceException {
|
184
|
return tMapFactory.getIt(getSchema(mdRef));
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* {@inheritDoc}
|
189
|
*
|
190
|
* @see eu.dnetlib.functionality.index.IndexServerDAO#getDocumentMapperFactory()
|
191
|
*/
|
192
|
@Override
|
193
|
public DocumentMapperFactory getDocumentMapperFactory() throws IndexServiceException {
|
194
|
return solrDocumentMapperFactory;
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* {@inheritDoc}
|
199
|
*
|
200
|
* @see eu.dnetlib.functionality.index.IndexServerDAO#shutdown(MetadataReference)
|
201
|
*/
|
202
|
@Override
|
203
|
public void shutdown(final MetadataReference mdRef) throws IndexServiceException {
|
204
|
try {
|
205
|
getClient(mdRef).close();
|
206
|
} catch (IOException e) {
|
207
|
throw new IndexServiceException(e);
|
208
|
}
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* Gets a server with the default collection set according to the given mdRef.
|
213
|
*
|
214
|
* @param mdRef
|
215
|
* the md ref
|
216
|
* @return a server instance
|
217
|
* @throws IndexServiceException
|
218
|
* the index service exception
|
219
|
*/
|
220
|
private CloudSolrClient getClient(final MetadataReference mdRef) throws IndexServiceException {
|
221
|
CloudSolrClient server = getClient();
|
222
|
server.setDefaultCollection(mdRef.toString());
|
223
|
return server;
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* Gets the server.
|
228
|
*
|
229
|
* @return a server instance
|
230
|
*/
|
231
|
private CloudSolrClient getClient() {
|
232
|
final String address = getEndpoint().get(ADDRESS);
|
233
|
log.info("connecting to address: " + address);
|
234
|
return new CloudSolrClient.Builder()
|
235
|
.withZkHost(address)
|
236
|
.build();
|
237
|
}
|
238
|
|
239
|
/**
|
240
|
* Gets the solr administrator.
|
241
|
*
|
242
|
* @return the solrAdministrator
|
243
|
*/
|
244
|
public RemoteSolrAdministrator getSolrAdministrator() {
|
245
|
return solrAdministrator;
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* Sets the solr administrator.
|
250
|
*
|
251
|
* @param solrAdministrator
|
252
|
* the solrAdministrator to set
|
253
|
*/
|
254
|
@Required
|
255
|
public void setSolrAdministrator(final RemoteSolrAdministrator solrAdministrator) {
|
256
|
this.solrAdministrator = solrAdministrator;
|
257
|
}
|
258
|
|
259
|
@Override
|
260
|
public IndexQueryFactory getIndexQueryFactory() {
|
261
|
return solrIndexQueryFactory;
|
262
|
}
|
263
|
|
264
|
}
|