Skip to content
Snippets Groups Projects
Commit 40bdb507 authored by Jared Rosoff's avatar Jared Rosoff Committed by Robert J. Moore
Browse files

Change inserts to upserts in mongoclient

When running workloads that use the "insert" operation, we get a lot of
duplicate key exceptions from the server. Changing to upsert to avoid
these exceptions.
parent 7762b5e0
No related branches found
No related tags found
No related merge requests found
......@@ -245,11 +245,16 @@ public class AsyncMongoDbClient extends DB {
final MongoCollection collection = db.getCollection(table);
final DocumentBuilder r = DOCUMENT_BUILDER.get().reset()
.add("_id", key);
final Document q = r.build();
for (final Map.Entry<String, ByteIterator> entry : values
.entrySet()) {
r.add(entry.getKey(), entry.getValue().toArray());
}
collection.insert(writeConcern, r);
collection.update(q, r, /* multi= */false, /* upsert= */true,
writeConcern);
return 0;
}
catch (final Exception e) {
......
......@@ -261,11 +261,15 @@ public class MongoDbClient extends DB {
db.requestStart();
DBCollection collection = db.getCollection(table);
DBObject r = new BasicDBObject().append("_id", key);
DBObject criteria = new BasicDBObject().append("_id", key);
DBObject toInsert = new BasicDBObject().append("_id", key);
for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
r.put(entry.getKey(), entry.getValue().toArray());
toInsert.put(entry.getKey(), entry.getValue().toArray());
}
collection.insert(r, writeConcern);
collection.update(criteria, toInsert, true, false, writeConcern);
return 0;
}
catch (Exception e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment