Project

General

Profile

1
package eu.dnetlib.index.solr;
2

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

    
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9
import org.apache.solr.client.solrj.impl.CloudSolrClient;
10
import org.dom4j.Document;
11
import org.dom4j.DocumentException;
12
import org.dom4j.io.SAXReader;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.beans.factory.annotation.Required;
15

    
16
import com.google.common.collect.Maps;
17

    
18
import eu.dnetlib.clients.index.model.Any.ValueType;
19
import eu.dnetlib.clients.index.query.IndexQueryFactory;
20
import eu.dnetlib.cql.CqlValueTransformerMap;
21
import eu.dnetlib.index.AbstractBackendDescriptor;
22
import eu.dnetlib.index.IndexCollection;
23
import eu.dnetlib.index.IndexServerDAO;
24
import eu.dnetlib.index.feed.DocumentMapperFactory;
25
import eu.dnetlib.index.query.SolrIndexQueryFactory;
26
import eu.dnetlib.index.query.SolrIndexQueryResponseFactory;
27
import eu.dnetlib.index.solr.cql.SolrTypeBasedCqlValueTransformerMapFactory;
28
import eu.dnetlib.index.solr.feed.SolrDocumentMapperFactory;
29
import eu.dnetlib.index.utils.IndexConfigFactory;
30
import eu.dnetlib.index.utils.RemoteSolrAdministrator;
31
import eu.dnetlib.index.utils.ZkServers;
32
import eu.dnetlib.index.utils.ZkUtils;
33
import eu.dnetlib.rmi.provision.IndexServiceException;
34
import eu.dnetlib.utils.MetadataReference;
35

    
36
/**
37
 * The Class SolrIndexServerDAO.
38
 */
39
public class SolrIndexServerDAO extends AbstractBackendDescriptor implements IndexServerDAO {
40

    
41
	/**
42
	 * The log.
43
	 */
44
	private static final Log log = LogFactory.getLog(SolrIndexServerDAO.class); // NOPMD by marko on 11/24/08 5:02 PM
45

    
46
	/** The zk utils. */
47
	@Autowired
48
	private ZkUtils zkUtils;
49

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

    
54
	@Autowired
55
	private SolrIndexQueryFactory solrIndexQueryFactory;
56

    
57
	/** The solr document mapper factory. */
58
	@Autowired
59
	private SolrDocumentMapperFactory solrDocumentMapperFactory;
60

    
61
	/** The solr administrator. */
62
	private RemoteSolrAdministrator solrAdministrator;
63

    
64
	/** The solr type based cql value transformer map. */
65
	@Autowired
66
	private SolrTypeBasedCqlValueTransformerMapFactory tMapFactory;
67

    
68
	/**
69
	 * {@inheritDoc}
70
	 * 
71
	 * @see eu.dnetlib.index.IndexServerDAO#createIndexCollection(eu.dnetlib.index.utils.MetadataReference,
72
	 *      java.lang.String)
73
	 */
74
	@Override
75
	public void createIndexCollection(final MetadataReference mdref, final String fields) throws IndexServiceException {
76
		
77
		log.debug("createIndexCollection  " + mdref.toString() + "   " + fields );
78
		
79
		try(CloudSolrClient client = getClient()) {
80

    
81
			client.connect();
82

    
83
			if (!solrAdministrator.indexCollectionExists(mdref.toString(), client)) {
84
				Map<String, String> params = Maps.newHashMap();
85

    
86
				final Map<String, String> p = getServiceProperties();
87
				params.put("numShards", p.get("numShards"));
88
				params.put("replicationFactor", p.get("replicationFactor"));
89

    
90
				for (IndexConfigFactory.CONFIG_PARAMS param_Name : IndexConfigFactory.CONFIG_PARAMS.values()) {
91
					params.put(param_Name.toString(), p.get(param_Name.toString()));
92
				}
93

    
94
				zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdref.toString(), parse(fields), params, true);
95
				solrAdministrator.createSolrIndex(p.get("host"), p.get("port"), mdref.toString(), p.get("numShards"), p.get("replicationFactor"),
96
						mdref.toString(), p.get("maxShardsPerNode"));
97
			}
98
			client.getZkStateReader().close();
99

    
100
		} catch (Exception e) {
101
			log.error("Error on creating IndexCollection", e);
102
			throw new IndexServiceException("Error on creating IndexCollection", e);
103
		}
104
	}
105

    
106
	@Override
107
	public void updateIndexCollection(final MetadataReference mdRef, final Document fields) throws IndexServiceException {
108
		try(CloudSolrClient client = getClient()) {
109

    
110
			client.connect();
111
			Map<String, String> params = Maps.newHashMap();
112

    
113
			params.put("numShards", getServiceProperties().get("numShards"));
114
			params.put("replicationFactor", getServiceProperties().get("replicationFactor"));
115

    
116
			for (IndexConfigFactory.CONFIG_PARAMS param_Name : IndexConfigFactory.CONFIG_PARAMS.values()) {
117
				params.put(param_Name.toString(), getServiceProperties().get(param_Name.toString()));
118
			}
119

    
120
			zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdRef.toString(), fields, params, true);
121
			client.getZkStateReader().close();
122
			solrAdministrator.reloadCollection(getServiceProperties().get("host"), getServiceProperties().get("port"), mdRef.toString());
123

    
124
		} catch (Exception e) {
125
			log.error("Error on updating IndexCollection", e);
126
			throw new IndexServiceException("Error on updating IndexCollection", e);
127
		}
