use of com.mongodb.bulk.BulkWriteResult in project mongo-java-driver by mongodb.
the class MongoCollectionImpl method insertMany.
@Override
public void insertMany(final List<? extends TDocument> documents, final InsertManyOptions options, final SingleResultCallback<Void> callback) {
notNull("documents", documents);
List<InsertRequest> requests = new ArrayList<InsertRequest>(documents.size());
for (TDocument document : documents) {
if (document == null) {
throw new IllegalArgumentException("documents can not contain a null value");
}
if (getCodec() instanceof CollectibleCodec) {
document = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
}
requests.add(new InsertRequest(documentToBsonDocument(document)));
}
executor.execute(new MixedBulkWriteOperation(namespace, requests, options.isOrdered(), writeConcern).bypassDocumentValidation(options.getBypassDocumentValidation()), errorHandlingCallback(new SingleResultCallback<BulkWriteResult>() {
@Override
public void onResult(final BulkWriteResult result, final Throwable t) {
callback.onResult(null, t);
}
}, LOGGER));
}
use of com.mongodb.bulk.BulkWriteResult in project mongo-java-driver by mongodb.
the class InsertCommandProtocol method execute.
@Override
public BulkWriteResult execute(final InternalConnection connection) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Inserting %d documents into namespace %s on connection [%s] to server %s", insertRequests.size(), getNamespace(), connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
}
BulkWriteResult writeResult = super.execute(connection);
LOGGER.debug("Insert completed");
return writeResult;
}
use of com.mongodb.bulk.BulkWriteResult 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.bulk.BulkWriteResult 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.bulk.BulkWriteResult in project AtherysCore by Atherys-Horizons.
the class AbstractMongoDatabaseManager method saveAll.
@Override
public void saveAll(Collection<T> objects) {
List<WriteModel<Document>> updates = new ArrayList<>();
for (T object : objects) {
Optional<Document> doc = toDocument(object);
if (!doc.isPresent())
continue;
updates.add(new UpdateOneModel<>(new Document("uuid", object.getUUID()), new Document("$set", doc.get()), new UpdateOptions().upsert(true)));
}
if (!updates.isEmpty()) {
BulkWriteResult bulkWriteResult = getCollection().bulkWrite(updates);
if (bulkWriteResult.wasAcknowledged()) {
int mods = bulkWriteResult.getModifiedCount();
int inserts = bulkWriteResult.getInsertedCount();
int deletes = bulkWriteResult.getDeletedCount();
int matched = bulkWriteResult.getMatchedCount();
logger.info("[MongoDB] " + this.getClass().getSimpleName() + " updated " + updates.size() + " objects, " + "where " + mods + " were modified, " + inserts + " were added, " + deletes + " were removed, and " + matched + " were matched.");
}
}
}
Aggregations