List of usage examples for com.mongodb DBCursor close
@Override public void close()
From source file:com.uquetignyadminapp.visual.Admin_NewTuples.java
public ArrayList<Map> fillSpecificCollection(DBCollection ObjectsOfCollection) { ArrayList<Map> objectData = new ArrayList<Map>(); DBCursor dbcur = ObjectsOfCollection.find(); int i = 0;/* w w w .jav a 2s. co m*/ while (dbcur.hasNext()) { objectData.add(ObjectsOfCollection.find().toArray().get(i).toMap()); dbcur.next(); i++; } dbcur.close(); return objectData; }
From source file:com.xemsdoom.xeco.core.storage.mongodb.MongoDBStorage.java
License:Open Source License
@Override public Set<Account> loadAccounts() { DBCollection coll = db.getCollection(collection); DBCursor cursor = coll.find(); HashSet<Account> accounts = new HashSet<Account>(); try {/* w ww. j a v a 2 s. c o m*/ // Iterate over all documents while (cursor.hasNext()) { DBObject document = cursor.next(); String accName = (String) document.get("_id"); double amount = (Double) document.get("balance"); boolean frozen = (Boolean) document.get("freezed"); // Cleaner if (main.getConfig().getNode("Xeco.Cleaner.CleanUnusedAccounts").getBoolean() && (amount == main.getConfig().getNode("Xeco.Account.DefaultBalance").getDouble() && !frozen)) { accManager.removeAccount(accName); continue; } accounts.add(new Account(accName, amount, frozen)); } } finally { if (cursor != null) { cursor.close(); } } return accounts; }
From source file:com.ycchung.crawler.Database.java
public boolean isVisited(URL url) { BasicDBObject query = new BasicDBObject("url", url.toString()); DBCursor cursor = coll.find(query); try {/*ww w. j a va 2s. co m*/ while (cursor.hasNext()) { return true; } } finally { cursor.close(); } return false; }
From source file:com.ycchung.crawler.Database.java
public void show() { BasicDBObject query = new BasicDBObject("tag", "html"); DBCursor cursor = coll.find(query); try {/*from w ww .j a v a 2 s .c om*/ while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } }
From source file:com.ycchung.crawler.Database.java
public void clear() { BasicDBObject query = new BasicDBObject("tag", "html"); DBCursor cursor = coll.find(query); try {/*from www . ja v a 2 s . co m*/ while (cursor.hasNext()) { coll.remove(cursor.next()); } } finally { cursor.close(); } }
From source file:com.zjy.mongo.splitter.MongoCollectionSplitter.java
License:Apache License
/** * Contacts the config server and builds a map of each shard's name to its * host(s) by examining config.shards./*w w w . j av a 2 s . co m*/ * @return a Map of shard name onto shard hostnames */ protected Map<String, String> getShardsMap() { DBCursor cur = null; HashMap<String, String> shardsMap = new HashMap<String, String>(); DB configDB = null; try { configDB = getConfigDB(); DBCollection shardsCollection = configDB.getCollection("shards"); cur = shardsCollection.find(); while (cur.hasNext()) { final BasicDBObject row = (BasicDBObject) cur.next(); String host = row.getString("host"); // for replica sets host will look like: "setname/localhost:20003,localhost:20004" int slashIndex = host.indexOf('/'); if (slashIndex > 0) { host = host.substring(slashIndex + 1); } shardsMap.put((String) row.get("_id"), host); } } finally { if (cur != null) { cur.close(); } } return shardsMap; }
From source file:com.zjy.mongo.splitter.StandaloneMongoSplitter.java
License:Apache License
@Override public List<InputSplit> calculateSplits() throws SplitFailedException { final DBObject splitKey = MongoConfigUtil.getInputSplitKey(getConfiguration()); final DBObject splitKeyMax = MongoConfigUtil.getMaxSplitKey(getConfiguration()); final DBObject splitKeyMin = MongoConfigUtil.getMinSplitKey(getConfiguration()); final int splitSize = MongoConfigUtil.getSplitSize(getConfiguration()); final MongoClientURI inputURI; DBCollection inputCollection = null; final ArrayList<InputSplit> returnVal; try {/*from w w w . jav a 2s . com*/ inputURI = MongoConfigUtil.getInputURI(getConfiguration()); MongoClientURI authURI = MongoConfigUtil.getAuthURI(getConfiguration()); if (authURI != null) { inputCollection = MongoConfigUtil.getCollectionWithAuth(inputURI, authURI); } else { inputCollection = MongoConfigUtil.getCollection(inputURI); } returnVal = new ArrayList<InputSplit>(); final String ns = inputCollection.getFullName(); if (LOG.isDebugEnabled()) { LOG.debug(String.format("Running splitVector on namespace: %s.%s; hosts: %s", inputURI.getDatabase(), inputURI.getCollection(), inputURI.getHosts())); } final DBObject cmd = BasicDBObjectBuilder.start("splitVector", ns).add("keyPattern", splitKey) .add("min", splitKeyMin).add("max", splitKeyMax) // force:True is misbehaving it seems .add("force", false).add("maxChunkSize", splitSize).get(); CommandResult data; boolean ok = true; try { data = inputCollection.getDB().getSisterDB(inputURI.getDatabase()).command(cmd, ReadPreference.primary()); } catch (final MongoException e) { // 2.0 servers throw exceptions rather than info in a CommandResult data = null; LOG.info(e.getMessage(), e); if (e.getMessage().contains("unrecognized command: splitVector")) { ok = false; } else { throw e; } } if (data != null) { if (data.containsField("$err")) { throw new SplitFailedException("Error calculating splits: " + data); } else if (!data.get("ok").equals(1.0)) { ok = false; } } if (!ok) { final CommandResult stats = inputCollection.getStats(); if (stats.containsField("primary")) { final DBCursor shards = inputCollection.getDB().getSisterDB("config").getCollection("shards") .find(new BasicDBObject("_id", stats.getString("primary"))); try { if (shards.hasNext()) { final DBObject shard = shards.next(); final String host = ((String) shard.get("host")).replace(shard.get("_id") + "/", ""); final MongoClientURI shardHost; if (authURI != null) { shardHost = new MongoClientURIBuilder(authURI).host(host).build(); } else { shardHost = new MongoClientURIBuilder(inputURI).host(host).build(); } MongoClient shardClient = null; try { shardClient = new MongoClient(shardHost); data = shardClient.getDB(shardHost.getDatabase()).command(cmd, ReadPreference.primary()); } catch (final Exception e) { LOG.error(e.getMessage(), e); } finally { if (shardClient != null) { shardClient.close(); } } } } finally { shards.close(); } } if (data != null && !data.get("ok").equals(1.0)) { throw new SplitFailedException("Unable to calculate input splits: " + data.get("errmsg")); } } // Comes in a format where "min" and "max" are implicit // and each entry is just a boundary key; not ranged final BasicDBList splitData = (BasicDBList) data.get("splitKeys"); if (splitData.size() == 0) { LOG.warn( "WARNING: No Input Splits were calculated by the split code. Proceeding with a *single* split. Data may be too" + " small, try lowering 'mongo.input.split_size' if this is undesirable."); } BasicDBObject lastKey = null; // Lower boundary of the first min split // If splitKeyMin was given, use it as first boundary. if (!splitKeyMin.toMap().isEmpty()) { lastKey = new BasicDBObject(splitKeyMin.toMap()); } for (final Object aSplitData : splitData) { final BasicDBObject currentKey = (BasicDBObject) aSplitData; returnVal.add(createSplitFromBounds(lastKey, currentKey)); lastKey = currentKey; } BasicDBObject maxKey = null; // If splitKeyMax was given, use it as last boundary. if (!splitKeyMax.toMap().isEmpty()) { maxKey = new BasicDBObject(splitKeyMax.toMap()); } // Last max split final MongoInputSplit lastSplit = createSplitFromBounds(lastKey, maxKey); returnVal.add(lastSplit); } finally { if (inputCollection != null) { MongoConfigUtil.close(inputCollection.getDB().getMongo()); } } return returnVal; }
From source file:ConecteMongoDB.Consultas.java
public ArrayList<Hero> buscaHero(String hero) { ArrayList<Hero> resultados = new ArrayList<>(); DBCollection colecao = MongoConnection.getInstance().getDB().getCollection("heroesdata"); BasicDBObject query = new BasicDBObject("Title", hero); DBCursor cursor; cursor = colecao.find(query);//from ww w . j av a 2 s. c o m try { while (cursor.hasNext()) { DBObject obj = cursor.next(); String nome = (String) obj.get("Title"); String lore = (String) obj.get("Lore"); String sider = (String) obj.get("Side"); String url = (String) obj.get("Url"); Hero novoHero = new Hero(nome, lore, sider, url); resultados.add(novoHero); } } finally { cursor.close(); } return resultados; }
From source file:ConecteMongoDB.Consultas.java
public ArrayList<Hero> buscaSide(String side) { ArrayList<Hero> resultados = new ArrayList<>(); DBCollection colecao = MongoConnection.getInstance().getDB().getCollection("heroesdata"); BasicDBObject query = new BasicDBObject("Side", side); DBCursor cursor; cursor = colecao.find(query).sort(new BasicDBObject("Initial" + "." + "Strength", -1)); try {/* w w w . ja v a 2s. c o m*/ while (cursor.hasNext()) { DBObject obj = cursor.next(); String nome = (String) obj.get("Title"); String lore = (String) obj.get("Lore"); String sider = (String) obj.get("Side"); String url = (String) obj.get("Url"); Hero novoHero = new Hero(nome, lore, sider, url); resultados.add(novoHero); } } finally { cursor.close(); } //System.out.println(colecao.count(query)); //System.out.println(resultados.size()); return resultados; }
From source file:ConecteMongoDB.Consultas.java
public ArrayList<Hero> buscaFunction(String function) { ArrayList<Hero> resultados = new ArrayList<>(); DBCollection colecao = MongoConnection.getInstance().getDB().getCollection("heroesdata"); DBCursor cursor; BasicDBObject query = new BasicDBObject("SuggestedRoleLevels" + "." + function, new BasicDBObject("$gt", 1)); cursor = colecao.find(query).sort(new BasicDBObject("SuggestedRoleLevels" + "." + function, -1)); try {//from w w w . ja v a2 s . c o m while (cursor.hasNext()) { DBObject obj = cursor.next(); String nome = (String) obj.get("Title"); String lore = (String) obj.get("Lore"); String sider = (String) obj.get("Side"); String url = (String) obj.get("Url"); Hero novoHero = new Hero(nome, lore, sider, url); resultados.add(novoHero); } } finally { cursor.close(); } return resultados; }