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