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