Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Avoid duplicate query mapping for document replacement operations when the filter query can be determined from the already mapped _id field.

See #4707
Original pull request: #4719
  • Loading branch information
mp911de committed Jun 12, 2024
1 parent 908e87b commit 1f94b36
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1603,9 +1603,7 @@ protected Object saveDocument(String collectionName, Document dbDoc, Class<?> en
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
UpdateContext updateContext = queryOperations.replaceSingleContext(mapped, true);
Document replacement = updateContext.getMappedUpdate(entity);

Document filter = updateContext.getMappedQuery(entity);

Document filter = updateContext.getReplacementQuery();
if (updateContext.requiresShardKey(filter, entity)) {

if (entity.getShardKey().isImmutable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ private Document evaluateFields(@Nullable MongoPersistentEntity<?> entity) {

for (Entry<String, Object> entry : fields.entrySet()) {

if (entry.getValue()instanceof MongoExpression mongoExpression) {
if (entry.getValue() instanceof MongoExpression mongoExpression) {

AggregationOperationContext ctx = entity == null ? Aggregation.DEFAULT_CONTEXT
: new RelaxedTypeBasedAggregationOperationContext(entity.getType(), mappingContext, queryMapper);
Expand Down Expand Up @@ -809,13 +809,23 @@ ReplaceOptions getReplaceOptions(@Nullable Class<?> domainType, @Nullable Consum

@Override
<T> Document getMappedQuery(@Nullable MongoPersistentEntity<T> domainType) {
return applyIsolation(super.getMappedQuery(domainType));
}

Document mappedQuery = super.getMappedQuery(domainType);
/**
* A replacement query that is derived from the already {@link MappedDocument}.
*
* @return
*/
Document getReplacementQuery() {
return applyIsolation(getQueryObject());
}

private Document applyIsolation(Document mappedQuery) {
if (multi && update != null && update.isIsolated() && !mappedQuery.containsKey("$isolated")) {
mappedQuery = new Document(mappedQuery);
mappedQuery.put("$isolated", 1);
}

return mappedQuery;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,7 @@ protected Mono<Object> saveDocument(String collectionName, Document document, Cl

MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
UpdateContext updateContext = queryOperations.replaceSingleContext(mapped, true);
Document filter = updateContext.getMappedQuery(entity);
Document filter = updateContext.getReplacementQuery();
Document replacement = updateContext.getMappedUpdate(entity);

Mono<Document> deferredFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ private Object convertIdField(Field documentField, Object source) {
} else if (isKeyword(key)) {
resultDbo.put(key, convertIdField(documentField, entry.getValue()));
} else {
if(documentField.getProperty() != null && documentField.getProperty().isEntity()) {
if (documentField.getProperty() != null && documentField.getProperty().isEntity()) {
Field propertyField = createPropertyField(documentField.getPropertyEntity(), key, mappingContext);
resultDbo.put(key, getMappedValue(propertyField, entry.getValue()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ public void storesAndRemovesTypeWithComplexId() {
id.id = Instant.now().minusSeconds(2);
id.first = "foo";
id.second = "bar";
id.time = Instant.now().minusSeconds(3);
id.id = Instant.now().minusSeconds(3);

TypeWithMyId source = new TypeWithMyId();
source.id = id;
Expand Down Expand Up @@ -4423,7 +4423,7 @@ static class MyId {

String first;
String second;
Instant time;
Instant id;

@Field("t") Instant time;
}
Expand Down

0 comments on commit 1f94b36

Please sign in to comment.