Search in sources :

Example 1 with BulkWriteOptions

use of com.mongodb.client.model.BulkWriteOptions 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) {
    MongoCollection<BasicDBObject> dbCollection = getDBCollection(collection);
    List<WriteModel<BasicDBObject>> writes = new ArrayList<>(updateOps.size());
    String[] bulkIds = new String[updateOps.size()];
    int i = 0;
    for (UpdateOp updateOp : updateOps) {
        String id = updateOp.getId();
        Bson query = createQueryForUpdate(id, updateOp.getConditions());
        T oldDoc = oldDocs.get(id);
        if (oldDoc == null || oldDoc == NodeDocument.NULL) {
            query = Filters.and(query, Filters.exists(Document.MOD_COUNT, false));
            writes.add(new UpdateOneModel<>(query, createUpdate(updateOp, true), new UpdateOptions().upsert(true)));
        } else {
            query = Filters.and(query, Filters.eq(Document.MOD_COUNT, oldDoc.getModCount()));
            writes.add(new UpdateOneModel<>(query, createUpdate(updateOp, false), new UpdateOptions().upsert(true)));
        }
        bulkIds[i++] = id;
    }
    BulkWriteResult bulkResult;
    Set<String> failedUpdates = new HashSet<String>();
    Set<String> upserts = new HashSet<String>();
    try {
        bulkResult = dbCollection.bulkWrite(writes, new BulkWriteOptions().ordered(false));
    } catch (MongoBulkWriteException 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 : BulkWriteUpsert(com.mongodb.bulk.BulkWriteUpsert) UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) ArrayList(java.util.ArrayList) BulkWriteError(com.mongodb.bulk.BulkWriteError) WriteModel(com.mongodb.client.model.WriteModel) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) Bson(org.bson.conversions.Bson) BasicDBObject(com.mongodb.BasicDBObject) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) HashSet(java.util.HashSet)

Example 2 with BulkWriteOptions

use of com.mongodb.client.model.BulkWriteOptions in project core-ng-project by neowu.

the class MongoCollectionImpl method bulkDelete.

@Override
public long bulkDelete(List<?> ids) {
    StopWatch watch = new StopWatch();
    int deletedRows = 0;
    try {
        List<DeleteOneModel<T>> models = new ArrayList<>(ids.size());
        for (Object id : ids) {
            models.add(new DeleteOneModel<>(Filters.eq("_id", id)));
        }
        BulkWriteResult result = collection().bulkWrite(models, new BulkWriteOptions().ordered(false));
        deletedRows = result.getDeletedCount();
        return deletedRows;
    } finally {
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("mongoDB", elapsedTime, 0, deletedRows);
        logger.debug("bulkDelete, collection={}, size={}, elapsedTime={}", collectionName, ids.size(), elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) ArrayList(java.util.ArrayList) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) StopWatch(core.framework.util.StopWatch)

Example 3 with BulkWriteOptions

use of com.mongodb.client.model.BulkWriteOptions in project mongo-java-driver by mongodb.

the class JsonPoweredCrudTestHelper method getBulkWriteResult.

BsonDocument getBulkWriteResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) {
    List<WriteModel<BsonDocument>> writeModels = new ArrayList<WriteModel<BsonDocument>>();
    for (BsonValue bsonValue : arguments.getArray("requests")) {
        BsonDocument cur = bsonValue.asDocument();
        String name = cur.getString("name").getValue();
        BsonDocument requestArguments = cur.getDocument("arguments");
        if (name.equals("insertOne")) {
            writeModels.add(new InsertOneModel<BsonDocument>(requestArguments.getDocument("document")));
        } else if (name.equals("updateOne")) {
            if (requestArguments.isDocument("update")) {
                writeModels.add(new UpdateOneModel<BsonDocument>(requestArguments.getDocument("filter"), requestArguments.getDocument("update"), getUpdateOptions(requestArguments)));
            } else {
                // update is a pipeline
                writeModels.add(new UpdateOneModel<BsonDocument>(requestArguments.getDocument("filter"), getListOfDocuments(requestArguments.getArray("update")), getUpdateOptions(requestArguments)));
            }
        } else if (name.equals("updateMany")) {
            if (requestArguments.isDocument("update")) {
                writeModels.add(new UpdateManyModel<BsonDocument>(requestArguments.getDocument("filter"), requestArguments.getDocument("update"), getUpdateOptions(requestArguments)));
            } else {
                // update is a pipeline
                writeModels.add(new UpdateManyModel<BsonDocument>(requestArguments.getDocument("filter"), getListOfDocuments(requestArguments.getArray("update")), getUpdateOptions(requestArguments)));
            }
        } else if (name.equals("deleteOne")) {
            writeModels.add(new DeleteOneModel<BsonDocument>(requestArguments.getDocument("filter"), getDeleteOptions(requestArguments)));
        } else if (name.equals("deleteMany")) {
            writeModels.add(new DeleteManyModel<BsonDocument>(requestArguments.getDocument("filter"), getDeleteOptions(requestArguments)));
        } else if (name.equals("replaceOne")) {
            writeModels.add(new ReplaceOneModel<BsonDocument>(requestArguments.getDocument("filter"), requestArguments.getDocument("replacement"), getReplaceOptions(requestArguments)));
        } else {
            throw new UnsupportedOperationException(format("Unsupported write request type: %s", name));
        }
    }
    try {
        BulkWriteResult bulkWriteResult;
        BsonDocument optionsDocument = arguments.getDocument("options", new BsonDocument());
        BulkWriteOptions options = new BulkWriteOptions().ordered(optionsDocument.getBoolean("ordered", BsonBoolean.TRUE).getValue());
        if (optionsDocument.containsKey("bypassDocumentValidation")) {
            options.bypassDocumentValidation(optionsDocument.getBoolean("bypassDocumentValidation").getValue());
        }
        if (clientSession == null) {
            bulkWriteResult = getCollection(collectionOptions).bulkWrite(writeModels, options);
        } else {
            bulkWriteResult = getCollection(collectionOptions).bulkWrite(clientSession, writeModels, options);
        }
        return toResult(bulkWriteResult, writeModels, Collections.<BulkWriteError>emptyList());
    } catch (MongoBulkWriteException e) {
        BsonDocument result = toResult(e.getWriteResult(), writeModels, e.getWriteErrors());
        result.put("error", BsonBoolean.TRUE);
        return result;
    }
}
Also used : ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) WriteModel(com.mongodb.client.model.WriteModel) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) BsonDocument(org.bson.BsonDocument) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) BsonValue(org.bson.BsonValue)

