com.mongodb.DBCursor Java Examples

The following examples show how to use com.mongodb.DBCursor. 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: InternalValidator.java    From scava with Eclipse Public License 2.0 8 votes vote down vote up
private Map<String, Float> readDistanceScores(String startingObject, Set<String> others) {
	Map<String, Float> result = new HashMap<String, Float>();
	Query query = new org.springframework.data.mongodb.core.query.Query();
	query.addCriteria(Criteria.where("type.name").is(_SIMILARITY_METHOD).orOperator(
			Criteria.where("fromArtifact.$id").is(new ObjectId(startingObject)),
			Criteria.where("toArtifact.$id").is(new ObjectId(startingObject))));
	DBCollection dbCollection = mongoTemplate.getCollection("relation");
	DBCursor cursor = dbCollection.find(query.getQueryObject());
	List<DBObject> list = cursor.toArray();
	for (DBObject dbObject : list) {
		String toArtifact = ((DBRef) dbObject.get("toArtifact")).getId().toString();
		String fromArtifact = ((DBRef) dbObject.get("fromArtifact")).getId().toString();
		double value = ((double) dbObject.get("value"));
		if (toArtifact.equals(startingObject)) {
			if (others.contains(fromArtifact))
				result.put(fromArtifact, (float) (1 - value));
		} else if (others.contains(toArtifact))
			result.put(toArtifact, (float) (1 - value));
	}
	return result;
}
 
Example #2
Source File: KmeansClulsterCalulator.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
private Map<String, Float> readDistanceScores(String object) {
	Map<String, Float> result = new HashMap<String, Float>();
	Query query = new org.springframework.data.mongodb.core.query.Query();
	query.addCriteria(Criteria.where("type.name").is(sm.getSimilarityName())
			.orOperator(Criteria.where("fromArtifact.$id").is(new ObjectId(object)), 
					    Criteria.where("toArtifact.$id").is(new ObjectId(object))));
	DBCollection dbCollection = mongoTemplate.getCollection("relation");
    DBCursor cursor = dbCollection.find(query.getQueryObject());
    List<DBObject> list = cursor.toArray();
    for (DBObject dbObject : list) {
		String toArtifact = ((DBRef)dbObject.get("toArtifact")).getId().toString();
		String fromArtifact = ((DBRef)dbObject.get("fromArtifact")).getId().toString();
		double value = ((double)dbObject.get("value"));
		if (toArtifact.equals(object))  
			result.put(fromArtifact, (float) (1 - value));
		else 
			result.put(toArtifact, (float) (1 - value));
	}
	return result;
}
 
Example #3
Source File: StudentProgramEdgeImporter.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@Override
public void importCollection() {
    DBCursor cursor = mongo.getCollection("studentProgramAssociation").find();
    while (cursor.hasNext()) {
        DBObject spa = cursor.next();
        Map<String, Object> body = (Map) spa.get("body");
        for (Vertex student : graph.getVertices("mongoid", body.get("studentId"))) {
            for (Vertex program : graph.getVertices("mongoid", body.get("programId"))) {
                Edge e = graph.addEdge(null, program, student, "studentProgram");
                e.setProperty("mongoid", spa.get("_id"));
                // This may not be safe.
                e.setProperty("endDate", body.get("endDate"));
            }
            
        }

    }

}
 
Example #4
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 #5
Source File: MongoDBUtil.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Find all DBObjects from database using this query. 
 * Note 1: it may do a full table scan if the query contains no index keys.
 * Note 2: it will fetch all content into JVM memory rather than use lazy loading.
 * So make sure you call it only at small collection such as configuration data.
 * 
 * @param query
 * @param databaseName
 * @param namespace
 * @param collection
 * @param fields
 * @return
 */
public static final List<DBObject> queryAllFromMongo(DBObject query, 
		String databaseName, String namespace, String collection, 
		DBObject fields, DBObject sortFields) {
	
	DBCollection coll = getDBCollection(databaseName, namespace, collection);
	//int count = (int)coll.count(query);
	ArrayList<DBObject> objList = new ArrayList<DBObject>();
	DBCursor list = coll.find(query, fields);
	if ( sortFields != null ) {
		list = list.sort(sortFields);
	}
	while ( list.hasNext() ) {
		objList.add(list.next());
	}
	return objList;
}
 
