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);
}
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);
}
}
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;
}
}
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");
});
}
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())));
}
Aggregations