List of usage examples for com.mongodb DBObject keySet
Set<String> keySet();
From source file:com.edgytech.umongo.MongoUtils.java
License:Apache License
public static void addChildrenToTreeNode(DefaultMutableTreeNode node, DBObject obj) { for (String key : obj.keySet()) { Object val = obj.get(key); // if (val == null) { // continue; // } DefaultMutableTreeNode child = new DefaultMutableTreeNode(new TreeNodeDocumentField(key, val)); if (val instanceof DBObject) { addChildrenToTreeNode(child, (DBObject) val); } else if (val instanceof ObjectId) { // break it down ObjectId id = (ObjectId) val; child.add(new DefaultMutableTreeNode( "Time: " + id.getTime() + " = " + new Date(id.getTime()).toString())); child.add(new DefaultMutableTreeNode("Machine: " + (id.getMachine() & 0xFFFFFFFFL))); child.add(new DefaultMutableTreeNode("Inc: " + (id.getInc() & 0xFFFFFFFFL))); }//from w ww.j av a 2s .c o m node.add(child); } }
From source file:com.edgytech.umongo.MongoUtils.java
License:Apache License
/** * Checks key strings for invalid characters. *///from w w w . j a va 2s. co m public static void checkKeys(DBObject o) { if (o instanceof LazyDBObject || o instanceof LazyDBList) return; for (String s : o.keySet()) { validateKey(s); Object inner = o.get(s); if (inner instanceof DBObject) { checkKeys((DBObject) inner); } else if (inner instanceof Map) { checkKeys((Map<String, Object>) inner); } } }
From source file:com.edgytech.umongo.RouterPanel.java
License:Apache License
public void regenConfigDB(ButtonBase button) throws UnknownHostException { MongoClient cmongo = getRouterNode().getMongoClient(); String servers = getStringFieldValue(Item.regenServers); final String db = getStringFieldValue(Item.regenDB); final String col = getStringFieldValue(Item.regenCollection); final String ns = db + "." + col; final DBObject shardKey = ((DocBuilderField) getBoundUnit(Item.regenShardKey)).getDBObject(); final boolean unique = getBooleanFieldValue(Item.regenKeyUnique); final BasicDBObject result = new BasicDBObject(); result.put("ns", ns); result.put("shardKey", shardKey); result.put("unique", unique); // create direct mongo for each replica set String[] serverList = servers.split("\n"); List<ServerAddress> list = new ArrayList<ServerAddress>(); String txt = ""; String primaryShard = null;// w w w . ja v a 2s. c o m final BasicDBObject shardList = new BasicDBObject(); HashMap<MongoClient, String> mongoToShard = new HashMap<MongoClient, String>(); sLoop: for (String server : serverList) { server = server.trim(); String[] tokens = server.split("/"); if (tokens.length != 2) { new InfoDialog(null, "Error", null, "Server format must be like 'hostname:port/shard', one by line") .show(); return; } server = tokens[0]; String shard = tokens[1]; if (primaryShard == null) { primaryShard = shard; } ServerAddress addr = new ServerAddress(server); // filter out if replset already exists for (MongoClient replset : mongoToShard.keySet()) { if (replset.getServerAddressList().contains(addr)) { continue sLoop; } } list.clear(); list.add(addr); MongoClient mongo = new MongoClient(list); // UMongo.instance.addMongoClient(mongo, null); // make request to force server detection mongo.getDatabaseNames(); mongoToShard.put(mongo, shard); String desc = null; if (!mongo.getDatabaseNames().contains(db) || !mongo.getDB(db).getCollectionNames().contains(col)) { desc = "Collection not present!"; } else { // try to see if shard key has index DBObject index = mongo.getDB(db).getCollection("system.indexes") .findOne(new BasicDBObject("key", shardKey)); if (index != null) { desc = "shard key found"; } else { desc = "shard key NOT found!"; } } txt += mongo.toString() + " shard=" + shard + " - " + desc + "\n"; BasicDBObject shardObj = new BasicDBObject("servers", mongo.toString()); shardObj.put("status", desc); if (shardList.containsField(shard)) { new InfoDialog(null, "Error", null, "Duplicate Shard name " + shard).show(); return; } shardList.put(shard, shardObj); } result.put("shards", shardList); FormDialog dia = (FormDialog) getBoundUnit(Item.regenRSList); dia.setStringFieldValue(Item.regenRSListArea, txt); if (!dia.show()) { return; } DB config = cmongo.getDB("config"); // add database record BasicDBObject doc = new BasicDBObject("_id", db); doc.put("partitioned", true); doc.put("primary", primaryShard); config.getCollection("databases").save(doc); // add collection record doc = new BasicDBObject("_id", ns); doc.put("lastmod", new Date()); doc.put("dropped", false); doc.put("key", shardKey); doc.put("unique", unique); config.getCollection("collections").save(doc); final DBCollection chunks = config.getCollection("chunks"); long count = chunks.count(new BasicDBObject("ns", ns)); if (count > 0) { dia = (FormDialog) getBoundUnit(Item.regenDeleteChunks); if (dia.show()) { chunks.remove(new BasicDBObject("ns", ns)); } else { return; } } // add temp collection to sort chunks with shard key final DBCollection tmpchunks = config.getCollection("_tmpchunks_" + col); tmpchunks.drop(); // should be safe environment, and dup keys should be ignored tmpchunks.setWriteConcern(WriteConcern.NORMAL); // can use shardKey as unique _id // tmpchunks.ensureIndex(shardKey, "shardKey", true); // create filter for shard fields final DBObject shardKeyFilter = new BasicDBObject(); // final DBObject shardKeyDescend = new BasicDBObject(); boolean hasId = false; for (String key : shardKey.keySet()) { shardKeyFilter.put(key, 1); if (key.equals("_id")) { hasId = true; } } if (!hasId) { shardKeyFilter.put("_id", 0); } dia = (FormDialog) getBoundUnit(Item.regenConfirm); if (!dia.show()) { return; } // now fetch all records from each shard final AtomicInteger todo = new AtomicInteger(mongoToShard.size()); for (Map.Entry<MongoClient, String> entry : mongoToShard.entrySet()) { final MongoClient mongo = entry.getKey(); final String shard = entry.getValue(); new DbJob() { @Override public Object doRun() throws Exception { BasicDBObject shardObj = (BasicDBObject) shardList.get(shard); long count = mongo.getDB(db).getCollection(col).count(); shardObj.put("count", count); DBCursor cur = mongo.getDB(db).getCollection(col).find(new BasicDBObject(), shardKeyFilter); long i = 0; int inserted = 0; long start = System.currentTimeMillis(); while (cur.hasNext() && !isCancelled()) { BasicDBObject key = (BasicDBObject) cur.next(); setProgress((int) ((++i * 100.0f) / count)); try { BasicDBObject entry = new BasicDBObject("_id", key); entry.put("_shard", shard); tmpchunks.insert(entry); ++inserted; } catch (Exception e) { getLogger().log(Level.WARNING, e.getMessage(), e); } } if (isCancelled()) { shardObj.put("cancelled", true); } shardObj.put("inserted", inserted); shardObj.put("scanTime", System.currentTimeMillis() - start); todo.decrementAndGet(); return null; } @Override public String getNS() { return tmpchunks.getFullName(); } @Override public String getShortName() { return "Scanning " + shard; } @Override public boolean isDeterminate() { return true; } }.addJob(); } new DbJob() { @Override public Object doRun() throws Exception { // wait for all shards to be done long start = System.currentTimeMillis(); while (todo.get() > 0 && !isCancelled()) { Thread.sleep(2000); } if (isCancelled()) { result.put("cancelled", true); return result; } // find highest current timestamp DBCursor cur = chunks.find().sort(new BasicDBObject("lastmod", -1)).batchSize(-1); BasicDBObject chunk = (BasicDBObject) (cur.hasNext() ? cur.next() : null); BSONTimestamp ts = (BSONTimestamp) (chunk != null ? chunk.get("lastmod") : null); // now infer chunk ranges long count = tmpchunks.count(); result.put("uniqueKeys", count); int numChunks = 0; cur = tmpchunks.find().sort(new BasicDBObject("_id", 1)); BasicDBObject prev = (BasicDBObject) cur.next(); BasicDBObject next = null; // snap prev to minkey BasicDBObject theid = (BasicDBObject) prev.get("_id"); for (String key : shardKey.keySet()) { theid.put(key, new MinKey()); } String currentShard = prev.getString("_shard"); int i = 1; while (cur.hasNext()) { next = (BasicDBObject) cur.next(); setProgress((int) ((++i * 100.0f) / count)); String newShard = next.getString("_shard"); if (newShard.equals(currentShard)) continue; // add chunk ts = getNextTimestamp(ts); chunk = getChunk(ns, shardKey, prev, next, ts); chunks.insert(chunk); prev = next; currentShard = prev.getString("_shard"); ++numChunks; } // build max next = new BasicDBObject(); for (String key : shardKey.keySet()) { next.put(key, new MaxKey()); } next = new BasicDBObject("_id", next); ts = getNextTimestamp(ts); chunk = getChunk(ns, shardKey, prev, next, ts); chunks.insert(chunk); ++numChunks; result.put("numChunks", numChunks); result.put("totalTime", System.currentTimeMillis() - start); return result; } @Override public String getNS() { return chunks.getFullName(); } @Override public String getShortName() { return "Creating Chunks"; } @Override public boolean isDeterminate() { return true; } }.addJob(); }
From source file:com.edgytech.umongo.RouterPanel.java
License:Apache License
BasicDBObject getChunk(String ns, DBObject shardKey, BasicDBObject min, BasicDBObject max, BSONTimestamp ts) { BasicDBObject chunk = new BasicDBObject(); BasicDBObject themin = (BasicDBObject) min.get("_id"); BasicDBObject themax = (BasicDBObject) max.get("_id"); String _id = ns;//from w w w . j a v a2s . c o m for (String key : shardKey.keySet()) { _id += "-" + key + "_"; Object val = themin.get(key); _id += (val != null ? val.toString() : "null"); } chunk.put("_id", _id); chunk.put("lastmod", ts); chunk.put("ns", ns); chunk.put("min", themin); chunk.put("max", themax); chunk.put("shard", min.getString("_shard")); return chunk; }
From source file:com.edgytech.umongo.ServerPanel.java
License:Apache License
public void getLog(ButtonBase button) { final DB db = getServerNode().getServerMongoClient().getDB("admin"); final String type = getStringFieldValue(Item.getLogType); final DBObject cmd = new BasicDBObject("getLog", type); new DbJob() { @Override//from w w w .jav a 2 s. c o m public Object doRun() throws Exception { CommandResult res = db.command(cmd); res.throwOnError(); StringBuilder sb = new StringBuilder(); BasicDBList list = (BasicDBList) res.get("log"); for (Object str : list) { sb.append(str); sb.append("\n"); } return sb.toString(); } @Override public String getNS() { return db.getName(); } @Override public String getShortName() { return cmd.keySet().iterator().next(); } }.addJob(); }
From source file:com.effektif.mongo.MongoHelper.java
License:Apache License
public static Map<String, Object> toMap(DBObject dbObject) { if (dbObject == null) { return null; }/*from www . ja v a2 s.co m*/ Map<String, Object> map = new HashMap<>(); for (String key : dbObject.keySet()) { Object value = dbObject.get(key); if (value instanceof DBObject) { value = toMap((DBObject) value); } map.put(key, value); } return map; }
From source file:com.eharmony.matching.seeking.translator.mongodb.MongoQueryTranslator.java
License:Apache License
private Multimap<String, DBObject> multimap(DBObject[] objects) { Multimap<String, DBObject> m = LinkedHashMultimap.<String, DBObject>create(objects.length, 8); for (DBObject o : objects) { Set<String> keys = o.keySet(); for (String key : keys) { m.put(key, o);/*from w ww . jav a2 s .co m*/ } } return m; }
From source file:com.epam.ta.reportportal.database.ActivityDocumentHandler.java
License:Open Source License
@Override public void processDocument(DBObject dbObject) throws MongoException, DataAccessException { Set<String> keySet = dbObject.keySet(); ChartObject activity = new ChartObject(); Map<String, String> objectValues = new HashMap<>(); for (String key : keySet) { switch (key) { case HISTORY: Map<String, String> historyProps = transformHistoryMap(dbObject, "history"); objectValues.putAll(historyProps); break; case Modifiable.LAST_MODIFIED: objectValues.put(key, String.valueOf(((Date) dbObject.get(key)).getTime())); break; case ID://w ww . j a va 2 s. c o m activity.setId(dbObject.get(ID).toString()); break; default: objectValues.put(key, dbObject.get(key).toString()); break; } } activity.setValues(objectValues); result.add(activity); }
From source file:com.ewcms.common.query.mongo.CriteriaWapper.java
License:Open Source License
/** * ???/* ww w . j av a 2s . c om*/ * * @param dbo * @return */ private boolean hasCriteria(DBObject dbo) { boolean has = false; for (String key : dbo.keySet()) { Object v = dbo.get(key); if (v instanceof DBObject) { has = has || !((DBObject) v).keySet().isEmpty(); } else { has = has || true; } } return has; }
From source file:com.eywa.impl.app.controllers.central.Mail.java
License:Open Source License
private static void notifyScheduledJob(final DBObject job, final JobReport report, final Throwable exception) { if (null != job) { // creates template final MailScheduledJob template; if (null == report) { // exception template = new MailScheduledJobError(); } else {// w w w . j a v a 2 s . co m // report template = new MailScheduledJobReport(); } // send default email to main user, if any notifyScheduledJob(ScheduledJob.getUserId(job), job, report, exception, template); // send emails to other recipients final DBObject recipients = ScheduledJob.getNotifyRecipients(job); if (null != recipients) { final DBObject recipients_templates = ItemJobMessage.getRecipientsWithTemplates(recipients); final Set<String> userIds = recipients_templates.keySet(); for (final String userId : userIds) { final DBObject templateItem = MongoUtils.getDBObject(recipients_templates, userId); if (null != templateItem) { final String title = MongoUtils.getString(templateItem, ItemJobMessage.TITLE); final String content = MongoUtils.getString(templateItem, ItemJobMessage.CONTENT); if (StringUtils.hasText(content)) { final DBObject user = UserService.get(userId); final String recipient_email = User.getEmail(user); final String recipient_realname = User.getRealname(user); final Locale locale = LocaleUtils.getLocaleByLang(User.getLang(user)); // override template data template.setTitle(locale, title); template.setContent(locale, content); notifyScheduledJob(locale, recipient_email, recipient_realname, job, report, exception, template); } } } } } }