Example #6
Source File: FanoutOnWriteSizedBuckets.java    From socialite with Apache License 2.0 6 votes vote down vote up
@Override
public List<Content> getFeedFor(final User user, final int limit) {

    List<Content> result = new ArrayList<Content>(limit);
    DBCursor cursor = buckets.find(
            findBy(BUCKET_OWNER_KEY, user.getUserId()), getFields(BUCKET_CONTENT_KEY)).
            sort(sortByDecending(BUCKET_ID_KEY)).batchSize(config.bucket_read_batch_size);
    
    try{
        while(cursor.hasNext() && result.size() < limit){
            DBObject currentBucket = cursor.next();
            @SuppressWarnings("unchecked")
            List<DBObject> contentList = (List<DBObject>)currentBucket.get(BUCKET_CONTENT_KEY);
            int bucketSize = contentList.size();
            for(int i = bucketSize - 1; i >= 0; --i){
                result.add(new Content(contentList.get(i)));
                if(result.size() >= limit)
                    break;
            } 
        }
    } finally {
        cursor.close();
    }
    
    return result;
}
 
Example #7
Source File: TagView.java    From XBDD with Apache License 2.0 6 votes vote down vote up
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/featureTagIndex/{product}/{major}.{minor}.{servicePack}/{build}")
public Response getFeatureTagIndexForReport(@BeanParam final Coordinates coordinates,
		@QueryParam("searchText") final String searchText, @QueryParam("viewPassed") final Integer viewPassed,
		@QueryParam("viewFailed") final Integer viewFailed,
		@QueryParam("viewUndefined") final Integer viewUndefined, @QueryParam("viewSkipped") final Integer viewSkipped,
		@QueryParam("start") final String start) {

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

	final BasicDBObject query = QueryBuilder.getInstance().buildFilterQuery(coordinates, searchText, viewPassed,
			viewFailed, viewUndefined, viewSkipped, start);

	query.append("$and", QueryBuilder.getInstance().buildHasTagsQuery());

	final DBCursor results = featuresCollection.find(query,
			new BasicDBObject("tags", 1).append("elements.tags", 1).append("name", 1).append("calculatedStatus", 1)
					.append("id", 1).append("elements.steps", 1).append("elements.name", 1).append("elements.id", 1));

	return Response.ok(SerializerUtil.serialise(getTagList(results))).build();
}
 
Example #8
Source File: StudentSectionEdgeImporter.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@Override
public void importCollection() {
    DBCursor cursor = mongo.getCollection("studentSectionAssociation").find();
    cursor.batchSize(BATCH_SIZE);
    while (cursor.hasNext()) {
        DBObject spa = cursor.next();
        @SuppressWarnings("unchecked")
        Map<String, Object> body = (Map<String, Object>) spa.get("body");
        String studentId = (String) body.get("studentId");
        String sectionId = (String) body.get("sectionId");
        for (Vertex student : graph.getVertices("mongoid", studentId)) {
            for (Vertex section : graph.getVertices("mongoid", sectionId)) {
                Edge e = graph.addEdge(null, section, student, "studentSection");
                e.setProperty("mongoid", spa.get("_id"));

                if (body.containsKey("endDate")) {
                    e.setProperty("endDate", body.get("endDate"));
                }
                
                logger.log(Level.INFO, "Adding an edge between section: " + sectionId + " --> student: " + studentId);
            }
        }            
    }        
}
 
Example #9
Source File: PrivateStorageRepository.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
private void toPrivateStorageList(String collectionName,
                                  List<PrivateStorage> privatesStorage,
                                  DBCursor cursor,
                                  int pageNumber,
                                  int pageSize) throws JsonProcessingException {
    try {
        cursor.skip(pageNumber);
        cursor.limit(pageSize);
        while (cursor.hasNext()) {
            cursor.next();

            String content = jsonParsingService.toJsonString(cursor.curr().toMap());

            privatesStorage.add(PrivateStorage.builder()
                    .collectionName(collectionName)
                    .collectionContent(content)
                    .build());
        }
    } finally {
        cursor.close();
    }
}
 
