Project

General

Profile

1
package eu.dnetlib.msro.openaireplus.workflows.nodes.index;
2

    
3
import java.io.StringReader;
4
import java.util.ArrayList;
5
import java.util.List;
6
import javax.annotation.Resource;
7

    
8
import com.googlecode.sarasvati.Arc;
9
import com.googlecode.sarasvati.NodeToken;
10
import eu.dnetlib.data.index.CloudIndexClient;
11
import eu.dnetlib.data.index.CloudIndexClientFactory;
12
import eu.dnetlib.data.index.CloudIndexClientException;
13
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
14
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
15
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
16
import eu.dnetlib.msro.rmi.MSROException;
17
import eu.dnetlib.msro.workflows.nodes.AsyncJobNode;
18
import eu.dnetlib.openaire.directindex.api.RecentResultsQueue;
19
import eu.dnetlib.openaire.directindex.utils.OafToIndexRecordFactory;
20

    
21
import org.apache.commons.io.IOUtils;
22
import org.apache.commons.logging.Log;
23
import org.apache.commons.logging.LogFactory;
24
import org.apache.solr.common.SolrInputDocument;
25
import org.dom4j.io.SAXReader;
26
import org.springframework.beans.factory.annotation.Required;
27
import org.springframework.beans.factory.annotation.Value;
28
import org.springframework.core.io.ClassPathResource;
29

    
30
/**
31
 * Created by michele on 15/12/15.
32
 */
33
public class FeedMissingClaimsJobNode extends AsyncJobNode {
34

    
35
	private static final Log log = LogFactory.getLog(FeedMissingClaimsJobNode.class);
36
	public static final int BATCH_SIZE = 1000;
37
	private RecentResultsQueue queue;
38
	private OafToIndexRecordFactory oafToIndexRecordFactory;
39

    
40
	@Resource
41
	private UniqueServiceLocator serviceLocator;
42

    
43
	@Value(value = "${openaire.api.directindex.findSolrIndexUrl.xquery}")
44
	private ClassPathResource findSolrIndexUrl;
45

    
46
	@Override
47
	protected String execute(final NodeToken nodeToken) throws Exception {
48

    
49
		final String format =
50
				nodeToken.getEnv().hasAttribute("format") ? nodeToken.getEnv().getAttribute("format") : nodeToken.getFullEnv().getAttribute("format");
51
		final String coll = format + "-index-openaire";
52
		final String indexDsId = nodeToken.getEnv().getAttribute("index_id");
53
		final String baseUrl = calculateIndexBaseUrl();
54

    
55
		CloudIndexClient idxClient = null;
56

    
57
		try {
58
			final List<SolrInputDocument> toFeed = new ArrayList<SolrInputDocument>();
59
			final List<String> toDeleteFromCache = new ArrayList<String>();
60

    
61
			final SAXReader reader = new SAXReader();
62
			final ApplyXslt xslt = oafToIndexRecordFactory.newTransformer(format);
63

    
64
			idxClient = CloudIndexClientFactory.newIndexClient(baseUrl, coll, false);
65
			log.info("Starting to feed claims in index collection "+coll);
66
			int count = 1;
67
			for (String record : queue) {
68
				final String id = reader.read(new StringReader(record)).valueOf("//*[local-name() = 'objIdentifier']");
69
				if(log.isDebugEnabled()){
70
					log.debug("Processing record "+count++);
71
				}
72
				if (idxClient.isRecordIndexed(id)) {
73
					toDeleteFromCache.add(id);
74
				} else {
75
					toFeed.add(idxClient.prepareSolrDocument(record, indexDsId, xslt));
76
				}
77
				if(count % BATCH_SIZE == 0) processLists(idxClient, toFeed, toDeleteFromCache);
78

    
79
			}
80
			if(!toFeed.isEmpty() || !toDeleteFromCache.isEmpty()) processLists(idxClient, toFeed, toDeleteFromCache);
81
			log.info(String.format("Finished feeding of claims in index collection %s, total: %d", coll, count));
82

    
83
		} catch (Throwable e) {
84
			log.error("Error feeding missing claims", e);
85
			throw new MSROException("Error feeding missing claims: " + e.getMessage(), e);
86
		} finally {
87
			if (idxClient != null) {
88
				idxClient.close();
89
			}
90
		}
91

    
92
		return Arc.DEFAULT_ARC;
93
	}
94

    
95

    
96

    
97
	private void processLists(final CloudIndexClient idxClient, final List<SolrInputDocument> toFeed, final List<String> toDeleteFromCache) throws CloudIndexClientException{
98
		idxClient.feed(toFeed, null);
99
		queue.remove(toDeleteFromCache);
100
		log.info(String.format("%d claims fed and cache cleaned of %d records", toFeed.size(), toDeleteFromCache.size()));
101
		toFeed.clear();
102
		toDeleteFromCache.clear();
103
	}
104

    
105
	public RecentResultsQueue getQueue() {
106
		return queue;
107
	}
108

    
109
	@Required
110
	public void setQueue(final RecentResultsQueue queue) {
111
		this.queue = queue;
112
	}
113

    
114
	public OafToIndexRecordFactory getOafToIndexRecordFactory() {
115
		return oafToIndexRecordFactory;
116
	}
117

    
118
	@Required
119
	public void setOafToIndexRecordFactory(final OafToIndexRecordFactory oafToIndexRecordFactory) {
120
		this.oafToIndexRecordFactory = oafToIndexRecordFactory;
121
	}
122

    
123
	private String calculateIndexBaseUrl() throws Exception {
124
		final String query = IOUtils.toString(findSolrIndexUrl.getInputStream());
125
		return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query);
126
	}
127
}
(3-3/10)