1
|
package eu.dnetlib.functionality.index.solr.feed;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.net.MalformedURLException;
|
5
|
import java.net.URI;
|
6
|
import java.net.URL;
|
7
|
import java.util.Collection;
|
8
|
import java.util.Iterator;
|
9
|
import java.util.List;
|
10
|
import java.util.regex.Matcher;
|
11
|
import java.util.regex.Pattern;
|
12
|
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.apache.solr.client.solrj.SolrServer;
|
16
|
import org.apache.solr.client.solrj.SolrServerException;
|
17
|
import org.apache.solr.client.solrj.impl.CloudSolrServer;
|
18
|
import org.apache.solr.client.solrj.impl.HttpSolrServer;
|
19
|
import org.apache.solr.client.solrj.response.UpdateResponse;
|
20
|
import org.apache.solr.common.SolrInputDocument;
|
21
|
|
22
|
import com.google.common.base.Splitter;
|
23
|
import com.google.common.collect.Lists;
|
24
|
import com.google.common.hash.HashFunction;
|
25
|
import com.google.common.hash.Hashing;
|
26
|
|
27
|
import eu.dnetlib.miscutils.functional.xml.DnetXsltFunctions;
|
28
|
|
29
|
@Deprecated
|
30
|
public class SolrServerPool {
|
31
|
|
32
|
/**
|
33
|
* logger.
|
34
|
*/
|
35
|
private static final Log log = LogFactory.getLog(SolrServerPool.class); // NOPMD by marko on 11/24/08 5:02 PM
|
36
|
|
37
|
/*
|
38
|
* We run into problems when using the ConcurrentUpdateSolrServer, so we're sticking to the HttpSolrServer.
|
39
|
*/
|
40
|
private final List<HttpSolrServer> updateServerPool = Lists.newArrayList();
|
41
|
|
42
|
private CloudSolrServer cloudServer;
|
43
|
|
44
|
private final HashFunction hash = Hashing.murmur3_32();
|
45
|
|
46
|
public SolrServerPool(final String updateUrlLocal, final String updateUrlList, final String zkHost, final String collection, final boolean localFeeding) {
|
47
|
for (URL url : parseUrlListPattern(updateUrlLocal, updateUrlList, localFeeding)) {
|
48
|
updateServerPool.add(new HttpSolrServer(url + "/" + collection));
|
49
|
}
|
50
|
cloudServer = new CloudSolrServer(zkHost);
|
51
|
cloudServer.setDefaultCollection(collection);
|
52
|
}
|
53
|
|
54
|
public UpdateResponse add(final SolrInputDocument doc) throws SolrServerException, IOException {
|
55
|
return updateServerPool.get(hashPick(doc)).add(doc);
|
56
|
}
|
57
|
|
58
|
public UpdateResponse addAll(final Iterator<SolrInputDocument> docs) throws SolrServerException, IOException {
|
59
|
if (updateServerPool.size() == 1) return updateServerPool.get(0).add(docs);
|
60
|
int i = Integer.parseInt(DnetXsltFunctions.randomInt(updateServerPool.size()));
|
61
|
return updateServerPool.get(i).add(docs);
|
62
|
}
|
63
|
|
64
|
public UpdateResponse addAll(final Collection<SolrInputDocument> docs) throws SolrServerException, IOException {
|
65
|
if (updateServerPool.size() == 1) return updateServerPool.get(0).add(docs);
|
66
|
int i = Integer.parseInt(DnetXsltFunctions.randomInt(updateServerPool.size()));
|
67
|
return updateServerPool.get(i).add(docs);
|
68
|
}
|
69
|
|
70
|
public void deleteByQuery(final String query) throws SolrServerException, IOException {
|
71
|
cloudServer.deleteByQuery(query);
|
72
|
}
|
73
|
|
74
|
public void commitAll() throws SolrServerException, IOException {
|
75
|
cloudServer.commit();
|
76
|
}
|
77
|
|
78
|
public void shutdownAll() throws SolrServerException {
|
79
|
cloudServer.shutdown();
|
80
|
for (SolrServer server : updateServerPool) {
|
81
|
server.shutdown();
|
82
|
}
|
83
|
}
|
84
|
|
85
|
// //////////////////
|
86
|
|
87
|
private int hashPick(final SolrInputDocument doc) {
|
88
|
final int hashCode = hash.hashBytes(doc.getFieldValue("__indexrecordidentifier").toString().getBytes()).asInt();
|
89
|
return Math.abs(hashCode) % updateServerPool.size();
|
90
|
}
|
91
|
|
92
|
public List<URL> parseUrlListPattern(final String local, final String list, final boolean localFeeding) {
|
93
|
final List<URL> res = Lists.newArrayList();
|
94
|
try {
|
95
|
if (localFeeding) {
|
96
|
res.add(new URL(local));
|
97
|
} else {
|
98
|
Matcher matcher = Pattern.compile("(^.*)\\[(\\d+)\\.\\.(\\d+)\\](.*$)").matcher(list);
|
99
|
if (matcher.matches()) {
|
100
|
final String prefix = matcher.group(1);
|
101
|
int lb = Integer.parseInt(matcher.group(2));
|
102
|
int ub = Integer.parseInt(matcher.group(3));
|
103
|
final String suffix = matcher.group(4);
|
104
|
|
105
|
for (int i = lb; i <= ub; i++) {
|
106
|
res.add(new URL(prefix + i + suffix));
|
107
|
}
|
108
|
}
|
109
|
}
|
110
|
} catch (MalformedURLException e) {
|
111
|
throw new IllegalArgumentException("invalid url list: " + list, e);
|
112
|
}
|
113
|
|
114
|
log.info("parsed url(s): " + res);
|
115
|
return res;
|
116
|
}
|
117
|
|
118
|
public List<URL> parseUrlList(final String list) throws MalformedURLException {
|
119
|
final List<URL> res = Lists.newArrayList();
|
120
|
for (final String url : Splitter.on(",").trimResults().split(list)) {
|
121
|
res.add(URI.create(url).toURL());
|
122
|
}
|
123
|
return res;
|
124
|
}
|
125
|
|
126
|
}
|