Example usage for com.mongodb DBCursor batchSize

List of usage examples for com.mongodb DBCursor batchSize

Introduction

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

Prototype

public DBCursor batchSize(final int numberOfElements) 

Source Link

Document

Limits the number of elements returned in one batch.

Usage

From source file:net.ion.framework.db.mongo.jdbc.MongoStatement.java

License:Apache License

public ResultSet executeQuery(String sql) throws SQLException {
    // TODO//  w ww.  j  a v  a  2s. co m
    // handle max rows

    DBCursor cursor = new Executor(conn.getDB(), sql).query();
    if (fetchSize > 0)
        cursor.batchSize(fetchSize);
    if (maxRows > 0)
        cursor.limit(maxRows);

    last = MClientResultSet.create(cursor);
    return last;
}

From source file:org.apache.camel.component.mongodb.MongoDbProducer.java

License:Apache License

protected void doFindAll(Exchange exchange) throws Exception {
    DBCollection dbCol = calculateCollection(exchange);
    // do not use getMandatoryBody, because if the body is empty we want to retrieve all objects in the collection
    DBObject query = null;/*  w ww  .j  a va  2  s .co  m*/
    // do not run around looking for a type converter unless there is a need for it
    if (exchange.getIn().getBody() != null) {
        query = exchange.getIn().getBody(DBObject.class);
    }
    DBObject fieldFilter = exchange.getIn().getHeader(MongoDbConstants.FIELDS_FILTER, DBObject.class);

    // get the batch size and number to skip
    Integer batchSize = exchange.getIn().getHeader(MongoDbConstants.BATCH_SIZE, Integer.class);
    Integer numToSkip = exchange.getIn().getHeader(MongoDbConstants.NUM_TO_SKIP, Integer.class);
    Integer limit = exchange.getIn().getHeader(MongoDbConstants.LIMIT, Integer.class);
    DBObject sortBy = exchange.getIn().getHeader(MongoDbConstants.SORT_BY, DBObject.class);
    DBCursor ret = null;
    try {
        if (query == null && fieldFilter == null) {
            ret = dbCol.find(new BasicDBObject());
        } else if (fieldFilter == null) {
            ret = dbCol.find(query);
        } else {
            ret = dbCol.find(query, fieldFilter);
        }

        if (sortBy != null) {
            ret.sort(sortBy);
        }

        if (batchSize != null) {
            ret.batchSize(batchSize.intValue());
        }

        if (numToSkip != null) {
            ret.skip(numToSkip.intValue());
        }

        if (limit != null) {
            ret.limit(limit.intValue());
        }

        Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.findAll);
        resultMessage.setBody(ret.toArray());
        resultMessage.setHeader(MongoDbConstants.RESULT_TOTAL_SIZE, ret.count());
        resultMessage.setHeader(MongoDbConstants.RESULT_PAGE_SIZE, ret.size());

    } catch (Exception e) {
        // rethrow the exception
        throw e;
    } finally {
        // make sure the cursor is closed
        if (ret != null) {
            ret.close();
        }
    }

}

From source file:org.apache.gora.mongodb.store.MongoStore.java

License:Apache License

/**
 * Execute the query and return the result.
 *//*w  w  w  .j a v  a 2  s.c om*/
@Override
public Result<K, T> execute(final Query<K, T> query) {

    String[] fields = getFieldsToQuery(query.getFields());
    // Build the actual MongoDB query
    DBObject q = MongoDBQuery.toDBQuery(query);
    DBObject p = MongoDBQuery.toProjection(fields, mapping);

    if (query.getFilter() != null) {
        boolean succeeded = filterUtil.setFilter(q, query.getFilter(), this);
        if (succeeded) {
            // don't need local filter
            query.setLocalFilterEnabled(false);
        }
    }

    // Execute the query on the collection
    DBCursor cursor = mongoClientColl.find(q, p);
    if (query.getLimit() > 0)
        cursor = cursor.limit((int) query.getLimit());
    cursor.batchSize(100);
    cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);

    // Build the result
    MongoDBResult<K, T> mongoResult = new MongoDBResult<K, T>(this, query);
    mongoResult.setCursor(cursor);

    return mongoResult;
}