128
	}
129

    
130
	/**
131
	 * Parses the fields parameter.
132
	 * 
133
	 * @param fields
134
	 *            the fields
135
	 * @return the document
136
	 * @throws IndexServiceException
137
	 *             the index service exception
138
	 */
139
	private Document parse(final String fields) throws IndexServiceException {
140
		try {
141
			return new SAXReader().read(new StringReader(fields));
142
		} catch (DocumentException e) {
143
			throw new IndexServiceException("unable to parse fields: " + fields, e);
144
		}
145
	}
146

    
147
	/**
148
	 * {@inheritDoc}
149
	 * 
150
	 * @see eu.dnetlib.index.IndexServerDAO#getIndexCollection(eu.dnetlib.index.utils.MetadataReference)
151
	 */
152
	@Override
153
	public IndexCollection getIndexCollection(final MetadataReference mdref) throws IndexServiceException {
154
		CloudSolrClient client = getClient(mdref);
155
		return new SolrIndexCollection(client);
156
	}
157

    
158
	/**
159
	 * {@inheritDoc}
160
	 * 
161
	 * @see eu.dnetlib.index.IndexServerDAO#getSchema(MetadataReference)
162
	 */
163
	@Override
164
	public Map<String, ValueType> getSchema(final MetadataReference mdRef) throws IndexServiceException {
165
		CloudSolrClient client = getClient(mdRef);
166
		Map<String, ValueType> fields = solrAdministrator.getFieldNamesAndTypes(mdRef.toString(), client);
167
		shutdown(mdRef);
168
		return fields;
169
	}
170

    
171
	/**
172
	 * {@inheritDoc}
173
	 * 
174
	 * @see eu.dnetlib.index.IndexServerDAO#getCqlValueTransformerMap(MetadataReference)
175
	 */
176
	@Override
177
	public CqlValueTransformerMap getCqlValueTransformerMap(final MetadataReference mdRef) throws IndexServiceException {
178
		return tMapFactory.getIt(getSchema(mdRef));
179
	}
180

    
181
	/**
182
	 * {@inheritDoc}
183
	 * 
184
	 * @see eu.dnetlib.index.IndexServerDAO#getDocumentMapperFactory()
185
	 */
186
	@Override
187
	public DocumentMapperFactory getDocumentMapperFactory() throws IndexServiceException {
188
		return solrDocumentMapperFactory;
189
	}
190

    
191
	/**
192
	 * {@inheritDoc}
193
	 * 
194
	 * @see eu.dnetlib.index.IndexServerDAO#shutdown(MetadataReference)
195
	 */
196
	@Override
197
	public void shutdown(final MetadataReference mdRef) throws IndexServiceException {
198
		try {
199
			getClient(mdRef).close();
200
		} catch (IOException e) {
201
			throw new IndexServiceException(e);
202
		}
203

    
204
	}
205

    
206
	/**
207
	 * Gets a server with the default collection set according to the given mdRef.
208
	 * 
209
	 * @param mdRef
210
	 *            the md ref
211
	 * @return a server instance
212
	 * @throws IndexServiceException
213
	 *             the index service exception
214
	 */
215
	private CloudSolrClient getClient(final MetadataReference mdRef) throws IndexServiceException {
216
		CloudSolrClient client = getClient();
217
		client.setDefaultCollection(mdRef.toString());
218
		return client;
219
	}
220

    
221
	/**
222
	 * Gets the server.
223
	 * 
224
	 * @return a server instance
225
	 */
226
	private CloudSolrClient getClient() {
227
		String address = getEndpoint().get(ADDRESS);
228
		log.info("connecting to address: " + address);
229

    
230
		final ZkServers zk = ZkServers.newInstance(address);
231
		return new CloudSolrClient.Builder(zk.getHosts(), zk.getChroot()).build();
232
	}
233

    
234
	/**
235
	 * Gets the solr administrator.
236
	 * 
237
	 * @return the solrAdministrator
238
	 */
239
	public RemoteSolrAdministrator getSolrAdministrator() {
240
		return solrAdministrator;
241
	}
242

    
243
	/**
244
	 * Sets the solr administrator.
245
	 * 
246
	 * @param solrAdministrator
247
	 *            the solrAdministrator to set
248
	 */
249
	@Required
250
	public void setSolrAdministrator(final RemoteSolrAdministrator solrAdministrator) {
251
		this.solrAdministrator = solrAdministrator;
252
	}
253

    
254
	@Override
255
	public IndexQueryFactory getIndexQueryFactory() {
256
		return solrIndexQueryFactory;
257
	}
258

    
259
}
(2-2/2)