Java Code Examples for com.mongodb.DBCursor#hasNext()

The following examples show how to use com.mongodb.DBCursor#hasNext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
public List<Long> getAllCollectionUUIDs(DBCollection collection) {

    List<Long> ids = new ArrayList<Long>();

    BasicDBObject query = new BasicDBObject();
    BasicDBObject keys = new BasicDBObject();
    keys.put("_id", 1); // 0 means exclude, rest are included
    DBCursor cur = collection.find(query, keys);

    while (cur.hasNext()) {
      DBObject o = cur.next();
      long uuid = (Integer)o.get("_id"); // checked: db type IS int
      ids.add(uuid);
    }
    cur.close();

    return ids;
  }
 
Example 2
Source File: BaseImporter.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
protected final void extractBasicNode(String collectionName) {
    int count = 0;
    long startTime = System.currentTimeMillis();
    logger.log(Level.INFO, "Building basic node for type: " + collectionName);
    DBCursor cursor = mongo.getCollection(collectionName).find();
    long recordCount = mongo.getCollection(collectionName).count();
    while (cursor.hasNext()) {
        DBObject item = cursor.next();
        Vertex v = graph.addVertex(null);
        logger.log(Level.FINE, "Adding vertex for {0}#{1} \t {2}",
                new String[] { collectionName, (String) item.get("_id"), v.getId().toString() });

        v.setProperty("mongoid", (String) item.get("_id"));
        count++;
        if (count % 200 == 0) {
            logger.log(Level.FINE, "Importing {0} @ {1}", new String[] { collectionName, "" + count });
        }
    }
    logger.log(Level.INFO, "\t RPS: {0}", (recordCount / (System.currentTimeMillis() - startTime)) * 1000);
}
 
Example 3
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
public List<Seq> getSeqWithRxnRef(Long rxnId) {
  List<Seq> seqs = new ArrayList<>();
  BasicDBObject query = new BasicDBObject();
  query.put("rxn_refs", rxnId);

  DBCursor cur = this.dbSeq.find(query, new BasicDBObject());
  try {
    while (cur.hasNext()) {
      DBObject o = cur.next();
      seqs.add(convertDBObjectToSeq(o));
    }
  } finally {
    if (cur != null) {
      cur.close();
    }
  }

  return seqs;
}
 
Example 4
Source File: MongoNativeExtractor.java    From deep-spark with Apache License 2.0 6 votes vote down vote up
/**
 * Gets shards.
 *
 * @param collection the collection
 * @return the shards
 */
private Map<String, String[]> getShards(DBCollection collection) {
    DB config = collection.getDB().getSisterDB("config");
    DBCollection configShards = config.getCollection("shards");

    DBCursor cursorShards = configShards.find();

    Map<String, String[]> map = new HashMap<>();
    while (cursorShards.hasNext()) {
        DBObject currentShard = cursorShards.next();
        String currentHost = (String) currentShard.get("host");
        int slashIndex = currentHost.indexOf("/");
        if (slashIndex > 0) {
            map.put((String) currentShard.get(MONGO_DEFAULT_ID),
                    currentHost.substring(slashIndex + 1).split(","));
        }
    }
    return map;
}
 
Example 5
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
private List<Seq> keywordInSequence(String in_field, String keyword) {
  List<Seq> seqs = new ArrayList<Seq>();
  BasicDBObject query = new BasicDBObject();
  query.put(in_field, keyword);

  BasicDBObject keys = new BasicDBObject();

  DBCursor cur = this.dbSeq.find(query, keys);
  while (cur.hasNext()) {
    DBObject o = cur.next();
    seqs.add( convertDBObjectToSeq(o) );
  }
  cur.close();

  return seqs;
}
 
Example 6
Source File: BingSearchRanker.java    From act with GNU General Public License v3.0 5 votes vote down vote up
/**
 * This function writes the Bing Search ranks for a chunk of inchis in a TSV file, append only option.
 * @param inchis (Set<String>) set of InChI string representations
 * @param outputPath (String) path indicating the output file
 * @param appendOutput (Boolean) whether to append the results to the output file
 * @throws IOException
 */
