Example usage for com.mongodb DBCursor skip

List of usage examples for com.mongodb DBCursor skip

Introduction

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

Prototype

public DBCursor skip(final int numberOfElements) 

Source Link

Document

Discards a given number of elements at the beginning of the cursor.

Usage

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 w w  w. j  a  va 2 s  . c  om
 * @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

/**
 * Lists all the files in the specified name space. Only latest versions are included in the list.
 * @param namespace - (optional) name space to be searched for files. When nothing specified, the default bucket is used
 * @param sortCriteria - objects in the collection are sorted with this criteria
 * @param start - starting index/*from  www . j  a  v a 2  s .c om*/
 * @param size - maximum number of objects returned
 * @param count - (optional) is updated with the number of objects in the database
 * @return a view of the files stored under the specified name space that contains the specified range.
 */
public List<GridFSDBFile> listFiles(final @Nullable String namespace, final DBObject sortCriteria,
        final int start, final int size, final @Nullable MutableLong count) {
    final List<GridFSDBFile> list = newArrayList();
    final DB db = client().getDB(CONFIG_MANAGER.getDbName());
    final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db);
    final DBCursor cursor = gfsNs.getFileList(
            new BasicDBObject(FILE_VERSION_PROP, new BasicDBObject("$exists", true)), sortCriteria);
    cursor.skip(start).limit(size);
    try {
        while (cursor.hasNext()) {
            list.add((GridFSDBFile) 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<GridFSDBFile> listFileOpenAccess(final @Nullable String namespace, final DBObject sortCriteria,
        final int start, final int size, final @Nullable MutableLong count) {
    final List<GridFSDBFile> list = newArrayList();
    final DB db = client().getDB(CONFIG_MANAGER.getDbName());
    final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db);
    final DBCursor cursor = gfsNs
            .getFileList(new BasicDBObject(FILE_VERSION_PROP, new BasicDBObject("$exists", true))
                    .append(FILE_OPEN_ACCESS_LINK_PROP, new BasicDBObject("$exists", true)), sortCriteria);
    cursor.skip(start).limit(size);
    try {/* w  w  w. j av  a 2  s .c o  m*/
        while (cursor.hasNext()) {
            list.add((GridFSDBFile) 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

/**
 * Lists all the versions of the specified file.
 * @param namespace - (optional) name space to be searched for files. When nothing specified, the default bucket is used
 * @param filename - filename to be searched for in the database
 * @param sortCriteria - objects in the collection are sorted with this criteria
 * @param start - starting index/* w w w . j av  a 2s .  c om*/
 * @param size - maximum number of objects returned
 * @param count - (optional) is updated with the number of objects in the database
 * @return a view of the versions stored of the specified file that contains the specified range.
 */
public List<GridFSDBFile> listFileVersions(final @Nullable String namespace, final String filename,
        final DBObject sortCriteria, final int start, final int size, final @Nullable MutableLong count) {
    checkArgument(isNotBlank(filename), "Uninitialized or invalid filename");
    final List<GridFSDBFile> list = newArrayList();
    final DB db = client().getDB(CONFIG_MANAGER.getDbName());
    final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db);
    final DBCursor cursor = gfsNs.getFileList(new BasicDBObject("filename", filename.trim()), sortCriteria);
    cursor.skip(start).limit(size);
    try {
        while (cursor.hasNext()) {
            list.add((GridFSDBFile) cursor.next());
        }
    } finally {
        cursor.close();
    }
    if (count != null) {
        count.setValue(cursor.count());
    }
    return list;
}

From source file:fr.cirad.web.controller.gigwa.base.AbstractVariantController.java

License:Open Source License

/**
 * Builds the variant rows./*  w  w  w.  j a va2s.  c o m*/
 *
 * @param mongoTemplate the mongo template
 * @param variantsToBuildRowsFor the variants to build rows for
 * @param sortBy the sort by
 * @param sortDir the sort dir
 * @param page the page
 * @param size the size
 * @param variantFieldMap the variant field map
 * @param runDataFieldMap the run data field map
 * @return the array list
 */
private ArrayList<Object[]> buildVariantRows(MongoTemplate mongoTemplate, DBCursor variantsToBuildRowsFor,
        String sortBy, String sortDir, int page, int size, HashMap<Integer, String> variantFieldMap,
        Map<Integer, String> runDataFieldMap) {
    if (sortBy != null && sortBy.length() > 0) {
        String cleanSortField = sortBy.replaceFirst("%23", "");
        variantsToBuildRowsFor.sort(
                new BasicDBObject(cleanSortField, Integer.valueOf("DESC".equalsIgnoreCase(sortDir) ? -1 : 1)));
    }
    variantsToBuildRowsFor.skip(page * size).limit(size);

    ArrayList<Object[]> variantRows = new ArrayList<Object[]>();
    HashMap<Comparable, Object[]> variantIdToRowMap = new HashMap<Comparable, Object[]>();

    Collection<Comparable> currentVariants = new ArrayList<Comparable>();
    while (variantsToBuildRowsFor.hasNext()) {
        DBObject record = variantsToBuildRowsFor.next();
        Object[] aRow = new Object[variantFieldMap.size() + runDataFieldMap.size()];
        for (int i : variantFieldMap.keySet())
            aRow[i] = Helper.readPossiblyNestedField(record, variantFieldMap.get(i));
        variantRows.add(aRow);
        variantIdToRowMap.put((Comparable) aRow[0], aRow);
        currentVariants.add((Comparable) aRow[0]);
    }

    if (!runDataFieldMap.isEmpty()) { // Query on VariantRunData so we can fill run-related fields
        ArrayList<DBObject> genotypingDataAggregationParams2 = new ArrayList<DBObject>();
        genotypingDataAggregationParams2.add(new BasicDBObject("$match", new BasicDBObject(
                "_id." + VariantRunDataId.FIELDNAME_VARIANT_ID, new BasicDBObject("$in", currentVariants))));
        DBObject project = new BasicDBObject();
        for (String sField : runDataFieldMap.values())
            project.put(sField.replaceAll("\\.", ""), "$" + sField);
        genotypingDataAggregationParams2.add(new BasicDBObject("$project", project));

        //         long beforeQuery = System.currentTimeMillis();
        Cursor genotypingDataCursor = mongoTemplate
                .getCollection(MongoTemplateManager.getMongoCollectionName(VariantRunData.class))
                .aggregate(genotypingDataAggregationParams2,
                        AggregationOptions.builder().allowDiskUse(true).build());
        while (genotypingDataCursor.hasNext()) {
            DBObject record = genotypingDataCursor.next();
            Object[] aRow = variantIdToRowMap.get(
                    Helper.readPossiblyNestedField(record, "_id." + VariantRunDataId.FIELDNAME_VARIANT_ID));
            for (int fieldIndex : runDataFieldMap.keySet())
                aRow[fieldIndex] = record.get(runDataFieldMap.get(fieldIndex).replaceAll("\\.", ""));
        }
        //         LOG.debug("Genotyping data main query treated in " + (System.currentTimeMillis() - beforeQuery) + "ms");
    }
    return variantRows;
}

From source file:fr.cnes.sitools.datasource.mongodb.business.SitoolsMongoDBDataSource.java

License:Open Source License

/**
 * Execute a query//from w  w w  .  ja  v  a 2  s. c o m
 * 
 * @param request the mongoBD request model
 * @return DBCursor
 */
public DBCursor limitedQuery(MongoDBRequestModel request) {
    DBCursor cursor = null;
    try {
        DBObject dbObjectQuery = (DBObject) JSON.parse(request.getFilterString());
        DBObject dbObjectFields = (DBObject) JSON.parse(request.getKeysString());
        DBObject dbObjectSort = (DBObject) JSON.parse(request.getSortString());
        DB database = getDatabase();
        DBCollection collection = database.getCollection(request.getCollectionName());

        cursor = collection.find(dbObjectQuery, dbObjectFields).sort(dbObjectSort);
        cursor.skip(request.getStart());
        cursor.limit(request.getLimit());
        return cursor;
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, null, ex);
        closeCursor(cursor);
    }
    return null;
}

From source file:fr.gouv.vitam.cases.DbRequest.java

License:Open Source License

/**
 * In MongoDB : <br/> //from w ww . j a v  a  2  s . c  o  m
 * find(Query, Projection).sort(SortFilter).skip(SkipFilter).limit(LimitFilter);<br/>
 * In addition, one shall limit the scan by: <br/>
 * find(Query, Projection)._addSpecial( "$maxscan", highlimit)
 *   .sort(SortFilter).skip(SkipFilter).limit(LimitFilter);
 * 
 * @param query
 * @param result to be filtered using query
 * @return the new result or null if the same
 */
private ResultInterface lastFilter(final AbstractQueryParser query, final ResultInterface result) {
    if (simulate) {
        return null;
    }
    boolean filter = (query.getLimit() > 0 || query.getOffset() > 0);
    if (!filter) {
        return null;
    }
    ObjectNode orderBy = query.getOrderBy();
    if (GlobalDatas.PRINT_REQUEST) {
        LOGGER.warn("Req1LevelMD Filter on: Limit {} Offset {} OrderBy {}", query.getLimit(), query.getOffset(),
                orderBy);
    }
    final ResultInterface subresult = CassandraAccess.createOneResult();
    BasicDBObject inClause = getInClauseForField(DAip.ID, result.getCurrentDaip());
    final DBCursor cursor = mdAccess.daips.collection.find(inClause, ID_NBCHILD);
    if (query.getLimit() > 0) {
        cursor.limit(query.getLimit());
    }
    if (query.getOffset() > 0) {
        cursor.skip(query.getOffset());
    }
    if (orderBy != null) {
        // orderBy is used only if limit and/or offset is set
        cursor.sort((DBObject) orderBy);
    }
    long tempCount = 0;
    while (cursor.hasNext()) {
        final DAip maip = (DAip) cursor.next();
        final String mid = maip.getId();
        subresult.getCurrentDaip().add(mid);
        tempCount += maip.getLong(DAip.NBCHILD);
    }
    cursor.close();
    if (subresult.getCurrentDaip().containsAll(result.getCurrentDaip())) {
        // same so don't change it
        return null;
    }
    subresult.setNbSubNodes(tempCount);
    // Not updateMinMax since result is not "valid" path but node UUID and not needed
    subresult.setMinLevel(result.getMinLevel());
    subresult.setMaxLevel(result.getMaxLevel());
    subresult.setLoaded(true);
    if (GlobalDatas.PRINT_REQUEST) {
        subresult.putBeforeSave();
        LOGGER.warn("Filtered: {}", subresult);
    }
    return subresult;
}

From source file:fr.gouv.vitam.mdbes.DbRequest.java

License:Open Source License

/**
 * In MongoDB : <br/> // w  w w .  j  a v  a 2 s.c  o m
 * find(Query, Projection).sort(SortFilter).skip(SkipFilter).limit(LimitFilter);<br/>
 * In addition, one shall limit the scan by: <br/>
 * find(Query, Projection)._addSpecial( "$maxscan", highlimit)
 *   .sort(SortFilter).skip(SkipFilter).limit(LimitFilter);
 * 
 * @param query
 * @param result to be filtered using query
 * @return the new result or null if the same
 */
private ResultInterface lastFilter(final AbstractQueryParser query, final ResultInterface result) {
    if (simulate) {
        return null;
    }
    boolean filter = (query.getLimit() > 0 || query.getOffset() > 0);
    if (!filter) {
        return null;
    }
    ObjectNode orderBy = query.getOrderBy();
    if (GlobalDatas.PRINT_REQUEST) {
        LOGGER.warn("Req1LevelMD Filter on: Limit {} Offset {} OrderBy {}", query.getLimit(), query.getOffset(),
                orderBy);
    }
    final ResultInterface subresult = MongoDbAccess.createOneResult();
    BasicDBObject inClause = getInClauseForField(DAip.ID, result.getCurrentDaip());
    final DBCursor cursor = mdAccess.daips.collection.find(inClause, ID_NBCHILD);
    if (query.getLimit() > 0) {
        cursor.limit(query.getLimit());
    }
    if (query.getOffset() > 0) {
        cursor.skip(query.getOffset());
    }
    if (orderBy != null) {
        // orderBy is used only if limit and/or offset is set
        cursor.sort((DBObject) orderBy);
    }
    long tempCount = 0;
    while (cursor.hasNext()) {
        final DAip maip = (DAip) cursor.next();
        final String mid = maip.getId();
        subresult.getCurrentDaip().add(mid);
        tempCount += maip.getLong(DAip.NBCHILD);
    }
    cursor.close();
    if (subresult.getCurrentDaip().containsAll(result.getCurrentDaip())) {
        // same so don't change it
        return null;
    }
    subresult.setNbSubNodes(tempCount);
    // Not updateMinMax since result is not "valid" path but node UUID and not needed
    subresult.setMinLevel(result.getMinLevel());
    subresult.setMaxLevel(result.getMaxLevel());
    subresult.setLoaded(true);
    if (GlobalDatas.PRINT_REQUEST) {
        subresult.putBeforeSave();
        LOGGER.warn("Filtered: {}", subresult);
    }
    return subresult;
}

From source file:hulop.hokoukukan.utils.MongoAdapter.java

License:Open Source License

@Override
public JSONArray getLogs(String clientId, String start, String end, String skip, String limit, String event) {
    BasicDBObject query = new BasicDBObject();
    if (clientId != null) {
        query.append("client", clientId);
    }//from w  ww . j  a v  a 2  s  .com

    new BasicDBObject("Date", new BasicDBObject("$gt", start).append("$lte", end));

    BasicDBObject timeRange = new BasicDBObject();
    if (start != null) {
        timeRange.append("$gte", Long.parseLong(start));
    }
    if (end != null) {
        timeRange.append("$lt", Long.parseLong(end));
    }
    if (timeRange.size() > 0) {
        query.append("timestamp", timeRange);
    }
    if (event != null) {
        query.append("event", event);
    }
    System.out.println(query.toString());
    DBCursor cursor = logCol.find(query);
    if (skip != null) {
        cursor = cursor.skip(Integer.parseInt(skip));
    }
    if (limit != null) {
        cursor = cursor.limit(Integer.parseInt(limit));
    }
    JSONArray result = new JSONArray();
    try {
        while (cursor.hasNext()) {
            result.add(new JSONObject(cursor.next().toString()));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:in.mtap.iincube.mongoapi.MongoReader.java

License:Apache License

private DBCursor getCursor() {
    DBCollection collection = collectionFactory.get();
    assertNotNull(queryObject, "findQuery == null");
    DBCursor cursor;
    if (fields != null) {
        BasicDBObject selectFields = new BasicDBObject();
        for (String field : fields) {
            selectFields.append(field, 1);
        }/*  ww w .  j a  va2  s .com*/
        cursor = collection.find(queryObject, selectFields);
    } else {
        cursor = collection.find(queryObject);
    }
    if (skip > 0)
        cursor.skip(skip);
    if (limit > 0)
        cursor.limit(limit);
    if (sortObject != null)
        cursor.sort(sortObject);
    return cursor;
}