Search in sources :

Example 1 with BulkWriteException

use of com.mongodb.BulkWriteException 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);
}
Also used : BulkWriteOperation(com.mongodb.BulkWriteOperation) BulkWriteUpsert(com.mongodb.BulkWriteUpsert) UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) QueryBuilder(com.mongodb.QueryBuilder) BulkWriteError(com.mongodb.BulkWriteError) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BulkWriteException(com.mongodb.BulkWriteException) BulkWriteResult(com.mongodb.BulkWriteResult) DBCollection(com.mongodb.DBCollection) HashSet(java.util.HashSet)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)1 BulkWriteError (com.mongodb.BulkWriteError)1 BulkWriteException (com.mongodb.BulkWriteException)1 BulkWriteOperation (com.mongodb.BulkWriteOperation)1 BulkWriteResult (com.mongodb.BulkWriteResult)1 BulkWriteUpsert (com.mongodb.BulkWriteUpsert)1 DBCollection (com.mongodb.DBCollection)1 DBObject (com.mongodb.DBObject)1 QueryBuilder (com.mongodb.QueryBuilder)1 HashSet (java.util.HashSet)1 UpdateOp (org.apache.jackrabbit.oak.plugins.document.UpdateOp)1