Project

General

Profile

1
package eu.dnetlib.data.objectstore.filesystem;
2

    
3
import java.io.IOException;
4
import java.nio.file.FileSystems;
5
import java.nio.file.Files;
6
import java.nio.file.Path;
7
import java.util.List;
8
import javax.annotation.Resource;
9

    
10
import com.mongodb.BasicDBObject;
11
import com.mongodb.DBObject;
12
import com.mongodb.client.MongoCollection;
13
import com.mongodb.client.MongoDatabase;
14
import com.mongodb.client.model.Filters;
15
import com.mongodb.client.result.UpdateResult;
16
import eu.dnetlib.data.objectstore.modular.connector.ObjectStore;
17
import eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao;
18
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFileNotFoundException;
19
import eu.dnetlib.data.objectstore.rmi.ObjectStoreServiceException;
20
import eu.dnetlib.miscutils.collections.MappedCollection;
21
import eu.dnetlib.miscutils.functional.UnaryFunction;
22
import org.apache.commons.lang3.StringUtils;
23
import org.apache.commons.logging.Log;
24
import org.apache.commons.logging.LogFactory;
25
import org.bson.conversions.Bson;
26

    
27
/**
28
 * @author sandro
29
 *
30
 */
31
public class FileSystemObjectStoreDao implements ObjectStoreDao {
32

    
33
	public static final String INTERPRETATION_FIELD = "interpretation";
34
	public final static String OBJECTSTORE_METADATA_NAME_FIELD = "metadataObjectStore";
35
	public final static String OBJECTSTORE_ID_FIELD = "obsId";
36
	public final static String BASE_PATH_FIELD = "basePath";
37
	private static final Log log = LogFactory.getLog(FileSystemObjectStoreDao.class); // NOPMD by marko on 11/24/08 5:02 PM
38
	private static final String OBJECTSTORE_PROFILE_SUFFIX = "_T2JqZWN0U3RvcmVEU1Jlc291cmNlcy9PYmplY3RTdG9yZURTUmVzb3VyY2VUeXBl";
39
	@Resource(name="objectstoreMongoDB")
40
	private MongoDatabase db;
41

    
42
	private boolean upsert;
43

    
44
	private String objectStoreRESTURI;
45

    
46
	/**
47
	 * {@inheritDoc}
48
	 * @throws ObjectStoreServiceException
49
	 *
50
	 * @see eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao#getObjectStore(java.lang.String)
51
	 */
52
	@Override
53
	public ObjectStore getObjectStore(final String obsId) throws ObjectStoreServiceException {
54
		String currentId = obsId.substring(0, 36);
55
		String find_id = obsId;
56
		if (find_id.length() == 36) {
57
			find_id += OBJECTSTORE_PROFILE_SUFFIX;
58
		}
59

    
60
		MongoCollection<DBObject> metadataObjectStore = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD, DBObject.class);
61
		Bson query = Filters.eq(OBJECTSTORE_ID_FIELD, find_id);
62
		log.debug("QUERY :" + query.toString());
63
		DBObject resultQuery = metadataObjectStore.find(query).first();
64
		log.debug("result " + resultQuery);
65
		if ((resultQuery == null)) throw new ObjectStoreFileNotFoundException("the objectStore with identifier: "+obsId+" was not found");
66

    
67
		final String basePath = resultQuery.get(BASE_PATH_FIELD).toString();
68
		final String interpretation = resultQuery.get("interpretation").toString();
69

    
70
		if (!resultQuery.containsField(BASE_PATH_FIELD) || StringUtils.isBlank(basePath))
71
			throw new ObjectStoreServiceException("Can't Get Objectstore, the metadata doesn't contain info about the basepath");
72

    
73
		final MongoCollection<DBObject> collection = getDb().getCollection(currentId, DBObject.class);
74
		return new FileSystemObjectStore(currentId, interpretation, basePath, collection, objectStoreRESTURI);
75
	}
76

    
77
	/**
78
	 * {@inheritDoc}
79
	 *
80
	 * @see eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao#listObjectStores()
81
	 */
82
	@Override
83
	public List<String> listObjectStores() {
84
		MongoCollection<DBObject> metadata = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD, DBObject.class);
85
		return MappedCollection.listMap(metadata.find(), new UnaryFunction<String, DBObject>() {
86

    
87
			@Override
88
			public String evaluate(final DBObject object) {
89
				return (String) object.get(OBJECTSTORE_ID_FIELD);
90
			}
91
		});
92
	}
93

    
94
	/**
95
	 * {@inheritDoc}
96
	 *
97
	 * @throws ObjectStoreServiceException
98
	 */
99
	@Override
