forked from spring-projects/spring-data-mongodb
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spring-projectsGH-3726: Add support for $sampleRate match expression.
Closes spring-projects#3726
- Loading branch information
1 parent
45971b2
commit caff79f
Showing
6 changed files
with
200 additions
and
3 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
46 changes: 46 additions & 0 deletions
46
...godb/src/main/java/org/springframework/data/mongodb/core/aggregation/MatchExpression.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,46 @@ | ||
/* | ||
* Copyright 2015-2021 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.core.aggregation; | ||
|
||
import org.bson.Document; | ||
import org.springframework.data.mongodb.MongoExpression; | ||
|
||
/** | ||
* A {@link MatchExpression} can be used within the {@code match} aggregation pipeline stage. | ||
* | ||
* @author James McNee | ||
*/ | ||
public interface MatchExpression extends MongoExpression { | ||
|
||
/** | ||
* Obtain the as is (unmapped) representation of the {@link MatchExpression}. Use {@link #toDocument(AggregationOperationContext)} | ||
* with a matching {@link AggregationOperationContext context} to engage domain type mapping including field name resolution. | ||
* | ||
* @see org.springframework.data.mongodb.MongoExpression#toDocument() | ||
*/ | ||
@Override | ||
default Document toDocument() { | ||
return toDocument(Aggregation.DEFAULT_CONTEXT); | ||
} | ||
|
||
/** | ||
* Turns the {@link MatchExpression} into a {@link Document} within the given {@link AggregationOperationContext}. | ||
* | ||
* @param context must not be {@literal null}. | ||
* @return the MongoDB native ({@link Document}) form of the expression. | ||
*/ | ||
Document toDocument(AggregationOperationContext context); | ||
} |
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
62 changes: 62 additions & 0 deletions
62
...db/src/main/java/org/springframework/data/mongodb/core/aggregation/SamplingOperators.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,62 @@ | ||
/* | ||
* Copyright 2016-2021 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.core.aggregation; | ||
|
||
import org.bson.Document; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Gateway to {@literal sampling expressions} that match documents using a provided criteria. | ||
* | ||
* @author James McNee | ||
* @since 3.2.4 | ||
*/ | ||
public class SamplingOperators { | ||
|
||
/** | ||
* Encapsulates the aggregation framework {@code $sampleRate} operator. | ||
* | ||
* @author James McNee | ||
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/sampleRate/"> | ||
* https://docs.mongodb.com/manual/reference/operator/aggregation/sampleRate/</a> | ||
*/ | ||
public static class SampleRate implements MatchExpression { | ||
|
||
private final double sampleRate; | ||
|
||
private SampleRate(double sampleRate) { | ||
this.sampleRate = sampleRate; | ||
} | ||
|
||
/** | ||
* Creates new {@link SamplingOperators.SampleRate}. | ||
* | ||
* @param sampleRate sample rate to determine number of documents to be randomly selected from the input. | ||
* @return new instance of {@link SamplingOperators.SampleRate}. | ||
*/ | ||
public static SampleRate sampleRate(double sampleRate) { | ||
Assert.isTrue(sampleRate >= 0, "Sample rate must be greater than zero!"); | ||
Assert.isTrue(sampleRate <= 1, "Sample rate must not be greater than one!"); | ||
|
||
return new SampleRate(sampleRate); | ||
} | ||
|
||
@Override | ||
public Document toDocument(AggregationOperationContext context) { | ||
return new Document("$sampleRate", sampleRate); | ||
} | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
...est/java/org/springframework/data/mongodb/core/aggregation/SamplingOperatorsUnitTest.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,46 @@ | ||
/* | ||
* Copyright 2018-2021 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.core.aggregation; | ||
|
||
import org.bson.Document; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.data.mongodb.core.aggregation.SamplingOperators.SampleRate; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
/** | ||
* Unit tests for {@link SamplingOperators}. | ||
* | ||
* @author James McNee | ||
*/ | ||
public class SamplingOperatorsUnitTest { | ||
|
||
@Test // GH-3726 | ||
public void shouldRejectNegativeSampleRate() { | ||
assertThatIllegalArgumentException().isThrownBy(() -> SampleRate.sampleRate(-1.0)); | ||
} | ||
|
||
@Test // GH-3726 | ||
public void shouldRejectSampleRateGreaterThanOne() { | ||
assertThatIllegalArgumentException().isThrownBy(() -> SampleRate.sampleRate(1.1)); | ||
} | ||
|
||
@Test // GH-3726 | ||
public void shouldCreateSampleRateExpression() { | ||
assertThat(SampleRate.sampleRate(0.34).toDocument()).isEqualTo(Document.parse("{ $sampleRate: 0.34 }")); | ||
} | ||
|
||
} |