List of usage examples for com.mongodb DBCursor close
@Override public void close()
From source file:com.jagornet.dhcp.db.MongoLeaseManager.java
License:Open Source License
protected List<DhcpLease> findExpiredLeases(final byte iatype) { List<DhcpLease> leases = null; DBObject query = new BasicDBObject("iatype", iatype) .append("state", new BasicDBObject("$ne", IaAddress.STATIC)) .append("validEndTime", new BasicDBObject("$lt", new Date())); DBCursor cursor = dhcpLeases.find(query).sort(new BasicDBObject("validEndTime", 1)); try {//from www . j a v a2 s . co m if (cursor.count() > 0) { leases = new ArrayList<DhcpLease>(); while (cursor.hasNext()) { leases.add(convertDBObject(cursor.next())); } } } finally { cursor.close(); } return leases; }
From source file:com.jagornet.dhcp.db.MongoLeaseManager.java
License:Open Source License
@Override public List<IaPrefix> findUnusedIaPrefixes(final InetAddress startAddr, final InetAddress endAddr) { long offerExpireMillis = DhcpServerPolicies.globalPolicyAsLong(Property.BINDING_MANAGER_OFFER_EXPIRATION); final Date offerExpiration = new Date(new Date().getTime() - offerExpireMillis); BasicDBList ipAdvBetw = new BasicDBList(); ipAdvBetw.add(new BasicDBObject("state", IaPrefix.ADVERTISED)); ipAdvBetw.add(new BasicDBObject("startTime", new BasicDBObject("$lte", offerExpiration))); ipAdvBetw.add(new BasicDBObject("ipAddress", new BasicDBObject("$gte", startAddr.getAddress()))); ipAdvBetw.add(new BasicDBObject("ipAddress", new BasicDBObject("$lte", endAddr.getAddress()))); BasicDBList ipExpRel = new BasicDBList(); ipExpRel.add(IaPrefix.EXPIRED);//from w ww . j av a 2s .c o m ipExpRel.add(IaPrefix.RELEASED); BasicDBList ipExpRelBetw = new BasicDBList(); ipExpRelBetw.add(new BasicDBObject("state", new BasicDBObject("$in", ipExpRel))); ipExpRelBetw.add(new BasicDBObject("ipAddress", new BasicDBObject("$gte", startAddr.getAddress()))); ipExpRelBetw.add(new BasicDBObject("ipAddress", new BasicDBObject("$lte", endAddr.getAddress()))); BasicDBList ipBetw = new BasicDBList(); ipBetw.add(new BasicDBObject("$and", ipAdvBetw)); ipBetw.add(new BasicDBObject("$and", ipExpRelBetw)); DBObject query = new BasicDBObject("$or", ipBetw); DBCursor cursor = dhcpLeases.find(query).sort(new BasicDBObject("state", 1)) .sort(new BasicDBObject("validEndTime", 1)).sort(new BasicDBObject("ipAddress", 1)); try { if (cursor.count() > 0) { List<DhcpLease> leases = new ArrayList<DhcpLease>(); while (cursor.hasNext()) { leases.add(convertDBObject(cursor.next())); } return toIaPrefixes(leases); } } finally { cursor.close(); } return null; }
From source file:com.jagornet.dhcp.db.MongoLeaseManager.java
License:Open Source License
@Override public List<IaPrefix> findExpiredIaPrefixes() { List<DhcpLease> leases = null; DBObject query = new BasicDBObject("iatype", IdentityAssoc.PD_TYPE).append("validEndTime", new BasicDBObject("$lt", new Date())); DBCursor cursor = dhcpLeases.find(query).sort(new BasicDBObject("validEndTime", 1)); try {//from w ww.j a v a2 s.c o m if (cursor.count() > 0) { leases = new ArrayList<DhcpLease>(); while (cursor.hasNext()) { leases.add(convertDBObject(cursor.next())); } } } finally { cursor.close(); } return toIaPrefixes(leases); }
From source file:com.jagornet.dhcp.db.MongoLeaseManager.java
License:Open Source License
/** * For unit tests only//from w ww. j a v a 2s.c o m */ public void deleteAllIAs() { DBCursor cursor = dhcpLeases.find(); try { if (cursor.count() > 0) { int i = 0; while (cursor.hasNext()) { dhcpLeases.remove(cursor.next()); i++; } log.info("Deleted all " + i + " dhcpLeases"); } } finally { cursor.close(); } }
From source file:com.jarova.mqtt.MongoDB.java
public static String update(String collection) throws UnknownHostException { if (!initialized) init();/*from www .ja v a 2 s. co m*/ DBCursor cursor = db.getCollection(collection).find(); String lazyReturn = "<html><body>"; try { while (cursor.hasNext()) { lazyReturn += "<p>" + cursor.next() + "</p>"; } } finally { cursor.close(); } lazyReturn += "</body></html>"; return lazyReturn; }
From source file:com.jive.myco.seyren.mongo.MongoStore.java
License:Apache License
@Override public SeyrenResponse<Check> getChecksByState(Set<String> states, Boolean enabled) { List<Check> checks = new ArrayList<Check>(); DBObject query = new BasicDBObject(); query.put("state", NiceDBObject.object("$in", states.toArray())); if (enabled != null) { query.put("enabled", enabled); }//from w w w . j ava 2s. c o m DBCursor dbc = getChecksCollection().find(query); while (dbc.hasNext()) { checks.add(mapper.checkFrom(dbc.next())); } dbc.close(); return new SeyrenResponse<Check>().withValues(checks).withTotal(dbc.count()); }
From source file:com.jive.myco.seyren.mongo.MongoStore.java
License:Apache License
@Override public SeyrenResponse<Alert> getAlerts(String checkId, int start, int items) { DBCursor dbc = getAlertsCollection().find(NiceDBObject.object("checkId", checkId)).sort(NiceDBObject .object("timestamp", -1)).skip(start).limit(items); List<Alert> alerts = new ArrayList<Alert>(); while (dbc.hasNext()) { alerts.add(mapper.alertFrom(dbc.next())); }/* w w w .j a v a 2 s. com*/ dbc.close(); return new SeyrenResponse<Alert>().withValues(alerts).withItems(items).withStart(start) .withTotal(dbc.count()); }
From source file:com.jive.myco.seyren.mongo.MongoStore.java
License:Apache License
@Override public SeyrenResponse<Alert> getAlerts(int start, int items) { DBCursor dbc = getAlertsCollection().find().sort(NiceDBObject.object("timestamp", -1)).skip(start) .limit(items);// w ww .j a v a 2 s . c o m List<Alert> alerts = new ArrayList<Alert>(); while (dbc.hasNext()) { alerts.add(mapper.alertFrom(dbc.next())); } dbc.close(); return new SeyrenResponse<Alert>().withValues(alerts).withItems(items).withStart(start) .withTotal(dbc.count()); }
From source file:com.jive.myco.seyren.mongo.MongoStore.java
License:Apache License
@Override public Alert getLastAlertForTargetOfCheck(String target, String checkId) { DBObject query = NiceDBObject.object("checkId", checkId).with("targetHash", TargetHash.create(target)); DBCursor cursor = getAlertsCollection().find(query).sort(NiceDBObject.object("timestamp", -1)).limit(1); try {//w w w . ja v a2s . co m while (cursor.hasNext()) { return mapper.alertFrom(cursor.next()); } } finally { cursor.close(); } return null; }
From source file:com.lakeside.data.mongo.MongoDbSnapshot.java
License:Open Source License
private List<CollectionInfo> getCollections() { List<CollectionInfo> collections = new ArrayList<CollectionInfo>(); try {/* www . j a v a2s . c o m*/ DB db = MongoDBConnectionManager.getConnection(databaseHost, databaseName, databaseUserName, databasePassword); Collection<String> collectionsInDb = db.getCollectionNames(); List<String> collectionsToAdd = new ArrayList<String>(); List<String> collectionsToSkip = new ArrayList<String>(); selectCollections(COLLECTION_STRING, collectionsToAdd, collectionsToSkip); boolean exclusionsOnly = collectionsToAdd.contains("*"); if (exclusionsOnly) { for (String collectionName : collectionsInDb) { if (!collectionsToSkip.contains(collectionName)) { collectionsToAdd.add(collectionName); } } } else { if (collectionsToAdd.size() == 0) { // add everything collectionsToAdd.addAll(collectionsInDb); } } for (String collectionName : collectionsToAdd) { if (!"system.indexes".equals(collectionName)) { long count = db.getCollection(collectionName).count(); if (count > 0) { CollectionInfo info = new CollectionInfo(collectionName, count); info.setCollectionExists(true); collections.add(info); String indexName = new StringBuilder().append(databaseName).append(".") .append(collectionName).toString(); DBCursor cur = db.getCollection("system.indexes").find(new BasicDBObject("ns", indexName)); if (cur != null) { while (cur.hasNext()) { info.addIndex((BasicDBObject) cur.next()); } } cur.close(); } } } } catch (Exception e) { throw new RuntimeException(e); } return collections; }