List of usage examples for com.mongodb DBCursor size
public int size()
From source file:mx.edu.cide.justiciacotidiana.v1.mongo.MongoInterface.java
License:Open Source License
/** * Obtiene la lista de los elementos de una coleccin en formato JSON. Si se proporciona una referencia, se hace un filtrado. * @param collectionName Nombre de la coleccin de donde se extraern los elementos. * @param query Objeto de referencia para la bsqueda. * @return Cadena JSON con el resultado. *///from www . j ava 2s. co m public String listItemsAsJSON(String collectionName, BasicDBObject query) { DBCursor cursor = findItems(collectionName, query); cursor.sort(new BasicDBObject(FIELD_CREATED, -1)); StringBuilder ret = new StringBuilder(); long count = 0; if (null != cursor && cursor.hasNext()) { count = cursor.size(); } ret.append("{\"count\":").append(count); if (cursor.hasNext()) { ret.append(", \"items\": ["); try { while (cursor.hasNext()) { BasicDBObject t = (BasicDBObject) cursor.next(); ret.append(Utils.JSON.toJSON(t)); if (cursor.hasNext()) ret.append(","); } } finally { cursor.close(); } ret.append("]"); } ret.append("}"); return ret.toString(); }
From source file:mx.edu.cide.justiciacotidiana.v1.services.Propuestas.java
License:Open Source License
private String getProposalsJSON(String categoryId) { StringBuilder ret = new StringBuilder(); BasicDBObject query = null;//from w ww .j av a 2 s.c o m if (null != categoryId && categoryId.length() > 0) { query = new BasicDBObject(Propuesta.FIELDS.CATEGORYID, categoryId); } long count = 0; DBCursor propuestas = Utils.mongo.findItems(MongoInterface.COLLECTIONS.PROPUESTAS, query); propuestas.sort(new BasicDBObject(Propuesta.FIELDS.CREATED, -1)); if (null != propuestas && propuestas.hasNext()) { count = propuestas.size(); } ret.append("{\"count\":").append(count); if (count > 0) { ret.append(", \"items\": ["); try { while (propuestas.hasNext()) { BasicDBObject propuesta = (BasicDBObject) propuestas.next(); ObjectId pId = (ObjectId) propuesta.get(Propuesta.FIELDS.MONGOID); String key = pId.toString(); BasicDBList comments = getCommentsList(key); BasicDBList likes = getLikes(key); BasicDBList dislikes = getDisLikes(key); BasicDBList refrains = getRefrains(key); BasicDBObject commentsContainer = new BasicDBObject("data", comments); propuesta.put(Propuesta.FIELDS.COMMENTS, commentsContainer); BasicDBObject votes = new BasicDBObject(); //Muy verbose y con muchos niveles, se puede simplificar la estructura BasicDBObject _likes = new BasicDBObject(Voto.PARTICIPANTS, likes); votes.put(Voto.LIKE, _likes); //Muy verbose y con muchos niveles, se puede simplificar la estructura BasicDBObject _dislikes = new BasicDBObject(Voto.PARTICIPANTS, dislikes); votes.put(Voto.DISLIKE, _dislikes); //Muy verbose y con muchos niveles, se puede simplificar la estructura BasicDBObject _refrains = new BasicDBObject(Voto.PARTICIPANTS, refrains); votes.put(Voto.REFRAIN, _refrains); propuesta.append(Propuesta.FIELDS.VOTES, votes); ret.append(Utils.JSON.toJSON(propuesta)); if (propuestas.hasNext()) ret.append(","); } } catch (MongoException ex) { System.out.println("Error al genera lista de comentarios asociada a propuesta"); } ret.append("]"); } ret.append("}"); return ret.toString(); }
From source file:net.onrc.openvirtex.db.DBManager.java
License:Apache License
/** * Read all virtual networks from database and spawn an OVXNetworkManager for each. *//*from www. j a v a 2 s. com*/ @SuppressWarnings("unchecked") private void readOVXNetworks() { PrintStream ps = System.err; System.setErr(null); try { // Get a cursor over all virtual networks DBCollection coll = this.collections.get(DBManager.DB_VNET); DBCursor cursor = coll.find(); log.info("Loading {} virtual networks from database", cursor.size()); while (cursor.hasNext()) { OVXNetworkManager mngr = null; Map<String, Object> vnet = cursor.next().toMap(); try { // Create vnet manager for each virtual network mngr = new OVXNetworkManager(vnet); OVXNetwork.reserveTenantId(mngr.getTenantId()); // Accessing DB_KEY field through a class derived from the abstract OVXSwitch List<Map<String, Object>> switches = (List<Map<String, Object>>) vnet.get(Switch.DB_KEY); List<Map<String, Object>> links = (List<Map<String, Object>>) vnet.get(Link.DB_KEY); List<Map<String, Object>> ports = (List<Map<String, Object>>) vnet.get(Port.DB_KEY); List<Map<String, Object>> routes = (List<Map<String, Object>>) vnet.get(SwitchRoute.DB_KEY); this.readOVXSwitches(switches, mngr); this.readOVXLinks(links, mngr); this.readOVXPorts(ports, mngr); this.readOVXRoutes(routes, mngr); DBManager.log.info("Virtual network {} waiting for {} switches, {} links and {} ports", mngr.getTenantId(), mngr.getSwitchCount(), mngr.getLinkCount(), mngr.getPortCount()); } catch (IndexOutOfBoundException | DuplicateIndexException e) { DBManager.log.error("Failed to load virtual network {}: {}", mngr.getTenantId(), e.getMessage()); } } } catch (Exception e) { log.error("Failed to load virtual networks from db: {}", e.getMessage()); } finally { System.setErr(ps); } }
From source file:net.scran24.datastore.mongodb.MongoDbDataStore.java
License:Apache License
@Override public Option<SecureUserRecord> getUserRecord(String survey_id, String username) throws DataStoreException { DBCollection col = getUsersCollection(survey_id); DBObject q = new BasicDBObject(FIELD_USERNAME, username); DBCursor result = col.find(q); try {//from ww w . j a v a 2 s. co m if (result.size() == 0) return Option.none(); else { BasicDBObject userData = (BasicDBObject) result.next(); return Option.some(parseUserRecord(userData)); } } catch (MongoException e) { throw new DataStoreException(e); } finally { result.close(); } }
From source file:net.tbnr.gearz.arena.ArenaManager.java
License:Open Source License
/** * Loads the arenas into the List./*www .jav a2s . co m*/ */ private void loadArenas() throws GearzException { DBCursor dbCursor = this.collection.find(); if (dbCursor.size() == 0) { throw new GearzException("No arenas found!"); } while (dbCursor.hasNext()) { DBObject next = dbCursor.next(); Arena attempt; try { attempt = ArenaManager.arenaFromDBObject(this.arenaClass, next); } catch (GearzException e) { e.printStackTrace(); continue; } this.arenas.add(attempt); Gearz.getInstance().getLogger() .info("Loaded Arena - " + attempt.getName() + " by - " + attempt.getAuthors()); } }
From source file:nl.vu.psy.relic.persistence.mongo.MongoStore.java
License:Open Source License
@Override public Relic getRelic(String relicId) { BasicDBObject bdo = new BasicDBObject(); bdo.put("relic.identifier", relicId); DBCursor find = relicCollection.find(bdo); if (find.size() == 1) { DBObject dbo = find.next();//from w w w .ja v a 2 s. c om return MongoMapper.DBObjectToRelic(dbo); } else if (find.size() > 1) { throw new RelicRuntimeException( "The relic collection is possibly corrupt, more than one entry was found for the identifier [" + relicId + "]."); } else { return null; } }
From source file:nl.vu.psy.relic.persistence.mongo.MongoStore.java
License:Open Source License
@Override public Relic[] getAllRelics() { Relic[] result = null;//w ww. j av a 2s . c o m BasicDBObject bdo = new BasicDBObject(); bdo.put("relic.identifier", new BasicDBObject("$exists", true)); DBCursor find = relicCollection.find(bdo); result = new Relic[find.size()]; int i = 0; while (find.hasNext()) { DBObject dbo = find.next(); result[i] = MongoMapper.DBObjectToRelic(dbo); i++; } return result; }
From source file:nl.vu.psy.relic.persistence.mongo.MongoStore.java
License:Open Source License
@Override public ResolverDescriptor getResolverDescriptor(String relicId, String environment) { BasicDBObject bdo = new BasicDBObject(); bdo.put("resolver.identifier", relicId); bdo.put("resolver.environment", environment); DBCursor find = relicCollection.find(bdo); if (find.size() == 1) { DBObject dbo = find.next();/*w ww. j av a 2 s.c om*/ return MongoMapper.DBObjectToResolverDescriptor(dbo); } else if (find.size() > 1) { throw new RelicRuntimeException( "The relic collection is possibly corrupt, more than one entry was found for the identifier [" + relicId + "] and environment [" + environment + "]."); } else { return null; } }
From source file:nl.vu.psy.rite.persistence.mongo.MongoCommandStore.java
License:Open Source License
@Override public ClientCommand consumeClientCommand(String clientId) { BasicDBObject query = new BasicDBObject(); query.put("clientcommand.clientid", clientId); DBCursor find = commandCollection.find(query); if (find.size() == 1) { DBObject dbo = find.next();/* w ww. ja v a 2s .c o m*/ ClientCommand co = MongoCommandMapper.DBObjectToClientCommand(dbo); commandCollection.remove(query); return co; } else if (find.size() > 1) { throw new RiteRuntimeException( "The rite command collection is possibly corrupt, more than one entry was found for the identifier [" + clientId + "]."); } else { return null; } }
From source file:nl.vu.psy.rite.persistence.mongo.MongoInfoStore.java
License:Open Source License
@Override public String[] getClientInfo(String clientId) { BasicDBObject query = new BasicDBObject(); query.put("clientinfo.clientid", clientId); DBCursor find = infoCollection.find(query); String[] result = new String[find.size()]; int i = 0;/*from w w w . j a va2 s . co m*/ while (find.hasNext()) { result[i] = MongoInfoMapper.DBObjectToString(find.next()); i++; } return result; }