Project

General

Profile

1 41977 sandro.lab
package eu.dnetlib.index.actors;
2
3 52694 alessia.ba
import java.util.Map;
4 41977 sandro.lab
import java.util.concurrent.ExecutorService;
5
import java.util.concurrent.Executors;
6
import java.util.function.Function;
7 57157 enrico.ott
import java.util.stream.Stream;
8
import java.util.stream.StreamSupport;
9 41977 sandro.lab
10 52694 alessia.ba
import com.google.common.collect.Maps;
11 57157 enrico.ott
12
import eu.dnetlib.clients.index.model.Any.ValueType;
13 41977 sandro.lab
import eu.dnetlib.clients.index.model.document.IndexDocument;
14
import eu.dnetlib.clients.index.utils.IndexFieldUtility;
15
import eu.dnetlib.clients.index.utils.ServiceTools;
16 52694 alessia.ba
import eu.dnetlib.cql.CqlTranslator;
17 41977 sandro.lab
import eu.dnetlib.enabling.tools.DnetStreamSupport;
18
import eu.dnetlib.index.IndexCollection;
19
import eu.dnetlib.index.IndexServerDAO;
20
import eu.dnetlib.index.IndexServerDAOMap;
21
import eu.dnetlib.index.feed.DocumentFeeder;
22
import eu.dnetlib.index.feed.DocumentMapperFactory;
23
import eu.dnetlib.index.feed.FeedMode;
24
import eu.dnetlib.index.feed.FeedResult;
25 52694 alessia.ba
import eu.dnetlib.index.utils.IndexDateUtility;
26 41977 sandro.lab
import eu.dnetlib.miscutils.datetime.DateUtils;
27
import eu.dnetlib.rmi.provision.IndexServiceException;
28 52694 alessia.ba
import eu.dnetlib.utils.MetadataReference;
29 41977 sandro.lab
import org.apache.commons.logging.Log;
30
import org.apache.commons.logging.LogFactory;
31
32
/**
33
 * The Class IndexFeedActorImpl.
34
 */
35
public class IndexFeedActorImpl implements IndexFeedActor {
36
37
	/**
38
	 * The Constant log.
39
	 */
40
	private static final Log log = LogFactory.getLog(IndexFeedActorImpl.class); // NOPMD by marko on 11/24/08 5:02 PM
41
42
	/**
43
	 * The index server dao map.
44
	 */
45
	private final transient IndexServerDAOMap indexServerDAOMap;
46
47
	/**
48
	 * The service tools.
49
	 */
50
	private final transient ServiceTools serviceTools;
51
	/**
52
	 * Thread pool used for the feeding process.
53
	 */
54
	private final transient ExecutorService threadPool = Executors.newCachedThreadPool();
55
	/**
56
	 * CqlTranslator.
57
	 */
58
	private CqlTranslator translator;
59
60
	/**
61
	 * Instantiates a new index feed actor impl.
62
	 *
63
	 * @param indexServerDAOMap the index server dao map
64
	 * @param serviceTools      the service tools
65
	 */
66
	public IndexFeedActorImpl(final IndexServerDAOMap indexServerDAOMap, final ServiceTools serviceTools, final CqlTranslator translator) {
67
		super();
68
		this.indexServerDAOMap = indexServerDAOMap;
69
		this.serviceTools = serviceTools;
70
		this.translator = translator;
71
	}
72
73
	/**
74
	 * {@inheritDoc}
75
	 */
76
	@Override
77
	public void feedIndex(final String dsId,
78
			final FeedMode feedMode,
79
			final Iterable<String> docIterator,
80
			final ResultsetKeepAliveCallback startCallback,
81
			final BlackboardActorCallback endCallback,
82
			final String backendId, final boolean emptyResult) {
83
		IndexCollection indexCollection = null;
84
		try {
85
			final MetadataReference mdref = serviceTools.getMetadataRef(dsId);
86
87
			final IndexServerDAO serverDAO = indexServerDAOMap.getIndexServerDAO(backendId);
88
			final DocumentMapperFactory docMapperFactory = serverDAO.getDocumentMapperFactory();
89 50565 alessia.ba
            final String version = IndexDateUtility.getParsedDateField(DateUtils.now_ISO8601());
90 57157 enrico.ott
            Map<String, ValueType> fields = serverDAO.getSchema(mdref);
91
            final Function<String, IndexDocument> docMapper = docMapperFactory.getRecordMapper(fields, mdref, dsId, version, emptyResult);
92
93
			Stream<IndexDocument> docStream = DnetStreamSupport.generateStreamFromIterator(docIterator.iterator()).map(docMapper);
94
95 41977 sandro.lab
			indexCollection = serverDAO.getIndexCollection(mdref);
96 57157 enrico.ott
			final FeedResult res = threadPool.submit(new DocumentFeeder(indexCollection, docStream)).get();
97
98 41977 sandro.lab
			cleanMarkedDocuments(indexCollection, dsId);
99
			if (feedMode.equals(FeedMode.REFRESH)) {
100 50565 alessia.ba
                deleteByVersion(indexCollection, dsId, version);
101
            }
102 41977 sandro.lab
103
			indexCollection.commit();
104
			indexCollection.shutdown();
105
106
			log.info("FeedResult: " + res.setTimeElapsed(System.currentTimeMillis()));
107 52694 alessia.ba
			endCallback.setJobDone(buildParams(res));
108 41977 sandro.lab
		} catch (final Exception e) {
109
			endCallback.setJobFailed(e);
110
			log.error("feed index job failed", e);
111
		} finally {
112
			if (indexCollection != null) {
113
				indexCollection.shutdown();
114
			}
115
		}
116
117
	}
118
119 52694 alessia.ba
	private Map<String, String> buildParams(final FeedResult feedResult) {
120
		Map<String, String> params = Maps.newHashMap();
121
		params.put("added", String.valueOf(feedResult.getAdded()));
122
		params.put("skipped", String.valueOf(feedResult.getSkipped()));
123
		params.put("marked", String.valueOf(feedResult.getMarked()));
124
		params.put("time", String.valueOf(feedResult.getTime()));
125
		return params;
126
	}
127
128 41977 sandro.lab
	/**
129
	 * mdFormatVersion.
130
	 *
131
	 * @param indexCollection the server dao
132
	 * @param dsId            the ds id
133
	 * @throws IndexServiceException the index service exception
134
	 */
135 44419 claudio.at
	private void deleteByVersion(final IndexCollection indexCollection, final String dsId, final String version) throws IndexServiceException {
136 44824 sandro.lab
        final String query = String.format("%s:[* TO \"%s\"}", IndexFieldUtility.DS_VERSION, version);
137
        indexCollection.deleteByQuery(query, dsId);
138 41977 sandro.lab
	}
139
140
	/**
141
	 * method delete documents where IndexMap.DELETE_DOCUMENT field is true
142
	 *
143
	 * @param indexCollection the server dao
144
	 * @param dsId            the ds id
145
	 * @throws IndexServiceException the index service exception
146
	 */
147
	public void cleanMarkedDocuments(final IndexCollection indexCollection, final String dsId) throws IndexServiceException {
148 44420 claudio.at
		final String query = IndexFieldUtility.DELETE_DOCUMENT + ":true ";
149
		indexCollection.deleteByQuery(query, dsId);
150 41977 sandro.lab
	}
151
152
}