Project

General

Profile

1
package eu.dnetlib.index.solr;
2

    
3
import java.io.IOException;
4
import java.io.StringReader;
5
import java.util.HashMap;
6
import java.util.Map;
7

    
8
import eu.dnetlib.clients.index.model.Any.ValueType;
9
import eu.dnetlib.clients.index.query.IndexQueryFactory;
10
import eu.dnetlib.utils.MetadataReference;
11
import eu.dnetlib.index.AbstractBackendDescriptor;
12
import eu.dnetlib.index.IndexCollection;
13
import eu.dnetlib.index.IndexServerDAO;
14
import eu.dnetlib.cql.CqlValueTransformerMap;
15
import eu.dnetlib.index.feed.DocumentMapperFactory;
16
import eu.dnetlib.index.query.SolrIndexQueryFactory;
17
import eu.dnetlib.index.query.SolrIndexQueryResponseFactory;
18
import eu.dnetlib.index.solr.cql.SolrTypeBasedCqlValueTransformerMapFactory;
19
import eu.dnetlib.index.solr.feed.SolrDocumentMapperFactory;
20
import eu.dnetlib.index.utils.IndexConfigFactory;
21
import eu.dnetlib.index.utils.RemoteSolrAdministrator;
22
import eu.dnetlib.index.utils.ZkUtils;
23
import eu.dnetlib.rmi.provision.IndexServiceException;
24
import org.apache.commons.logging.Log;
25
import org.apache.commons.logging.LogFactory;
26
import org.apache.solr.client.solrj.SolrServerException;
27
import org.apache.solr.client.solrj.impl.CloudSolrClient;
28
import org.dom4j.Document;
29
import org.dom4j.DocumentException;
30
import org.dom4j.io.SAXReader;
31
import org.springframework.beans.factory.annotation.Autowired;
32
import org.springframework.beans.factory.annotation.Required;
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
	/**
45
	 * The zk utils.
46
	 */
47
	@Autowired
48
	private ZkUtils zkUtils;
49

    
50
	/**
51
	 * The query response factory.
52
	 */
53
	@Autowired
54
	private SolrIndexQueryResponseFactory queryResponseFactory;
55

    
56
	@Autowired
57
	private SolrIndexQueryFactory solrIndexQueryFactory;
58

    
59
	/**
60
	 * The solr document mapper factory.
61
	 */
62
	@Autowired
63
	private SolrDocumentMapperFactory solrDocumentMapperFactory;
64

    
65
	/**
66
	 * The solr administrator.
67
	 */
68
	private RemoteSolrAdministrator solrAdministrator;
69

    
70
	/**
71
	 * The solr type based cql value transformer map.
72
	 */
73
	@Autowired
74
	private SolrTypeBasedCqlValueTransformerMapFactory tMapFactory;
75

    
76
	/**
77
	 * {@inheritDoc}
78
	 * <p>
79
	 * String)
80
	 */
81
	@Override
82
	public void createIndexCollection(final MetadataReference mdref, final String fields) throws IndexServiceException {
83
		CloudSolrClient client = null;
84
		try {
85

    
86
			client = getClient();
87
			client.connect();
88
			final Map<String, String> p = getServiceProperties();
89

    
90
			if (!solrAdministrator.indexCollectionExists(mdref.toString(), client, p.get("host"), p.get("port"))) {
91
				Map<String, String> params = new HashMap<>();
92

    
93
				params.put("numShards", p.get("numShards"));
94
				params.put("replicationFactor", p.get("replicationFactor"));
95

    
96
				for (IndexConfigFactory.CONFIG_PARAMS param_Name : IndexConfigFactory.CONFIG_PARAMS.values()) {
97
					params.put(param_Name.toString(), p.get(param_Name.toString()));
98
				}
99
				params.put(IndexConfigFactory.CONFIG_PARAMS.indexDataDir.toString(), mdref.toString());
100

    
101
				zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdref.toString(), parse(fields), params, false);
102
				solrAdministrator.createSolrIndex(p.get("host"), p.get("port"), mdref.toString(), p.get("numShards"), p.get("replicationFactor"),
103
						mdref.toString());
104
			}
105
			client.getZkStateReader().close();
106

    
107
		} catch (Exception e) {
108
			log.error("Error on creating IndexCollection", e);
109
			throw new IndexServiceException("Error on creating IndexCollection", e);
110
		} finally {
111
			if (client != null) {
112
				try {
113
					client.close();
114
				} catch (IOException e) {
115
					throw new IndexServiceException("Error while closing client", e);
116
				}
117
			}
118
		}
119
	}
120

    
121
	@Override
