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
|
* @param queryResponseFactory
|
46
|
* the query response factory
|
47
|
*/
|
48
|
public SolrIndexCollection(final CloudSolrServer newServer) {
|
49
|
this.server = newServer;
|
50
|
server.connect();
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* {@inheritDoc}
|
55
|
*
|
56
|
* @see eu.dnetlib.functionality.index.IndexCollection#add(eu.dnetlib.functionality.index.model.document.IndexDocument)
|
57
|
*/
|
58
|
@Override
|
59
|
public boolean add(final IndexDocument doc) throws IndexServiceException {
|
60
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
61
|
final SolrIndexDocument solrDocument = (SolrIndexDocument) doc;
|
62
|
try {
|
63
|
final UpdateResponse response = server.add(solrDocument.getSolrDocument());
|
64
|
return response.getStatus() == 0;
|
65
|
} catch (final Exception e) {
|
66
|
throw new IndexServiceException("Unable to add document", e);
|
67
|
}
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* {@inheritDoc}
|
72
|
*
|
73
|
* @see eu.dnetlib.functionality.index.IndexCollection#addAll(java.util.Iterator)
|
74
|
*/
|
75
|
@Override
|
76
|
public boolean addAll(final Iterator<IndexDocument> docs) throws IndexServiceException {
|
77
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
78
|
final Iterator<SolrInputDocument> solrDocs = Iterators.transform(docs, new Function<IndexDocument, SolrInputDocument>() {
|
79
|
|
80
|
@Override
|
81
|
public SolrInputDocument apply(final IndexDocument doc) {
|
82
|
final SolrIndexDocument solrDocument = (SolrIndexDocument) doc;
|
83
|
return solrDocument.getSolrDocument();
|
84
|
}
|
85
|
});
|
86
|
|
87
|
try {
|
88
|
final UpdateResponse response = server.add(Lists.newArrayList(solrDocs));
|
89
|
return response.getStatus() == 0;
|
90
|
} catch (final Exception e) {
|
91
|
throw new IndexServiceException("Unable to add document", e);
|
92
|
}
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* {@inheritDoc}
|
97
|
*
|
98
|
* @see eu.dnetlib.functionality.index.IndexCollection#addAll(java.util.Collection)
|
99
|
*/
|
100
|
@Override
|
101
|
public boolean addAll(final Collection<IndexDocument> docs) throws IndexServiceException {
|
102
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
103
|
return addAll(docs.iterator());
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* {@inheritDoc}
|
108
|
*
|
109
|
* @see eu.dnetlib.functionality.index.IndexCollection#deleteIndex(java.lang.String)
|
110
|
*/
|
111
|
@Override
|
112
|
public boolean deleteIndex(final String dsId) throws IndexServiceException {
|
113
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
114
|
return doDelete(IndexFieldUtility.DS_ID + " : \"" + dsId + "\"");
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* {@inheritDoc}
|
119
|
*
|
120
|
* @see eu.dnetlib.functionality.index.IndexCollection#deleteByQuery(java.lang.String, java.lang.String)
|
121
|
*/
|
122
|
@Override
|
123
|
public boolean deleteByQuery(final String query, final String dsId) throws IndexServiceException {
|
124
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
125
|
if (StringUtils.isBlank(dsId)) return doDelete(query);
|
126
|
return doDelete(query + " AND " + IndexFieldUtility.DS_ID + " : \"" + dsId + "\"");
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Do delete.
|
131
|
*
|
132
|
* @param query
|
133
|
* the query
|
134
|
* @return true, if do delete
|
135
|
* @throws IndexServiceException
|
136
|
* the index service exception
|
137
|
*/
|
138
|
protected boolean doDelete(final String query) throws IndexServiceException {
|
139
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
140
|
try {
|
141
|
log.debug("delete by query: " + query);
|
142
|
return server.deleteByQuery(query).getStatus() == STATUS_INDEX_OK;
|
143
|
} catch (final Exception e) {
|
144
|
throw new IndexServiceException("unable to run delete by query: " + query, e);
|
145
|
}
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* {@inheritDoc}
|
150
|
*
|
151
|
* @see eu.dnetlib.functionality.index.IndexCollection#commit()
|
152
|
*/
|
153
|
@Override
|
154
|
public boolean commit() throws IndexServiceException {
|
155
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
156
|
try {
|
157
|
log.info("performing commit");
|
158
|
final UpdateResponse rsp = server.commit();
|
159
|
log.info(String.format("commit completed in %s, status %s", rsp.getElapsedTime(), rsp.getStatus()));
|
160
|
return rsp.getStatus() == STATUS_INDEX_OK;
|
161
|
} catch (final Throwable e) {
|
162
|
throw new IndexServiceException("unable to perform index commit", e);
|
163
|
}
|
164
|
}
|
165
|
|
166
|
@Override
|
167
|
public void shutdown() {
|
168
|
server.shutdown();
|
169
|
shutdown = true;
|
170
|
}
|
171
|
|
172
|
public boolean isShutdown() {
|
173
|
return shutdown;
|
174
|
}
|
175
|
|
176
|
public void setShutdown(final boolean shutdown) {
|
177
|
this.shutdown = shutdown;
|
178
|
}
|
179
|
|
180
|
}
|