private void writeBingSearchRanksAsTSVForInchiChunk(Set<String> inchis, String outputPath, Boolean appendOutput)
    throws IOException {

  // Define headers
  List<String> bingRankerHeaderFields = new ArrayList<>();
  addChemicalHeaders(bingRankerHeaderFields);

  // Open TSV writer
  try(TSVWriter<String, String> tsvWriter = new TSVWriter<>(bingRankerHeaderFields)) {
    tsvWriter.open(new File(outputPath), appendOutput);

    int counter = 0;
    DBCursor cursor = mongoDB.fetchNamesAndUsageForInchis(inchis);

    // Iterate through the target chemicals
    while (cursor.hasNext()) {
      counter++;
      BasicDBObject o = (BasicDBObject) cursor.next();
      Map<String, String> row = new HashMap<>();
      updateRowWithChemicalInformation(o, row);
      tsvWriter.append(row);
      tsvWriter.flush();
    }
    LOGGER.info("Wrote %d Bing Search results to %s", counter, outputPath);
  }
}
 
Example 7
Source File: MongoDbGridFSIO.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public List<? extends BoundedSource<ObjectId>> split(
    long desiredBundleSizeBytes, PipelineOptions options) throws Exception {
  Mongo mongo = spec.connectionConfiguration().setupMongo();
  try {
    GridFS gridfs = spec.connectionConfiguration().setupGridFS(mongo);
    DBCursor cursor = createCursor(gridfs);
    long size = 0;
    List<BoundedGridFSSource> list = new ArrayList<>();
    List<ObjectId> objects = new ArrayList<>();
    while (cursor.hasNext()) {
      GridFSDBFile file = (GridFSDBFile) cursor.next();
      long len = file.getLength();
      if ((size + len) > desiredBundleSizeBytes && !objects.isEmpty()) {
        list.add(new BoundedGridFSSource(spec, objects));
        size = 0;
        objects = new ArrayList<>();
      }
      objects.add((ObjectId) file.getId());
      size += len;
    }
    if (!objects.isEmpty() || list.isEmpty()) {
      list.add(new BoundedGridFSSource(spec, objects));
    }
    return list;
  } finally {
    mongo.close();
  }
}
 
Example 8
Source File: MongoDB.java    From act with GNU General Public License v3.0 5 votes vote down vote up
public List<Long> getRxnsWithSubstrate(String enzyme, Long org, List<Long> substrates) {
  BasicDBObject query = new BasicDBObject();
  query.put("organisms.id", org);
  BasicDBObject enzymeQuery = new BasicDBObject();
  enzymeQuery.put("ecnum", enzyme);
  query.put("$ne", enzymeQuery);
  for (Long substrate: substrates) {
    BasicDBList queryList = new BasicDBList();
    DBObject querySubstrate = new BasicDBObject();
    querySubstrate.put("enz_summary.substrates.pubchem", substrate);
    DBObject queryProduct = new BasicDBObject();
    queryProduct.put("enz_summary.products.pubchem", substrate);
    queryList.add(querySubstrate);
    queryList.add(queryProduct);
    query.put("$or", queryList);
  }

  DBCursor cur = this.dbReactions.find(query);
  List<Long> reactions = new ArrayList<Long>();
  while (cur.hasNext()) {
    DBObject o = cur.next();
    long id = (Integer) o.get("_id"); // checked: db type IS int
    reactions.add(id);
  }
  cur.close();
  return reactions;
}
 
Example 9
Source File: StudentSchoolEdgeImporter.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
/**
 * Imports student school associations (as edges) into the graph structure.
 */
@Override
public void importCollection() {
    logger.log(Level.INFO, "Importing collection into graph: " + STUDENT_SCHOOL_ASSOCIATION);
    
    DBCursor cursor = mongo.getCollection(STUDENT_SCHOOL_ASSOCIATION).find();
    cursor.batchSize(BATCH_SIZE);
    while (cursor.hasNext()) {
        DBObject association = cursor.next();
        
        @SuppressWarnings("unchecked")
        Map<String, Object> body = (Map<String, Object>) association.get("body");
        String studentId = (String) body.get("studentId");
        String schoolId = (String) body.get("schoolId");
        for (Vertex student : graph.getVertices("mongoid", studentId)) {
            for (Vertex school : graph.getVertices("mongoid", schoolId)) {
                Edge e = graph.addEdge(null, school, student, "studentSchoolAssociation");
                e.setProperty("mongoid", association.get("_id"));
                
                if (body.containsKey("exitWithdrawDate")) {
                    e.setProperty("exitWithdrawDate", body.get("exitWithdrawDate"));
                }
                
                logger.log(Level.INFO, "Adding an edge between school: " + schoolId + " --> student: " + studentId);
            }
        }            
    }
}
 
Example 10
Source File: MongoNativeExtractor.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates shard chunks.
 *
 * @param collection the collection
 * @return the deep partition [ ]
 */
