Project

General

Profile

« Previous | Next » 

Revision 28055

Added transaction Class

View differences:

modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/MDStoreTransactionManagerImpl.java
1
package eu.dnetlib.data.mdstore.modular.mongodb;
2

  
3
import java.util.Date;
4
import java.util.UUID;
5

  
6
import org.apache.commons.lang.StringUtils;
7
import org.joda.time.DateTime;
8
import org.joda.time.Days;
9
import org.springframework.beans.factory.annotation.Required;
10

  
11
import com.mongodb.BasicDBList;
12
import com.mongodb.BasicDBObject;
13
import com.mongodb.DB;
14
import com.mongodb.DBCollection;
15
import com.mongodb.DBCursor;
16
import com.mongodb.DBObject;
17

  
18
import eu.dnetlib.data.mdstore.DocumentNotFoundException;
19
import eu.dnetlib.data.mdstore.MDStoreServiceException;
20
import eu.dnetlib.data.mdstore.modular.connector.MDStoreExpiredInfo;
21
import eu.dnetlib.data.mdstore.modular.connector.MDStoreManagerInfo;
22
import eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionInfo;
23
import eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager;
24

  
25
/**
26
 * The Class MDStoreTransactionManager.
27
 */
28
public class MDStoreTransactionManagerImpl implements MDStoreTransactionManager {
29

  
30
	/** The table name. */
31
	private static String TABLE_NAME = "metadataManager";
32

  
33
	/** The db. */
34

  
35
	private DB db;
36

  
37
	/** The manager table. */
38
	private DBCollection managerTable;
39

  
40
	private int expiredDays;
41

  
42
	/**
43
	 * Bootstrap manager.
44
	 */
45
	private void bootstrapManager() {
46

  
47
		DBCollection metadataColl = db.getCollection("metadata");
48
		DBCursor values = metadataColl.find();
49
		this.setManagerTable(db.getCollection(TABLE_NAME));
50
		while (values.hasNext()) {
51
			DBObject object = values.next();
52
			String id = (String) object.get("mdId");
53
			String newId = null;
54
			if (id.contains("_")) {
55

  
56
				newId = StringUtils.substringBefore(id, "_");
57
			}
58

  
59
			BasicDBObject input = new BasicDBObject();
60
			input.put("mdId", id);
61
			input.put("currentId", newId);
62
			input.put("stillUsed", new String[] {});
63
			input.put("transactions", new String[] {});
64
			getManagerTable().insert(input);
65

  
66
		}
67
		BasicDBObject ensureIndex = new BasicDBObject();
68
		ensureIndex.put("mdId", 1);
69
		this.getManagerTable().ensureIndex(ensureIndex);
70
	}
71

  
72
	/**
73
	 * Verify consistency.
74
	 * 
75
	 * @throws MDStoreServiceException
76
	 *             the MD store service exception
77
	 */
78
	private void verifyConsistency() throws MDStoreServiceException {
79
		if (this.getManagerTable() == null) {
80
			if (!db.collectionExists(TABLE_NAME)) {
81
				bootstrapManager();
82
				if (this.getManagerTable() == null) throw new MDStoreServiceException("Something bad happen, unable to create managerTable");
83
			} else {
84
				this.setManagerTable(db.getCollection(TABLE_NAME));
85
			}
86
		}
87
	}
88

  
89
	/**
90
	 * {@inheritDoc}
91
	 * 
92
	 * @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#createMDStore(java.lang.String)
93
	 */
94
	@Override
95
	public void createMDStore(final String mdId) throws MDStoreServiceException {
96
		verifyConsistency();
97
		String newId = mdId;
98
		if (mdId.contains("_")) {
99
			newId = StringUtils.substringBefore(mdId, "_");
100
		}
101
		BasicDBObject instance = new BasicDBObject();
102
		instance.put("mdId", mdId);
103
		instance.put("currentId", newId);
104
		instance.put("stillUsed", new String[] {});
105
		getManagerTable().insert(instance);
106
		this.getManagerTable().save(instance);
107
	}
108

  
109
	/**
110
	 * {@inheritDoc}
111
	 * 
112
	 * @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#dropMDStore(java.lang.String)
113
	 */
114
	@Override
115
	public void dropMDStore(final String mdId) throws MDStoreServiceException {
116
		BasicDBObject query = new BasicDBObject();
117
		query.put("mdId", mdId);
118
		DBCursor cursor = this.getManagerTable().find(query);
119
		if (cursor.hasNext() == false) return;
120
		DBObject item = cursor.next();
121
		BasicDBList stillUsedList = (BasicDBList) item.get("stillUsed");
122

  
123
		if (stillUsedList.size() != 0) {
124
			// TODO create the code in case of still used mdstore
125

  
126
		}
127
		this.db.getCollection((String) item.get("currentId")).drop();
128
		this.getManagerTable().remove(query);
129

  
130
	}
131

  
132
	/**
133
	 * {@inheritDoc}
134
	 * 
135
	 * @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#getMDStoreCollection(java.lang.String)
136
	 */
137
	@Override
138
	public String getMDStoreCollection(final String mdId) throws MDStoreServiceException {
139
		verifyConsistency();
140
		BasicDBObject query = new BasicDBObject();
141
		query.put("mdId", mdId);
142
		DBCursor cursor = this.getManagerTable().find(query);
143
		if (cursor.hasNext() == false) throw new DocumentNotFoundException("Error, unable to find Mdstore with Id " + mdId);
144
		return (String) cursor.next().get("currentId");
145
	}
146

  
147
	/**
148
	 * {@inheritDoc}
149
	 * 
150
	 * @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#startTransaction(java.lang.String, boolean)
151
	 */
152
	@Override
153
	public String startTransaction(final String mdId, final boolean refresh) throws MDStoreServiceException {
154
		verifyConsistency();
155
		BasicDBObject query = new BasicDBObject();
156
		query.put("mdId", mdId);
157
		DBCursor cursor = this.getManagerTable().find(query);
158
		if (cursor.hasNext() == false) throw new DocumentNotFoundException("Error, unable to find Mdstore with Id " + mdId);
159
		if (refresh == true) {
160
			UUID idCreation = UUID.randomUUID();
161
			DBObject object = cursor.next();
162
			BasicDBObject transactionMetadata = new BasicDBObject();
163
			transactionMetadata.put("id", idCreation.toString());
164
			transactionMetadata.put("refresh", refresh);
165
			transactionMetadata.put("date", new Date());
166
			BasicDBList values;
167
			if (object.containsField("transactions")) {
168
				values = (BasicDBList) object.get("transactions");
169
			} else {
170
				values = new BasicDBList();
171
				object.put("transactions", values);
172
			}
173
			values.add(transactionMetadata);
174
			this.getManagerTable().save(object);
175
			return idCreation.toString();
176
		}
177
		return null;
178
	}
179

  
180
	/**
181
	 * {@inheritDoc}
182
	 * 
183
	 * @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#commit(java.lang.String)
184
	 */
185
	@Override
186
	public boolean commit(final String transactionId, final String mdId) throws MDStoreServiceException {
187
		verifyConsistency();
188
		BasicDBObject query = new BasicDBObject();
189
		query.put("mdId", mdId);
190
		DBCursor cursor = this.getManagerTable().find(query);
191
		if (cursor.hasNext() == false) throw new DocumentNotFoundException("Error, unable to find Mdstore with Id " + mdId);
192
		DBObject object = cursor.next();
193
		BasicDBList transactions = (BasicDBList) object.get("transactions");
194
		DBObject transaction = findTransaction(transactions, transactionId);
195
		if (transaction == null) throw new DocumentNotFoundException("Error, unable to find transaction with Id " + transactionId);
196
		boolean refresh = (Boolean) transaction.get("refresh");
197
		transactions.remove(transaction);
198
		String oldId = (String) object.get("currentId");
199
		object.put("currentId", transactionId);
200
		if (refresh == true) {
201
			BasicDBList stillUsed = (BasicDBList) object.get("stillUsed");
202
			if (stillUsed.size() == 0) {
203
				db.getCollection(oldId).drop();
204
			}
205
		} else {
206
			// TODO Implement the incremental way
207
		}
208
		this.managerTable.save(object);
209
		return true;
210
	}
211

  
212
	private DBObject findTransaction(final BasicDBList transactions, final String transactionId) {
213
		if (transactions.size() == 0) return null;
214
		for (int i = 0; i < transactions.size(); i++) {
215
			BasicDBObject transaction = (BasicDBObject) transactions.get(i);
216
			String id = (String) transaction.get("id");
217
			if (transactionId.equals(id)) return transaction;
218
		}
219
		return null;
220

  
221
	}
222

  
223
	/**
224
	 * Gets the db.
225
	 * 
226
	 * @return the db
227
	 */
228
	public DB getDb() {
229
		return db;
230
	}
231

  
232
	/**
233
	 * Sets the db.
234
	 * 
235
	 * @param db
236
	 *            the db to set
237
	 */
238
	@Required
239
	public void setDb(final DB db) {
240
		this.db = db;
241
	}
242

  
243
	/**
244
	 * {@inheritDoc}
245
	 * 
246
	 * @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#readMdStore(java.lang.String)
247
	 */
248
	@Override
249
	public String readMdStore(final String mdStoreId) throws MDStoreServiceException {
250
		verifyConsistency();
251
		BasicDBObject query = new BasicDBObject();
252
		query.put("mdId", mdStoreId);
253
		DBCursor cursor = this.getManagerTable().find(query);
254
		if (cursor.hasNext() == false) throw new DocumentNotFoundException("Error, unable to find Mdstore with Id " + mdStoreId);
255
		DBObject object = cursor.next();
256
		String currentId = (String) object.get("currentId");
257
		BasicDBList values = (BasicDBList) object.get("stillUsed");
258
		updateMdstoreUsed(values, currentId);
259
		this.getManagerTable().save(object);
260
		return currentId;
261

  
262
	}
263

  
264
	private void updateMdstoreUsed(final BasicDBList values, final String mdId) {
265
		if (values.size() > 0) {
266
			for (int i = 0; i < values.size(); i++) {
267
				DBObject obj = (DBObject) values.get(i);
268
				String id = (String) obj.get("id");
269
				if (mdId.equals(id)) {
270
					obj.put("lastRead", new Date());
271
					return;
272
				}
273
			}
274
		}
275
		BasicDBObject readStore = new BasicDBObject();
276
		readStore.put("id", mdId);
277
		readStore.put("lastRead", new Date());
278
		values.add(readStore);
279
	}
280

  
281
	/**
282
	 * @return the managerTable
283
	 */
284
	public DBCollection getManagerTable() {
285
		return managerTable;
286
	}
287

  
288
	/**
289
	 * @param managerTable
290
	 *            the managerTable to set
291
	 */
292
	public void setManagerTable(final DBCollection managerTable) {
293
		this.managerTable = managerTable;
294
	}
295

  
296
	@Override
297
	public MDStoreManagerInfo getInfoForCurrentMdStore(final String mdStoreId) throws MDStoreServiceException {
298
		verifyConsistency();
299
		BasicDBObject query = new BasicDBObject();
300
		query.put("mdId", mdStoreId);
301
		DBCursor cursor = this.getManagerTable().find(query);
302
		if (cursor.hasNext() == false) throw new DocumentNotFoundException("Error, unable to find Mdstore with Id " + mdStoreId);
303
		DBObject object = cursor.next();
304
		MDStoreManagerInfo result = new MDStoreManagerInfo();
305
		result.setCurrentId((String) object.get("currentId"));
306
		result.setMdId((String) object.get("mdId"));
307
		BasicDBList values = (BasicDBList) object.get("stillUsed");
308
		for (int i = 0; i < values.size(); i++) {
309
			MDStoreExpiredInfo stillused = new MDStoreExpiredInfo();
310
			DBObject value = (DBObject) values.get(i);
311
			stillused.setId((String) value.get("id"));
312
			stillused.setLastRead((Date) value.get("lastRead"));
313
			result.addExpiredItem(stillused);
314
		}
315
		BasicDBList transactions = (BasicDBList) object.get("transactions");
316
		for (int i = 0; i < transactions.size(); i++) {
317
			MDStoreTransactionInfo transaction = new MDStoreTransactionInfo();
318
			DBObject value = (DBObject) transactions.get(i);
319
			String transactionId = (String) value.get("id");
320
			transaction.setId(transactionId);
321
			transaction.setDate((Date) value.get("date"));
322
			transaction.setRefresh((Boolean) value.get("refresh"));
323
			transaction.setSize(db.getCollection(transactionId).count());
324
			result.addTransactionInfo(transaction);
325
		}
326

  
327
		return result;
328
	}
329

  
330
	@Override
331
	public Boolean dropUsed(final String mdId, final String idToDrop) throws MDStoreServiceException {
332
		verifyConsistency();
333
		BasicDBObject query = new BasicDBObject();
334
		query.put("mdId", mdId);
335
		DBCursor cursor = this.getManagerTable().find(query);
336
		if (cursor.hasNext() == false) throw new DocumentNotFoundException("Error, unable to find Mdstore with Id " + mdId);
337
		DBObject object = cursor.next();
338
		BasicDBList values = (BasicDBList) object.get("stillUsed");
339
		for (int i = 0; i < values.size(); i++) {
340
			DBObject value = (DBObject) values.get(i);
341
			String currentUsedId = (String) value.get("id");
342
			if (currentUsedId.equals(idToDrop)) {
343
				db.getCollection(idToDrop).drop();
344
				values.remove(value);
345
				this.managerTable.save(object);
346
				return true;
347
			}
348
		}
349
		throw new DocumentNotFoundException("Error, unable to drop old collection " + idToDrop);
350
	}
351

  
352
	@Override
353
	public void garbage() throws MDStoreServiceException {
354
		verifyConsistency();
355
		DBCursor cursor = this.managerTable.find();
356
		while (cursor.hasNext()) {
357
			DBObject currentObject = cursor.next();
358
			BasicDBList values = (BasicDBList) currentObject.get("stillUsed");
359
			String currentId = (String) currentObject.get("currentId");
360
			for (int i = 0; i < values.size(); i++) {
361
				DBObject value = (DBObject) values.get(i);
362
				String currentUsedId = (String) value.get("id");
363
				Date lastRead = (Date) value.get("lastRead");
364
				DateTime last = new DateTime(lastRead);
365
				DateTime today = new DateTime();
366
				Days d = Days.daysBetween(today, last);
367
				// DELETE the collection where the last time they was read
368
				// is more than 3 days ago
369
				if (d.getDays() > getExpiredDays()) {
370
					if (currentUsedId.equals(currentId) == false) {
371
						db.getCollection(currentUsedId).drop();
372
					}
373
					values.remove(value);
374
				}
375
			}
376
		}
377
	}
378

  
379
	/**
380
	 * @return the expiredDays
381
	 */
382
	public int getExpiredDays() {
383
		if (this.expiredDays == 0) return 3;
384
		return expiredDays;
385
	}
386

  
387
	/**
388
	 * @param expiredDays
389
	 *            the expiredDays to set
390
	 */
391
	public void setExpiredDays(final int expiredDays) {
392
		this.expiredDays = expiredDays;
393
	}
394
}
modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/MDStoreDaoImpl.java
37 37
	private boolean discardRecords = true;
