List of usage examples for com.mongodb DBCollection getDB
public DB getDB()
From source file:org.icgc.dcc.release.job.imports.hadoop.MongoAdminInputFormat.java
License:Open Source License
private static Mongo getHadoopMongo(MongoURI mongoUri) { DBCollection collection = MongoConfigUtil.getCollection(mongoUri); return collection.getDB().getMongo(); }
From source file:org.semispace.semimeter.dao.mongo.SemiMeterDaoMongo.java
License:Apache License
private void deleteOldMinutes(long before24h, long before180min, long before15min) { Calendar cal = new GregorianCalendar(); cal.setTimeInMillis(before24h);//from w w w .j a v a 2s .com cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); long targetHour = cal.getTimeInMillis(); DBCollection meterCollection = mongoTemplate.getCollection("meter"); DBCursor result = meterCollection.find(new BasicDBObject(), new BasicDBObject("_id", 1)); while (result.hasNext()) { DBObject doc = result.next(); try { //start a new "session" for each document. not sure if this actually helps anything consistency-wise meterCollection.getDB().requestStart(); //and fetch actual object (result only contains _id's) doc = meterCollection.findOne(doc); // TODO Double check log.trace("cleaning document : {}", doc); DBObject day = (DBObject) doc.get("day"); //log.trace("day: {}", day); DBObject hours = (DBObject) day.get("hours"); //log.trace("hours: {}", hours); Set<String> hrSet = new HashSet<String>(); hrSet.addAll(hours.keySet()); boolean docChanged = false; if (hrSet.isEmpty()) { log.trace("no hours in document, remove it: {}", doc); meterCollection.remove(new BasicDBObject("_id", doc.get("_id")), WriteConcern.UNACKNOWLEDGED); } else { for (String h : hrSet) { long hourmillis = Long.valueOf(h); log.trace("checking hour: {}", hourmillis); if (hourmillis < targetHour) { if (log.isTraceEnabled()) { log.trace("removing hour " + h + " because it is older than target" + targetHour); } docChanged = true; DBObject obj = (DBObject) hours.get(h); day.put("count", (Integer) day.get("count") - (Integer) obj.get("count")); hours.removeField(h); } else if (hourmillis == targetHour) { log.trace("current hour is targetHour, check minutes"); DBObject currentHour = (DBObject) hours.get(h); DBObject minutes = (DBObject) currentHour.get("minutes"); Set<String> keys = new HashSet<String>(); keys.addAll(minutes.keySet()); for (String m : keys) { long minutemillis = Long.valueOf(m); log.trace("checking minute: {}", minutemillis); if (minutemillis < before24h) { if (log.isTraceEnabled()) { log.trace("removing minute " + minutemillis + " because it is older than " + before24h); } docChanged = true; DBObject obj = (DBObject) minutes.get(m); DBObject hourObj = (DBObject) hours.get(h); day.put("count", (Integer) day.get("count") - (Integer) obj.get("count")); hourObj.put("count", (Integer) hourObj.get("count") - (Integer) obj.get("count")); minutes.removeField(m); } } if (minutes.keySet().isEmpty()) { log.trace("no more minutes, removing hour {}", h); hours.removeField(h); docChanged = true; } } } } docChanged |= updateTrendCounters(doc, before180min, before15min); if (docChanged) { meterCollection.save(doc); } } finally { meterCollection.getDB().requestDone(); } } }
From source file:org.slc.sli.dal.aspect.MongoTrackingAspect.java
License:Apache License
@Around("call(* com.mongodb.DBCollection.*(..)) && !this(MongoTrackingAspect) && !within(org..*Test) && !within(org..*MongoPerfRepository)") public Object trackDBCollection(ProceedingJoinPoint pjp) throws Throwable { long start = System.currentTimeMillis(); Object result = pjp.proceed(); long end = System.currentTimeMillis(); if (isEnabled()) { DBCollection col = (DBCollection) pjp.getTarget(); proceedAndTrack(pjp, col.getDB().getName(), pjp.getSignature().getName(), col.getName(), start, end); }//from ww w . ja v a 2 s . com if (Boolean.valueOf(dbCallTracking)) { dbCallTracker.incrementHitCount(); // dbCallTracker.addMetric("t", pjp.getSignature().toShortString(), end-start); } return result; }