1
|
package eu.dnetlib.data.objectstore.modular.gridFS;
|
2
|
|
3
|
import java.io.UnsupportedEncodingException;
|
4
|
import java.net.URLEncoder;
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.List;
|
7
|
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
|
11
|
import com.google.common.collect.Lists;
|
12
|
import com.mongodb.BasicDBObject;
|
13
|
import com.mongodb.DBCursor;
|
14
|
import com.mongodb.DBObject;
|
15
|
import com.mongodb.QueryBuilder;
|
16
|
import com.mongodb.gridfs.GridFS;
|
17
|
import com.mongodb.gridfs.GridFSDBFile;
|
18
|
|
19
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFile;
|
20
|
import eu.dnetlib.data.objectstore.rmi.Protocols;
|
21
|
import eu.dnetlib.enabling.resultset.ResultSet;
|
22
|
import eu.dnetlib.enabling.resultset.ResultSetAware;
|
23
|
import eu.dnetlib.enabling.resultset.ResultSetListener;
|
24
|
import eu.dnetlib.miscutils.collections.MappedCollection;
|
25
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
26
|
|
27
|
public class GridFSObjectstoreResultSetListener implements ResultSetListener, ResultSetAware {
|
28
|
|
29
|
private static final Log log = LogFactory.getLog(GridFSObjectstoreResultSetListener.class);
|
30
|
|
31
|
private Double fromDate;
|
32
|
private Double untilDate;
|
33
|
private List<String> records;
|
34
|
private String objectStoreID;
|
35
|
private GridFS collection;
|
36
|
private String baseURI;
|
37
|
private int currentSize = -1;
|
38
|
private DBCursor currentCursor;
|
39
|
private long cursorPosition;
|
40
|
|
41
|
@Override
|
42
|
public List<String> getResult(final int from, final int to) {
|
43
|
if (records != null) {
|
44
|
List<String> ids = Lists.newArrayList();
|
45
|
for (int i = from; i < to; i++) {
|
46
|
ids.add(records.get(i));
|
47
|
}
|
48
|
QueryBuilder qBuilder = QueryBuilder.start("metadata.id").in(ids);
|
49
|
DBObject q = qBuilder.get();
|
50
|
List<GridFSDBFile> out = collection.find(q);
|
51
|
UnaryFunction<String, GridFSDBFile> obtainURL = new UnaryFunction<String, GridFSDBFile>() {
|
52
|
|
53
|
@Override
|
54
|
public String evaluate(final GridFSDBFile input) {
|
55
|
|
56
|
long size = input.getLength();
|
57
|
DBObject metadata = input.getMetaData();
|
58
|
ObjectStoreFile newFile = new ObjectStoreFile();
|
59
|
newFile.setObjectID((String) metadata.get("id"));
|
60
|
newFile.setAccessProtocol(Protocols.HTTP);
|
61
|
newFile.setMimeType((String) metadata.get("mime"));
|
62
|
log.debug(metadata.get("originalObject"));
|
63
|
newFile.setURI(baseURI + "objectStore=" + objectStoreID + "&objectId=" + newFile.getObjectID());
|
64
|
newFile.setMd5Sum((String) input.get("md5"));
|
65
|
newFile.setFileSizeKB(size / 1024);
|
66
|
return newFile.toJSON();
|
67
|
}
|
68
|
};
|
69
|
return MappedCollection.listMap(out, obtainURL);
|
70
|
} else if ((fromDate != null) && (untilDate != null)) {
|
71
|
if ((currentCursor == null) || (cursorPosition > from)) {
|
72
|
createCurrentCursor();
|
73
|
}
|
74
|
while (cursorPosition < from) {
|
75
|
currentCursor.next();
|
76
|
cursorPosition++;
|
77
|
}
|
78
|
ArrayList<DBObject> out = new ArrayList<DBObject>();
|
79
|
|
80
|
for (int i = from; i <= to; i++) {
|
81
|
if (currentCursor.hasNext()) {
|
82
|
out.add(currentCursor.next());
|
83
|
cursorPosition++;
|
84
|
}
|
85
|
}
|
86
|
|
87
|
UnaryFunction<String, DBObject> obtainURL = new UnaryFunction<String, DBObject>() {
|
88
|
|
89
|
@Override
|
90
|
public String evaluate(final DBObject input) {
|
91
|
DBObject metadata = (DBObject) input.get("metadata");
|
92
|
GridFSDBFile file = (GridFSDBFile) input;
|
93
|
String orginalFile = (String) metadata.get("originalObject");
|
94
|
ObjectStoreFile original = ObjectStoreFile.createObject(orginalFile);
|
95
|
ObjectStoreFile newFile = new ObjectStoreFile();
|
96
|
newFile.setObjectID((String) metadata.get("id"));
|
97
|
newFile.setAccessProtocol(Protocols.HTTP);
|
98
|
newFile.setMimeType((String) metadata.get("mime"));
|
99
|
newFile.setMd5Sum((String) input.get("md5"));
|
100
|
newFile.setFileSizeKB(file.getLength() / 1024);
|
101
|
if (orginalFile != null) {
|
102
|
if ((original.getDownloadedURL() == null) || (original.getDownloadedURL().length() == 0)) {
|
103
|
newFile.setDownloadedURL(original.getURI());
|
104
|
} else {
|
105
|
newFile.setDownloadedURL(original.getDownloadedURL());
|
106
|
}
|
107
|
|
108
|
}
|
109
|
try {
|
110
|
newFile.setURI(baseURI + "?objectStore=" + URLEncoder.encode(objectStoreID, "UTF-8") + "&objectId="
|
111
|
+ URLEncoder.encode(newFile.getObjectID(), "UTF-8"));
|
112
|
} catch (UnsupportedEncodingException e) {
|
113
|
|
114
|
}
|
115
|
return newFile.toJSON();
|
116
|
}
|
117
|
};
|
118
|
return MappedCollection.listMap(out, obtainURL);
|
119
|
}
|
120
|
|
121
|
return null;
|
122
|
}
|
123
|
|
124
|
private void createCurrentCursor() {
|
125
|
BasicDBObject query = new BasicDBObject();
|
126
|
query.put("$gt", fromDate.doubleValue());
|
127
|
query.put("$lt", untilDate.doubleValue());
|
128
|
if (currentCursor != null) {
|
129
|
currentCursor.close();
|
130
|
}
|
131
|
currentCursor = collection.getFileList(new BasicDBObject("metadata.timestamp", query)).sort(new BasicDBObject("_id", 1));
|
132
|
cursorPosition = 1;
|
133
|
|
134
|
}
|
135
|
|
136
|
@Override
|
137
|
public int getSize() {
|
138
|
if (currentSize == -1) {
|
139
|
currentSize = calculateSize();
|
140
|
}
|
141
|
return currentSize - 1;
|
142
|
}
|
143
|
|
144
|
private int calculateSize() {
|
145
|
if (records != null) {
|
146
|
QueryBuilder qBuilder = QueryBuilder.start("metadata.id").in(records);
|
147
|
DBObject q = qBuilder.get();
|
148
|
List<GridFSDBFile> out = collection.find(q);
|
149
|
return out.size();
|
150
|
} else if ((fromDate != null) && (untilDate != null)) {
|
151
|
BasicDBObject query = new BasicDBObject();
|
152
|
query.put("$gt", fromDate.doubleValue());
|
153
|
query.put("$lt", untilDate.doubleValue());
|
154
|
return collection.getFileList(new BasicDBObject("metadata.timestamp", query)).size();
|
155
|
}
|
156
|
return 0;
|
157
|
}
|
158
|
|
159
|
@Override
|
160
|
public void setResultSet(final ResultSet resultSet) {
|
161
|
resultSet.close();
|
162
|
}
|
163
|
|
164
|
public Double getFromDate() {
|
165
|
return fromDate;
|
166
|
}
|
167
|
|
168
|
public void setFromDate(final Double fromdate) {
|
169
|
this.fromDate = fromdate;
|
170
|
}
|
171
|
|
172
|
public Double getUntilDate() {
|
173
|
return untilDate;
|
174
|
}
|
175
|
|
176
|
public void setUntilDate(final Double untilDate) {
|
177
|
this.untilDate = untilDate;
|
178
|
}
|
179
|
|
180
|
public List<String> getRecords() {
|
181
|
return records;
|
182
|
}
|
183
|
|
184
|
public void setRecords(final List<String> records) {
|
185
|
this.records = records;
|
186
|
}
|
187
|
|
188
|
public GridFS getCollection() {
|
189
|
return collection;
|
190
|
}
|
191
|
|
192
|
public void setCollection(final GridFS collection) {
|
193
|
this.collection = collection;
|
194
|
}
|
195
|
|
196
|
public String getBaseURI() {
|
197
|
return baseURI;
|
198
|
}
|
199
|
|
200
|
public void setBaseURI(final String baseURI) {
|
201
|
this.baseURI = baseURI;
|
202
|
}
|
203
|
|
204
|
public String getObjectStoreID() {
|
205
|
return objectStoreID;
|
206
|
}
|
207
|
|
208
|
public void setObjectStoreID(final String objectStoreID) {
|
209
|
this.objectStoreID = objectStoreID;
|
210
|
}
|
211
|
}
|