use of com.mongodb.BulkWriteUpsert in project jackrabbit-oak by apache.
the class MongoDocumentStore method sendBulkUpdate.
private <T extends Document> BulkUpdateResult sendBulkUpdate(Collection<T> collection, java.util.Collection<UpdateOp> updateOps, Map<String, T> oldDocs) {
DBCollection dbCollection = getDBCollection(collection);
BulkWriteOperation bulk = dbCollection.initializeUnorderedBulkOperation();
String[] bulkIds = new String[updateOps.size()];
int i = 0;
for (UpdateOp updateOp : updateOps) {
String id = updateOp.getId();
QueryBuilder query = createQueryForUpdate(id, updateOp.getConditions());
T oldDoc = oldDocs.get(id);
DBObject update;
if (oldDoc == null || oldDoc == NodeDocument.NULL) {
query.and(Document.MOD_COUNT).exists(false);
update = createUpdate(updateOp, true);
} else {
query.and(Document.MOD_COUNT).is(oldDoc.getModCount());
update = createUpdate(updateOp, false);
}
bulk.find(query.get()).upsert().updateOne(update);
bulkIds[i++] = id;
}
BulkWriteResult bulkResult;
Set<String> failedUpdates = new HashSet<String>();
Set<String> upserts = new HashSet<String>();
try {
bulkResult = bulk.execute();
} catch (BulkWriteException e) {
bulkResult = e.getWriteResult();
for (BulkWriteError err : e.getWriteErrors()) {
failedUpdates.add(bulkIds[err.getIndex()]);
}
}
for (BulkWriteUpsert upsert : bulkResult.getUpserts()) {
upserts.add(bulkIds[upsert.getIndex()]);
}
return new BulkUpdateResult(failedUpdates, upserts);
}
Aggregations