1
|
package eu.dnetlib.functionality.index;
|
2
|
|
3
|
import java.util.Collection;
|
4
|
import java.util.Iterator;
|
5
|
|
6
|
import org.apache.commons.lang3.StringUtils;
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.apache.solr.client.solrj.impl.CloudSolrServer;
|
10
|
import org.apache.solr.client.solrj.response.UpdateResponse;
|
11
|
import org.apache.solr.common.SolrInputDocument;
|
12
|
|
13
|
import com.google.common.base.Function;
|
14
|
import com.google.common.collect.Iterators;
|
15
|
import com.google.common.collect.Lists;
|
16
|
|
17
|
import eu.dnetlib.data.provision.index.rmi.IndexServiceException;
|
18
|
import eu.dnetlib.functionality.index.model.document.IndexDocument;
|
19
|
import eu.dnetlib.functionality.index.model.util.SolrIndexDocument;
|
20
|
import eu.dnetlib.functionality.index.utils.IndexFieldUtility;
|
21
|
|
22
|
/**
|
23
|
* The Class SolrIndexCollection.
|
24
|
*/
|
25
|
public class SolrIndexCollection implements IndexCollection {
|
26
|
|
27
|
/**
|
28
|
* The log.
|
29
|
*/
|
30
|
private static final Log log = LogFactory.getLog(SolrIndexCollection.class); // NOPMD by marko on 11/24/08 5:02 PM
|
31
|
|
32
|
/** The Constant STATUS_INDEX_OK. */
|
33
|
public static final int STATUS_INDEX_OK = 0;
|
34
|
|
35
|
/** The server. */
|
36
|
private CloudSolrServer server;
|
37
|
|
38
|
private boolean shutdown = false;
|
39
|
|
40
|
/**
|
41
|
* The Constructor.
|
42
|
*
|
43
|
* @param newServer
|
44
|
* the server
|
45
|
*/
|
46
|
public SolrIndexCollection(final CloudSolrServer newServer) {
|
47
|
this.server = newServer;
|
48
|
server.connect();
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* {@inheritDoc}
|
53
|
*
|
54
|
* @see eu.dnetlib.functionality.index.IndexCollection#add(eu.dnetlib.functionality.index.model.document.IndexDocument)
|
55
|
*/
|
56
|
@Override
|
57
|
public boolean add(final IndexDocument doc) throws IndexServiceException {
|
58
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
59
|
final SolrIndexDocument solrDocument = (SolrIndexDocument) doc;
|
60
|
try {
|
61
|
final UpdateResponse response = server.add(solrDocument.getSolrDocument());
|
62
|
return response.getStatus() == 0;
|
63
|
} catch (final Exception e) {
|
64
|
throw new IndexServiceException("Unable to add document", e);
|
65
|
}
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* {@inheritDoc}
|
70
|
*
|
71
|
* @see eu.dnetlib.functionality.index.IndexCollection#addAll(java.util.Iterator)
|
72
|
*/
|
73
|
@Override
|
74
|
public boolean addAll(final Iterator<IndexDocument> docs) throws IndexServiceException {
|
75
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
76
|
final Iterator<SolrInputDocument> solrDocs = Iterators.transform(docs, new Function<IndexDocument, SolrInputDocument>() {
|
77
|
|
78
|
@Override
|
79
|
public SolrInputDocument apply(final IndexDocument doc) {
|
80
|
final SolrIndexDocument solrDocument = (SolrIndexDocument) doc;
|
81
|
return solrDocument.getSolrDocument();
|
82
|
}
|
83
|
});
|
84
|
|
85
|
try {
|
86
|
final UpdateResponse response = server.add(Lists.newArrayList(solrDocs));
|
87
|
return response.getStatus() == 0;
|
88
|
} catch (final Exception e) {
|
89
|
throw new IndexServiceException("Unable to add document", e);
|
90
|
}
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* {@inheritDoc}
|
95
|
*
|
96
|
* @see eu.dnetlib.functionality.index.IndexCollection#addAll(java.util.Collection)
|
97
|
*/
|
98
|
@Override
|
99
|
public boolean addAll(final Collection<IndexDocument> docs) throws IndexServiceException {
|
100
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
101
|
return addAll(docs.iterator());
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* {@inheritDoc}
|
106
|
*
|
107
|
* @see eu.dnetlib.functionality.index.IndexCollection#deleteIndex(java.lang.String)
|
108
|
*/
|
109
|
@Override
|
110
|
public boolean deleteIndex(final String dsId) throws IndexServiceException {
|
111
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
112
|
return doDelete(IndexFieldUtility.DS_ID + " : \"" + dsId + "\"");
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* {@inheritDoc}
|
117
|
*
|
118
|
* @see eu.dnetlib.functionality.index.IndexCollection#deleteByQuery(java.lang.String, java.lang.String)
|
119
|
*/
|
120
|
@Override
|
121
|
public boolean deleteByQuery(final String query, final String dsId) throws IndexServiceException {
|
122
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
123
|
if (StringUtils.isBlank(dsId)) return doDelete(query);
|
124
|
return doDelete(query + " AND " + IndexFieldUtility.DS_ID + " : \"" + dsId + "\"");
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* Do delete.
|
129
|
*
|
130
|
* @param query
|
131
|
* the query
|
132
|
* @return true, if do delete
|
133
|
* @throws IndexServiceException
|
134
|
* the index service exception
|
135
|
*/
|
136
|
protected boolean doDelete(final String query) throws IndexServiceException {
|
137
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
138
|
try {
|
139
|
log.debug("delete by query: " + query);
|
140
|
return server.deleteByQuery(query).getStatus() == STATUS_INDEX_OK;
|
141
|
} catch (final Exception e) {
|
142
|
throw new IndexServiceException("unable to run delete by query: " + query, e);
|
143
|
}
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* {@inheritDoc}
|
148
|
*
|
149
|
* @see eu.dnetlib.functionality.index.IndexCollection#commit()
|
150
|
*/
|
151
|
@Override
|
152
|
public boolean commit() throws IndexServiceException {
|
153
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
154
|
try {
|
155
|
log.info("performing commit");
|
156
|
final UpdateResponse rsp = server.commit();
|
157
|
log.info(String.format("commit completed in %s, status %s", rsp.getElapsedTime(), rsp.getStatus()));
|
158
|
return rsp.getStatus() == STATUS_INDEX_OK;
|
159
|
} catch (final Throwable e) {
|
160
|
throw new IndexServiceException("unable to perform index commit", e);
|
161
|
}
|
162
|
}
|
163
|
|
164
|
@Override
|
165
|
public void shutdown() {
|
166
|
server.shutdown();
|
167
|
shutdown = true;
|
168
|
}
|
169
|
|
170
|
public boolean isShutdown() {
|
171
|
return shutdown;
|
172
|
}
|
173
|
|
174
|
public void setShutdown(final boolean shutdown) {
|
175
|
this.shutdown = shutdown;
|
176
|
}
|
177
|
|
178
|
}
|