1
|
package eu.dnetlib.oai.mongo;
|
2
|
|
3
|
import java.util.Iterator;
|
4
|
|
5
|
import com.google.common.collect.Lists;
|
6
|
import com.mongodb.DBObject;
|
7
|
import com.mongodb.client.MongoCursor;
|
8
|
import eu.dnetlib.data.information.oai.publisher.info.RecordInfo;
|
9
|
import eu.dnetlib.data.oai.store.Cursor;
|
10
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
11
|
|
12
|
public class DNetOAIMongoCursor implements Cursor {
|
13
|
|
14
|
/**
|
15
|
* Underlying mongo cursor.
|
16
|
*/
|
17
|
private MongoCursor<DBObject> dbCursor;
|
18
|
private int size = 0;
|
19
|
/**
|
20
|
* Function to apply to records before delivering.
|
21
|
*/
|
22
|
private UnaryFunction<String, String> function;
|
23
|
|
24
|
/**
|
25
|
* true if the RecordInfo returned by this Cursor must include the record body, false otherwise.
|
26
|
*/
|
27
|
private boolean bodyIncluded;
|
28
|
|
29
|
private RecordInfoGenerator recordInfoGenerator;
|
30
|
private MetadataExtractor metadataExtractor;
|
31
|
private ProvenanceExtractor provenanceExtractor;
|
32
|
|
33
|
public DNetOAIMongoCursor() {
|
34
|
super();
|
35
|
}
|
36
|
|
37
|
public DNetOAIMongoCursor(final MongoCursor<DBObject> dbCursor, final boolean bodyIncluded, final RecordInfoGenerator recordInfoGenerator,
|
38
|
final MetadataExtractor metadataExtractor) {
|
39
|
this(dbCursor, null, bodyIncluded, recordInfoGenerator, metadataExtractor);
|
40
|
}
|
41
|
|
42
|
public DNetOAIMongoCursor(final MongoCursor<DBObject> dbCursor, final UnaryFunction<String, String> function, final boolean bodyIncluded,
|
43
|
final RecordInfoGenerator recordInfoGenerator, final MetadataExtractor metadataExtractor) {
|
44
|
super();
|
45
|
this.dbCursor = dbCursor;
|
46
|
this.size = 0;
|
47
|
this.function = function;
|
48
|
this.bodyIncluded = bodyIncluded;
|
49
|
this.recordInfoGenerator = recordInfoGenerator;
|
50
|
this.metadataExtractor = metadataExtractor;
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* {@inheritDoc}
|
55
|
*/
|
56
|
@Override
|
57
|
public int count() {
|
58
|
//I can do it because MongoCursor are always created from queries with "limit", so I do not expect the creation of the list to explode
|
59
|
//to not exagerate, I'll get the size only if the current size is 0
|
60
|
if (size == 0)
|
61
|
size = Lists.newArrayList(dbCursor).size();
|
62
|
return size;
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* {@inheritDoc}
|
67
|
*
|
68
|
* @see Iterable#iterator()
|
69
|
*/
|
70
|
@Override
|
71
|
public Iterator<RecordInfo> iterator() {
|
72
|
|
73
|
return new Iterator<RecordInfo>() {
|
74
|
|
75
|
@Override
|
76
|
public boolean hasNext() {
|
77
|
return dbCursor.hasNext();
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
public RecordInfo next() {
|
82
|
DBObject res = dbCursor.next();
|
83
|
RecordInfo info = recordInfoGenerator.transformDBObject(res, bodyIncluded);
|
84
|
if ((function != null) && bodyIncluded && (info != null)) {
|
85
|
info.setMetadata(function.evaluate(info.getMetadata()));
|
86
|
}
|
87
|
return info;
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
public void remove() {
|
92
|
throw new UnsupportedOperationException();
|
93
|
}
|
94
|
|
95
|
};
|
96
|
}
|
97
|
|
98
|
public UnaryFunction<String, String> getFunction() {
|
99
|
return function;
|
100
|
}
|
101
|
|
102
|
public void setFunction(final UnaryFunction<String, String> function) {
|
103
|
this.function = function;
|
104
|
}
|
105
|
|
106
|
public MongoCursor<DBObject> getDbCursor() {
|
107
|
return dbCursor;
|
108
|
}
|
109
|
|
110
|
public void setDbCursor(final MongoCursor<DBObject> dbCursor) {
|
111
|
this.dbCursor = dbCursor;
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public boolean isBodyIncluded() {
|
116
|
return this.bodyIncluded;
|
117
|
}
|
118
|
|
119
|
@Override
|
120
|
public void setBodyIncluded(final boolean bodyIncluded) {
|
121
|
this.bodyIncluded = bodyIncluded;
|
122
|
}
|
123
|
|
124
|
public RecordInfoGenerator getRecordInfoGenerator() {
|
125
|
return recordInfoGenerator;
|
126
|
}
|
127
|
|
128
|
public void setRecordInfoGenerator(final RecordInfoGenerator recordInfoGenerator) {
|
129
|
this.recordInfoGenerator = recordInfoGenerator;
|
130
|
}
|
131
|
|
132
|
public MetadataExtractor getMetadataExtractor() {
|
133
|
return metadataExtractor;
|
134
|
}
|
135
|
|
136
|
public void setMetadataExtractor(final MetadataExtractor metadataExtractor) {
|
137
|
this.metadataExtractor = metadataExtractor;
|
138
|
}
|
139
|
|
140
|
public ProvenanceExtractor getProvenanceExtractor() {
|
141
|
return provenanceExtractor;
|
142
|
}
|
143
|
|
144
|
public void setProvenanceExtractor(final ProvenanceExtractor provenanceExtractor) {
|
145
|
this.provenanceExtractor = provenanceExtractor;
|
146
|
}
|
147
|
|
148
|
}
|