Example 4 with BulkWriteOptions

use of com.mongodb.client.model.BulkWriteOptions in project mongo-java-driver by mongodb.

the class MongoCollectionImplTest method testBulkWrite.

@Test
public void testBulkWrite() {
    List<WriteModel<Document>> requests = singletonList(new InsertOneModel<>(new Document()));
    BulkWriteOptions options = new BulkWriteOptions().ordered(false);
    assertAll("bulkWrite", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.bulkWrite(null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.bulkWrite(requests, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.bulkWrite(clientSession, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.bulkWrite(clientSession, requests, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.bulkWrite(null, requests)), () -> assertThrows(IllegalArgumentException.class, () -> collection.bulkWrite(null, requests, options))), () -> {
        Publisher<BulkWriteResult> expected = mongoOperationPublisher.bulkWrite(null, requests, new BulkWriteOptions());
        assertPublisherIsTheSameAs(expected, collection.bulkWrite(requests), "Default");
    }, () -> {
        Publisher<BulkWriteResult> expected = mongoOperationPublisher.bulkWrite(null, requests, options);
        assertPublisherIsTheSameAs(expected, collection.bulkWrite(requests, options), "With options");
    }, () -> {
        Publisher<BulkWriteResult> expected = mongoOperationPublisher.bulkWrite(clientSession, requests, new BulkWriteOptions());
        assertPublisherIsTheSameAs(expected, collection.bulkWrite(clientSession, requests), "With client session");
    }, () -> {
        Publisher<BulkWriteResult> expected = mongoOperationPublisher.bulkWrite(clientSession, requests, options);
        assertPublisherIsTheSameAs(expected, collection.bulkWrite(clientSession, requests, options), "With client session & options");
    });
}
Also used : BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) WriteModel(com.mongodb.client.model.WriteModel) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) Test(org.junit.jupiter.api.Test)

Example 5 with BulkWriteOptions

use of com.mongodb.client.model.BulkWriteOptions in project mongo-java-driver by mongodb.

the class JsonPoweredCrudTestHelper method getBulkWriteResult.

BsonDocument getBulkWriteResult(final BsonDocument arguments) {
    WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;
    if (arguments.containsKey("writeConcern")) {
        if (arguments.getDocument("writeConcern").size() > 1) {
            throw new UnsupportedOperationException("Write concern document contains unexpected keys: " + arguments.getDocument("writeConcern").keySet());
        }
        writeConcern = new WriteConcern(arguments.getDocument("writeConcern").getInt32("w").intValue());
    }
    List<WriteModel<BsonDocument>> writeModels = new ArrayList<WriteModel<BsonDocument>>();
    for (Iterator<BsonValue> iter = arguments.getArray("requests").iterator(); iter.hasNext(); ) {
        BsonDocument cur = iter.next().asDocument();
        if (cur.get("insertOne") != null) {
            writeModels.add(new InsertOneModel<BsonDocument>(cur.getDocument("insertOne").getDocument("document")));
        } else if (cur.get("updateOne") != null) {
            writeModels.add(new UpdateOneModel<BsonDocument>(cur.getDocument("updateOne").getDocument("filter"), cur.getDocument("updateOne").getDocument("update")));
        } else {
            throw new UnsupportedOperationException("Unsupported write request type");
        }
    }
    return toResult(collection.withWriteConcern(writeConcern).bulkWrite(writeModels, new BulkWriteOptions().ordered(arguments.getBoolean("ordered", BsonBoolean.TRUE).getValue())));
}
Also used : UpdateOneModel(com.mongodb.client.model.UpdateOneModel) BsonDocument(org.bson.BsonDocument) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) WriteConcern(com.mongodb.WriteConcern) ArrayList(java.util.ArrayList) WriteModel(com.mongodb.client.model.WriteModel) BsonValue(org.bson.BsonValue)

Aggregations

BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)7 ArrayList (java.util.ArrayList)6 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)5 WriteModel (com.mongodb.client.model.WriteModel)5 BsonDocument (org.bson.BsonDocument)4 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)3 ReplaceOneModel (com.mongodb.client.model.ReplaceOneModel)3 UpdateOneModel (com.mongodb.client.model.UpdateOneModel)3 UpdateOptions (com.mongodb.client.model.UpdateOptions)3 BsonValue (org.bson.BsonValue)3 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)2 WriteConcern (com.mongodb.WriteConcern)2 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)2 BasicDBObject (com.mongodb.BasicDBObject)1 ReadConcern (com.mongodb.ReadConcern)1 ReadConcernLevel (com.mongodb.ReadConcernLevel)1 ReadPreference (com.mongodb.ReadPreference)1 TransactionOptions (com.mongodb.TransactionOptions)1 BulkWriteError (com.mongodb.bulk.BulkWriteError)1 BulkWriteUpsert (com.mongodb.bulk.BulkWriteUpsert)1