1
|
package eu.dnetlib.data.objectstore.s3;
|
2
|
|
3
|
import com.mongodb.BasicDBObject;
|
4
|
import com.mongodb.client.MongoCollection;
|
5
|
import com.mongodb.client.MongoDatabase;
|
6
|
import com.mongodb.client.model.Filters;
|
7
|
import com.mongodb.client.model.IndexModel;
|
8
|
import com.mongodb.client.result.DeleteResult;
|
9
|
import com.mongodb.client.result.UpdateResult;
|
10
|
import eu.dnetlib.data.objectstore.modular.connector.ObjectStore;
|
11
|
import eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao;
|
12
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFileNotFoundException;
|
13
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreServiceException;
|
14
|
import org.apache.commons.lang3.StringUtils;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
import org.bson.Document;
|
18
|
import org.bson.conversions.Bson;
|
19
|
import org.springframework.beans.factory.annotation.Value;
|
20
|
|
21
|
import javax.annotation.Resource;
|
22
|
import java.util.Arrays;
|
23
|
import java.util.List;
|
24
|
import java.util.stream.Collectors;
|
25
|
import java.util.stream.StreamSupport;
|
26
|
|
27
|
public class S3ObjectStoreDao implements ObjectStoreDao {
|
28
|
public static final String INTERPRETATION_FIELD = "interpretation";
|
29
|
public final static String OBJECTSTORE_METADATA_NAME_FIELD = "metadataObjectStore";
|
30
|
public final static String OBJECTSTORE_ID_FIELD = "obsId";
|
31
|
private static final String OBJECTSTORE_PROFILE_SUFFIX = "_T2JqZWN0U3RvcmVEU1Jlc291cmNlcy9PYmplY3RTdG9yZURTUmVzb3VyY2VUeXBl";
|
32
|
private static final Log log = LogFactory.getLog(S3ObjectStoreDao.class);
|
33
|
|
34
|
private static final List<IndexModel> metadataIndexes = Arrays.asList(new IndexModel(new Document().append("id",1)), new IndexModel(new Document().append("timestamp",1)));
|
35
|
|
36
|
@Resource(name="objectstoreMongoDB")
|
37
|
private MongoDatabase db;
|
38
|
|
39
|
@Value("${dnet.objectStore.s3.accessKey}")
|
40
|
private String accessKey;
|
41
|
|
42
|
|
43
|
@Value("${dnet.objectStore.s3.secretKey}")
|
44
|
private String secretKey;
|
45
|
|
46
|
@Value("${dnet.objectStore.s3.endPoint}")
|
47
|
private String s3EndPoint;
|
48
|
|
49
|
@Value("${dnet.objectStore.s3.objectStoreBucket}")
|
50
|
private String objectStoreBucket;
|
51
|
|
52
|
|
53
|
private Document getObjectStoreMetadata(final String objectStoreId) {
|
54
|
String find_id = objectStoreId;
|
55
|
if (objectStoreId.length() == 36) {
|
56
|
find_id += OBJECTSTORE_PROFILE_SUFFIX;
|
57
|
}
|
58
|
final MongoCollection<Document> metadataObjectStore = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD);
|
59
|
final Bson query = Filters.eq(OBJECTSTORE_ID_FIELD, find_id);
|
60
|
log.debug("QUERY :" + query.toString());
|
61
|
final Document resultQuery = metadataObjectStore.find(query).first();
|
62
|
log.debug("result " + resultQuery);
|
63
|
return resultQuery;
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public ObjectStore getObjectStore(final String objectStoreId) throws ObjectStoreServiceException {
|
68
|
if (StringUtils.isBlank(objectStoreId)) throw new ObjectStoreServiceException("Error on getting ObjectStore, id is Blank");
|
69
|
final Document resultQuery = getObjectStoreMetadata(objectStoreId);
|
70
|
if ((resultQuery == null)) throw new ObjectStoreFileNotFoundException("the objectStore with identifier: "+objectStoreId+" was not found");
|
71
|
final MongoCollection<Document> collection = getDb().getCollection(objectStoreId.substring(0,36));
|
72
|
collection.createIndexes(metadataIndexes);
|
73
|
return new S3ObjectStore(resultQuery.getString(OBJECTSTORE_ID_FIELD),resultQuery.getString(INTERPRETATION_FIELD),accessKey, secretKey, s3EndPoint, objectStoreBucket, collection);
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
public List<String> listObjectStores() {
|
78
|
MongoCollection<Document> metadata = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD);
|
79
|
Iterable<Document> tmp = () -> metadata.find().iterator();
|
80
|
return StreamSupport.stream(tmp.spliterator(), false).map(it-> it.getString(OBJECTSTORE_ID_FIELD)).collect(Collectors.toList());
|
81
|
}
|
82
|
|
83
|
|
84
|
@Override
|
85
|
public boolean createObjectStore(final String obsId, final String interpretation, final String basePath) throws ObjectStoreServiceException {
|
86
|
if (getObjectStoreMetadata(obsId)!= null)
|
87
|
throw new ObjectStoreServiceException("Error unable to create an ObjectStore that already exists in mongo");
|
88
|
final MongoCollection<Document> metadata = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD);
|
89
|
final Document item = new Document()
|
90
|
.append(OBJECTSTORE_ID_FIELD, obsId)
|
91
|
.append(INTERPRETATION_FIELD, interpretation);
|
92
|
metadata.insertOne(item);
|
93
|
MongoCollection<Document> objectStore = getDb().getCollection(obsId.substring(0, 36));
|
94
|
objectStore.createIndex(new BasicDBObject("id", 1));
|
95
|
objectStore.createIndex(new BasicDBObject("timestamp", 1));
|
96
|
return true;
|
97
|
}
|
98
|
|
99
|
@Override
|
100
|
public boolean updateObjectStore(final String obsId, final String interpretation) {
|
101
|
MongoCollection<Document> coll = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD);
|
102
|
final Document update = new Document().append("$set", new Document(INTERPRETATION_FIELD, interpretation));
|
103
|
final UpdateResult updateResult = coll.updateOne(Filters.eq(OBJECTSTORE_ID_FIELD, obsId), update);
|
104
|
if (updateResult.isModifiedCountAvailable()) {
|
105
|
log.debug("Matched / Modified " + updateResult.getMatchedCount() + " / " + updateResult.getModifiedCount());
|
106
|
}
|
107
|
return true;
|
108
|
}
|
109
|
|
110
|
@Override
|
111
|
public boolean deleteObjectStore(String obsId) throws ObjectStoreServiceException {
|
112
|
final Document objectStoreMetadata = getObjectStoreMetadata(obsId);
|
113
|
if (objectStoreMetadata== null)
|
114
|
throw new ObjectStoreServiceException("ObjectStore not found with Identifier "+obsId);
|
115
|
log.debug("Start to deleting all the object on the ObjectStore in teh bucket");
|
116
|
getObjectStore(obsId.substring(0,36)).dropContent();
|
117
|
log.debug("All object Deleted");
|
118
|
log.debug("Deleting mongo collection");
|
119
|
MongoCollection<Document> objectStoreCollection = db.getCollection(objectStoreMetadata.getString(OBJECTSTORE_ID_FIELD));
|
120
|
objectStoreCollection.drop();
|
121
|
log.debug("Deleting item on mongo metadata Collection");
|
122
|
final MongoCollection<Document> metadata = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD);
|
123
|
DeleteResult deleteResult = metadata.deleteOne(Filters.eq(OBJECTSTORE_ID_FIELD, obsId));
|
124
|
if (deleteResult.getDeletedCount() != 1)
|
125
|
throw new ObjectStoreServiceException("Unexpected number of Deleting object on ObjectStoreMetadata, should be 1 instead of"+deleteResult.getDeletedCount());
|
126
|
return true;
|
127
|
}
|
128
|
|
129
|
@Override
|
130
|
public boolean dropContent(String obsId) throws ObjectStoreServiceException {
|
131
|
return getObjectStore(obsId).dropContent();
|
132
|
}
|
133
|
|
134
|
public MongoDatabase getDb() {
|
135
|
return db;
|
136
|
}
|
137
|
|
138
|
public void setDb(MongoDatabase db) {
|
139
|
this.db = db;
|
140
|
}
|
141
|
}
|