Search in sources :

Example 26 with ByteIterator

use of com.yahoo.ycsb.ByteIterator 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 27 with ByteIterator

use of com.yahoo.ycsb.ByteIterator in project YCSB by brianfrankcooper.

the class MongoDbClient 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 this class's
   *         description for a discussion of error codes.
   */
@Override
public Status update(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        MongoCollection<Document> collection = database.getCollection(table);
        Document query = new Document("_id", key);
        Document fieldsToSet = new Document();
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
            fieldsToSet.put(entry.getKey(), entry.getValue().toArray());
        }
        Document update = new Document("$set", fieldsToSet);
        UpdateResult result = collection.updateOne(query, update);
        if (result.wasAcknowledged() && result.getMatchedCount() == 0) {
            System.err.println("Nothing updated for key " + key);
            return Status.NOT_FOUND;
        }
        return Status.OK;
    } catch (Exception e) {
        System.err.println(e.toString());
        return Status.ERROR;
    }
}
Also used : ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) Document(org.bson.Document) HashMap(java.util.HashMap) Map(java.util.Map) UpdateResult(com.mongodb.client.result.UpdateResult) DBException(com.yahoo.ycsb.DBException)

Example 28 with ByteIterator

use of com.yahoo.ycsb.ByteIterator in project YCSB by brianfrankcooper.

the class MongoDbClient 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 Status insert(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        MongoCollection<Document> collection = database.getCollection(table);
        Document toInsert = new Document("_id", key);
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
            toInsert.put(entry.getKey(), entry.getValue().toArray());
        }
        if (batchSize == 1) {
            if (useUpsert) {
                // this is effectively an insert, but using an upsert instead due
                // to current inability of the framework to clean up after itself
                // between test runs.
                collection.replaceOne(new Document("_id", toInsert.get("_id")), toInsert, UPDATE_WITH_UPSERT);
            } else {
                collection.insertOne(toInsert);
            }
        } else {
            bulkInserts.add(toInsert);
            if (bulkInserts.size() == batchSize) {
                if (useUpsert) {
                    List<UpdateOneModel<Document>> updates = new ArrayList<UpdateOneModel<Document>>(bulkInserts.size());
                    for (Document doc : bulkInserts) {
                        updates.add(new UpdateOneModel<Document>(new Document("_id", doc.get("_id")), doc, UPDATE_WITH_UPSERT));
                    }
                    collection.bulkWrite(updates);
                } else {
                    collection.insertMany(bulkInserts, INSERT_UNORDERED);
                }
                bulkInserts.clear();
            } else {
                return Status.BATCHED_OK;
            }
        }
        return Status.OK;
    } catch (Exception e) {
        System.err.println("Exception while trying bulk insert with " + bulkInserts.size());
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : UpdateOneModel(com.mongodb.client.model.UpdateOneModel) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) ArrayList(java.util.ArrayList) Document(org.bson.Document) HashMap(java.util.HashMap) Map(java.util.Map) DBException(com.yahoo.ycsb.DBException)

Example 29 with ByteIterator

use of com.yahoo.ycsb.ByteIterator in project YCSB by brianfrankcooper.

the class MongoDbClient 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 Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    MongoCursor<Document> cursor = null;
    try {
        MongoCollection<Document> collection = database.getCollection(table);
        Document scanRange = new Document("$gte", startkey);
        Document query = new Document("_id", scanRange);
        Document sort = new Document("_id", INCLUDE);
        FindIterable<Document> findIterable = collection.find(query).sort(sort).limit(recordcount);
        if (fields != null) {
            Document projection = new Document();
            for (String fieldName : fields) {
                projection.put(fieldName, INCLUDE);
            }
            findIterable.projection(projection);
        }
        cursor = findIterable.iterator();
        if (!cursor.hasNext()) {
            System.err.println("Nothing found in scan for key " + startkey);
            return Status.ERROR;
        }
        result.ensureCapacity(recordcount);
        while (cursor.hasNext()) {
            HashMap<String, ByteIterator> resultMap = new HashMap<String, ByteIterator>();
            Document obj = cursor.next();
            fillMap(resultMap, obj);
            result.add(resultMap);
        }
        return Status.OK;
    } catch (Exception e) {
        System.err.println(e.toString());
        return Status.ERROR;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
Also used : ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) HashMap(java.util.HashMap) Document(org.bson.Document) DBException(com.yahoo.ycsb.DBException)