38 38

  
39 39
	@Override
40
	public void createMDStore(String mdId, String format, String interpretation, String layout) {
40
	public void createMDStore(final String mdId, final String format, final String interpretation, final String layout) {
41 41

  
42 42
		String internalId = mdId;
43 43
		if (internalId.contains("_")) {
......
56 56
	}
57 57

  
58 58
	@Override
59
	public void deleteMDStore(String mdId) {
59
	public void deleteMDStore(final String mdId) {
60 60
		DBCollection coll = db.getCollection("metadata");
61 61

  
62 62
		String internalId = mdId;
......
74 74
	}
75 75

  
76 76
	@Override
77
	public MDStore getMDStore(String mdId) {
77
	public MDStore getMDStore(final String mdId) {
78 78
		String internalId = mdId;
79 79
		if (internalId.contains("_")) {
80 80
			internalId = StringUtils.substringBefore(mdId, "_");
......
88 88
	}
89 89

  
90 90
	@Override
91
	public List<String> listMDStores(String format, String layout, String interpretation) {
91
	public List<String> listMDStores(final String format, final String layout, final String interpretation) {
92 92
		return MappedCollection.listMap(
93 93
				FilteredCollection.listFilter(getDb().getCollection("metadata").find(), MDStoreUtils.dboFilter(format, layout, interpretation)),
94 94
				MDStoreUtils.mdId());
......
99 99
	}
100 100

  
101 101
	@Required
102
	public void setDb(DB db) {
102
	public void setDb(final DB db) {
103 103
		this.db = db;
104 104
	}
105 105

  
......
108 108
	}
109 109

  
110 110
	@Required
111
	public void setRecordParser(RecordParser recordParser) {
111
	public void setRecordParser(final RecordParser recordParser) {
112 112
		this.recordParser = recordParser;
113 113
	}
114 114

  
115 115
	@Required
116
	public void setUpsert(boolean upsert) {
116
	public void setUpsert(final boolean upsert) {
117 117
		this.upsert = upsert;
118 118
	}
119 119

  
......
125 125
		return discardRecords;
126 126
	}
127 127

  
128
	public void setDiscardRecords(boolean discardRecords) {
128
	public void setDiscardRecords(final boolean discardRecords) {
129 129
		this.discardRecords = discardRecords;
130 130
	}
131 131

  
modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/main/java/eu/dnetlib/data/mdstore/modular/inspector/MDStoreInspector.java
8 8

  
9 9
import org.apache.commons.logging.Log;
10 10
import org.apache.commons.logging.LogFactory;
11
import org.springframework.beans.factory.annotation.Autowired;
11 12
import org.springframework.stereotype.Controller;
12 13
import org.springframework.ui.Model;
13 14
import org.springframework.web.bind.annotation.RequestMapping;
......
17 18
import com.google.common.collect.Iterables;
18 19
import com.google.common.collect.Lists;
19 20

  
21
import eu.dnetlib.data.mdstore.MDStoreServiceException;
20 22
import eu.dnetlib.data.mdstore.modular.MDStoreFeeder;
21 23
import eu.dnetlib.data.mdstore.modular.connector.MDStore;
22 24
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao;
25
import eu.dnetlib.data.mdstore.modular.connector.MDStoreManagerInfo;
26
import eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager;
23 27
import eu.dnetlib.data.mdstore.modular.mongodb.MongoMDStore;
24 28
import eu.dnetlib.enabling.inspector.AbstractInspectorController;
25 29
import eu.dnetlib.enabling.resultset.ResultSetListener;
......
29 33

  
30 34
@Controller
31 35
public class MDStoreInspector extends AbstractInspectorController {
36

  
32 37
	private static final Log log = LogFactory.getLog(MDStoreInspector.class); // NOPMD by marko on 11/24/08 5:02 PM
33 38

  
34
	@Resource(name="mongodbMDStoreDao")
39
	@Resource(name = "mongodbMDStoreDao")
35 40
	private MDStoreDao mdstoreDao;
36 41

  
42
	@Autowired
43
	private MDStoreTransactionManager transactionManager;
44

  
37 45
	@Resource
38 46
	private MDStoreFeeder feeder;
39 47

  
40 48
	class MDStoreDescription {
49

  
41 50
		private String id;
42 51

  
43 52
		private int size;
44 53

  
45
		public MDStoreDescription(String id, int size) {
54
		public MDStoreDescription(final String id, final int size) {
46 55
			super();
47 56
			this.id = id;
48 57
			this.size = size;
......
59 68
		public String getInterpretation() {
60 69
			return mdstoreDao.getMDStore(id).getInterpretation();
61 70
		}
62
		
71

  
63 72
		public boolean getIndexed() {
64
			return ((MongoMDStore) mdstoreDao.getMDStore(id)).isIndexed();
73

  
74
			String currentid;
75
			try {
76
				currentid = transactionManager.getMDStoreCollection(id);
77
				return ((MongoMDStore) mdstoreDao.getMDStore(currentid)).isIndexed();
78
			} catch (MDStoreServiceException e) {
79
				return false;
80
			}
81

  
65 82
		}
66 83

  
67 84
		public String getId() {
68 85
			return id;
69 86
		}
70 87

  
71
		public void setId(String id) {
88
		public void setId(final String id) {
72 89
			this.id = id;
73 90
		}
74 91

  
......
76 93
			return size;
77 94
		}
78 95

  
79
		public void setSize(int size) {
96
		public void setSize(final int size) {
80 97
			this.size = size;
81 98
		}
82 99
	}
......
84 101
	@RequestMapping(value = "/inspector/mdstores.do")
85 102
	public void mdstores(final Model model) {
86 103
		model.addAttribute("mdstores", MappedCollection.listMap(mdstoreDao.listMDStores(), new UnaryFunction<MDStoreDescription, String>() {
104

  
87 105
			@Override
88
			public MDStoreDescription evaluate(String arg) {
89
				MDStore mdstore = mdstoreDao.getMDStore(arg);
90
				return new MDStoreDescription(arg, mdstore.getSize());
106
			public MDStoreDescription evaluate(final String arg) {
107
				try {
108
					String currentid = transactionManager.getMDStoreCollection(arg);
109
					MDStore mdstore = mdstoreDao.getMDStore(currentid);
110
					return new MDStoreDescription(arg, mdstore.getSize());
111
				} catch (MDStoreServiceException e) {
112
					return null;
113
				}
114

  
91 115
			}
92 116
		}));
93 117
	}
94 118

  
95 119
	@RequestMapping(value = "/inspector/mdstore.do", method = RequestMethod.GET)
96
	public void mdstore(
97
			final Model model, 
98
			@RequestParam("id") String id, 
99
			@RequestParam(value = "start", required = false) Integer startParam,
100
			@RequestParam(value = "regex", required = false) String regex) {
120
	public void mdstore(final Model model,
121
			@RequestParam("id") final String id,
122
			@RequestParam(value = "start", required = false) final Integer startParam,
123
			@RequestParam(value = "regex", required = false) final String regex) throws MDStoreServiceException {
101 124
		int pageSize = 10;
102 125
		int start = 0;
103 126

  
104
		if (startParam != null)
127
		if (startParam != null) {
105 128
			start = startParam;
129
		}
106 130

  
107
		MDStore mdstore = mdstoreDao.getMDStore(id);
131
		String currentId = transactionManager.getMDStoreCollection(id);
108 132

  
133
		MDStore mdstore = mdstoreDao.getMDStore(currentId);
134

  
109 135
		ResultSetListener rs = mdstore.deliver(null, null, regex);
110 136
		List<String> page = rs.getResult(1 + start, start + pageSize);
111
		
137

  
112 138
		model.addAttribute("id", id);
113 139
		model.addAttribute("start", start);
114 140
		model.addAttribute("regex", regex);
......
117 143
		model.addAttribute("size", rs.getSize());
118 144
		model.addAttribute("page", listMap(page, new EscapeHtml()));
119 145
	}
120
	
146

  
121 147
	@RequestMapping(value = "/inspector/mdstore.do", method = RequestMethod.POST)
122
	public String bulkReplace(
123
			final Model model, 
124
			@RequestParam("id") String id,
148
	public String bulkReplace(final Model model,
149
			@RequestParam("id") final String id,
125 150
			@RequestParam("regex") final String regex,
126 151
			@RequestParam("replace") final String replace,
127
			@RequestParam(value = "checkReplace", required = false) final Boolean checkReplace) {
152
			@RequestParam(value = "checkReplace", required = false) final Boolean checkReplace) throws MDStoreServiceException {
128 153

  
129 154
		log.debug("regex: " + regex);
130 155
		log.debug("replace: " + replace);
131
		
132
		MongoMDStore mdstore = (MongoMDStore) mdstoreDao.getMDStore(id);
133
		
134
		boolean replaceEnable = checkReplace != null && checkReplace == true;
135
		
136
		if (replaceEnable)
156
		String currentId = transactionManager.getMDStoreCollection(id);
157

  
158
		MongoMDStore mdstore = (MongoMDStore) mdstoreDao.getMDStore(currentId);
159

  
160
		boolean replaceEnable = (checkReplace != null) && (checkReplace == true);
161

  
162
		if (replaceEnable) {
137 163
			mdstore.replace(regex, replace);
138
		else
164
		} else {
139 165
			model.addAttribute("regex", regex);
140
		
166
		}
167

  
141 168
		return "redirect:mdstore.do?id=" + id;
142
	}	
169
	}
143 170

  
144 171
	@RequestMapping(value = "/inspector/mdstoreEditResult.do")
145
	public void mdstoreEditRecord(final Model model, @RequestParam("id") String id, @RequestParam("start") int start, @RequestParam("index") int index) {
146
		MDStore mdstore = mdstoreDao.getMDStore(id);
172
	public void mdstoreEditRecord(final Model model,
173
			@RequestParam("id") final String id,
174
			@RequestParam("start") final int start,
175
			@RequestParam("index") final int index) throws MDStoreServiceException {
176
		String currentId = transactionManager.getMDStoreCollection(id);
177
		MDStore mdstore = mdstoreDao.getMDStore(currentId);
147 178

  
148 179
		ResultSetListener rs = mdstore.deliver(null, null, null);
149 180
		List<String> page = rs.getResult(1 + index + start, 1 + index + start);
......
154 185
	}
155 186

  
156 187
	@RequestMapping(value = "/inspector/mdstoreSaveRecord.do")
157
	public String mdstoreSaveRecord(
158
			final Model model,
159
			@RequestParam("id") String id,
160
			@RequestParam("start") int start,
161
			@RequestParam("record") String record) {
188
	public String mdstoreSaveRecord(final Model model,
189
			@RequestParam("id") final String id,
190
			@RequestParam("start") final int start,
191
			@RequestParam("record") final String record) {
162 192
		MDStore mdstore = mdstoreDao.getMDStore(id);
163 193

  
164 194
		mdstore.feed(Lists.newArrayList(record), true);
......
167 197
	}
168 198

  
169 199
	@RequestMapping(value = "/inspector/mdstoreDeleteRecord.do")
170
	public String mdstoreDeleteRecord(final Model model, @RequestParam("id") String id, @RequestParam("start") int start, @RequestParam("index") int index) {
171
		MDStore mdstore = mdstoreDao.getMDStore(id);
200
	public String mdstoreDeleteRecord(final Model model,
201
			@RequestParam("id") final String id,
202
			@RequestParam("start") final int start,
203
			@RequestParam("index") final int index) throws MDStoreServiceException {
172 204

  
205
		String currentId = transactionManager.getMDStoreCollection(id);
206
		MDStore mdstore = mdstoreDao.getMDStore(currentId);
207

  
173 208
		ResultSetListener rs = mdstore.deliverIds(null, null, null);
174 209
		List<String> page = rs.getResult(1 + index + start, 1 + index + start);
175 210
		String recordId = Iterables.getOnlyElement(page);
......
181 216
	}
182 217

  
183 218
	@RequestMapping(value = "/inspector/mdstoresRefreshSizes.do")
184
	public String mdstoresRefreshSizes(final Model model) {
219
	public String mdstoresRefreshSizes(final Model model) throws MDStoreServiceException {
185 220

  
186
		for (String mdId : mdstoreDao.listMDStores())
187
			feeder.touchSize(mdId, mdstoreDao.getMDStore(mdId).getSize());
221
		for (String mdId : mdstoreDao.listMDStores()) {
222
			String currentId = transactionManager.getMDStoreCollection(mdId);
223
			feeder.touchSize(mdId, mdstoreDao.getMDStore(currentId).getSize());
224
		}
188 225

  
189 226
		return "redirect:mdstores.do";
190 227
	}
191
	
228

  
192 229
	@RequestMapping(value = "/inspector/ensure.do")
193
	public String mdstoreEnsureIndex(final Model model, @RequestParam("id") String id) {
194
		MongoMDStore mdStore = (MongoMDStore) mdstoreDao.getMDStore(id);
195
		
230
	public String mdstoreEnsureIndex(final Model model, @RequestParam("id") final String id) throws MDStoreServiceException {
231

  
232
		String currentId = transactionManager.getMDStoreCollection(id);
233
		MongoMDStore mdStore = (MongoMDStore) mdstoreDao.getMDStore(currentId);
234

  
196 235
		log.info("manual ensureIndex for mdId: " + id);
197 236
		mdStore.ensureIndices();
198
		
237

  
199 238
		return "redirect:mdstores.do";
200 239
	}
201 240

  
241
	@RequestMapping(value = "/inspector/infoTransaction.do")
242
	public void mdstoreInfoTransaction(final Model model, @RequestParam("id") final String id) throws MDStoreServiceException {
243
		MDStoreManagerInfo info = transactionManager.getInfoForCurrentMdStore(id);
244
		model.addAttribute("info", info);
245
	}
246

  
247
	@RequestMapping(value = "/inspector/dropUsedCollection.do")
248
	public String dropUsedCollection(final Model model, @RequestParam("mdId") final String mdId, @RequestParam("id") final String id)
249
			throws MDStoreServiceException {
250
		transactionManager.dropUsed(mdId, id);
251
		return "redirect:mdstores.do?id=" + mdId;
252
	}
202 253
}
modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/main/resources/eu/dnetlib/enabling/views/inspector/mdstores.st
11 11
    <th>ID</th>
12 12
    <th>Indexed?</th>
13 13
    <th>Ensure index</th>
14
    <th>MDStore Transaction</th>
14 15
  </tr>
15 16
  $mdstores:{
16
    <tr><td>$it.format$</td><td>$it.layout$</td><td>$it.interpretation$</td><td>$it.size$</td><td><a href="mdstore.do?id=$it.id$">$it.id$</a></td><td><span class="$if(it.indexed)$enabled$else$disabled$endif$">$it.indexed$</span></td><td><a href="ensure.do?id=$it.id$">GO</a></td></tr>
17
    <tr><td>$it.format$</td><td>$it.layout$</td><td>$it.interpretation$</td><td>$it.size$</td><td><a href="mdstore.do?id=$it.id$">$it.id$</a></td><td><span class="$if(it.indexed)$enabled$else$disabled$endif$">$it.indexed$</span></td><td><a href="ensure.do?id=$it.id$">GO</a></td><td><a href="infoTransaction.do?id=$it.id$">GO</a></td></tr>
17 18
  }$
18 19
</table>
19 20
})$
modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/main/resources/eu/dnetlib/enabling/views/inspector/infoTransaction.st
1
$inspector/master(it={
2

  
3
<h2>MDStores Transaction Info</h2>
4

  
5
<h3>MDStore Id : </h3>
6
<a href="mdstore.do?id=$info.mdId$">$info.mdId$</a>
7

  
8
<h3>Current collection id: </h3>
9
<a href="mdstore.do?id=$info.mdId$">$info.currentId$</a>
10

  
11

  
12

  
13

  
14

  
15

  
16

  
17
<h3> Expired MDStore : </h3>
18
<table>
19
  <tr>
20
    <th>Id</th>
21
    <th>Last time read</th>    
22
    <th>drop</th>    
23
  </tr>  
24
  $info.stillUsed:{
25
   	<tr><td>$it.id$</td> <td>$it.lastRead$</td><td><a href="dropUsedCollection.do?mdId=$info.mdId$&id=$it.id$">drop </a></td> </tr>
26
   }$ 
27
</table>
28

  
29

  
30
<h3> Transaction MDStore : </h3>
31
<table>
32
  <tr>
33
    <th>Id</th>
34
    <th>Date</th>    
35
    <th>Size</th>    
36
  </tr>  
37
  $info.transactions:{
38
   	<tr><td>$it.id$</td> <td>$it.date$</td><td>$it.size$</td> </tr>
39
   }$ 
40
</table>
41

  
42

  
43
})$
modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/main/resources/eu/dnetlib/data/mdstore/modular/mongodb/applicationContext-mongodb-mdstore.xml
22 22
				p:connectionsPerHost="${services.mdstore.mongodb.connectionsPerHost}" />
23 23
		</constructor-arg>
24 24
	</bean>
25
	
26
	<bean id= "mongoMdStoreTransaction" 
27
			class="eu.dnetlib.data.mdstore.modular.mongodb.MDStoreTransactionManagerImpl" 
28
			p:db-ref="mdstoreMongoDB" 
29
	/>
30
	
25 31

  
26 32
	<bean id="mdstoreMongoDB" factory-bean="mdstoreMongoServer"
27 33
		factory-method="getDB">
modules/cnr-mongo-mdstore/branches/transaction-mdstore/pom.xml
51 51
			<artifactId>mongo-java-driver</artifactId>
52 52
			<version>${mongodb.driver.version}</version>
53 53
		</dependency>
54
		<dependency>
55
			<groupId>com.google.code.gson</groupId>
56
			<artifactId>gson</artifactId>
57
			<version>2.2</version>
58
		</dependency>
59
		<dependency>
60
			<groupId>joda-time</groupId>
61
			<artifactId>joda-time</artifactId>
62
			<version>2.3</version>
63
		</dependency>
64

  
54 65
	</dependencies>
55 66
</project>

Also available in: Unified diff