List of usage examples for com.mongodb DBCursor count
public int count()
From source file:com.ikanow.infinit.e.utility.MongoEntityFeatureTxfer.java
License:Apache License
static void doDelete(BasicDBObject query, int nLimit, boolean automatedRequest) { try {//from ww w . ja va2s.c om // Initialize the DB: DBCollection entityFeatureDB = DbManager.getFeature().getEntity(); ElasticSearchManager elasticManager = ElasticSearchManager.getIndex("entity_index"); BasicDBObject fields = new BasicDBObject(); fields.put(EntityFeaturePojo.index_, 1); fields.put(EntityFeaturePojo.communityId_, 1); DBCursor cur = entityFeatureDB.find(query, fields).limit(nLimit); // (this internally works in batches of 1000) if (automatedRequest) { System.out.println("Found " + cur.count() + " records to delete from _id list"); } else { System.out.println("Found " + cur.count() + " records to delete from " + query.toString()); } if (nLimit > 0) { System.out.println("(limited to " + nLimit + " records)"); } int nArraySize = (cur.count() > 1000) ? 1000 : cur.count(); ArrayList<EntityFeaturePojo> batchList = new ArrayList<EntityFeaturePojo>(nArraySize); while (cur.hasNext()) { EntityFeaturePojo gp = EntityFeaturePojo.fromDb(cur.next(), EntityFeaturePojo.class); batchList.add(gp); if (batchList.size() >= nArraySize) { internalDelete(batchList, elasticManager); batchList.clear(); } } if (!batchList.isEmpty()) { internalDelete(batchList, elasticManager); } entityFeatureDB.remove(query); } catch (NumberFormatException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } finally { } }
From source file:com.ikanow.infinit.e.utility.MongoIndexerUtils.java
License:Apache License
public static List<BasicDBObject> getChunks(String namespace, String description) { DBCollection configDB = DbManager.getCollection("config", "chunks"); BasicDBObject query = new BasicDBObject("ns", namespace); // Parse description: if (!description.equalsIgnoreCase("all")) { if (description.startsWith("+")) { query.put("_id", new BasicDBObject(DbManager.gt_, description.substring(1))); } else if (description.startsWith("@")) { // it's a list of replica sets String replicas[] = description.substring(1).split("\\s*,\\s*"); query.put("shard", new BasicDBObject(DbManager.in_, replicas)); } else { // assume it's a set of chunks (allow for hacky replacement because "s don't seem to get loaded from windows?) String ids[] = description.replace("^QUOTE^", "\"").split("\\s*,\\s*"); query.put("_id", new BasicDBObject(DbManager.in_, ids)); }/* ww w . ja v a 2s. co m*/ } //TESTED all 3 cases //DEBUG //System.out.println("CHUNKQ=" + query.toString()); // Get chunks and build queries DBCursor dbc = configDB.find(query); ArrayList<BasicDBObject> retList = new ArrayList<BasicDBObject>(dbc.count()); while (dbc.hasNext()) { DBObject chunk = dbc.next(); BasicDBObject derivedQuery = new BasicDBObject(); BasicDBObject minObj = (BasicDBObject) chunk.get("min"); BasicDBObject maxObj = (BasicDBObject) chunk.get("max"); if (null != minObj) { BasicDBObject modifier = addChunkModifier(minObj); if (null != modifier) derivedQuery.put(DbManager.min_, modifier); } if (null != maxObj) { BasicDBObject modifier = addChunkModifier(maxObj); if (null != modifier) derivedQuery.put(DbManager.max_, modifier); } if (!derivedQuery.isEmpty()) { derivedQuery.put("$id", chunk.get("_id")); // (temp save the _id for printing in the main loop) retList.add(derivedQuery); } } return retList; }
From source file:com.jagornet.dhcp.db.MongoLeaseManager.java
License:Open Source License
/** * Find dhcp leases for ia.//from w w w . j av a2 s. com * * @param duid the duid * @param iatype the iatype * @param iaid the iaid * @return the list */ protected List<DhcpLease> findDhcpLeasesForIA(final byte[] duid, final byte iatype, final long iaid) { List<DhcpLease> leases = null; DBObject query = new BasicDBObject("duid", duid).append("iatype", iatype).append("iaid", iaid); // SQL databases will store in ipAddress order because it is a primary key, // but Mongo is not so smart because it is just an index on the field DBCursor cursor = dhcpLeases.find(query).sort(new BasicDBObject("ipAddress", 1)); ; try { 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
/** * Find dhcp lease for InetAddr.//from ww w .j av a2 s .c om * * @param inetAddr the InetAddr * @return the DhcpLease */ protected DhcpLease findDhcpLeaseForInetAddr(final InetAddress inetAddr) { DhcpLease lease = null; DBObject query = ipAddressQuery(inetAddr); DBCursor cursor = dhcpLeases.find(query); try { if (cursor.count() > 0) { if (cursor.count() == 1) { lease = convertDBObject(cursor.next()); } else { //TODO: ensure this is impossible with mongo's unique index? log.error("Found more than one lease for IP=" + inetAddr.getHostAddress()); } } } finally { cursor.close(); } return lease; }
From source file:com.jagornet.dhcp.db.MongoLeaseManager.java
License:Open Source License
@Override public List<InetAddress> findExistingIPs(final InetAddress startAddr, final InetAddress endAddr) { List<InetAddress> inetAddrs = new ArrayList<InetAddress>(); BasicDBList ipBetw = new BasicDBList(); ipBetw.add(new BasicDBObject("ipAddress", new BasicDBObject("$gte", startAddr.getAddress()))); ipBetw.add(new BasicDBObject("ipAddress", new BasicDBObject("$lte", endAddr.getAddress()))); DBObject query = new BasicDBObject("$and", ipBetw); DBCursor cursor = dhcpLeases.find(query).sort(new BasicDBObject("ipAddress", 1)); try {/*w w w. j av a 2 s. c om*/ if (cursor.count() > 0) { while (cursor.hasNext()) { inetAddrs.add(convertDBObject(cursor.next()).getIpAddress()); } } } finally { cursor.close(); } return inetAddrs; }
From source file:com.jagornet.dhcp.db.MongoLeaseManager.java
License:Open Source License
@Override public List<IaAddress> findUnusedIaAddresses(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", IaAddress.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(IaAddress.EXPIRED);/*from www . j av a 2 s .c om*/ ipExpRel.add(IaAddress.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 toIaAddresses(leases); } } finally { cursor.close(); } return null; }
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 {/*w w w.jav a 2 s .c o 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 w w. ja va 2s . com*/ 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 {//www .j a va 2 s. c om 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// ww w . j av 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(); } }