List of usage examples for com.mongodb DBCursor close
@Override public void close()
From source file:eu.cassandra.csn.mongo.MongoQueries.java
License:Apache License
/** * /*from w w w.j av a 2 s . c om*/ * @param inst_id * @return */ public static Double[] getInstallationResults(String inst_id) { DBCursor cursor = DBConn.getConn().getCollection("inst_results").find(new BasicDBObject("inst_id", inst_id)) .sort(new BasicDBObject("tick", 1)).limit(5 * 1440); Vector<Double> d = new Vector<Double>(); while (cursor.hasNext()) { d.add(Double.valueOf(cursor.next().get("p").toString())); } cursor.close(); Double[] data = d.toArray(new Double[0]); return data; }
From source file:eu.cassandra.csn.mongo.MongoQueries.java
License:Apache License
/** * //from www . j a v a2 s.c o m * @param edgeType * @return */ public static HashMap<String, Vector<InstallationInfo>> getInstallationsKL(int edgeType) { HashMap<String, Vector<InstallationInfo>> installations = new HashMap<String, Vector<InstallationInfo>>(); DBCursor cursor = DBConn.getConn().getCollection("installations").find().limit(limit); Vector<InstallationInfo> instInfos = new Vector<InstallationInfo>(); List<DBObject> insts = cursor.toArray(); double[][] dataForKL = new double[Math.min(insts.size(), limit)][]; cursor.close(); for (int i = 0; i < Math.min(insts.size(), limit); i++) { DBObject obj = insts.get(i); String id = obj.get("_id").toString(); String name = null; if (obj.containsField("name")) name = obj.get("name").toString(); String instType = null; if (obj.containsField("type")) instType = obj.get("type").toString(); InstallationInfo instInfo = new InstallationInfo(id, name, instType, null); instInfos.add(instInfo); DBCursor cursor2 = DBConn.getConn().getCollection("inst_results").find(new BasicDBObject("inst_id", id)) .sort(new BasicDBObject("tick", 1)); List<DBObject> res = cursor2.toArray(); cursor2.close(); double[] data1Inst = new double[res.size()]; for (int j = 0; j < Math.min(insts.size(), limit); j++) { data1Inst[j] = Double.parseDouble(res.get(j).get("p").toString()); } dataForKL[i] = data1Inst; } double[][] results = ConsumptionDetector.estimateKLD(dataForKL); for (int i = 0; i < instInfos.size(); i++) { instInfos.get(i).setKlValuesWithOtherInsts(results[i]); } installations.put("ALL", instInfos); return installations; }
From source file:eu.cassandra.csn.mongo.MongoQueries.java
License:Apache License
/** * /*from ww w . j a v a 2s . co m*/ * @param edgeType * @param inst_id * @return */ public static HashMap<String, Vector<InstallationInfo>> getInstallations(int edgeType, String inst_id) { if (edgeType == KL_SIM || edgeType == KL_DISSIM) { return getInstallationsKL(edgeType); } else { HashMap<String, Vector<InstallationInfo>> installations = new HashMap<String, Vector<InstallationInfo>>(); if (DBConn.getConn() != null) { DBCursor cursor; if (inst_id == null) cursor = DBConn.getConn().getCollection("installations").find(); else cursor = DBConn.getConn().getCollection("installations") .find(new BasicDBObject("_id", new ObjectId(inst_id))); int cc = 0; while (cursor.hasNext()) { cc++; if (cc == 30) break; DBObject obj = cursor.next(); String id = obj.get("_id").toString(); String name = null; if (obj.containsField("name")) name = obj.get("name").toString(); String instType = null; if (obj.containsField("type")) instType = obj.get("type").toString(); InstallationInfo instInfo = new InstallationInfo(id, name, instType, null); String keyType = null; if (edgeType == INSTALLATION_TYPE) { keyType = instType; } else if (edgeType == PERSON_TYPE) { DBObject person = DBConn.getConn().getCollection("persons") .findOne(new BasicDBObject("inst_id", id)); String personType = null; if (person.containsField("type")) personType = person.get("type").toString(); keyType = personType; } else if (edgeType == AVERAGE_CONSUMPTION) { DBCursor res = DBConn.getConn().getCollection("inst_results") .find(new BasicDBObject("inst_id", id)); int counter = 0; double avg = 0; while (res.hasNext()) { counter++; double p = Double.parseDouble(res.next().get("p").toString()); avg += p; } res.close(); avg /= counter; int avgConsumType = (int) (avg / 10); keyType = String.valueOf(avgConsumType); } else if (edgeType == PEAK_CONSUMPTION) { DBCursor res = DBConn.getConn().getCollection("inst_results").find() .sort(new BasicDBObject("p", -1)).limit(1); double totalMax = 0; while (res.hasNext()) { totalMax = Double.parseDouble(res.next().get("p").toString()); } // res = DBConn.getConn().getCollection("inst_results").find().sort(new BasicDBObject( "p",1)).limit(2); // double totalMin = 0; // while(res.hasNext()) { // totalMin = Double.parseDouble(res.next().get("p").toString()); // } // System.out.println(totalMax + "\t" + totalMin); res = DBConn.getConn().getCollection("inst_results").find(new BasicDBObject("inst_id", id)) .sort(new BasicDBObject("p", -1)).limit(1); double max = 0; while (res.hasNext()) { max = Double.parseDouble(res.next().get("p").toString()); } res.close(); System.out.println(totalMax + "\t" + max); int maxConsumType = (int) (max * 5 / (totalMax)); keyType = String.valueOf(maxConsumType); } else { keyType = id; } if (!installations.containsKey(keyType)) { Vector<InstallationInfo> v = new Vector<InstallationInfo>(); v.add(instInfo); installations.put(keyType, v); System.out.println("installations.put " + keyType); } else { Vector<InstallationInfo> v = installations.get(keyType); v.add(instInfo); installations.put(keyType, v); System.out.println("installations.put " + keyType); } } cursor.close(); } return installations; } }
From source file:eu.cassandra.server.mongo.csn.MongoCluster.java
License:Apache License
/** * /*w w w . jav a2s . co m*/ * @param clusterBasedOn * @param graph_id * @param httpHeaders * @return */ private Instances getInstances(String clusterBasedOn, String graph_id) { FastVector attributes = new FastVector(); if (clusterBasedOn.equalsIgnoreCase("hoursP") || clusterBasedOn.equalsIgnoreCase("hoursQ") || clusterBasedOn.equalsIgnoreCase("hoursE")) { for (int i = 0; i < 24; i++) { attributes.addElement(new Attribute("att" + i)); } } else { attributes.addElement(new Attribute("att0")); } Instances instances = new Instances("data", attributes, 0); DBCursor nodes = DBConn.getConn().getCollection(MongoGraphs.COL_CSN_NODES) .find(new BasicDBObject("graph_id", graph_id)); //Get all nodes while (nodes.hasNext()) { double[] values = null; DBObject installationDBObj = nodes.next(); nodeIDs.add(installationDBObj.get("_id").toString()); //If graph was build based on Person or Installation Type do nothing if (clusterBasedOn.equalsIgnoreCase(MongoEdges.PersonType) || clusterBasedOn.equalsIgnoreCase(MongoEdges.InstallationType) || clusterBasedOn.equalsIgnoreCase(MongoEdges.TransformerID) || clusterBasedOn.equalsIgnoreCase(MongoEdges.TopologicalDistance) || clusterBasedOn.equalsIgnoreCase(MongoEdges.Location) || clusterBasedOn.equalsIgnoreCase(MongoEdges.Location) || clusterBasedOn.equalsIgnoreCase(MongoEdges.SocialDistance)) { continue; } else { Object vS = installationDBObj.get(CSNTypes.getCsnTypes(clusterBasedOn)); if (vS != null) { if (clusterBasedOn.equalsIgnoreCase("hoursP") || clusterBasedOn.equalsIgnoreCase("hoursQ") || clusterBasedOn.equalsIgnoreCase("hoursE")) { if (vS instanceof BasicDBList) { BasicDBList v = (BasicDBList) vS; values = new double[v.size()]; for (int i = 0; i < v.size(); i++) { Object d = v.get(i); if (d instanceof Double) { values[i] = (Double) d; } } } } else { Double v = Double.parseDouble(vS.toString()); values = new double[1]; values[0] = v; } } } if (values != null) { Instance instance = new Instance(1, values); instances.add(instance); } } nodes.close(); return instances; }
From source file:eu.cassandra.server.mongo.csn.MongoCluster.java
License:Apache License
public DBObject clusterGraphEgdetweenness(String message, String graph_id, String run_id, String clusterBasedOn, int numberOfEdgesToRemove, String name, String clusterbasedon) { try {//from w ww. j a va2s .c o m UndirectedSparseGraph<String, CEdge> graph = new UndirectedSparseGraph<String, CEdge>(); DBCursor nodes = DBConn.getConn().getCollection(MongoGraphs.COL_CSN_NODES) .find(new BasicDBObject("graph_id", graph_id)); while (nodes.hasNext()) { DBObject installationDBObj = nodes.next(); graph.addVertex(installationDBObj.get("_id").toString()); } nodes.close(); DBCursor edges = DBConn.getConn().getCollection(MongoGraphs.COL_CSN_EDGES) .find(new BasicDBObject("graph_id", graph_id)); while (edges.hasNext()) { DBObject edgeObj = edges.next(); String edgeId = edgeObj.get("_id").toString(); String node1 = edgeObj.get("inst_id1").toString(); String node2 = edgeObj.get("inst_id2").toString(); CEdge CEdge = new CEdge(edgeId, node1, node2); graph.addEdge(CEdge, node1, node2); } edges.close(); EdgeBetweennessClusterer<String, CEdge> clusterer = new EdgeBetweennessClusterer<String, CEdge>( numberOfEdgesToRemove); Set<Set<String>> allClusters = clusterer.transform(graph); Iterator<Set<String>> allClustersIter = allClusters.iterator(); HashMap<Integer, Vector<String>> clusters = new HashMap<Integer, Vector<String>>(); int clusterN = 0; while (allClustersIter.hasNext()) { Set<String> cluster = allClustersIter.next(); Iterator<String> clusterIter = cluster.iterator(); while (clusterIter.hasNext()) { String node = clusterIter.next(); if (clusters.containsKey(clusterN)) { Vector<String> c = clusters.get(clusterN); c.add(node); clusters.put(clusterN, c); } else { Vector<String> c = new Vector<String>(); c.add(node); clusters.put(clusterN, c); } } clusterN++; } List<CEdge> edgesRemoved = clusterer.getEdgesRemoved(); return saveClusters(graph_id, run_id, "graphedgebetweenness", clusters, edgesRemoved, name, clusterbasedon); } catch (Exception e) { e.printStackTrace(); return new JSONtoReturn().createJSONError(message, e); } }
From source file:eu.cassandra.server.mongo.util.MongoDBQueries.java
License:Apache License
/** * /*from w ww . j a v a2s . c o m*/ * @param httpHeaders * @param scn_id * @param obj2Get * @return */ public DBObject getCountsPerType(HttpHeaders httpHeaders, String scn_id, String obj2Get) { HashMap<String, Integer> counterMap = new HashMap<String, Integer>(); BasicDBObject q = new BasicDBObject(); q.put(MongoInstallations.REF_SCENARIO, scn_id); DBCursor cursorDoc = DBConn.getConn(getDbNameFromHTTPHeader(httpHeaders)) .getCollection(MongoInstallations.COL_INSTALLATIONS).find(q); while (cursorDoc.hasNext()) { //Iterate installations DBObject obj = cursorDoc.next(); if (obj2Get.equalsIgnoreCase(MongoInstallations.COL_INSTALLATIONS)) { String type = obj.get("type").toString(); addToMap(type, counterMap); } else { BasicDBObject q2 = new BasicDBObject(); q2.put("inst_id", obj.get("_id").toString()); DBCursor cursorDoc2 = DBConn.getConn(getDbNameFromHTTPHeader(httpHeaders)).getCollection(obj2Get) .find(q2); while (cursorDoc2.hasNext()) { //Iterate Persons or Appliances DBObject obj2 = cursorDoc2.next(); String type = obj2.get("type").toString(); addToMap(type, counterMap); } cursorDoc2.close(); } } cursorDoc.close(); Vector<DBObject> data = new Vector<DBObject>(); for (String type : counterMap.keySet()) { BasicDBObject d = new BasicDBObject(); d.put("type", type); d.put("count", counterMap.get(type)); data.add(d); } return jSON2Rrn.createJSON(data, "Counters per type for " + obj2Get + " of Scenario: " + scn_id); }
From source file:eu.cassandra.server.mongo.util.MongoDBQueries.java
License:Apache License
/** * /*from w ww .j a va2s . c om*/ * @param httpHeaders * @param scn_id * @param obj2Get * @return */ public DBObject getSecondLevelCounts(HttpHeaders httpHeaders, String scn_id, String obj2Get) { int counter = 0; BasicDBObject q = new BasicDBObject(); q.put(MongoInstallations.REF_SCENARIO, scn_id); DBCursor cursorDoc = DBConn.getConn(getDbNameFromHTTPHeader(httpHeaders)) .getCollection(MongoInstallations.COL_INSTALLATIONS).find(q); while (cursorDoc.hasNext()) { //Iterate installations DBObject obj = cursorDoc.next(); BasicDBObject q2 = new BasicDBObject(); q2.put("inst_id", obj.get("_id").toString()); counter += DBConn.getConn(getDbNameFromHTTPHeader(httpHeaders)).getCollection(obj2Get).find(q2).count(); } cursorDoc.close(); BasicDBObject data = new BasicDBObject(); data.put("count", counter); return jSON2Rrn.createJSON(data, "Number of " + obj2Get + " of Scenario: " + scn_id + " retrieved"); }
From source file:eu.cassandra.server.mongo.util.MongoDBQueries.java
License:Apache License
/** * /*from w w w .j av a 2s. c o m*/ * @param httpHeaders * @param collection * @param dbObj1 * @param dbObj2 * @param successMsg * @param sort * @param limit * @param skip * @param count * @return */ public DBObject executeFindQuery(HttpHeaders httpHeaders, String dbName, String collection, DBObject dbObj1, DBObject dbObj2, String successMsg, String sort, int limit, int skip, boolean count) { try { if (dbName == null) dbName = getDbNameFromHTTPHeader(httpHeaders); DBCursor cursorDoc = null; if (count) { BasicDBObject dbObject = new BasicDBObject(); dbObject.put("count", DBConn.getConn(dbName).getCollection(collection).find(dbObj1).count()); return jSON2Rrn.createJSON(dbObject, successMsg); } else { if (dbObj2 == null) { cursorDoc = DBConn.getConn(dbName).getCollection(collection).find(dbObj1); } else { cursorDoc = DBConn.getConn(dbName).getCollection(collection).find(dbObj1, dbObj2); } } if (sort != null) { try { DBObject sortObj = (DBObject) JSON.parse(sort); cursorDoc = cursorDoc.sort(sortObj); } catch (Exception e) { return jSON2Rrn.createJSONError("Error in filtering JSON sorting object: " + sort, e); } } if (skip != 0) cursorDoc = cursorDoc.skip(skip); if (limit != 0) cursorDoc = cursorDoc.limit(limit); Vector<DBObject> recordsVec = new Vector<DBObject>(); while (cursorDoc.hasNext()) { DBObject obj = cursorDoc.next(); if (collection.equalsIgnoreCase(MongoDistributions.COL_DISTRIBUTIONS) && dbObj1.containsField("_id")) { obj = getValues(obj, httpHeaders, dbName, obj.get("_id").toString(), MongoDistributions.COL_DISTRIBUTIONS); } else if (collection.equalsIgnoreCase(MongoConsumptionModels.COL_CONSMODELS) && dbObj1.containsField("_id")) { obj = getValues(obj, httpHeaders, dbName, obj.get("_id").toString(), MongoConsumptionModels.COL_CONSMODELS); } else if (collection.equalsIgnoreCase(MongoPricingPolicy.COL_PRICING)) { PricingPolicy pp = new PricingPolicy(obj); double oneKw24Cost = pp.calcOneKw24(); obj.put("onekw24", oneKw24Cost); } if (obj.containsField("_id")) obj = addChildrenCounts(httpHeaders, collection, obj); recordsVec.add(obj); } cursorDoc.close(); return jSON2Rrn.createJSON(recordsVec, successMsg); } catch (Exception e) { e.printStackTrace(); return jSON2Rrn.createJSONError("MongoQueryError: Cannot execute find query for collection: " + collection + " with qKey=" + dbObj1 + " and qValue=" + dbObj2, e); } }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Returns a view of the objects in the collection that contains the specified range. The objects are sorted by the key in ascending order. * Optionally, the number of objects found in the collection is returned to the caller. Also, the returned fields can be filtered. * @param sortCriteria - objects in the collection are sorted with this criteria * @param collection - collection where the objects are searched * @param start - starting index/*from ww w. ja va 2s . c o m*/ * @param size - maximum number of objects returned * @param query - the expression to be used to query the collection * @param projection - (optional) Specifies the fields to return using projection operators. To return all fields in the matching document, * omit this parameter * @param count - (optional) is updated with the number of objects in the database * @return a view of the objects in the collection that contains the specified range */ public List<BasicDBObject> list(final DBObject sortCriteria, final String collection, final int start, final int size, final @Nullable DBObject query, final @Nullable DBObject projection, final @Nullable MutableLong count) { checkArgument(sortCriteria != null, "Uninitialized sort criteria"); checkArgument(isNotBlank(collection), "Uninitialized or invalid collection"); final List<BasicDBObject> list = newArrayList(); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final DBCollection dbcol = db.getCollection(collection); final DBCursor cursor = dbcol.find(query != null ? query : new BasicDBObject(), projection != null ? projection : new BasicDBObject()); cursor.sort(sortCriteria); cursor.skip(start).limit(size); try { while (cursor.hasNext()) { list.add((BasicDBObject) cursor.next()); } } finally { cursor.close(); } if (count != null) { count.setValue(cursor.count()); } return list; }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
public List<BasicDBObject> geoWithin(final String locationField, final String collection, final Polygon polygon) { checkArgument(isNotBlank(locationField), "Uninitialized or invalid location field"); checkArgument(isNotBlank(collection), "Uninitialized or invalid collection"); checkArgument(polygon != null, "Uninitialized polygon"); final List<BasicDBObject> list = newArrayList(); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final DBCollection dbcol = db.getCollection(collection); try {/* w ww.j a v a 2 s . co m*/ final BasicDBObject query = new BasicDBObject(locationField, new BasicDBObject("$geoWithin", new BasicDBObject("$geometry", (DBObject) parse(JSON_MAPPER.writeValueAsString(polygon))))); LOGGER.trace("geoWithin query: " + JSON.serialize(query)); final DBCursor cursor = dbcol.find(query); try { while (cursor.hasNext()) { list.add((BasicDBObject) cursor.next()); } } finally { cursor.close(); } return list; } catch (JsonProcessingException e) { throw new IllegalStateException("Failed to parse request parameters", e); } }