100
	public boolean createObjectStore(final String obsId, final String interpretation, final String basePath) throws ObjectStoreServiceException {
101

    
102
		log.debug(String.format("Create object Store method\n\t Id:%s  Interpretation:%s BasePath : %s", obsId, interpretation, basePath) );
103

    
104
		if (StringUtils.isBlank(basePath)) throw new ObjectStoreServiceException("Can't create the object store: the base path cannot be blank");
105
		Path path = FileSystems.getDefault().getPath(basePath);
106
		if (!Files.exists(path) || !Files.isDirectory(path))
107
			throw new ObjectStoreServiceException("Can't create the object store: base path: '" + basePath + "' doesn't exist or it is not a folder");
108
		try {
109
			String currentObsId = obsId.substring(0, 36);
110
			log.debug("Cleaned objectStore Id " + currentObsId);
111
			if (Files.exists(path.resolve(currentObsId)))
112
				throw new ObjectStoreServiceException("Can't create the object store: base path: '" + path.resolve(currentObsId) + "' already exists");
113
			Files.createDirectory(path.resolve(currentObsId));
114
			MongoCollection<DBObject> coll = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD, DBObject.class);
115
			final BasicDBObject obj = new BasicDBObject();
116
			obj.put(OBJECTSTORE_ID_FIELD, obsId);
117
			obj.put(INTERPRETATION_FIELD, interpretation);
118
			obj.put(BASE_PATH_FIELD, basePath);
119
			coll.insertOne(obj);
120
			MongoCollection<DBObject> objectStore = getDb().getCollection(currentObsId, DBObject.class);
121
			objectStore.createIndex(new BasicDBObject("id", 1));
122
			objectStore.createIndex(new BasicDBObject("timestamp", 1));
123
			return true;
124
		} catch (Throwable e) {
125
			throw new ObjectStoreServiceException("Can't Create the object Store id: '" + obsId, e);
126
		}
127
	}
128

    
129
	/**
130
	 * {@inheritDoc}
131
	 *
132
	 * @see eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao#updateObjectStore(java.lang.String, java.lang.String)
133
	 */
134
	@Override
135
	public boolean updateObjectStore(final String obsId, final String interpretation) {
136
		MongoCollection<DBObject> coll = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD, DBObject.class);
137
		final BasicDBObject obj = new BasicDBObject();
138
		obj.put("$set", new BasicDBObject(INTERPRETATION_FIELD, interpretation));
139

    
140
		final UpdateResult updateResult = coll.updateOne(Filters.eq(OBJECTSTORE_ID_FIELD, obsId), obj);
141
		if (updateResult.isModifiedCountAvailable()) {
142
			log.debug("Matched / Modified " + updateResult.getMatchedCount() + " / " + updateResult.getModifiedCount());
143
		}
144
		return true;
145
	}
146

    
147
	/**
148
	 * {@inheritDoc}
149
	 *
150
	 * @throws ObjectStoreServiceException
151
	 * @see eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao#deleteObjectStore(java.lang.String)
152
	 */
153
	@Override
154
	public boolean deleteObjectStore(final String obsId) throws ObjectStoreServiceException {
155
		MongoCollection<DBObject> coll = getDb().getCollection(OBJECTSTORE_METADATA_NAME_FIELD, DBObject.class);
156
		Bson query = Filters.eq(OBJECTSTORE_ID_FIELD, obsId);
157
		DBObject resultQuery = coll.find(query).first();
158
		String basePath = checkAndGetFsPathField(resultQuery, obsId);
159
		String currentObsId = obsId.substring(0, 36);
160
		Path basePathFS = FileSystems.getDefault().getPath(basePath, currentObsId);
161
		if (!Files.exists(basePathFS))
162
			throw new ObjectStoreServiceException("Can't Delete ObjectStore " + obsId + ": the base path does not exist :" + basePathFS);
163
		try {
164
			FileSystemUtility.deleteFolderRecursive(basePathFS);
165
		} catch (IOException e) {
166
			throw new ObjectStoreServiceException("Can't Delete ObjectStore " + obsId, e);
167
		}
168
		coll.deleteOne(Filters.eq(OBJECTSTORE_ID_FIELD, obsId));
169
		getDb().getCollection(obsId).drop();
170
		return true;
171
	}
172

    
173
	@Override
174
	public boolean dropContent(final String obsId) throws ObjectStoreServiceException {
175
		return getObjectStore(obsId).dropContent();
176
	}
177

    
178
	private String checkAndGetFsPathField(final DBObject resultQuery, final String objectStoreID) throws ObjectStoreServiceException {
179
		if (resultQuery == null || !resultQuery.containsField(BASE_PATH_FIELD))
180
			throw new ObjectStoreServiceException("ObjectStore with identifier :" + objectStoreID + " not found or missing " + BASE_PATH_FIELD + " field");
181
		String pathStr = (String) resultQuery.get(BASE_PATH_FIELD);
182
		if (StringUtils.isBlank(pathStr))
183
			throw new ObjectStoreServiceException("ObjectStore with identifier :" + objectStoreID + " with blank " + BASE_PATH_FIELD);
184
		return pathStr;
185
	}
186

    
187

    
188
	public boolean isUpsert() {
189
		return upsert;
190
	}
191

    
192
	public void setUpsert(final boolean upsert) {
193
		this.upsert = upsert;
194
	}
195

    
196
	public String getObjectStoreRESTURI() {
197
		return objectStoreRESTURI;
198
	}
199

    
200
	public void setObjectStoreRESTURI(final String objectStoreRESTURI) {
201
		this.objectStoreRESTURI = objectStoreRESTURI;
202
	}
203

    
204
	public MongoDatabase getDb() {
205
		return db;
206
	}
207

    
208
	public void setDb(final MongoDatabase db) {
209
		this.db = db;
210
	}
211

    
212
}
(2-2/8)