/** * Return Stats of a particular Database in mongo to which user is connected to. * * @param dbName Name of Database * @return Array of JSON Objects each containing a key value pair in Db Stats. * @throws JSONException While parsing JSON * @throws DatabaseException Error while performing this operation * @throws ValidationException throw super type of EmptyDatabaseNameException */ public JSONArray getDbStats(String dbName) throws DatabaseException, ValidationException, JSONException { if (dbName == null) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database name is null"); } if (dbName.equals("")) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty"); } JSONArray dbStats = new JSONArray(); try { List<String> dbList = getDbList(); boolean dbPresent = dbList.contains(dbName); if (!dbPresent) { throw new DatabaseException( ErrorCodes.DB_DOES_NOT_EXISTS, "DB with name '" + dbName + "' DOES NOT EXIST"); } MongoDatabase db = mongoInstance.getDatabase(dbName); Document stats = db.runCommand(new Document("dbStats", "1")); Set<String> keys = stats.keySet(); Iterator<String> keyIterator = keys.iterator(); while (keyIterator.hasNext()) { JSONObject temp = new JSONObject(); String key = keyIterator.next(); temp.put("Key", key); String value = stats.get(key).toString(); temp.put("Value", value); String type = stats.get(key).getClass().toString(); temp.put("Type", type.substring(type.lastIndexOf('.') + 1)); dbStats.put(temp); } } catch (MongoException m) { throw new DatabaseException(ErrorCodes.GET_DB_STATS_EXCEPTION, m.getMessage()); } return dbStats; }