private DeepPartition[] calculateShardChunks(DBCollection collection) {

    DBCursor chuncks = getChunks(collection);

    Map<String, String[]> shards = getShards(collection);

    MongoPartition[] deepPartitions = new MongoPartition[chuncks.count()];
    int i = 0;
    boolean keyAssigned = false;
    String key = null;
    while (chuncks.hasNext()) {

        DBObject dbObject = chuncks.next();
        if (!keyAssigned) {
            Set<String> keySet = ((DBObject) dbObject.get("min")).keySet();
            for (String s : keySet) {
                key = s;
                keyAssigned = true;
            }
        }
        deepPartitions[i] = new MongoPartition(mongoDeepJobConfig.getRddId(), i,
                new DeepTokenRange(shards.get(dbObject.get
                        ("shard")),
                        ((DBObject) dbObject.get
                                ("min")).get(key),
                        ((DBObject) dbObject.get("max")).get(key)), key);
        i++;
    }
    List<MongoPartition> mongoPartitions = Arrays.asList(deepPartitions);

    Collections.shuffle(mongoPartitions);
    return mongoPartitions.toArray(new MongoPartition[mongoPartitions.size()]);
}
 
Example 11
Source File: EdOrgImporter.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
public void importEducationOrganizations(String type) {
    logger.log(Level.INFO, "Importing education organizations into graph: " + type);
    
    DBObject query = new BasicDBObject();
    query.put("type", type);
    
    DBCursor cursor = mongo.getCollection(EDUCATION_ORGANIZATION).find(query);
    cursor.batchSize(BATCH_SIZE);
    while (cursor.hasNext()) {
        DBObject edOrg = cursor.next();
        String currentEdOrgId = (String) edOrg.get("_id");
        
        Vertex v = graph.addVertex(null);
        logger.log(Level.INFO, "Adding vertex for {0}#{1} \t {2}", new String[] { type,
                currentEdOrgId, v.getId().toString() });
        
        v.setProperty("mongoid", currentEdOrgId);
        
        @SuppressWarnings("unchecked")
        Map<String, Object> body = (Map<String, Object>) edOrg.get("body");
        if (body.containsKey("parentEducationAgencyReference")) {
            String parentId = (String) body.get("parentEducationAgencyReference");
            for (Vertex child : graph.getVertices("mongoid", currentEdOrgId)) {
                for (Vertex parent : graph.getVertices("mongoid", parentId)) {
                    graph.addEdge(null, parent, child, EDUCATION_ORGANIZATION_ASSOCIATION);
                    logger.log(Level.INFO, "Adding an edge between ed org: " + parentId + " --> " + currentEdOrgId);
                }
            }
        }
    }
}
 
Example 12
Source File: AddNeuroscienceFlag.java    From bluima with Apache License 2.0 5 votes vote down vote up
public static void main__(String[] args) throws Exception {

        File ns_pmIds = new File(
                System.getProperty("user.home")
                        + "/Dropbox/dev_shared/tmp2/rrrrrabbit_fetchlist_meshNeuron_SUS.txt");
        Set<Integer> ns_pmIds_set = new HashSet<Integer>();
        for (String line : new LineReader(new FileInputStream(ns_pmIds))) {
            ns_pmIds_set.add(Integer.parseInt(line));
        }
        System.out.println("done reading list");

        MongoConnection mongo = new MongoConnection(MONGO_FT_CONNECTION);
        // retrieve only _id
        BasicDBObject keys = new BasicDBObject();
        keys.put("pmid", 1);
        DBCursor it = mongo.coll.find(new BasicDBObject(), keys)
                .batchSize(1000);
        int i = 0, flagged = 0;
        while (it.hasNext()) {
            DBObject dbObject = (DBObject) it.next();
            int pmId = Integer.parseInt(dbObject.get("_id").toString());

            if (ns_pmIds_set.contains(pmId)) {

                MongoUtils.addFlag(pmId, "ns", mongo);

                flagged++;
            }
            if (i++ % 1000 == 0)
                System.err.println("updated " + i + "th, on " + pmId
                        + ", flagged:" + flagged);
        }
        System.err.println("END updated " + i + ", flagged:" + flagged);
    }
 
