List of usage examples for com.mongodb MongoClient getDB
@Deprecated public DB getDB(final String dbName)
From source file:com.ebay.cloud.cms.sysmgmt.monitor.metrics.MongoMetric.java
License:Apache License
private Map<String, Object> listDatabases(final MongoClient client) { try {/*from ww w. ja v a 2s .c om*/ Future<Map<String, Object>> future = executor.submit(new Callable<Map<String, Object>>() { @Override public Map<String, Object> call() { Map<String, Object> resultMap = new HashMap<String, Object>(); List<String> databaseNames = client.getDatabaseNames(); for (String databaseName : databaseNames) { DB db = client.getDB(databaseName); if (db != null) { CommandResult cr = db.getStats(); if (cr != null) { Object dataSize = cr.get("dataSize"); resultMap.put(databaseName, dataSize); } } } return resultMap; } }); return future.get(listWaitPeroid, TimeUnit.MILLISECONDS); } catch (Exception e) { return Collections.emptyMap(); } }
From source file:com.edgytech.umongo.CmdField.java
License:Apache License
void updateFromCmd(MongoClient mongo) { if (db == null) { return; } updateFromCmd(mongo.getDB(db)); }
From source file:com.edgytech.umongo.CollectionPanel.java
License:Apache License
public void fixCollection(ButtonBase button) { final MongoClient m = getCollectionNode().getDbNode().getMongoNode().getMongoClient(); ArrayList<MongoNode> mongoNodes = UMongo.instance.getMongos(); String[] mongonames = new String[mongoNodes.size() - 1]; MongoClient[] mongos = new MongoClient[mongonames.length]; int i = 0;/*from w w w . j a v a 2 s . c o m*/ for (MongoNode node : mongoNodes) { MongoClient m2 = node.getMongoClient(); if (m == m2) { continue; } mongonames[i] = m2.toString(); mongos[i] = m2; ++i; } ComboBox src = (ComboBox) getBoundUnit(Item.fcSrcMongo); src.items = mongonames; src.structureComponent(); FormDialog dialog = (FormDialog) getBoundUnit(Item.fcDialog); if (!dialog.show()) { return; } final DBCollection dstCol = getCollectionNode().getCollection(); final MongoClient srcMongo = mongos[src.getIntValue()]; final boolean upsert = getBooleanFieldValue(Item.fcUpsert); final String dbname = dstCol.getDB().getName(); final String colname = dstCol.getName(); final DBCollection srcCol = srcMongo.getDB(dbname).getCollection(colname); String txt = "About to copy from "; txt += srcMongo.getConnectPoint() + "(" + srcCol.count() + ")"; txt += " to "; txt += m.getConnectPoint() + "(" + dstCol.count() + ")"; if (!new ConfirmDialog(null, "Confirm Fix Collection", null, txt).show()) { return; } new DbJob() { @Override public Object doRun() { DBCursor cur = srcCol.find(); int count = 0; int dup = 0; while (cur.hasNext()) { DBObject obj = cur.next(); if (upsert) { BasicDBObject id = new BasicDBObject("_id", obj.get("_id")); dstCol.update(id, obj, true, false); } else { try { dstCol.insert(obj); } catch (DuplicateKey e) { // dup keys are expected here ++dup; } } ++count; } DBObject res = new BasicDBObject("count", count); res.put("dups", dup); return res; } @Override public String getNS() { return "*"; } @Override public String getShortName() { return "Fix Collection"; } }.addJob(); }
From source file:com.edgytech.umongo.DbPanel.java
License:Apache License
public void enableSharding(ButtonBase button) { MongoClient m = getDbNode().getMongoNode().getMongoClient(); DB admin = m.getDB("admin"); DBObject cmd = new BasicDBObject("enableSharding", getDbNode().getDb().getName()); new DbJobCmd(admin, cmd, this, null).addJob(); }
From source file:com.edgytech.umongo.DbPanel.java
License:Apache License
public void movePrimary(ButtonBase button) { FormDialog dialog = (FormDialog) ((MenuItem) getBoundUnit(Item.movePrimary)).getDialog(); ComboBox combo = (ComboBox) getBoundUnit(Item.mvpToShard); combo.value = 0;// ww w. j ava 2 s. co m combo.items = getDbNode().getMongoNode().getShardNames(); combo.structureComponent(); if (!dialog.show()) { return; } MongoClient m = getDbNode().getMongoNode().getMongoClient(); DB admin = m.getDB("admin"); String shard = getStringFieldValue(Item.mvpToShard); DBObject cmd = new BasicDBObject("movePrimary", getDbNode().getDb().getName()); cmd.put("to", shard); new DbJobCmd(admin, cmd, this, null).addJob(); }
From source file:com.edgytech.umongo.MainMenu.java
License:Apache License
public void connect() { try {//from ww w. ja va 2s. com ConnectDialog dialog = (ConnectDialog) getBoundUnit(Item.connectDialog); ProgressDialog progress = (ProgressDialog) getBoundUnit(Item.connectProgressDialog); MongoClient mongo = null; List<String> dbs = new ArrayList<String>(); String uri = dialog.getStringFieldValue(ConnectDialog.Item.uri); if (!uri.trim().isEmpty()) { if (!uri.startsWith(MongoURI.MONGODB_PREFIX)) { uri = MongoURI.MONGODB_PREFIX + uri; } MongoClientURI muri = new MongoClientURI(uri); mongo = new MongoClient(muri); String db = muri.getDatabase(); if (db != null && !db.trim().isEmpty()) { dbs.add(db.trim()); } } else { String servers = dialog.getStringFieldValue(ConnectDialog.Item.servers); if (servers.trim().isEmpty()) { return; } String[] serverList = servers.split(","); ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>(); for (String server : serverList) { String[] tmp = server.split(":"); if (tmp.length > 1) { addrs.add(new ServerAddress(tmp[0], Integer.valueOf(tmp[1]).intValue())); } else { addrs.add(new ServerAddress(tmp[0])); } } if ("Direct".equals(dialog.getStringFieldValue(ConnectDialog.Item.connectionMode))) mongo = new MongoClient(addrs.get(0), dialog.getMongoClientOptions()); else mongo = new MongoClient(addrs, dialog.getMongoClientOptions()); String sdbs = dialog.getStringFieldValue(ConnectDialog.Item.databases); if (!sdbs.trim().isEmpty()) { for (String db : sdbs.split(",")) { dbs.add(db.trim()); } } } if (dbs.size() == 0) { dbs = null; } String user = dialog.getStringFieldValue(ConnectDialog.Item.user).trim(); String password = dialog.getStringFieldValue(ConnectDialog.Item.password); if (!user.isEmpty()) { // authenticate against all dbs if (dbs != null) { for (String db : dbs) { mongo.getDB(db).authenticate(user, password.toCharArray()); } } else { mongo.getDB("admin").authenticate(user, password.toCharArray()); } } final MongoClient fmongo = mongo; final List<String> fdbs = dbs; // doing in background can mean concurrent modification, but dialog is modal so unlikely progress.show(new ProgressDialogWorker(progress) { @Override protected void finished() { } @Override protected Object doInBackground() throws Exception { UMongo.instance.addMongoClient(fmongo, fdbs); return null; } }); } catch (Exception ex) { UMongo.instance.showError(id, ex); } }
From source file:com.edgytech.umongo.MongoPanel.java
License:Apache License
public void createDB(ButtonBase button) { final String name = getStringFieldValue(Item.createDbName); final MongoClient mongo = getMongoNode().getMongoClient(); // need to do a command to actually create on server final DB db = mongo.getDB(name); new DbJob() { @Override/*from www . ja v a 2 s. c o m*/ public Object doRun() throws IOException { db.getStats(); return new BasicDBObject("ok", 1); } @Override public String getNS() { return db.getName(); } @Override public String getShortName() { return "Create DB"; } @Override public DBObject getRoot(Object result) { return new BasicDBObject("name", name); } @Override public void wrapUp(Object res) { super.wrapUp(res); getMongoNode().structureComponent(); } }.addJob(); }
From source file:com.edgytech.umongo.MongoPanel.java
License:Apache License
public void authenticate(final ButtonBase button) { final MongoClient mongo = getMongoNode().getMongoClient(); final String user = getStringFieldValue(Item.authUser); final String passwd = getStringFieldValue(Item.authPassword); new DbJob() { @Override//from www .j a v a 2 s.com public Object doRun() throws IOException { mongo.getDB("admin").authenticateCommand(user, passwd.toCharArray()); return null; } @Override public String getNS() { return "Mongo"; } @Override public String getShortName() { return "Auth"; } @Override public void wrapUp(Object res) { super.wrapUp(res); if (res == null) { // need to refresh tree refresh(); } } @Override public ButtonBase getButton() { return button; } }.addJob(); }
From source file:com.edgytech.umongo.MongoPanel.java
License:Apache License
public void cloneDB(ButtonBase button) { final MongoClient mongo = getMongoNode().getMongoClient(); final String host = getStringFieldValue(Item.cloneDBHost); final String from = getStringFieldValue(Item.cloneDBFrom); final String to = getStringFieldValue(Item.cloneDBTo); final boolean slaveOk = getBooleanFieldValue(Item.cloneDBSlaveOk); final BasicDBObject cmd = new BasicDBObject("copydb", 1); cmd.put("fromhost", host); cmd.put("fromdb", from); if (!to.isEmpty()) { cmd.put("todb", to); }//from ww w . j a v a2 s.co m if (slaveOk) { cmd.put("slaveOk", slaveOk); } new DbJobCmd(mongo.getDB("admin"), cmd, this, null).addJob(); }
From source file:com.edgytech.umongo.MongoPanel.java
License:Apache License
public void currentOps(ButtonBase button) { final MongoClient mongo = getMongoNode().getMongoClient(); final DBObject query = ((DocBuilderField) getBoundUnit(Item.currentOpsQuery)).getDBObject(); CollectionPanel.doFind(mongo.getDB("admin").getCollection("$cmd.sys.inprog"), query); }