1
|
package eu.dnetlib.functionality.index.feed;
|
2
|
|
3
|
import java.util.concurrent.Callable;
|
4
|
|
5
|
import org.apache.commons.logging.Log;
|
6
|
import org.apache.commons.logging.LogFactory;
|
7
|
|
8
|
import eu.dnetlib.functionality.index.model.document.IndexDocument;
|
9
|
import eu.dnetlib.functionality.index.utils.IndexFieldUtility;
|
10
|
|
11
|
/**
|
12
|
* The Class DocumentFeeder.
|
13
|
*/
|
14
|
public class DocumentFeeder implements Callable<FeedResult> {
|
15
|
|
16
|
/** The Constant log. */
|
17
|
private static final Log log = LogFactory.getLog(DocumentFeeder.class);
|
18
|
|
19
|
/** The index collection. */
|
20
|
private IndexFeederCollection indexCollection;
|
21
|
|
22
|
/** The records. */
|
23
|
private Iterable<IndexDocument> records;
|
24
|
|
25
|
/**
|
26
|
* Instantiates a new document feeder.
|
27
|
*
|
28
|
* @param indexCollection
|
29
|
* the index collection
|
30
|
* @param records
|
31
|
* the records
|
32
|
*/
|
33
|
public DocumentFeeder(final IndexFeederCollection indexCollection, final Iterable<IndexDocument> records) {
|
34
|
this.indexCollection = indexCollection;
|
35
|
this.records = records;
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* {@inheritDoc}
|
40
|
*
|
41
|
* @see java.util.concurrent.Callable#call()
|
42
|
*/
|
43
|
@Override
|
44
|
public FeedResult call() throws Exception {
|
45
|
final FeedResult res = new FeedResult(System.currentTimeMillis());
|
46
|
for (IndexDocument doc : records) {
|
47
|
boolean rsp;
|
48
|
switch (doc.getStatus()) {
|
49
|
|
50
|
case OK:
|
51
|
rsp = indexCollection.add(doc);
|
52
|
if (rsp) {
|
53
|
res.add();
|
54
|
} else {
|
55
|
res.mark();
|
56
|
}
|
57
|
break;
|
58
|
|
59
|
case MARKED:
|
60
|
res.mark();
|
61
|
log.debug("skipping record: " + doc.getFieldValue(IndexFieldUtility.INDEX_RECORD_ID));
|
62
|
break;
|
63
|
|
64
|
case ERROR:
|
65
|
res.skip();
|
66
|
log.info("Error on record: " + doc.getFieldValue(IndexFieldUtility.INDEX_RECORD_ID));
|
67
|
break;
|
68
|
|
69
|
default:
|
70
|
throw new IllegalStateException("unknow document status");
|
71
|
}
|
72
|
}
|
73
|
return res;
|
74
|
}
|
75
|
|
76
|
}
|