Example 13
Source File: Stats.java    From XBDD with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/product/{product}/{major}.{minor}.{servicePack}/{build}")
@Produces(MediaType.APPLICATION_JSON)
public Response getProductHistory(@BeanParam final Coordinates coordinates) {
	List<String> buildList = new ArrayList<>();
	final DBCollection collection = this.mongoLegacyDb.getCollection("summary");
	final DBObject productQuery = coordinates.getQueryObject(Field.PRODUCT, Field.MAJOR, Field.MINOR, Field.SERVICEPACK);
	final DBCursor results = collection.find(productQuery);

	while (results.hasNext()) {
		buildList = (List<String>) results.next().get("builds");
	}

	final DBCollection featureCollection = this.mongoLegacyDb.getCollection("features");

	final List<BasicDBObject> buildDBObjectList = new ArrayList<>();
	for (final String build : buildList) {
		coordinates.setBuild(build);
		final DBObject buildQuery = coordinates.getQueryObject();

		final BasicDBObject buildObj = new BasicDBObject();
		buildObj.put("passed", getNumberOfState(featureCollection, buildQuery, "passed"));
		buildObj.put("failed", getNumberOfState(featureCollection, buildQuery, "failed"));
		buildObj.put("skipped", getNumberOfState(featureCollection, buildQuery, "skipped"));
		buildObj.put("undefined", getNumberOfState(featureCollection, buildQuery, "undefined"));
		buildObj.put("name", build);
		buildDBObjectList.add(buildObj);
	}

	return Response.ok(SerializerUtil.serialise(buildDBObjectList)).build();
}
 
Example 14
Source File: RootController.java    From hcp-cloud-foundry-tutorials with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody Result onRootAccess() {

    DBCollection collection = mongoTemplate.getCollection("test");
    long count = collection.getCount();
    log.info("Object count in 'test' collection before insert: " + count + "<br/> Inserting one object.<br/>");

    BasicDBObject dBObject = new BasicDBObject();
    dBObject.put("hello", "world");
    collection.insert(dBObject);
    count = collection.count();
    log.info("Object count in test collection after insert:" + count);

    Result result = new Result();
    List<DBObject> dbObjects = new ArrayList<DBObject>();
    DBCursor cursor = collection.find();
    while (cursor.hasNext()) {
        com.mongodb.DBObject obj = cursor.next();
        final String value = (String) obj.get("hello");
        DBObject object = new DBObject();
        object.setKey("hello");
        object.setValue(value);
        dbObjects.add(object);
    }
    result.setDbObjects(dbObjects);
    result.setStatus(
            "Successfully accessed Mongodb service. Retrieving the data object inserted in test collection.");
    collection.drop();
    return result;
}
 
Example 15
Source File: MongoDBKeyValueStore.java    From boon with Apache License 2.0 5 votes vote down vote up
@Override
public KeyValueIterable<K, V> search(K startKey) {


    final DBCursor cursor = collection.find(new BasicDBObject(map("key", new BasicDBObject("$gt", startKey))));
    return new KeyValueIterable<K, V>() {
        @Override
        public void close() {
            cursor.close();
        }

        @Override
        public Iterator<Entry<K, V>> iterator() {
            return new Iterator<Entry<K, V>>() {
                @Override
                public boolean hasNext() {
                    return cursor.hasNext();
                }

                @Override
                public Entry<K, V> next() {
                    //TODO left off here
                    return null;
                }

                @Override
                public void remove() {

                }
            };
        }
    };
}
 
Example 16
Source File: UserResource.java    From sample-acmegifts with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get all user profiles.
 *
 * @return All user profiles (excluding private fields like password).
 */
@GET
@Produces("application/json")
public Response getAllUsers() {
  // Validate the JWT. The JWT must be in the 'users' group.
  try {
    validateJWT(new HashSet<String>(Arrays.asList("users")));
  } catch (JWTException jwte) {
    return Response.status(Status.UNAUTHORIZED)
        .type(MediaType.TEXT_PLAIN)
        .entity(jwte.getMessage())
        .build();
  }

  // Get all the users from the database, and add them to an array.
  DB database = mongo.getMongoDB();
  DBCollection dbCollection = database.getCollection(User.DB_COLLECTION_NAME);
  DBCursor cursor = dbCollection.find();
  JsonArrayBuilder userArray = Json.createArrayBuilder();
  while (cursor.hasNext()) {
    // Exclude all private information from the list.
    userArray.add((new User(cursor.next()).getPublicJsonObject()));
  }

  // Return the user list to the caller.
  JsonObjectBuilder responseBuilder = Json.createObjectBuilder().add("users", userArray.build());
  return Response.ok(responseBuilder.build(), MediaType.APPLICATION_JSON).build();
}
 
