|
| 1 | +/* |
| 2 | + * Copyright 2026-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.bson.Document; |
| 22 | +import org.springframework.dao.DataAccessException; |
| 23 | +import org.springframework.data.mongodb.core.MongoTemplate.SourceAwareDocument; |
| 24 | +import org.springframework.data.mongodb.core.QueryOperations.DeleteContext; |
| 25 | +import org.springframework.data.mongodb.core.QueryOperations.UpdateContext; |
| 26 | +import org.springframework.data.mongodb.core.bulk.Bulk; |
| 27 | +import org.springframework.data.mongodb.core.bulk.BulkWriteOptions.Order; |
| 28 | +import org.springframework.data.mongodb.core.bulk.BulkWriteOptions; |
| 29 | +import org.springframework.data.mongodb.core.bulk.BulkOperation; |
| 30 | +import org.springframework.data.mongodb.core.bulk.BulkOperation.Insert; |
| 31 | +import org.springframework.data.mongodb.core.bulk.BulkOperation.Remove; |
| 32 | +import org.springframework.data.mongodb.core.bulk.BulkOperation.RemoveFirst; |
| 33 | +import org.springframework.data.mongodb.core.bulk.BulkOperation.Replace; |
| 34 | +import org.springframework.data.mongodb.core.bulk.BulkOperation.Update; |
| 35 | +import org.springframework.data.mongodb.core.bulk.BulkOperation.UpdateFirst; |
| 36 | +import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; |
| 37 | +import org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent; |
| 38 | + |
| 39 | +import com.mongodb.MongoBulkWriteException; |
| 40 | +import com.mongodb.MongoNamespace; |
| 41 | +import com.mongodb.client.model.DeleteOptions; |
| 42 | +import com.mongodb.client.model.UpdateOptions; |
| 43 | +import com.mongodb.client.model.bulk.ClientBulkWriteOptions; |
| 44 | +import com.mongodb.client.model.bulk.ClientBulkWriteResult; |
| 45 | +import com.mongodb.client.model.bulk.ClientNamespacedWriteModel; |
| 46 | + |
| 47 | +/** |
| 48 | + * Internal API wrapping a {@link MongoTemplate} to encapsulate {@link Bulk} handling. |
| 49 | + * |
| 50 | + * @author Christoph Strobl |
| 51 | + * @since 2026/02 |
| 52 | + */ |
| 53 | +class BulkWriter { |
| 54 | + |
| 55 | + MongoTemplate template; |
| 56 | + |
| 57 | + BulkWriter(MongoTemplate template) { |
| 58 | + this.template = template; |
| 59 | + } |
| 60 | + |
| 61 | + public ClientBulkWriteResult write(String defaultDatabase, BulkWriteOptions options, Bulk bulk) { |
| 62 | + |
| 63 | + List<ClientNamespacedWriteModel> writeModels = new ArrayList<>(); |
| 64 | + List<SourceAwareDocument<Object>> afterSaveCallables = new ArrayList<>(); |
| 65 | + |
| 66 | + for (BulkOperation bulkOp : bulk.operations()) { |
| 67 | + |
| 68 | + String collectionName = bulkOp.context().namespace().collection() != null |
| 69 | + ? bulkOp.context().namespace().collection() |
| 70 | + : template.getCollectionName(bulkOp.context().namespace().type()); |
| 71 | + |
| 72 | + MongoNamespace mongoNamespace = new MongoNamespace(defaultDatabase, collectionName); |
| 73 | + if (bulkOp instanceof Insert insert) { |
| 74 | + |
| 75 | + SourceAwareDocument<Object> sourceAwareDocument = template.prepareObjectForSave(collectionName, insert.value(), |
| 76 | + template.getConverter()); |
| 77 | + writeModels.add(ClientNamespacedWriteModel.insertOne(mongoNamespace, sourceAwareDocument.document())); |
| 78 | + afterSaveCallables.add(sourceAwareDocument); |
| 79 | + } else if (bulkOp instanceof Update update) { |
| 80 | + |
| 81 | + Class<?> domainType = update.context().namespace().type(); |
| 82 | + boolean multi = !(bulkOp instanceof UpdateFirst); |
| 83 | + |
| 84 | + UpdateContext updateContext = template.getQueryOperations().updateContext(update.update(), update.query(), |
| 85 | + update.upsert()); |
| 86 | + MongoPersistentEntity<?> entity = template.getPersistentEntity(domainType); |
| 87 | + |
| 88 | + Document mappedQuery = updateContext.getMappedQuery(entity); |
| 89 | + Object mappedUpdate = updateContext.isAggregationUpdate() ? updateContext.getUpdatePipeline(domainType) |
| 90 | + : updateContext.getMappedUpdate(entity); |
| 91 | + UpdateOptions updateOptions = updateContext.getUpdateOptions(domainType, update.query()); |
| 92 | + |
| 93 | + if (multi) { |
| 94 | + writeModels.add(BulkWriteSupport.updateMany(mongoNamespace, mappedQuery, mappedUpdate, updateOptions)); |
| 95 | + } else { |
| 96 | + writeModels.add(BulkWriteSupport.updateOne(mongoNamespace, mappedQuery, mappedUpdate, updateOptions)); |
| 97 | + } |
| 98 | + } else if (bulkOp instanceof Remove remove) { |
| 99 | + |
| 100 | + Class<?> domainType = remove.context().namespace().type(); |
| 101 | + DeleteContext deleteContext = template.getQueryOperations().deleteQueryContext(remove.query()); |
| 102 | + |
| 103 | + Document mappedQuery = deleteContext.getMappedQuery(template.getPersistentEntity(domainType)); |
| 104 | + DeleteOptions deleteOptions = deleteContext.getDeleteOptions(domainType); |
| 105 | + |
| 106 | + if (remove instanceof RemoveFirst) { |
| 107 | + writeModels.add(BulkWriteSupport.removeOne(mongoNamespace, mappedQuery, deleteOptions)); |
| 108 | + } else { |
| 109 | + writeModels.add(BulkWriteSupport.removeMany(mongoNamespace, mappedQuery, deleteOptions)); |
| 110 | + } |
| 111 | + } else if (bulkOp instanceof Replace replace) { |
| 112 | + |
| 113 | + Class<?> domainType = replace.context().namespace().type(); |
| 114 | + |
| 115 | + SourceAwareDocument<Object> sourceAwareDocument = template.prepareObjectForSave(collectionName, |
| 116 | + replace.replacement(), template.getConverter()); |
| 117 | + |
| 118 | + UpdateContext updateContext = template.getQueryOperations().replaceSingleContext(replace.query(), |
| 119 | + MappedDocument.of(sourceAwareDocument.document()), replace.upsert()); |
| 120 | + |
| 121 | + Document mappedQuery = updateContext.getMappedQuery(template.getPersistentEntity(domainType)); |
| 122 | + UpdateOptions updateOptions = updateContext.getUpdateOptions(domainType, replace.query()); |
| 123 | + |
| 124 | + writeModels.add( |
| 125 | + BulkWriteSupport.replaceOne(mongoNamespace, mappedQuery, sourceAwareDocument.document(), updateOptions)); |
| 126 | + afterSaveCallables.add(sourceAwareDocument); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + try { |
| 131 | + |
| 132 | + ClientBulkWriteResult clientBulkWriteResult = template.doWithClient(client -> client.bulkWrite(writeModels, |
| 133 | + ClientBulkWriteOptions.clientBulkWriteOptions().ordered(options.getOrder().equals(BulkWriteOptions.Order.ORDERED)))); |
| 134 | + |
| 135 | + afterSaveCallables.forEach(callable -> { |
| 136 | + template |
| 137 | + .maybeEmitEvent(new AfterSaveEvent<>(callable.source(), callable.document(), callable.collectionName())); |
| 138 | + template.maybeCallAfterSave(callable.source(), callable.document(), callable.collectionName()); |
| 139 | + }); |
| 140 | + return clientBulkWriteResult; |
| 141 | + } catch (MongoBulkWriteException e) { |
| 142 | + DataAccessException dataAccessException = template.getExceptionTranslator().translateExceptionIfPossible(e); |
| 143 | + if (dataAccessException != null) { |
| 144 | + throw dataAccessException; |
| 145 | + } |
| 146 | + throw e; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments