Revision 42184
Added by Michele Artini almost 8 years ago
DNetOAIMongoCursor.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.oai.mongo; |
2 | 2 |
|
3 | 3 |
import java.util.Iterator; |
4 |
import java.util.function.Function; |
|
4 | 5 |
|
5 | 6 |
import com.google.common.collect.Lists; |
6 | 7 |
import com.mongodb.DBObject; |
7 | 8 |
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 | 9 |
|
10 |
import eu.dnetlib.oai.Cursor; |
|
11 |
import eu.dnetlib.oai.info.RecordInfo; |
|
12 |
|
|
12 | 13 |
public class DNetOAIMongoCursor implements Cursor { |
13 | 14 |
|
14 | 15 |
/** |
... | ... | |
19 | 20 |
/** |
20 | 21 |
* Function to apply to records before delivering. |
21 | 22 |
*/ |
22 |
private UnaryFunction<String, String> function;
|
|
23 |
private Function<String, String> function; |
|
23 | 24 |
|
24 | 25 |
/** |
25 | 26 |
* true if the RecordInfo returned by this Cursor must include the record body, false otherwise. |
... | ... | |
39 | 40 |
this(dbCursor, null, bodyIncluded, recordInfoGenerator, metadataExtractor); |
40 | 41 |
} |
41 | 42 |
|
42 |
public DNetOAIMongoCursor(final MongoCursor<DBObject> dbCursor, final UnaryFunction<String, String> function, final boolean bodyIncluded,
|
|
43 |
public DNetOAIMongoCursor(final MongoCursor<DBObject> dbCursor, final Function<String, String> function, final boolean bodyIncluded, |
|
43 | 44 |
final RecordInfoGenerator recordInfoGenerator, final MetadataExtractor metadataExtractor) { |
44 | 45 |
super(); |
45 | 46 |
this.dbCursor = dbCursor; |
... | ... | |
55 | 56 |
*/ |
56 | 57 |
@Override |
57 | 58 |
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; |
|
59 |
// I can do it because MongoCursor are always created from queries with "limit", so I do not expect the creation of the list to |
|
60 |
// explode |
|
61 |
// to not exagerate, I'll get the size only if the current size is 0 |
|
62 |
if (this.size == 0) { |
|
63 |
this.size = Lists.newArrayList(this.dbCursor).size(); |
|
64 |
} |
|
65 |
return this.size; |
|
63 | 66 |
} |
64 | 67 |
|
65 | 68 |
/** |
... | ... | |
74 | 77 |
|
75 | 78 |
@Override |
76 | 79 |
public boolean hasNext() { |
77 |
return dbCursor.hasNext(); |
|
80 |
return DNetOAIMongoCursor.this.dbCursor.hasNext();
|
|
78 | 81 |
} |
79 | 82 |
|
80 | 83 |
@Override |
81 | 84 |
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()));
|
|
85 |
final DBObject res = DNetOAIMongoCursor.this.dbCursor.next();
|
|
86 |
final RecordInfo info = DNetOAIMongoCursor.this.recordInfoGenerator.transformDBObject(res, DNetOAIMongoCursor.this.bodyIncluded);
|
|
87 |
if ((DNetOAIMongoCursor.this.function != null) && DNetOAIMongoCursor.this.bodyIncluded && (info != null)) {
|
|
88 |
info.setMetadata(DNetOAIMongoCursor.this.function.apply(info.getMetadata()));
|
|
86 | 89 |
} |
87 | 90 |
return info; |
88 | 91 |
} |
... | ... | |
95 | 98 |
}; |
96 | 99 |
} |
97 | 100 |
|
98 |
public UnaryFunction<String, String> getFunction() {
|
|
99 |
return function; |
|
101 |
public Function<String, String> getFunction() { |
|
102 |
return this.function;
|
|
100 | 103 |
} |
101 | 104 |
|
102 |
public void setFunction(final UnaryFunction<String, String> function) {
|
|
105 |
public void setFunction(final Function<String, String> function) { |
|
103 | 106 |
this.function = function; |
104 | 107 |
} |
105 | 108 |
|
106 | 109 |
public MongoCursor<DBObject> getDbCursor() { |
107 |
return dbCursor; |
|
110 |
return this.dbCursor;
|
|
108 | 111 |
} |
109 | 112 |
|
110 | 113 |
public void setDbCursor(final MongoCursor<DBObject> dbCursor) { |
... | ... | |
122 | 125 |
} |
123 | 126 |
|
124 | 127 |
public RecordInfoGenerator getRecordInfoGenerator() { |
125 |
return recordInfoGenerator; |
|
128 |
return this.recordInfoGenerator;
|
|
126 | 129 |
} |
127 | 130 |
|
128 | 131 |
public void setRecordInfoGenerator(final RecordInfoGenerator recordInfoGenerator) { |
... | ... | |
130 | 133 |
} |
131 | 134 |
|
132 | 135 |
public MetadataExtractor getMetadataExtractor() { |
133 |
return metadataExtractor; |
|
136 |
return this.metadataExtractor;
|
|
134 | 137 |
} |
135 | 138 |
|
136 | 139 |
public void setMetadataExtractor(final MetadataExtractor metadataExtractor) { |
... | ... | |
138 | 141 |
} |
139 | 142 |
|
140 | 143 |
public ProvenanceExtractor getProvenanceExtractor() { |
141 |
return provenanceExtractor; |
|
144 |
return this.provenanceExtractor;
|
|
142 | 145 |
} |
143 | 146 |
|
144 | 147 |
public void setProvenanceExtractor(final ProvenanceExtractor provenanceExtractor) { |
Also available in: Unified diff
oai import