Project

General

Profile

1
package eu.dnetlib.index.actors;
2

    
3
import java.util.Map;
4
import java.util.concurrent.ExecutorService;
5
import java.util.concurrent.Executors;
6
import java.util.function.Function;
7
import java.util.stream.Stream;
8
import java.util.stream.StreamSupport;
9

    
10
import com.google.common.collect.Maps;
11

    
12
import eu.dnetlib.clients.index.model.Any.ValueType;
13
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
import eu.dnetlib.cql.CqlTranslator;
17
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
import eu.dnetlib.index.utils.IndexDateUtility;
26
import eu.dnetlib.miscutils.datetime.DateUtils;
27
import eu.dnetlib.rmi.provision.IndexServiceException;
28
import eu.dnetlib.utils.MetadataReference;
29
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
            final String version = IndexDateUtility.getParsedDateField(DateUtils.now_ISO8601());
90
            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
			indexCollection = serverDAO.getIndexCollection(mdref);
96
			final FeedResult res = threadPool.submit(new DocumentFeeder(indexCollection, docStream)).get();
97
			
98
			cleanMarkedDocuments(indexCollection, dsId);
99
			if (feedMode.equals(FeedMode.REFRESH)) {
100
                deleteByVersion(indexCollection, dsId, version);
101
            }
102

    
103
			indexCollection.commit();
104
			indexCollection.shutdown();
105

    
106
			log.info("FeedResult: " + res.setTimeElapsed(System.currentTimeMillis()));
107
			endCallback.setJobDone(buildParams(res));
108
		} 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
	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
	/**
129
	 * mdFormatVersion.
130
	 *
131
	 * @param indexCollection the server dao
132
	 * @param dsId            the ds id
133
	 * @throws IndexServiceException the index service exception
134
	 */
135
	private void deleteByVersion(final IndexCollection indexCollection, final String dsId, final String version) throws IndexServiceException {
136
        final String query = String.format("%s:[* TO \"%s\"}", IndexFieldUtility.DS_VERSION, version);
137
        indexCollection.deleteByQuery(query, dsId);
138
	}
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
		final String query = IndexFieldUtility.DELETE_DOCUMENT + ":true ";
149
		indexCollection.deleteByQuery(query, dsId);
150
	}
151

    
152
}
(6-6/8)