Project

General

Profile

« Previous | Next » 

Revision 50676

using commons-lang3, using lambdas

View differences:

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

  
3 3
import java.util.ArrayList;
4
import java.util.Collections;
5 4
import java.util.Date;
6 5
import java.util.List;
7 6

  
......
20 19
import eu.dnetlib.data.mdstore.MDStoreServiceException;
21 20
import eu.dnetlib.data.mdstore.modular.connector.*;
22 21
import eu.dnetlib.data.mdstore.modular.mongodb.utils.MDStoreUtils;
23
import org.apache.commons.lang.StringUtils;
22
import org.apache.commons.lang3.StringUtils;
24 23
import org.apache.commons.logging.Log;
25 24
import org.apache.commons.logging.LogFactory;
26 25
import org.bson.conversions.Bson;
......
91 90
	/**
92 91
	 * Gets the DBObject describing an mdstore. null if there is no mdstore with the given id.
93 92
	 *
94
	 * @param mdstoreID
93
	 * @param mdstoreID the mdStore identifier
95 94
	 * @return DBObject or null
96 95
	 */
97
	private DBObject getMDStoreAsDBObject(String mdstoreID) throws MDStoreServiceException {
96
	private DBObject getMDStoreAsDBObject(final String mdstoreID) throws MDStoreServiceException {
98 97
		final BasicDBObject query = new BasicDBObject();
99 98
		query.put("mdId", mdstoreID);
100 99
		final FindIterable<DBObject> it = this.getManagerTable().find(query);
101
		DBObject mdstoreInfo = it.first();
102
		return mdstoreInfo;
100
		return it.first();
103 101
	}
104 102

  
105 103
	/**
......
196 194
			values = new BasicDBList();
197 195
		}
198 196
		final BasicDBObject transactionMetadata = new BasicDBObject();
199
		transactionMetadata.put("id", idCreation.toString());
197
		transactionMetadata.put("id", idCreation);
200 198
		transactionMetadata.put("refresh", refresh);
201 199
		transactionMetadata.put("date", new Date());
202 200
		values.add(transactionMetadata);
203 201
		mdstoreInfo.put("transactions", values);
204 202
		this.getManagerTable().findOneAndReplace(new BasicDBObject("_id", mdstoreInfo.get("_id")), mdstoreInfo);
205
		return idCreation.toString();
203
		return idCreation;
206 204
	}
207 205

  
208 206
	/**
......
253 251
	 */
254 252
	private DBObject findTransaction(final BasicDBList transactions, final String transactionId) {
255 253
		if (transactions.size() == 0) return null;
256
		for (int i = 0; i < transactions.size(); i++) {
257
			final BasicDBObject transaction = (BasicDBObject) transactions.get(i);
254
		for (Object tx : transactions) {
255
			final BasicDBObject transaction = (BasicDBObject) tx;
258 256
			final String id = (String) transaction.get("id");
259 257
			if (transactionId.equals(id)) return transaction;
260 258
		}
......
311 309
	 */
312 310
	private void updateMdstoreUsed(final BasicDBList values, final String mdId) {
313 311
		if (values.size() > 0) {
314
			for (int i = 0; i < values.size(); i++) {
315
				final DBObject obj = (DBObject) values.get(i);
312
			for (Object value : values) {
313
				final DBObject obj = (DBObject) value;
316 314
				final String id = (String) obj.get("id");
317 315
				if (mdId.equals(id)) {
318 316
					obj.put("lastRead", new Date());
......
359 357
		result.setCurrentId((String) mdstoreInfo.get("currentId"));
360 358
		result.setMdId((String) mdstoreInfo.get("mdId"));
361 359
		final BasicDBList values = (BasicDBList) mdstoreInfo.get("expiring");
362
		for (int i = 0; i < values.size(); i++) {
360
		for (Object v : values) {
363 361
			final MDStoreExpiredInfo stillused = new MDStoreExpiredInfo();
364
			final DBObject value = (DBObject) values.get(i);
362
			final DBObject value = (DBObject) v;
365 363
			stillused.setId((String) value.get("id"));
366 364
			stillused.setLastRead((Date) value.get("lastRead"));
367 365
			result.addExpiredItem(stillused);
368 366
		}
369 367
		final BasicDBList transactions = (BasicDBList) mdstoreInfo.get("transactions");
370 368
		if (transactions != null) {
371
			for (int i = 0; i < transactions.size(); i++) {
369
			for (Object tx : transactions) {
372 370
				final MDStoreTransactionInfo transaction = new MDStoreTransactionInfo();
373
				final DBObject value = (DBObject) transactions.get(i);
371
				final DBObject value = (DBObject) tx;
374 372
				final String transactionId = (String) value.get("id");
375 373
				transaction.setId(transactionId);
376 374
				transaction.setDate((Date) value.get("date"));
......
435 433
		// DELETING Collection that are not in the metadataManager table
436 434
		MongoIterable<String> collections = this.db.listCollectionNames();
437 435
		for (String collection : collections) {
438
			if ((collection.length() > 30) && (collection.contains("discarded-") == false)) {
436
			if ((collection.length() > 30) && (!collection.contains("discarded-"))) {
439 437
				final DBObject item = getMetadataObjectForCollections(collection);
440 438

  
441 439
				if (shouldDelete(collection, item)) {
......
457 455
	private DBObject getMetadataObjectForCollections(final String collectionName) {
458 456
		if (collectionName == null) return null;
459 457
		final String postfix = "_TURTdG9yZURTUmVzb3VyY2VzL01EU3RvcmVEU1Jlc291cmNlVHlwZQ==";
460
		final String tmp = collectionName.contains("discarded-") == true ? StringUtils.substringAfter(collectionName, "discarded-") : collectionName;
458
		final String tmp = collectionName.contains("discarded-") ? StringUtils.substringAfter(collectionName, "discarded-") : collectionName;
461 459
		final String collectionNameCleaned = StringUtils.substringBefore(tmp, "::") + postfix;
462 460

  
463 461
		//DBObject query = QueryBuilder.start("mdId").is(collectionNameCleaned).get();
......
475 473
		String currentId = (String) metadataManagerInstance.get("currentId");
476 474
		if (collectionName.equals(currentId)) return false;
477 475
		BasicDBList expiringList = (BasicDBList) metadataManagerInstance.get("expiring");
478
		if (findInList(expiringList, collectionName, "id") == true) return false;
476
		if (findInList(expiringList, collectionName, "id")) return false;
479 477
		BasicDBList transactionsList = (BasicDBList) metadataManagerInstance.get("transactions");
480
		return findInList(transactionsList, collectionName, "id") != true;
478
		return !findInList(transactionsList, collectionName, "id");
481 479
	}
482 480

  
483 481
	private boolean findInList(final BasicDBList list, final String object, final String tagname) {
484 482
		if (list == null) return false;
485
		for (int i = 0; i < list.size(); i++) {
486
			DBObject currentObject = (DBObject) list.get(i);
483
		for (Object o : list) {
484
			DBObject currentObject = (DBObject) o;
487 485
			final String id = (String) currentObject.get(tagname);
488 486
			if (id.equals(object)) return true;
489 487
		}
......
526 524

  
527 525
		List<DBObject> expiringList = Lists.newArrayList();
528 526

  
529
		for (int i = 0; i < expiring.size(); i++) {
530
			final DBObject cobj = (DBObject) expiring.get(i);
527
		for (Object o : expiring) {
528
			final DBObject cobj = (DBObject) o;
531 529
			if (cobj != null) {
532
				expiringList.add((DBObject) expiring.get(i));
530
				expiringList.add((DBObject) o);
533 531
			}
534 532

  
535 533
		}
536 534

  
537
		Collections.sort(expiringList, MDStoreUtils.getComparatorOnDate());
535
		expiringList.sort(MDStoreUtils.getComparatorOnDate());
538 536

  
539 537
		List<DBObject> toRemove = Lists.newArrayList();
540 538
		int i = 0;
......
592 590
			log.debug("Deleted  0  expired  collections, mdStore Id:" + currentObject.get("mdId"));
593 591
			return;
594 592
		}
595
		for (int i = 0; i < expiring.size(); i++) {
596
			final DBObject currentExpiredStore = (DBObject) expiring.get(i);
593
		for (Object anExpiring : expiring) {
594
			final DBObject currentExpiredStore = (DBObject) anExpiring;
597 595
			final String currentUsedId = (String) currentExpiredStore.get("id");
598 596
			final Days d = getExpiringDays(currentExpiredStore, "lastRead");
599 597
			if (log.isDebugEnabled()) {
......
602 600
			// DELETE the collection where the last time they was read
603 601
			// is more than 3 days ago
604 602
			if (d.getDays() > getExpiredDays()) {
605
				if (currentUsedId.equals(currentId) == false) {
603
				if (!currentUsedId.equals(currentId)) {
606 604
					db.getCollection(currentUsedId).drop();
607 605
					db.getCollection("discarded-" + currentUsedId).drop();
608 606
					log.debug("deleted collection " + currentUsedId);
......
702 700
			currentMdStoreId.add((String) currentObject.get("currentId"));
703 701
			final BasicDBList transactions = (BasicDBList) currentObject.get("transactions");
704 702
			if ((transactions != null) && (transactions.size() > 0)) {
705
				for (int i = 0; i < transactions.size(); i++) {
706
					final DBObject currentTransactions = (DBObject) transactions.get(i);
703
				for (Object tx : transactions) {
704
					final DBObject currentTransactions = (DBObject) tx;
707 705
					final String id = (String) currentTransactions.get("id");
708 706
					db.getCollection(id).drop();
709 707
					db.getCollection("discarded-" + id).drop();
......
726 724
				}
727 725
			}
728 726
		}
729

  
730

  
731

  
732

  
733 727
	}
734 728

  
735 729
	/**
modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/utils/MetadataCheckJob.java
57 57
	/**
58 58
	 * Job execution method.
59 59
	 *
60
	 * @throws MDStoreServiceException
60
	 * @throws MDStoreServiceException in case of ISLookUpException or DocumentException
61 61
	 */
62 62
	public void repairMetadata() throws MDStoreServiceException {
63 63
		MongoCollection<DBObject> metadata = ((MDStoreDaoImpl) getDao()).getDb().getCollection("metadata", DBObject.class);
......
68 68
		try {
69 69
			List<String> mdStores =
70 70
					serviceLocator.getService(ISLookUpService.class).quickSearchProfile(
71
							"//RESOURCE_PROFILE[" + ".//RESOURCE_TYPE/@value='MDStoreDSResourceType' and " + ".//RESOURCE_URI/@value='" + getServiceAddress()
72
							+ "']");
71
							"//RESOURCE_PROFILE[" +
72
									".//RESOURCE_TYPE/@value='MDStoreDSResourceType' and " +
73
									".//RESOURCE_URI/@value='" + getServiceAddress() + "']");
73 74

  
74 75
			log.debug("repairing mdstore metadata");
75 76

  
......
81 82
			}
82 83
			log.debug("FINISHED repairing mdstore metadata");
83 84

  
84
		} catch (ISLookUpException e) {
85
		} catch (ISLookUpException | DocumentException e) {
85 86
			throw new RuntimeException(e);
86
		} catch (DocumentException e) {
87
			throw new RuntimeException(e);
88 87
		}
89 88
	}
90 89

  
......
93 92
	 *
94 93
	 * @param MDStoreProfile as obtain from the IS
95 94
	 * @return a DBObject representing the metadata informations
96
	 * @throws DocumentException
95
	 * @throws DocumentException when parsing invalid xml
97 96
	 */
98 97
	private DBObject getMdInfo(final String MDStoreProfile) throws DocumentException {
99 98
		Document doc = new SAXReader().read(new StringReader(MDStoreProfile));
modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/utils/MDStoreUtils.java
11 11
public class MDStoreUtils {
12 12

  
13 13
	public static UnaryFunction<String, DBObject> mdId() {
14
		return new UnaryFunction<String, DBObject>() {
15

  
16
			@Override
17
			public String evaluate(final DBObject arg) {
18
				return (String) arg.get("mdId");
19
			}
20
		};
14
		return arg -> (String) arg.get("mdId");
21 15
	}
22 16

  
23 17
	public static UnaryFunction<Boolean, DBObject> dboFilter(final String format, final String layout, final String interpretation) {
24
		return new UnaryFunction<Boolean, DBObject>() {
25

  
26
			@Override
27
			public Boolean evaluate(final DBObject dbo) {
28
				return dbo.get("format").toString().equals(format) && dbo.get("layout").toString().equals(layout)
29
						&& dbo.get("interpretation").toString().equals(interpretation);
30
			}
31
		};
18
		return dbo -> dbo.get("format").toString().equals(format) && dbo.get("layout").toString().equals(layout)
19
				&& dbo.get("interpretation").toString().equals(interpretation);
32 20
	}
33 21

  
34 22
	public static Function<DBObject, String> body() {
35
		return new Function<DBObject, String>() {
36

  
37
			@Override
38
			public String apply(final DBObject dbo) {
39
				return (String) dbo.get("body");
40
			}
41
		};
23
		return dbo -> (String) dbo.get("body");
42 24
	}
43 25

  
44 26
	public static Comparator<DBObject> getComparatorOnDate() {
45
		return new Comparator<DBObject>() {
46

  
47
			@Override
48
			public int compare(final DBObject o1, final DBObject o2) {
49
				Date d1 = (Date) o1.get("date");
50
				Date d2 = (Date) o2.get("date");
51
				return d1.compareTo(d2);
52
			}
27
		return (o1, o2) -> {
28
			Date d1 = (Date) o1.get("date");
29
			Date d2 = (Date) o2.get("date");
30
			return d1.compareTo(d2);
53 31
		};
54 32
	}
55 33
}
modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/MDStoreDaoImpl.java
22 22
import eu.dnetlib.miscutils.collections.FilteredCollection;
23 23
import eu.dnetlib.miscutils.collections.MappedCollection;
24 24
import eu.dnetlib.miscutils.datetime.DateUtils;
25
import eu.dnetlib.miscutils.functional.UnaryFunction;
26 25
import org.apache.commons.logging.Log;
27 26
import org.apache.commons.logging.LogFactory;
28 27
import org.bson.Document;
......
119 118
	 */
120 119
	@Override
121 120
	public List<MDStoreDescription> listMDStores() {
122
		return MappedCollection.listMap(getDb().getCollection(METADATA_NAME, DBObject.class).find(), new UnaryFunction<MDStoreDescription, DBObject>() {
121
		return MappedCollection.listMap(getDb().getCollection(METADATA_NAME, DBObject.class).find(), input -> {
123 122

  
124
			@Override
125
			public MDStoreDescription evaluate(final DBObject input) {
123
			final String mdId = (String) input.get(MD_ID);
124
			log.debug("Getting info for " + mdId);
125
			final String format = (String) input.get(FORMAT);
126
			final String layout = (String) input.get(LAYOUT);
127
			final String interpretation = (String) input.get(INTERPRETATION);
128
			MongoMDStore currentMDStore = null;
129
			final MDStoreDescription description = new MDStoreDescription();
130
			try {
131
				currentMDStore = (MongoMDStore) getMDStore(mdId);
132
			} catch (final MDStoreServiceException e) {
133
				log.error("Error on retrieving mdstore for getting info mdId " + mdId);
134
			}
126 135

  
127
				final String mdId = (String) input.get(MD_ID);
128
				log.debug("Getting info for " + mdId);
129
				final String format = (String) input.get(FORMAT);
130
				final String layout = (String) input.get(LAYOUT);
131
				final String interpretation = (String) input.get(INTERPRETATION);
132
				MongoMDStore currentMDStore = null;
133
				final MDStoreDescription description = new MDStoreDescription();
134
				try {
135
					currentMDStore = (MongoMDStore) getMDStore(mdId);
136
				} catch (final MDStoreServiceException e) {
137
					log.error("Error on retrieving mdstore for getting info mdId " + mdId);
138
				}
139

  
140
				int size = 0;
141
				if (input.containsField(SIZE)) {
142
					log.debug("Size retrieved from metadata for mdId :" + mdId);
143
					size = (Integer) input.get(SIZE);
144
				} else {
145
					if (currentMDStore != null) {
146
						log.debug("Size not Found in metadata for mdId :" + mdId + " calling getCount ");
147
						size = currentMDStore.getSize();
148
						input.put("size", size);
149
						getDb().getCollection(METADATA_NAME, DBObject.class).findOneAndReplace(new BasicDBObject(MD_ID, mdId), input);
150
					}
151
				}
136
			int size = 0;
137
			if (input.containsField(SIZE)) {
138
				log.debug("Size retrieved from metadata for mdId :" + mdId);
139
				size = (Integer) input.get(SIZE);
140
			} else {
152 141
				if (currentMDStore != null) {
153
					description.setIndexed(currentMDStore.isIndexed());
142
					log.debug("Size not Found in metadata for mdId :" + mdId + " calling getCount ");
143
					size = currentMDStore.getSize();
144
					input.put("size", size);
145
					getDb().getCollection(METADATA_NAME, DBObject.class).findOneAndReplace(new BasicDBObject(MD_ID, mdId), input);
154 146
				}
155
				description.setId(mdId);
156
				description.setFormat(format);
157
				description.setLayout(layout);
158
				description.setInterpretation(interpretation);
159
				description.setSize(size);
160
				return description;
161 147
			}
148
			if (currentMDStore != null) {
149
				description.setIndexed(currentMDStore.isIndexed());
150
			}
151
			description.setId(mdId);
152
			description.setFormat(format);
153
			description.setLayout(layout);
154
			description.setInterpretation(interpretation);
155
			description.setSize(size);
156
			return description;
162 157
		});
163 158
	}
164 159

  
......
296 291
		statsQuery.put("scale", 1024 * 1024); //storageSize in MB
297 292
		final Document statsRes = getDb().runCommand(statsQuery);
298 293
		log.debug("DBStatus --  " + statsRes.toJson());
299
		int usedDiskSpace = 0;
294
		int usedDiskSpace;
300 295
		//trying to handle different versions of the mongo server: old version returns storage size as long, new version as double
301 296
		//TODO: simplify this when dev, beta, production are aligned with our local, latest, mongo version
302 297
		String usedDiskSpaceStr = statsRes.get("storageSize").toString();
modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/MongoMDStore.java
23 23
import eu.dnetlib.data.mdstore.modular.RecordParser;
24 24
import eu.dnetlib.data.mdstore.modular.connector.MDStore;
25 25
import eu.dnetlib.enabling.resultset.ResultSetListener;
26
import org.apache.commons.lang.StringUtils;
26
import org.apache.commons.lang3.StringUtils;
27 27
import org.apache.commons.logging.Log;
28 28
import org.apache.commons.logging.LogFactory;
29 29
import org.bson.conversions.Bson;

Also available in: Unified diff