Example usage for com.mongodb DBCursor size

List of usage examples for com.mongodb DBCursor size

Introduction

In this page you can find the example usage for com.mongodb DBCursor size.

Prototype

public int size() 

Source Link

Document

Counts the number of objects matching the query this does take limit/skip into consideration

Usage

From source file:org.slc.sli.ingestion.processors.PurgeProcessor.java

License:Apache License

private void removeTenantCollection(Query searchTenantId, String collectionName) {
    TenantContext.setIsSystemCall(false);

    while (true) {
        LOGGER.debug("{}: Fetching ids", collectionName);
        DBCursor cursor = mongoTemplate.getCollection(collectionName)
                .find(new BasicDBObject(), new BasicDBObject("_id", "1")).limit(purgeBatchSize);
        LOGGER.debug("{}: Completed fetching ids", collectionName);

        if (cursor != null && cursor.size() != 0) {
            List<Object> entitiesToRemove = new ArrayList<Object>();
            while (cursor.hasNext()) {
                entitiesToRemove.add(cursor.next().get("_id"));
            }//w ww.  ja v  a 2  s . c  o m

            DBObject inQuery = new BasicDBObject("_id", new BasicDBObject("$in", entitiesToRemove));

            LOGGER.debug("{}: Starting removal of records", collectionName);
            mongoTemplate.getCollection(collectionName).remove(inQuery);
            LOGGER.debug("{}: Completed removing records for this batch", collectionName);
        } else {
            break;
        }
    }
}

From source file:org.springframework.datastore.mapping.mongo.query.MongoQuery.java

License:Apache License

@Override
protected List executeQuery(final PersistentEntity entity, final Junction criteria) {
    final MongoTemplate template = mongoSession.getMongoTemplate(entity);

    return template.execute(new DbCallback<List>() {
        @Override//from  w w  w  .  ja  v a  2  s. com
        public List doInDB(DB db) throws MongoException, DataAccessException {

            final DBCollection collection = db.getCollection(mongoEntityPersister.getCollectionName(entity));
            if (uniqueResult) {
                final DBObject dbObject;
                if (criteria.isEmpty()) {
                    if (entity.isRoot()) {
                        dbObject = collection.findOne();
                    } else {
                        DBObject query = new BasicDBObject(MongoEntityPersister.MONGO_CLASS_FIELD,
                                entity.getDiscriminator());
                        dbObject = collection.findOne(query);
                    }
                } else {
                    DBObject query = getMongoQuery();

                    dbObject = collection.findOne(query);
                }
                final Object object = createObjectFromDBObject(dbObject);
                return wrapObjectResultInList(object);
            } else {
                DBCursor cursor;
                DBObject query = createQueryObject(entity);

                final List<Projection> projectionList = projections().getProjectionList();
                if (projectionList.isEmpty()) {
                    cursor = executeQuery(entity, criteria, collection, query);
                    return new MongoResultList(cursor, mongoEntityPersister);
                } else {
                    List projectedResults = new ArrayList();
                    for (Projection projection : projectionList) {
                        if (projection instanceof CountProjection) {
                            // For some reason the below doesn't return the expected result whilst executing the query and returning the cursor does
                            //projectedResults.add(collection.getCount(query));
                            cursor = executeQuery(entity, criteria, collection, query);
                            projectedResults.add(cursor.size());
                        } else if (projection instanceof MinProjection) {
                            cursor = executeQuery(entity, criteria, collection, query);
                            MinProjection mp = (MinProjection) projection;

                            MongoResultList results = new MongoResultList(cursor, mongoEntityPersister);
                            projectedResults.add(
                                    manualProjections.min((Collection) results.clone(), mp.getPropertyName()));
                        } else if (projection instanceof MaxProjection) {
                            cursor = executeQuery(entity, criteria, collection, query);
                            MaxProjection mp = (MaxProjection) projection;

                            MongoResultList results = new MongoResultList(cursor, mongoEntityPersister);
                            projectedResults.add(
                                    manualProjections.max((Collection) results.clone(), mp.getPropertyName()));
                        } else if ((projection instanceof PropertyProjection)
                                || (projection instanceof IdProjection)) {
                            final PersistentProperty persistentProperty;
                            final String propertyName;
                            if (projection instanceof IdProjection) {
                                persistentProperty = entity.getIdentity();
                                propertyName = MongoEntityPersister.MONGO_ID_FIELD;
                            } else {
                                PropertyProjection pp = (PropertyProjection) projection;
                                persistentProperty = entity.getPropertyByName(pp.getPropertyName());
                                propertyName = pp.getPropertyName();
                            }
                            if (persistentProperty != null) {
                                populateMongoQuery(entity, query, criteria);
                                List propertyResults = collection.distinct(propertyName, query);

                                if (persistentProperty instanceof ToOne) {
                                    Association a = (Association) persistentProperty;
                                    propertyResults = session.retrieveAll(
                                            a.getAssociatedEntity().getJavaClass(), propertyResults);
                                }

                                if (projectedResults.size() == 0 && projectionList.size() == 1) {
                                    return propertyResults;
                                } else {
                                    projectedResults.add(propertyResults);
                                }
                            } else {
                                throw new InvalidDataAccessResourceUsageException(
                                        "Cannot use [" + projection.getClass().getSimpleName()
                                                + "] projection on non-existent property: " + propertyName);
                            }
                        }
                    }

                    return projectedResults;
                }

            }
        }

        protected DBCursor executeQuery(final PersistentEntity entity, final Junction criteria,
                final DBCollection collection, DBObject query) {
            final DBCursor cursor;
            if (criteria.isEmpty()) {
                cursor = executeQueryAndApplyPagination(collection, query);
            } else {

                populateMongoQuery(entity, query, criteria);

                cursor = executeQueryAndApplyPagination(collection, query);

            }
            return cursor;
        }

        protected DBCursor executeQueryAndApplyPagination(final DBCollection collection, DBObject query) {
            final DBCursor cursor;
            cursor = collection.find(query);
            if (offset > 0) {
                cursor.skip(offset);
            }
            if (max > -1) {
                cursor.limit(max);
            }

            for (Order order : orderBy) {
                DBObject orderObject = new BasicDBObject();
                orderObject.put(order.getProperty(), order.getDirection() == Order.Direction.DESC ? -1 : 1);
                cursor.sort(orderObject);
            }

            return cursor;
        }

    });
}

