Search in sources :

Example 1 with Find

use of com.allanbank.mongodb.builder.Find in project YCSB by brianfrankcooper.

the class AsyncMongoDbClient method scan.

/**
   * Perform a range scan for a set of records in the database. Each field/value
   * pair from the result will be stored in a HashMap.
   * 
   * @param table
   *          The name of the table
   * @param startkey
   *          The record key of the first record to read.
   * @param recordcount
   *          The number of records to read
   * @param fields
   *          The list of fields to read, or null for all of them
   * @param result
   *          A Vector of HashMaps, where each HashMap is a set field/value
   *          pairs for one record
   * @return Zero on success, a non-zero error code on error. See the {@link DB}
   *         class's description for a discussion of error codes.
   */
@Override
public final Status scan(final String table, final String startkey, final int recordcount, final Set<String> fields, final Vector<HashMap<String, ByteIterator>> result) {
    try {
        final MongoCollection collection = database.getCollection(table);
        final Find.Builder find = Find.builder().query(where("_id").greaterThanOrEqualTo(startkey)).limit(recordcount).batchSize(recordcount).sort(Sort.asc("_id")).readPreference(readPreference);
        if (fields != null) {
            final DocumentBuilder fieldsDoc = BuilderFactory.start();
            for (final String field : fields) {
                fieldsDoc.add(field, INCLUDE);
            }
            find.projection(fieldsDoc);
        }
        result.ensureCapacity(recordcount);
        final MongoIterator<Document> cursor = collection.find(find);
        if (!cursor.hasNext()) {
            System.err.println("Nothing found in scan for key " + startkey);
            return Status.NOT_FOUND;
        }
        while (cursor.hasNext()) {
            // toMap() returns a Map but result.add() expects a
            // Map<String,String>. Hence, the suppress warnings.
            final Document doc = cursor.next();
            final HashMap<String, ByteIterator> docAsMap = new HashMap<String, ByteIterator>();
            fillMap(docAsMap, doc);
            result.add(docAsMap);
        }
        return Status.OK;
    } catch (final Exception e) {
        System.err.println(e.toString());
        return Status.ERROR;
    }
}
Also used : MongoCollection(com.allanbank.mongodb.MongoCollection) ByteIterator(com.yahoo.ycsb.ByteIterator) DocumentBuilder(com.allanbank.mongodb.bson.builder.DocumentBuilder) HashMap(java.util.HashMap) Find(com.allanbank.mongodb.builder.Find) Document(com.allanbank.mongodb.bson.Document) DBException(com.yahoo.ycsb.DBException)

Aggregations

MongoCollection (com.allanbank.mongodb.MongoCollection)1 Document (com.allanbank.mongodb.bson.Document)1 DocumentBuilder (com.allanbank.mongodb.bson.builder.DocumentBuilder)1 Find (com.allanbank.mongodb.builder.Find)1 ByteIterator (com.yahoo.ycsb.ByteIterator)1 DBException (com.yahoo.ycsb.DBException)1 HashMap (java.util.HashMap)1