Search in sources :

Example 71 with DBCursor

use of com.mongodb.DBCursor in project mica2 by obiba.

the class Mica460Upgrade method execute.

@Override
public void execute(Version currentVersion) {
    logger.info("Executing Mica upgrade to version 4.6.0");
    try {
        DBObject dataAccessForm = mongoTemplate.execute(db -> db.getCollection("dataAccessForm").find().next());
        DBObject dataAccessFeasibilityForm = mongoTemplate.execute(db -> db.getCollection("dataAccessFeasibilityForm").find().next());
        DBObject dataAccessAmendmentForm = mongoTemplate.execute(db -> db.getCollection("dataAccessAmendmentForm").find().next());
        String csvExportFormat = dataAccessForm.get("csvExportFormat").toString();
        String feasibilityCsvExportFormat = dataAccessFeasibilityForm.get("csvExportFormat").toString();
        String amendmentCsvExportFormat = dataAccessAmendmentForm.get("csvExportFormat").toString();
        logger.info("Moving data access config out of data access form");
        insertDataAccessConfig(dataAccessForm, csvExportFormat, feasibilityCsvExportFormat, amendmentCsvExportFormat);
        logger.info("Updating and publishing data access form");
        updateAndPublishDataAccessForm(dataAccessForm);
        logger.info("Updating and publishing data access feasibility form");
        updateAndPublishDataAccessFeasibilityForm(dataAccessFeasibilityForm);
        logger.info("Updating and publishing data access amendment form");
        updateAndPublishDataAccessAmendmentForm(dataAccessAmendmentForm);
        logger.info("Applying form revision to submitted data access requests");
        DBCursor dataAccessRequests = mongoTemplate.execute(db -> db.collectionExists("dataAccessRequest") ? db.getCollection("dataAccessRequest").find() : null);
        applyDataAccessRequestFormRevision(dataAccessRequests);
        logger.info("Applying form revision to submitted data access feasibilities");
        DBCursor dataAccessFeasibilities = mongoTemplate.execute(db -> db.collectionExists("dataAccessFeasibility") ? db.getCollection("dataAccessFeasibility").find() : null);
        applyDataAccessFeasibilityFormRevision(dataAccessFeasibilities);
        logger.info("Applying form revision to submitted data access amendments");
        DBCursor dataAccessAmendments = mongoTemplate.execute(db -> db.collectionExists("dataAccessAmendment") ? db.getCollection("dataAccessAmendment").find() : null);
        applyDataAccessAmendmentFormRevision(dataAccessAmendments);
    } catch (RuntimeException e) {
        logger.error("Error occurred when trying to update data access forms or requests.", e);
    }
    try {
        logger.info("Adding 'sets' vocabulary to the network taxonomy");
        DBObject networkTaxonomy = mongoTemplate.execute(db -> db.getCollection("taxonomyEntityWrapper").findOne("network"));
        if (networkTaxonomy != null) {
            DBObject setsVocabulary = BasicDBObject.parse("{\n" + "  \"name\" : \"sets\",\n" + "  \"title\" : {\n" + "      \"en\" : \"Sets\",\n" + "      \"fr\" : \"Ensembles\"\n" + "  },\n" + "  \"description\" : {\n" + "      \"en\" : \"Sets in which the network appears.\",\n" + "      \"fr\" : \"Ensembles dans lesquels est définie le réseau.\"\n" + "  },\n" + "  \"repeatable\" : false,\n" + "  \"keywords\" : {},\n" + "  \"attributes\" : {\n" + "      \"static\" : \"true\",\n" + "      \"hidden\" : \"true\",\n" + "      \"type\" : \"keyword\"\n" + "  }\n" + "}");
            ((ArrayList<Object>) (((DBObject) networkTaxonomy.get("taxonomy")).get("vocabularies"))).add(setsVocabulary);
            mongoTemplate.execute(db -> db.getCollection("taxonomyEntityWrapper").save(networkTaxonomy));
        }
        logger.info("Adding 'sets' vocabulary to the study taxonomy");
        DBObject studyTaxonomy = mongoTemplate.execute(db -> db.getCollection("taxonomyEntityWrapper").findOne("study"));
        if (studyTaxonomy != null) {
            DBObject setsVocabulary = BasicDBObject.parse("{\n" + "  \"name\" : \"sets\",\n" + "  \"title\" : {\n" + "      \"en\" : \"Sets\",\n" + "      \"fr\" : \"Ensembles\"\n" + "  },\n" + "  \"description\" : {\n" + "      \"en\" : \"Sets in which the study appears.\",\n" + "      \"fr\" : \"Ensembles dans lesquels est définie l'étude.\"\n" + "  },\n" + "  \"repeatable\" : false,\n" + "  \"keywords\" : {},\n" + "  \"attributes\" : {\n" + "      \"static\" : \"true\",\n" + "      \"hidden\" : \"true\",\n" + "      \"type\" : \"keyword\"\n" + "  }\n" + "}");
            ((ArrayList<Object>) (((DBObject) studyTaxonomy.get("taxonomy")).get("vocabularies"))).add(setsVocabulary);
            mongoTemplate.execute(db -> db.getCollection("taxonomyEntityWrapper").save(studyTaxonomy));
        }
        logger.info("Indexing Taxonomies");
        eventBus.post(new TaxonomiesUpdatedEvent());
    } catch (Exception e) {
        logger.error("Failed to index Taxonomies", e);
    }
}
Also used : TaxonomiesUpdatedEvent(org.obiba.mica.micaConfig.event.TaxonomiesUpdatedEvent) DBCursor(com.mongodb.DBCursor) ArrayList(java.util.ArrayList) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 72 with DBCursor

