1
|
package eu.dnetlib.data.objectstore.modular.gridFS;
|
2
|
|
3
|
import com.mongodb.*;
|
4
|
import com.mongodb.gridfs.GridFS;
|
5
|
import eu.dnetlib.data.objectstore.modular.connector.ObjectStore;
|
6
|
import eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao;
|
7
|
import eu.dnetlib.miscutils.collections.MappedCollection;
|
8
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
9
|
import org.bson.BSONObject;
|
10
|
import org.springframework.beans.factory.annotation.Required;
|
11
|
|
12
|
import java.util.List;
|
13
|
import java.util.regex.Pattern;
|
14
|
|
15
|
@SuppressWarnings("ALL")
|
16
|
public class GridFSObjectstoreDaoImpl implements ObjectStoreDao {
|
17
|
|
18
|
public static final String INTERPRETATION = "interpretation";
|
19
|
private final static String OBJECTSTOREMETADATANAME = "metadataObjectStore";
|
20
|
private final static String OBJECTSTOREIDFIELD = "obsId";
|
21
|
private DB db;
|
22
|
private boolean upsert;
|
23
|
private String objectStoreRESTURI;
|
24
|
|
25
|
@Override
|
26
|
public ObjectStore getObjectStore(String obsId) {
|
27
|
db.setWriteConcern(WriteConcern.SAFE);
|
28
|
obsId = obsId.substring(0, 36);
|
29
|
GridFSObjectStore objectStore = new GridFSObjectStore(obsId, new GridFS(db, obsId), this.upsert);
|
30
|
objectStore.setBaseURI(objectStoreRESTURI);
|
31
|
checkIndexes(obsId);
|
32
|
return objectStore;
|
33
|
}
|
34
|
|
35
|
private void checkIndexes(final String id) {
|
36
|
DBCollection coll = db.getCollection(id + ".files");
|
37
|
List<DBObject> indexInfo = coll.getIndexInfo();
|
38
|
boolean hasTimestampIndex = false;
|
39
|
for (DBObject info : indexInfo) {
|
40
|
BSONObject boj = (BSONObject) info.get("key");
|
41
|
if (boj.get("metadata.timestamp") != null) {
|
42
|
hasTimestampIndex = true;
|
43
|
}
|
44
|
}
|
45
|
if (!hasTimestampIndex) {
|
46
|
coll.ensureIndex("metadata.timestamp");
|
47
|
}
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
public List<String> listObjectStores() {
|
52
|
DBCollection metadata = db.getCollection(OBJECTSTOREMETADATANAME);
|
53
|
return MappedCollection.listMap(metadata.find(), new UnaryFunction<String, DBObject>() {
|
54
|
|
55
|
@Override
|
56
|
public String evaluate(final DBObject object) {
|
57
|
return (String) object.get(OBJECTSTOREIDFIELD);
|
58
|
}
|
59
|
});
|
60
|
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
public boolean createObjectStore(final String obsId, final String interpretation) {
|
65
|
DBCollection coll = db.getCollection(OBJECTSTOREMETADATANAME);
|
66
|
final BasicDBObject obj = new BasicDBObject();
|
67
|
obj.put(OBJECTSTOREIDFIELD, obsId);
|
68
|
obj.put(INTERPRETATION, interpretation);
|
69
|
coll.save(obj);
|
70
|
return true;
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
public boolean updateObjectStore(final String obsId, final String interpretation) {
|
75
|
DBCollection coll = db.getCollection(OBJECTSTOREMETADATANAME);
|
76
|
final BasicDBObject obj = new BasicDBObject();
|
77
|
obj.put(OBJECTSTOREIDFIELD, obsId);
|
78
|
obj.put(INTERPRETATION, interpretation);
|
79
|
coll.update(new BasicDBObject(OBJECTSTOREIDFIELD, obsId), obj, true, false);
|
80
|
coll.save(obj);
|
81
|
return true;
|
82
|
}
|
83
|
|
84
|
@Override
|
85
|
public boolean deleteObjectStore(String obsId) {
|
86
|
DBCollection coll = db.getCollection(OBJECTSTOREMETADATANAME);
|
87
|
coll.remove(new BasicDBObject(OBJECTSTOREIDFIELD, obsId));
|
88
|
obsId = obsId.substring(0, 36);
|
89
|
GridFS objectStore = new GridFS(db, obsId);
|
90
|
Pattern any = Pattern.compile(".");
|
91
|
BasicDBObject query = new BasicDBObject("md5", any);
|
92
|
objectStore.remove(query);
|
93
|
return true;
|
94
|
}
|
95
|
|
96
|
public DB getDb() {
|
97
|
return db;
|
98
|
}
|
99
|
|
100
|
@Required
|
101
|
public void setDb(final DB db) {
|
102
|
this.db = db;
|
103
|
}
|
104
|
|
105
|
public boolean isUpsert() {
|
106
|
return upsert;
|
107
|
}
|
108
|
|
109
|
@Required
|
110
|
public void setUpsert(final boolean upsert) {
|
111
|
this.upsert = upsert;
|
112
|
}
|
113
|
|
114
|
public String getObjectStoreRESTURI() {
|
115
|
return objectStoreRESTURI;
|
116
|
}
|
117
|
|
118
|
@Required
|
119
|
public void setObjectStoreRESTURI(final String objectStoreRESTURI) {
|
120
|
this.objectStoreRESTURI = objectStoreRESTURI;
|
121
|
}
|
122
|
|
123
|
}
|