Revision 55808
Added by Claudio Atzori almost 5 years ago
SolrIndexServerDAO.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.functionality.index; |
2 | 2 |
|
3 |
import java.io.IOException; |
|
4 |
import java.io.StringReader; |
|
5 |
import java.util.Map; |
|
6 |
|
|
3 |
import com.google.common.collect.Maps; |
|
4 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
|
7 | 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.*; |
|
8 | 14 |
import org.apache.commons.logging.Log; |
9 | 15 |
import org.apache.commons.logging.LogFactory; |
10 |
|
|
11 | 16 |
import org.apache.solr.client.solrj.impl.CloudSolrClient; |
12 | 17 |
import org.dom4j.Document; |
13 | 18 |
import org.dom4j.DocumentException; |
... | ... | |
15 | 20 |
import org.springframework.beans.factory.annotation.Autowired; |
16 | 21 |
import org.springframework.beans.factory.annotation.Required; |
17 | 22 |
|
18 |
import com.google.common.collect.Maps; |
|
23 |
import java.io.IOException; |
|
24 |
import java.io.StringReader; |
|
25 |
import java.util.Map; |
|
19 | 26 |
|
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 | 27 |
/** |
35 | 28 |
* The Class SolrIndexServerDAO. |
36 | 29 |
*/ |
... | ... | |
71 | 64 |
*/ |
72 | 65 |
@Override |
73 | 66 |
public void createIndexCollection(final MetadataReference mdref, final String fields) throws IndexServiceException { |
74 |
CloudSolrClient client = null; |
|
75 |
try { |
|
76 |
client = getClient(); |
|
67 |
try(CloudSolrClient client = getClient()) { |
|
68 |
|
|
77 | 69 |
client.connect(); |
78 | 70 |
|
79 | 71 |
if (!solrAdministrator.indexCollectionExists(mdref.toString(), client)) { |
... | ... | |
96 | 88 |
} catch (Exception e) { |
97 | 89 |
log.error("Error on creating IndexCollection", e); |
98 | 90 |
throw new IndexServiceException("Error on creating IndexCollection", e); |
99 |
} finally { |
|
100 |
if (client != null) { |
|
101 |
shutdown(mdref); |
|
102 |
} |
|
103 | 91 |
} |
104 | 92 |
} |
105 | 93 |
|
106 | 94 |
@Override |
107 | 95 |
public void updateIndexCollection(final MetadataReference mdRef, final Document fields) throws IndexServiceException { |
108 |
CloudSolrClient client = null; |
|
109 |
try { |
|
110 |
client = getClient(); |
|
96 |
try(CloudSolrClient client = getClient()) { |
|
97 |
|
|
111 | 98 |
client.connect(); |
112 | 99 |
Map<String, String> params = Maps.newHashMap(); |
113 | 100 |
|
... | ... | |
120 | 107 |
|
121 | 108 |
zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdRef.toString(), fields, params, true); |
122 | 109 |
client.getZkStateReader().close(); |
123 |
shutdown(mdRef); |
|
124 | 110 |
solrAdministrator.reloadCollection(getServiceProperties().get("host"), getServiceProperties().get("port"), mdRef.toString()); |
125 | 111 |
|
126 | 112 |
} catch (Exception e) { |
127 | 113 |
log.error("Error on updating IndexCollection", e); |
128 | 114 |
throw new IndexServiceException("Error on updating IndexCollection", e); |
129 |
} finally { |
|
130 |
if (client != null) { |
|
131 |
shutdown(mdRef); |
|
132 |
} |
|
133 | 115 |
} |
134 | 116 |
} |
135 | 117 |
|
... | ... | |
157 | 139 |
*/ |
158 | 140 |
@Override |
159 | 141 |
public IndexCollection getIndexCollection(final MetadataReference mdref) throws IndexServiceException { |
160 |
final CloudSolrClient client = getClient(mdref);
|
|
142 |
CloudSolrClient client = getClient(mdref); |
|
161 | 143 |
return new SolrIndexCollection(client); |
162 | 144 |
} |
163 | 145 |
|
... | ... | |
206 | 188 |
} catch (IOException e) { |
207 | 189 |
throw new IndexServiceException(e); |
208 | 190 |
} |
191 |
|
|
209 | 192 |
} |
210 | 193 |
|
211 | 194 |
/** |
... | ... | |
218 | 201 |
* the index service exception |
219 | 202 |
*/ |
220 | 203 |
private CloudSolrClient getClient(final MetadataReference mdRef) throws IndexServiceException { |
221 |
CloudSolrClient server = getClient();
|
|
222 |
server.setDefaultCollection(mdRef.toString());
|
|
223 |
return server;
|
|
204 |
CloudSolrClient client = getClient();
|
|
205 |
client.setDefaultCollection(mdRef.toString());
|
|
206 |
return client;
|
|
224 | 207 |
} |
225 | 208 |
|
226 | 209 |
/** |
... | ... | |
229 | 212 |
* @return a server instance |
230 | 213 |
*/ |
231 | 214 |
private CloudSolrClient getClient() { |
232 |
final String address = getEndpoint().get(ADDRESS);
|
|
215 |
String address = getEndpoint().get(ADDRESS); |
|
233 | 216 |
log.info("connecting to address: " + address); |
234 |
return new CloudSolrClient.Builder() |
|
235 |
.withZkHost(address)
|
|
236 |
.build();
|
|
217 |
|
|
218 |
final ZkServers zk = ZkServers.newInstance(address);
|
|
219 |
return new CloudSolrClient.Builder(zk.getHosts(), zk.getChroot()).build();
|
|
237 | 220 |
} |
238 | 221 |
|
239 | 222 |
/** |
Also available in: Unified diff
reintegrated branch solr75 -r53828:HEAD