Revision 49044
Added by Claudio Atzori over 6 years ago
CloudIndexClient.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.index; |
2 | 2 |
|
3 |
import java.io.Closeable; |
|
3 | 4 |
import java.io.IOException; |
4 | 5 |
import java.text.SimpleDateFormat; |
5 | 6 |
import java.util.Date; |
... | ... | |
20 | 21 |
/** |
21 | 22 |
* Created by michele on 11/11/15. |
22 | 23 |
*/ |
23 |
public class CloudIndexClient { |
|
24 |
public class CloudIndexClient implements Closeable {
|
|
24 | 25 |
|
25 | 26 |
private static final Log log = LogFactory.getLog(CloudIndexClient.class); |
26 | 27 |
private static final String INDEX_RECORD_RESULT_FIELD = "dnetResult"; |
27 | 28 |
|
28 |
private final CloudSolrClient solrServer;
|
|
29 |
private final CloudSolrClient solrClient;
|
|
29 | 30 |
|
30 | 31 |
protected CloudIndexClient(final CloudSolrClient solrServer) { |
31 |
this.solrServer = solrServer;
|
|
32 |
this.solrClient = solrServer;
|
|
32 | 33 |
} |
33 | 34 |
|
34 | 35 |
public int feed(final String record, final String indexDsId, final UnaryFunction<String, String> toIndexRecord) throws CloudIndexClientException { |
... | ... | |
52 | 53 |
|
53 | 54 |
public int feed(final SolrInputDocument document, final boolean commit) throws CloudIndexClientException { |
54 | 55 |
try { |
55 |
final UpdateResponse res = solrServer.add(document);
|
|
56 |
final UpdateResponse res = solrClient.add(document);
|
|
56 | 57 |
log.debug("feed time for single records, elapsed time: " + HumanTime.exactly(res.getElapsedTime())); |
57 | 58 |
if (res.getStatus() != 0) { throw new CloudIndexClientException("bad status: " + res.getStatus()); } |
58 | 59 |
if (commit) { |
59 |
solrServer.commit();
|
|
60 |
solrClient.commit();
|
|
60 | 61 |
} |
61 | 62 |
return res.getStatus(); |
62 | 63 |
} catch (final Throwable e) { |
... | ... | |
77 | 78 |
} |
78 | 79 |
return; |
79 | 80 |
} |
80 |
final UpdateResponse res = solrServer.add(docs);
|
|
81 |
final UpdateResponse res = solrClient.add(docs);
|
|
81 | 82 |
|
82 | 83 |
log.debug("feed time for " + docs.size() + " records, elapsed tipe: : " + HumanTime.exactly(res.getElapsedTime())); |
83 | 84 |
|
84 | 85 |
if (commit) { |
85 |
solrServer.commit();
|
|
86 |
solrClient.commit();
|
|
86 | 87 |
} |
87 | 88 |
if (callback != null) { |
88 | 89 |
callback.doAfterFeeding(res); |
... | ... | |
122 | 123 |
|
123 | 124 |
public int remove(final String id, final boolean commit) throws CloudIndexClientException { |
124 | 125 |
try { |
125 |
final UpdateResponse res = solrServer.deleteByQuery("objidentifier:\"" + id + "\"");
|
|
126 |
final UpdateResponse res = solrClient.deleteByQuery("objidentifier:\"" + id + "\"");
|
|
126 | 127 |
if (commit) { |
127 |
solrServer.commit();
|
|
128 |
solrClient.commit();
|
|
128 | 129 |
} |
129 | 130 |
return res.getResponse().size(); |
130 | 131 |
} catch (final Throwable e) { |
... | ... | |
144 | 145 |
if(rows != null && rows >= 0) { |
145 | 146 |
solrQuery.setRows(rows); |
146 | 147 |
} |
147 |
return solrServer.query(solrQuery);
|
|
148 |
return solrClient.query(solrQuery);
|
|
148 | 149 |
} catch (final Throwable e) { |
149 | 150 |
throw new CloudIndexClientException("Error searching documents", e); |
150 | 151 |
} |
151 | 152 |
} |
152 | 153 |
|
153 | 154 |
public void close() throws IOException { |
154 |
if (solrServer != null) {
|
|
155 |
solrServer.close();
|
|
155 |
if (solrClient != null) {
|
|
156 |
solrClient.close();
|
|
156 | 157 |
} |
157 | 158 |
} |
158 | 159 |
|
Also available in: Unified diff
do not close the client too early