Example 30 with ByteIterator

use of com.yahoo.ycsb.ByteIterator in project YCSB by brianfrankcooper.

the class AbstractDBTestCases method testScan.

/**
   * Test method for {@link DB#scan}.
   */
@Test
public void testScan() {
    final DB client = getDB();
    final String table = getClass().getSimpleName();
    // Insert a bunch of documents.
    for (int i = 0; i < 100; ++i) {
        HashMap<String, ByteIterator> inserted = new HashMap<String, ByteIterator>();
        inserted.put("a", new ByteArrayByteIterator(new byte[] { (byte) (i & 0xFF), (byte) (i >> 8 & 0xFF), (byte) (i >> 16 & 0xFF), (byte) (i >> 24 & 0xFF) }));
        Status result = client.insert(table, padded(i), inserted);
        assertThat("Insert did not return success (0).", result, is(Status.OK));
    }
    Set<String> keys = Collections.singleton("a");
    Vector<HashMap<String, ByteIterator>> results = new Vector<HashMap<String, ByteIterator>>();
    Status result = client.scan(table, "00050", 5, null, results);
    assertThat("Read did not return success (0).", result, is(Status.OK));
    assertThat(results.size(), is(5));
    for (int i = 0; i < 5; ++i) {
        HashMap<String, ByteIterator> read = results.get(i);
        for (String key : keys) {
            ByteIterator iter = read.get(key);
            assertThat("Did not read the inserted field: " + key, iter, notNullValue());
            assertTrue(iter.hasNext());
            assertThat(iter.nextByte(), is(Byte.valueOf((byte) ((i + 50) & 0xFF))));
            assertTrue(iter.hasNext());
            assertThat(iter.nextByte(), is(Byte.valueOf((byte) ((i + 50) >> 8 & 0xFF))));
            assertTrue(iter.hasNext());
            assertThat(iter.nextByte(), is(Byte.valueOf((byte) ((i + 50) >> 16 & 0xFF))));
            assertTrue(iter.hasNext());
            assertThat(iter.nextByte(), is(Byte.valueOf((byte) ((i + 50) >> 24 & 0xFF))));
            assertFalse(iter.hasNext());
        }
    }
}
Also used : Status(com.yahoo.ycsb.Status) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) HashMap(java.util.HashMap) Vector(java.util.Vector) DB(com.yahoo.ycsb.DB) Test(org.junit.Test)

Aggregations

ByteIterator (com.yahoo.ycsb.ByteIterator)87 HashMap (java.util.HashMap)70 StringByteIterator (com.yahoo.ycsb.StringByteIterator)52 Status (com.yahoo.ycsb.Status)33 ByteArrayByteIterator (com.yahoo.ycsb.ByteArrayByteIterator)30 Test (org.junit.Test)30 DBException (com.yahoo.ycsb.DBException)20 Map (java.util.Map)14 IOException (java.io.IOException)8 EntityProperty (com.microsoft.azure.storage.table.EntityProperty)5 Vector (java.util.Vector)5 BaseDocument (com.arangodb.entity.BaseDocument)4 DynamicTableEntity (com.microsoft.azure.storage.table.DynamicTableEntity)4 DB (com.yahoo.ycsb.DB)4 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 Put (org.apache.hadoop.hbase.client.Put)4 MongoCollection (com.allanbank.mongodb.MongoCollection)3 DocumentBuilder (com.allanbank.mongodb.bson.builder.DocumentBuilder)3 ArangoDBException (com.arangodb.ArangoDBException)3