Example #10
Source File: StaffCohortEdgeImporter.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@Override
public void importCollection() {
    DBCursor cursor = mongo.getCollection("staffCohortAssociation").find();
    while (cursor.hasNext()) {
        DBObject spa = cursor.next();
        Map<String, Object> body = (Map) spa.get("body");
        List<String> staffIds = (List) body.get("staffId");
        for (String staffId : staffIds) {
            for (Vertex staff : graph.getVertices("mongoid", staffId)) {
                List<String> cohortIds = (List) body.get("cohortId");
                for (String cohortId : cohortIds)
                    for (Vertex program : graph.getVertices("mongoid", cohortId)) {
                        Edge e = graph.addEdge(null, program, staff, "staffCohort");
                        e.setProperty("mongoid", spa.get("_id"));
                        // This may not be safe.
                        e.setProperty("endDate", body.containsKey("endDate") ? body.get("endDate") : "");
                        e.setProperty("studentRecordAccess", body.get("studentRecordAccess"));
                    }

            }
        }
        
    }
    
}
 
Example #11
Source File: MongoUtil.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Find all DBObjects from database using this query. 
 * Note 1: it may do a full table scan if the query contains no index keys.
 * Note 2: it will fetch all content into JVM memory rather than use lazy loading.
 * So make sure you call it only at small collection such as configuration data.
 * 
 * @param query
 * @param databaseName
 * @param namespace
 * @param collection
 * @param fields
 * @return
 */
public static final List<DBObject> queryAllFromMongo(DBObject query, 
		String databaseName, String namespace, String collection, 
		DBObject fields, DBObject sortFields) {
	
	DBCollection coll = getDBCollection(databaseName, namespace, collection);
	int count = (int)coll.count(query);
	ArrayList<DBObject> objList = new ArrayList<DBObject>();
	DBCursor list = coll.find(query, fields);
	if ( sortFields != null ) {
		list = list.sort(sortFields);
	}
	while ( list.hasNext() ) {
		objList.add(list.next());
	}
	return objList;
}
 
Example #12
Source File: DatabaseSpec.java    From bdt with Apache License 2.0 6 votes vote down vote up
/**
 * Execute a query on (mongo) database
 *
 * @param query         path to query
 * @param type          type of data in query (string or json)
 * @param collection    collection in database
 * @param modifications modifications to perform in query
 */
@When("^I execute a query '(.+?)' of type '(json|string)' in mongo '(.+?)' database using collection '(.+?)' with:$")
public void sendQueryOfType(String query, String type, String database, String collection, DataTable modifications) throws Exception {
    try {
        commonspec.setResultsType("mongo");
        String retrievedData = commonspec.retrieveData(query, type);
        String modifiedData = commonspec.modifyData(retrievedData, type, modifications);
        commonspec.getMongoDBClient().connectToMongoDBDataBase(database);
        DBCollection dbCollection = commonspec.getMongoDBClient().getMongoDBCollection(collection);
        DBObject dbObject = (DBObject) JSON.parse(modifiedData);
        DBCursor cursor = dbCollection.find(dbObject);
        commonspec.setMongoResults(cursor);
    } catch (Exception e) {
        commonspec.getExceptions().add(e);
    }
}
 