use of com.mongodb.DBCursor in project aws-doc-sdk-examples by awsdocs.

the class MongoDBService method getListItemsReport.

// Retrieves all items from MongoDB
public ArrayList<WorkItem> getListItemsReport() {
    MongoClient mongoClient = getConnection();
    // Get the database name
    DB database = mongoClient.getDB("local");
    DBCursor cur = database.getCollection("items").find();
    DBObject dbo = null;
    ArrayList<WorkItem> itemList = new ArrayList();
    WorkItem item = null;
    int index = 0;
    while (cur.hasNext()) {
        index = 0;
        item = new WorkItem();
        dbo = cur.next();
        Set<String> keys = dbo.keySet();
        Iterator iterator = keys.iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            String value = (String) dbo.get(key).toString();
            if (key.compareTo("_id") == 0)
                item.setId(value);
            else if (key.compareTo("archive") == 0)
                item.setArc(value);
            else if (key.compareTo("date") == 0)
                item.setDate(value);
            else if (key.compareTo("description") == 0)
                item.setDescription(value);
            else if (key.compareTo("guide") == 0)
                item.setGuide(value);
            else if (key.compareTo("status") == 0)
                item.setStatus(value);
            else if (key.compareTo("username") == 0) {
                item.setName(value);
                // last item read
                itemList.add(item);
            }
        }
    }
    return itemList;
}
Also used : MongoClient(com.mongodb.MongoClient) DBCursor(com.mongodb.DBCursor) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) WorkItem(com.example.entities.WorkItem) DB(com.mongodb.DB)

Example 73 with DBCursor

use of com.mongodb.DBCursor in project Mycat-Server by MyCATApache.

the class MongoSQLParser method query.

public MongoData query() throws MongoSQLException {
    if (!(statement instanceof SQLSelectStatement)) {
        // return null;
        throw new IllegalArgumentException("not a query sql statement");
    }
    MongoData mongo = new MongoData();
    DBCursor c = null;
    SQLSelectStatement selectStmt = (SQLSelectStatement) statement;
    SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
    int icount = 0;
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
        BasicDBObject fields = new BasicDBObject();
        // 显示的字段
        for (SQLSelectItem item : mysqlSelectQuery.getSelectList()) {
            // System.out.println(item.toString());
            if (!(item.getExpr() instanceof SQLAllColumnExpr)) {
                if (item.getExpr() instanceof SQLAggregateExpr) {
                    SQLAggregateExpr expr = (SQLAggregateExpr) item.getExpr();
                    if (expr.getMethodName().equals("COUNT")) {
                        icount = 1;
                        mongo.setField(getExprFieldName(expr), Types.BIGINT);
                    }
                    fields.put(getExprFieldName(expr), Integer.valueOf(1));
                } else {
                    fields.put(getFieldName(item), Integer.valueOf(1));
                }
            }
        }
        // 表名
        SQLTableSource table = mysqlSelectQuery.getFrom();
        DBCollection coll = this._db.getCollection(table.toString());
        mongo.setTable(table.toString());
        SQLExpr expr = mysqlSelectQuery.getWhere();
        DBObject query = parserWhere(expr);
        // System.out.println(query);
        SQLSelectGroupByClause groupby = mysqlSelectQuery.getGroupBy();
        BasicDBObject gbkey = new BasicDBObject();
        if (groupby != null) {
            for (SQLExpr gbexpr : groupby.getItems()) {
                if (gbexpr instanceof SQLIdentifierExpr) {
                    String name = ((SQLIdentifierExpr) gbexpr).getName();
                    gbkey.put(name, Integer.valueOf(1));
                }
            }
            icount = 2;
        }
        int limitoff = 0;
        int limitnum = 0;
        if (mysqlSelectQuery.getLimit() != null) {
            limitoff = getSQLExprToInt(mysqlSelectQuery.getLimit().getOffset());
            limitnum = getSQLExprToInt(mysqlSelectQuery.getLimit().getRowCount());
        }
        if (icount == 1) {
            mongo.setCount(coll.count(query));
        } else if (icount == 2) {
            BasicDBObject initial = new BasicDBObject();
            initial.put("num", 0);
            String reduce = "function (obj, prev) { " + "  prev.num++}";
            mongo.setGrouyBy(coll.group(gbkey, query, initial, reduce));
        } else {
            if ((limitoff > 0) || (limitnum > 0)) {
                c = coll.find(query, fields).skip(limitoff).limit(limitnum);
            } else {
                c = coll.find(query, fields);
            }
            SQLOrderBy orderby = mysqlSelectQuery.getOrderBy();
            if (orderby != null) {
                BasicDBObject order = new BasicDBObject();
                for (int i = 0; i < orderby.getItems().size(); i++) {
                    SQLSelectOrderByItem orderitem = orderby.getItems().get(i);
                    order.put(orderitem.getExpr().toString(), Integer.valueOf(getSQLExprToAsc(orderitem.getType())));
                }
                c.sort(order);
            // System.out.println(order);
            }
        }
        mongo.setCursor(c);
    }
    return mongo;
}
Also used : MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor)

