1
|
package eu.dnetlib.data.mdstore.modular;
|
2
|
|
3
|
import com.google.common.collect.Maps;
|
4
|
import com.googlecode.sarasvati.GraphProcess;
|
5
|
import eu.dnetlib.data.mdstore.MDStoreServiceException;
|
6
|
import eu.dnetlib.data.mdstore.modular.action.DoneCallback;
|
7
|
import eu.dnetlib.data.mdstore.modular.action.FailedCallback;
|
8
|
import eu.dnetlib.data.mdstore.modular.connector.MDStore;
|
9
|
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao;
|
10
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
11
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
12
|
import eu.dnetlib.enabling.resultset.client.ResultSetClientFactory;
|
13
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
14
|
import org.apache.commons.logging.Log;
|
15
|
import org.apache.commons.logging.LogFactory;
|
16
|
import org.springframework.beans.factory.annotation.Required;
|
17
|
|
18
|
import java.util.List;
|
19
|
import java.util.Map;
|
20
|
|
21
|
public class MDStoreFeeder {
|
22
|
|
23
|
private static final Log log = LogFactory.getLog(MDStoreFeeder.class);
|
24
|
|
25
|
private MDStoreDao dao;
|
26
|
|
27
|
private ResultSetClientFactory resultSetClientFactory;
|
28
|
|
29
|
private UniqueServiceLocator serviceLocator;
|
30
|
|
31
|
private boolean syncFeed = true;
|
32
|
|
33
|
public void feed(final String mdId,
|
34
|
final String rsEpr,
|
35
|
final String storingType,
|
36
|
final boolean sync,
|
37
|
final List<MDFormatDescription> mdformats,
|
38
|
final GraphProcess graphProcess,
|
39
|
final DoneCallback doneCallback,
|
40
|
final FailedCallback failCallback) throws MDStoreServiceException {
|
41
|
|
42
|
log.debug("Start feeding mdstore " + mdId + " with epr " + rsEpr);
|
43
|
|
44
|
String transactionId = null;
|
45
|
|
46
|
try {
|
47
|
final boolean refresh = "REFRESH".equals(storingType);
|
48
|
|
49
|
final MDStore mdstore = dao.startTransaction(mdId, refresh);
|
50
|
transactionId = mdstore.getId();
|
51
|
|
52
|
final Iterable<String> records = resultSetClientFactory.getClient(rsEpr);
|
53
|
|
54
|
if (refresh) {
|
55
|
mdstore.truncate();
|
56
|
}
|
57
|
int writeOps;
|
58
|
|
59
|
if (mdformats == null) {
|
60
|
writeOps = mdstore.feed(records, refresh);
|
61
|
} else {
|
62
|
writeOps = mdstore.feed(records, refresh, mdformats);
|
63
|
}
|
64
|
|
65
|
if(graphProcess.isCanceled()){
|
66
|
//means the process was cancelled, so we must not commit
|
67
|
log.warn(String.format("The feeding process on mdstore %s has been cancelled. Records will not be committed. Transaction %s set as invalid.", mdId, transactionId));
|
68
|
dao.invalidTransaction(transactionId, mdId);
|
69
|
failCallback.call(new MDStoreServiceException(String.format("Feeding cancelled for mdstore %s on transaction %s", mdId, transactionId)));
|
70
|
}
|
71
|
else {
|
72
|
dao.commit(mdstore.getId(), mdId);
|
73
|
int size = dao.refreshSize(mdId);
|
74
|
touch(mdId, size);
|
75
|
log.info("Finished feeding mdstore " + mdId + " - new size: " + size);
|
76
|
doneCallback.call(buildParams(size, writeOps));
|
77
|
}
|
78
|
} catch (Throwable e) {
|
79
|
if (transactionId != null) {
|
80
|
dao.invalidTransaction(transactionId, mdId);
|
81
|
}
|
82
|
log.error("Error feeding mdstore: " + mdId);
|
83
|
failCallback.call(e);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
private Map<String, String> buildParams(final int size, final int storeCount) {
|
88
|
Map<String, String> params = Maps.newHashMap();
|
89
|
params.put("mdstoreSize", String.valueOf(size));
|
90
|
params.put("writeOps", String.valueOf(storeCount));
|
91
|
return params;
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Sets the last modified date in the profile.
|
96
|
*
|
97
|
* @param mdId
|
98
|
*/
|
99
|
public void touch(final String mdId, final int size) {
|
100
|
try {
|
101
|
final String now = DateUtils.now_ISO8601();
|
102
|
|
103
|
final String mdstoreXUpdate = "for $x in //RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value = '" + mdId + "']"
|
104
|
+ "return update value $x//LAST_STORAGE_DATE with '" + now + "'";
|
105
|
|
106
|
serviceLocator.getService(ISRegistryService.class).executeXUpdate(mdstoreXUpdate);
|
107
|
|
108
|
touchSize(mdId, size);
|
109
|
} catch (final Exception e) {
|
110
|
throw new IllegalStateException(e);
|
111
|
}
|
112
|
}
|
113
|
|
114
|
public void touchSize(final String mdId, final int size) {
|
115
|
try {
|
116
|
final String mdstoreNumberXUpdate = "for $x in //RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value = '" + mdId + "']"
|
117
|
+ "return update value $x//NUMBER_OF_RECORDS with '" + size + "'";
|
118
|
|
119
|
serviceLocator.getService(ISRegistryService.class).executeXUpdate(mdstoreNumberXUpdate);
|
120
|
} catch (final Exception e) {
|
121
|
throw new IllegalStateException(e);
|
122
|
}
|
123
|
}
|
124
|
|
125
|
public MDStoreDao getDao() {
|
126
|
return dao;
|
127
|
}
|
128
|
|
129
|
@Required
|
130
|
public void setDao(final MDStoreDao dao) {
|
131
|
this.dao = dao;
|
132
|
}
|
133
|
|
134
|
public ResultSetClientFactory getResultSetClientFactory() {
|
135
|
return resultSetClientFactory;
|
136
|
}
|
137
|
|
138
|
@Required
|
139
|
public void setResultSetClientFactory(final ResultSetClientFactory resultSetClientFactory) {
|
140
|
this.resultSetClientFactory = resultSetClientFactory;
|
141
|
}
|
142
|
|
143
|
public boolean isSyncFeed() {
|
144
|
return syncFeed;
|
145
|
}
|
146
|
|
147
|
public void setSyncFeed(final boolean syncFeed) {
|
148
|
this.syncFeed = syncFeed;
|
149
|
}
|
150
|
|
151
|
public UniqueServiceLocator getServiceLocator() {
|
152
|
return serviceLocator;
|
153
|
}
|
154
|
|
155
|
@Required
|
156
|
public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
|
157
|
this.serviceLocator = serviceLocator;
|
158
|
}
|
159
|
|
160
|
}
|