List of usage examples for com.mongodb DBCursor hasNext
@Override public boolean hasNext()
From source file:com.appleframework.monitor.action.LogsAction.java
License:Open Source License
@RequestMapping(value = "/projects/{projectName}/logs/more", method = RequestMethod.GET) public void console(final HttpServletResponse response, ModelMap map, @PathVariable String projectName, LogQuery logQuery) throws IOException, ParseException { Project project = projectService.findProject(projectName); map.put("project", project); final MongoConverter converter = project.fetchMongoTemplate().getConverter(); final DBCursor cursor = logsService.findLogs(projectName, logQuery); final StringBuffer buf = new StringBuffer(); FutureTask<String> task = new FutureTask<String>(new Callable<String>() { @Override// www .j a va 2s . c o m public String call() throws Exception { long startTime = System.currentTimeMillis(); //???20 logger.debug("result:"); while (cursor.hasNext()) { Log log = converter.read(Log.class, cursor.next()); buf.insert(0, log.toString() + "\n"); long current = System.currentTimeMillis(); if ((current - startTime) / 1000 >= mongWaitSeconds) break; } return buf.toString(); } }); executor.execute(task); try { task.get(mongWaitSeconds + 5, TimeUnit.SECONDS); cursor.close(); } catch (Exception e) { logger.error("time out ", e); task.cancel(true); } response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(buf.toString()); response.getWriter().flush(); }
From source file:com.arquivolivre.mongocom.management.CollectionManager.java
License:Apache License
/** * Find all documents that match the specified query in the given * collection.//from w w w.ja v a 2 s . c o m * * @param <A> generic type of the collection. * @param collectionClass * @param query * @return a list of documents. */ public <A extends Object> List<A> find(Class<A> collectionClass, MongoQuery query) { List<A> resultSet = new ArrayList<>(); DBCursor cursor = null; try { A obj = collectionClass.newInstance(); String collectionName = reflectCollectionName(obj); cursor = db.getCollection(collectionName).find(query.getQuery(), query.getConstraits()); if (query.getSkip() > 0) { cursor = cursor.skip(query.getSkip()); } if (query.getLimit() > 0) { cursor = cursor.limit(query.getLimit()); } while (cursor.hasNext()) { DBObject objDB = cursor.next(); loadObject(obj, objDB); resultSet.add(obj); obj = collectionClass.newInstance(); } } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) { LOG.log(Level.SEVERE, null, ex); } finally { if (cursor != null) { cursor.close(); } } return resultSet; }
From source file:com.aw.app.action.BuildingAction.java
/** * /*from w ww . j a v a 2 s . co m*/ * @param uid * @return */ public List getUserBuildings(long uid) { List buildings = new ArrayList(); DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base"); BasicDBObject query = new BasicDBObject("uid", uid); DBCursor cursor = table.find(query); while (cursor.hasNext()) { DBObject object = cursor.next(); buildings.add(object); } return buildings; }
From source file:com.aw.app.action.BuildingAction.java
/** * // w ww . j a v a 2 s . com * @param uid * @return */ public long getTotalHousingSpace(long uid) { long campSpace = 0; DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base"); BasicDBObject query = new BasicDBObject(); query.put("tid", HOUSING_TID); query.put("uid", uid); DBCursor cursor = table.find(query); while (cursor.hasNext()) { DBObject building = cursor.next(); if (building.containsField("buildingArmies")) { /// In complete Code } } return campSpace; }
From source file:com.aw.app.action.BuildingAction.java
/** * /*from w w w. j a va 2s . c om*/ * @param uid * @param buildingId * @param time * @return */ public boolean checkBuildingUpgrade(Object uid, Object buildingId, long time) { boolean state = false; DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base"); BasicDBObject query = new BasicDBObject(); query.put("uid", uid); query.put("building_id", buildingId); DBCursor cursor = table.find(query); { while (cursor.hasNext()) { DBObject buildingBase = cursor.next(); if (time == 0) time = new Date().getTime(); boolean isConstructing = (Boolean) buildingBase.get("is_constructing"); if (isConstructing) { long startTime = (Long) buildingBase.get("construction_start_time"); long endTime = startTime + ApplicationSettings.convertToTimeStamp(buildingBase); if (time > endTime) { BasicDBObject queryUpdate = new BasicDBObject(); queryUpdate.put("uid", buildingBase.get("uid")); queryUpdate.put("building_id", buildingBase.get("building_id")); BasicDBObject toUpdate = new BasicDBObject(); toUpdate.put("is_constructing", 0); toUpdate.put("construction_start_time", 0); BasicDBObject update = new BasicDBObject("$set", toUpdate); MongoDbUtil.updateCollection("aw_building_base", queryUpdate, update); return true; } } boolean upgrading = (Boolean) buildingBase.get("is_upgrading"); if (upgrading) { long startTime = (Long) buildingBase.get("upgrade_start_time"); DBObject nextLevelConfig = this.getBuildingConfiguration(buildingBase.get("tid"), ((Long) buildingBase.get("level") + 1)); long endTime = startTime + ApplicationSettings.convertToTimeStamp(nextLevelConfig); if (time > endTime) { buildingBase.put("is_upgrading", 0); buildingBase.put("upgrade_start_time", 0); buildingBase.put("level", (Long) buildingBase.get("level") + 1); state = this.updateBuildingFromConfiguration(buildingBase); } } } } return state; }
From source file:com.aw.app.action.StrategyAction.java
/** * //from w w w .ja v a2s . c o m * @param uid */ public void bootStrapAction(Long uid) { /********** Perform User Building Checking ***************/ DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base"); BasicDBObject query = new BasicDBObject("uid", uid); DBCursor userBuildings = table.find(query); int count = 0; while (userBuildings.hasNext()) { DBObject building = userBuildings.next(); if (building.containsField("tid")) { String tid = (String) building.get("tid"); TroopsAction troopsAction = new TroopsAction(); if (tid != null && tid.equalsIgnoreCase("TID_BUILDING_TAVERN")) { troopsAction.checkToopsUpgrades(uid, building); count++; } else if (tid != null && tid.equalsIgnoreCase("TID_BUILDING_BARRACK")) { int buildingId = (Integer) building.get("building_id"); troopsAction.checkToopsInBarrack(uid, buildingId); count++; } } } }
From source file:com.aw.app.action.TroopsAction.java
/** * /*from w w w. j a va2 s .c o m*/ * @param uid * @param buildingId * @return */ public boolean checkToopsInBarrack(Long uid, long buildingId) { boolean state = false; Long time = new Date().getTime(); DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_barrack_troops"); BasicDBObject query = new BasicDBObject("uid", uid); query.put("building_id", buildingId); DBCursor barrackTroops = table.find(query); long nextTroopsTrainingTime = 0; if (barrackTroops != null && barrackTroops.size() > 0) { while (barrackTroops.hasNext()) { DBObject barrackTroop = barrackTroops.next(); if (nextTroopsTrainingTime > 0 && state) { BasicDBObject updateQuery = new BasicDBObject(); updateQuery.put("bc_id", barrackTroop.get("bc_id")); BasicDBObject updateBt = new BasicDBObject(); Map data = new HashMap(); data.put("is_first_in_queue", 1); data.put("training_start_time", nextTroopsTrainingTime); BasicDBObject object = new BasicDBObject(data); updateBt.put("$set", object); MongoDbUtil.updateCollection("aw_barrack_troops", updateQuery, updateBt); } Map result = processTroopTraining(barrackTroop, time); boolean breakState = (boolean) result.get("breakState"); state = (boolean) result.get("status"); long nextTrainingTime = (long) result.get("nextTrainingTime"); if (breakState) { // Error Condition } else if (state && result.containsKey("nextTrainingTime") && nextTrainingTime > 0) { nextTroopsTrainingTime = nextTrainingTime; } else { nextTroopsTrainingTime = 0; } } } return state; }
From source file:com.aw.app.action.TroopsAction.java
/** * //from w ww . j a v a 2s .c o m * @param troopSet * @param time * @return */ public Map processTroopTraining(DBObject troopSet, Long time) { Map result = new HashMap(); DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_troops_configuration"); BasicDBObject query = new BasicDBObject(); query.put("tid", troopSet.get("tid")); query.put("level", troopSet.get("level")); DBCursor resultSet = table.find(query); while (resultSet.hasNext()) { DBObject config = resultSet.next(); long remainingSpace = this.getRemainingTroopSpace((Long) troopSet.get("uid")); // Get from Method Later. if (config != null) { long trainingTime = (Long) config.get("training_time"); long count = (Long) troopSet.get("count"); long totalElapseTimeRequired = trainingTime * count; long trainingStartTime = (Long) troopSet.get("training_start_time"); long endTime = trainingStartTime + totalElapseTimeRequired; long housingSpace = (Long) config.get("housing_space"); long spaceRequired = count * housingSpace; if (endTime < new Date().getTime()) { if (remainingSpace >= spaceRequired) { boolean isAdded = addTroopOfUsers((Long) config.get("troop_id"), (Long) troopSet.get("uid"), (String) config.get("tid"), (Long) troopSet.get("count")); if (isAdded) { BasicDBObject deleteQuery = new BasicDBObject(); deleteQuery.put("bc_id", troopSet.get("bc_id")); MongoDbUtil.removeCollection("aw_barrack_troops", deleteQuery); result.put("status", true); result.put("nextTrainingTime", endTime); } } else if (remainingSpace == 0) { BasicDBObject queryObject = new BasicDBObject("bc_id", troopSet.get("bc_id")); BasicDBObject updateData = new BasicDBObject("training_start_time", new Date().getTime()); BasicDBObject updateQuery = new BasicDBObject("$set", updateData); MongoDbUtil.updateCollection(MongoDbUtil.defaultDBName, queryObject, updateQuery); result.put("status", false); result.put("breakStatus", true); result.put("reason", "Training Fields are full."); } else { long troopCount = (long) Math.floor(remainingSpace / housingSpace); this.addTroopOfUsers((Long) config.get("troop_id"), (Long) troopSet.get("uid"), (String) config.get("tid"), troopCount); long intialCount = (Long) troopSet.get("count"); long resultCount = intialCount - count; Map queryMap = new HashMap(); queryMap.put("bc_id", troopSet.get("bc_id")); Map updateData = new HashMap(); updateData.put("count", resultCount); updateData.put("training_start_time", trainingStartTime + (trainingTime * troopCount)); Map dataMap = new HashMap(); dataMap.put("query", queryMap); dataMap.put("update", updateData); MongoDbUtil.updateCollection("aw_barrack_troops", updateData); result.put("status", true); result.put("breakStatus", true); result.put("reason", "Training Fields are full."); } } } else { result.put("reason", "Configuration not found"); } } return result; }
From source file:com.aw.app.action.TroopsAction.java
/** * /*from www. ja v a2 s . com*/ * @param uid * @return */ public long getRemainingTroopSpace(long uid) { long totalSpace = new BuildingAction().getTotalHousingSpace(uid); long consumedSpace = 0; long remainingSpace = totalSpace; DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_troops"); BasicDBObject query = new BasicDBObject("uid", uid); DBCursor cursor = table.find(query); Map troopMap = new HashMap(); while (cursor.hasNext()) { DBObject userTroop = cursor.next(); long troopId = (Long) userTroop.get("troop_id"); if (troopMap.containsKey(troopId)) { long value = (Long) troopMap.get(troopId); ++value; troopMap.put(troopId, value); } else { troopMap.put(troopId, 1); } } Iterator entries = troopMap.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); long key = (Long) entry.getKey(); long value = (Long) entry.getValue(); DBCollection configTable = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_troops_configuration"); DBCursor configCursor = configTable.find(new BasicDBObject("troop_id", key)); while (configCursor.hasNext()) { DBObject config = configCursor.next(); long configHousingSpace = (Long) config.get("housing_space"); consumedSpace = consumedSpace + (configHousingSpace * value); } } remainingSpace = remainingSpace - consumedSpace; if (remainingSpace < 0) remainingSpace = 0; return remainingSpace; }
From source file:com.aw.app.action.TroopsAction.java
/** * //from w w w . j av a 2 s. c om * @param uid * @param action * @return */ public List getUserTroopsData(long uid, String action) { List troopsData = new ArrayList(); String owner = ""; if ("self".equalsIgnoreCase(action)) owner = "1"; if ("".equalsIgnoreCase(action)) owner = "0"; DBCursor userTroopsResult = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_troops") .find(new BasicDBObject("uid", uid).append("owner", owner)); while (userTroopsResult.hasNext()) { DBObject userTroop = userTroopsResult.next(); troopsData.add(userTroop); } return troopsData; }