1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.dnetlib.data.objectstore.filesystem;
|
5
|
|
6
|
import java.io.IOException;
|
7
|
import java.nio.file.FileSystems;
|
8
|
import java.nio.file.Files;
|
9
|
import java.nio.file.Path;
|
10
|
import java.util.List;
|
11
|
import javax.annotation.Resource;
|
12
|
|
13
|
import com.mongodb.BasicDBObject;
|
14
|
import com.mongodb.DBObject;
|
15
|
import com.mongodb.client.MongoCollection;
|
16
|
import com.mongodb.client.MongoDatabase;
|
17
|
import com.mongodb.client.model.Filters;
|
18
|
import com.mongodb.client.result.UpdateResult;
|
19
|
import eu.dnetlib.data.objectstore.modular.connector.ObjectStore;
|
20
|
import eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao;
|
21
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFileNotFoundException;
|
22
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreServiceException;
|
23
|
import eu.dnetlib.miscutils.collections.MappedCollection;
|
24
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
25
|
import org.apache.commons.lang.StringUtils;
|
26
|
import org.apache.commons.logging.Log;
|
27
|
import org.apache.commons.logging.LogFactory;
|
28
|
import org.bson.conversions.Bson;
|
29
|
|
30
|
/**
|
31
|
* @author sandro
|
32
|
*
|
33
|
*/
|
34
|
public class FileSystemObjectStoreDao implements ObjectStoreDao {
|
35
|
|
36
|
public static final String INTERPRETATION_FIELD = "interpretation";
|
37
|
public final static String OBJECTSTORE_METADATA_NAME_FIELD = "metadataObjectStore";
|
38
|
public final static String OBJECTSTORE_ID_FIELD = "obsId";
|
39
|
public final static String BASE_PATH_FIELD = "basePath";
|
40
|
private static final Log log = LogFactory.getLog(FileSystemObjectStoreDao.class); // NOPMD by marko on 11/24/08 5:02 PM
|
41
|
private static final String OBJECTSTORE_PROFILE_SUFFIX = "_T2JqZWN0U3RvcmVEU1Jlc291cmNlcy9PYmplY3RTdG9yZURTUmVzb3VyY2VUeXBl";
|
42
|
@Resource(name="objectstoreMongoDB")
|
43
|
private MongoDatabase db;
|
44
|
|
45
|
private boolean upsert;
|
46
|
|
47
|
private String objectStoreRESTURI;
|
48
|
|
49
|
/**
|
50
|
* {@inheritDoc}
|
51
|
* @throws ObjectStoreServiceException
|
52
|
*
|
53
|
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao#getObjectStore(java.lang.String)
|
54
|
*/
|
55
|
@Override
|
56
|
public ObjectStore getObjectStore(final String obsId) throws ObjectStoreServiceException {
|
57
|
String currentId = obsId.substring(0, 36);
|
58
|
String find_id = obsId;
|
59
|
if (find_id.length() == 36) {
|
60
|
find_id += OBJECTSTORE_PROFILE_SUFFIX;
|
61
|
}
|
62
|
|
63
|
MongoCollection<DBObject> metadataObjectStore = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD, DBObject.class);
|
64
|
Bson query = Filters.eq(OBJECTSTORE_ID_FIELD, find_id);
|
65
|
log.debug("QUERY :" + query.toString());
|
66
|
DBObject resultQuery = metadataObjectStore.find(query).first();
|
67
|
log.debug("result " + resultQuery);
|
68
|
if ((resultQuery == null)) throw new ObjectStoreFileNotFoundException("the objectStore with identifier: "+obsId+" was not found");
|
69
|
|
70
|
final String basePath = resultQuery.get(BASE_PATH_FIELD).toString();
|
71
|
final String interpretation = resultQuery.get("interpretation").toString();
|
72
|
|
73
|
if (!resultQuery.containsField(BASE_PATH_FIELD) || StringUtils.isBlank(basePath))
|
74
|
throw new ObjectStoreServiceException("Can't Get Objectstore, the metadata doesn't contain info about the basepath");
|
75
|
|
76
|
final MongoCollection<DBObject> collection = getDb().getCollection(currentId, DBObject.class);
|
77
|
return new FileSystemObjectStore(currentId, interpretation, basePath, collection, objectStoreRESTURI);
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* {@inheritDoc}
|
82
|
*
|
83
|
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao#listObjectStores()
|
84
|
*/
|
85
|
@Override
|
86
|
public List<String> listObjectStores() {
|
87
|
MongoCollection<DBObject> metadata = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD, DBObject.class);
|
88
|
return MappedCollection.listMap(metadata.find(), new UnaryFunction<String, DBObject>() {
|
89
|
|
90
|
@Override
|
91
|
public String evaluate(final DBObject object) {
|
92
|
return (String) object.get(OBJECTSTORE_ID_FIELD);
|
93
|
}
|
94
|
});
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* {@inheritDoc}
|
99
|
*
|
100
|
* @throws ObjectStoreServiceException
|
101
|
*/
|
102
|
@Override
|
103
|
public boolean createObjectStore(final String obsId, final String interpretation, final String basePath) throws ObjectStoreServiceException {
|
104
|
|
105
|
log.debug(String.format("Create object Store method\n\t Id:%s Interpretation:%s BasePath : %s", obsId, interpretation, basePath) );
|
106
|
|
107
|
if (StringUtils.isBlank(basePath)) throw new ObjectStoreServiceException("Can't create the object store: the base path cannot be blank");
|
108
|
Path path = FileSystems.getDefault().getPath(basePath);
|
109
|
if (!Files.exists(path) || !Files.isDirectory(path))
|
110
|
throw new ObjectStoreServiceException("Can't create the object store: base path: '" + basePath + "' doesn't exist or it is not a folder");
|
111
|
try {
|
112
|
String currentObsId = obsId.substring(0, 36);
|
113
|
log.debug("Cleaned objectStore Id " + currentObsId);
|
114
|
if (Files.exists(path.resolve(currentObsId)))
|
115
|
throw new ObjectStoreServiceException("Can't create the object store: base path: '" + path.resolve(currentObsId) + "' already exists");
|
116
|
Files.createDirectory(path.resolve(currentObsId));
|
117
|
MongoCollection<DBObject> coll = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD, DBObject.class);
|
118
|
final BasicDBObject obj = new BasicDBObject();
|
119
|
obj.put(OBJECTSTORE_ID_FIELD, obsId);
|
120
|
obj.put(INTERPRETATION_FIELD, interpretation);
|
121
|
obj.put(BASE_PATH_FIELD, basePath);
|
122
|
coll.insertOne(obj);
|
123
|
MongoCollection<DBObject> objectStore = getDb().getCollection(currentObsId, DBObject.class);
|
124
|
objectStore.createIndex(new BasicDBObject("id", 1));
|
125
|
objectStore.createIndex(new BasicDBObject("timestamp", 1));
|
126
|
return true;
|
127
|
} catch (Throwable e) {
|
128
|
throw new ObjectStoreServiceException("Can't Create the object Store id: '" + obsId, e);
|
129
|
}
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* {@inheritDoc}
|
134
|
*
|
135
|
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao#updateObjectStore(java.lang.String, java.lang.String)
|
136
|
*/
|
137
|
@Override
|
138
|
public boolean updateObjectStore(final String obsId, final String interpretation) {
|
139
|
MongoCollection<DBObject> coll = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD, DBObject.class);
|
140
|
final BasicDBObject obj = new BasicDBObject();
|
141
|
obj.put("$set", new BasicDBObject(INTERPRETATION_FIELD, interpretation));
|
142
|
|
143
|
final UpdateResult updateResult = coll.updateOne(Filters.eq(OBJECTSTORE_ID_FIELD, obsId), obj);
|
144
|
if (updateResult.isModifiedCountAvailable()) {
|
145
|
log.debug("Matched / Modified " + updateResult.getMatchedCount() + " / " + updateResult.getModifiedCount());
|
146
|
}
|
147
|
return true;
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* {@inheritDoc}
|
152
|
*
|
153
|
* @throws ObjectStoreServiceException
|
154
|
* @see eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao#deleteObjectStore(java.lang.String)
|
155
|
*/
|
156
|
@Override
|
157
|
public boolean deleteObjectStore(final String obsId) throws ObjectStoreServiceException {
|
158
|
MongoCollection<DBObject> coll = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD, DBObject.class);
|
159
|
Bson query = Filters.eq(OBJECTSTORE_ID_FIELD, obsId);
|
160
|
DBObject resultQuery = coll.find(query).first();
|
161
|
String basePath = checkAndGetFsPathField(resultQuery, obsId);
|
162
|
String currentObsId = obsId.substring(0, 36);
|
163
|
Path basePathFS = FileSystems.getDefault().getPath(basePath, currentObsId);
|
164
|
if (!Files.exists(basePathFS))
|
165
|
throw new ObjectStoreServiceException("Can't Delete ObjectStore " + obsId + ": the base path does not exist :" + basePathFS);
|
166
|
try {
|
167
|
FileSystemUtility.deleteFolderRecursive(basePathFS);
|
168
|
} catch (IOException e) {
|
169
|
throw new ObjectStoreServiceException("Can't Delete ObjectStore " + obsId, e);
|
170
|
}
|
171
|
coll.deleteOne(Filters.eq(OBJECTSTORE_ID_FIELD, obsId));
|
172
|
getDb().getCollection(obsId).drop();
|
173
|
return true;
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
public boolean dropContent(final String obsId) throws ObjectStoreServiceException {
|
178
|
return getObjectStore(obsId).dropContent();
|
179
|
}
|
180
|
|
181
|
private String checkAndGetFsPathField(final DBObject resultQuery, final String objectStoreID) throws ObjectStoreServiceException {
|
182
|
if (resultQuery == null || !resultQuery.containsField(BASE_PATH_FIELD))
|
183
|
throw new ObjectStoreServiceException("ObjectStore with identifier :" + objectStoreID + " not found or missing " + BASE_PATH_FIELD + " field");
|
184
|
String pathStr = (String) resultQuery.get(BASE_PATH_FIELD);
|
185
|
if (StringUtils.isBlank(pathStr))
|
186
|
throw new ObjectStoreServiceException("ObjectStore with identifier :" + objectStoreID + " with blank " + BASE_PATH_FIELD);
|
187
|
return pathStr;
|
188
|
}
|
189
|
|
190
|
|
191
|
public boolean isUpsert() {
|
192
|
return upsert;
|
193
|
}
|
194
|
|
195
|
public void setUpsert(final boolean upsert) {
|
196
|
this.upsert = upsert;
|
197
|
}
|
198
|
|
199
|
public String getObjectStoreRESTURI() {
|
200
|
return objectStoreRESTURI;
|
201
|
}
|
202
|
|
203
|
public void setObjectStoreRESTURI(final String objectStoreRESTURI) {
|
204
|
this.objectStoreRESTURI = objectStoreRESTURI;
|
205
|
}
|
206
|
|
207
|
public MongoDatabase getDb() {
|
208
|
return db;
|
209
|
}
|
210
|
|
211
|
public void setDb(final MongoDatabase db) {
|
212
|
this.db = db;
|
213
|
}
|
214
|
|
215
|
}
|