Revision 28577
Added by Claudio Atzori over 10 years ago
modules/cnr-index-solr-common/trunk/src/main/java/eu/dnetlib/functionality/index/solr/feed/SolrServerPool.java | ||
---|---|---|
10 | 10 |
import java.util.regex.Matcher; |
11 | 11 |
import java.util.regex.Pattern; |
12 | 12 |
|
13 |
import org.apache.commons.lang.StringUtils; |
|
13 | 14 |
import org.apache.commons.logging.Log; |
14 | 15 |
import org.apache.commons.logging.LogFactory; |
15 | 16 |
import org.apache.solr.client.solrj.SolrServer; |
... | ... | |
38 | 39 |
*/ |
39 | 40 |
private final List<HttpSolrServer> updateServerPool = Lists.newArrayList(); |
40 | 41 |
|
41 |
private CloudSolrServer cloudServer; |
|
42 |
private CloudSolrServer cloudServer = null;
|
|
42 | 43 |
|
43 | 44 |
private final HashFunction hash = Hashing.murmur3_32(); |
44 | 45 |
|
... | ... | |
46 | 47 |
for (URL url : parseUrlListPattern(updateUrlLocal, updateUrlList, localFeeding)) { |
47 | 48 |
updateServerPool.add(new HttpSolrServer(url + "/" + collection)); |
48 | 49 |
} |
49 |
try { |
|
50 |
cloudServer = new CloudSolrServer(zkHost); |
|
51 |
cloudServer.setDefaultCollection(collection); |
|
52 |
} catch (MalformedURLException e) { |
|
53 |
throw new IllegalArgumentException(e); |
|
50 |
if (!StringUtils.isBlank(zkHost)) { |
|
51 |
try { |
|
52 |
cloudServer = new CloudSolrServer(zkHost); |
|
53 |
cloudServer.setDefaultCollection(collection); |
|
54 |
} catch (MalformedURLException e) { |
|
55 |
throw new IllegalArgumentException(e); |
|
56 |
} |
|
54 | 57 |
} |
55 | 58 |
} |
56 | 59 |
|
... | ... | |
59 | 62 |
} |
60 | 63 |
|
61 | 64 |
public UpdateResponse addAll(final Iterator<SolrInputDocument> docs) throws SolrServerException, IOException { |
62 |
if (updateServerPool.size() == 1) { return updateServerPool.get(0).add(docs); }
|
|
65 |
if (updateServerPool.size() == 1) return updateServerPool.get(0).add(docs);
|
|
63 | 66 |
int i = Integer.parseInt(DnetXsltFunctions.randomInt(updateServerPool.size())); |
64 | 67 |
return updateServerPool.get(i).add(docs); |
65 | 68 |
} |
66 | 69 |
|
67 | 70 |
public UpdateResponse addAll(final Collection<SolrInputDocument> docs) throws SolrServerException, IOException { |
68 |
if (updateServerPool.size() == 1) { return updateServerPool.get(0).add(docs); }
|
|
71 |
if (updateServerPool.size() == 1) return updateServerPool.get(0).add(docs);
|
|
69 | 72 |
int i = Integer.parseInt(DnetXsltFunctions.randomInt(updateServerPool.size())); |
70 | 73 |
return updateServerPool.get(i).add(docs); |
71 | 74 |
} |
72 | 75 |
|
73 | 76 |
public void deleteByQuery(final String query) throws SolrServerException, IOException { |
74 |
cloudServer.deleteByQuery(query); |
|
77 |
if (cloudServer != null) { |
|
78 |
cloudServer.deleteByQuery(query); |
|
79 |
} else { |
|
80 |
updateServerPool.get(0).deleteByQuery(query); |
|
81 |
} |
|
75 | 82 |
} |
76 | 83 |
|
77 | 84 |
public void commitAll() throws SolrServerException, IOException { |
78 |
cloudServer.commit(); |
|
85 |
if (cloudServer != null) { |
|
86 |
cloudServer.commit(); |
|
87 |
} else { |
|
88 |
updateServerPool.get(0).commit(); |
|
89 |
} |
|
79 | 90 |
} |
80 | 91 |
|
81 | 92 |
public void shutdownAll() throws SolrServerException { |
82 |
cloudServer.shutdown(); |
|
93 |
if (cloudServer != null) { |
|
94 |
cloudServer.shutdown(); |
|
95 |
} |
|
83 | 96 |
for (SolrServer server : updateServerPool) { |
84 | 97 |
server.shutdown(); |
85 | 98 |
} |
Also available in: Unified diff
when zkUrl is not defined, fallback to the httpSolrServer