122
	public void updateIndexCollection(final MetadataReference mdRef, final Document fields) throws IndexServiceException {
123
		CloudSolrClient client = null;
124
		try {
125
			client = getClient();
126
			client.connect();
127
			Map<String, String> params = new HashMap<>();
128

    
129
			params.put("numShards", getServiceProperties().get("numShards"));
130
			params.put("replicationFactor", getServiceProperties().get("replicationFactor"));
131

    
132
			for (IndexConfigFactory.CONFIG_PARAMS param_Name : IndexConfigFactory.CONFIG_PARAMS.values()) {
133
				params.put(param_Name.toString(), getServiceProperties().get(param_Name.toString()));
134
			}
135
			params.put(IndexConfigFactory.CONFIG_PARAMS.indexDataDir.toString(), mdRef.toString());
136
			zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdRef.toString(), fields, params, true);
137
			client.getZkStateReader().close();
138
			client.shutdown();
139
			solrAdministrator.reloadCollection(getServiceProperties().get("host"), getServiceProperties().get("port"), mdRef.toString());
140

    
141
		} catch (Exception e) {
142
			log.error("Error on updating IndexCollection", e);
143
			throw new IndexServiceException("Error on updating IndexCollection", e);
144
		} finally {
145
			if (client != null) {
146
				client.shutdown();
147
			}
148
		}
149
	}
150

    
151
	/**
152
	 * Parses the fields parameter.
153
	 *
154
	 * @param fields the fields
155
	 * @return the document
156
	 * @throws IndexServiceException the index service exception
157
	 */
158
	private Document parse(final String fields) throws IndexServiceException {
159
		try {
160
			return new SAXReader().read(new StringReader(fields));
161
		} catch (DocumentException e) {
162
			throw new IndexServiceException("unable to parse fields: " + fields, e);
163
		}
164
	}
165

    
166
	/**
167
	 * {@inheritDoc}
168
	 */
169
	@Override
170
	public IndexCollection getIndexCollection(final MetadataReference mdref) throws IndexServiceException {
171
		CloudSolrClient newServer = getClient(mdref);
172
		try {
173
			newServer.ping();
174
		} catch (SolrServerException e) {
175
			e.printStackTrace();
176
		} catch (IOException e) {
177
			e.printStackTrace();
178
		}
179
		return new SolrIndexCollection(newServer);
180
	}
181

    
182
	/**
183
	 * {@inheritDoc}
184
	 */
185
	@Override
186
	public Map<String, ValueType> getSchema(final MetadataReference mdRef) throws IndexServiceException {
187
		CloudSolrClient server = getClient(mdRef);
188
		Map<String, ValueType> fields = solrAdministrator.getFieldNamesAndTypes(mdRef.toString(), server);
189
		try {
190
			server.close();
191
		} catch (IOException e) {
192
			throw new IndexServiceException("Error on closing client");
193
		}
194
		return fields;
195
	}
196

    
197
	/**
198
	 * {@inheritDoc}
199
	 */
200
	@Override
201
	public CqlValueTransformerMap getCqlValueTransformerMap(final MetadataReference mdRef) throws IndexServiceException {
202
		return tMapFactory.getIt(getSchema(mdRef));
203
	}
204

    
205
	/**
206
	 * {@inheritDoc}
207
	 */
208
	@Override
209
	public DocumentMapperFactory getDocumentMapperFactory() throws IndexServiceException {
210
		return solrDocumentMapperFactory;
211
	}
212

    
213
	/**
214
	 * {@inheritDoc}
215
	 */
216
	@Override
217
	public void shutdown(final MetadataReference mdRef) throws IndexServiceException {
218
		try {
219
			getClient(mdRef).close();
220
		} catch (IOException e) {
221
			throw new IndexServiceException("Error on closing client", e);
222
		}
223

    
224
	}
225

    
226
	/**
227
	 * Gets a server with the default collection set according to the given mdRef.
228
	 *
229
	 * @param mdRef the md ref
230
	 * @return a server instance
231
	 * @throws IndexServiceException the index service exception
232
	 */
233
	private CloudSolrClient getClient(final MetadataReference mdRef) throws IndexServiceException {
234
		CloudSolrClient client = getClient();
235
		client.setDefaultCollection(mdRef.toString());
236
		return client;
237
	}
238

    
239
	/**
240
	 * Gets the server.
241
	 *
242
	 * @return a server instance
243
	 */
244
	private CloudSolrClient getClient() {
245
		String address = getEndpoint().get(ADDRESS);
246
		log.info("connecting to address: " + address);
247
		return new CloudSolrClient(address);
248

    
249
	}
250

    
251
	/**
252
	 * Gets the solr administrator.
253
	 *
254
	 * @return the solrAdministrator
255
	 */
256
	public RemoteSolrAdministrator getSolrAdministrator() {
257
		return solrAdministrator;
258
	}
259

    
260
	/**
261
	 * Sets the solr administrator.
262
	 *
263
	 * @param solrAdministrator the solrAdministrator to set
264
	 */
265
	@Required
266
	public void setSolrAdministrator(final RemoteSolrAdministrator solrAdministrator) {
267
		this.solrAdministrator = solrAdministrator;
268
	}
269

    
270
	@Override
271
	public IndexQueryFactory getIndexQueryFactory() {
272
		return solrIndexQueryFactory;
273
	}
274

    
275
}
(2-2/2)