List of usage examples for com.mongodb DBCollection getFullName
public String getFullName()
From source file:com.edgytech.umongo.DbPanel.java
License:Apache License
public void addJSFunction(final ButtonBase button) { final DB db = getDbNode().getDb(); final DBCollection col = db.getCollection("system.js"); final String name = getStringFieldValue(Item.addJSFunctionName); final String code = getStringFieldValue(Item.addJSFunctionCode); CollectionPanel.doFind(col, new BasicDBObject("_id", name)); new DbJob() { @Override//w w w . ja va2 s .c o m public Object doRun() throws Exception { DBObject obj = new BasicDBObject("_id", name); obj.put("value", new Code(code)); return col.insert(obj); } @Override public String getNS() { return col.getFullName(); } @Override public String getShortName() { return "Add JS Function"; } @Override public ButtonBase getButton() { return null; } }.addJob(); }
From source file:com.edgytech.umongo.DocumentFieldMenu.java
License:Apache License
public void setValue(ButtonBase button) { final DocView dv = (DocView) (UMongo.instance.getTabbedResult().getSelectedUnit()); if (dv.getDBCursor() == null) { // local data new InfoDialog(null, null, null, "Cannot do in-place update on local data.").show(); return;//from w ww . j av a 2 s .c o m } TreeNodeDocumentField field = (TreeNodeDocumentField) dv.getSelectedNode().getUserObject(); DBObject doc = dv.getSelectedDocument(); String path = dv.getSelectedDocumentPath(); Object newValue = UMongo.instance.getGlobalStore().editValue(field.getKey(), field.getValue()); if (newValue == null) { // new InfoDialog(null, null, null, "Cannot edit this type of data.").show(); return; } final DBObject query = doc.containsField("_id") ? new BasicDBObject("_id", doc.get("_id")) : doc; DBObject setValue = new BasicDBObject(path, newValue); final DBObject update = new BasicDBObject("$set", setValue); final DBCollection col = dv.getDBCursor().getCollection(); new DbJob() { @Override public Object doRun() { return col.update(query, update, false, false); } @Override public String getNS() { return col.getFullName(); } @Override public String getShortName() { return "Update"; } @Override public DBObject getRoot(Object result) { BasicDBObject obj = new BasicDBObject("query", query); obj.put("update", update); return obj; } @Override public void wrapUp(Object res) { super.wrapUp(res); dv.refresh(null); } }.addJob(); }
From source file:com.edgytech.umongo.DocumentFieldMenu.java
License:Apache License
public void unsetValue(ButtonBase button) { final DocView dv = (DocView) (UMongo.instance.getTabbedResult().getSelectedUnit()); DBObject doc = dv.getSelectedDocument(); String path = dv.getSelectedDocumentPath(); if (dv.getDBCursor() == null) { // local data new InfoDialog(null, null, null, "Cannot do in-place update on local data.").show(); return;/*from w w w. j a v a 2s . com*/ } if (!UMongo.instance.getGlobalStore().confirmDataLossOperation()) return; final DBObject query = doc.containsField("_id") ? new BasicDBObject("_id", doc.get("_id")) : doc; DBObject setValue = new BasicDBObject(path, 1); final DBObject update = new BasicDBObject("$unset", setValue); final DBCollection col = dv.getDBCursor().getCollection(); new DbJob() { @Override public Object doRun() { return col.update(query, update, false, false); } @Override public String getNS() { return col.getFullName(); } @Override public String getShortName() { return "Update"; } @Override public DBObject getRoot(Object result) { BasicDBObject obj = new BasicDBObject("query", query); obj.put("update", update); return obj; } @Override public void wrapUp(Object res) { super.wrapUp(res); dv.refresh(null); } }.addJob(); }
From source file:com.edgytech.umongo.DocumentMenu.java
License:Apache License
public void update(ButtonBase button) { final DocView dv = (DocView) (UMongo.instance.getTabbedResult().getSelectedUnit()); TreeNodeDocument node = (TreeNodeDocument) dv.getSelectedNode().getUserObject(); final DBObject doc = node.getDBObject(); ((DocBuilderField) getBoundUnit(Item.upUpdate)).setDBObject((BasicDBObject) doc); if (!((MenuItem) getBoundUnit(Item.update)).getDialog().show()) { return;// w w w. j a v a 2s. c om } final DBObject query = doc.containsField("_id") ? new BasicDBObject("_id", doc.get("_id")) : doc; final DBObject update = ((DocBuilderField) getBoundUnit(Item.upUpdate)).getDBObject(); if (dv.getDBCursor() == null) { // local data Tree tree = dv.getTree(); tree.removeChild(node); dv.addDocument(update, null); tree.structureComponent(); tree.expandNode(tree.getTreeNode()); return; } final DBCollection col = dv.getDBCursor().getCollection(); new DbJob() { @Override public Object doRun() { return col.update(query, update, false, false); } @Override public String getNS() { return col.getFullName(); } @Override public String getShortName() { return "Update"; } @Override public DBObject getRoot(Object result) { BasicDBObject obj = new BasicDBObject("query", query); obj.put("update", update); return obj; } @Override public void wrapUp(Object res) { super.wrapUp(res); dv.refresh(null); } }.addJob(); }
From source file:com.edgytech.umongo.DocumentMenu.java
License:Apache License
public void remove(ButtonBase button) { final DocView dv = (DocView) (UMongo.instance.getTabbedResult().getSelectedUnit()); TreeNodeDocument node = (TreeNodeDocument) dv.getSelectedNode().getUserObject(); final DBObject doc = node.getDBObject(); if (dv.getDBCursor() == null) { // local data Tree tree = dv.getTree();//from w ww. j a v a2 s. c o m tree.removeChild(node); tree.structureComponent(); tree.expandNode(tree.getTreeNode()); return; } // go by _id if possible final DBObject query = doc.containsField("_id") ? new BasicDBObject("_id", doc.get("_id")) : doc; final DBCollection col = dv.getDBCursor().getCollection(); new DbJob() { @Override public Object doRun() { return col.remove(query); } @Override public String getNS() { return col.getFullName(); } @Override public String getShortName() { return "Remove"; } @Override public void wrapUp(Object res) { super.wrapUp(res); dv.refresh(null); } @Override public DBObject getRoot(Object result) { return query; } }.addJob(); }
From source file:com.edgytech.umongo.ReplSetPanel.java
License:Apache License
public void addTag(ButtonBase button) { final DB config = ((RouterNode) getReplSetNode().getParentNode()).getMongoClient().getDB("config"); final DBCollection col = config.getCollection("shards"); ((DynamicComboBox) getBoundUnit(Item.atTag)).items = TagRangeDialog.getExistingTags(config); FormDialog dia = (FormDialog) button.getDialog(); if (!dia.show()) return;//from w ww. java2 s . c om final String tag = getStringFieldValue(Item.atTag); final DBObject query = new BasicDBObject("_id", getReplSetNode().getShardName()); final DBObject update = new BasicDBObject("$addToSet", new BasicDBObject("tags", tag)); new DbJob() { @Override public Object doRun() { return col.update(query, update); } @Override public String getNS() { return col.getFullName(); } @Override public String getShortName() { return "Add Tag"; } @Override public DBObject getRoot(Object result) { BasicDBObject obj = new BasicDBObject("query", query); obj.put("update", update); return obj; } @Override public void wrapUp(Object res) { super.wrapUp(res); refreshTagList(); } }.addJob(); }
From source file:com.edgytech.umongo.ReplSetPanel.java
License:Apache License
public void removeTag(ButtonBase button) { final DB db = ((RouterNode) getReplSetNode().getParentNode()).getMongoClient().getDB("config"); final DBCollection col = db.getCollection("shards"); final String tag = getComponentStringFieldValue(Item.tagList); if (tag == null) return;//from ww w. j a va2 s.c om final DBObject query = new BasicDBObject("_id", getReplSetNode().getShardName()); final DBObject update = new BasicDBObject("$pull", new BasicDBObject("tags", tag)); new DbJob() { @Override public Object doRun() { return col.update(query, update); } @Override public String getNS() { return col.getFullName(); } @Override public String getShortName() { return "Remove Tag"; } @Override public DBObject getRoot(Object result) { BasicDBObject obj = new BasicDBObject("query", query); obj.put("update", update); return obj; } @Override public void wrapUp(Object res) { super.wrapUp(res); refreshTagList(); } }.addJob(); }
From source file:com.edgytech.umongo.RouterPanel.java
License:Apache License
public void balancer(ButtonBase button) { final MongoClient mongo = getRouterNode().getMongoClient(); final DB config = mongo.getDB("config"); final DBCollection settings = config.getCollection("settings"); FormDialog diag = (FormDialog) ((MenuItem) getBoundUnit(Item.balancer)).getDialog(); diag.xmlLoadCheckpoint();// ww w . j a v a2s . co m final BasicDBObject query = new BasicDBObject("_id", "balancer"); BasicDBObject balDoc = (BasicDBObject) settings.findOne(query); if (balDoc != null) { if (balDoc.containsField("stopped")) setIntFieldValue(Item.balStopped, balDoc.getBoolean("stopped") ? 1 : 2); if (balDoc.containsField("_secondaryThrottle")) setIntFieldValue(Item.balSecThrottle, balDoc.getBoolean("_secondaryThrottle") ? 1 : 2); BasicDBObject window = (BasicDBObject) balDoc.get("activeWindow"); if (window != null) { setStringFieldValue(Item.balStartTime, window.getString("start")); setStringFieldValue(Item.balStopTime, window.getString("stop")); } } if (!diag.show()) return; if (balDoc == null) balDoc = new BasicDBObject("_id", "balancer"); int stopped = getIntFieldValue(Item.balStopped); if (stopped > 0) balDoc.put("stopped", stopped == 1 ? true : false); else balDoc.removeField("stopped"); int throttle = getIntFieldValue(Item.balSecThrottle); if (throttle > 0) balDoc.put("_secondaryThrottle", throttle == 1 ? true : false); else balDoc.removeField("_secondaryThrottle"); if (!getStringFieldValue(Item.balStartTime).trim().isEmpty()) { BasicDBObject aw = new BasicDBObject(); aw.put("start", getStringFieldValue(Item.balStartTime).trim()); aw.put("stop", getStringFieldValue(Item.balStopTime).trim()); balDoc.put("activeWindow", aw); } final BasicDBObject newDoc = balDoc; new DbJob() { @Override public Object doRun() throws IOException { return settings.update(query, newDoc, true, false); } @Override public String getNS() { return settings.getFullName(); } @Override public String getShortName() { return "Balancer"; } @Override public void wrapUp(Object res) { updateComponent(); super.wrapUp(res); } }.addJob(); }
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 va 2 s . 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.ikanow.aleph2.management_db.mongodb.services.MongoDbManagementDbService.java
License:Apache License
/** Utility function handling all the functionality required for getPerLibraryState, getBucket*State * @param clazz/* w w w .j a va 2s.co m*/ * @param name * @param collection * @param type * @return */ @SuppressWarnings("unchecked") protected <T> ICrudService<T> getAppStateStore(final Class<T> clazz, final String name, final Optional<String> collection, final AssetStateDirectoryBean.StateDirectoryType type) { if (!collection.isPresent()) { // return the directory if (!clazz.equals(AssetStateDirectoryBean.class)) { throw new RuntimeException( "Empty collection => return directory, so clazz type must be AssetStateDirectoryBean"); } return ((ICrudService<T>) getStateDirectory(Optional.of(BeanTemplateUtils.build(DataBucketBean.class) .with(DataBucketBean::full_name, name).done().get()), Optional.of(type))).readOnlyVersion(true); //(always read only) } // Otherwise... final String prefix = Lambdas.get(() -> { switch (type) { case analytic_thread: return MongoDbManagementDbService.BUCKET_STATE_ANALYTICS_DB_PREFIX; case enrichment: return MongoDbManagementDbService.BUCKET_STATE_ENRICH_DB_PREFIX; case harvest: return MongoDbManagementDbService.BUCKET_STATE_HARVEST_DB_PREFIX; case library: return MongoDbManagementDbService.LIBRARY_STATE_DB_PREFIX; } return null; }); final String collection_name = BucketUtils.getUniqueSignature(name, collection); final DB db = MongoDbCollectionUtils.findDatabase(_crud_factory.getMongoDb("test").getMongo(), prefix, collection_name); final DBCollection db_collection = db.getCollection(collection_name); final ICrudService<T> state_crud = _crud_factory.getMongoDbCrudService(clazz, String.class, db_collection, Optional.empty(), _auth, _project); // add a delete interceptor to remove the entry from the directory final Map<String, BiFunction<Object, Object[], Object>> interceptors = Lambdas.get(() -> { if (!this._read_only) { // add entry to directory: final ICrudService<AssetStateDirectoryBean> raw_state_crud_dir = getStateDirectory(Optional.empty(), Optional.empty()); raw_state_crud_dir.storeObject( new AssetStateDirectoryBean(name, type, collection.get(), db_collection.getFullName()), true); //^SIDE EFFECT: return ImmutableMap.<String, BiFunction<Object, Object[], Object>>builder() .put("deleteDatastore", (Object ret_val, Object[] args) -> { raw_state_crud_dir.deleteObjectById(db_collection.getFullName()); return ret_val; }).build(); } // (else not needed because can't deleteDatastore anyway) else { return Collections.<String, BiFunction<Object, Object[], Object>>emptyMap(); } }); final ICrudService<T> intercepted_crud = CrudServiceUtils.intercept(clazz, state_crud, Optional.empty(), interceptors, Optional.empty()); return intercepted_crud.readOnlyVersion(_read_only); }