public static JSONObject findReportsRaw() { JSONArray reports = new JSONArray(); FindIterable<Document> result = findReports(); result.forEach(new Block<Document>() { @Override public void apply(final Document document) { JSONObject jsonReport = new JSONObject(); jsonReport.put("id", document.getObjectId("_id").toString()); jsonReport.put("comment", document.getString("comment")); jsonReport.put("city_item_id", document.getString("city_item_id")); jsonReport.put("priority", document.getInteger("priority")); jsonReport.put("resolved", document.getBoolean("resolved")); jsonReport.put("report_date", document.getLong("report_date")); reports.put(jsonReport); } }); JSONObject jsonResults = new JSONObject(); jsonResults.put("reports", reports); return jsonResults; }
public HashMap<UUID, ArrayList<LoadedRegion>> loadRegionData() { HashMap<UUID, ArrayList<LoadedRegion>> regions = new HashMap<>(); MongoCollection<Document> collection = getDatabase().getCollection("chunks"); collection.find().forEach((Block<Document>) document -> { UUID id = UUID.fromString(document.getString("_id")); UUID owner = UUID.fromString(document.getString("owner")); UUID world = UUID.fromString(document.getString("world")); LoadedRegion.ChunkType type = LoadedRegion.ChunkType.valueOf(document.getString("type")); int fromX = document.getInteger("fromX"); int fromZ = document.getInteger("fromZ"); int toX = document.getInteger("toX"); int toZ = document.getInteger("toZ"); Date date = (Date) document.getDate("created"); Region region = new Region(new Coordinate(fromX, fromZ), new Coordinate(toX, toZ), world); if (regions.containsKey(id)) regions.get(id).add(new LoadedRegion(owner, id, region, date, type)); else regions.put(id, Lists.newArrayList(new LoadedRegion(owner, id, region, date, type))); }); return regions; }
/** * Returns all reports for view * * @return List */ public static List<ReportViewObject> findReportsForView() { List<ReportViewObject> reports = new ArrayList<>(); FindIterable<Document> result = findReports(); result.forEach(new Block<Document>() { @Override public void apply(final Document document) { Report.Builder b = new Report.Builder(); DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); String reportDate = df.format(new Date(document.getLong("report_date"))); reports.add(new ReportViewObject( document.getObjectId("_id").toString(), Integer.toString(document.getInteger("priority")), document.getString("city_item_id"), reportDate, document.getBoolean("resolved") ? "YES" : "NO")); } }); return reports; }
public int returnValueToURL(String URL) { Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document.toJson()); } }; MongoCollection<Document> collection = db.getCollection("ratings"); collection.aggregate( Arrays.asList( Aggregates.group("URL", Accumulators.avg("rating", 1)))) .forEach(printBlock); System.out.println(printBlock.toString()); return 0; }
@Override public void run() { System.out.println("Reader started"); MongoCollection<Document> collection = DBCacheManager.INSTANCE.getCachedMongoPool(mongoDbName, mongoUserName) .getDatabase(mongoDbName).getCollection(collectionName); FindIterable<Document> it = collection.find().batchSize(batchSize); it.forEach(new Block<Document>() { @Override public void apply(Document t) { System.out.println("Document read " + t); try { dataBuffer.put(t); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); }
@Override public List<SurveyActivity> findByCategory(String categoryName) { ArrayList<SurveyActivity> activities = new ArrayList<>(); FindIterable<Document> result = collection.find(eq("category.name", categoryName)); result.forEach((Block<Document>) document -> activities.add(SurveyActivity.deserialize(document.toJson()))); return activities; }
public List<DeviceEntity> getAllDevicesByCategory(String category) { List<DeviceEntity> result = new ArrayList<>(); Document filter; if (category.isEmpty()) { filter = new Document(); } else { filter = new Document("category", category); } collection.find(filter) .forEach((Block<? super Document>) document -> { Short devId = document.getInteger("dev_id").shortValue(); String name = document.getString("name"); String cat = document.getString("category"); String type = document.getString("type"); boolean isTrusted = document.getBoolean("isTrusted"); result.add(new DeviceEntity(devId, name, cat, type, isTrusted)); }); return result; }
public List<DeviceEntity> getDeviceEntitiesByName(String name) { Document query = new Document("name", name); List<DeviceEntity> result = new ArrayList<>(); collection.find(query).forEach((Block<? super Document>) document -> { Short devId = document.getInteger("dev_id").shortValue(); String category = document.getString("category"); String type = document.getString("type"); boolean isTrusted = document.getBoolean("isTrusted"); result.add(new DeviceEntity(devId, name, category, type, isTrusted)); }); return result; }
public List<DeviceEntity> getDeviceEntitiesByType(String type) { Document query = new Document("type", type); List<DeviceEntity> result = new ArrayList<>(); collection.find(query).forEach((Block<? super Document>) document -> { Short devId = document.getInteger("dev_id").shortValue(); String category = document.getString("category"); String name = document.getString("name"); boolean isTrusted = document.getBoolean("isTrusted"); result.add(new DeviceEntity(devId, name, category, type, isTrusted)); }); return result; }
public List<Integer> addPostings(int topic) { List<Integer> postings = new ArrayList<Integer>(); BasicDBObject query = new BasicDBObject(); query.put("topic_id", topic); BasicDBObject sortby = new BasicDBObject("post_number", 1); log.info(" Querying postings in topic " + topic); progress_counter = 0; List<Document> lbd = new ArrayList<>(); mongoClient.getDatabase(mongoDbName).getCollection("postings").find(query).sort(sortby).forEach((Block<Document>) d -> { lbd.add(d); }); for (int clump=0; clump<= lbd.size(); clump += 300) { log.info(" adding posts " + clump + " through " + Math.min(clump+300,lbd.size())); postings.addAll(helper.addPostingGroup(topic, lbd.subList(clump, Math.min(clump+300,lbd.size())),this)); } log.info("done with adding posts to this topic"); return postings; }
private void migrateGeneralScriptFunctions(GlobalContext context) { logger.info("Searching for functions of type 'step.plugins.functions.types.GeneralScriptFunction' to be migrated..."); com.mongodb.client.MongoCollection<Document> functions = MongoDBAccessorHelper.getMongoCollection_(context.getMongoClient(), "functions"); AtomicInteger i = new AtomicInteger(); Document filterCallFunction = new Document("type", "step.plugins.functions.types.GeneralScriptFunction"); functions.find(filterCallFunction).forEach(new Block<Document>() { @Override public void apply(Document t) { t.replace("type", "step.plugins.java.GeneralScriptFunction"); Document filter = new Document("_id", t.get("_id")); functions.replaceOne(filter, t); i.incrementAndGet(); } }); logger.info("Migrated "+i.get()+" functions of type 'step.plugins.functions.types.GeneralScriptFunction'"); }
private void renameArtefactType(GlobalContext context, String classFrom, String classTo) { logger.info("Searching for artefacts of type '"+classFrom+"' to be migrated..."); com.mongodb.client.MongoCollection<Document> artefacts = MongoDBAccessorHelper.getMongoCollection_(context.getMongoClient(), "artefacts"); AtomicInteger i = new AtomicInteger(); Document filterCallFunction = new Document("_class", classFrom); artefacts.find(filterCallFunction).forEach(new Block<Document>() { @Override public void apply(Document t) { try { i.incrementAndGet(); t.put("_class", classTo); Document filter = new Document("_id", t.get("_id")); artefacts.replaceOne(filter, t); logger.info("Migrating "+classFrom+ " to "+t.toJson()); } catch(ClassCastException e) { // ignore } } }); logger.info("Migrated "+i.get()+" artefacts of type '"+classFrom+"'"); }
public boolean checkAndCleanup(String userId, String fileName) { List<Document> l = query(new Query().equals(USERID, userId).addSortCriteria(CREATION_TIME, false)); if (l.size() >= maxPersistedPagesPerUser) { Document oldest = l.iterator().next(); if ((System.currentTimeMillis() - oldest.get(CREATION_TIME).getTime()) < minimumDelay) { //there have been to many page persistences for this user in a short time, so don't persist return false; } else { //clean up oldest to free space for new persisted page gridFS.find(Filters.eq("filename", oldest.get(FILENAME))).forEach(new Block<GridFSFile>() { @Override public void apply(GridFSFile file) { gridFS.delete(file.getObjectId()); } }); oldest.delete(); } } //create new entry Document newOne = createNew(); newOne.set(USERID, userId); newOne.set(FILENAME, fileName); newOne.set(CREATION_TIME, new Date()); newOne.writeToDatabase(false); return true; }
/** * Fires an event on all objects in the cache, which fulfill the query * * @param event * @param query */ public void fireEvent(final DataEvent event, BaseQuery query) { //query only ids FindIterable cursor = collection.find(query.getQuery()); cursor.forEach(new Block() { @Override public void apply(Object t) { org.bson.Document dbObject = (org.bson.Document) t; String id = "" + dbObject.get("_id"); synchronized (map) { Document o = map.get(id); if (o != null) { o.fireChangedEvent(event); } } } }); }
public List<Deployment> getDeployments(String clusterId) { List<Deployment> deployments = new ArrayList<>(); Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) { Deployment deployment = new Deployment(); deployment.setUuid(document.getString("uuid")); deployment.setStatus(document.getString("status")); deployment.setHostCount(document.getInteger("hostCount")); deployment.setCompletedCount(document.getInteger("completedCount")); TarFile tarFile = new TarFile(); tarFile.setUrl(document.getString("sourceTar")); deployment.setSourceTar(tarFile); deployment.setTimestamp(document.getDate("timestamp")); deployments.add(deployment); } }; connection.getCollection(DEPLOYMENTS_COLLECTION_NAME).find(eq("clusterId", clusterId)).forEach(printBlock); return deployments; }
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { MongoClient client = getMongoClient( _session, argStruct ); try{ cfArrayData arr = cfArrayData.createArray(1); client.listDatabaseNames().forEach( new Block<String>(){ @Override public void apply( String dbName ) { try { arr.addElement( new cfStringData( dbName ) ); } catch ( cfmRunTimeException e ) {} } }); return arr; } catch (MongoException me){ throwException(_session, me.getMessage()); return null; } }
public static void main(String[] args) { MongoClient mongoClient = new MongoClient(); MongoDatabase database = mongoClient.getDatabase("people-registry-example"); MongoCollection<Person> collection = BsoneeCodecRegistry.to(database.getCollection("people", Person.class)); try{ Person oldJohnny = new Person("John", "Doe", new Date(), GrowthStatus.ALIVE); collection.insertOne(oldJohnny); System.out.println("We have " + collection.count() + " person(s) registered"); collection.find().forEach(new Block<Person>() { public void apply(Person t) { System.out.println("Registered " + t); } }); }catch(Exception e) { System.out.println("Example Failed"); e.printStackTrace(); }finally{ collection.drop(); database.drop(); mongoClient.close(); } }
public static void main(String[] args) { // new client MongoClient client = new MongoClient(); // get the data base MongoDatabase db = client.getDatabase("other"); // here create a new collection //db.createCollection("filestore"); // insert data into the collection db.getCollection("filestore").insertOne( new Document() .append("name", "smile") .append("age", 20)); // retrive the data FindIterable<Document> iterable = db.getCollection("filestore").find(); // show data iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document); } }); }
@SuppressWarnings("unchecked") @RequestMapping(NectarServerApplication.ROOT_PATH + "/query/queryUsers") public ResponseEntity queryUsers(@RequestParam(value = "token") String jwtRaw, HttpServletRequest request) { ManagementSessionToken token = ManagementSessionToken.fromJSON(Util.getJWTPayload(jwtRaw)); if(token == null) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid TOKENTYPE."); if(!SessionController.getInstance().checkManagementToken(token)) { return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Token expired/not valid."); } JSONObject returnJSON = new JSONObject(); MongoCollection<Document> clients = NectarServerApplication.getDb().getCollection("clients"); MongoCollection<Document> users = NectarServerApplication.getDb().getCollection("users"); users.find().forEach((Block<Document>) document -> { String username = document.getString("username"); boolean admin = document.getBoolean("admin", false); JSONObject userJSON = new JSONObject(); userJSON.put("admin", admin); Document clientDoc = clients.find(Filters.eq("loggedInUser", username)).first(); if(clientDoc == null) { userJSON.put("signedIn", false); } else { userJSON.put("signedIn", true); } returnJSON.put(username, userJSON); }); return ResponseEntity.ok(returnJSON.toJSONString()); }
public ArrayList<UserData> loadUserData() { ArrayList<UserData> userDatas = new ArrayList<UserData>(); MongoCollection<Document> collection = getDatabase().getCollection("user"); collection.find().forEach((Block<Document>) document -> { UUID player = UUID.fromString(document.getString("user")); Date seen = (Date) document.getDate("seen"); Date joined = (Date) document.getDate("joined"); UserData userData = new UserData(player, joined, seen); userDatas.add(userData); }); return userDatas; }
public CommonMetaData[] getAll() throws NotConnectedException { CommonMetaData[] commonList = new CommonMetaData[(int) this.getCount()]; Block<Document> listBlock = new Block<Document>() { private int i = 0; @Override public void apply(final Document document) { commonList[i] = gson.fromJson(document.toJson(), CommonMetaData.class); i++; } }; getCollection().find().forEach(listBlock); return commonList; }
public List<ObjectId> checkCancelledEvents(final Set<ObjectId> activeEventList) { final List<ObjectId> cancelledEvents = new ArrayList<ObjectId>(); syncEvents .find(Filters.and(Filters.in(SyncAttrs.ID, activeEventList), Filters.eq(SyncAttrs.STATUS, SyncStatus.CANCELLED)), Document.class) .projection(Projections.include(SyncAttrs.ID)).forEach(new Block<Document>() { @Override public void apply(Document arg0) { cancelledEvents.add(arg0.getObjectId(SyncAttrs.ID)); } }); return cancelledEvents; }
private void processEventLogFlow() throws InterruptedException { FindIterable<O2MSyncEventLog> it = getCursor(); retryCount = 0; it.forEach(new Block<O2MSyncEventLog>() { @Override public void apply(O2MSyncEventLog eventLog) { try { if (marker.isFailed()) { releaseReources(); return; } logCollection.findOneAndUpdate(Filters.eq(SyncAttrs.ID, eventLog.getLogId()), Updates.set(O2MSyncEventLogCodec.STATUS, O2MSyncEventLogCodec.RUNNING)); logger.info("Processing filter : "+eventLog.getEventFilters()); if (eventLog.getOperation().equals(SyncConstants.DELETE)) { processDeletedDoc(eventLog); }else{ processMongoObject(map.getMapObject(), true, null, map.getMapObject().getCollectionName(), eventLog); } logCollection.findOneAndUpdate(Filters.eq(SyncAttrs.ID, eventLog.getLogId()), Updates.set(O2MSyncEventLogCodec.STATUS, O2MSyncEventLogCodec.COMPLETE)); logger.info("Processed filter : "+eventLog.getEventFilters()); } catch (SyncError e) { logger.error("Error in O2M replication", e); Mailer.sendmail(eventId, null, e, Mailer.FAILURE); } } }); }
@Override public List<DataSource> find() { ArrayList<DataSource> dataSources = new ArrayList<DataSource>(); FindIterable<Document> result = collection.find(); result.forEach((Block<Document>) document -> { dataSources.add(DataSource.deserialize(document.toJson())); }); return dataSources; }
@Override public ArrayList<FieldCenter> find() { ArrayList<FieldCenter> fieldCenters = new ArrayList<>(); list().forEach((Block<Document>) document -> { fieldCenters.add(new Gson().fromJson(document.toJson(), FieldCenter.class)); }); return fieldCenters; }
public List<SurveyForm> find() { ArrayList<SurveyForm> surveys = new ArrayList<SurveyForm>(); list().forEach((Block<Document>) document -> { surveys.add(SurveyForm.deserialize(document.toJson())); }); return surveys; }
public List<SurveyForm> findByAcronym(String acronym) { ArrayList<SurveyForm> surveys = new ArrayList<>(); collection.find(eq("surveyTemplate.identity.acronym", acronym)).forEach((Block<Document>) document -> { surveys.add(SurveyForm.deserialize(document.toJson())); }); return surveys; }
public List<SurveyForm> findByCustomId(Set<String> ids) { ArrayList<SurveyForm> surveys = new ArrayList<>(); collection .find(or(in("surveyTemplate.itemContainer.customID", ids), in("surveyTemplate.itemContainer.options.customOptionID", ids))) .forEach((Block<Document>) document -> { surveys.add(SurveyForm.deserialize(document.toJson())); }); return surveys; }
/** * Return activities considering that they were not discarded * @param rn * @return */ @Override public List<SurveyActivity> find(long rn) { ArrayList<SurveyActivity> activities = new ArrayList<SurveyActivity>(); FindIterable<Document> result = collection.find(and(eq("participantData.recruitmentNumber", rn), eq("isDiscarded", false))); result.forEach((Block<Document>) document -> { activities.add(SurveyActivity.deserialize(document.toJson())); }); return activities; }
@Override public List<ActivityCategory> findNonDeleted() { ArrayList<ActivityCategory> categories = new ArrayList<>(); BasicDBObject query = new BasicDBObject(); query.put("objectType", "ActivityCategory"); query.put("disabled", false); FindIterable<Document> documents = collection.find(query); documents.forEach((Block<? super Document>) document -> categories.add(ActivityCategory.deserialize(document.toJson()))); return categories; }
@Override public List<ExamLot> find() { ArrayList<ExamLot> ExamLots = new ArrayList<>(); FindIterable<Document> result = collection.find(); result.forEach((Block<Document>) document -> ExamLots.add(ExamLot.deserialize(document.toJson()))); return ExamLots; }
@Override public List<TransportationLot> find() { ArrayList<TransportationLot> transportationLots = new ArrayList<>(); FindIterable<Document> result = collection.find(); result.forEach((Block<Document>) document -> transportationLots.add(TransportationLot.deserialize(document.toJson()))); return transportationLots; }
@Override public ArrayList<Aliquot> getFullAliquotsList() { ArrayList<Aliquot> fullList = new ArrayList<Aliquot>(); FindIterable<Document> list = collection.find(); list.forEach((Block<Document>) document ->{ ParticipantLaboratory laboratory = ParticipantLaboratory.deserialize(document.toJson()); fullList.addAll(laboratory.getAliquotsList()); }); return fullList; }
@Override public ArrayList<ParticipantLaboratory> getAllParticipantLaboratory() { FindIterable<Document> result = super.list(); ArrayList<ParticipantLaboratory> participantList = new ArrayList<ParticipantLaboratory>(); result.forEach((Block<Document>) document -> { participantList.add(ParticipantLaboratory.deserialize(document.toJson())); }); return participantList; }
public List<String> getSensorTypes() { List<String> result = new ArrayList<>(); collection.distinct("type", new Document("category", "sensor"), String.class) .forEach((Block<? super String>) result::add); return result; }
public List<String> getActorTypes() { List<String> result = new ArrayList<>(); collection.distinct("type", new Document("category", "actor"), String.class) .forEach((Block<? super String>) result::add); return result; }
public List<FlowEntity> getAllFlows() { List<FlowEntity> result = new ArrayList<>(); collection.find().forEach((Block<? super Document>) document -> { FlowEntity flowEntity = new FlowEntity( document.getInteger("flow_id"), document.getString("name"), document.getInteger("order_num") ); List<Document> conditions = (List<Document>) document.get("conditions"); conditions.forEach(conditionDocument -> { flowEntity.addCondition(new ConditionEntity( conditionDocument.getInteger("dev_id").shortValue(), conditionDocument.getString("dev_type"), conditionDocument.getString("type"), conditionDocument.getString("parameter") )); }); List<Document> actions = (List<Document>) document.get("actions"); actions.forEach(actionDocument -> { flowEntity.addAction(new ActionEntity( actionDocument.getInteger("dev_id").shortValue(), actionDocument.getString("dev_type"), actionDocument.getString("type"), actionDocument.getString("parameter") )); }); result.add(flowEntity); }); return result; }
private void deleteLog(Date olderThan) { MongoCollection<Log> logCollection = mongoService.getMongoClient().getDatabase(database).getCollection(collection, Log.class); Bson filter = Filters.lt("timeStamp", olderThan); logCollection.find(filter).forEach((Block<? super Log>) log -> { log.getLogFiles().forEach(logFile -> { GridFSBucket gridFSBucket = GridFSBuckets.create(mongoService.getMongoClient().getDatabase(database), logFile.getBucket()); gridFSBucket.delete(logFile.getFileObjectId()); }); }); DeleteResult deleteResult = logCollection.deleteMany(filter); }
@Override public void forEach(Block<? super TResult> block) { OperationMetric metric = startMetric("forEach " + block.getClass().getName(), null); findIterable.forEach(block); stopMetric(metric, 0); }
@Override public void forEach(Block<? super U> block) { OperationMetric metric = startMetric("forEach " + block.getClass().getName(), null); mongoIterable.forEach(block); stopMetric(metric, 0); }