Example 74 with DBCursor

use of com.mongodb.DBCursor in project mongo-java-driver by mongodb.

the class GridFS method find.

/**
 * Finds a list of files matching the given query.
 *
 * @param query the filter to apply
 * @param sort  the fields to sort with
 * @return list of gridfs files
 * @throws com.mongodb.MongoException if the operation fails
 */
public List<GridFSDBFile> find(final DBObject query, final DBObject sort) {
    List<GridFSDBFile> files = new ArrayList<GridFSDBFile>();
    DBCursor cursor = filesCollection.find(query);
    if (sort != null) {
        cursor.sort(sort);
    }
    try {
        while (cursor.hasNext()) {
            files.add(injectGridFSInstance(cursor.next()));
        }
    } finally {
        cursor.close();
    }
    return Collections.unmodifiableList(files);
}
Also used : DBCursor(com.mongodb.DBCursor) ArrayList(java.util.ArrayList)

Example 75 with DBCursor

use of com.mongodb.DBCursor in project mongomvcc by igd-geo.

the class MongoDBVCollectionTest method lifetimeInsertedLaterOptimization.

/**
 * Tests if lifetime optimization takes effect. Objects that have
 * been inserted in a later commit should not be loaded but filtered
 * out on the database level already.
 */
@Test
@Ignore("Not ready yet. We need to implement full branch history.")
public void lifetimeInsertedLaterOptimization() {
    // ignore this test if we're on MongoDB 1.x
    assumeNotNull(((MongoDBVDatabase) _db).getBuildInfo());
    assumeTrue(((MongoDBVDatabase) _db).getBuildInfo().getMajorVersion() >= 2);
    // insert two documents to skip in-index shortcut
    putPerson("Max", 6);
    putPerson("Pax", 8);
    long firstCID = _master.commit();
    putPerson("Elvis", 3);
    _master.commit();
    VBranch oldMaster = _db.checkout(firstCID);
    VCollection persons = oldMaster.getCollection("persons");
    VCursor cursor = persons.find();
    DBCursor dbcursor = extractDBCursor(cursor);
    assertEquals(2, cursor.size());
    assertTrue(hasAttachedFilter(cursor));
    assertEquals(2, dbcursor.size());
}
Also used : DBCursor(com.mongodb.DBCursor) VBranch(de.fhg.igd.mongomvcc.VBranch) VCursor(de.fhg.igd.mongomvcc.VCursor) VCollection(de.fhg.igd.mongomvcc.VCollection) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DBCursor (com.mongodb.DBCursor)87 BasicDBObject (com.mongodb.BasicDBObject)70 DBObject (com.mongodb.DBObject)54 DBCollection (com.mongodb.DBCollection)42 ArrayList (java.util.ArrayList)20 JSONObject (org.json.JSONObject)15 MongoException (com.mongodb.MongoException)11 DB (com.mongodb.DB)10 Test (org.junit.Test)9 BasicDBList (com.mongodb.BasicDBList)8 HashMap (java.util.HashMap)8 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)6 JSONArray (org.json.JSONArray)5 MongoClient (com.mongodb.MongoClient)4 ObjectId (org.locationtech.geogig.api.ObjectId)4 Function (com.google.common.base.Function)3 Date (java.util.Date)3 Iterator (java.util.Iterator)3 ObjectId (org.bson.types.ObjectId)3 WorkItem (com.example.entities.WorkItem)2