List of usage examples for com.mongodb MongoClient getDB
@Deprecated public DB getDB(final String dbName)
From source file:mini_mirc_server.miniIRCHandler.java
/** * Moved active user to a passive user (soon to be deleted) * @param username/* w ww.j a v a 2 s.c om*/ * @return code */ public int SoftDelete(String username) { int ret = 0; try { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll = db.getCollection("activeUser"); DBCollection coll2 = db.getCollection("passiveUser"); BasicDBObject query = new BasicDBObject("username", username); DBCursor cursor = coll.find(query); try { if (cursor.hasNext()) { DBObject temp = cursor.next(); coll2.insert(temp); coll.remove(temp); } else { ret = 1; } } finally { cursor.close(); } } catch (UnknownHostException ex) { Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex); } return ret; }
From source file:mini_mirc_server.miniIRCHandler.java
public String GetMessage(String username) { String ret = ""; try {/*from w ww . j a v a 2 s. c o m*/ MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll = db.getCollection("inbox"); BasicDBObject query = new BasicDBObject("target", username); JSONObject obj = new JSONObject(); JSONArray arr = new JSONArray(); DBCursor cursor = coll.find(query); try { while (cursor.hasNext()) { BasicDBObject temp = (BasicDBObject) cursor.next(); JSONObject sav = new JSONObject(); sav.put("target", temp.getString("target")); sav.put("username", temp.getString("username")); sav.put("channel", temp.getString("channel")); sav.put("message", temp.getString("message")); sav.put("timestamp", temp.getLong("timestamp")); arr.add(sav); coll.remove(temp); } obj.put("msg", arr); ret = obj.toJSONString(); } finally { cursor.close(); } } catch (UnknownHostException ex) { Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex); } UpdateLastActive(username); return ret; }
From source file:mini_mirc_server.miniIRCHandler.java
private int PutMessage(String username, String channelname, String msg) { int ret = 0;//from w ww . ja v a 2 s.c om try { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll = db.getCollection("inbox"); DBCollection coll2 = db.getCollection("channelCollection"); BasicDBObject query2 = new BasicDBObject("channel", channelname); BasicDBObject query = new BasicDBObject("channel", channelname).append("username", username); DBCursor cursor = coll2.find(query); try { if (cursor.hasNext()) { DBCursor cursor2 = coll2.find(query2); System.out.println("Got message from " + username); try { java.util.Date date = new java.util.Date(); while (cursor2.hasNext()) { ret = 1; BasicDBObject temp = (BasicDBObject) cursor2.next(); String target = temp.get("username").toString(); BasicDBObject put = new BasicDBObject("target", target).append("username", username) .append("channel", channelname).append("message", msg) .append("timestamp", date.getTime()); coll.insert(put); ret = 0; } } finally { cursor2.close(); } } else { ret = 2; System.out.println(username + " not registered to Channel : " + channelname); } } finally { cursor.close(); } } catch (UnknownHostException ex) { ret = 1; Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex); } return ret; }
From source file:mini_mirc_server.miniIRCHandler.java
private int PutMessageWild(String username, String msg) { int ret = 0;//from w w w . ja v a2 s.co m try { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll2 = db.getCollection("channelCollection"); BasicDBObject query = new BasicDBObject("username", username); System.out.println("Wild message appear from " + username + " !"); DBCursor cursor = coll2.find(query); try { while (cursor.hasNext()) { ret = 1; BasicDBObject temp = (BasicDBObject) cursor.next(); String channelname = temp.getString("channel"); ret = PutMessage(username, channelname, msg); } } finally { cursor.close(); } } catch (UnknownHostException ex) { Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex); } return 0; }
From source file:mini_mirc_server.Mini_mirc_server.java
public static void cleaner() { try {/* ww w .j a v a2 s.com*/ boolean hard_clean = false; MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll = db.getCollection("activeUser"); DBCursor cursor = coll.find(); try { Date now = new Date(); long timestamp_now = now.getTime(); long treshold = timestamp_now - (1000 * 60 * 5); //5 minutes while (cursor.hasNext()) { hard_clean = true; BasicDBObject temp = (BasicDBObject) cursor.next(); Date time_temp = (Date) temp.get("timestamp"); long timestamp_temp = time_temp.getTime(); if (timestamp_temp < treshold) { String target = temp.getString("username"); handler.SoftDelete(target); } } HardClean(); } finally { cursor.close(); } } catch (UnknownHostException ex) { Logger.getLogger(Mini_mirc_server.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:mini_mirc_server.Mini_mirc_server.java
public static void HardClean() { try {//from w w w.j av a 2 s . co m MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll[] = new DBCollection[4]; coll[0] = db.getCollection("channelCollection"); coll[1] = db.getCollection("inbox"); coll[2] = db.getCollection("activeUser"); coll[3] = db.getCollection("passiveUser"); DBCursor cursor = coll[3].find(); try { while (cursor.hasNext()) { BasicDBObject temp = (BasicDBObject) cursor.next(); String username = temp.getString("username"); BasicDBObject query = new BasicDBObject("username", username); System.out.println("cleaning " + username); for (int i = 0; i < 4; i++) { DBCursor cursor2 = coll[i].find(query); try { while (cursor2.hasNext()) { DBObject temp2 = cursor2.next(); coll[i].remove(temp2); } } finally { cursor2.close(); } } } } finally { cursor.close(); } } catch (UnknownHostException ex) { Logger.getLogger(Mini_mirc_server.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:mini_mirc_server.Mini_mirc_server.java
/** * ONLY IF NO USER IS IN THE ACTIVE OR PASIVE COLLECTION;getting server to clean state *///from ww w.j ava 2 s.c om private static void UltimateClean() { try { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll[] = new DBCollection[4]; coll[0] = db.getCollection("activeUser"); coll[1] = db.getCollection("passiveUser"); coll[2] = db.getCollection("channelCollection"); coll[3] = db.getCollection("inbox"); DBCursor cursor[] = new DBCursor[4]; cursor[0] = coll[0].find(); cursor[1] = coll[1].find(); cursor[2] = coll[2].find(); cursor[3] = coll[3].find(); try { if (!cursor[0].hasNext() && !cursor[1].hasNext() && cursor[2].hasNext() && cursor[3].hasNext()) { System.out.println("SYSTEM RESTARTING with ULTIMATE CLEANING !"); for (int i = 2; i <= 3; i++) { coll[i].drop(); } System.out.println("RESTART COMPLETE!"); } } finally { cursor[0].close(); cursor[1].close(); } } catch (UnknownHostException ex) { Logger.getLogger(Mini_mirc_server.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:mongocrud.DBManager.java
public static DB getDatabase() { if (database == null) { MongoClient mongo; try {/*www . jav a2 s . c o m*/ mongo = new MongoClient("localhost", 27017); database = mongo.getDB("University"); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return database; }
From source file:mongodb.countDocument.java
public static void main(String[] args) { try {//from w ww. j a v a 2s . com MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("words"); DBCollection collection = db.getCollection("word_stats"); countDocument.countWords(collection); } catch (Exception e) { System.out.println(e); } }
From source file:mongoDB.DBManager.java
public static DB getDatabase() { if (database == null) { MongoClient mongo; try {/*w w w. ja va 2 s . com*/ mongo = new MongoClient("RiviConn", 27017); mongo.getDB("usermanager"); } catch (UnknownHostException e) { e.printStackTrace(); } } return database; }