in mongodb/src/main/java/site/ycsb/db/AsyncMongoDbClient.java [253:314]
public final Status insert(final String table, final String key,
final Map<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;
}
}