-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid multiple converter calls when parsing string aggregation stages.
Parsing of string based aggregations lead to multiple invocations of potential converters due to missing reuse of bound parameter value as well as attempts to verify out/merge stages within the pipeline that triggered the stage to be converted into the target document. The changes in this commit, reduce the number down to 2. One for examining potential expression dependencies and one for the actual conversion and parameter binding. See #4712 Original pull request: #4717
- Loading branch information
1 parent
bbef940
commit ccecd0c
Showing
5 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...in/java/org/springframework/data/mongodb/repository/query/StringAggregationOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.mongodb.repository.query; | ||
|
||
import java.util.function.Function; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.bson.Document; | ||
import org.springframework.data.mongodb.core.aggregation.AggregationOperation; | ||
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
*/ | ||
class StringAggregationOperation implements AggregationOperation { | ||
|
||
private static final Pattern OPERATOR_PATTERN = Pattern.compile("\\$\\w+"); | ||
|
||
private final String source; | ||
private final Class<?> domainType; | ||
private final Function<String, Document> bindFunction; | ||
|
||
StringAggregationOperation(String source, Class<?> domainType, Function<String, Document> bindFunction) { | ||
|
||
this.source = source; | ||
this.domainType = domainType; | ||
this.bindFunction = bindFunction; | ||
} | ||
|
||
@Override | ||
public Document toDocument(AggregationOperationContext context) { | ||
return context.getMappedObject(bindFunction.apply(source), domainType); | ||
} | ||
|
||
@Override | ||
public String getOperator() { | ||
|
||
Matcher matcher = OPERATOR_PATTERN.matcher(source); | ||
if (matcher.find()) { | ||
return matcher.group(); | ||
} | ||
return AggregationOperation.super.getOperator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...ringframework/data/mongodb/repository/query/StringBasedAggregationOperationUnitTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.mongodb.repository.query; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.bson.Document; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
*/ | ||
public class StringBasedAggregationOperationUnitTests { | ||
|
||
@ParameterizedTest // GH-4712 | ||
@ValueSource(strings = { "$project", "'$project'", "\"$project\"" }) | ||
void extractsAggregationOperatorFromAggregationStringWithoutBindingParameters(String operator) { | ||
|
||
StringAggregationOperation agg = new StringAggregationOperation("{ %s : { 'fn' : 1 } }".formatted(operator), | ||
Object.class, (it) -> Assertions.fail("o_O Parameter binding")); | ||
|
||
assertThat(agg.getOperator()).isEqualTo("$project"); | ||
} | ||
|
||
@Test | ||
// GH-4712 | ||
void fallbackToParameterBindingIfAggregationOperatorCannotBeExtractedFromAggregationStringWithoutBindingParameters() { | ||
|
||
StringAggregationOperation agg = new StringAggregationOperation("{ happy-madison : { 'fn' : 1 } }", Object.class, | ||
(it) -> new Document("$project", "")); | ||
|
||
assertThat(agg.getOperator()).isEqualTo("$project"); | ||
} | ||
} |