Example #13
Source File: RawMetricResource.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
private ArrayNode getHistoricDocuments(DBCollection dbCollection, DBObject query) {
	ObjectMapper mapper = new ObjectMapper();
	ArrayNode nodeArray = mapper.createArrayNode();		
	DBCursor cursor = dbCollection.find(query);
	while(cursor.hasNext()) {
		DBObject obj = cursor.next();
		JsonNode json;
		try {
			json = mapper.readTree(obj.toString());
			nodeArray.add(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return nodeArray;
}
 
Example #14
Source File: RascalMetricsTest.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
private JSONArray getJSONArrayFromDB(String db, String col) {
	try {
		JSONArray result = new JSONArray(); 
		DBCollection collection = mongo.getDB(db).getCollectionFromString(col);
		DBCursor cursor = collection.find();

		while(cursor.hasNext()) {
			DBObject obj = cursor.next();
			JSONObject json = new JSONObject(JSON.serialize(obj));
			result.put(json);
		}
		return result;
	}
	catch(Exception e) {
		System.out.println("We got an error when creating a JSONArray: " + e);
		return null;
	}
}
 
Example #15
Source File: AdvancedDao.java    From bugu-mongo with Apache License 2.0 6 votes vote down vote up
private synchronized Iterable<DBObject> mapReduce(String map, String reduce, String outputTarget, MapReduceCommand.OutputType outputType, String orderBy, int pageNum, int pageSize, DBObject query) {
    MapReduceOutput output = getCollection().mapReduce(map, reduce, outputTarget, outputType, query);
    DBCollection c = output.getOutputCollection();
    DBCursor cursor;
    if(orderBy != null){
        cursor = c.find().sort(SortUtil.getSort(orderBy)).skip((pageNum-1)*pageSize).limit(pageSize);
    }else{
        cursor = c.find().skip((pageNum-1)*pageSize).limit(pageSize);
    }
    List<DBObject> list = new ArrayList<>();
    for(Iterator<DBObject> it = cursor.iterator(); it.hasNext(); ){
        list.add(it.next());
    }
    cursor.close();
    return list;
}
 
Example #16
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
public List<Long> getRxnsWith(Long reactant, Long product) {

    BasicDBObject query = new BasicDBObject();
    query.put("enz_summary.products.pubchem", product);
    query.put("enz_summary.substrates.pubchem", reactant);
    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 #17
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 #18
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
public List<Seq> getSeqWithSARConstraints() {
  List<Seq> seqs = new ArrayList<Seq>();
  BasicDBObject query = new BasicDBObject();
  query.put("sar_constraints", new BasicDBObject("$exists", true));

  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 #19
Source File: Tailer.java    From zerowing with MIT License 6 votes vote down vote up
private DBCursor createCursor() {
  DBCollection oplog = _mongo.getDB("local").getCollection("oplog.rs");
  BSONTimestamp startingTimestamp = getStartingTimestamp();

  DBCursor cursor;
  if (startingTimestamp == null) {
    log.info("Tailing the oplog from the beginning...");
    cursor = oplog.find();
  } else {
    log.info("Tailing the oplog from " + startingTimestamp);
    BasicDBObject query = new BasicDBObject("ts", new BasicDBObject("$gt", startingTimestamp));
    cursor = oplog.find(query);
    cursor.addOption(Bytes.QUERYOPTION_OPLOGREPLAY);
  }

  cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
  cursor.addOption(Bytes.QUERYOPTION_TAILABLE);
  cursor.addOption(Bytes.QUERYOPTION_AWAITDATA);

  return cursor;
}
 
Example #20
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 #21
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
private List<DBObject> keywordInWaterfall(String in_field, String keyword) {
  List<DBObject> waterfalls = new ArrayList<DBObject>();
  BasicDBObject query = new BasicDBObject();
  query.put(in_field, keyword);

  BasicDBObject keys = new BasicDBObject();

  DBCursor cur = this.dbWaterfalls.find(query, keys);
  while (cur.hasNext()) {
    DBObject o = cur.next();
    waterfalls.add( convertDBObjectToWaterfall(o) );
  }
  cur.close();

  return waterfalls;
}
 
Example #22
Source File: MongoGridFSSession.java    From ymate-platform-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public List<GridFSDBFile> findAll(OrderBy orderBy, Page page) {
    DBCursor _cursor = __dbCollection.find();
    if (orderBy != null) {
        _cursor.sort(orderBy.toBson());
    }
    if (page != null && page.page() > 0 && page.pageSize() > 0) {
        _cursor.skip((page.page() - 1) * page.pageSize()).limit(page.pageSize());
    }
    List<GridFSDBFile> _results = new ArrayList<GridFSDBFile>();
    while (_cursor.hasNext()) {
        _results.add((GridFSDBFile) _cursor.next());
    }
    _cursor.close();
    return _results;
}
 
Example #23
Source File: MongoGridFSSession.java    From ymate-platform-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public List<GridFSDBFile> find(Query query, OrderBy orderBy, Page page) {
    DBCursor _cursor = __dbCollection.find(query.toBson());
    if (orderBy != null) {
        _cursor.sort(orderBy.toBson());
    }
    if (page != null && page.page() > 0 && page.pageSize() > 0) {
        _cursor.skip((page.page() - 1) * page.pageSize()).limit(page.pageSize());
    }
    List<GridFSDBFile> _results = new ArrayList<GridFSDBFile>();
    while (_cursor.hasNext()) {
        _results.add((GridFSDBFile) _cursor.next());
    }
    _cursor.close();
    return _results;
}
 
Example #24
Source File: MongoDBUtil.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Find all DBObjects from database using this query. 
 * Note 1: it may do a full table scan if the query contains no index keys.
 * Note 2: it will fetch all content into JVM memory rather than use lazy loading.
 * So make sure you call it only at small collection such as configuration data.
 * 
 * @param query
 * @param databaseName
 * @param namespace
 * @param collection
 * @param fields
 * @return
 */
public static final List<DBObject> queryAllFromMongo(DBObject query, 
		String databaseName, String namespace, String collection, 
		DBObject fields, DBObject sortFields, int numToSkip, int limit) {
	
	DBCollection coll = getDBCollection(databaseName, namespace, collection);
	int count = (int)coll.count(query);
	ArrayList<DBObject> objList = new ArrayList<DBObject>();
	DBCursor list = coll.find(query, fields).skip(numToSkip).limit(limit);
	if ( sortFields != null ) {
		list = list.sort(sortFields);
	}
	while ( list.hasNext() ) {
		objList.add(list.next());
	}
	return objList;
}
 
Example #25
Source File: ITeslaDatasource.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected DBCursor getDataCursor(Map<String, ?> ranges, int start, int count, ColumnDescriptor[] columnDescriptors, Map<String, INDEXTYPE> indexTypes) {
    DBCursor cursor = super.getDataCursor(ranges, start, count, columnDescriptors, indexTypes);

    //TODO use indexes on filters ; check http://emptysqua.re/blog/optimizing-mongodb-compound-indexes/
    // example : {horizon:1,datetime:1,CHOO17GROUP_1_NGU_SM_P:1}

    return cursor.sort(new BasicDBObject("datetime", 1));
}
 
Example #26
Source File: MongoDB.java    From act with GNU General Public License v3.0 5 votes vote down vote up
public DBCursor fetchNamesAndUsageForInchis(Set<String> inchis) {
  BasicDBList inchiList = new BasicDBList();
  inchiList.addAll(inchis);
  BasicDBObject inClause = new BasicDBObject("$in", inchiList);
  BasicDBObject whereQuery = new BasicDBObject("InChI", inClause);
  whereQuery.put("xref.BING", new BasicDBObject("$exists", true));
  BasicDBObject fields = new BasicDBObject();
  fields.put("InChI", true);
  fields.put("names.brenda", true);
  fields.put("xref", true);
  DBCursor cursor = dbChemicals.find(whereQuery, fields);
  return cursor;
}
 
Example #27
Source File: MongoDbDeltaStore.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
@Override
public ImmutableSet<WaveletId> lookup(WaveId waveId) throws PersistenceException {


  DBObject query = new BasicDBObject();
  query.put(MongoDbDeltaStoreUtil.FIELD_WAVE_ID, waveId.serialise());

  DBObject projection = new BasicDBObject();
  projection.put(MongoDbDeltaStoreUtil.FIELD_WAVELET_ID, 1);

  DBCursor cursor = null;

  try {
    cursor = getDeltaDbCollection().find(query, projection);
  } catch (MongoException e) {
    throw new PersistenceException(e);
  }


  if (cursor == null || !cursor.hasNext()) {
    return ImmutableSet.of();
  } else {
    ImmutableSet.Builder<WaveletId> builder = ImmutableSet.builder();
    for (DBObject waveletIdDBObject : cursor) {
      builder.add(WaveletId.deserialise((String) waveletIdDBObject
          .get(MongoDbDeltaStoreUtil.FIELD_WAVELET_ID)));
    }
    return builder.build();
  }
}
 
Example #28
Source File: MongoDB.java    From act with GNU General Public License v3.0 5 votes vote down vote up
private List<Chemical> keywordInChemicals(String in_field, String keyword) {
  List<Chemical> chemicals = new ArrayList<Chemical>();

  DBCursor cur = constructCursorForMatchingChemicals(in_field, keyword, null);
  while (cur.hasNext()) {
    DBObject o = cur.next();
    chemicals.add( convertDBObjectToChemical(o) );
  }
  cur.close();

  return chemicals;
}
 
Example #29
Source File: MongoAddressing.java    From sissi with Apache License 2.0 5 votes vote down vote up
private MongoJIDContexts read(DBCursor cursor) {
	try (DBCursor iterator = cursor) {
		while (iterator.hasNext()) {
			JIDContext context = MongoAddressing.this.contexts.get(MongoUtils.asLong(iterator.next(), Dictionary.FIELD_INDEX));
			// Double check 4 multi thread
			if (context != null) {
				super.add(context);
			}
		}
	}
	return this;
}
 
Example #30
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();
  }
}