Project

General

Profile

1
package eu.dnetlib.index.solr;
2

    
3
import java.io.IOException;
4
import java.util.Collection;
5
import java.util.Iterator;
6
import java.util.List;
7
import java.util.stream.Collectors;
8

    
9
import eu.dnetlib.clients.index.model.document.IndexDocument;
10
import eu.dnetlib.clients.index.utils.IndexFieldUtility;
11
import eu.dnetlib.enabling.tools.DnetStreamSupport;
12
import eu.dnetlib.index.IndexCollection;
13
import eu.dnetlib.index.solr.model.SolrIndexDocument;
14
import eu.dnetlib.rmi.provision.IndexServiceException;
15
import org.apache.commons.lang3.StringUtils;
16
import org.apache.commons.logging.Log;
17
import org.apache.commons.logging.LogFactory;
18
import org.apache.solr.client.solrj.impl.CloudSolrClient;
19
import org.apache.solr.client.solrj.response.UpdateResponse;
20
import org.apache.solr.common.SolrInputDocument;
21

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

    
27
	/**
28
	 * The Constant STATUS_INDEX_OK.
29
	 */
30
	public static final int STATUS_INDEX_OK = 0;
31
	/**
32
	 * The log.
33
	 */
34
	private static final Log log = LogFactory.getLog(SolrIndexCollection.class); // NOPMD by marko on 11/24/08 5:02 PM
35
	/**
36
	 * The client.
37
	 */
38
	private CloudSolrClient client;
39

    
40
	private boolean shutdown = false;
41

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

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

    
67
	/**
68
	 * {@inheritDoc}
69
	 */
70
	@Override
71
	public boolean addAll(final Iterator<IndexDocument> docs) throws IndexServiceException {
72
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
73

    
74
		final List<SolrInputDocument> solrDocs = DnetStreamSupport.generateStreamFromIterator(docs)
75
				.map(doc -> ((SolrIndexDocument) doc).getSolrDocument()).collect(Collectors.toList());
76
		try {
77
			final UpdateResponse response = client.add(solrDocs);
78
			return response.getStatus() == 0;
79
		} catch (final Exception e) {
80
			throw new IndexServiceException("Unable to add document", e);
81
		}
82
	}
83

    
84
	/**
85
	 * {@inheritDoc}
86
	 */
87
	@Override
88
	public boolean addAll(final Collection<IndexDocument> docs) throws IndexServiceException {
89
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
90
		return addAll(docs.iterator());
91
	}
92

    
93
	/**
94
	 * {@inheritDoc}
95
	 */
96
	@Override
97
	public boolean deleteIndex(final String dsId) throws IndexServiceException {
98
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
99
		return doDelete(IndexFieldUtility.DS_ID + " : \"" + dsId + "\"");
100
	}
101

    
102
	/**
103
	 * {@inheritDoc}
104
	 */
105
	@Override
106
	public boolean deleteByQuery(final String query, final String dsId) throws IndexServiceException {
107
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
108
		if (StringUtils.isBlank(dsId)) return doDelete(query);
109
		return doDelete(query + " AND " + IndexFieldUtility.DS_ID + " : \"" + dsId + "\"");
110
	}
111

    
112
	/**
113
	 * Do delete.
114
	 *
115
	 * @param query the query
116
	 * @return true, if do delete
117
	 * @throws IndexServiceException the index service exception
118
	 */
119
	protected boolean doDelete(final String query) throws IndexServiceException {
120
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
121
		try {
122
			log.debug("delete by query: " + query);
123
			return client.deleteByQuery(query).getStatus() == STATUS_INDEX_OK;
124
		} catch (final Exception e) {
125
			throw new IndexServiceException("unable to run delete by query: " + query, e);
126
		}
127
	}
128

    
129
	/**
130
	 * {@inheritDoc}
131
	 */
132
	@Override
133
	public boolean commit() throws IndexServiceException {
134
		if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
135
		try {
136
			log.info("performing commit");
137
			final UpdateResponse rsp = client.commit();
138
			log.info(String.format("commit completed in %s, status %s", rsp.getElapsedTime(), rsp.getStatus()));
139
			return rsp.getStatus() == STATUS_INDEX_OK;
140
		} catch (final Throwable e) {
141
			throw new IndexServiceException("unable to perform index commit", e);
142
		}
143
	}
144

    
145
	@Override
146
	public void shutdown() {
147
		try {
148
			client.close();
149
		} catch (IOException e) {
150
			log.error("Error on closing client", e);
151
		}
152
		shutdown = true;
153
	}
154

    
155
	public boolean isShutdown() {
156
		return shutdown;
157
	}
158

    
159
	public void setShutdown(final boolean shutdown) {
160
		this.shutdown = shutdown;
161
	}
162

    
163
}
(1-1/2)