Search in sources :

Example 1 with DocumentBuilder

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

the class AsyncMongoDbClient method update.

/**
   * Update a record in the database. Any field/value pairs in the specified
   * values HashMap will be written into the record with the specified record
   * key, overwriting any existing values with the same field name.
   * 
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to write.
   * @param values
   *          A HashMap of field/value pairs to update in the 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 update(final String table, final String key, final HashMap<String, ByteIterator> values) {
    try {
        final MongoCollection collection = database.getCollection(table);
        final DocumentBuilder query = BuilderFactory.start().add("_id", key);
        final DocumentBuilder update = BuilderFactory.start();
        final DocumentBuilder fieldsToSet = update.push("$set");
        for (final Map.Entry<String, ByteIterator> entry : values.entrySet()) {
            fieldsToSet.add(entry.getKey(), entry.getValue().toArray());
        }
        final long res = collection.update(query, update, false, false, writeConcern);
        return writeConcern == Durability.NONE || res == 1 ? Status.OK : Status.NOT_FOUND;
    } 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) Map(java.util.Map) DBException(com.yahoo.ycsb.DBException)

Example 2 with DocumentBuilder

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

the class AsyncMongoDbClient method read.

/**
   * Read a record from the database. Each field/value pair from the result will
   * be stored in a HashMap.
   * 
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to read.
   * @param fields
   *          The list of fields to read, or null for all of them
   * @param result
   *          A HashMap of field/value pairs for the result
   * @return Zero on success, a non-zero error code on error or "not found".
   */
@Override
public final Status read(final String table, final String key, final Set<String> fields, final HashMap<String, ByteIterator> result) {
    try {
        final MongoCollection collection = database.getCollection(table);
        final DocumentBuilder query = DOCUMENT_BUILDER.get().reset().add("_id", key);
        Document queryResult = null;
        if (fields != null) {
            final DocumentBuilder fieldsToReturn = BuilderFactory.start();
            final Iterator<String> iter = fields.iterator();
            while (iter.hasNext()) {
                fieldsToReturn.add(iter.next(), 1);
            }
            final Find.Builder fb = new Find.Builder(query);
            fb.projection(fieldsToReturn);
            fb.setLimit(1);
            fb.setBatchSize(1);
            fb.readPreference(readPreference);
            final MongoIterator<Document> ci = collection.find(fb.build());
            if (ci.hasNext()) {
                queryResult = ci.next();
                ci.close();
            }
        } else {
            queryResult = collection.findOne(query);
        }
        if (queryResult != null) {
            fillMap(result, queryResult);
        }
        return queryResult != null ? Status.OK : Status.NOT_FOUND;
    } catch (final Exception e) {
        System.err.println(e.toString());
        return Status.ERROR;
    }
}
Also used : MongoCollection(com.allanbank.mongodb.MongoCollection) DocumentBuilder(com.allanbank.mongodb.bson.builder.DocumentBuilder) DocumentBuilder(com.allanbank.mongodb.bson.builder.DocumentBuilder) Find(com.allanbank.mongodb.builder.Find) Document(com.allanbank.mongodb.bson.Document) DBException(com.yahoo.ycsb.DBException)

Example 3 with DocumentBuilder

use of com.allanbank.mongodb.bson.builder.DocumentBuilder 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)

Example 4 with DocumentBuilder

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

the class AsyncMongoDbClient method insert.

/**
   * Insert a record in the database. Any field/value pairs in the specified
   * values HashMap will be written into the record with the specified record
   * key.
   * 
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to insert.
   * @param values
   *          A HashMap of field/value pairs to insert in the 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 insert(final String table, final String key, final HashMap<String, ByteIterator> values) {
    try {
        final MongoCollection collection = database.getCollection(table);
        final DocumentBuilder toInsert = DOCUMENT_BUILDER.get().reset().add("_id", key);
        final Document query = toInsert.build();
        for (final Map.Entry<String, ByteIterator> entry : values.entrySet()) {
            toInsert.add(entry.getKey(), entry.getValue().toArray());
        }
        // Do an upsert.
        if (batchSize <= 1) {
            long result;
            if (useUpsert) {
                result = collection.update(query, toInsert, /* multi= */
                false, /* upsert= */
                true, writeConcern);
            } else {
                // Return is not stable pre-SERVER-4381. No exception is success.
                collection.insert(writeConcern, toInsert);
                result = 1;
            }
            return result == 1 ? Status.OK : Status.NOT_FOUND;
        }
        // Use a bulk insert.
        try {
            if (useUpsert) {
                batchedWrite.update(query, toInsert, /* multi= */
                false, /* upsert= */
                true);
            } else {
                batchedWrite.insert(toInsert);
            }
            batchedWriteCount += 1;
            if (batchedWriteCount < batchSize) {
                return Status.BATCHED_OK;
            }
            long count = collection.write(batchedWrite);
            if (count == batchedWriteCount) {
                batchedWrite.reset().mode(BatchedWriteMode.REORDERED);
                batchedWriteCount = 0;
                return Status.OK;
            }
            System.err.println("Number of inserted documents doesn't match the " + "number sent, " + count + " inserted, sent " + batchedWriteCount);
            batchedWrite.reset().mode(BatchedWriteMode.REORDERED);
            batchedWriteCount = 0;
            return Status.ERROR;
        } catch (Exception e) {
            System.err.println("Exception while trying bulk insert with " + batchedWriteCount);
            e.printStackTrace();
            return Status.ERROR;
        }
    } catch (final Exception e) {
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : MongoCollection(com.allanbank.mongodb.MongoCollection) ByteIterator(com.yahoo.ycsb.ByteIterator) DocumentBuilder(com.allanbank.mongodb.bson.builder.DocumentBuilder) Document(com.allanbank.mongodb.bson.Document) HashMap(java.util.HashMap) Map(java.util.Map) DBException(com.yahoo.ycsb.DBException)

Aggregations

MongoCollection (com.allanbank.mongodb.MongoCollection)4 DocumentBuilder (com.allanbank.mongodb.bson.builder.DocumentBuilder)4 DBException (com.yahoo.ycsb.DBException)4 Document (com.allanbank.mongodb.bson.Document)3 ByteIterator (com.yahoo.ycsb.ByteIterator)3 HashMap (java.util.HashMap)3 Find (com.allanbank.mongodb.builder.Find)2 Map (java.util.Map)2