Revision 42432
Added by Andrea Mannocci over 8 years ago
modules/dnet-gridfs-objectstore/tags/dnet-gridfs-objectstore-4.0.4-TAG/deploy.info | ||
---|---|---|
1 |
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/dnet-gridfs-objectstore/trunk/", "deploy_repository": "dnet4-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet4-snapshots", "name": "dnet-gridfs-objectstore"} |
modules/dnet-gridfs-objectstore/tags/dnet-gridfs-objectstore-4.0.4-TAG/src/test/java/eu/dnetlib/test/TestQuery.java | ||
---|---|---|
1 |
package eu.dnetlib.test; |
|
2 |
|
|
3 |
import com.google.common.collect.Lists; |
|
4 |
import com.mongodb.*; |
|
5 |
import com.mongodb.gridfs.GridFS; |
|
6 |
import eu.dnetlib.data.objectstore.modular.gridFS.GridFSObjectstoreDaoImpl; |
|
7 |
import org.junit.Ignore; |
|
8 |
import org.junit.Test; |
|
9 |
|
|
10 |
import java.io.UnsupportedEncodingException; |
|
11 |
import java.net.URLEncoder; |
|
12 |
import java.net.UnknownHostException; |
|
13 |
import java.util.ArrayList; |
|
14 |
import java.util.Date; |
|
15 |
|
|
16 |
public class TestQuery { |
|
17 |
|
|
18 |
@Test |
|
19 |
@Ignore |
|
20 |
public void testFromUntil() throws UnknownHostException, MongoException, UnsupportedEncodingException { |
|
21 |
GridFSObjectstoreDaoImpl dao = new GridFSObjectstoreDaoImpl(); |
|
22 |
Mongo mongo = new Mongo("localhost", 27017); |
|
23 |
DB db = mongo.getDB("objectStore"); |
|
24 |
dao.setDb(db); |
|
25 |
GridFS obs = new GridFS(db, "aeiouy"); |
|
26 |
BasicDBObject q = new BasicDBObject(); |
|
27 |
Date d = new Date(); |
|
28 |
|
|
29 |
Double from = new Double(d.getTime()); |
|
30 |
System.out.println(URLEncoder.encode("MY CRZY QUERY! +&+ :)", "UTF-8")); |
|
31 |
|
|
32 |
q.put("$lt", from.doubleValue()); |
|
33 |
|
|
34 |
DBCursor files = obs.getFileList(new BasicDBObject("metadata.timestamp", q)).sort(new BasicDBObject("metadata.id", 1)).skip(4).limit(3); |
|
35 |
ArrayList<DBObject> p = Lists.newArrayList((Iterable<DBObject>) files); |
|
36 |
for (DBObject tmp : p) { |
|
37 |
DBObject o = (DBObject) tmp.get("metadata"); |
|
38 |
System.out.println(o); |
|
39 |
} |
|
40 |
|
|
41 |
} |
|
42 |
} |
modules/dnet-gridfs-objectstore/tags/dnet-gridfs-objectstore-4.0.4-TAG/src/test/java/eu/dnetlib/test/ConfigurationTestConfig.java | ||
---|---|---|
1 |
package eu.dnetlib.test; |
|
2 |
|
|
3 |
import java.net.UnknownHostException; |
|
4 |
|
|
5 |
import org.springframework.context.annotation.Bean; |
|
6 |
import org.springframework.context.annotation.Configuration; |
|
7 |
|
|
8 |
import com.mongodb.DB; |
|
9 |
import com.mongodb.MongoClient; |
|
10 |
|
|
11 |
@Configuration |
|
12 |
public class ConfigurationTestConfig { |
|
13 |
|
|
14 |
@Bean |
|
15 |
public DB db() throws UnknownHostException { |
|
16 |
MongoClient mongo = new MongoClient(); |
|
17 |
return mongo.getDB("objectStore"); |
|
18 |
} |
|
19 |
|
|
20 |
} |
modules/dnet-gridfs-objectstore/tags/dnet-gridfs-objectstore-4.0.4-TAG/src/test/java/eu/dnetlib/test/TestGridfs.java | ||
---|---|---|
1 |
package eu.dnetlib.test; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.FileInputStream; |
|
5 |
import java.io.FileNotFoundException; |
|
6 |
|
|
7 |
import com.mongodb.DB; |
|
8 |
import com.mongodb.gridfs.GridFS; |
|
9 |
import eu.dnetlib.data.objectstore.modular.ObjectStoreRecord; |
|
10 |
import eu.dnetlib.data.objectstore.modular.connector.ObjectStore; |
|
11 |
import eu.dnetlib.data.objectstore.modular.gridFS.GridFSObjectStore; |
|
12 |
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFile; |
|
13 |
import eu.dnetlib.data.objectstore.rmi.ObjectStoreServiceException; |
|
14 |
import eu.dnetlib.data.objectstore.rmi.Protocols; |
|
15 |
import org.junit.Ignore; |
|
16 |
import org.junit.Test; |
|
17 |
import org.junit.runner.RunWith; |
|
18 |
import org.springframework.beans.factory.annotation.Autowired; |
|
19 |
import org.springframework.test.context.ContextConfiguration; |
|
20 |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
|
21 |
|
|
22 |
|
|
23 |
@RunWith(SpringJUnit4ClassRunner.class) |
|
24 |
@ContextConfiguration(classes = ConfigurationTestConfig.class) |
|
25 |
public class TestGridfs { |
|
26 |
|
|
27 |
@Autowired |
|
28 |
private DB db; |
|
29 |
|
|
30 |
@Ignore |
|
31 |
@Test |
|
32 |
public void testTag() { |
|
33 |
final GridFS collection = new GridFS(db, "testSTORE"); |
|
34 |
ObjectStore objectStore = new GridFSObjectStore("85499fec-7ef8-4bae-8b51-6eb1abc12076", collection, true); |
|
35 |
|
|
36 |
final double start = System.currentTimeMillis(); |
|
37 |
objectStore.untagObjectstore("suca"); |
|
38 |
final double end = System.currentTimeMillis(); |
|
39 |
System.out.println("Done in end = " + (end - start) / 1000); |
|
40 |
|
|
41 |
} |
|
42 |
|
|
43 |
@Ignore |
|
44 |
@Test |
|
45 |
public void populationTest() throws FileNotFoundException, ObjectStoreServiceException { |
|
46 |
|
|
47 |
final GridFS collection = new GridFS(db, "testSTORE"); |
|
48 |
ObjectStore objectStore = new GridFSObjectStore("85499fec-7ef8-4bae-8b51-6eb1abc12076", collection, true); |
|
49 |
|
|
50 |
for (int i = 300000; i < 1000000; i++) { |
|
51 |
final ObjectStoreRecord record; |
|
52 |
record = new ObjectStoreRecord(); |
|
53 |
|
|
54 |
record.setInputStream(new FileInputStream(new File("/Users/sandro/dropall.js"))); |
|
55 |
|
|
56 |
final ObjectStoreFile metadata = new ObjectStoreFile(); |
|
57 |
metadata.setObjectID("" + i); |
|
58 |
metadata.setAccessProtocol(Protocols.File_System); |
|
59 |
metadata.setDownloadedURL("suca"); |
|
60 |
metadata.setMimeType("javascript"); |
|
61 |
|
|
62 |
record.setFileMetadata(metadata); |
|
63 |
objectStore.feedObjectRecord(record); |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
} |
modules/dnet-gridfs-objectstore/tags/dnet-gridfs-objectstore-4.0.4-TAG/src/main/java/eu/dnetlib/data/objectstore/modular/gridFS/GridFSObjectStore.java | ||
---|---|---|
1 |
package eu.dnetlib.data.objectstore.modular.gridFS; |
|
2 |
|
|
3 |
import java.io.ByteArrayInputStream; |
|
4 |
import java.io.IOException; |
|
5 |
import java.io.InputStream; |
|
6 |
import java.io.UnsupportedEncodingException; |
|
7 |
import java.net.URLEncoder; |
|
8 |
import java.util.List; |
|
9 |
import java.util.Set; |
|
10 |
import java.util.regex.Pattern; |
|
11 |
|
|
12 |
import com.google.common.collect.Lists; |
|
13 |
import com.google.common.collect.Sets; |
|
14 |
import com.mongodb.*; |
|
15 |
import com.mongodb.gridfs.GridFS; |
|
16 |
import com.mongodb.gridfs.GridFSDBFile; |
|
17 |
import com.mongodb.gridfs.GridFSInputFile; |
|
18 |
import eu.dnetlib.data.objectstore.modular.ObjectStoreRecord; |
|
19 |
import eu.dnetlib.data.objectstore.modular.connector.ObjectStore; |
|
20 |
import eu.dnetlib.data.objectstore.rmi.MetadataObjectRecord; |
|
21 |
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFile; |
|
22 |
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFileNotFoundException; |
|
23 |
import eu.dnetlib.data.objectstore.rmi.ObjectStoreServiceException; |
|
24 |
import eu.dnetlib.enabling.resultset.ResultSetListener; |
|
25 |
import eu.dnetlib.miscutils.collections.Pair; |
|
26 |
import org.apache.commons.logging.Log; |
|
27 |
import org.apache.commons.logging.LogFactory; |
|
28 |
|
|
29 |
// TODO: Auto-generated Javadoc |
|
30 |
/** |
|
31 |
* The Class GridFSObjectStore. |
|
32 |
*/ |
|
33 |
|
|
34 |
public class GridFSObjectStore implements ObjectStore { |
|
35 |
|
|
36 |
// ================================================================================ |
|
37 |
// Properties |
|
38 |
// ================================================================================ |
|
39 |
|
|
40 |
/** The Constant log. */ |
|
41 |
private static final Log log = LogFactory.getLog(GridFSObjectStore.class); |
|
42 |
/** |
|
43 |
* The collection. |
|
44 |
*/ |
|
45 |
private final GridFS collection; |
|
46 |
/** The id. */ |
|
47 |
private String id; |
|
48 |
/** The upsert. */ |
|
49 |
private boolean upsert; |
|
50 |
/** The base uri. */ |
|
51 |
private String baseURI; |
|
52 |
|
|
53 |
/** The interpretation. */ |
|
54 |
private String interpretation; |
|
55 |
|
|
56 |
// ================================================================================ |
|
57 |
// Constructor |
|
58 |
// ================================================================================ |
|
59 |
|
|
60 |
/** |
|
61 |
* Instantiates a new gridfs objectstore. |
|
62 |
* |
|
63 |
* @param id |
|
64 |
* the id |
|
65 |
* @param collection |
|
66 |
* the collection |
|
67 |
* @param upsert |
|
68 |
* the upsert |
|
69 |
*/ |
|
70 |
public GridFSObjectStore(final String id, final GridFS collection, final boolean upsert) { |
|
71 |
this.id = id; |
|
72 |
this.setUpsert(upsert); |
|
73 |
this.collection = collection; |
|
74 |
|
|
75 |
} |
|
76 |
|
|
77 |
// ================================================================================ |
|
78 |
// Getter and Setter |
|
79 |
// ================================================================================ |
|
80 |
|
|
81 |
/* |
|
82 |
* (non-Javadoc) |
|
83 |
* |
|
84 |
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStore#getId() |
|
85 |
*/ |
|
86 |
/** |
|
87 |
* Gets the id. |
|
88 |
* |
|
89 |
* @return the id |
|
90 |
*/ |
|
91 |
@Override |
|
92 |
public String getId() { |
|
93 |
return id; |
|
94 |
} |
|
95 |
|
|
96 |
/* |
|
97 |
* (non-Javadoc) |
|
98 |
* |
|
99 |
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStore#getSize() |
|
100 |
*/ |
|
101 |
/** |
|
102 |
* Gets the size. |
|
103 |
* |
|
104 |
* @return the size |
|
105 |
*/ |
|
106 |
@Override |
|
107 |
public int getSize() { |
|
108 |
return collection.getFileList().count(); |
|
109 |
} |
|
110 |
|
|
111 |
/* |
|
112 |
* (non-Javadoc) |
|
113 |
* |
|
114 |
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStore#getInterpretation() |
|
115 |
*/ |
|
116 |
/** |
|
117 |
* Gets the interpretation. |
|
118 |
* |
|
119 |
* @return the interpretation |
|
120 |
*/ |
|
121 |
@Override |
|
122 |
public String getInterpretation() { |
|
123 |
if (interpretation == null) { |
|
124 |
this.interpretation = (String) getMDStoreMetadata().get("interpretation"); |
|
125 |
} |
|
126 |
|
|
127 |
return this.interpretation; |
|
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* Gets the MD store metadata. |
|
132 |
* |
|
133 |
* @return the MD store metadata |
|
134 |
*/ |
|
135 |
public DBObject getMDStoreMetadata() { |
|
136 |
return collection.getDB().getCollection("metadataObjectStore").findOne(new BasicDBObject("obsId", Pattern.compile(getId()))); |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* Gets the base uri. |
|
141 |
* |
|
142 |
* @return the base uri |
|
143 |
*/ |
|
144 |
public String getBaseURI() { |
|
145 |
return baseURI; |
|
146 |
} |
|
147 |
|
|
148 |
/** |
|
149 |
* Sets the base uri. |
|
150 |
* |
|
151 |
* @param baseURI |
|
152 |
* the new base uri |
|
153 |
*/ |
|
154 |
public void setBaseURI(final String baseURI) { |
|
155 |
this.baseURI = baseURI; |
|
156 |
|
|
157 |
} |
|
158 |
|
|
159 |
/** |
|
160 |
* Checks if is upsert. |
|
161 |
* |
|
162 |
* @return true, if is upsert |
|
163 |
*/ |
|
164 |
public boolean isUpsert() { |
|
165 |
return upsert; |
|
166 |
} |
|
167 |
|
|
168 |
/** |
|
169 |
* Sets the upsert. |
|
170 |
* |
|
171 |
* @param upsert |
|
172 |
* the new upsert |
|
173 |
*/ |
|
174 |
public void setUpsert(final boolean upsert) { |
|
175 |
this.upsert = upsert; |
|
176 |
} |
|
177 |
|
|
178 |
/* |
|
179 |
* (non-Javadoc) |
|
180 |
* |
|
181 |
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStore#deleteObject(java.lang.String) |
|
182 |
*/ |
|
183 |
/** |
|
184 |
* Delete object. |
|
185 |
* |
|
186 |
* @param objectId |
|
187 |
* the object id |
|
188 |
*/ |
|
189 |
@Override |
|
190 |
public void deleteObject(final String objectId) { |
|
191 |
collection.remove(new BasicDBObject("_id", objectId)); |
|
192 |
} |
|
193 |
|
|
194 |
// ================================================================================ |
|
195 |
// Deliver |
|
196 |
// ================================================================================ |
|
197 |
|
|
198 |
/* |
|
199 |
* (non-Javadoc) |
|
200 |
* |
|
201 |
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStore#deliver(java.lang.Double, java.lang.Double) |
|
202 |
*/ |
|
203 |
/** |
|
204 |
* Deliver. |
|
205 |
* |
|
206 |
* @param from |
|
207 |
* the from |
|
208 |
* @param until |
|
209 |
* the until |
|
210 |
* @return the result set listener |
|
211 |
*/ |
|
212 |
@Override |
|
213 |
public ResultSetListener deliver(final Long from, final Long until) { |
|
214 |
final GridFSObjectstoreResultSetListener resulset = new GridFSObjectstoreResultSetListener(); |
|
215 |
resulset.setBaseURI(baseURI); |
|
216 |
resulset.setObjectStoreID(id); |
|
217 |
resulset.setCollection(collection); |
|
218 |
resulset.setFromDate(from); |
|
219 |
resulset.setUntilDate(until); |
|
220 |
return resulset; |
|
221 |
} |
|
222 |
|
|
223 |
/* |
|
224 |
* (non-Javadoc) |
|
225 |
* |
|
226 |
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStore#deliverIds(java.lang.Iterable) |
|
227 |
*/ |
|
228 |
/** |
|
229 |
* Deliver ids. |
|
230 |
* |
|
231 |
* @param ids |
|
232 |
* the ids |
|
233 |
* @return the result set listener |
|
234 |
*/ |
|
235 |
@Override |
|
236 |
public ResultSetListener deliverIds(final Iterable<String> ids) { |
|
237 |
final GridFSObjectstoreResultSetListener resulset = new GridFSObjectstoreResultSetListener(); |
|
238 |
resulset.setBaseURI(baseURI); |
|
239 |
resulset.setObjectStoreID(id); |
|
240 |
resulset.setCollection(collection); |
|
241 |
resulset.setRecords((List<String>) ids); |
|
242 |
return resulset; |
|
243 |
} |
|
244 |
|
|
245 |
/* |
|
246 |
* (non-Javadoc) |
|
247 |
* |
|
248 |
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStore#deliverObject(java.lang.String) |
|
249 |
*/ |
|
250 |
/** |
|
251 |
* Deliver object. |
|
252 |
* |
|
253 |
* @param objectId |
|
254 |
* the object id |
|
255 |
* @return the object store file |
|
256 |
* @throws ObjectStoreServiceException |
|
257 |
* the object store service exception |
|
258 |
*/ |
|
259 |
@Override |
|
260 |
public ObjectStoreFile deliverObject(final String objectId) throws ObjectStoreServiceException { |
|
261 |
final BasicDBObject query = new BasicDBObject("_id", objectId); |
|
262 |
final List<GridFSDBFile> file = collection.find(query); |
|
263 |
checkSingleItem(objectId, file); |
|
264 |
return ObjectStoreFileUtility.build(file.get(0), baseURI, id); |
|
265 |
|
|
266 |
} |
|
267 |
|
|
268 |
/** |
|
269 |
* Deliver records by tag. |
|
270 |
* |
|
271 |
* @param tags |
|
272 |
* the tags |
|
273 |
* @return the string |
|
274 |
*/ |
|
275 |
@Override |
|
276 |
public ResultSetListener deliverRecordsByTag(final Iterable<String> tags) { |
|
277 |
final GridFSObjectstoreResultSetListener resulset = new GridFSObjectstoreResultSetListener(); |
|
278 |
resulset.setBaseURI(baseURI); |
|
279 |
resulset.setObjectStoreID(id); |
|
280 |
resulset.setCollection(collection); |
|
281 |
resulset.setTags(Sets.newHashSet(tags)); |
|
282 |
return resulset; |
|
283 |
} |
|
284 |
|
|
285 |
/* |
|
286 |
* (non-Javadoc) |
|
287 |
* |
|
288 |
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStore#deliverStream(java.lang.String) |
|
289 |
*/ |
|
290 |
/** |
|
291 |
* Deliver stream. |
|
292 |
* |
|
293 |
* @param objectId |
|
294 |
* the object id |
|
295 |
* @return the input stream |
|
296 |
* @throws ObjectStoreFileNotFoundException |
|
297 |
* the object store file not found exception |
|
298 |
*/ |
|
299 |
@Override |
|
300 |
public InputStream deliverStream(final String objectId) throws ObjectStoreFileNotFoundException { |
|
301 |
final BasicDBObject query = new BasicDBObject("_id", objectId); |
|
302 |
final List<GridFSDBFile> file = collection.find(query); |
|
303 |
checkSingleItem(objectId, file); |
|
304 |
return file.get(0).getInputStream(); |
|
305 |
} |
|
306 |
|
|
307 |
/* |
|
308 |
* (non-Javadoc) |
|
309 |
* |
|
310 |
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStore#existIDStartsWith(java.lang.String) |
|
311 |
*/ |
|
312 |
/** |
|
313 |
* Exist id starts with. |
|
314 |
* |
|
315 |
* @param startId |
|
316 |
* the start id |
|
317 |
* @return true, if successful |
|
318 |
*/ |
|
319 |
@Override |
|
320 |
public boolean existIDStartsWith(final String startId) { |
|
321 |
final BasicDBObject query = new BasicDBObject("_id", startId); |
|
322 |
final List<GridFSDBFile> file = collection.find(query); |
|
323 |
return file.size() > 0; |
|
324 |
} |
|
325 |
|
|
326 |
// ================================================================================ |
|
327 |
// Feed |
|
328 |
// ================================================================================ |
|
329 |
|
|
330 |
/* |
|
331 |
* (non-Javadoc) |
|
332 |
* |
|
333 |
* @see |
|
334 |
* eu.dnetlib.data.objectstore.modular.connector.ObjectStore#feedObjectRecord(eu.dnetlib.data.objectstore.modular.ObjectStoreRecord) |
|
335 |
*/ |
|
336 |
/** |
|
337 |
* Feed object record. |
|
338 |
* |
|
339 |
* @param record |
|
340 |
* the record |
|
341 |
* @return the string |
|
342 |
* @throws ObjectStoreServiceException |
|
343 |
* the object store service exception |
|
344 |
*/ |
|
345 |
@Override |
|
346 |
public String feedObjectRecord(final ObjectStoreRecord record) throws ObjectStoreServiceException { |
|
347 |
if (record == null || record.getFileMetadata() == null) throw new ObjectStoreServiceException("Empty input Record"); |
|
348 |
|
|
349 |
if (existIDStartsWith(record.getFileMetadata().getObjectID())) { |
|
350 |
log.debug("Object already exist "); |
|
351 |
if (record.getInputStream() != null) { |
|
352 |
try { |
|
353 |
record.getInputStream().close(); |
|
354 |
} catch (IOException e) { |
|
355 |
log.debug("Exception happen in closing inputstream " + e); |
|
356 |
throw new ObjectStoreServiceException(e); |
|
357 |
} |
|
358 |
} |
|
359 |
ObjectStoreFile obj = deliverObject(record.getFileMetadata().getObjectID()); |
|
360 |
return obj.getURI(); |
|
361 |
} |
|
362 |
long timestamp = System.currentTimeMillis(); |
|
363 |
String URI = ""; |
|
364 |
|
|
365 |
if (record.getInputStream() == null) throw new ObjectStoreServiceException("missing inputstream on record " + record.getFileMetadata().getObjectID()); |
|
366 |
final GridFSInputFile currentFile = collection.createFile(record.getInputStream()); |
|
367 |
currentFile.setId(record.getFileMetadata().getObjectID()); |
|
368 |
currentFile.setFilename(record.getFileMetadata().getObjectID()); |
|
369 |
final BasicDBObject metadata = new BasicDBObject(); |
|
370 |
metadata.put("id", record.getFileMetadata().getObjectID()); |
|
371 |
metadata.put("mime", record.getFileMetadata().getMimeType()); |
|
372 |
metadata.put("originalObject", record.getFileMetadata().toJSON()); |
|
373 |
metadata.put("timestamp", timestamp); |
|
374 |
try { |
|
375 |
URI = baseURI + "?objectStore=" + URLEncoder.encode(id, "UTF-8") + "&objectId=" |
|
376 |
+ URLEncoder.encode(record.getFileMetadata().getObjectID(), "UTF-8"); |
|
377 |
metadata.put("uri", URI); |
|
378 |
if (metadata.get("tags") == null) { |
|
379 |
metadata.put("tags", Sets.newHashSet("UNVISITED")); |
|
380 |
} else { |
|
381 |
final BasicDBList tagList = (BasicDBList) metadata.get("tags"); |
|
382 |
final Set<String> inputSet = Sets.newHashSet(); |
|
383 |
for (final Object o : tagList) { |
|
384 |
inputSet.add((String) o); |
|
385 |
} |
|
386 |
inputSet.add("UNVISITED"); |
|
387 |
metadata.put("tags", inputSet); |
|
388 |
} |
|
389 |
currentFile.setMetaData(metadata); |
|
390 |
currentFile.save(); |
|
391 |
|
|
392 |
} catch (Exception e) { |
|
393 |
log.error(e); |
|
394 |
throw new ObjectStoreServiceException(e); |
|
395 |
} finally { |
|
396 |
try { |
|
397 |
record.getInputStream().close(); |
|
398 |
} catch (IOException e) { |
|
399 |
log.error(e); |
|
400 |
} |
|
401 |
} |
|
402 |
return URI; |
|
403 |
} |
|
404 |
|
|
405 |
/* |
|
406 |
* (non-Javadoc) |
|
407 |
* |
|
408 |
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStore#feedMetadataRecord(java.lang.Iterable, boolean) |
|
409 |
*/ |
|
410 |
/** |
|
411 |
* Feed metadata record. |
|
412 |
* |
|
413 |
* @param records |
|
414 |
* the records |
|
415 |
* @param incremental |
|
416 |
* the incremental |
|
417 |
* @return the int |
|
418 |
*/ |
|
419 |
@Override |
|
420 |
public int feedMetadataRecord(final Iterable<MetadataObjectRecord> records, final boolean incremental) { |
|
421 |
final long timestamp = System.currentTimeMillis(); |
|
422 |
for (final MetadataObjectRecord o : records) { |
|
423 |
if (o == null || o.getRecord() == null) { |
|
424 |
log.debug("Null object metadata record"); |
|
425 |
continue; |
|
426 |
} |
|
427 |
|
|
428 |
final GridFSInputFile currentFile = collection.createFile(new ByteArrayInputStream(o.getRecord().getBytes())); |
|
429 |
currentFile.setId(o.getId()); |
|
430 |
final BasicDBObject metadata = new BasicDBObject(); |
|
431 |
metadata.put("id", o.getId()); |
|
432 |
metadata.put("mime", o.getMime()); |
|
433 |
metadata.put("timestamp", timestamp); |
|
434 |
try { |
|
435 |
final String URI = baseURI + "?objectStore=" + URLEncoder.encode(id, "UTF-8") + "&objectId=" + URLEncoder.encode(o.getId(), "UTF-8"); |
|
436 |
metadata.put("uri", URI); |
|
437 |
|
|
438 |
} catch (UnsupportedEncodingException e) { |
|
439 |
log.error("Got an exception during the feed ", e); |
|
440 |
} |
|
441 |
currentFile.setMetaData(metadata); |
|
442 |
currentFile.save(); |
|
443 |
} |
|
444 |
return this.getSize(); |
|
445 |
} |
|
446 |
|
|
447 |
/* |
|
448 |
* (non-Javadoc) |
|
449 |
* |
|
450 |
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStore#feed(java.lang.Iterable, boolean) |
|
451 |
*/ |
|
452 |
/** |
|
453 |
* Feed. |
|
454 |
* |
|
455 |
* @param records |
|
456 |
* the records |
|
457 |
* @param incremental |
|
458 |
* the incremental |
|
459 |
* @return the int |
|
460 |
*/ |
|
461 |
@Override |
|
462 |
public int feed(final Iterable<ObjectStoreRecord> records, final boolean incremental) { |
|
463 |
final long timestamp = System.currentTimeMillis(); |
|
464 |
for (final ObjectStoreRecord o : records) { |
|
465 |
if (o == null || o.getInputStream() == null) { |
|
466 |
if (o != null) { |
|
467 |
log.debug("Null object " + o.getFileMetadata().toJSON()); |
|
468 |
} else { |
|
469 |
log.debug("Null Object"); |
|
470 |
} |
|
471 |
try { |
|
472 |
Thread.sleep(1000); |
|
473 |
} catch (InterruptedException e) { |
|
474 |
log.error(e); |
|
475 |
} |
|
476 |
continue; |
|
477 |
} |
|
478 |
if (alreadyExist(o.getFileMetadata().getObjectID())) { |
|
479 |
try { |
|
480 |
o.getInputStream().close(); |
|
481 |
} catch (IOException e) { |
|
482 |
log.error("Error on closing inputStream ", e); |
|
483 |
} |
|
484 |
continue; |
|
485 |
} |
|
486 |
final GridFSInputFile currentFile = collection.createFile(o.getInputStream()); |
|
487 |
currentFile.setId(o.getFileMetadata().getObjectID()); |
|
488 |
currentFile.setFilename(o.getFileMetadata().getObjectID()); |
|
489 |
final BasicDBObject metadata = new BasicDBObject(); |
|
490 |
metadata.put("id", o.getFileMetadata().getObjectID()); |
|
491 |
metadata.put("mime", o.getFileMetadata().getMimeType()); |
|
492 |
metadata.put("originalObject", o.getFileMetadata().toJSON()); |
|
493 |
metadata.put("timestamp", timestamp); |
|
494 |
try { |
|
495 |
final String URI = baseURI + "?objectStore=" + URLEncoder.encode(id, "UTF-8") + "&objectId=" |
|
496 |
+ URLEncoder.encode(o.getFileMetadata().getObjectID(), "UTF-8"); |
|
497 |
metadata.put("uri", URI); |
|
498 |
|
|
499 |
} catch (UnsupportedEncodingException e) { |
|
500 |
log.error("Got an exception during the feed ", e); |
|
501 |
} |
|
502 |
currentFile.setMetaData(metadata); |
|
503 |
currentFile.save(); |
|
504 |
} |
|
505 |
return getSize(); |
|
506 |
} |
|
507 |
|
|
508 |
// ================================================================================ |
|
509 |
// Tag Section |
|
510 |
// ================================================================================ |
|
511 |
|
|
512 |
/** |
|
513 |
* Tag ids. |
|
514 |
* |
|
515 |
* @param ids |
|
516 |
* the ids |
|
517 |
* @param tag |
|
518 |
* the tag |
|
519 |
* @return the int |
|
520 |
*/ |
|
521 |
@Override |
|
522 |
public int tagIds(final Iterable<String> ids, final String tag) { |
|
523 |
if (ids == null) return 0; |
|
524 |
|
|
525 |
int counter = 0; |
|
526 |
for (final String id : ids) { |
|
527 |
DBObject query = BasicDBObjectBuilder.start().add("_id", id).get(); |
|
528 |
final GridFSDBFile foundObject = collection.findOne(query); |
|
529 |
final DBObject metadata = foundObject.getMetaData(); |
|
530 |
final DBObject currentTags = (DBObject) metadata.get("tags"); |
|
531 |
counter++; |
|
532 |
if (currentTags == null) { |
|
533 |
metadata.put("tags", tag); |
|
534 |
} else { |
|
535 |
final BasicDBList tagList = (BasicDBList) currentTags; |
|
536 |
final Set<String> inputSet = Sets.newHashSet(); |
|
537 |
for (final Object o : tagList) { |
|
538 |
inputSet.add((String) o); |
|
539 |
} |
|
540 |
inputSet.add(tag); |
|
541 |
metadata.put("tags", inputSet); |
|
542 |
} |
|
543 |
foundObject.save(); |
|
544 |
} |
|
545 |
return counter; |
|
546 |
|
|
547 |
} |
|
548 |
|
|
549 |
/** |
|
550 |
* Untag ids. |
|
551 |
* |
|
552 |
* @param ids |
|
553 |
* the ids |
|
554 |
* @param tag |
|
555 |
* the tag |
|
556 |
* @return the int |
|
557 |
*/ |
|
558 |
@Override |
|
559 |
public int untagIds(final Iterable<String> ids, final String tag) { |
|
560 |
if (ids == null) return 0; |
|
561 |
|
|
562 |
int counter = 0; |
|
563 |
for (final String id : ids) { |
|
564 |
DBObject query = BasicDBObjectBuilder.start().add("_id", id).get(); |
|
565 |
final GridFSDBFile foundObject = collection.findOne(query); |
|
566 |
final DBObject metadata = foundObject.getMetaData(); |
|
567 |
final DBObject currentTags = (DBObject) metadata.get("tags"); |
|
568 |
if (currentTags != null) { |
|
569 |
counter++; |
|
570 |
final BasicDBList tagList = (BasicDBList) currentTags; |
|
571 |
final Set<String> inputSet = Sets.newHashSet(); |
|
572 |
for (final Object o : tagList) { |
|
573 |
inputSet.add((String) o); |
|
574 |
} |
|
575 |
inputSet.remove(tag); |
|
576 |
metadata.put("tags", inputSet); |
|
577 |
} |
|
578 |
foundObject.save(); |
|
579 |
} |
|
580 |
return counter; |
|
581 |
} |
|
582 |
|
|
583 |
/** |
|
584 |
* Tag objectstore. |
|
585 |
* |
|
586 |
* @param tag |
|
587 |
* the tag |
|
588 |
* @return the int |
|
589 |
*/ |
|
590 |
@Override |
|
591 |
public int tagObjectstore(final String tag) { |
|
592 |
|
|
593 |
log.info(String.format("Starting to tag (%s) for object store %s" , tag, id)); |
|
594 |
final BasicDBObject fields = new BasicDBObject("metadata.tags", tag); |
|
595 |
final BasicDBObject update = new BasicDBObject("$push", fields); |
|
596 |
final DBCollection mongoCollection = collection.getDB().getCollection(collection.getBucketName() + ".files"); |
|
597 |
final WriteResult result = mongoCollection.update(new BasicDBObject(), update, true, true); |
|
598 |
return result.getN(); |
|
599 |
|
|
600 |
} |
|
601 |
|
|
602 |
/** |
|
603 |
* Untag objectstore. |
|
604 |
* |
|
605 |
* @param tag |
|
606 |
* the tag |
|
607 |
* @return the int |
|
608 |
*/ |
|
609 |
@Override |
|
610 |
public int untagObjectstore(final String tag) { |
|
611 |
|
|
612 |
log.info(String.format("Starting to untag (%s) for object store %s" , tag, id)); |
|
613 |
final BasicDBList objects = new BasicDBList(); |
|
614 |
objects.add(tag); |
|
615 |
final BasicDBObject fields = new BasicDBObject("metadata.tags", new BasicDBObject("$in", objects)); |
|
616 |
final BasicDBObject update = new BasicDBObject("$pull", fields); |
|
617 |
|
|
618 |
DBCollection mongoCollection = collection.getDB().getCollection(collection.getBucketName() + ".files"); |
|
619 |
|
|
620 |
final WriteResult result = mongoCollection.update(new BasicDBObject(), update, true, true); |
|
621 |
return result.getN(); |
|
622 |
|
|
623 |
} |
|
624 |
|
|
625 |
// ================================================================================ |
|
626 |
// Private Method |
|
627 |
// ================================================================================ |
|
628 |
|
|
629 |
/** |
|
630 |
* Check single item. |
|
631 |
* |
|
632 |
* |
|
633 |
* @param objectId |
|
634 |
* the object id |
|
635 |
* @param file |
|
636 |
* the file |
|
637 |
* @throws ObjectStoreFileNotFoundException |
|
638 |
* the object store file not found exception |
|
639 |
*/ |
|
640 |
private void checkSingleItem(final String objectId, final List<GridFSDBFile> file) throws ObjectStoreFileNotFoundException { |
|
641 |
if (file.isEmpty()) throw new ObjectStoreFileNotFoundException(String.format("Object file not found with id: %s", objectId)); |
|
642 |
if (file.size() > 1) throw new IllegalStateException(String.format("More than one objects found with id: %s", objectId)); |
|
643 |
} |
|
644 |
|
|
645 |
/** |
|
646 |
* Already exist. |
|
647 |
* |
|
648 |
* @param objectId |
|
649 |
* the object id |
|
650 |
* @return true, if successful |
|
651 |
*/ |
|
652 |
private boolean alreadyExist(final String objectId) { |
|
653 |
final BasicDBObject query = new BasicDBObject("_id", objectId); |
|
654 |
final List<GridFSDBFile> file = collection.find(query); |
|
655 |
return file != null && file.size() > 0; |
|
656 |
} |
|
657 |
|
|
658 |
@Override |
|
659 |
public List<Pair<String, Integer>> listTags() { |
|
660 |
String map = "function () {" + "this.metadata.tags.forEach(function(data) { emit (data, 1) } );" + "}"; |
|
661 |
|
|
662 |
String reduce = "function (key, values) { return Array.sum(values) } "; |
|
663 |
|
|
664 |
DBCollection inputCollection = collection.getDB().getCollection(this.id + ".files"); |
|
665 |
MapReduceCommand cmd = new MapReduceCommand(inputCollection, map, reduce, "", MapReduceCommand.OutputType.INLINE, QueryBuilder.start().get()); |
|
666 |
|
|
667 |
MapReduceOutput out = inputCollection.mapReduce(cmd); |
|
668 |
|
|
669 |
List<Pair<String, Integer>> tags = Lists.newArrayList(); |
|
670 |
|
|
671 |
for (DBObject o : out.results()) { |
|
672 |
String tag_id = (String) o.get("_id"); |
|
673 |
Double tag_count = (Double) o.get("value"); |
|
674 |
|
|
675 |
tags.add(new Pair<String, Integer>(tag_id, tag_count.intValue())); |
|
676 |
} |
|
677 |
return tags; |
|
678 |
} |
|
679 |
|
|
680 |
} |
modules/dnet-gridfs-objectstore/tags/dnet-gridfs-objectstore-4.0.4-TAG/src/main/java/eu/dnetlib/data/objectstore/modular/gridFS/MongoGridFSOptionsFactory.java | ||
---|---|---|
1 |
package eu.dnetlib.data.objectstore.modular.gridFS; |
|
2 |
|
|
3 |
import org.springframework.beans.BeansException; |
|
4 |
import org.springframework.beans.factory.FactoryBean; |
|
5 |
|
|
6 |
import com.mongodb.MongoOptions; |
|
7 |
|
|
8 |
public class MongoGridFSOptionsFactory implements FactoryBean<MongoOptions> { |
|
9 |
|
|
10 |
private int connectionsPerHost; |
|
11 |
|
|
12 |
@Override |
|
13 |
public MongoOptions getObject() throws BeansException { |
|
14 |
MongoOptions opts = new MongoOptions(); |
|
15 |
opts.connectionsPerHost = connectionsPerHost; |
|
16 |
return opts; |
|
17 |
} |
|
18 |
|
|
19 |
@Override |
|
20 |
public Class<MongoOptions> getObjectType() { |
|
21 |
return MongoOptions.class; |
|
22 |
} |
|
23 |
|
|
24 |
@Override |
|
25 |
public boolean isSingleton() { |
|
26 |
return false; |
|
27 |
} |
|
28 |
|
|
29 |
public int getConnectionsPerHost() { |
|
30 |
return connectionsPerHost; |
|
31 |
} |
|
32 |
|
|
33 |
public void setConnectionsPerHost(final int connectionsPerHost) { |
|
34 |
this.connectionsPerHost = connectionsPerHost; |
|
35 |
} |
|
36 |
|
|
37 |
} |
modules/dnet-gridfs-objectstore/tags/dnet-gridfs-objectstore-4.0.4-TAG/src/main/java/eu/dnetlib/data/objectstore/modular/gridFS/GridFSObjectstoreResultSetListener.java | ||
---|---|---|
1 |
package eu.dnetlib.data.objectstore.modular.gridFS; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
5 |
import java.util.Set; |
|
6 |
|
|
7 |
import com.google.common.collect.Lists; |
|
8 |
import com.mongodb.BasicDBObject; |
|
9 |
import com.mongodb.DBCursor; |
|
10 |
import com.mongodb.DBObject; |
|
11 |
import com.mongodb.QueryBuilder; |
|
12 |
import com.mongodb.gridfs.GridFS; |
|
13 |
import com.mongodb.gridfs.GridFSDBFile; |
|
14 |
import eu.dnetlib.enabling.resultset.ResultSet; |
|
15 |
import eu.dnetlib.enabling.resultset.ResultSetAware; |
|
16 |
import eu.dnetlib.enabling.resultset.ResultSetListener; |
|
17 |
import eu.dnetlib.miscutils.collections.MappedCollection; |
|
18 |
import org.apache.commons.logging.Log; |
|
19 |
import org.apache.commons.logging.LogFactory; |
|
20 |
|
|
21 |
/** |
|
22 |
* The listener interface for receiving gridFSObjectstoreResultSet events. The class that is interested in processing a |
|
23 |
* gridFSObjectstoreResultSet event implements this interface, and the object created with that class is registered with a component using |
|
24 |
* the component's <code>addGridFSObjectstoreResultSetListener<code> method. When the gridFSObjectstoreResultSet event occurs, that object's |
|
25 |
* appropriate method is invoked. |
|
26 |
* |
|
27 |
*/ |
|
28 |
public class GridFSObjectstoreResultSetListener implements ResultSetListener, ResultSetAware { |
|
29 |
|
|
30 |
/** The Constant log. */ |
|
31 |
private static final Log log = LogFactory.getLog(GridFSObjectstoreResultSetListener.class); |
|
32 |
|
|
33 |
/** The from date. */ |
|
34 |
private Long fromDate; |
|
35 |
|
|
36 |
/** The until date. */ |
|
37 |
private Long untilDate; |
|
38 |
|
|
39 |
/** The records. */ |
|
40 |
private List<String> records; |
|
41 |
|
|
42 |
/** The object store id. */ |
|
43 |
private String objectStoreID; |
|
44 |
|
|
45 |
/** The collection. */ |
|
46 |
private GridFS collection; |
|
47 |
|
|
48 |
/** The base uri. */ |
|
49 |
private String baseURI; |
|
50 |
|
|
51 |
/** The current size. */ |
|
52 |
private int currentSize = -1; |
|
53 |
|
|
54 |
/** The current cursor. */ |
|
55 |
private DBCursor currentCursor; |
|
56 |
|
|
57 |
/** The cursor position. */ |
|
58 |
private long cursorPosition; |
|
59 |
|
|
60 |
/** The tags. */ |
|
61 |
private Set<String> tags; |
|
62 |
|
|
63 |
/* |
|
64 |
* (non-Javadoc) |
|
65 |
* |
|
66 |
* @see eu.dnetlib.enabling.resultset.TypedResultSetListener#getResult(int, int) |
|
67 |
*/ |
|
68 |
@Override |
|
69 |
public List<String> getResult(final int from, final int to) { |
|
70 |
log.debug(String.format("ObjectStoreId :%s, from: %d, to: %d", objectStoreID, from, to)); |
|
71 |
if (records != null) { |
|
72 |
List<String> ids = Lists.newArrayList(); |
|
73 |
for (int i = from; i < to; i++) { |
|
74 |
ids.add(records.get(i)); |
|
75 |
} |
|
76 |
QueryBuilder qBuilder = QueryBuilder.start("metadata.id").in(ids); |
|
77 |
DBObject q = qBuilder.get(); |
|
78 |
List<GridFSDBFile> out = collection.find(q); |
|
79 |
|
|
80 |
return MappedCollection.listMap(out, ObjectStoreFileUtility.asJSON(baseURI, objectStoreID)); |
|
81 |
} else if (tags != null && !tags.isEmpty()) { |
|
82 |
if (currentCursor == null || cursorPosition > from) { |
|
83 |
createCurrentCursor(this.tags); |
|
84 |
} |
|
85 |
while (cursorPosition < from) { |
|
86 |
currentCursor.next(); |
|
87 |
cursorPosition++; |
|
88 |
} |
|
89 |
ArrayList<GridFSDBFile> out = new ArrayList<GridFSDBFile>(); |
|
90 |
|
|
91 |
for (int i = from; i <= to; i++) { |
|
92 |
if (currentCursor.hasNext()) { |
|
93 |
out.add((GridFSDBFile) currentCursor.next()); |
|
94 |
cursorPosition++; |
|
95 |
} |
|
96 |
} |
|
97 |
|
|
98 |
return MappedCollection.listMap(out, ObjectStoreFileUtility.asJSON(baseURI, objectStoreID)); |
|
99 |
|
|
100 |
} |
|
101 |
|
|
102 |
else if (fromDate != null && untilDate != null) { |
|
103 |
if (currentCursor == null || cursorPosition > from) { |
|
104 |
createCurrentCursor(); |
|
105 |
} |
|
106 |
while (cursorPosition < from) { |
|
107 |
currentCursor.next(); |
|
108 |
cursorPosition++; |
|
109 |
} |
|
110 |
ArrayList<GridFSDBFile> out = new ArrayList<GridFSDBFile>(); |
|
111 |
|
|
112 |
for (int i = from; i <= to; i++) { |
|
113 |
if (currentCursor.hasNext()) { |
|
114 |
out.add((GridFSDBFile) currentCursor.next()); |
|
115 |
cursorPosition++; |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
return MappedCollection.listMap(out, ObjectStoreFileUtility.asJSON(baseURI, objectStoreID)); |
|
120 |
} |
|
121 |
|
|
122 |
throw new IllegalArgumentException("Missing parameters on Delivery must provide either from, to, or ObjectStoreIDs"); |
|
123 |
} |
|
124 |
|
|
125 |
/** |
|
126 |
* Creates the current cursor. |
|
127 |
*/ |
|
128 |
private void createCurrentCursor(final Set<String> tags) { |
|
129 |
QueryBuilder qBuilder = QueryBuilder.start("metadata.tags").in(tags); |
|
130 |
if (currentCursor != null) { |
|
131 |
currentCursor.close(); |
|
132 |
} |
|
133 |
currentCursor = collection.getFileList(qBuilder.get()).sort(new BasicDBObject("_id", 1)); |
|
134 |
currentCursor.batchSize(20); |
|
135 |
cursorPosition = 1; |
|
136 |
} |
|
137 |
|
|
138 |
/** |
|
139 |
* Creates the current cursor. |
|
140 |
*/ |
|
141 |
private void createCurrentCursor() { |
|
142 |
BasicDBObject query = new BasicDBObject(); |
|
143 |
query.put("$gt", fromDate.doubleValue()); |
|
144 |
query.put("$lt", untilDate.doubleValue()); |
|
145 |
if (currentCursor != null) { |
|
146 |
currentCursor.close(); |
|
147 |
} |
|
148 |
|
|
149 |
currentCursor = collection.getFileList(new BasicDBObject("metadata.timestamp", query)).sort(new BasicDBObject("_id", 1)); |
|
150 |
currentCursor.batchSize(20); |
|
151 |
cursorPosition = 1; |
|
152 |
} |
|
153 |
|
|
154 |
/* |
|
155 |
* (non-Javadoc) |
|
156 |
* |
|
157 |
* @see eu.dnetlib.enabling.resultset.TypedResultSetListener#getSize() |
|
158 |
*/ |
|
159 |
@Override |
|
160 |
public int getSize() { |
|
161 |
if (currentSize == -1) { |
|
162 |
currentSize = calculateSize(); |
|
163 |
} |
|
164 |
return currentSize; |
|
165 |
} |
|
166 |
|
|
167 |
/** |
|
168 |
* Calculate size. |
|
169 |
* |
|
170 |
* @return the int |
|
171 |
*/ |
|
172 |
private int calculateSize() { |
|
173 |
if (records != null) { |
|
174 |
QueryBuilder qBuilder = QueryBuilder.start("metadata.id").in(records); |
|
175 |
DBObject q = qBuilder.get(); |
|
176 |
List<GridFSDBFile> out = collection.find(q); |
|
177 |
return out.size(); |
|
178 |
} else if (fromDate != null && untilDate != null) { |
|
179 |
BasicDBObject query = new BasicDBObject(); |
|
180 |
query.put("$gt", fromDate.doubleValue()); |
|
181 |
query.put("$lt", untilDate.doubleValue()); |
|
182 |
return collection.getFileList(new BasicDBObject("metadata.timestamp", query)).size(); |
|
183 |
} else if (tags != null) { |
|
184 |
QueryBuilder qBuilder = QueryBuilder.start("metadata.tags").in(tags); |
|
185 |
DBObject q = qBuilder.get(); |
|
186 |
List<GridFSDBFile> out = collection.find(q); |
|
187 |
return out.size(); |
|
188 |
} |
|
189 |
return 0; |
|
190 |
} |
|
191 |
|
|
192 |
/* |
|
193 |
* (non-Javadoc) |
|
194 |
* |
|
195 |
* @see eu.dnetlib.enabling.resultset.ResultSetAware#setResultSet(eu.dnetlib.enabling.resultset.ResultSet) |
|
196 |
*/ |
|
197 |
@Override |
|
198 |
public void setResultSet(final ResultSet resultSet) { |
|
199 |
resultSet.close(); |
|
200 |
} |
|
201 |
|
|
202 |
/** |
|
203 |
* Gets the from date. |
|
204 |
* |
|
205 |
* @return the from date |
|
206 |
*/ |
|
207 |
public Long getFromDate() { |
|
208 |
return fromDate; |
|
209 |
} |
|
210 |
|
|
211 |
/** |
|
212 |
* Sets the from date. |
|
213 |
* |
|
214 |
* @param fromdate |
|
215 |
* the new from date |
|
216 |
*/ |
|
217 |
public void setFromDate(final Long fromdate) { |
|
218 |
this.fromDate = fromdate; |
|
219 |
} |
|
220 |
|
|
221 |
/** |
|
222 |
* Gets the until date. |
|
223 |
* |
|
224 |
* @return the until date |
|
225 |
*/ |
|
226 |
public Long getUntilDate() { |
|
227 |
return untilDate; |
|
228 |
} |
|
229 |
|
|
230 |
/** |
|
231 |
* Sets the until date. |
|
232 |
* |
|
233 |
* @param untilDate |
|
234 |
* the new until date |
|
235 |
*/ |
|
236 |
public void setUntilDate(final Long untilDate) { |
|
237 |
this.untilDate = untilDate; |
|
238 |
} |
|
239 |
|
|
240 |
/** |
|
241 |
* Gets the records. |
|
242 |
* |
|
243 |
* @return the records |
|
244 |
*/ |
|
245 |
public List<String> getRecords() { |
|
246 |
return records; |
|
247 |
} |
|
248 |
|
|
249 |
/** |
|
250 |
* Sets the records. |
|
251 |
* |
|
252 |
* @param records |
|
253 |
* the new records |
|
254 |
*/ |
|
255 |
public void setRecords(final List<String> records) { |
|
256 |
this.records = records; |
|
257 |
} |
|
258 |
|
|
259 |
/** |
|
260 |
* Gets the collection. |
|
261 |
* |
|
262 |
* @return the collection |
|
263 |
*/ |
|
264 |
public GridFS getCollection() { |
|
265 |
return collection; |
|
266 |
} |
|
267 |
|
|
268 |
/** |
|
269 |
* Sets the collection. |
|
270 |
* |
|
271 |
* @param collection |
|
272 |
* the new collection |
|
273 |
*/ |
|
274 |
public void setCollection(final GridFS collection) { |
|
275 |
this.collection = collection; |
|
276 |
} |
|
277 |
|
|
278 |
/** |
|
279 |
* Gets the base uri. |
|
280 |
* |
|
281 |
* @return the base uri |
|
282 |
*/ |
|
283 |
public String getBaseURI() { |
|
284 |
return baseURI; |
|
285 |
} |
|
286 |
|
|
287 |
/** |
|
288 |
* Sets the base uri. |
|
289 |
* |
|
290 |
* @param baseURI |
|
291 |
* the new base uri |
|
292 |
*/ |
|
293 |
public void setBaseURI(final String baseURI) { |
|
294 |
this.baseURI = baseURI; |
|
295 |
} |
|
296 |
|
|
297 |
/** |
|
298 |
* Gets the object store id. |
|
299 |
* |
|
300 |
* @return the object store id |
|
301 |
*/ |
|
302 |
public String getObjectStoreID() { |
|
303 |
return objectStoreID; |
|
304 |
} |
|
305 |
|
|
306 |
/** |
|
307 |
* Sets the object store id. |
|
308 |
* |
|
309 |
* @param objectStoreID |
|
310 |
* the new object store id |
|
311 |
*/ |
|
312 |
public void setObjectStoreID(final String objectStoreID) { |
|
313 |
this.objectStoreID = objectStoreID; |
|
314 |
} |
|
315 |
|
|
316 |
/** |
|
317 |
* @return the tags |
|
318 |
*/ |
|
319 |
public Set<String> getTags() { |
|
320 |
return tags; |
|
321 |
} |
|
322 |
|
|
323 |
/** |
|
324 |
* @param tags |
|
325 |
* the tags to set |
|
326 |
*/ |
|
327 |
public void setTags(final Set<String> tags) { |
|
328 |
this.tags = tags; |
|
329 |
} |
|
330 |
} |
modules/dnet-gridfs-objectstore/tags/dnet-gridfs-objectstore-4.0.4-TAG/src/main/java/eu/dnetlib/data/objectstore/modular/gridFS/ObjectStoreFileUtility.java | ||
---|---|---|
1 |
package eu.dnetlib.data.objectstore.modular.gridFS; |
|
2 |
|
|
3 |
import java.io.UnsupportedEncodingException; |
|
4 |
import java.net.URLEncoder; |
|
5 |
import java.util.Set; |
|
6 |
|
|
7 |
import org.apache.commons.logging.Log; |
|
8 |
import org.apache.commons.logging.LogFactory; |
|
9 |
|
|
10 |
import com.google.common.collect.Sets; |
|
11 |
import com.mongodb.BasicDBList; |
|
12 |
import com.mongodb.DBObject; |
|
13 |
import com.mongodb.gridfs.GridFSDBFile; |
|
14 |
|
|
15 |
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFile; |
|
16 |
import eu.dnetlib.data.objectstore.rmi.Protocols; |
|
17 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
18 |
|
|
19 |
/** |
|
20 |
* The Class ObjectStoreFileBuilder generates an objectStoreFile bean |
|
21 |
* |
|
22 |
*/ |
|
23 |
public class ObjectStoreFileUtility { |
|
24 |
|
|
25 |
private static final int KB_SIZE = 1024; |
|
26 |
|
|
27 |
/** The Constant log. */ |
|
28 |
private static final Log log = LogFactory.getLog(ObjectStoreFileUtility.class); |
|
29 |
|
|
30 |
/** |
|
31 |
* Builds the. |
|
32 |
* |
|
33 |
* @param input |
|
34 |
* the input File |
|
35 |
* @param baseURI |
|
36 |
* the base URI to associate to the metadata |
|
37 |
* @param objectStoreID |
|
38 |
* The objectStoreID the input file |
|
39 |
* @return the object store file |
|
40 |
*/ |
|
41 |
|
|
42 |
public static ObjectStoreFile build(final GridFSDBFile input, final String baseURI, final String objectStoreID) { |
|
43 |
DBObject metadata = input.getMetaData(); |
|
44 |
String originalFile = (String) metadata.get("originalObject"); |
|
45 |
ObjectStoreFile original = ObjectStoreFile.createObject(originalFile); |
|
46 |
ObjectStoreFile newFile = new ObjectStoreFile(); |
|
47 |
newFile.setObjectID((String) metadata.get("id")); |
|
48 |
newFile.setAccessProtocol(Protocols.HTTP); |
|
49 |
newFile.setMimeType((String) metadata.get("mime")); |
|
50 |
newFile.setMd5Sum((String) input.get("md5")); |
|
51 |
newFile.setFileSizeKB(input.getLength() / KB_SIZE); |
|
52 |
BasicDBList metadataTag = (BasicDBList) metadata.get("tags"); |
|
53 |
if (metadataTag != null) { |
|
54 |
Set<String> tagFiles = Sets.newHashSet(); |
|
55 |
for (Object tag : metadataTag) { |
|
56 |
tagFiles.add((String) tag); |
|
57 |
} |
|
58 |
newFile.setTags(tagFiles); |
|
59 |
} |
|
60 |
if (originalFile != null) { |
|
61 |
newFile.setMetadataRelatedID(original.getMetadataRelatedID()); |
|
62 |
if (original.getDownloadedURL() == null || original.getDownloadedURL().length() == 0) { |
|
63 |
newFile.setDownloadedURL(original.getURI()); |
|
64 |
} else { |
|
65 |
newFile.setDownloadedURL(original.getDownloadedURL()); |
|
66 |
} |
|
67 |
|
|
68 |
} |
|
69 |
try { |
|
70 |
newFile.setURI(baseURI + "?objectStore=" + URLEncoder.encode(objectStoreID, "UTF-8") + "&objectId=" |
|
71 |
+ URLEncoder.encode(newFile.getObjectID(), "UTF-8")); |
|
72 |
} catch (UnsupportedEncodingException e) { |
|
73 |
log.error("Error on Build objectStoreFile ", e); |
|
74 |
} |
|
75 |
return newFile; |
|
76 |
} |
|
77 |
|
|
78 |
public static UnaryFunction<String, GridFSDBFile> asJSON(final String baseURI, final String objectStoreID) { |
|
79 |
return new UnaryFunction<String, GridFSDBFile>() { |
|
80 |
|
|
81 |
@Override |
|
82 |
public String evaluate(final GridFSDBFile input) { |
|
83 |
return build(input, baseURI, objectStoreID).toJSON(); |
|
84 |
|
|
85 |
} |
|
86 |
}; |
|
87 |
} |
|
88 |
} |
modules/dnet-gridfs-objectstore/tags/dnet-gridfs-objectstore-4.0.4-TAG/src/main/java/eu/dnetlib/data/objectstore/modular/gridFS/GridFSObjectstoreDaoImpl.java | ||
---|---|---|
1 |
package eu.dnetlib.data.objectstore.modular.gridFS; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
import java.util.regex.Pattern; |
|
5 |
|
|
6 |
import org.bson.BSONObject; |
|
7 |
import org.springframework.beans.factory.annotation.Required; |
|
8 |
|
|
9 |
import com.mongodb.BasicDBObject; |
|
10 |
import com.mongodb.DB; |
|
11 |
import com.mongodb.DBCollection; |
|
12 |
import com.mongodb.DBObject; |
|
13 |
import com.mongodb.WriteConcern; |
|
14 |
import com.mongodb.gridfs.GridFS; |
|
15 |
|
|
16 |
import eu.dnetlib.data.objectstore.modular.connector.ObjectStore; |
|
17 |
import eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao; |
|
18 |
import eu.dnetlib.miscutils.collections.MappedCollection; |
|
19 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
20 |
|
|
21 |
@SuppressWarnings("all") |
|
22 |
public class GridFSObjectstoreDaoImpl implements ObjectStoreDao { |
|
23 |
|
|
24 |
public static final String INTERPRETATION = "interpretation"; |
|
25 |
private final static String OBJECTSTOREMETADATANAME = "metadataObjectStore"; |
|
26 |
private final static String OBJECTSTOREIDFIELD = "obsId"; |
|
27 |
private DB db; |
|
28 |
private boolean upsert; |
|
29 |
private String objectStoreRESTURI; |
|
30 |
|
|
31 |
@Override |
|
32 |
public ObjectStore getObjectStore(String obsId) { |
|
33 |
db.setWriteConcern(WriteConcern.SAFE); |
|
34 |
obsId = obsId.substring(0, 36); |
|
35 |
GridFSObjectStore objectStore = new GridFSObjectStore(obsId, new GridFS(db, obsId), this.upsert); |
|
36 |
objectStore.setBaseURI(objectStoreRESTURI); |
|
37 |
checkIndexes(obsId); |
|
38 |
return objectStore; |
|
39 |
} |
|
40 |
|
|
41 |
private void checkIndexes(final String id) { |
|
42 |
DBCollection coll = db.getCollection(id + ".files"); |
|
43 |
List<DBObject> indexInfo = coll.getIndexInfo(); |
|
44 |
boolean hasTimestampIndex = false; |
|
45 |
for (DBObject info : indexInfo) { |
|
46 |
BSONObject boj = (BSONObject) info.get("key"); |
|
47 |
if (boj.get("metadata.timestamp") != null) { |
|
48 |
hasTimestampIndex = true; |
|
49 |
} |
|
50 |
} |
|
51 |
if (!hasTimestampIndex) { |
|
52 |
coll.createIndex(new BasicDBObject("metadata.timestamp", 1)); |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
@Override |
|
57 |
public List<String> listObjectStores() { |
|
58 |
DBCollection metadata = db.getCollection(OBJECTSTOREMETADATANAME); |
|
59 |
return MappedCollection.listMap(metadata.find(), new UnaryFunction<String, DBObject>() { |
|
60 |
|
|
61 |
@Override |
|
62 |
public String evaluate(final DBObject object) { |
|
63 |
return (String) object.get(OBJECTSTOREIDFIELD); |
|
64 |
} |
|
65 |
}); |
|
66 |
|
|
67 |
} |
|
68 |
|
|
69 |
@Override |
|
70 |
public boolean createObjectStore(final String obsId, final String interpretation) { |
|
71 |
DBCollection coll = db.getCollection(OBJECTSTOREMETADATANAME); |
|
72 |
final BasicDBObject obj = new BasicDBObject(); |
|
73 |
obj.put(OBJECTSTOREIDFIELD, obsId); |
|
74 |
obj.put(INTERPRETATION, interpretation); |
|
75 |
coll.save(obj); |
|
76 |
return true; |
|
77 |
} |
|
78 |
|
|
79 |
@Override |
|
80 |
public boolean updateObjectStore(final String obsId, final String interpretation) { |
|
81 |
DBCollection coll = db.getCollection(OBJECTSTOREMETADATANAME); |
|
82 |
final BasicDBObject obj = new BasicDBObject(); |
|
83 |
obj.put(OBJECTSTOREIDFIELD, obsId); |
|
84 |
obj.put(INTERPRETATION, interpretation); |
|
85 |
coll.update(new BasicDBObject(OBJECTSTOREIDFIELD, obsId), obj, true, false); |
|
86 |
coll.save(obj); |
|
87 |
return true; |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public boolean deleteObjectStore(String obsId) { |
|
92 |
DBCollection coll = db.getCollection(OBJECTSTOREMETADATANAME); |
|
93 |
coll.remove(new BasicDBObject(OBJECTSTOREIDFIELD, obsId)); |
|
94 |
obsId = obsId.substring(0, 36); |
|
95 |
GridFS objectStore = new GridFS(db, obsId); |
|
96 |
Pattern any = Pattern.compile("."); |
|
97 |
BasicDBObject query = new BasicDBObject("md5", any); |
|
98 |
objectStore.remove(query); |
|
99 |
return true; |
|
100 |
} |
|
101 |
|
|
102 |
public DB getDb() { |
|
103 |
return db; |
|
104 |
} |
|
105 |
|
|
106 |
@Required |
|
107 |
public void setDb(final DB db) { |
|
108 |
this.db = db; |
|
109 |
} |
|
110 |
|
|
111 |
public boolean isUpsert() { |
|
112 |
return upsert; |
|
113 |
} |
|
114 |
|
|
115 |
@Required |
|
116 |
public void setUpsert(final boolean upsert) { |
|
117 |
this.upsert = upsert; |
|
118 |
} |
|
119 |
|
|
120 |
public String getObjectStoreRESTURI() { |
|
121 |
return objectStoreRESTURI; |
|
122 |
} |
|
123 |
|
|
124 |
@Required |
|
125 |
public void setObjectStoreRESTURI(final String objectStoreRESTURI) { |
|
126 |
this.objectStoreRESTURI = objectStoreRESTURI; |
|
127 |
} |
|
128 |
|
|
129 |
} |
modules/dnet-gridfs-objectstore/tags/dnet-gridfs-objectstore-4.0.4-TAG/src/main/resources/eu/dnetlib/data/objectstore/modular/gridFS/applicationContext-gridFS-objectstore.properties | ||
---|---|---|
1 |
services.objectStore.mongodb.host=localhost |
|
2 |
services.objectStore.mongodb.port=27017 |
|
3 |
services.objectStore.mongodb.db=objectStore |
|
4 |
services.objectStore.mongodb.connectionsPerHost=20 |
|
5 |
services.objectStore.mongodb.upsert=true |
|
6 |
services.objectStore.RESTURI=http://${container.hostname}:${container.port}/${container.context}/mvc/objectStore/retrieve.do |
modules/dnet-gridfs-objectstore/tags/dnet-gridfs-objectstore-4.0.4-TAG/src/main/resources/eu/dnetlib/data/objectstore/modular/gridFS/applicationContext-gridFS-objectstore.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<beans xmlns="http://www.springframework.org/schema/beans" |
|
3 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" |
|
4 |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> |
|
5 |
<bean id="gridFSObjectstoreDao" |
|
6 |
class="eu.dnetlib.data.objectstore.modular.gridFS.GridFSObjectstoreDaoImpl" |
|
7 |
p:db-ref="objectstoreMongoDB" |
|
8 |
p:objectStoreRESTURI-ref="restURIString" |
|
9 |
p:upsert="${services.objectStore.mongodb.upsert}" |
|
10 |
/> |
|
11 |
|
|
12 |
<bean id="restURIString" class="java.lang.String"> |
|
13 |
<constructor-arg value="${services.objectStore.RESTURI}"/> |
|
14 |
</bean> |
|
15 |
|
|
16 |
<bean id="objectStoreMongoServer" class="com.mongodb.Mongo"> |
|
17 |
<constructor-arg index="0" type="com.mongodb.ServerAddress"> |
|
18 |
<bean class="com.mongodb.ServerAddress"> |
|
19 |
<constructor-arg index="0" |
|
20 |
value="${services.objectStore.mongodb.host}" /> |
|
21 |
<constructor-arg index="1" |
|
22 |
value="${services.objectStore.mongodb.port}" /> |
|
23 |
</bean> |
|
24 |
</constructor-arg> |
|
25 |
<constructor-arg index="1" type="com.mongodb.MongoOptions"> |
|
26 |
<bean class="eu.dnetlib.data.objectstore.modular.gridFS.MongoGridFSOptionsFactory" |
|
27 |
p:connectionsPerHost="${services.objectStore.mongodb.connectionsPerHost}" |
|
28 |
/> |
|
29 |
</constructor-arg> |
|
30 |
</bean> |
|
31 |
|
|
32 |
<bean id="objectstoreMongoDB" factory-bean="objectStoreMongoServer" |
|
33 |
factory-method="getDB"> |
|
34 |
<constructor-arg index="0" |
|
35 |
value="${services.objectStore.mongodb.db}" /> |
|
36 |
</bean> |
|
37 |
|
|
38 |
|
|
39 |
</beans> |
Also available in: Unified diff
[maven-release-plugin] copy for tag dnet-gridfs-objectstore-4.0.4-TAG