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;
}
}
Aggregations