From source file:tango.dataStructure.Field.java

License:Open Source License

public final void createCells() {
    try {//w  w  w  .j  a va  2s  .  c o m
        DBCursor cur = mc.getFieldNuclei(xp.getName(), name);
        int nbCells = cur.size();
        this.cells = new ArrayList<Cell>(nbCells);
        //IJ.log("creating cells.. field:"+name+ " nb cell found:"+nbCells);
        for (int i = 0; i < nbCells; i++) {
            Cell c = new Cell((BasicDBObject) cur.next(), this, xp);
            cells.add(c);
            c.createChannels();
            //IJ.log("creating cells.. cell:"+i);
        }
        cur.close();
    } catch (Exception e) {
        exceptionPrinter.print(e, "", Core.GUIMode);
    }
}

From source file:tango.gui.util.FieldFactory.java

License:Open Source License

public static Field[] getFields(Experiment xp) {
    DBCursor cursor = xp.getConnector().getFields(xp.getId());
    Field[] res = new Field[cursor.size()];
    for (int i = 0; i < res.length; i++) {
        res[i] = new Field((BasicDBObject) cursor.next(), xp);
    }// w ww.ja v  a 2  s .  co m
    cursor.close();
    return res;
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public ArrayList<String> getUsers() {
    DBCursor cur = adminUser.find();
    ArrayList<String> res = new ArrayList(cur.size());
    while (cur.hasNext()) {
        BasicDBObject u = (BasicDBObject) cur.next();
        String name = u.getString("name");
        if (Utils.isValid(name, false)) {
            res.add(name);/*  w  w  w.ja  v a 2s  .c o  m*/
        }
    }
    cur.close();
    Collections.sort(res);
    return res;
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public ArrayList<String> getProjects() { //db names: prefixe + _ + username + _ + xp element
    DBCursor cur = adminProject.find(new BasicDBObject("user_id", this.userId));
    ArrayList<String> res = new ArrayList<String>(cur.size());
    while (cur.hasNext()) {
        BasicDBObject f = (BasicDBObject) cur.next();
        res.add(f.getString("name"));
    }//from w  w w. j  av a2 s. c  o m
    cur.close();
    Collections.sort(res);
    return res;
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public synchronized ArrayList<Selection> getSelections(ObjectId xpId) {
    if (selection == null) {
        return null;
    }/*from w ww  .j a  va  2  s.c o  m*/
    BasicDBObject query = new BasicDBObject("experiment_id", xpId);
    DBCursor cursor = selection.find(query);
    cursor.sort(new BasicDBObject("name", 1));
    ArrayList<Selection> res = new ArrayList<Selection>(cursor.size());
    while (cursor.hasNext()) {
        Selection s = (Selection) cursor.next();
        s.init();
        res.add(s);
    }
    cursor.close();
    return res;
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public synchronized HashMap<ObjectId, BasicDBObject> getNucleiObjects(ObjectId experimentId) {
    BasicDBObject query = new BasicDBObject("experiment_id", experimentId).append("channelIdx", 0);
    DBCursor cursor = object3D.find(query);
    HashMap<ObjectId, BasicDBObject> res = new HashMap<ObjectId, BasicDBObject>(cursor.size());
    while (cursor.hasNext()) {
        BasicDBObject nuc = (BasicDBObject) cursor.next();
        res.put(nuc.getObjectId("nucleus_id"), nuc);
    }//from  w  ww .  j a  va2 s .  c  o m
    cursor.close();
    return res;
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public synchronized HashMap<Integer, BasicDBObject> getObjects(ObjectId nucleusId, int channelIdx) {
    BasicDBObject query = new BasicDBObject("nucleus_id", nucleusId).append("channelIdx", channelIdx);
    DBCursor cursor = object3D.find(query);
    HashMap<Integer, BasicDBObject> res = new HashMap<Integer, BasicDBObject>(cursor.size());
    while (cursor.hasNext()) {
        BasicDBObject nuc = (BasicDBObject) cursor.next();
        res.put(nuc.getInt("idx"), nuc);
    }/*from  ww  w.j ava2 s  . c o  m*/
    cursor.close();
    return res;
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public synchronized BasicDBObject[] getObjectsArray(ObjectId nucleusId, int channelIdx,
        ArrayList<String> fields) {
    BasicDBObject query = new BasicDBObject("nucleus_id", nucleusId).append("channelIdx", channelIdx);
    DBCursor cursor;
    if (fields == null || fields.isEmpty()) {
        cursor = object3D.find(query);//from   ww w.  java2s  .c  o  m
    } else {
        BasicDBObject f = new BasicDBObject();
        for (String s : fields) {
            f.append(s, 1);
        }
        cursor = object3D.find(query, f);
    }
    cursor.sort(new BasicDBObject("idx", 1));
    BasicDBObject[] res = new BasicDBObject[cursor.size()];
    int count = 0;
    while (cursor.hasNext()) {
        res[count] = (BasicDBObject) cursor.next();
    }
    cursor.close();
    return res;
}