Project

General

Profile

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

    
3
import java.util.List;
4
import java.util.regex.Pattern;
5
import java.util.stream.Collectors;
6

    
7
import com.mongodb.*;
8
import com.mongodb.gridfs.GridFS;
9
import eu.dnetlib.data.objectstore.connector.ObjectStore;
10
import eu.dnetlib.data.objectstore.connector.ObjectStoreDao;
11
import eu.dnetlib.enabling.tools.DnetStreamSupport;
12
import eu.dnetlib.rmi.data.ObjectStoreServiceException;
13
import org.bson.BSONObject;
14
import org.springframework.beans.factory.annotation.Required;
15

    
16
// TODO: Auto-generated Javadoc
17

    
18
/**
19
 * The Class GridFSObjectstoreDaoImpl.
20
 */
21

    
22
public class GridFSObjectstoreDaoImpl implements ObjectStoreDao {
23

    
24
	/**
25
	 * The Constant INTERPRETATION.
26
	 */
27
	public static final String INTERPRETATION = "interpretation";
28

    
29
	/**
30
	 * The Constant OBJECTSTOREMETADATANAME.
31
	 */
32
	private final static String OBJECTSTOREMETADATANAME = "metadataObjectStore";
33

    
34
	/**
35
	 * The Constant OBJECTSTOREIDFIELD.
36
	 */
37
	private final static String OBJECTSTOREIDFIELD = "obsId";
38

    
39
	/**
40
	 * The db.
41
	 */
42
	private DB db;
43

    
44
	/**
45
	 * The upsert.
46
	 */
47
	private boolean upsert;
48

    
49
	/**
50
	 * The object store resturi.
51
	 */
52
	private String objectStoreRESTURI;
53

    
54
	/**
55
	 * {@inheritDoc}
56
	 */
57
	@Override
58
	public ObjectStore getObjectStore(String obsId) {
59
		db.setWriteConcern(WriteConcern.SAFE);
60
		obsId = obsId.substring(0, 36);
61
		GridFSObjectStore objectStore = new GridFSObjectStore(obsId, new GridFS(db, obsId), this.upsert);
62
		objectStore.setBaseURI(objectStoreRESTURI);
63
		checkIndexes(obsId);
64
		return objectStore;
65
	}
66

    
67
	/**
68
	 * Check indexes.
69
	 *
70
	 * @param id the id
71
	 */
72
	private void checkIndexes(final String id) {
73
		DBCollection coll = db.getCollection(id + ".files");
74
		List<DBObject> indexInfo = coll.getIndexInfo();
75
		boolean hasTimestampIndex = false;
76
		for (DBObject info : indexInfo) {
77
			BSONObject boj = (BSONObject) info.get("key");
78
			if (boj.get("metadata.timestamp") != null) {
79
				hasTimestampIndex = true;
80
			}
81
		}
82
		if (!hasTimestampIndex) {
83
			coll.createIndex("metadata.timestamp");
84
		}
85
	}
86

    
87
	/**
88
	 * {@inheritDoc}
89
	 */
90
	@Override
91
	public List<String> listObjectStores() {
92
		DBCollection metadata = db.getCollection(OBJECTSTOREMETADATANAME);
93
		return DnetStreamSupport.generateStreamFromIterator(metadata.find().iterator())
94
				.map(object -> (String) object.get(OBJECTSTOREIDFIELD))
95
				.collect(Collectors.toList());
96
	}
97

    
98
	/**
99
	 * {@inheritDoc}
100
	 */
101
	@Override
102
	public boolean createObjectStore(final String obsId, final String interpretation, final String basePath) throws ObjectStoreServiceException {
103
		try {
104
			DBCollection coll = db.getCollection(OBJECTSTOREMETADATANAME);
105
			final BasicDBObject obj = new BasicDBObject();
106
			obj.put(OBJECTSTOREIDFIELD, obsId);
107
			obj.put(INTERPRETATION, interpretation);
108
			coll.save(obj);
109
		} catch (Throwable e) {
110
			throw new ObjectStoreServiceException("Error on create object store with id: " + obsId, e);
111
		}
112
		return true;
113
	}
114

    
115
	/**
116
	 * {@inheritDoc}
117
	 */
118
	@Override
119
	public boolean updateObjectStore(final String obsId, final String interpretation) {
120
		DBCollection coll = db.getCollection(OBJECTSTOREMETADATANAME);
121
		final BasicDBObject obj = new BasicDBObject();
122
		obj.put(OBJECTSTOREIDFIELD, obsId);
123
		obj.put(INTERPRETATION, interpretation);
124
		coll.update(new BasicDBObject(OBJECTSTOREIDFIELD, obsId), obj, true, false);
125
		coll.save(obj);
126
		return true;
127
	}
128

    
129
	/**
130
	 * {@inheritDoc}
131
	 */
132
	@Override
133
	public boolean deleteObjectStore(String obsId) {
134
		DBCollection coll = db.getCollection(OBJECTSTOREMETADATANAME);
135
		coll.remove(new BasicDBObject(OBJECTSTOREIDFIELD, obsId));
136
		obsId = obsId.substring(0, 36);
137
		GridFS objectStore = new GridFS(db, obsId);
138
		Pattern any = Pattern.compile(".");
139
		BasicDBObject query = new BasicDBObject("md5", any);
140
		objectStore.remove(query);
141
		return true;
142
	}
143

    
144
	@Override
145
	public boolean dropContent(final String obsId) throws ObjectStoreServiceException {
146
		return getObjectStore(obsId).dropContent();
147
	}
148

    
149
	/**
150
	 * Gets the db.
151
	 *
152
	 * @return the db
153
	 */
154
	public DB getDb() {
155
		return db;
156
	}
157

    
158
	/**
159
	 * Sets the db.
160
	 *
161
	 * @param db the new db
162
	 */
163
	@Required
164
	public void setDb(final DB db) {
165
		this.db = db;
166
	}
167

    
168
	/**
169
	 * Checks if is upsert.
170
	 *
171
	 * @return true, if is upsert
172
	 */
173
	public boolean isUpsert() {
174
		return upsert;
175
	}
176

    
177
	/**
178
	 * Sets the upsert.
179
	 *
180
	 * @param upsert the new upsert
181
	 */
182
	@Required
183
	public void setUpsert(final boolean upsert) {
184
		this.upsert = upsert;
185
	}
186

    
187
	/**
188
	 * Gets the object store resturi.
189
	 *
190
	 * @return the object store resturi
191
	 */
192
	public String getObjectStoreRESTURI() {
193
		return objectStoreRESTURI;
194
	}
195

    
196
	/**
197
	 * Sets the object store resturi.
198
	 *
199
	 * @param objectStoreRESTURI the new object store resturi
200
	 */
201
	@Required
202
	public void setObjectStoreRESTURI(final String objectStoreRESTURI) {
203
		this.objectStoreRESTURI = objectStoreRESTURI;
204
	}
205

    
206
}
(2-2/5)