Example 17
Source File: DeviceManagerController.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@ResponseBody
@RequestMapping(value="/getMgmtCmdResult.do")
public Map<String, Object> getMgmtCmdResult(HttpServletRequest request) throws Exception {
	Map<String, Object> response = new HashMap<String, Object>();
	HttpSession session = request.getSession(false);
	
	if(session != null){
		//페이지 권한 확인
		GroupAuthorization requestAuth = (GroupAuthorization) session.getAttribute("requestAuth");
		if(!requestAuth.getAuthorizationDBRead().equals("1")){
			response.put("result", 1);
			response.put("errorCode", -1);
			response.put("content", "authMessage: onem2m 페이지 접근 권한이 없습니다.");
		} else {
			String collection = HeritProperties.getProperty("Globals.MongoDB.Collection");
			DBCollection col = db.getCollection(collection);
			
			String rn = request.getParameter("rn");
			
			if (rn == null || rn.equals("")) {
				response.put("result", 1);
				response.put("errorCode", -1);
				response.put("content", "parameters[rn] is missing");
			}
			
			BasicDBObject query = new BasicDBObject();
			query.put("ty", 8);
			query.put("ext", rn);
			
			DBCursor cursor = col.find(query);
			
			List<DBObject> dbObjList = new ArrayList<DBObject>();
			try {
			    while (cursor.hasNext()) {
			
			        dbObjList.add(cursor.next());
			        ////System.out.println("############# dbObjList.toString() = " + dbObjList.toString());
			    }
			} finally {
			    cursor.close();
			}
			
			response.put("result", 0);
			response.put("errorCode", 0);
			response.put("content", dbObjList.toString());
			
		}
	} else {
		response.put("result", 1);
		response.put("errorCode", -1);
		response.put("content", "session is null");
	}
	
	return response;
}
 
Example 18
Source File: MongoMapSet.java    From jelectrum with MIT License 4 votes vote down vote up
public Set<Sha256Hash> getSet(String key, int max_results)
{
    Set<Sha256Hash> ret = new HashSet<Sha256Hash>();

    DBCursor c = collection.find(new BasicDBObject(KEY, key));

    int count =0;

    while(c.hasNext())
    {
      ret.add(new Sha256Hash( MongoEntry.getValueString(c.next())));
      count++;
      if (count > max_results) throw new DBTooManyResultsException();
    }

    return ret;


}
 
Example 19
Source File: MongoExample.java    From tutorials with MIT License 4 votes vote down vote up
public static void main(String[] args) {

        MongoClient mongoClient = new MongoClient("localhost", 27017);

        DB database = mongoClient.getDB("myMongoDb");

        // print existing databases
        mongoClient.getDatabaseNames().forEach(System.out::println);

        database.createCollection("customers", null);

        // print all collections in customers database
        database.getCollectionNames().forEach(System.out::println);

        // create data
        DBCollection collection = database.getCollection("customers");
        BasicDBObject document = new BasicDBObject();
        document.put("name", "Shubham");
        document.put("company", "Baeldung");
        collection.insert(document);

        // update data
        BasicDBObject query = new BasicDBObject();
        query.put("name", "Shubham");
        BasicDBObject newDocument = new BasicDBObject();
        newDocument.put("name", "John");
        BasicDBObject updateObject = new BasicDBObject();
        updateObject.put("$set", newDocument);
        collection.update(query, updateObject);

        // read data
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("name", "John");
        DBCursor cursor = collection.find(searchQuery);
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

        // delete data
        BasicDBObject deleteQuery = new BasicDBObject();
        deleteQuery.put("name", "John");
        collection.remove(deleteQuery);
    }
 
Example 20
Source File: Projects.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Representation doRepresent() {
	String view = (String) getRequest().getAttributes().get("view");
	
	DBCollection col = mongo.getDB("scava").getCollection("projects");
	
	BasicDBObject query = new BasicDBObject();
	switch (view) {
		case "error":
			query.put("executionInformation.inErrorState", true);
			break;
		case "analysed":
			query.put("executionInformation.analysed", true);
			break;
		default:
			break;
	}
	
	DBCursor cursor = col.find(query);

	ArrayNode res = mapper.createArrayNode();
	
	while (cursor.hasNext()) {
		DBObject project = cursor.next();
		ObjectNode node = mapper.createObjectNode();
		
		node.put("id", project.get("shortName").toString());
		node.put("name", project.get("name").toString());
		
		BasicDBObject ei = (BasicDBObject)project.get("executionInformation");
		
		node.put("lastExecuted", (String)ei.get("lastExecuted"));
		node.put("analysed", (Boolean)ei.get("analysed"));
		node.put("inErrorState", (Boolean)ei.get("inErrorState"));
		node.put("monitor", (Boolean)ei.get("monitor"));
		
		res.add(node);
	}
	
	return Util.createJsonRepresentation(res);
}