From source file:org.axonframework.eventsourcing.eventstore.mongo.documentpercommit.DocumentPerCommitStorageStrategy.java

License:Apache License

@Override
protected DBCursor applyBatchSize(DBCursor cursor, int batchSize) {
    return cursor.batchSize(batchSize / 8).limit(batchSize);
}

From source file:org.eclipse.birt.data.oda.mongodb.internal.impl.MDbOperation.java

License:Open Source License

/**
 * Applies data set query properties and hints on DBCursor, except
 * for cursor limit.//  ww  w. j  a  va  2 s  .c o m
 * @see #applyPropertiesToCursor(DBCursor,QueryProperties,boolean,boolean)
 */
static void applyPropertiesToCursor(DBCursor rowsCursor, QueryProperties queryProps, boolean includeSortExpr) {
    if (includeSortExpr) // normally done only when executing a query to get full result set
    {
        DBObject sortExprObj = null;
        try {
            sortExprObj = queryProps.getSortExprAsParsedObject();
        } catch (OdaException ex) {
            // log warning and ignore
            DriverUtil.getLogger().log(Level.WARNING,
                    Messages.bind("Unable to parse the user-defined Sort Expression: {0}", //$NON-NLS-1$
                            queryProps.getSortExpr()), ex);
        }

        if (sortExprObj != null)
            rowsCursor.sort(sortExprObj);
    }

    ReadPreference readPref = queryProps.getTaggableReadPreference();
    if (readPref != null)
        rowsCursor.setReadPreference(readPref);

    if (queryProps.getBatchSize() > 0)
        rowsCursor.batchSize(queryProps.getBatchSize());

    if (queryProps.getNumDocsToSkip() > 0)
        rowsCursor.skip(queryProps.getNumDocsToSkip());

    DBObject hintObj = queryProps.getIndexHintsAsParsedObject();
    if (hintObj != null)
        rowsCursor.hint(hintObj);
    else // try to pass the hint string value as is
    {
        String hintValue = queryProps.getIndexHints();
        if (!hintValue.isEmpty())
            rowsCursor.hint(hintValue);
    }

    if (queryProps.hasNoTimeOut())
        rowsCursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
    if (queryProps.isPartialResultsOk())
        rowsCursor.addOption(Bytes.QUERYOPTION_PARTIAL);
}

From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java

License:Apache License

@Override
public VariantDBIterator iterator(Query query, QueryOptions options) {
    if (options == null) {
        options = new QueryOptions();
    }/* w ww  .  j a  v a  2s  . co  m*/
    if (query == null) {
        query = new Query();
    }
    QueryBuilder qb = QueryBuilder.start();
    //        parseQueryOptions(options, qb);
    qb = parseQuery(query, qb);
    //        DBObject projection = parseProjectionQueryOptions(options);
    DBObject projection = createProjection(query, options);
    DBCursor dbCursor = variantsCollection.nativeQuery().find(qb.get(), projection, options);
    dbCursor.batchSize(options.getInt("batchSize", 100));
    return new VariantMongoDBIterator(dbCursor, getDbObjectToVariantConverter(query, options));
}

From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBIterator.java

License:Apache License

VariantMongoDBIterator(DBCursor dbCursor, DBObjectToVariantConverter dbObjectToVariantConverter,
        int batchSize) { //Package protected
    this.dbCursor = dbCursor;
    this.dbObjectToVariantConverter = dbObjectToVariantConverter;
    if (batchSize > 0) {
        dbCursor.batchSize(batchSize);
    }/*from w ww  . j ava  2  s.com*/
}