Project

General

Profile

1
package eu.dnetlib.functionality.index;
2

    
3
import java.io.IOException;
4
import java.util.Collection;
5
import java.util.Iterator;
6

    
7
import org.apache.commons.lang.StringUtils;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.apache.solr.client.solrj.impl.CloudSolrClient;
11
import org.apache.solr.client.solrj.response.UpdateResponse;
12
import org.apache.solr.common.SolrInputDocument;
13

    
14
import com.google.common.base.Function;
15
import com.google.common.collect.Iterators;
16
import com.google.common.collect.Lists;
17

    
18
import eu.dnetlib.data.provision.index.rmi.IndexServiceException;
19
import eu.dnetlib.functionality.index.model.document.IndexDocument;
20
import eu.dnetlib.functionality.index.model.util.SolrIndexDocument;
21
import eu.dnetlib.functionality.index.utils.IndexFieldUtility;
22

    
23
/**
24
 * The Class SolrIndexCollection.
25
 */
26
public class SolrIndexCollection implements IndexCollection {
27

    
28
	/**
29
	 * The log.
30
	 */
31
	private static final Log log = LogFactory.getLog(SolrIndexCollection.class); // NOPMD by marko on 11/24/08 5:02 PM
32

    
33
	/** The Constant STATUS_INDEX_OK. */
34
	public static final int STATUS_INDEX_OK = 0;
35

    
36
	/** The server. */
37
	private CloudSolrClient client;
38

    
39
	private boolean shutdown = false;
40

    
41
	/**
42
	 * The Constructor.
43
	 *
44
	 * @param client
45
	 *            the client
46
	 */
47
	public SolrIndexCollection(final CloudSolrClient client) {
48
		this.client = client;
49
		client.connect();
50
	}
51

    
52
	/**
53
	 * {@inheritDoc}
54
	 *
55
	 * @see eu.dnetlib.functionality.index.IndexCollection#add(eu.dnetlib.functionality.index.model.document.IndexDocument)
56
	 */
57
	@Override
58
	public boolean add(final IndexDocument doc) throws IndexServiceException {
59
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
60
		final SolrIndexDocument solrDocument = (SolrIndexDocument) doc;
61
		try {
62
			final UpdateResponse response = client.add(solrDocument.getSolrDocument());
63
			return response.getStatus() == 0;
64
		} catch (final Exception e) {
65
			throw new IndexServiceException("Unable to add document", e);
66
		}
67
	}
68

    
69
	/**
70
	 * {@inheritDoc}
71
	 *
72
	 * @see eu.dnetlib.functionality.index.IndexCollection#addAll(java.util.Iterator)
73
	 */
74
	@Override
75
	public boolean addAll(final Iterator<IndexDocument> docs) throws IndexServiceException {
76
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
77
		final Iterator<SolrInputDocument> solrDocs = Iterators.transform(docs, new Function<IndexDocument, SolrInputDocument>() {
78

    
79
			@Override
80
			public SolrInputDocument apply(final IndexDocument doc) {
81
				final SolrIndexDocument solrDocument = (SolrIndexDocument) doc;
82
				return solrDocument.getSolrDocument();
83
			}
84
		});
85

    
86
		try {
87
			final UpdateResponse response = client.add(Lists.newArrayList(solrDocs));
88
			return response.getStatus() == 0;
89
		} catch (final Exception e) {
90
			throw new IndexServiceException("Unable to add document", e);
91
		}
92
	}
93

    
94
	/**
95
	 * {@inheritDoc}
96
	 *
97
	 * @see eu.dnetlib.functionality.index.IndexCollection#addAll(java.util.Collection)
98
	 */
99
	@Override
100
	public boolean addAll(final Collection<IndexDocument> docs) throws IndexServiceException {
101
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
102
		return addAll(docs.iterator());
103
	}
104

    
105
	/**
106
	 * {@inheritDoc}
107
	 *
108
	 * @see eu.dnetlib.functionality.index.IndexCollection#deleteIndex(java.lang.String)
109
	 */
110
	@Override
111
	public boolean deleteIndex(final String dsId) throws IndexServiceException {
112
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
113
		return doDelete(IndexFieldUtility.DS_ID + " : \"" + dsId + "\"");
114
	}
115

    
116
	/**
117
	 * {@inheritDoc}
118
	 *
119
	 * @see eu.dnetlib.functionality.index.IndexCollection#deleteByQuery(java.lang.String, java.lang.String)
120
	 */
121
	@Override
122
	public boolean deleteByQuery(final String query, final String dsId) throws IndexServiceException {
123
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
124
		if (StringUtils.isBlank(dsId)) return doDelete(query);
125
		return doDelete(query + " AND " + IndexFieldUtility.DS_ID + " : \"" + dsId + "\"");
126
	}
127

    
128
	/**
129
	 * Do delete.
130
	 *
131
	 * @param query
132
	 *            the query
133
	 * @return true, if do delete
134
	 * @throws IndexServiceException
135
	 *             the index service exception
136
	 */
137
	protected boolean doDelete(final String query) throws IndexServiceException {
138
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
139
		try {
140
			log.debug("delete by query: " + query);
141
			return client.deleteByQuery(query).getStatus() == STATUS_INDEX_OK;
142
		} catch (final Exception e) {
143
			throw new IndexServiceException("unable to run delete by query: " + query, e);
144
		}
145
	}
146

    
147
	/**
148
	 * {@inheritDoc}
149
	 *
150
	 * @see eu.dnetlib.functionality.index.IndexCollection#commit()
151
	 */
152
	@Override
153
	public boolean commit() throws IndexServiceException {
154
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
155
		try {
156
			log.info("performing commit");
157
			final UpdateResponse rsp = client.commit();
158
			log.info(String.format("commit completed in %s, status %s", rsp.getElapsedTime(), rsp.getStatus()));
159
			return rsp.getStatus() == STATUS_INDEX_OK;
160
		} catch (final Throwable e) {
161
			throw new IndexServiceException("unable to perform index commit", e);
162
		}
163
	}
164

    
165
	@Override
166
	public void shutdown() {
167
		try {
168
			client.close();
169
		} catch (IOException e) {
170
			throw new RuntimeException(e);
171
		}
172
		shutdown = true;
173
	}
174

    
175
	public boolean isShutdown() {
176
		return shutdown;
177
	}
178

    
179
	public void setShutdown(final boolean shutdown) {
180
		this.shutdown = shutdown;
181
	}
182

    
183
}
(1-1/2)